001: /*
002: * $RCSfile: MlibSubtractFromConstOpImage.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:07 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.Raster;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.WritableRaster;
019: import javax.media.jai.ImageLayout;
020: import javax.media.jai.OpImage;
021: import javax.media.jai.PointOpImage;
022: import java.util.Map;
023: import com.sun.media.jai.util.ImageUtil; // import com.sun.media.jai.test.OpImageTester;
024: import com.sun.medialib.mlib.*;
025:
026: /**
027: * A mediaLib implementation of "SubtractFromConst" operator.
028: *
029: */
030: final class MlibSubtractFromConstOpImage extends PointOpImage {
031: private double[] constants;
032:
033: /**
034: * Constructs an MlibSubtractFromConstOpImage. The image dimensions
035: * are copied from the source image. The tile grid layout, SampleModel,
036: * and ColorModel may optionally be specified by an ImageLayout object.
037: *
038: * @param source a RenderedImage.
039: * @param layout an ImageLayout optionally containing the tile
040: * grid layout, SampleModel, and ColorModel, or null.
041: */
042: public MlibSubtractFromConstOpImage(RenderedImage source,
043: Map config, ImageLayout layout, double[] constants) {
044: super (source, layout, config, true);
045: this .constants = MlibUtils.initConstants(constants,
046: getSampleModel().getNumBands());
047: // Set flag to permit in-place operation.
048: permitInPlaceOperation();
049: }
050:
051: /**
052: * Subtract the pixel values of a rectangle from a given constant.
053: * The sources are cobbled.
054: *
055: * @param sources an array of sources, guarantee to provide all
056: * necessary source data for computing the rectangle.
057: * @param dest a tile that contains the rectangle to be computed.
058: * @param destRect the rectangle within this OpImage to be processed.
059: */
060: protected void computeRect(Raster[] sources, WritableRaster dest,
061: Rectangle destRect) {
062:
063: Raster source = sources[0];
064: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
065: dest);
066:
067: // For PointOpImages, the srcRect and the destRect are the same.
068: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
069: destRect, formatTag);
070: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
071: destRect, formatTag);
072:
073: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
074: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
075:
076: switch (dstAccessor.getDataType()) {
077: case DataBuffer.TYPE_BYTE:
078: case DataBuffer.TYPE_USHORT:
079: case DataBuffer.TYPE_SHORT:
080: case DataBuffer.TYPE_INT:
081: int[] constInt = new int[constants.length];
082: for (int i = 0; i < constants.length; i++) {
083: constInt[i] = ImageUtil.clampRoundInt(constants[i]);
084: }
085:
086: for (int i = 0; i < dstML.length; i++) {
087: int mlconstants[] = dstAccessor.getIntParameters(i,
088: constInt);
089: Image.ConstSub(dstML[i], srcML[i], mlconstants);
090: }
091: break;
092:
093: case DataBuffer.TYPE_FLOAT:
094: case DataBuffer.TYPE_DOUBLE:
095: for (int i = 0; i < dstML.length; i++) {
096: double[] mlconstants = dstAccessor.getDoubleParameters(
097: i, constants);
098: Image.ConstSub_Fp(dstML[i], srcML[i], mlconstants);
099: }
100: break;
101:
102: default:
103: String className = this .getClass().getName();
104: throw new RuntimeException(className
105: + JaiI18N.getString("Generic2"));
106: }
107:
108: if (dstAccessor.isDataCopy()) {
109: dstAccessor.clampDataArrays();
110: dstAccessor.copyDataToRaster();
111: }
112: }
113:
114: // public static OpImage createTestImage(OpImageTester oit) {
115: // double[] consts = {5, 5, 5};
116: // return new MlibSubtractFromConstOpImage(oit.getSource(), null,
117: // new ImageLayout(oit.getSource()),
118: // consts);
119: // }
120:
121: // // Calls a method on OpImage that uses introspection, to make this
122: // // class, discover it's createTestImage() call, call it and then
123: // // benchmark the performance of the created OpImage chain.
124: // public static void main (String args[]) {
125: // String classname = "com.sun.media.jai.mlib.MlibSubtractFromConstOpImage";
126: // OpImageTester.performDiagnostics(classname,args);
127: // }
128: }
|