001: /*
002: * $RCSfile: MlibHistogramRIF.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/11/23 22:25:07 $
010: * $State: Exp $
011: */package com.sun.media.jai.mlib;
012:
013: import java.awt.Rectangle;
014: import java.awt.RenderingHints;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.SampleModel;
018: import java.awt.image.renderable.ParameterBlock;
019: import java.awt.image.renderable.RenderedImageFactory;
020: import javax.media.jai.JAI;
021: import javax.media.jai.ROI;
022: import javax.media.jai.util.ImagingListener;
023: import com.sun.media.jai.util.ImageUtil;
024: import com.sun.media.jai.opimage.RIFUtil;
025:
026: /**
027: * A <code>RIF</code> supporting the "Histogram" operation in the
028: * rendered image layer.
029: *
030: * @since EA2
031: * @see javax.media.jai.operator.HistogramDescriptor
032: * @see MlibHistogramOpImage
033: */
034: public class MlibHistogramRIF implements RenderedImageFactory {
035:
036: /** Constructor. */
037: public MlibHistogramRIF() {
038: }
039:
040: /**
041: * Creates a new instance of <code>MlibHistogramOpImage</code>
042: * in the rendered layer. Any image layout information in
043: * <code>RenderingHints</code> is ignored.
044: * This method satisfies the implementation of RIF.
045: */
046: public RenderedImage create(ParameterBlock args,
047: RenderingHints hints) {
048:
049: // Return null of not mediaLib-compatible.
050: if (!MediaLibAccessor.isMediaLibCompatible(args)) {
051: return null;
052: }
053:
054: // Return null if source data type is floating point.
055: RenderedImage src = args.getRenderedSource(0);
056: int dataType = src.getSampleModel().getDataType();
057: if (dataType == DataBuffer.TYPE_FLOAT
058: || dataType == DataBuffer.TYPE_DOUBLE) {
059: return null;
060: }
061:
062: // Return null if ROI is non-null and not equals to source bounds.
063: ROI roi = (ROI) args.getObjectParameter(0);
064: if (roi != null
065: && !roi.equals(new Rectangle(src.getMinX(), src
066: .getMinY(), src.getWidth(), src.getHeight()))) {
067: return null;
068: }
069:
070: // Get the non-ROI parameters.
071: int xPeriod = args.getIntParameter(1);
072: int yPeriod = args.getIntParameter(2);
073: int[] numBins = (int[]) args.getObjectParameter(3);
074: double[] lowValueFP = (double[]) args.getObjectParameter(4);
075: double[] highValueFP = (double[]) args.getObjectParameter(5);
076:
077: // Return null if lowValueFP or highValueFP is out of dataType range.
078: int minPixelValue;
079: int maxPixelValue;
080: switch (dataType) {
081: case DataBuffer.TYPE_SHORT:
082: minPixelValue = Short.MIN_VALUE;
083: maxPixelValue = Short.MAX_VALUE;
084: break;
085: case DataBuffer.TYPE_USHORT:
086: minPixelValue = 0;
087: maxPixelValue = -((int) Short.MIN_VALUE) + Short.MAX_VALUE;
088: break;
089: case DataBuffer.TYPE_INT:
090: minPixelValue = Integer.MIN_VALUE;
091: maxPixelValue = Integer.MAX_VALUE;
092: break;
093: case DataBuffer.TYPE_BYTE:
094: default:
095: minPixelValue = 0;
096: maxPixelValue = -((int) Byte.MIN_VALUE) + Byte.MAX_VALUE;
097: break;
098: }
099: for (int i = 0; i < lowValueFP.length; i++) {
100: if (lowValueFP[i] < minPixelValue
101: || lowValueFP[i] > maxPixelValue) {
102: return null;
103: }
104: }
105: for (int i = 0; i < highValueFP.length; i++) {
106: if (highValueFP[i] <= minPixelValue
107: || highValueFP[i] > (maxPixelValue + 1)) {
108: return null;
109: }
110: }
111:
112: MlibHistogramOpImage op = null;
113: try {
114: op = new MlibHistogramOpImage(src, xPeriod, yPeriod,
115: numBins, lowValueFP, highValueFP);
116: } catch (Exception e) {
117: ImagingListener listener = ImageUtil
118: .getImagingListener(hints);
119: String message = JaiI18N.getString("MlibHistogramRIF0");
120: listener.errorOccurred(message, e, this , false);
121: }
122:
123: return op;
124: }
125: }
|