home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 16.8 KB | 464 lines |
- /*
- * @(#)LookupOp.java 1.37 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
-
- package java.awt.image;
-
- import java.awt.GraphicsEnvironment;
- import java.awt.color.ColorSpace;
- import java.awt.geom.Rectangle2D;
- import java.awt.Rectangle;
- import java.awt.geom.Point2D;
-
- /**
- * This class implements a lookup operation from the source
- * to the destination. The LookupTable object may contain a single array
- * or multiple arrays, subject to the restrictions below.
- * <p>
- * For Rasters, the lookup operates on channel elements. The number of
- * lookup arrays may be one, in which case the same array is
- * applied to all channels, or it must equal the number of Source
- * Raster channels.
- * <p>
- * For BufferedImages, the lookup operates on color and alpha components.
- * The number of lookup arrays may be one, in which case the
- * same array is applied to all color and alpha components, or it
- * must equal the number of Source color components, in which case no
- * lookup of the alpha component (if present) is performed, or it
- * must equal the number of Source color components plus alpha components,
- * in which case lookup is performed for all color and alpha components.
- * This allows non-uniform rescaling of multi-channelled BufferedImages.
- *
- * Images with an IndexColorModel cannot be used.
- * <p>
- * This class allows the Source to be the same as Destination.
- *
- * @version 10 Feb 1997
- * @see LookupTable
- */
-
- public class LookupOp implements BufferedImageOp, RasterOp {
- private LookupTable ltable;
-
- /**
- * Constructs a LookupOp object given the lookup table.
- */
- public LookupOp(LookupTable lookup) {
- this.ltable = lookup;
- }
-
- /**
- * Returns the LookupTable.
- */
- public final LookupTable getLookupTable() {
- return ltable;
- }
-
- /**
- * Performs a lookup operation on a BufferedImage.
- * If the color model in the source image is not the same as that
- * in the destination image, the pixels will be converted
- * in the destination. If the destination image is null,
- * a BufferedImage will be created with the source ColorModel.
- * The IllegalArgumentException may be thrown if the number of
- * arrays in the LookupTable does not meet the restrictions
- * stated in the class comment above.
- */
- public BufferedImage filter (BufferedImage src, BufferedImage dst) {
- ColorModel srcCM = src.getColorModel();
- int numBands = srcCM.getNumColorComponents();
- if (srcCM instanceof IndexColorModel) {
- throw new
- IllegalArgumentException("LookupOp cannot be "+
- "performed on an indexed image");
- }
- boolean needToConvert = false;
-
- int width = src.getWidth();
- int height = src.getHeight();
-
- if (dst == null) {
- dst = createCompatibleDestImage(src, null);
- }
- else {
- if (width != dst.getWidth()) {
- throw new
- IllegalArgumentException("Src width ("+width+
- ") not equal to dst width ("+
- dst.getWidth()+")");
- }
- if (height != dst.getHeight()) {
- throw new
- IllegalArgumentException("Src height ("+height+
- ") not equal to dst height ("+
- dst.getHeight()+")");
- }
-
- ColorModel dstCM = dst.getColorModel();
- if (srcCM.getColorSpace().getType() !=
- dstCM.getColorSpace().getType())
- {
- needToConvert = true;
- dst = createCompatibleDestImage(src, null);
- }
-
- }
-
- BufferedImage origDst = dst;
- GraphicsEnvironment ge =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- ImagingLib imlib = ge.getImagingLib();
-
- if (imlib.filter(this, src, dst) == null) {
- if (srcCM instanceof ComponentColorModel) {
- filter(src.getRaster(), dst.getRaster());
- }
- else {
- // Do it the slow way
- WritableRaster srcRaster = src.getRaster();
- WritableRaster dstRaster = dst.getRaster();
- int[] data = null;
- int[] components = null;
-
- int sminX = src.getMinXCoord();
- int sX;
- int sY = src.getMinYCoord();
- int dminX = dst.getMinXCoord();
- int dX;
- int dY = dst.getMinYCoord();
- for (int y=0; y < height; y++, sY++, dY++) {
- sX = sminX;
- dX = dminX;
- for (int x = 0; x < width; x++, sX++, dX++) {
- data = srcRaster.getPixel(sX, sY, data);
- // Lookup the data for all bands at this x,y position
- ltable.lookupPixel(data, data);
-
- dstRaster.setPixel(dX, dY, data);
- }
- }
- }
- }
-
- if (needToConvert) {
- // ColorModels are not the same
- ColorConvertOp ccop = new ColorConvertOp();
- ccop.filter(dst, origDst);
- }
-
- return origDst;
- }
-
- /**
- * Performs a lookup operation on a Raster. If the destination Raster is
- * null, a new Raster will be created.
- * The IllegalArgumentException may be thrown if the source and
- * destination Rasters have different number of channels or if
- * the number of arrays in the LookupTable does not meet the
- * restrictions stated in the class comment above.
- */
- public WritableRaster filter (Raster src, WritableRaster dst) {
- int x, y, band;
- int numBands = src.getNumBands();
- int dstLength = dst.getNumBands();
- int height = src.getHeight();
- int width = src.getWidth();
- int srcPix[] = new int[width];
-
- // Create a new destination Raster, if needed
- if (dst == null) {
- dst = createCompatibleDestRaster(src);
- }
- else if (height != dst.getHeight() || width != dst.getWidth()) {
- throw new
- IllegalArgumentException ("Width or height of Rasters do not "+
- "match");
- }
- dstLength = dst.getNumBands();
-
- if (numBands != dstLength) {
- throw new
- IllegalArgumentException ("Number of channels in the src ("
- + numBands +
- ") does not match number of channels"
- + " in the destination ("
- + dstLength + ")");
- }
-
- // Optimize for cases we know about
- if (ltable instanceof ByteLookupTable) {
- byteFilter ((ByteLookupTable) ltable, src, dst,
- width, height, numBands);
- }
- else if (ltable instanceof ShortLookupTable) {
- shortFilter ((ShortLookupTable) ltable, src, dst, width,
- height, numBands);
- }
- else {
- // Not one we recognize so do it slowly
- int sminX = src.getMinX();
- int sX;
- int sY = src.getMinY();
- int dminX = dst.getMinX();
- int dX;
- int dY = dst.getMinY();
- for (y=0; y < height; y++, sY++, dY++) {
- sX = sminX;
- dX = dminX;
- for (x=0; x < width; x++, sX++, dX++) {
- // Find data for all bands at this x,y position
- src.getPixel(sX, sY, srcPix);
-
- // Lookup the data for all bands at this x,y position
- ltable.lookupPixel(srcPix, srcPix);
-
- // Put it back for all bands
- dst.setPixel(dX, dY, srcPix);
- }
- }
- }
-
- return dst;
- }
-
- /**
- * Returns the bounding box of the destination. Since
- * this is not a geometric operation, the bounding box does not
- * change.
- * The IllegalArgumentException may be thrown if the number of
- * arrays in the LookupTable does not meet the restrictions
- * stated in the class comment above.
- */
- public Rectangle2D getDestBounds (BufferedImage src) {
- return getDestBounds(src.getRaster());
- }
-
- /**
- * Returns the bounding box of the destination. Since
- * this is not a geometric operation, the bounding box does not
- * change.
- * The IllegalArgumentException may be thrown if the number of
- * arrays in the LookupTable does not meet the restrictions
- * stated in the class comment above.
- */
- public Rectangle2D getDestBounds (Raster src) {
- /* return new Rectangle (src.getXOffset(),
- src.getYOffset(),
- src.getWidth(), src.getHeight()); */
- return src.getBounds();
-
- }
-
- /**
- * Creates an empty destination image with the correct size and number of
- * channels.
- * The IllegalArgumentException may be thrown if the number of
- * arrays in the LookupTable does not meet the restrictions
- * stated in the class comment above.
- * @param src Source image for the filter operation.
- * @param destCM ColorModel of the destination. If null, the
- * ColorModel of the source will be used.
- */
- public BufferedImage createCompatibleDestImage (BufferedImage src,
- ColorModel destCM) {
- BufferedImage image;
- int w = src.getWidth();
- int h = src.getHeight();
- if (destCM == null) {
- ColorModel cm = src.getColorModel();
- Raster raster = src.getRaster();
- if (cm instanceof ComponentColorModel) {
- DataBuffer db = raster.getDataBuffer();
- boolean hasAlpha = cm.hasAlpha();
- boolean isPre = cm.isAlphaPremultiplied();
- int trans = cm.getTransparency();
- int[] nbits = null;
- if (ltable instanceof ByteLookupTable) {
- if (db.getDataType() == db.SHORT_DATA) {
- // Dst raster should be of type byte
- if (hasAlpha) {
- nbits = new int[2];
- if (trans == cm.BITMASK) {
- nbits[1] = 1;
- }
- else {
- nbits[1] = 8;
- }
- }
- else {
- nbits = new int[1];
- }
- nbits[0] = 8;
- }
- // For byte, no need to change the cm
- }
- else if (ltable instanceof ShortLookupTable) {
- if (db.getDataType() == db.BYTE_DATA) {
- if (hasAlpha) {
- nbits = new int[2];
- if (trans == cm.BITMASK) {
- nbits[1] = 1;
- }
- else {
- nbits[1] = 16;
- }
- }
- else {
- nbits = new int[1];
- }
- nbits[0] = 16;
- }
- }
- if (nbits != null) {
- cm = new ComponentColorModel(cm.getColorSpace(),
- nbits, hasAlpha, isPre,
- trans);
- }
- }
- image = new BufferedImage(cm,
- cm.createCompatibleWritableRaster(w, h),
- cm.isAlphaPremultiplied());
- }
- else {
- image = new BufferedImage(destCM,
- destCM.createCompatibleWritableRaster(w,
- h),
- destCM.isAlphaPremultiplied());
- }
-
- return image;
- }
-
- /**
- * Creates an empty destination Raster with the correct size and number of
- * channels.
- * The IllegalArgumentException may be thrown if the number of
- * arrays in the LookupTable does not meet the restrictions
- * stated in the class comment above.
- */
- public WritableRaster createCompatibleDestRaster (Raster src) {
- return src.createCompatibleWritableRaster();
- }
-
- /**
- * Returns the location of the destination point given a
- * point in the source image. If dstPt is non-null, it will
- * be used to hold the return value. Since this is not a geometric
- * operation, the srcPt will equal the dstPt.
- */
- public Point2D getDestPoint (Point2D srcPt, Point2D dstPt) {
- if (dstPt == null) {
- dstPt = new Point2D.Float();
- }
- dstPt.setLocation(srcPt.getX(), srcPt.getY());
-
- return dstPt;
- }
-
- private final void byteFilter(ByteLookupTable lookup, Raster src,
- WritableRaster dst,
- int width, int height, int numBands) {
- int[] srcPix = null;
-
- // Find the ref to the table and the offset
- byte[][] table = lookup.getDataStorage();
- int offset = lookup.getOffset();
- int tidx;
- int step=1;
-
- // Check if it is one lookup applied to all bands
- if (table.length == 1) {
- step=0;
- }
-
- int x;
- int y;
- int band;
- int len = table[0].length;
-
- // Loop through the data
- for ( y=0; y < height; y++) {
- tidx = 0;
- for ( band=0; band < numBands; band++, tidx+=step) {
- // Find data for this band, scanline
- srcPix = src.getSample(0, y, width, 1, band, srcPix);
-
- for ( x=0; x < width; x++) {
- int index = srcPix[x]-offset;
- if (index < 0 || index > len) {
- throw new
- IllegalArgumentException("index ("+index+
- "(out of range: "+
- " srcPix["+x+
- "]="+ srcPix[x]+
- " offset="+ offset);
- }
- // Do the lookup
- srcPix[x] = table[tidx][index];
- }
- // Put it back
- dst.setSample(0, y, width, 1, band, srcPix);
- }
- }
- }
-
- private final void shortFilter(ShortLookupTable lookup, Raster src,
- WritableRaster dst,
- int width, int height, int numBands) {
- int band;
- int[] srcPix = null;
-
- // Find the ref to the table and the offset
- short[][] table = lookup.getDataStorage();
- int offset = lookup.getOffset();
- int tidx;
- int step=1;
-
- // Check if it is one lookup applied to all bands
- if (table.length == 1) {
- step=0;
- }
-
- int x = 0;
- int y = 0;
- int index;
- int maxShort = (1<<16)-1;
- // Loop through the data
- for (y=0; y < height; y++) {
- tidx = 0;
- for ( band=0; band < numBands; band++, tidx+=step) {
- // Find data for this band, scanline
- srcPix = src.getSample(0, y, width, 1, band, srcPix);
-
- for ( x=0; x < width; x++) {
- index = srcPix[x]-offset;
- if (index < 0 || index > maxShort) {
- throw new
- IllegalArgumentException("index out of range "+
- index+" x is "+x+
- "srcPix[x]="+srcPix[x]
- +" offset="+ offset);
- }
- // Do the lookup
- srcPix[x] = table[tidx][index];
- }
- // Put it back
- dst.setSample(0, y, width, 1, band, srcPix);
- }
- }
- }
- }
-
-
-