FileDocCategorySizeDatePackage
ColorConvertOp.javaAPI DocAndroid 1.5 API25476Wed May 06 22:41:54 BST 2009java.awt.image

ColorConvertOp

public class ColorConvertOp extends Object implements BufferedImageOp, RasterOp
The ColorConvertOp class converts the pixels of the data in the source image with the specified ColorSpace objects or an array of ICC_Profile objects. The result pixels are scaled to the precision of the destination image.
since
Android 1.0

Fields Summary
RenderingHints
renderingHints
The rendering hints.
Object[]
conversionSequence
The conversion sequence.
private ICC_Profile[]
midProfiles
The mid profiles.
private final org.apache.harmony.awt.gl.color.ColorConverter
cc
The cc.
private final ICC_TransfomCreator
tCreator
The t creator.
private boolean
isICC
The is icc.
Constructors Summary
public ColorConvertOp(ColorSpace srcCS, ColorSpace dstCS, RenderingHints hints)
Instantiates a new ColorConvertOp object using two specified ColorSpace objects.

param
srcCS the source ColorSpace.
param
dstCS the destination ColorSpace.
param
hints the RenderingHints object used for the color conversion, or null.

        if (srcCS == null || dstCS == null) {
            throw new NullPointerException(Messages.getString("awt.25B")); //$NON-NLS-1$
        }

        renderingHints = hints;

        boolean srcICC = srcCS instanceof ICC_ColorSpace;
        boolean dstICC = dstCS instanceof ICC_ColorSpace;

        if (srcICC && dstICC) {
            conversionSequence = new ICC_Profile[2];
        } else {
            conversionSequence = new Object[2];
            isICC = false;
        }

        if (srcICC) {
            conversionSequence[0] = ((ICC_ColorSpace)srcCS).getProfile();
        } else {
            conversionSequence[0] = srcCS;
        }

        if (dstICC) {
            conversionSequence[1] = ((ICC_ColorSpace)dstCS).getProfile();
        } else {
            conversionSequence[1] = dstCS;
        }
    
public ColorConvertOp(ICC_Profile[] profiles, RenderingHints hints)
Instantiates a new ColorConvertOp object from the specified ICC_Profile objects.

param
profiles the array of ICC_Profile objects.
param
hints the RenderingHints object used for the color conversion, or null.

        if (profiles == null) {
            throw new NullPointerException(Messages.getString("awt.25C")); //$NON-NLS-1$
        }

        renderingHints = hints;

        // This array is not used in the program logic, so don't need to copy it
        // Store it only to return back
        midProfiles = profiles;

        conversionSequence = new ICC_Profile[midProfiles.length];

        // Add profiles to the conversion sequence
        for (int i = 0, length = midProfiles.length; i < length; i++) {
            conversionSequence[i] = midProfiles[i];
        }
    
public ColorConvertOp(ColorSpace cs, RenderingHints hints)
Instantiates a new ColorConvertOp object using the specified ColorSpace object.

param
cs the destination ColorSpace or an intermediate ColorSpace.
param
hints the RenderingHints object used for the color conversion, or null.

        if (cs == null) {
            throw new NullPointerException(Messages.getString("awt.25B")); //$NON-NLS-1$
        }

        renderingHints = hints;

        if (cs instanceof ICC_ColorSpace) {
            conversionSequence = new ICC_Profile[1];
            conversionSequence[0] = ((ICC_ColorSpace)cs).getProfile();
        } else {
            conversionSequence = new Object[1];
            conversionSequence[0] = cs;
            isICC = false;
        }
    
public ColorConvertOp(RenderingHints hints)
Instantiates a new ColorConvertOp object which converts from a source color space to a destination color space.

param
hints the RenderingHints object used for the color conversion, or null.

        renderingHints = hints;
    
Methods Summary
private voidapplySequence(java.lang.Object[] sequence, float[][] tmpData, java.awt.color.ColorSpace srcCS, java.awt.color.ColorSpace dstCS)
Apply sequence.

param
sequence the sequence.
param
tmpData the tmp data.
param
srcCS the src cs.
param
dstCS the dst cs.

        ColorSpace xyzCS = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);

        int numPixels = tmpData.length;

        // First transform...
        if (sequence[0] instanceof ICC_Transform) { // ICC
            ICC_Transform t = (ICC_Transform)sequence[0];
            cc.translateColor(t, tmpData, srcCS, xyzCS, numPixels);
        } else { // non ICC
            for (int k = 0; k < numPixels; k++) {
                tmpData[k] = srcCS.toCIEXYZ(tmpData[k]);
            }
            cc.loadScalingData(xyzCS); // prepare for scaling XYZ
        }

        for (Object element : sequence) {
            if (element instanceof ICC_Transform) {
                ICC_Transform t = (ICC_Transform)element;
                cc.translateColor(t, tmpData, null, null, numPixels);
            } else {
                ColorSpace cs = (ColorSpace)element;
                for (int k = 0; k < numPixels; k++) {
                    tmpData[k] = cs.fromCIEXYZ(tmpData[k]);
                    tmpData[k] = cs.toCIEXYZ(tmpData[k]);
                }
            }
        }

        // Last transform...
        if (sequence[sequence.length - 1] instanceof ICC_Transform) { // ICC
            ICC_Transform t = (ICC_Transform)sequence[sequence.length - 1];
            cc.translateColor(t, tmpData, xyzCS, dstCS, numPixels);
        } else { // non ICC
            for (int k = 0; k < numPixels; k++) {
                tmpData[k] = dstCS.fromCIEXYZ(tmpData[k]);
            }
        }
    
public java.awt.image.BufferedImagecreateCompatibleDestImage(java.awt.image.BufferedImage src, java.awt.image.ColorModel destCM)

        // If destination color model is passed only one line needed
        if (destCM != null) {
            return new BufferedImage(destCM, destCM.createCompatibleWritableRaster(src.getWidth(),
                    src.getHeight()), destCM.isAlphaPremultiplied(), null);
        }

        int nSpaces = conversionSequence.length;

        if (nSpaces < 1) {
            throw new IllegalArgumentException(Messages.getString("awt.261")); //$NON-NLS-1$
        }

        // Get destination color space
        Object destination = conversionSequence[nSpaces - 1];
        ColorSpace dstCS = (destination instanceof ColorSpace) ? (ColorSpace)destination
                : new ICC_ColorSpace((ICC_Profile)destination);

        ColorModel srcCM = src.getColorModel();
        ColorModel dstCM = new ComponentColorModel(dstCS, srcCM.hasAlpha(), srcCM
                .isAlphaPremultiplied(), srcCM.getTransparency(), srcCM.getTransferType());

        return new BufferedImage(dstCM, destCM.createCompatibleWritableRaster(src.getWidth(), src
                .getHeight()), destCM.isAlphaPremultiplied(), null);
    
public java.awt.image.WritableRastercreateCompatibleDestRaster(java.awt.image.Raster src)

        int nComps = 0;
        int nSpaces = conversionSequence.length;

        if (nSpaces < 2) {
            throw new IllegalArgumentException(Messages.getString("awt.261")); //$NON-NLS-1$
        }

        Object lastCS = conversionSequence[nSpaces - 1];
        if (lastCS instanceof ColorSpace) {
            nComps = ((ColorSpace)lastCS).getNumComponents();
        } else {
            nComps = ((ICC_Profile)lastCS).getNumComponents();
        }

        // Calculate correct data type
        int dstDataType = src.getDataBuffer().getDataType();
        if (dstDataType != DataBuffer.TYPE_BYTE && dstDataType != DataBuffer.TYPE_SHORT) {
            dstDataType = DataBuffer.TYPE_SHORT;
        }

        return Raster.createInterleavedRaster(dstDataType, src.getWidth(), src.getHeight(), nComps,
                new Point(src.getMinX(), src.getMinY()));
    
public final java.awt.image.WritableRasterfilter(java.awt.image.Raster src, java.awt.image.WritableRaster dst)

        if (conversionSequence.length < 2) {
            throw new IllegalArgumentException(Messages.getString("awt.25D")); //$NON-NLS-1$
        }

        ICC_Profile srcPf = null, dstPf = null; // unused if isICC is false
        int nSrcColorComps, nDstColorComps;
        Object first = conversionSequence[0];
        Object last = conversionSequence[conversionSequence.length - 1];

        // Get the number of input/output color components
        if (isICC) {
            srcPf = (ICC_Profile)first;
            dstPf = (ICC_Profile)last;
            nSrcColorComps = srcPf.getNumComponents();
            nDstColorComps = dstPf.getNumComponents();
        } else {
            if (first instanceof ICC_Profile) {
                srcPf = (ICC_Profile)first;
                nSrcColorComps = srcPf.getNumComponents();
            } else {
                nSrcColorComps = ((ColorSpace)first).getNumComponents();
            }

            if (last instanceof ICC_Profile) {
                dstPf = (ICC_Profile)last;
                nDstColorComps = dstPf.getNumComponents();
            } else {
                nDstColorComps = ((ColorSpace)last).getNumComponents();
            }
        }

        // Check that source and destination rasters are compatible with
        // transforms and with each other
        if (src.getNumBands() != nSrcColorComps) {
            // awt.25E=Incorrect number of source raster bands. Should be equal
            // to the number of color components of source colorspace.
            throw new IllegalArgumentException(Messages.getString("awt.25E")); //$NON-NLS-1$
        }

        if (dst != null) { // Check destination raster
            if (dst.getNumBands() != nDstColorComps) {
                // awt.25F=Incorrect number of destination raster bands. Should
                // be equal to the number of color components of destination
                // colorspace.
                throw new IllegalArgumentException(Messages.getString("awt.25F")); //$NON-NLS-1$
            }

            if (src.getWidth() != dst.getWidth() || src.getHeight() != dst.getHeight()) {
                throw new IllegalArgumentException(Messages.getString("awt.260")); //$NON-NLS-1$
            }

        } else {
            dst = createCompatibleDestRaster(src);
        }

        if (isICC) {
            // Create transform
            ICC_Transform t = tCreator
                    .getTransform(srcPf, dstPf, (ICC_Profile[])conversionSequence);
            cc.translateColor(t, src, dst);
        } else {
            Object[] sequence = tCreator.getSequence(null, null);

            // Get data from the source raster
            ColorScaler scaler = new ColorScaler();
            scaler.loadScalingData(src, null);
            float tmpData[][] = scaler.scaleNormalize(src);

            // Get source and destination color spaces
            ColorSpace srcCS = (srcPf == null) ? (ColorSpace)first : new ICC_ColorSpace(srcPf);
            ColorSpace dstCS = (dstPf == null) ? (ColorSpace)last : new ICC_ColorSpace(dstPf);

            applySequence(sequence, tmpData, srcCS, dstCS);

            scaler.loadScalingData(dst, null);
            scaler.unscaleNormalized(dst, tmpData);
        }

        return dst;
    
public final java.awt.image.BufferedImagefilter(java.awt.image.BufferedImage src, java.awt.image.BufferedImage dst)

        if (dst == null && conversionSequence.length < 1) {
            throw new IllegalArgumentException(Messages.getString("awt.262")); //$NON-NLS-1$
        }

        ColorModel srcCM = src.getColorModel();
        // First handle index color model
        if (srcCM instanceof IndexColorModel) {
            src = ((IndexColorModel)srcCM).convertToIntDiscrete(src.getRaster(), false);
        }
        ColorSpace srcCS = srcCM.getColorSpace();

        BufferedImage res;
        boolean isDstIndex = false;
        if (dst != null) {

            if (src.getWidth() != dst.getWidth() || src.getHeight() != dst.getHeight()) {
                throw new IllegalArgumentException(Messages.getString("awt.263")); //$NON-NLS-1$
            }

            if (dst.getColorModel() instanceof IndexColorModel) {
                isDstIndex = true;
                res = createCompatibleDestImage(src, null);
            } else {
                res = dst;
            }
        } else {
            res = createCompatibleDestImage(src, null);
        }
        ColorModel dstCM = res.getColorModel();
        ColorSpace dstCS = dstCM.getColorSpace();

        ICC_Profile srcPf = null, dstPf = null;
        if (srcCS instanceof ICC_ColorSpace) {
            srcPf = ((ICC_ColorSpace)srcCS).getProfile();
        }
        if (dstCS instanceof ICC_ColorSpace) {
            dstPf = ((ICC_ColorSpace)dstCS).getProfile();
        }

        boolean isFullICC = isICC && srcPf != null && dstPf != null;

        if (isFullICC) {
            ICC_Transform t = tCreator
                    .getTransform(srcPf, dstPf, (ICC_Profile[])conversionSequence);
            cc.translateColor(t, src, res);
        } else { // Perform non-ICC transform
            Object sequence[] = tCreator.getSequence(srcPf == null ? (Object)srcCS : srcPf,
                    dstPf == null ? (Object)dstCS : dstPf);

            int srcW = src.getWidth();
            int srcH = src.getHeight();
            int numPixels = srcW * srcH;

            // Load all pixel data into array tmpData
            float tmpData[][] = new float[numPixels][tCreator.maxComponents];
            for (int row = 0, dataPos = 0; row < srcW; row++) {
                for (int col = 0; col < srcH; col++) {
                    tmpData[dataPos] = srcCM.getNormalizedComponents(src.getRaster()
                            .getDataElements(row, col, null), tmpData[dataPos], 0);
                    dataPos++;
                }
            }

            // Copy alpha channel if needed
            float alpha[] = null;
            int alphaIdx = srcCM.numComponents - 1;
            if (srcCM.hasAlpha() && dstCM.hasAlpha()) {
                alpha = new float[numPixels];
                for (int i = 0; i < numPixels; i++) {
                    alpha[i] = tmpData[i][alphaIdx];
                }
            }

            // Translate colors
            applySequence(sequence, tmpData, srcCS, dstCS);

            // Copy alpha if needed
            if (dstCM.hasAlpha()) {
                alphaIdx = dstCM.numComponents - 1;
                if (alpha != null) {
                    for (int i = 0; i < numPixels; i++) {
                        tmpData[i][alphaIdx] = alpha[i];
                    }
                } else {
                    for (int i = 0; i < numPixels; i++) {
                        tmpData[i][alphaIdx] = 1f;
                    }
                }
            }

            // Store data back to the image
            for (int row = 0, dataPos = 0; row < srcW; row++) {
                for (int col = 0; col < srcH; col++) {
                    res.getRaster().setDataElements(row, col,
                            dstCM.getDataElements(tmpData[dataPos++], 0, null));
                }
            }
        }

        if (isDstIndex) { // Convert image into indexed color
            Graphics2D g2d = dst.createGraphics();
            g2d.drawImage(res, 0, 0, null);
            g2d.dispose();
            return dst;
        }

        return res;
    
public final java.awt.geom.Rectangle2DgetBounds2D(java.awt.image.Raster src)

        return src.getBounds();
    
public final java.awt.geom.Rectangle2DgetBounds2D(java.awt.image.BufferedImage src)

        return src.getRaster().getBounds();
    
public final java.awt.color.ICC_Profile[]getICC_Profiles()
Gets an array of ICC_Profiles objects which constructs this ColorConvertOp object or returns null if this ColorConvertOp is not constructed from array of ICC_Profiles.

return
an array of ICC_Profiles objects which constructs this ColorConvertOp object or returns null if this ColorConvertOp is not constructed from array of ICC_Profiles.

        if (midProfiles != null) {
            return midProfiles;
        }
        return null;
    
public final java.awt.geom.Point2DgetPoint2D(java.awt.geom.Point2D srcPt, java.awt.geom.Point2D dstPt)

        if (dstPt != null) {
            dstPt.setLocation(srcPt);
            return dstPt;
        }
        return new Point2D.Float((float)srcPt.getX(), (float)srcPt.getY());
    
public final java.awt.RenderingHintsgetRenderingHints()

        return renderingHints;