001: /*
002: * $RCSfile: MlibAffineBilinearOpImage.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:45 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.Rectangle;
015: import java.awt.geom.AffineTransform;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.Raster;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.SampleModel;
020: import java.awt.image.WritableRaster;
021: import java.awt.image.renderable.ParameterBlock;
022: import java.awt.image.renderable.RenderedImageFactory;
023: import javax.media.jai.AreaOpImage;
024: import javax.media.jai.BorderExtender;
025: import javax.media.jai.ImageLayout;
026: import javax.media.jai.Interpolation;
027: import javax.media.jai.InterpolationBilinear;
028: import javax.media.jai.KernelJAI;
029: import javax.media.jai.OpImage;
030: import java.util.Map;
031: import com.sun.medialib.mlib.*;
032:
033: // import com.sun.media.jai.test.OpImageTester;
034:
035: /**
036: * An OpImage class to perform Bilinear AffineTransform
037: * on a source image.
038: *
039: */
040: public class MlibAffineBilinearOpImage extends MlibAffineOpImage {
041:
042: /**
043: * Creates a MlibAffineBilinearOpImage given a ParameterBlock containing
044: * the image source and the AffineTransform. The image dimensions are
045: * derived 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 extender a BorderExtender, or null.
051: * @param layout an ImageLayout optionally containing the tile grid layout,
052: * SampleModel, and ColorModel, or null.
053: * @param tr the AffineTransform.
054: * @param interp the Interpolation to be used (Bilinear)
055: */
056: public MlibAffineBilinearOpImage(RenderedImage source,
057: BorderExtender extender, Map config, ImageLayout layout,
058: AffineTransform tr, Interpolation interp,
059: double[] backgroundValues) {
060: super (source, layout, config, extender, tr, interp,
061: backgroundValues);
062: }
063:
064: /**
065: * Performs bilinear affine transformation on a specified
066: * rectangle. The sources are cobbled and extended.
067: *
068: * @param sources an array of source Rasters, guaranteed to provide all
069: * necessary source data for computing the output.
070: * @param dest a WritableRaster tile containing the area to be computed.
071: * @param destRect the rectangle within dest to be processed.
072: */
073: protected void computeRect(Raster[] sources, WritableRaster dest,
074: Rectangle destRect) {
075:
076: Raster source = sources[0];
077: Rectangle srcRect = source.getBounds();
078:
079: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
080: dest);
081:
082: MediaLibAccessor srcAccessor = new MediaLibAccessor(source,
083: srcRect, formatTag);
084: MediaLibAccessor dstAccessor = new MediaLibAccessor(dest,
085: destRect, formatTag);
086:
087: //
088: // The AffineTransform needs to be readjusted as per the
089: // location of the current source & destination rectangles
090: //
091:
092: // Clone the global transform so as not to write to an instance
093: // variable as this method may be called from different threads.
094: double[] medialib_tr = (double[]) this .medialib_tr.clone();
095:
096: medialib_tr[2] = m_transform[0] * srcRect.x + m_transform[1]
097: * srcRect.y + m_transform[2] - destRect.x;
098: medialib_tr[5] = m_transform[3] * srcRect.x + m_transform[4]
099: * srcRect.y + m_transform[5] - destRect.y;
100:
101: mediaLibImage srcML[], dstML[];
102:
103: switch (dstAccessor.getDataType()) {
104: case DataBuffer.TYPE_BYTE:
105: case DataBuffer.TYPE_USHORT:
106: case DataBuffer.TYPE_SHORT:
107: case DataBuffer.TYPE_INT:
108: srcML = srcAccessor.getMediaLibImages();
109: dstML = dstAccessor.getMediaLibImages();
110:
111: if (setBackground)
112: Image.Affine2(dstML[0], srcML[0], medialib_tr,
113: Constants.MLIB_BILINEAR,
114: Constants.MLIB_EDGE_DST_NO_WRITE,
115: intBackgroundValues);
116: else
117: Image.Affine(dstML[0], srcML[0], medialib_tr,
118: Constants.MLIB_BILINEAR,
119: Constants.MLIB_EDGE_DST_NO_WRITE);
120: MlibUtils.clampImage(dstML[0], getColorModel());
121: break;
122:
123: case DataBuffer.TYPE_FLOAT:
124: case DataBuffer.TYPE_DOUBLE:
125: srcML = srcAccessor.getMediaLibImages();
126: dstML = dstAccessor.getMediaLibImages();
127:
128: if (setBackground)
129: Image.Affine2_Fp(dstML[0], srcML[0], medialib_tr,
130: Constants.MLIB_BILINEAR,
131: Constants.MLIB_EDGE_DST_NO_WRITE,
132: backgroundValues);
133: else
134: Image.Affine_Fp(dstML[0], srcML[0], medialib_tr,
135: Constants.MLIB_BILINEAR,
136: Constants.MLIB_EDGE_DST_NO_WRITE);
137: break;
138:
139: default:
140: String className = this .getClass().getName();
141: throw new RuntimeException(JaiI18N.getString("Generic2"));
142: }
143:
144: if (dstAccessor.isDataCopy()) {
145: dstAccessor.copyDataToRaster();
146: }
147: }
148:
149: // public static OpImage createTestImage(OpImageTester oit) {
150: // Interpolation interp = new InterpolationBilinear();
151: // AffineTransform tr = new AffineTransform(0.707107,
152: // -0.707106,
153: // 0.707106,
154: // 0.707107,
155: // 0.0,
156: // 0.0);
157: // return new MlibAffineBilinearOpImage(oit.getSource(), null, null,
158: // new ImageLayout(oit.getSource()),
159: // tr,
160: // interp);
161: // }
162:
163: // // Calls a method on OpImage that uses introspection, to make this
164: // // class, discover it's createTestImage() call, call it and then
165: // // benchmark the performance of the created OpImage chain.
166: // public static void main (String args[]) {
167: // String classname = "com.sun.media.jai.mlib.MlibAffineBilinearOpImage";
168: // OpImageTester.performDiagnostics(classname, args);
169: // }
170: }
|