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