/* * com.fc.vrml.image.ImageToSFImage * * 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. * * free for use and distribution, no modification without consent * disclaimer */ package com.fc.vrml.image; import vrml.field.SFImage; import vrml.field.ConstSFImage; import com.fc.vrml.image.ExternalSFImage; /** * This class is an object to convert an int array of Java (tm) image pixels to a 1, 2, 3, or 4 component SFImage or ConstSFImage. *

* Use the height, width, components, and pixel array as arguments to the methods to get VRML SFImage objects. * @see com.fc.vrml.image.ExternalSFImage * @version 0.5 * @author Ross A. Finlayson * @author FC VRML Group */ public class ImageToSFImage extends Object{ int numComponents=4; private byte[] fourbytes={0, 0, 0, 0}; private byte[] threebytes={0, 0, 0}; private byte[] twobytes={0, 0}; private byte[] onebyte={0}; byte[] sfpixels; public ImageToSFImage(){ numComponents=4; } public ImageToSFImage(int components){ switch(components){ case 1: numComponents=1; break; case 2: numComponents=2; break; case 3: numComponents=3; break; case 4: numComponents=4; break; } } // use this method to convert an int array of Java (tm) image pixels to VRML 2.0 SFImage and ConstSFImage forma /** * This method returns SFImage object from height, width, number of color components, and pixel array from Java image source. * @param height Height of Image * @param width Width of Image * @param pixels Pixel array of Image * @return SFImage */ public SFImage getSFImage(int width, int height, int numComponents, int[] jpixels){ height=height; width=width; numComponents=numComponents; int[] pixels=jpixels; switch(numComponents){ case 1: sfpixels=new byte[height*width]; for (int j=(height-1);j>-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i-1; j--){ for (int i=0;i> 24)& 0xff; // int red=(pixel >> 16)& 0xff; // int green=(pixel >> 8)& 0xff; // int blue=(pixel )& 0xff; // int vrmlpixel=((red << 24)| (green << 16) | (blue << 8)|(alpha)); // return vrmlpixel; // return vrmlpixel; // } // */ /** * This method returns a byte array containing the pixel data in SFImage format. *

* For a 1 component image, this returns one byte of the image intensity. *

* For a 2 component image, this returns two bytes, the first image intensity and the second transparency. *

* For a 3 component image, this returns three bytes, the red, green, and blue intensities. *

* For a 4 component image, this returns four bytes, the red, green, and blue intensities and alpha transparency. *

* @param pixel Pixel from default Java RGB ColorModel * @return byte array of pixel data */ public final byte[] pixelToBytes(int pixel){ switch(numComponents){ case 4: fourbytes[0]= (byte)((pixel >> 16) & 0xff);//red fourbytes[1]= (byte)((pixel >> 8) & 0xff); //green fourbytes[2]= (byte)(pixel & 0xff); //blue fourbytes[3]= (byte)((pixel >> 24) & 0xff); //alpha return fourbytes; case 3: threebytes[0]= (byte)((pixel >> 16) & 0xff); threebytes[1]= (byte)((pixel >> 8) & 0xff); threebytes[2]= (byte)(pixel & 0xff); return threebytes; case 2: twobytes[0]=(byte)( .2125 * ((pixel >> 16) & 0xff) + .7154 * ((pixel >> 8) & 0xff) + .0721 * (pixel & 0xff)); twobytes[1]=(byte)((pixel >> 24) & 0xff); return twobytes; case 1: onebyte[0]=(byte)( .2125 * ((pixel >> 16) & 0xff) + .7154 * ((pixel >> 8) & 0xff) + .0721 * (pixel & 0xff)); return onebyte; } return fourbytes; } }