001: /*
002: * $RCSfile: MlibMultiplyOpImage.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.PixelInterleavedSampleModel;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.SampleModel;
020: import java.awt.image.WritableRaster;
021: import javax.media.jai.ImageLayout;
022: import javax.media.jai.OpImage;
023: import javax.media.jai.PointOpImage;
024: import java.util.Map;
025: import com.sun.medialib.mlib.*;
026:
027: // import com.sun.media.jai.test.OpImageTester;
028:
029: /**
030: * An OpImage that performs the Multiply operation on two images through
031: * mediaLib.
032: *
033: */
034: final class MlibMultiplyOpImage extends PointOpImage {
035:
036: /**
037: * Constructs an MlibMultiplyOpImage. The image dimensions are copied
038: * from the source image. The tile grid layout, SampleModel, and
039: * ColorModel may optionally be specified by an ImageLayout object.
040: *
041: * @param source a RenderedImage.
042: * @param layout an ImageLayout optionally containing the tile
043: * grid layout, SampleModel, and ColorModel, or null.
044: */
045: public MlibMultiplyOpImage(RenderedImage source1,
046: RenderedImage source2, Map config, ImageLayout layout) {
047: super (source1, source2, layout, config, true);
048: // Set flag to permit in-place operation.
049: permitInPlaceOperation();
050: }
051:
052: /**
053: * Multiply the pixel values of a rectangle from the two sources.
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:
064: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
065: dest);
066:
067: MediaLibAccessor srcAccessor1 = new MediaLibAccessor(
068: sources[0], destRect, formatTag);
069: MediaLibAccessor srcAccessor2 = new MediaLibAccessor(
070: sources[1], destRect, formatTag);
071: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
072: destRect, formatTag);
073:
074: mediaLibImage[] srcML1 = srcAccessor1.getMediaLibImages();
075: mediaLibImage[] srcML2 = srcAccessor2.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: Image.MulShift(dstML[i], srcML1[i], srcML2[i], 0);
085: }
086: break;
087:
088: case DataBuffer.TYPE_FLOAT:
089: case DataBuffer.TYPE_DOUBLE:
090: for (int i = 0; i < dstML.length; i++) {
091: Image.Mul_Fp(dstML[i], srcML1[i], srcML2[i]);
092: }
093: break;
094:
095: default:
096: String className = this .getClass().getName();
097: throw new RuntimeException(className
098: + JaiI18N.getString("Generic2"));
099: }
100:
101: if (dstAccessor.isDataCopy()) {
102: dstAccessor.clampDataArrays();
103: dstAccessor.copyDataToRaster();
104: }
105: }
106:
107: // public static void main (String args[]) {
108: // System.out.println("MlibMultiplyOpImage Test");
109: // ImageLayout layout;
110: // OpImage src1, src2, dst;
111: // Rectangle rect = new Rectangle(0, 0, 5, 5);
112:
113: // System.out.println("1. PixelInterleaved byte 3-band");
114: // layout = OpImageTester.createImageLayout(0, 0, 800, 800, 0, 0,
115: // 200, 200, DataBuffer.TYPE_BYTE,
116: // 3, false);
117: // src1 = OpImageTester.createRandomOpImage(layout);
118: // src2 = OpImageTester.createRandomOpImage(layout);
119: // dst = new MlibMultiplyOpImage(src1, src2, null, null);
120: // OpImageTester.testOpImage(dst, rect);
121: // OpImageTester.timeOpImage(dst, 10);
122:
123: // System.out.println("2. Banded byte 3-band");
124: // layout = OpImageTester.createImageLayout(0, 0, 800, 800, 0, 0,
125: // 200, 200, DataBuffer.TYPE_BYTE,
126: // 3, true);
127: // src1 = OpImageTester.createRandomOpImage(layout);
128: // src2 = OpImageTester.createRandomOpImage(layout);
129: // dst = new MlibMultiplyOpImage(src1, src2, null, null);
130: // OpImageTester.testOpImage(dst, rect);
131: // OpImageTester.timeOpImage(dst, 10);
132:
133: // System.out.println("3. PixelInterleaved int 3-band");
134: // layout = OpImageTester.createImageLayout(0, 0, 512, 512, 0, 0, 200, 200,
135: // DataBuffer.TYPE_INT, 3, false);
136: // src1 = OpImageTester.createRandomOpImage(layout);
137: // src2 = OpImageTester.createRandomOpImage(layout);
138: // dst = new MlibMultiplyOpImage(src1, src2, null, null);
139: // OpImageTester.testOpImage(dst, rect);
140: // OpImageTester.timeOpImage(dst, 10);
141:
142: // System.out.println("4. Banded int 3-band");
143: // layout = OpImageTester.createImageLayout(0, 0, 512, 512, 0, 0,
144: // 200, 200, DataBuffer.TYPE_INT,
145: // 3, true);
146: // src1 = OpImageTester.createRandomOpImage(layout);
147: // src2 = OpImageTester.createRandomOpImage(layout);
148: // dst = new MlibMultiplyOpImage(src1, src2, null, null);
149: // OpImageTester.testOpImage(dst, rect);
150: // OpImageTester.timeOpImage(dst, 10);
151: // }
152: }
|