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