001: /*
002: * $RCSfile: MlibClampOpImage.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.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 "Clamp" operation
027: * using MediaLib.
028: *
029: * @see javax.media.jai.operator.ClampDescriptor
030: * @see MlibClampRIF
031: *
032: * @since 1.0
033: *
034: */
035: final class MlibClampOpImage 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: /** Constructor. */
050: public MlibClampOpImage(RenderedImage source, Map config,
051: ImageLayout layout, double[] low, double[] high) {
052: super (source, layout, config, true);
053:
054: int numBands = getSampleModel().getNumBands();
055: this .low = new double[numBands];
056: this .lowInt = new int[numBands];
057: this .high = new double[numBands];
058: this .highInt = new int[numBands];
059:
060: for (int i = 0; i < numBands; i++) {
061: if (low.length < numBands) {
062: this .low[i] = low[0];
063: } else {
064: this .low[i] = low[i];
065: }
066: this .lowInt[i] = ImageUtil.clampRoundInt(this .low[i]);
067:
068: if (high.length < numBands) {
069: this .high[i] = high[0];
070: } else {
071: this .high[i] = high[i];
072: }
073: this .highInt[i] = ImageUtil.clampRoundInt(this .high[i]);
074: }
075: // Set flag to permit in-place operation.
076: permitInPlaceOperation();
077: }
078:
079: /**
080: * Performs the "Clamp" operation on a rectangular region of
081: * the same.
082: */
083: protected void computeRect(Raster[] sources, WritableRaster dest,
084: Rectangle destRect) {
085: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
086: dest);
087:
088: MediaLibAccessor srcMA = new MediaLibAccessor(sources[0],
089: destRect, formatTag);
090: MediaLibAccessor dstMA = new MediaLibAccessor(dest, destRect,
091: formatTag);
092:
093: mediaLibImage[] srcMLI = srcMA.getMediaLibImages();
094: mediaLibImage[] dstMLI = dstMA.getMediaLibImages();
095:
096: switch (dstMA.getDataType()) {
097: case DataBuffer.TYPE_BYTE:
098: case DataBuffer.TYPE_USHORT:
099: case DataBuffer.TYPE_SHORT:
100: case DataBuffer.TYPE_INT:
101: for (int i = 0; i < dstMLI.length; i++) {
102: int[] mlLow = dstMA.getIntParameters(i, lowInt);
103: int[] mlHigh = dstMA.getIntParameters(i, highInt);
104: Image.Thresh4(dstMLI[i], srcMLI[i], mlHigh, mlLow,
105: mlHigh, mlLow);
106: }
107: break;
108:
109: case DataBuffer.TYPE_FLOAT:
110: case DataBuffer.TYPE_DOUBLE:
111: for (int i = 0; i < dstMLI.length; i++) {
112: double[] mlLow = dstMA.getDoubleParameters(i, low);
113: double[] mlHigh = dstMA.getDoubleParameters(i, high);
114: Image.Thresh4_Fp(dstMLI[i], srcMLI[i], mlHigh, mlLow,
115: mlHigh, mlLow);
116: }
117: break;
118:
119: default:
120: throw new RuntimeException(JaiI18N.getString("Generic2"));
121: }
122:
123: if (dstMA.isDataCopy()) {
124: dstMA.clampDataArrays();
125: dstMA.copyDataToRaster();
126: }
127: }
128: }
|