/* * com.fc.vrml.image.ExternalSFImage.java * * Copyright (c) 1996-1998 Finlayson Consulting (FC). All Rights Reserved. * * FC grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) this software is not reverse engineereed without written * consent. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. FC AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL FC OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF FC HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. * */ package com.fc.vrml.image; import vrml.field.*; /** * This is a class to store SFImage data. Create ExternalSFImage then use methods for VRML 2.0 SFImage values. * @see com.fc.vrml.image.ImageToSFImage * @see com.fc.vrml.image.SFImageConverter * @version 0.5 * @author Ross A. Finlayson * @author FC VRML Group */ public class ExternalSFImage implements Cloneable{ private int _width=0; private int _height=0; private int _numComponents=0; private byte[] _pixels=null; public ExternalSFImage(){} /** * @param width width of SFImage * @param height height of SFImage * @param numComponents numComponents of SFImage * @param pixels pixel array of SFImage */ public ExternalSFImage(int width, int height, int numComponents, byte[] pixels){ _width=width; _height=height; _numComponents=numComponents; _pixels=pixels; } public ExternalSFImage(ConstSFImage constsfimage){ _width=constsfimage.getWidth(); _height=constsfimage.getHeight(); _numComponents=constsfimage.getComponents(); _pixels=new byte[_width*_height*_numComponents]; constsfimage.getPixels(_pixels); } public ExternalSFImage(SFImage sfimage){ _width=sfimage.getWidth(); _height=sfimage.getHeight(); _numComponents=sfimage.getComponents(); _pixels=new byte[_width*_height*_numComponents]; sfimage.getPixels(_pixels); } public ExternalSFImage(ExternalSFImage externalsfimage){ _width=externalsfimage.getWidth(); _height=externalsfimage.getHeight(); _numComponents=externalsfimage.getComponents(); _pixels=externalsfimage.getPixels(); } /** * @return width of SFImage */ public int getWidth(){ return _width; } /** * @return height of SFImage */ public int getHeight(){ return _height; } /** * @return color components of SFImage */ public int getComponents(){ return _numComponents; } /** * @return pixel array of SFImage */ public byte[] getPixels(){ return _pixels; } /** * @return ConstSFImage vrml.field.ConstSFImage */ public ConstSFImage getConstSFImage(){ return new ConstSFImage(_width, _height, _numComponents, _pixels); } /** * @return SFImage vrml.field.SFImage */ public SFImage getSFImage(){ return new SFImage(_width, _height, _numComponents, _pixels); } public void setValue(int width, int height, int numComponents, byte[] pixels){ _width=width; _height=height; _numComponents=numComponents; _pixels=pixels; } public void setValue(SFImage sfimage){ _width=sfimage.getWidth(); _height=sfimage.getHeight(); _numComponents=sfimage.getComponents(); _pixels=new byte[_width*_height*_numComponents]; sfimage.getPixels(_pixels); } public void setValue(ConstSFImage sfimage){ _width=sfimage.getWidth(); _height=sfimage.getHeight(); _numComponents=sfimage.getComponents(); _pixels=new byte[_width*_height*_numComponents]; sfimage.getPixels(_pixels); } public void setValue(ExternalSFImage sfimage){ _width=sfimage.getWidth(); _height=sfimage.getHeight(); _numComponents=sfimage.getComponents(); _pixels=sfimage.getPixels(); } public Object clone(){ return new ExternalSFImage(this); } public boolean equals(Object object){ if (object==null) return false; if (!(object instanceof ExternalSFImage)) return false; if (!(((ExternalSFImage)object).getWidth()==_width)) return false; if (!(((ExternalSFImage)object).getHeight()==_height)) return false; if (!(((ExternalSFImage)object).getComponents()==_numComponents)) return false; byte[] objectpixels=((ExternalSFImage)object).getPixels(); for (int i=0;i< (_width*_height*_numComponents);i++){ if (_pixels[i]!=objectpixels[i]) return false; } return true; } /** * @return VRML format SFImage field data */ public String toString(){ StringBuffer stringbuffer=new StringBuffer( (3 + 2*_numComponents)*(_height*_width)); int linelengthcount=0; stringbuffer.append(" "+_width+" "+_height+" "+_numComponents+" "); int hexals=2*_numComponents; for (int k=0;k<(_height*_width);k++){ stringbuffer.append('0'); int pixelmask=0xffffffff; switch (_numComponents){ case 1: pixelmask= ((_pixels[k] ) & 0x000000ff); break; case 2: pixelmask= (_pixels[k*2] << 8 & 0x0000ff00) | (_pixels[k*2+1] & 0x000000ff); break; case 3: pixelmask= (_pixels[k*3] << 16 & 0x00ff0000) | (_pixels[k*3+1] << 8 & 0x0000ff00) | (_pixels[k*3+2] & 0x000000ff); break; case 4: pixelmask= (_pixels[k*4] << 24 & 0xff000000) | (_pixels[k*4+1] << 16 & 0x00ff0000) | (_pixels[k*4+2] << 8 & 0x0000ff00) | (_pixels[k*4+3] ) & 0x000000ff; break; } boolean nonzero=false; for (int i=hexals-1;i>-1;i--){ switch ( (pixelmask>>>(i*4)) & 0x0f ){ case 0: if (nonzero==true)stringbuffer.append('0'); linelengthcount++; break; case 1: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('1'); linelengthcount++; break; case 2: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('2'); linelengthcount++; break; case 3: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('3'); linelengthcount++; break; case 4: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('4'); linelengthcount++; break; case 5: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('5'); linelengthcount++; break; case 6: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('6'); linelengthcount++; break; case 7: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('7'); linelengthcount++; break; case 8: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('8'); linelengthcount++; break; case 9: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('9'); linelengthcount++; break; case 10: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('A'); linelengthcount++; break; case 11: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('B'); linelengthcount++; break; case 12: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('C'); linelengthcount++; break; case 13: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('D'); linelengthcount++; break; case 14: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('E'); linelengthcount++; break; case 15: if (nonzero==false) {stringbuffer.append('x'); linelengthcount++; nonzero=true;} stringbuffer.append('F'); linelengthcount++; break; } } stringbuffer.append(' '); linelengthcount++; if (linelengthcount>76){ stringbuffer.append('\n'); linelengthcount=0; } } return stringbuffer.toString(); } }