001: /*
002: * $RCSfile: MlibConvolveOpImage.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:52 $
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.SampleModel;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.WritableRaster;
020: import java.awt.image.renderable.ParameterBlock;
021: import java.awt.image.renderable.RenderedImageFactory;
022: import javax.media.jai.AreaOpImage;
023: import javax.media.jai.BorderExtender;
024: import javax.media.jai.ImageLayout;
025: import javax.media.jai.KernelJAI;
026: import javax.media.jai.OpImage;
027: import java.util.Map;
028: import com.sun.medialib.mlib.*;
029:
030: // import com.sun.media.jai.test.OpImageTester;
031:
032: /**
033: * An OpImage class to perform convolution on a source image.
034: *
035: * <p> This class implements a convolution operation. Convolution is a
036: * spatial operation that computes each output sample by multiplying
037: * elements of a kernel with the samples surrounding a particular
038: * source sample.
039: *
040: * <p> For each destination sample, the kernel is rotated 180 degrees
041: * and its "key element" is placed over the source pixel corresponding
042: * with the destination pixel. The kernel elements are multiplied
043: * with the source pixels under them, and the resulting products are
044: * summed together to produce the destination sample value.
045: *
046: * <p> Example code for the convolution operation on a single sample
047: * dst[x][y] is as follows, assuming the kernel is of size M rows x N
048: * columns and has already been rotated through 180 degrees. The
049: * kernel's key element is located at position (xKey, yKey):
050: *
051: * <pre>
052: * dst[x][y] = 0;
053: * for (int i = -xKey; i < M - xKey; i++) {
054: * for (int j = -yKey; j < N - yKey; j++) {
055: * dst[x][y] += src[x + i][y + j] * kernel[xKey + i][yKey + j];
056: * }
057: * }
058: * </pre>
059: *
060: * <p> Convolution, or any neighborhood operation, leaves a band of
061: * pixels around the edges undefined, i.e., for a 3x3 kernel, only
062: * four kernel elements and four source pixels contribute to the
063: * destination pixel located at (0,0). Such pixels are not includined
064: * in the destination image, unless a non-null BorderExtender is provided.
065: *
066: * <p> The Kernel cannot be bigger in any dimension than the image data.
067: *
068: *
069: * @see KernelJAI
070: */
071: final class MlibConvolveOpImage extends AreaOpImage {
072:
073: /**
074: * The kernel with which to do the convolve operation.
075: */
076: protected KernelJAI kernel;
077:
078: /** Kernel variables. */
079: private int kw, kh, kx, ky;
080: float kData[];
081: double doublekData[];
082: int intkData[];
083: int shift = -1;
084:
085: /**
086: * Creates a MlibConvolveOpImage given the image source and
087: * pre-rotated convolution kernel. The image dimensions are
088: * derived from the source image. The tile grid layout,
089: * SampleModel, and ColorModel may optionally be specified by an
090: * ImageLayout object.
091: *
092: * @param source a RenderedImage.
093: * @param extender a BorderExtender, or null.
094:
095: * or null. If null, a default cache will be used.
096: * @param layout an ImageLayout optionally containing the tile grid layout,
097: * SampleModel, and ColorModel, or null.
098: * @param kernel the pre-rotated convolution KernelJAI.
099: */
100: public MlibConvolveOpImage(RenderedImage source,
101: BorderExtender extender, Map config, ImageLayout layout,
102: KernelJAI kernel) {
103: super (source, layout, config, true, extender, kernel
104: .getLeftPadding(), kernel.getRightPadding(), kernel
105: .getTopPadding(), kernel.getBottomPadding());
106:
107: this .kernel = kernel;
108: kw = kernel.getWidth();
109: kh = kernel.getHeight();
110:
111: // this looks wrong, but it's right. AreaOpImage chops the image
112: // up so that the kernels are "centered" by selecting the
113: // appropriate source chunk (translating the source instead of the
114: // kernel). The X and Y offsets are taken care of there, not here.
115: kx = kw / 2;
116: ky = kh / 2;
117:
118: kData = kernel.getKernelData();
119:
120: int count = kw * kh;
121:
122: // A little inefficient but figuring out what datatype
123: // mediaLibAccessor will want is tricky.
124: intkData = new int[count];
125: doublekData = new double[count];
126: for (int i = 0; i < count; i++) {
127: doublekData[i] = (double) kData[i];
128: }
129: }
130:
131: private synchronized void setShift(int formatTag) {
132: if (shift == -1) {
133: int mediaLibDataType = MediaLibAccessor
134: .getMediaLibDataType(formatTag);
135: shift = Image.ConvKernelConvert(intkData, doublekData, kw,
136: kh, mediaLibDataType);
137: }
138: }
139:
140: /**
141: * Performs convolution on a specified rectangle. The sources are
142: * cobbled.
143: *
144: * @param sources an array of source Rasters, guaranteed to provide all
145: * necessary source data for computing the output.
146: * @param dest a WritableRaster tile containing the area to be computed.
147: * @param destRect the rectangle within dest to be processed.
148: */
149: protected void computeRect(Raster[] sources, WritableRaster dest,
150: Rectangle destRect) {
151:
152: Raster source = sources[0];
153: Rectangle srcRect = mapDestRect(destRect, 0);
154:
155: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
156: dest);
157:
158: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
159: srcRect, formatTag);
160: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
161: destRect, formatTag);
162: int numBands = getSampleModel().getNumBands();
163:
164: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
165: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
166: for (int i = 0; i < dstML.length; i++) {
167: switch (dstAccessor.getDataType()) {
168: case DataBuffer.TYPE_BYTE:
169: case DataBuffer.TYPE_USHORT:
170: case DataBuffer.TYPE_SHORT:
171: case DataBuffer.TYPE_INT:
172: if (shift == -1) {
173: setShift(formatTag);
174: }
175: Image.ConvMxN(dstML[i], srcML[i], intkData, kw, kh, kx,
176: ky, shift, ((1 << numBands) - 1),
177: Constants.MLIB_EDGE_DST_NO_WRITE);
178: break;
179: case DataBuffer.TYPE_FLOAT:
180: case DataBuffer.TYPE_DOUBLE:
181: Image.ConvMxN_Fp(dstML[i], srcML[i], doublekData, kw,
182: kh, kx, ky, ((1 << numBands) - 1),
183: Constants.MLIB_EDGE_DST_NO_WRITE);
184: break;
185: default:
186: String className = this .getClass().getName();
187: throw new RuntimeException(JaiI18N
188: .getString("Generic2"));
189: }
190: }
191:
192: if (dstAccessor.isDataCopy()) {
193: dstAccessor.copyDataToRaster();
194: }
195: }
196:
197: // public static OpImage createTestImage(OpImageTester oit) {
198: // float data[] = {0.05f,0.10f,0.05f,
199: // 0.10f,0.40f,0.10f,
200: // 0.05f,0.10f,0.05f};
201: // KernelJAI k1 = new KernelJAI(3,3,1,1,data);
202:
203: // return new MlibConvolveOpImage(oit.getSource(), null, null,
204: // new ImageLayout(oit.getSource()),
205: // k1);
206: // }
207:
208: // public static void main (String args[]) {
209: // String classname = "com.sun.media.jai.mlib.MlibConvolveOpImage";
210: // OpImageTester.performDiagnostics(classname,args);
211: // }
212: }
|