001: /*
002: * $RCSfile: MlibFilteredSubsampleOpImage.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:55:56 $
010: * $State: Exp $
011: */package com.sun.media.jai.mlib;
012:
013: import java.awt.Rectangle;
014: import java.awt.image.DataBuffer;
015: import java.awt.image.Raster;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.WritableRaster;
018: import javax.media.jai.ImageLayout;
019: import javax.media.jai.Interpolation;
020: import javax.media.jai.InterpolationNearest;
021: import javax.media.jai.OpImage;
022: import java.util.Map;
023: import javax.media.jai.BorderExtender;
024: import com.sun.media.jai.opimage.FilteredSubsampleOpImage;
025: import com.sun.medialib.mlib.*;
026:
027: /**
028: * <p> A class extending <code>FilteredSubsampleOpImage</code> to
029: * subsample and antialias filter images using medialib.
030: *
031: * @see FilteredSubsampleOpImage
032: */
033: final class MlibFilteredSubsampleOpImage extends
034: FilteredSubsampleOpImage {
035:
036: protected double[] m_hKernel;
037: protected double[] m_vKernel;
038:
039: private static final boolean DEBUG = false;
040:
041: /** <p> <code>MlibFilteredSubsampleOpImage</code> constructs an OpImage representing
042: * filtered integral subsampling. The scale factors represent the ratio of
043: * source to destination dimensions.
044: *
045: * @param source a RenderedImage.
046: * @param extender a BorderExtender, or null.
047: * @param config a Map object possibly holding tile cache information
048: * @param layout an ImageLayout optionally containing the tile grid layout,
049: * SampleModel, and ColorModel, or null.
050: * @param interp a Interpolation object to use for resampling.
051: * @param scaleX downsample factor along x axis.
052: * @param scaleY downsample factor along y axis.
053: * @param qsFilter symmetric filter coefficients (partial kernel).
054: * @throws IllegalArgumentException if the interp type is not one of:
055: * INTERP_NEAREST, INTERP_BILINEAR, INTERP_BICUBIC, or INTERP_BICUBIC_2
056: */
057: public MlibFilteredSubsampleOpImage(RenderedImage source,
058: BorderExtender extender, Map config, ImageLayout layout,
059: int scaleX, int scaleY, float[] qsFilter,
060: Interpolation interp) {
061:
062: // Propagate to FilteredSubsampleOpImage constructor
063: super (source, extender, config, layout, scaleX, scaleY,
064: qsFilter, interp);
065:
066: // If enabled, print debug information
067: if (DEBUG)
068: System.out.println("Object: MlibFilteredSubsampleOpImage");
069:
070: // Copy floating arrays to medialib double arrays
071: m_hKernel = new double[hKernel.length];
072: m_vKernel = new double[vKernel.length];
073: if (DEBUG)
074: System.out.print("\n hParity: " + hParity + " hKernel: ");
075: for (int i = 0; i < hKernel.length; i++) {
076: m_hKernel[i] = (double) hKernel[i];
077: if (DEBUG)
078: System.out.print(" " + m_hKernel[i]);
079: } // for i
080: if (DEBUG)
081: System.out.print("\n vParity: " + vParity + " vKernel: ");
082: for (int j = 0; j < vKernel.length; j++) {
083: m_vKernel[j] = (double) vKernel[j];
084: if (DEBUG)
085: System.out.print(" " + m_vKernel[j]);
086: } // for j
087: if (DEBUG)
088: System.out.println("\n");
089:
090: } // MlibFilteredSubsampleOpImage
091:
092: /**
093: * <p> Performs a combined subsample/filter operation on a specified rectangle.
094: * The sources are cobbled.
095: *
096: * @param sources an array of source Rasters, guaranteed to provide all
097: * necessary source data for computing the output.
098: * @param dest a WritableRaster containing the area to be computed.
099: * @param destRect the rectangle within dest to be processed.
100: */
101: public void computeRect(Raster[] sources, WritableRaster dest,
102: Rectangle destRect) {
103:
104: // Get RasterAccessor tag
105: int formatTag = MediaLibAccessor.findCompatibleTag(sources,
106: dest);
107:
108: // Get destination accessor.
109: MediaLibAccessor dst = new MediaLibAccessor(dest, destRect,
110: formatTag);
111:
112: // Get source accessor.
113: MediaLibAccessor src = new MediaLibAccessor(sources[0],
114: mapDestRect(destRect, 0), formatTag);
115:
116: // Medialib requires translation terms (java version doesn't)
117: int transX = m_hKernel.length - (scaleX + 1) / 2
118: - (hParity * (1 + scaleX)) % 2;
119: int transY = m_vKernel.length - (scaleY + 1) / 2
120: - (vParity * (1 + scaleY)) % 2;
121: mediaLibImage srcML[], dstML[];
122: switch (dst.getDataType()) {
123: case DataBuffer.TYPE_BYTE:
124: case DataBuffer.TYPE_USHORT:
125: case DataBuffer.TYPE_SHORT:
126: case DataBuffer.TYPE_INT:
127: srcML = src.getMediaLibImages();
128: dstML = dst.getMediaLibImages();
129: for (int i = 0; i < dstML.length; i++) {
130: Image.FilteredSubsample(dstML[i], srcML[i], scaleX,
131: scaleY, transX, transY, m_hKernel, m_vKernel,
132: hParity, vParity,
133: Constants.MLIB_EDGE_DST_NO_WRITE);
134: } // for
135: break;
136: case DataBuffer.TYPE_FLOAT:
137: case DataBuffer.TYPE_DOUBLE:
138: srcML = src.getMediaLibImages();
139: dstML = dst.getMediaLibImages();
140: for (int i = 0; i < dstML.length; i++) {
141: Image.FilteredSubsample_Fp(dstML[i], srcML[i], scaleX,
142: scaleY, transX, transY, m_hKernel, m_vKernel,
143: hParity, vParity,
144: Constants.MLIB_EDGE_DST_NO_WRITE);
145: } // for
146: break;
147: default:
148: throw new IllegalArgumentException(JaiI18N
149: .getString("Generic2"));
150: } // switch getDataType
151:
152: // If the RasterAccessor set up a temporary write buffer for the
153: // operator, tell it to copy that data to the destination Raster.
154: if (dst.isDataCopy()) {
155: dst.clampDataArrays();
156: dst.copyDataToRaster();
157: } // if isDataCopy
158:
159: } // computeRect
160:
161: } // class MlibFilteredSubsampleOpImage
|