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