001: /*
002: * $RCSfile: MlibErode3PlusOpImage.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.2 $
009: * $Date: 2005/12/09 00:20:28 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.RenderingHints;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.SampleModel;
018: import java.awt.image.Raster;
019: import java.awt.image.RenderedImage;
020: import java.awt.image.WritableRaster;
021: import java.awt.image.renderable.ParameterBlock;
022: import java.awt.image.renderable.RenderedImageFactory;
023: import javax.media.jai.AreaOpImage;
024: import javax.media.jai.BorderExtender;
025: import javax.media.jai.ImageLayout;
026: import javax.media.jai.JAI;
027: import javax.media.jai.KernelJAI;
028: import javax.media.jai.OpImage;
029: import java.util.Map;
030: import com.sun.medialib.mlib.*;
031:
032: // import com.sun.media.jai.test.OpImageTester;
033:
034: /**
035: * An OpImage class to perform erosion on a source image
036: * for the specific case when the kernel is a 3x3 plus shape
037: * with the key position in the middle,
038: * using mediaLib, of course :-).
039: *
040: * @see javax.media.jai.operator.ErodeDescriptor
041: * @see KernelJAI
042: */
043: final class MlibErode3PlusOpImage extends AreaOpImage {
044:
045: // Since medialib expects single banded data with IndexColorModel, we
046: // should not expand the indexed data
047: private static Map configHelper(Map configuration) {
048:
049: Map config;
050:
051: if (configuration == null) {
052:
053: config = new RenderingHints(
054: JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.FALSE);
055:
056: } else {
057:
058: config = configuration;
059:
060: // If the user has specified a hint for this, then we don't
061: // want to change it, so change only if this hint is not
062: // already specified
063: if (!(config.containsKey(JAI.KEY_REPLACE_INDEX_COLOR_MODEL))) {
064: RenderingHints hints = (RenderingHints) configuration;
065: config = (RenderingHints) hints.clone();
066: config.put(JAI.KEY_REPLACE_INDEX_COLOR_MODEL,
067: Boolean.FALSE);
068: }
069: }
070:
071: return config;
072: }
073:
074: /**
075: * Creates a MlibErode3PlusOpImage given the image source
076: * The image dimensions are
077: * derived from the source image. The tile grid layout,
078: * SampleModel, and ColorModel may optionally be specified by an
079: * ImageLayout object.
080: *
081: * @param source a RenderedImage.
082: * @param extender a BorderExtender, or null.
083:
084: * or null. If null, a default cache will be used.
085: * @param layout an ImageLayout optionally containing the tile grid layout,
086: * SampleModel, and ColorModel, or null.
087: */
088: public MlibErode3PlusOpImage(RenderedImage source,
089: BorderExtender extender, Map config, ImageLayout layout) {
090: super (source, layout, configHelper(config), true, extender, 1, //kernel.getLeftPadding(),
091: 1, //kernel.getRightPadding(),
092: 1, //kernel.getTopPadding(),
093: 1 //kernel.getBottomPadding()
094: );
095: }
096:
097: /**
098: * Performs erosion on a specified rectangle. The sources are
099: * cobbled.
100: *
101: * @param sources an array of source Rasters, guaranteed to provide all
102: * necessary source data for computing the output.
103: * @param dest a WritableRaster tile containing the area to be computed.
104: * @param destRect the rectangle within dest to be processed.
105: */
106: protected void computeRect(Raster[] sources, WritableRaster dest,
107: Rectangle destRect) {
108:
109: Raster source = sources[0];
110: Rectangle srcRect = mapDestRect(destRect, 0);
111:
112: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
113: dest);
114:
115: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
116: srcRect, formatTag, true);
117: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
118: destRect, formatTag, true);
119: int numBands = getSampleModel().getNumBands();
120:
121: mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
122: mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
123: for (int i = 0; i < dstML.length; i++) {
124: switch (dstAccessor.getDataType()) {
125: case DataBuffer.TYPE_BYTE:
126: case DataBuffer.TYPE_USHORT:
127: case DataBuffer.TYPE_SHORT:
128: case DataBuffer.TYPE_INT:
129: Image.Erode4(dstML[i], srcML[i]);
130: break;
131: case DataBuffer.TYPE_FLOAT:
132: case DataBuffer.TYPE_DOUBLE:
133: Image.Erode4_Fp(dstML[i], srcML[i]);
134: break;
135: default:
136: String className = this .getClass().getName();
137: throw new RuntimeException(JaiI18N
138: .getString("Generic2"));
139: }
140: }
141:
142: if (dstAccessor.isDataCopy()) {
143: dstAccessor.copyDataToRaster();
144: }
145: }
146:
147: // public static OpImage createTestImage(OpImageTester oit) {
148: // float data[] = {0.05f,0.10f,0.05f,
149: // 0.10f,0.40f,0.10f,
150: // 0.05f,0.10f,0.05f};
151: // KernelJAI kJAI = new KernelJAI(3,3,1,1,data);
152: // return new MlibErode4OpImage(oit.getSource(), null, null,
153: // new ImageLayout(oit.getSource()),
154: // kJAI);
155: // }
156:
157: // public static void main (String args[]) {
158: // String classname = "com.sun.media.jai.mlib.MlibErode4OpImage";
159: // OpImageTester.performDiagnostics(classname,args);
160: // }
161: }
|