001: /*
002: * $RCSfile: MlibMultiplyConstOpImage.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:01 $
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.medialib.mlib.*;
024:
025: // import com.sun.media.jai.test.OpImageTester;
026:
027: /**
028: * An OpImage class that multiplies an image by a constant
029: *
030: */
031: final class MlibMultiplyConstOpImage extends PointOpImage {
032: private double[] constants;
033:
034: /**
035: * Constructs an MlibMultiplyConstOpImage. The image dimensions are copied
036: * from the source image. The tile grid layout, SampleModel, and
037: * ColorModel may optionally be specified by an ImageLayout object.
038: *
039: * @param source a RenderedImage.
040: * @param layout an ImageLayout optionally containing the tile
041: * grid layout, SampleModel, and ColorModel, or null.
042: */
043: public MlibMultiplyConstOpImage(RenderedImage source, Map config,
044: ImageLayout layout, double[] constants) {
045: super (source, layout, config, true);
046: this .constants = MlibUtils.initConstants(constants,
047: getSampleModel().getNumBands());
048: // Set flag to permit in-place operation.
049: permitInPlaceOperation();
050: }
051:
052: /**
053: * Multiply the pixel values of a rectangle with a given constant.
054: * The sources are cobbled.
055: *
056: * @param sources an array of sources, guarantee to provide all
057: * necessary source data for computing the rectangle.
058: * @param dest a tile that contains the rectangle to be computed.
059: * @param destRect the rectangle within this OpImage to be processed.
060: */
061: protected void computeRect(Raster[] sources, WritableRaster dest,
062: Rectangle destRect) {
063: Raster source = sources[0];
064: Rectangle srcRect = mapDestRect(destRect, 0);
065:
066: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
067: dest);
068:
069: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
070: srcRect, formatTag);
071: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
072: destRect, formatTag);
073:
074: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
075: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
076:
077: switch (dstAccessor.getDataType()) {
078: case DataBuffer.TYPE_BYTE:
079: case DataBuffer.TYPE_USHORT:
080: case DataBuffer.TYPE_SHORT:
081: case DataBuffer.TYPE_INT:
082: for (int i = 0; i < dstML.length; i++) {
083: double[] mlconstants = dstAccessor.getDoubleParameters(
084: i, constants);
085: Image.ConstMul(dstML[i], srcML[i], mlconstants);
086: }
087: break;
088:
089: case DataBuffer.TYPE_FLOAT:
090: case DataBuffer.TYPE_DOUBLE:
091: for (int i = 0; i < dstML.length; i++) {
092: double[] mlconstants = dstAccessor.getDoubleParameters(
093: i, constants);
094: Image.ConstMul_Fp(dstML[i], srcML[i], mlconstants);
095: }
096: break;
097:
098: default:
099: String className = this .getClass().getName();
100: throw new RuntimeException(className
101: + JaiI18N.getString("Generic2"));
102: }
103:
104: if (dstAccessor.isDataCopy()) {
105: dstAccessor.clampDataArrays();
106: dstAccessor.copyDataToRaster();
107: }
108: }
109:
110: // public static OpImage createTestImage(OpImageTester oit) {
111: // double[] consts = { 5, 5, 5 };
112: // return new MlibMultiplyConstOpImage(oit.getSource(), null,
113: // new ImageLayout(oit.getSource()),
114: // consts);
115: // }
116:
117: // // Calls a method on OpImage that uses introspection, to make this
118: // // class, discover it's createTestImage() call, call it and then
119: // // benchmark the performance of the created OpImage chain.
120: // public static void main (String args[]) {
121: // String classname = "com.sun.media.jai.mlib.MlibMultiplyConstOpImage";
122: // OpImageTester.performDiagnostics(classname,args);
123: // }
124: }
|