001: /*
002: * $RCSfile: MlibAddConstOpImage.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:55:48 $
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: * An OpImage class that copies an image from source to dest.
028: *
029: */
030: final class MlibAddConstOpImage extends PointOpImage {
031: private double[] constants;
032:
033: /**
034: * Constructs an MlibAddConstOpImage. The image dimensions are copied
035: * from the source image. The tile grid layout, SampleModel, and
036: * 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 MlibAddConstOpImage(RenderedImage source, Map config,
043: 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: * Add the pixel values of a rectangle with 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: Raster source = sources[0];
063: Rectangle srcRect = mapDestRect(destRect, 0);
064:
065: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
066: dest);
067:
068: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
069: srcRect, 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.ConstAdd(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.ConstAdd_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 MlibAddConstOpImage(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.MlibAddConstOpImage";
126: // OpImageTester.performDiagnostics(classname,args);
127: // }
128: }
|