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