001: /*
002: * $RCSfile: MlibThresholdOpImage.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:07 $
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.Raster;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.WritableRaster;
019: import javax.media.jai.ImageLayout;
020: import javax.media.jai.PointOpImage;
021: import java.util.Map;
022: import com.sun.media.jai.util.ImageUtil;
023: import com.sun.medialib.mlib.*;
024:
025: /**
026: * An <code>OpImage</code> implementing the "Threshold" operation
027: * using MediaLib.
028: *
029: * @see javax.media.jai.operator.ThresholdDescriptor
030: * @see MlibThresholdRIF
031: *
032: * @since 1.0
033: *
034: */
035: final class MlibThresholdOpImage extends PointOpImage {
036:
037: /** The lower bound, one for each band. */
038: private double[] low;
039:
040: /** The integer version of lower bound. */
041: private int[] lowInt;
042:
043: /** The upper bound, one for each band. */
044: private double[] high;
045:
046: /** The integer version of upper bound. */
047: private int[] highInt;
048:
049: /** The constants to be mapped, one for each band. */
050: private double[] constants;
051:
052: /** The integer version of constants. */
053: private int[] constantsInt;
054:
055: /** Constructor. */
056: public MlibThresholdOpImage(RenderedImage source, Map config,
057: ImageLayout layout, double[] low, double[] high,
058: double[] constants) {
059: super (source, layout, config, true);
060:
061: int numBands = getSampleModel().getNumBands();
062: this .low = new double[numBands];
063: this .lowInt = new int[numBands];
064: this .high = new double[numBands];
065: this .highInt = new int[numBands];
066: this .constants = new double[numBands];
067: this .constantsInt = new int[numBands];
068:
069: for (int i = 0; i < numBands; i++) {
070: if (low.length < numBands) {
071: this .low[i] = low[0];
072: } else {
073: this .low[i] = low[i];
074: }
075: this .lowInt[i] = ImageUtil.clampInt((int) Math
076: .ceil(this .low[i]));
077:
078: if (high.length < numBands) {
079: this .high[i] = high[0];
080: } else {
081: this .high[i] = high[i];
082: }
083: this .highInt[i] = ImageUtil.clampInt((int) Math
084: .floor(this .high[i]));
085:
086: if (constants.length < numBands) {
087: this .constants[i] = constants[0];
088: } else {
089: this .constants[i] = constants[i];
090: }
091: this .constantsInt[i] = ImageUtil
092: .clampRoundInt(this .constants[i]);
093: }
094: // Set flag to permit in-place operation.
095: permitInPlaceOperation();
096: }
097:
098: /**
099: * Performs the "Threshold" operation on a rectangular region of
100: * the same.
101: */
102: protected void computeRect(Raster[] sources, WritableRaster dest,
103: Rectangle destRect) {
104: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
105: dest);
106:
107: MediaLibAccessor srcMA = new MediaLibAccessor(sources[0],
108: destRect, formatTag);
109: MediaLibAccessor dstMA = new MediaLibAccessor(dest, destRect,
110: formatTag);
111:
112: mediaLibImage[] srcMLI = srcMA.getMediaLibImages();
113: mediaLibImage[] dstMLI = dstMA.getMediaLibImages();
114:
115: switch (dstMA.getDataType()) {
116: case DataBuffer.TYPE_BYTE:
117: case DataBuffer.TYPE_USHORT:
118: case DataBuffer.TYPE_SHORT:
119: case DataBuffer.TYPE_INT:
120: for (int i = 0; i < dstMLI.length; i++) {
121: int[] mlLow = dstMA.getIntParameters(i, lowInt);
122: int[] mlHigh = dstMA.getIntParameters(i, highInt);
123: int[] mlConstants = dstMA.getIntParameters(i,
124: constantsInt);
125: Image.Thresh5(dstMLI[i], srcMLI[i], mlHigh, mlLow,
126: mlConstants);
127: }
128: break;
129:
130: case DataBuffer.TYPE_FLOAT:
131: case DataBuffer.TYPE_DOUBLE:
132: for (int i = 0; i < dstMLI.length; i++) {
133: double[] mlLow = dstMA.getDoubleParameters(i, low);
134: double[] mlHigh = dstMA.getDoubleParameters(i, high);
135: double[] mlConstants = dstMA.getDoubleParameters(i,
136: constants);
137: Image.Thresh5_Fp(dstMLI[i], srcMLI[i], mlHigh, mlLow,
138: mlConstants);
139: }
140: break;
141:
142: default:
143: throw new RuntimeException(JaiI18N.getString("Generic2"));
144: }
145:
146: if (dstMA.isDataCopy()) {
147: dstMA.clampDataArrays();
148: dstMA.copyDataToRaster();
149: }
150: }
151: }
|