001: /*
002: * $RCSfile: MlibBinarizeOpImage.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:51 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.image.RenderedImage;
015: import javax.media.jai.ImageLayout;
016: import javax.media.jai.Interpolation;
017: import java.util.Map;
018: import java.awt.Rectangle;
019: import java.awt.image.Raster;
020: import java.awt.image.WritableRaster;
021: import java.awt.image.DataBuffer;
022: import java.awt.image.SampleModel;
023: import java.awt.image.ColorModel;
024: import java.awt.image.PackedColorModel;
025: import java.awt.image.MultiPixelPackedSampleModel;
026: import javax.media.jai.ImageLayout;
027: import javax.media.jai.PlanarImage;
028: import javax.media.jai.PointOpImage;
029: import com.sun.medialib.mlib.*;
030: import com.sun.media.jai.util.ImageUtil;
031: import com.sun.media.jai.util.JDKWorkarounds;
032:
033: /**
034: * A mediaLib class extending <code>PointOpImage</code> to
035: * binarize images to bileval images.
036: * <p> <pre>
037: * dst(i,j) = src(i,j) > threshvalue? 1: 0;
038: *
039: * @see com.sun.media.jai.operator.BinarizeOpImage
040: * @see javax.media.jai.operator.BinarizeDescriptor
041: *
042: */
043: class MlibBinarizeOpImage extends PointOpImage {
044:
045: /**
046: * threshold value
047: */
048: private double thresh;
049:
050: /**
051: * Constructs a <code>MlibBinarizeOpImage</code>
052: * from a <code>RenderedImage</code> source, thresh value.
053: *
054: * @param source a <code>RenderedImage</code>.
055: * @param layout an <code>ImageLayout</code> optionally containing
056: * the tile grid layout, <code>SampleModel</code>, and
057: * <code>ColorModel</code>, or <code>null</code>.
058:
059: * from this <code>OpImage</code>, or <code>null</code>. If
060: * <code>null</code>, no caching will be performed.
061:
062: * @param thresh Threshold value.
063: *
064: * @throws IllegalArgumentException if combining the
065: * source bounds with the layout parameter results in negative
066: * output width or height.
067: */
068: public MlibBinarizeOpImage(RenderedImage source,
069: ImageLayout layout, Map config, double thresh) {
070:
071: super (source, layoutHelper(source, layout, config), config,
072: true);
073:
074: this .thresh = thresh;
075: }
076:
077: // set the OpImage's SM to be MultiPixelPackedSampleModel
078: private static ImageLayout layoutHelper(RenderedImage source,
079: ImageLayout il, Map config) {
080:
081: ImageLayout layout = (il == null) ? new ImageLayout()
082: : (ImageLayout) il.clone();
083:
084: SampleModel sm = layout.getSampleModel(source);
085: if (!ImageUtil.isBinary(sm)) {
086: sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
087: layout.getTileWidth(source), layout
088: .getTileHeight(source), 1);
089: layout.setSampleModel(sm);
090: }
091:
092: ColorModel cm = layout.getColorModel(null);
093: if (cm == null
094: || !JDKWorkarounds.areCompatibleDataModels(sm, cm)) {
095: layout.setColorModel(ImageUtil.getCompatibleColorModel(sm,
096: config));
097: }
098:
099: return layout;
100: }
101:
102: /**
103: *
104: * @param sources an array of sources, guarantee to provide all
105: * necessary source data for computing the rectangle.
106: * @param dest a tile that contains the rectangle to be computed.
107: * @param destRect the rectangle within this OpImage to be processed.
108: */
109: protected void computeRect(Raster[] sources, WritableRaster dest,
110: Rectangle destRect) {
111:
112: Raster source = sources[0];
113:
114: Rectangle srcRect = mapDestRect(destRect, 0);
115:
116: // Hack: derive the source format tag as if it was writing to
117: // a destination with the same layout as itself.
118: int sourceFormatTag = MediaLibAccessor.findCompatibleTag(
119: sources, source);
120:
121: // Hard-code the destination format tag as we know that the image
122: // has a binary layout.
123: int destFormatTag = dest.getSampleModel().getDataType()
124: | MediaLibAccessor.BINARY | MediaLibAccessor.UNCOPIED;
125:
126: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
127: srcRect, sourceFormatTag, false);
128: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
129: destRect, destFormatTag, true);
130:
131: mediaLibImage srcML[], dstML[];
132:
133: switch (srcAccessor.getDataType()) {
134: case DataBuffer.TYPE_BYTE:
135: case DataBuffer.TYPE_USHORT:
136: case DataBuffer.TYPE_SHORT:
137: case DataBuffer.TYPE_INT:
138: srcML = srcAccessor.getMediaLibImages();
139: dstML = dstAccessor.getMediaLibImages();
140: for (int i = 0; i < dstML.length; i++) {
141: //XXX
142: //medialib version is not inclusive
143: //pure java version is inclusive
144: //thus medialib version is the same
145: //as the java version with thresh value decremented by 1
146: Image.Thresh1(dstML[i], srcML[i],
147: new int[] { (int) thresh - 1 }, // decrease by 1
148: new int[] { 1 }, new int[] { 0 });
149: }
150: break;
151: case DataBuffer.TYPE_FLOAT:
152: case DataBuffer.TYPE_DOUBLE:
153: srcML = srcAccessor.getMediaLibImages();
154: dstML = dstAccessor.getMediaLibImages();
155: for (int i = 0; i < dstML.length; i++) {
156: Image.Thresh1_Fp(dstML[i], srcML[i],
157: new double[] { thresh }, new double[] { 1.0D },
158: new double[] { 0.0D });
159: }
160: break;
161: default:
162: String className = this .getClass().getName();
163: throw new RuntimeException(JaiI18N.getString("Generic2"));
164: }
165:
166: if (dstAccessor.isDataCopy()) {
167: dstAccessor.clampDataArrays();
168: dstAccessor.copyDataToRaster();
169: }
170: }
171:
172: }
|