001: /*
002: * $RCSfile: MlibConvolveNxNOpImage.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.4 $
009: * $Date: 2005/08/16 00:17:28 $
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, e.g., 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: * @see KernelJAI
069: */
070: final class MlibConvolveNxNOpImage extends AreaOpImage {
071:
072: /**
073: * The kernel with which to do the convolve operation.
074: */
075: protected KernelJAI kernel;
076:
077: /** Kernel variables. */
078: private int kw, kh;
079: float kData[];
080: double doublekData[];
081: int intkData[];
082: int shift = -1;
083:
084: /**
085: * Creates a MlibConvolveNxNOpImage given the image source and
086: * pre-rotated convolution kernel. The image dimensions are
087: * derived from the source image. The tile grid layout,
088: * SampleModel, and ColorModel may optionally be specified by an
089: * ImageLayout object.
090: *
091: * @param source a RenderedImage.
092: * @param extender a BorderExtender, or null.
093:
094: * or null. If null, a default cache will be used.
095: * @param layout an ImageLayout optionally containing the tile grid layout,
096: * SampleModel, and ColorModel, or null.
097: * @param kernel the pre-rotated convolution KernelJAI.
098: */
099: public MlibConvolveNxNOpImage(RenderedImage source,
100: BorderExtender extender, Map config, ImageLayout layout,
101: KernelJAI kernel) {
102: super (source, layout, config, true, extender, kernel
103: .getLeftPadding(), kernel.getRightPadding(), kernel
104: .getTopPadding(), kernel.getBottomPadding());
105:
106: this .kernel = kernel;
107: kw = kernel.getWidth();
108: kh = kernel.getHeight();
109:
110: // kx and ky are centered in AreaOpImage, not here.
111:
112: kData = kernel.getKernelData();
113:
114: int count = kw * kh;
115:
116: // A little inefficient but figuring out what datatype
117: // mediaLibAccessor will want is tricky.
118: intkData = new int[count];
119: doublekData = new double[count];
120: for (int i = 0; i < count; i++) {
121: doublekData[i] = (double) kData[i];
122: }
123: }
124:
125: private synchronized void setShift(int formatTag) {
126: if (shift == -1) {
127: int mediaLibDataType = MediaLibAccessor
128: .getMediaLibDataType(formatTag);
129: shift = Image.ConvKernelConvert(intkData, doublekData, kw,
130: kh, mediaLibDataType);
131: }
132: }
133:
134: /**
135: * Performs convolution on a specified rectangle. The sources are
136: * cobbled.
137: *
138: * @param sources an array of source Rasters, guaranteed to provide all
139: * necessary source data for computing the output.
140: * @param dest a WritableRaster tile containing the area to be computed.
141: * @param destRect the rectangle within dest to be processed.
142: */
143: protected void computeRect(Raster[] sources, WritableRaster dest,
144: Rectangle destRect) {
145:
146: Raster source = sources[0];
147: Rectangle srcRect = mapDestRect(destRect, 0);
148:
149: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
150: dest);
151:
152: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
153: srcRect, formatTag, true);
154: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
155: destRect, formatTag, true);
156: int numBands = getSampleModel().getNumBands();
157:
158: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
159: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
160: for (int i = 0; i < dstML.length; i++) {
161: switch (dstAccessor.getDataType()) {
162: case DataBuffer.TYPE_BYTE:
163: case DataBuffer.TYPE_USHORT:
164: case DataBuffer.TYPE_SHORT:
165: case DataBuffer.TYPE_INT:
166: if (shift == -1) {
167: setShift(formatTag);
168: }
169:
170: if (kw == 2) {
171: Image.Conv2x2(dstML[i], srcML[i], intkData, shift,
172: ((1 << numBands) - 1),
173: Constants.MLIB_EDGE_DST_NO_WRITE);
174: } else if (kw == 3) {
175: Image.Conv3x3(dstML[i], srcML[i], intkData, shift,
176: ((1 << numBands) - 1),
177: Constants.MLIB_EDGE_DST_NO_WRITE);
178: } else if (kw == 4) {
179: Image.Conv4x4(dstML[i], srcML[i], intkData, shift,
180: ((1 << numBands) - 1),
181: Constants.MLIB_EDGE_DST_NO_WRITE);
182: } else if (kw == 5) {
183: Image.Conv5x5(dstML[i], srcML[i], intkData, shift,
184: ((1 << numBands) - 1),
185: Constants.MLIB_EDGE_DST_NO_WRITE);
186: } else if (kw == 7) {
187: Image.Conv7x7(dstML[i], srcML[i], intkData, shift,
188: ((1 << numBands) - 1),
189: Constants.MLIB_EDGE_DST_NO_WRITE);
190: }
191: break;
192: case DataBuffer.TYPE_FLOAT:
193: case DataBuffer.TYPE_DOUBLE:
194: if (kw == 2) {
195: Image.Conv2x2_Fp(dstML[i], srcML[i], doublekData,
196: ((1 << numBands) - 1),
197: Constants.MLIB_EDGE_DST_NO_WRITE);
198: } else if (kw == 3) {
199: Image.Conv3x3_Fp(dstML[i], srcML[i], doublekData,
200: ((1 << numBands) - 1),
201: Constants.MLIB_EDGE_DST_NO_WRITE);
202: } else if (kw == 4) {
203: Image.Conv4x4_Fp(dstML[i], srcML[i], doublekData,
204: ((1 << numBands) - 1),
205: Constants.MLIB_EDGE_DST_NO_WRITE);
206: } else if (kw == 5) {
207: Image.Conv5x5_Fp(dstML[i], srcML[i], doublekData,
208: ((1 << numBands) - 1),
209: Constants.MLIB_EDGE_DST_NO_WRITE);
210: } else if (kw == 7) {
211: Image.Conv7x7_Fp(dstML[i], srcML[i], doublekData,
212: ((1 << numBands) - 1),
213: Constants.MLIB_EDGE_DST_NO_WRITE);
214: }
215: break;
216: default:
217: String className = this .getClass().getName();
218: throw new RuntimeException(JaiI18N
219: .getString("Generic2"));
220: }
221: }
222:
223: if (dstAccessor.isDataCopy()) {
224: dstAccessor.copyDataToRaster();
225: }
226: }
227:
228: // public static OpImage createTestImage(OpImageTester oit) {
229: // float data[] = {0.05f,0.10f,0.05f,
230: // 0.10f,0.40f,0.10f,
231: // 0.05f,0.10f,0.05f};
232: // KernelJAI kJAI = new KernelJAI(3,3,1,1,data);
233: // return new MlibConvolve3x3OpImage(oit.getSource(), null, null,
234: // new ImageLayout(oit.getSource()),
235: // kJAI);
236: // }
237:
238: // public static void main (String args[]) {
239: // String classname = "com.sun.media.jai.mlib.MlibConvolve3x3OpImage";
240: // OpImageTester.performDiagnostics(classname,args);
241: // }
242: }
|