001: /*
002: * $RCSfile: MlibAffineTableOpImage.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.3 $
009: * $Date: 2005/12/15 18:35:46 $
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.SampleModel;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.WritableRaster;
020: import java.awt.image.renderable.ParameterBlock;
021: import java.awt.image.renderable.RenderedImageFactory;
022: import java.awt.geom.AffineTransform;
023: import javax.media.jai.AreaOpImage;
024: import javax.media.jai.ImageLayout;
025: import javax.media.jai.Interpolation;
026: import javax.media.jai.InterpolationTable;
027: import javax.media.jai.KernelJAI;
028: import javax.media.jai.OpImage;
029: import java.util.Map;
030: import javax.media.jai.BorderExtender;
031: import com.sun.medialib.mlib.*;
032:
033: // import com.sun.media.jai.test.OpImageTester;
034:
035: /**
036: * An OpImage class to perform an Affine using interpolation coefficients
037: * represented in a Table, on a source image.
038: *
039: */
040: public class MlibAffineTableOpImage extends MlibAffineOpImage {
041:
042: /**
043: * Creates a MlibAffineTableOpImage given a ParameterBlock containing the
044: * image source and the AffineTransform. The image dimensions are derived
045: * from the source image. The tile grid layout, SampleModel, and
046: * ColorModel may optionally be specified by an ImageLayout
047: * object.
048: *
049: * @param source a RenderedImage.
050: * @param layout an ImageLayout optionally containing the tile grid layout,
051: * SampleModel, and ColorModel, or null.
052: * @param tr the AffineTransform.
053: * @param interp the Interpolation to be used (Table-based)
054: */
055: public MlibAffineTableOpImage(RenderedImage source,
056: BorderExtender extender, Map config, ImageLayout layout,
057: AffineTransform tr, Interpolation interp,
058: double[] backgroundValues) {
059: super (source, layout, config, extender, tr, interp,
060: backgroundValues);
061: }
062:
063: /**
064: * Performs nearest-neighbour affine transformation on a specified
065: * rectangle. The sources are cobbled.
066: *
067: * @param sources an array of source Rasters, guaranteed to provide all
068: * necessary source data for computing the output.
069: * @param dest a WritableRaster tile containing the area to be computed.
070: * @param destRect the rectangle within dest to be processed.
071: */
072: protected void computeRect(Raster[] sources, WritableRaster dest,
073: Rectangle destRect) {
074:
075: // Cast the Interpolation object to InterpolationTable object
076: InterpolationTable jtable = (InterpolationTable) interp;
077:
078: // The Medialib InterpolationTable class equivalent
079: mediaLibImageInterpTable mlibInterpTable;
080:
081: Raster source = sources[0];
082: Rectangle srcRect = source.getBounds();
083:
084: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
085: dest);
086:
087: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
088: srcRect, formatTag);
089: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
090: destRect, formatTag);
091:
092: //
093: // The AffineTransform needs to be readjusted as per the
094: // location of the current source & destination rectangles
095: //
096:
097: // Clone the global transform so as not to write to an instance
098: // variable as this method may be called from different threads.
099: double[] medialib_tr = (double[]) this .medialib_tr.clone();
100:
101: medialib_tr[2] = m_transform[0] * srcRect.x + m_transform[1]
102: * srcRect.y + m_transform[2] - destRect.x;
103: medialib_tr[5] = m_transform[3] * srcRect.x + m_transform[4]
104: * srcRect.y + m_transform[5] - destRect.y;
105:
106: mediaLibImage srcML[], dstML[];
107:
108: switch (dstAccessor.getDataType()) {
109: case DataBuffer.TYPE_BYTE:
110: case DataBuffer.TYPE_USHORT:
111: case DataBuffer.TYPE_SHORT:
112: case DataBuffer.TYPE_INT:
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:
125: if (setBackground)
126: Image.AffineTable2(dstML[0], srcML[0], medialib_tr,
127: mlibInterpTable,
128: Constants.MLIB_EDGE_DST_NO_WRITE,
129: intBackgroundValues);
130: else
131: Image.AffineTable(dstML[0], srcML[0], medialib_tr,
132: mlibInterpTable,
133: Constants.MLIB_EDGE_DST_NO_WRITE);
134: MlibUtils.clampImage(dstML[0], getColorModel());
135: break;
136:
137: case DataBuffer.TYPE_FLOAT:
138: mlibInterpTable = new mediaLibImageInterpTable(
139: Constants.MLIB_FLOAT, jtable.getWidth(), jtable
140: .getHeight(), jtable.getLeftPadding(),
141: jtable.getTopPadding(), jtable.getSubsampleBitsH(),
142: jtable.getSubsampleBitsV(), jtable
143: .getPrecisionBits(), jtable
144: .getHorizontalTableDataFloat(), jtable
145: .getVerticalTableDataFloat());
146:
147: srcML = srcAccessor.getMediaLibImages();
148: dstML = dstAccessor.getMediaLibImages();
149:
150: if (setBackground)
151: Image.AffineTable2_Fp(dstML[0], srcML[0], medialib_tr,
152: mlibInterpTable,
153: Constants.MLIB_EDGE_DST_NO_WRITE,
154: backgroundValues);
155: else
156: Image.AffineTable_Fp(dstML[0], srcML[0], medialib_tr,
157: mlibInterpTable,
158: Constants.MLIB_EDGE_DST_NO_WRITE);
159: break;
160:
161: case DataBuffer.TYPE_DOUBLE:
162: mlibInterpTable = new mediaLibImageInterpTable(
163: Constants.MLIB_DOUBLE, jtable.getWidth(), jtable
164: .getHeight(), jtable.getLeftPadding(),
165: jtable.getTopPadding(), jtable.getSubsampleBitsH(),
166: jtable.getSubsampleBitsV(), jtable
167: .getPrecisionBits(), jtable
168: .getHorizontalTableDataDouble(), jtable
169: .getVerticalTableDataDouble());
170:
171: srcML = srcAccessor.getMediaLibImages();
172: dstML = dstAccessor.getMediaLibImages();
173:
174: if (setBackground)
175: Image.AffineTable2_Fp(dstML[0], srcML[0], medialib_tr,
176: mlibInterpTable,
177: Constants.MLIB_EDGE_DST_NO_WRITE,
178: backgroundValues);
179: else
180: Image.AffineTable_Fp(dstML[0], srcML[0], medialib_tr,
181: mlibInterpTable,
182: Constants.MLIB_EDGE_DST_NO_WRITE);
183: break;
184:
185: default:
186: String className = this .getClass().getName();
187: throw new RuntimeException(JaiI18N.getString("Generic2"));
188: }
189:
190: if (dstAccessor.isDataCopy()) {
191: dstAccessor.copyDataToRaster();
192: }
193: }
194: }
|