001: /*
002: * $RCSfile: MlibAddOpImage.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:48 $
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 Add operation on 2 images through mediaLib.
031: *
032: */
033: final class MlibAddOpImage extends PointOpImage {
034:
035: // XXX This cloning of the ImageLayout object should be centraliZed
036: // into the superclass PointOpImage and removed from the mlib subclasses.
037: private static ImageLayout layoutHelper(ImageLayout layout) {
038: if (layout == null) {
039: return null;
040: } else {
041: return (ImageLayout) layout.clone();
042: }
043: }
044:
045: /**
046: * Constructs an MlibAddOpImage. The image dimensions are copied
047: * from the source image. The tile grid layout, SampleModel, and
048: * ColorModel may optionally be specified by an ImageLayout object.
049: *
050: * @param source a RenderedImage.
051: * @param layout an ImageLayout optionally containing the tile
052: * grid layout, SampleModel, and ColorModel, or null.
053: */
054: public MlibAddOpImage(RenderedImage source1, RenderedImage source2,
055: Map config, ImageLayout layout) {
056: super (source1, source2, layoutHelper(layout), config, true);
057: // Set flag to permit in-place operation.
058: permitInPlaceOperation();
059: }
060:
061: /**
062: * Add the pixel values of a rectangle from the two sources.
063: * The sources are cobbled.
064: *
065: * @param sources an array of sources, guarantee to provide all
066: * necessary source data for computing the rectangle.
067: * @param dest a tile that contains the rectangle to be computed.
068: * @param destRect the rectangle within this OpImage to be processed.
069: */
070: protected void computeRect(Raster[] sources, WritableRaster dest,
071: Rectangle destRect) {
072:
073: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
074: dest);
075:
076: MediaLibAccessor srcAccessor1 = new MediaLibAccessor(
077: sources[0], destRect, formatTag);
078: MediaLibAccessor srcAccessor2 = new MediaLibAccessor(
079: sources[1], destRect, formatTag);
080: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
081: destRect, formatTag);
082:
083: mediaLibImage[] srcML1 = srcAccessor1.getMediaLibImages();
084: mediaLibImage[] srcML2 = srcAccessor2.getMediaLibImages();
085: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
086:
087: switch (dstAccessor.getDataType()) {
088: case DataBuffer.TYPE_BYTE:
089: case DataBuffer.TYPE_USHORT:
090: case DataBuffer.TYPE_SHORT:
091: case DataBuffer.TYPE_INT:
092: for (int i = 0; i < dstML.length; i++) {
093: Image.Add(dstML[i], srcML1[i], srcML2[i]);
094: }
095: break;
096:
097: case DataBuffer.TYPE_FLOAT:
098: case DataBuffer.TYPE_DOUBLE:
099: for (int i = 0; i < dstML.length; i++) {
100: Image.Add_Fp(dstML[i], srcML1[i], srcML2[i]);
101: }
102: break;
103:
104: default:
105: String className = this .getClass().getName();
106: throw new RuntimeException(className
107: + JaiI18N.getString("Generic2"));
108: }
109:
110: if (dstAccessor.isDataCopy()) {
111: dstAccessor.clampDataArrays();
112: dstAccessor.copyDataToRaster();
113: }
114: }
115:
116: // public static void main (String args[]) {
117: // System.out.println("MlibAddOpImage Test");
118: // ImageLayout layout;
119: // OpImage src1, src2, dst;
120: // Rectangle rect = new Rectangle(0, 0, 5, 5);
121:
122: // System.out.println("1. PixelInterleaved byte 3-band");
123: // layout = OpImageTester.createImageLayout(0, 0, 800, 800, 0, 0,
124: // 200, 200, DataBuffer.TYPE_BYTE,
125: // 3, false);
126: // src1 = OpImageTester.createRandomOpImage(layout);
127: // src2 = OpImageTester.createRandomOpImage(layout);
128: // dst = new MlibAddOpImage(src1, src2, null, null);
129: // OpImageTester.testOpImage(dst, rect);
130: // OpImageTester.timeOpImage(dst, 10);
131:
132: // System.out.println("2. Banded byte 3-band");
133: // layout = OpImageTester.createImageLayout(0, 0, 800, 800, 0, 0,
134: // 200, 200, DataBuffer.TYPE_BYTE,
135: // 3, true);
136: // src1 = OpImageTester.createRandomOpImage(layout);
137: // src2 = OpImageTester.createRandomOpImage(layout);
138: // dst = new MlibAddOpImage(src1, src2, null, null);
139: // OpImageTester.testOpImage(dst, rect);
140: // OpImageTester.timeOpImage(dst, 10);
141:
142: // System.out.println("3. PixelInterleaved int 3-band");
143: // layout = OpImageTester.createImageLayout(0, 0, 512, 512, 0, 0, 200, 200,
144: // DataBuffer.TYPE_INT, 3, false);
145: // src1 = OpImageTester.createRandomOpImage(layout);
146: // src2 = OpImageTester.createRandomOpImage(layout);
147: // dst = new MlibAddOpImage(src1, src2, null, null);
148: // OpImageTester.testOpImage(dst, rect);
149: // OpImageTester.timeOpImage(dst, 10);
150:
151: // System.out.println("4. Banded int 3-band");
152: // layout = OpImageTester.createImageLayout(0, 0, 512, 512, 0, 0,
153: // 200, 200, DataBuffer.TYPE_INT,
154: // 3, true);
155: // src1 = OpImageTester.createRandomOpImage(layout);
156: // src2 = OpImageTester.createRandomOpImage(layout);
157: // dst = new MlibAddOpImage(src1, src2, null, null);
158: // OpImageTester.testOpImage(dst, rect);
159: // OpImageTester.timeOpImage(dst, 10);
160: // }
161: }
|