001: /*
002: * $RCSfile: ImageFunctionOpImage.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:29 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.DataBuffer;
016: import java.awt.image.SampleModel;
017: import java.awt.image.WritableRaster;
018: import java.lang.ref.SoftReference;
019: import javax.media.jai.ImageFunction;
020: import javax.media.jai.ImageLayout;
021: import javax.media.jai.PlanarImage;
022: import javax.media.jai.RasterFactory;
023: import javax.media.jai.SourcelessOpImage;
024: import java.util.Map;
025:
026: /**
027: * An OpImage class to generate an image from a functional description.
028: *
029: * @see javax.media.jai.operator.ImageFunctionDescriptor
030: * @see javax.media.jai.ImageFunction
031: * @since EA4
032: */
033: final class ImageFunctionOpImage extends SourcelessOpImage {
034: /** The functional description of the image. */
035: protected ImageFunction function;
036:
037: /** The X scale factor. */
038: protected float xScale;
039:
040: /** The Y scale factor. */
041: protected float yScale;
042:
043: /** The X translation. */
044: protected float xTrans;
045:
046: /** The Y translation. */
047: protected float yTrans;
048:
049: private static SampleModel sampleModelHelper(int numBands,
050: ImageLayout layout) {
051: SampleModel sampleModel;
052: if (layout != null
053: && layout.isValid(ImageLayout.SAMPLE_MODEL_MASK)) {
054: sampleModel = layout.getSampleModel(null);
055:
056: if (sampleModel.getNumBands() != numBands) {
057: throw new RuntimeException(JaiI18N
058: .getString("ImageFunctionRIF0"));
059: }
060: } else { // Create a SampleModel.
061: // Use a dummy width and height, OpImage will fix them
062: sampleModel = RasterFactory.createBandedSampleModel(
063: DataBuffer.TYPE_FLOAT, 1, 1, numBands);
064: }
065:
066: return sampleModel;
067: }
068:
069: /**
070: * Constructs an ImageFunctionOpImage.
071: *
072: * @param width The output image width.
073: * @param height The output image height.
074: */
075: public ImageFunctionOpImage(ImageFunction function, int minX,
076: int minY, int width, int height, float xScale,
077: float yScale, float xTrans, float yTrans, Map config,
078: ImageLayout layout) {
079: super (layout, config, sampleModelHelper(function
080: .getNumElements()
081: * (function.isComplex() ? 2 : 1), layout), minX, minY,
082: width, height);
083:
084: // Cache the parameters.
085: this .function = function;
086: this .xScale = xScale;
087: this .yScale = yScale;
088: this .xTrans = xTrans;
089: this .yTrans = yTrans;
090: }
091:
092: /**
093: * Compute a Rectangle of output data based on the ImageFunction.
094: * Note that the sources parameter is not used.
095: */
096: protected void computeRect(PlanarImage[] sources,
097: WritableRaster dest, Rectangle destRect) {
098: // Cache some info.
099: int dataType = sampleModel.getTransferType();
100: int numBands = sampleModel.getNumBands();
101:
102: // Allocate the actual data memory.
103: int length = width * height;
104: Object data;
105: if (dataType == DataBuffer.TYPE_DOUBLE) {
106: data = function.isComplex() ? (Object) new double[2][length]
107: : (Object) new double[length];
108: } else {
109: data = function.isComplex() ? (Object) new float[2][length]
110: : (Object) new float[length];
111: }
112:
113: if (dataType == DataBuffer.TYPE_DOUBLE) {
114: double[] real = function.isComplex() ? ((double[][]) data)[0]
115: : ((double[]) data);
116: double[] imag = function.isComplex() ? ((double[][]) data)[1]
117: : null;
118:
119: int element = 0;
120: for (int band = 0; band < numBands; band++) {
121: function.getElements(xScale * (destRect.x - xTrans),
122: yScale * (destRect.y - yTrans), xScale, yScale,
123: destRect.width, destRect.height, element++,
124: real, imag);
125: dest.setSamples(destRect.x, destRect.y, destRect.width,
126: destRect.height, band, (double[]) real);
127: if (function.isComplex()) {
128: dest.setSamples(destRect.x, destRect.y,
129: destRect.width, destRect.height, ++band,
130: imag);
131: }
132: } // for (band ...
133: } else { // not double precision
134: float[] real = function.isComplex() ? ((float[][]) data)[0]
135: : ((float[]) data);
136: float[] imag = function.isComplex() ? ((float[][]) data)[1]
137: : null;
138:
139: int element = 0;
140: for (int band = 0; band < numBands; band++) {
141: function.getElements(xScale * (destRect.x - xTrans),
142: yScale * (destRect.y - yTrans), xScale, yScale,
143: destRect.width, destRect.height, element++,
144: real, imag);
145: dest.setSamples(destRect.x, destRect.y, destRect.width,
146: destRect.height, band, real);
147: if (function.isComplex()) {
148: dest.setSamples(destRect.x, destRect.y,
149: destRect.width, destRect.height, ++band,
150: imag);
151: }
152: } // for (band ...
153: }
154: }
155: }
|