001: /*
002: * $RCSfile: MlibScaleTableOpImage.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/12/15 18:35:47 $
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.BorderExtender;
020: import javax.media.jai.ImageLayout;
021: import javax.media.jai.Interpolation;
022: import javax.media.jai.InterpolationTable;
023: import javax.media.jai.OpImage;
024: import java.util.Map;
025: import com.sun.medialib.mlib.*;
026:
027: /**
028: * An OpImage class that scales an image using interpolation coefficients
029: * specified in a table format.
030: *
031: */
032: final class MlibScaleTableOpImage extends MlibScaleOpImage {
033:
034: /**
035: * Constructs an MlibScaleTableOpImage. The image dimensions are copied
036: * from the source image. The tile grid layout, SampleModel, and
037: * ColorModel may optionally be specified by an ImageLayout object.
038: *
039: * @param source a RenderedImage.
040: * @param extender a BorderExtender, or null.
041: * @param layout an ImageLayout optionally containing the tile
042: * grid layout, SampleModel, and ColorModel, or null.
043: * @param xScale the x scaling factor.
044: * @param yScale the y scaling factor.
045: * @param xTrans the x translation factor.
046: * @param yTrans the y translation factor.
047: * @param interp the InterpolationTable object.
048: */
049: public MlibScaleTableOpImage(RenderedImage source,
050: BorderExtender extender, Map config, ImageLayout layout,
051: float xScale, float yScale, float xTrans, float yTrans,
052: Interpolation interp) {
053: super (source, extender, config, layout, xScale, yScale, xTrans,
054: yTrans, interp, true);
055: }
056:
057: /**
058: * Scale the given rectangle by the specified scale and translation
059: * factors. The sources are cobbled.
060: *
061: * @param sources an array of sources, guarantee to provide all
062: * necessary source data for computing the rectangle.
063: * @param dest a tile that contains the rectangle to be computed.
064: * @param destRect the rectangle within this OpImage to be processed.
065: */
066: protected void computeRect(Raster[] sources, WritableRaster dest,
067: Rectangle destRect) {
068:
069: // Cast the Interpolation object to InterpolationTable object
070: InterpolationTable jtable = (InterpolationTable) interp;
071:
072: // The Medialib InterpolationTable class equivalent
073: mediaLibImageInterpTable mlibInterpTable;
074:
075: Raster source = sources[0];
076: Rectangle srcRect = source.getBounds();
077:
078: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
079: dest);
080:
081: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
082: srcRect, formatTag);
083: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
084: destRect, formatTag);
085:
086: // Get the floating point scale factors
087: float mlibScaleX = (float) scaleXRationalNum
088: / (float) scaleXRationalDenom;
089: float mlibScaleY = (float) scaleYRationalNum
090: / (float) scaleYRationalDenom;
091:
092: // Translation to be specified to Medialib. Note that we have to
093: // specify an additional translation since all images are 0 based
094: // in Medialib.
095:
096: // Calculate intermediate values.
097: float tempDX = (float) (srcRect.x * scaleXRationalNum)
098: / (float) scaleXRationalDenom;
099: float tempDY = (float) (srcRect.y * scaleYRationalNum)
100: / (float) scaleYRationalDenom;
101:
102: float tx = transX - destRect.x + tempDX;
103: float ty = transY - destRect.y + tempDY;
104:
105: mediaLibImage srcML[], dstML[];
106:
107: switch (dstAccessor.getDataType()) {
108: case DataBuffer.TYPE_BYTE:
109: case DataBuffer.TYPE_USHORT:
110: case DataBuffer.TYPE_SHORT:
111: case DataBuffer.TYPE_INT:
112:
113: mlibInterpTable = new mediaLibImageInterpTable(
114: Constants.MLIB_INT, jtable.getWidth(), jtable
115: .getHeight(), jtable.getLeftPadding(),
116: jtable.getTopPadding(), jtable.getSubsampleBitsH(),
117: jtable.getSubsampleBitsV(), jtable
118: .getPrecisionBits(), jtable
119: .getHorizontalTableData(), jtable
120: .getVerticalTableData());
121:
122: srcML = srcAccessor.getMediaLibImages();
123: dstML = dstAccessor.getMediaLibImages();
124: for (int i = 0; i < dstML.length; i++) {
125: Image.ZoomTranslateTable(dstML[i], srcML[i],
126: (double) mlibScaleX, (double) mlibScaleY,
127: (double) tx, (double) ty, mlibInterpTable,
128: Constants.MLIB_EDGE_DST_NO_WRITE);
129: MlibUtils.clampImage(dstML[i], getColorModel());
130: }
131: break;
132:
133: case DataBuffer.TYPE_FLOAT:
134:
135: mlibInterpTable = new mediaLibImageInterpTable(
136: Constants.MLIB_FLOAT, jtable.getWidth(), jtable
137: .getHeight(), jtable.getLeftPadding(),
138: jtable.getTopPadding(), jtable.getSubsampleBitsH(),
139: jtable.getSubsampleBitsV(), jtable
140: .getPrecisionBits(), jtable
141: .getHorizontalTableDataFloat(), jtable
142: .getVerticalTableDataFloat());
143:
144: srcML = srcAccessor.getMediaLibImages();
145: dstML = dstAccessor.getMediaLibImages();
146: for (int i = 0; i < dstML.length; i++) {
147: Image.ZoomTranslateTable_Fp(dstML[i], srcML[i],
148: (double) mlibScaleX, (double) mlibScaleY,
149: (double) tx, (double) ty, mlibInterpTable,
150: Constants.MLIB_EDGE_DST_NO_WRITE);
151: }
152: break;
153:
154: case DataBuffer.TYPE_DOUBLE:
155:
156: mlibInterpTable = new mediaLibImageInterpTable(
157: Constants.MLIB_DOUBLE, jtable.getWidth(), jtable
158: .getHeight(), jtable.getLeftPadding(),
159: jtable.getTopPadding(), jtable.getSubsampleBitsH(),
160: jtable.getSubsampleBitsV(), jtable
161: .getPrecisionBits(), jtable
162: .getHorizontalTableDataDouble(), jtable
163: .getVerticalTableDataDouble());
164:
165: srcML = srcAccessor.getMediaLibImages();
166: dstML = dstAccessor.getMediaLibImages();
167: for (int i = 0; i < dstML.length; i++) {
168: Image.ZoomTranslateTable_Fp(dstML[i], srcML[i],
169: (double) mlibScaleX, (double) mlibScaleY,
170: (double) tx, (double) ty, mlibInterpTable,
171: Constants.MLIB_EDGE_DST_NO_WRITE);
172: }
173: break;
174:
175: default:
176: String className = this .getClass().getName();
177: throw new RuntimeException(JaiI18N.getString("Generic2"));
178: }
179:
180: if (dstAccessor.isDataCopy()) {
181: dstAccessor.clampDataArrays();
182: dstAccessor.copyDataToRaster();
183: }
184: }
185: }
|