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