001: /*
002: * $RCSfile: MlibSobelOpImage.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:06 $
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 Sobel (Gradient) on a source image.
034: *
035: *
036: *
037: * @see KernelJAI
038: */
039: final class MlibSobelOpImage extends AreaOpImage {
040:
041: /**
042: * Creates a MlibSobelOpImage given the image source.
043: * The image dimensions are derived from the source image.
044: * The tile grid layout, SampleModel, and ColorModel may
045: * optionally be specified by an ImageLayout object.
046: *
047: * @param source a RenderedImage.
048: * @param extender a BorderExtender, or null.
049:
050: * @param layout an ImageLayout optionally containing the tile grid layout,
051: * SampleModel, and ColorModel, or null.
052: */
053: public MlibSobelOpImage(RenderedImage source,
054: BorderExtender extender, Map config, ImageLayout layout,
055: KernelJAI kernel) {
056: //
057: // Both the orthogonal pair of kernels have the same width & height
058: // Hence either of them can be used here
059: //
060: super (source, layout, config, true, extender, kernel
061: .getLeftPadding(), kernel.getRightPadding(), kernel
062: .getTopPadding(), kernel.getBottomPadding());
063: }
064:
065: /**
066: * Performs Sobel on a specified rectangle. The sources are cobbled.
067: *
068: * @param sources an array of source Rasters, guaranteed to provide all
069: * necessary source data for computing the output.
070: * @param dest a WritableRaster tile containing the area to be computed.
071: * @param destRect the rectangle within dest to be processed.
072: */
073: protected void computeRect(Raster[] sources, WritableRaster dest,
074: Rectangle destRect) {
075: Raster source = sources[0];
076: Rectangle srcRect = mapDestRect(destRect, 0);
077:
078: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
079: dest);
080:
081: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
082: srcRect, formatTag);
083: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
084: destRect, formatTag);
085: int numBands = getSampleModel().getNumBands();
086:
087: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
088: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
089: for (int i = 0; i < dstML.length; i++) {
090: switch (dstAccessor.getDataType()) {
091: case DataBuffer.TYPE_BYTE:
092: case DataBuffer.TYPE_USHORT:
093: case DataBuffer.TYPE_SHORT:
094: case DataBuffer.TYPE_INT:
095: Image.Sobel(dstML[i], srcML[i], ((1 << numBands) - 1),
096: Constants.MLIB_EDGE_DST_NO_WRITE);
097: break;
098:
099: case DataBuffer.TYPE_FLOAT:
100: case DataBuffer.TYPE_DOUBLE:
101: Image.Sobel_Fp(dstML[i], srcML[i],
102: ((1 << numBands) - 1),
103: Constants.MLIB_EDGE_DST_NO_WRITE);
104: break;
105:
106: default:
107: String className = this .getClass().getName();
108: throw new RuntimeException(JaiI18N
109: .getString("Generic2"));
110: }
111: }
112:
113: if (dstAccessor.isDataCopy()) {
114: dstAccessor.copyDataToRaster();
115: }
116: }
117:
118: // public static OpImage createTestImage(OpImageTester oit) {
119: // float data_h[] = {-1.0f, -2.0f, -1.0f,
120: // 0.0f, 0.0f, 0.0f,
121: // 1.0f, 2.0f, 1.0f};
122: // float data_v[] = {-1.0f, 0.0f, 1.0f,
123: // -2.0f, 0.0f, 2.0f,
124: // -1.0f, 0.0f, 1.0f};
125:
126: // KernelJAI kern_h = new KernelJAI(3,3,data_h);
127: // KernelJAI kern_v = new KernelJAI(3,3,data_v);
128:
129: // return new MlibSobelOpImage(oit.getSource(), null, null,
130: // new ImageLayout(oit.getSource()),
131: // kern_h);
132: // }
133:
134: // public static void main (String args[]) {
135: // String classname = "com.sun.media.jai.mlib.MlibSobelOpImage";
136: // OpImageTester.performDiagnostics(classname,args);
137: // }
138: }
|