0N/A/*
2362N/A * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.awt.image;
0N/A
0N/Aimport java.awt.color.ColorSpace;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.RenderingHints;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport sun.awt.image.ImagingLib;
0N/A
0N/A/**
0N/A * This class implements a lookup operation from the source
0N/A * to the destination. The LookupTable object may contain a single array
0N/A * or multiple arrays, subject to the restrictions below.
0N/A * <p>
0N/A * For Rasters, the lookup operates on bands. The number of
0N/A * lookup arrays may be one, in which case the same array is
0N/A * applied to all bands, or it must equal the number of Source
0N/A * Raster bands.
0N/A * <p>
0N/A * For BufferedImages, the lookup operates on color and alpha components.
0N/A * The number of lookup arrays may be one, in which case the
0N/A * same array is applied to all color (but not alpha) components.
0N/A * Otherwise, the number of lookup arrays may
0N/A * equal the number of Source color components, in which case no
0N/A * lookup of the alpha component (if present) is performed.
0N/A * If neither of these cases apply, the number of lookup arrays
0N/A * must equal the number of Source color components plus alpha components,
0N/A * in which case lookup is performed for all color and alpha components.
0N/A * This allows non-uniform rescaling of multi-band BufferedImages.
0N/A * <p>
0N/A * BufferedImage sources with premultiplied alpha data are treated in the same
0N/A * manner as non-premultiplied images for purposes of the lookup. That is,
0N/A * the lookup is done per band on the raw data of the BufferedImage source
0N/A * without regard to whether the data is premultiplied. If a color conversion
0N/A * is required to the destination ColorModel, the premultiplied state of
0N/A * both source and destination will be taken into account for this step.
0N/A * <p>
0N/A * Images with an IndexColorModel cannot be used.
0N/A * <p>
0N/A * If a RenderingHints object is specified in the constructor, the
0N/A * color rendering hint and the dithering hint may be used when color
0N/A * conversion is required.
0N/A * <p>
0N/A * This class allows the Source to be the same as the Destination.
0N/A *
0N/A * @see LookupTable
0N/A * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
0N/A * @see java.awt.RenderingHints#KEY_DITHERING
0N/A */
0N/A
0N/Apublic class LookupOp implements BufferedImageOp, RasterOp {
0N/A private LookupTable ltable;
0N/A private int numComponents;
0N/A RenderingHints hints;
0N/A
0N/A /**
0N/A * Constructs a <code>LookupOp</code> object given the lookup
0N/A * table and a <code>RenderingHints</code> object, which might
0N/A * be <code>null</code>.
0N/A * @param lookup the specified <code>LookupTable</code>
0N/A * @param hints the specified <code>RenderingHints</code>,
0N/A * or <code>null</code>
0N/A */
0N/A public LookupOp(LookupTable lookup, RenderingHints hints) {
0N/A this.ltable = lookup;
0N/A this.hints = hints;
0N/A numComponents = ltable.getNumComponents();
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>LookupTable</code>.
0N/A * @return the <code>LookupTable</code> of this
0N/A * <code>LookupOp</code>.
0N/A */
0N/A public final LookupTable getTable() {
0N/A return ltable;
0N/A }
0N/A
0N/A /**
0N/A * Performs a lookup operation on a <code>BufferedImage</code>.
0N/A * If the color model in the source image is not the same as that
0N/A * in the destination image, the pixels will be converted
0N/A * in the destination. If the destination image is <code>null</code>,
0N/A * a <code>BufferedImage</code> will be created with an appropriate
0N/A * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
0N/A * might be thrown if the number of arrays in the
0N/A * <code>LookupTable</code> does not meet the restrictions
0N/A * stated in the class comment above, or if the source image
0N/A * has an <code>IndexColorModel</code>.
0N/A * @param src the <code>BufferedImage</code> to be filtered
0N/A * @param dst the <code>BufferedImage</code> in which to
0N/A * store the results of the filter operation
0N/A * @return the filtered <code>BufferedImage</code>.
0N/A * @throws IllegalArgumentException if the number of arrays in the
0N/A * <code>LookupTable</code> does not meet the restrictions
0N/A * described in the class comments, or if the source image
0N/A * has an <code>IndexColorModel</code>.
0N/A */
0N/A public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
0N/A ColorModel srcCM = src.getColorModel();
0N/A int numBands = srcCM.getNumColorComponents();
0N/A ColorModel dstCM;
0N/A if (srcCM instanceof IndexColorModel) {
0N/A throw new
0N/A IllegalArgumentException("LookupOp cannot be "+
0N/A "performed on an indexed image");
0N/A }
0N/A int numComponents = ltable.getNumComponents();
0N/A if (numComponents != 1 &&
0N/A numComponents != srcCM.getNumComponents() &&
0N/A numComponents != srcCM.getNumColorComponents())
0N/A {
0N/A throw new IllegalArgumentException("Number of arrays in the "+
0N/A " lookup table ("+
0N/A numComponents+
0N/A " is not compatible with the "+
0N/A " src image: "+src);
0N/A }
0N/A
0N/A
0N/A boolean needToConvert = false;
0N/A
0N/A int width = src.getWidth();
0N/A int height = src.getHeight();
0N/A
0N/A if (dst == null) {
0N/A dst = createCompatibleDestImage(src, null);
0N/A dstCM = srcCM;
0N/A }
0N/A else {
0N/A if (width != dst.getWidth()) {
0N/A throw new
0N/A IllegalArgumentException("Src width ("+width+
0N/A ") not equal to dst width ("+
0N/A dst.getWidth()+")");
0N/A }
0N/A if (height != dst.getHeight()) {
0N/A throw new
0N/A IllegalArgumentException("Src height ("+height+
0N/A ") not equal to dst height ("+
0N/A dst.getHeight()+")");
0N/A }
0N/A
0N/A dstCM = dst.getColorModel();
0N/A if (srcCM.getColorSpace().getType() !=
0N/A dstCM.getColorSpace().getType())
0N/A {
0N/A needToConvert = true;
0N/A dst = createCompatibleDestImage(src, null);
0N/A }
0N/A
0N/A }
0N/A
0N/A BufferedImage origDst = dst;
0N/A
0N/A if (ImagingLib.filter(this, src, dst) == null) {
0N/A // Do it the slow way
0N/A WritableRaster srcRaster = src.getRaster();
0N/A WritableRaster dstRaster = dst.getRaster();
0N/A
0N/A if (srcCM.hasAlpha()) {
0N/A if (numBands-1 == numComponents || numComponents == 1) {
0N/A int minx = srcRaster.getMinX();
0N/A int miny = srcRaster.getMinY();
0N/A int[] bands = new int[numBands-1];
0N/A for (int i=0; i < numBands-1; i++) {
0N/A bands[i] = i;
0N/A }
0N/A srcRaster =
0N/A srcRaster.createWritableChild(minx, miny,
0N/A srcRaster.getWidth(),
0N/A srcRaster.getHeight(),
0N/A minx, miny,
0N/A bands);
0N/A }
0N/A }
0N/A if (dstCM.hasAlpha()) {
0N/A int dstNumBands = dstRaster.getNumBands();
0N/A if (dstNumBands-1 == numComponents || numComponents == 1) {
0N/A int minx = dstRaster.getMinX();
0N/A int miny = dstRaster.getMinY();
0N/A int[] bands = new int[numBands-1];
0N/A for (int i=0; i < numBands-1; i++) {
0N/A bands[i] = i;
0N/A }
0N/A dstRaster =
0N/A dstRaster.createWritableChild(minx, miny,
0N/A dstRaster.getWidth(),
0N/A dstRaster.getHeight(),
0N/A minx, miny,
0N/A bands);
0N/A }
0N/A }
0N/A
0N/A filter(srcRaster, dstRaster);
0N/A }
0N/A
0N/A if (needToConvert) {
0N/A // ColorModels are not the same
0N/A ColorConvertOp ccop = new ColorConvertOp(hints);
0N/A ccop.filter(dst, origDst);
0N/A }
0N/A
0N/A return origDst;
0N/A }
0N/A
0N/A /**
0N/A * Performs a lookup operation on a <code>Raster</code>.
0N/A * If the destination <code>Raster</code> is <code>null</code>,
0N/A * a new <code>Raster</code> will be created.
0N/A * The <code>IllegalArgumentException</code> might be thrown
0N/A * if the source <code>Raster</code> and the destination
0N/A * <code>Raster</code> do not have the same
0N/A * number of bands or if the number of arrays in the
0N/A * <code>LookupTable</code> does not meet the
0N/A * restrictions stated in the class comment above.
0N/A * @param src the source <code>Raster</code> to filter
0N/A * @param dst the destination <code>WritableRaster</code> for the
0N/A * filtered <code>src</code>
0N/A * @return the filtered <code>WritableRaster</code>.
0N/A * @throws IllegalArgumentException if the source and destinations
0N/A * rasters do not have the same number of bands, or the
0N/A * number of arrays in the <code>LookupTable</code> does
0N/A * not meet the restrictions described in the class comments.
0N/A *
0N/A */
0N/A public final WritableRaster filter (Raster src, WritableRaster dst) {
0N/A int numBands = src.getNumBands();
0N/A int dstLength = dst.getNumBands();
0N/A int height = src.getHeight();
0N/A int width = src.getWidth();
0N/A int srcPix[] = new int[numBands];
0N/A
0N/A // Create a new destination Raster, if needed
0N/A
0N/A if (dst == null) {
0N/A dst = createCompatibleDestRaster(src);
0N/A }
0N/A else if (height != dst.getHeight() || width != dst.getWidth()) {
0N/A throw new
0N/A IllegalArgumentException ("Width or height of Rasters do not "+
0N/A "match");
0N/A }
0N/A dstLength = dst.getNumBands();
0N/A
0N/A if (numBands != dstLength) {
0N/A throw new
0N/A IllegalArgumentException ("Number of channels in the src ("
0N/A + numBands +
0N/A ") does not match number of channels"
0N/A + " in the destination ("
0N/A + dstLength + ")");
0N/A }
0N/A int numComponents = ltable.getNumComponents();
0N/A if (numComponents != 1 && numComponents != src.getNumBands()) {
0N/A throw new IllegalArgumentException("Number of arrays in the "+
0N/A " lookup table ("+
0N/A numComponents+
0N/A " is not compatible with the "+
0N/A " src Raster: "+src);
0N/A }
0N/A
0N/A
0N/A if (ImagingLib.filter(this, src, dst) != null) {
0N/A return dst;
0N/A }
0N/A
0N/A // Optimize for cases we know about
0N/A if (ltable instanceof ByteLookupTable) {
0N/A byteFilter ((ByteLookupTable) ltable, src, dst,
0N/A width, height, numBands);
0N/A }
0N/A else if (ltable instanceof ShortLookupTable) {
0N/A shortFilter ((ShortLookupTable) ltable, src, dst, width,
0N/A height, numBands);
0N/A }
0N/A else {
0N/A // Not one we recognize so do it slowly
0N/A int sminX = src.getMinX();
0N/A int sY = src.getMinY();
0N/A int dminX = dst.getMinX();
0N/A int dY = dst.getMinY();
0N/A for (int y=0; y < height; y++, sY++, dY++) {
0N/A int sX = sminX;
0N/A int dX = dminX;
0N/A for (int x=0; x < width; x++, sX++, dX++) {
0N/A // Find data for all bands at this x,y position
0N/A src.getPixel(sX, sY, srcPix);
0N/A
0N/A // Lookup the data for all bands at this x,y position
0N/A ltable.lookupPixel(srcPix, srcPix);
0N/A
0N/A // Put it back for all bands
0N/A dst.setPixel(dX, dY, srcPix);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return dst;
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounding box of the filtered destination image. Since
0N/A * this is not a geometric operation, the bounding box does not
0N/A * change.
0N/A * @param src the <code>BufferedImage</code> to be filtered
0N/A * @return the bounds of the filtered definition image.
0N/A */
0N/A public final Rectangle2D getBounds2D (BufferedImage src) {
0N/A return getBounds2D(src.getRaster());
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounding box of the filtered destination Raster. Since
0N/A * this is not a geometric operation, the bounding box does not
0N/A * change.
0N/A * @param src the <code>Raster</code> to be filtered
0N/A * @return the bounds of the filtered definition <code>Raster</code>.
0N/A */
0N/A public final Rectangle2D getBounds2D (Raster src) {
0N/A return src.getBounds();
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Creates a zeroed destination image with the correct size and number of
0N/A * bands. If destCM is <code>null</code>, an appropriate
0N/A * <code>ColorModel</code> will be used.
0N/A * @param src Source image for the filter operation.
0N/A * @param destCM the destination's <code>ColorModel</code>, which
0N/A * can be <code>null</code>.
0N/A * @return a filtered destination <code>BufferedImage</code>.
0N/A */
0N/A public BufferedImage createCompatibleDestImage (BufferedImage src,
0N/A ColorModel destCM) {
0N/A BufferedImage image;
0N/A int w = src.getWidth();
0N/A int h = src.getHeight();
0N/A int transferType = DataBuffer.TYPE_BYTE;
0N/A if (destCM == null) {
0N/A ColorModel cm = src.getColorModel();
0N/A Raster raster = src.getRaster();
0N/A if (cm instanceof ComponentColorModel) {
0N/A DataBuffer db = raster.getDataBuffer();
0N/A boolean hasAlpha = cm.hasAlpha();
0N/A boolean isPre = cm.isAlphaPremultiplied();
0N/A int trans = cm.getTransparency();
0N/A int[] nbits = null;
0N/A if (ltable instanceof ByteLookupTable) {
0N/A if (db.getDataType() == db.TYPE_USHORT) {
0N/A // Dst raster should be of type byte
0N/A if (hasAlpha) {
0N/A nbits = new int[2];
0N/A if (trans == cm.BITMASK) {
0N/A nbits[1] = 1;
0N/A }
0N/A else {
0N/A nbits[1] = 8;
0N/A }
0N/A }
0N/A else {
0N/A nbits = new int[1];
0N/A }
0N/A nbits[0] = 8;
0N/A }
0N/A // For byte, no need to change the cm
0N/A }
0N/A else if (ltable instanceof ShortLookupTable) {
0N/A transferType = DataBuffer.TYPE_USHORT;
0N/A if (db.getDataType() == db.TYPE_BYTE) {
0N/A if (hasAlpha) {
0N/A nbits = new int[2];
0N/A if (trans == cm.BITMASK) {
0N/A nbits[1] = 1;
0N/A }
0N/A else {
0N/A nbits[1] = 16;
0N/A }
0N/A }
0N/A else {
0N/A nbits = new int[1];
0N/A }
0N/A nbits[0] = 16;
0N/A }
0N/A }
0N/A if (nbits != null) {
0N/A cm = new ComponentColorModel(cm.getColorSpace(),
0N/A nbits, hasAlpha, isPre,
0N/A trans, transferType);
0N/A }
0N/A }
0N/A image = new BufferedImage(cm,
0N/A cm.createCompatibleWritableRaster(w, h),
0N/A cm.isAlphaPremultiplied(),
0N/A null);
0N/A }
0N/A else {
0N/A image = new BufferedImage(destCM,
0N/A destCM.createCompatibleWritableRaster(w,
0N/A h),
0N/A destCM.isAlphaPremultiplied(),
0N/A null);
0N/A }
0N/A
0N/A return image;
0N/A }
0N/A
0N/A /**
0N/A * Creates a zeroed-destination <code>Raster</code> with the
0N/A * correct size and number of bands, given this source.
0N/A * @param src the <code>Raster</code> to be transformed
0N/A * @return the zeroed-destination <code>Raster</code>.
0N/A */
0N/A public WritableRaster createCompatibleDestRaster (Raster src) {
0N/A return src.createCompatibleWritableRaster();
0N/A }
0N/A
0N/A /**
0N/A * Returns the location of the destination point given a
0N/A * point in the source. If <code>dstPt</code> is not
0N/A * <code>null</code>, it will be used to hold the return value.
0N/A * Since this is not a geometric operation, the <code>srcPt</code>
0N/A * will equal the <code>dstPt</code>.
0N/A * @param srcPt a <code>Point2D</code> that represents a point
0N/A * in the source image
0N/A * @param dstPt a <code>Point2D</code>that represents the location
0N/A * in the destination
0N/A * @return the <code>Point2D</code> in the destination that
0N/A * corresponds to the specified point in the source.
0N/A */
0N/A public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
0N/A if (dstPt == null) {
0N/A dstPt = new Point2D.Float();
0N/A }
0N/A dstPt.setLocation(srcPt.getX(), srcPt.getY());
0N/A
0N/A return dstPt;
0N/A }
0N/A
0N/A /**
0N/A * Returns the rendering hints for this op.
0N/A * @return the <code>RenderingHints</code> object associated
0N/A * with this op.
0N/A */
0N/A public final RenderingHints getRenderingHints() {
0N/A return hints;
0N/A }
0N/A
0N/A private final void byteFilter(ByteLookupTable lookup, Raster src,
0N/A WritableRaster dst,
0N/A int width, int height, int numBands) {
0N/A int[] srcPix = null;
0N/A
0N/A // Find the ref to the table and the offset
0N/A byte[][] table = lookup.getTable();
0N/A int offset = lookup.getOffset();
0N/A int tidx;
0N/A int step=1;
0N/A
0N/A // Check if it is one lookup applied to all bands
0N/A if (table.length == 1) {
0N/A step=0;
0N/A }
0N/A
0N/A int x;
0N/A int y;
0N/A int band;
0N/A int len = table[0].length;
0N/A
0N/A // Loop through the data
0N/A for ( y=0; y < height; y++) {
0N/A tidx = 0;
0N/A for ( band=0; band < numBands; band++, tidx+=step) {
0N/A // Find data for this band, scanline
0N/A srcPix = src.getSamples(0, y, width, 1, band, srcPix);
0N/A
0N/A for ( x=0; x < width; x++) {
0N/A int index = srcPix[x]-offset;
0N/A if (index < 0 || index > len) {
0N/A throw new
0N/A IllegalArgumentException("index ("+index+
0N/A "(out of range: "+
0N/A " srcPix["+x+
0N/A "]="+ srcPix[x]+
0N/A " offset="+ offset);
0N/A }
0N/A // Do the lookup
0N/A srcPix[x] = table[tidx][index];
0N/A }
0N/A // Put it back
0N/A dst.setSamples(0, y, width, 1, band, srcPix);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private final void shortFilter(ShortLookupTable lookup, Raster src,
0N/A WritableRaster dst,
0N/A int width, int height, int numBands) {
0N/A int band;
0N/A int[] srcPix = null;
0N/A
0N/A // Find the ref to the table and the offset
0N/A short[][] table = lookup.getTable();
0N/A int offset = lookup.getOffset();
0N/A int tidx;
0N/A int step=1;
0N/A
0N/A // Check if it is one lookup applied to all bands
0N/A if (table.length == 1) {
0N/A step=0;
0N/A }
0N/A
0N/A int x = 0;
0N/A int y = 0;
0N/A int index;
0N/A int maxShort = (1<<16)-1;
0N/A // Loop through the data
0N/A for (y=0; y < height; y++) {
0N/A tidx = 0;
0N/A for ( band=0; band < numBands; band++, tidx+=step) {
0N/A // Find data for this band, scanline
0N/A srcPix = src.getSamples(0, y, width, 1, band, srcPix);
0N/A
0N/A for ( x=0; x < width; x++) {
0N/A index = srcPix[x]-offset;
0N/A if (index < 0 || index > maxShort) {
0N/A throw new
0N/A IllegalArgumentException("index out of range "+
0N/A index+" x is "+x+
0N/A "srcPix[x]="+srcPix[x]
0N/A +" offset="+ offset);
0N/A }
0N/A // Do the lookup
0N/A srcPix[x] = table[tidx][index];
0N/A }
0N/A // Put it back
0N/A dst.setSamples(0, y, width, 1, band, srcPix);
0N/A }
0N/A }
0N/A }
0N/A}