001: /*
002: * $RCSfile: MlibTranslateRIF.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:56:08 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.mlib;
013:
014: import java.awt.RenderingHints;
015: import java.awt.geom.AffineTransform;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.renderable.ParameterBlock;
018: import java.awt.image.renderable.RenderedImageFactory;
019: import javax.media.jai.BorderExtender;
020: import javax.media.jai.ImageLayout;
021: import javax.media.jai.Interpolation;
022: import javax.media.jai.InterpolationNearest;
023: import javax.media.jai.InterpolationBilinear;
024: import javax.media.jai.InterpolationBicubic;
025: import javax.media.jai.InterpolationBicubic2;
026: import javax.media.jai.InterpolationTable;
027: import java.util.Map;
028: import javax.media.jai.BorderExtender;
029: import com.sun.media.jai.opimage.RIFUtil;
030: import com.sun.media.jai.opimage.TranslateIntOpImage;
031:
032: /**
033: * A <code>RIF</code> supporting the "Translate" operation in the
034: * rendered image mode using MediaLib.
035: *
036: * @see javax.media.jai.operator.TranslateDescriptor
037: * @see MlibScaleOpImage
038: */
039: public class MlibTranslateRIF implements RenderedImageFactory {
040:
041: private static final float TOLERANCE = 0.01F;
042:
043: /** Constructor. */
044: public MlibTranslateRIF() {
045: }
046:
047: /**
048: * Creates a new instance of <code>MlibAffineOpImage</code> in
049: * the rendered image mode.
050: *
051: * @param args The source image, the <code>AffineTransform</code>,
052: * and the <code>Interpolation</code>.
053: * @param hints May contain rendering hints and destination image layout.
054: */
055: public RenderedImage create(ParameterBlock args,
056: RenderingHints hints) {
057: RenderedImage source = args.getRenderedSource(0);
058: float xTrans = args.getFloatParameter(0);
059: float yTrans = args.getFloatParameter(1);
060: Interpolation interp = (Interpolation) args
061: .getObjectParameter(2);
062:
063: /* Get ImageLayout and TileCache from RenderingHints. */
064: ImageLayout layout = RIFUtil.getImageLayoutHint(hints);
065:
066: if ((Math.abs(xTrans - (int) xTrans) < TOLERANCE)
067: && (Math.abs(yTrans - (int) yTrans) < TOLERANCE)
068: && layout == null) { // TranslateIntOpImage can't deal with ImageLayout hint
069: return new TranslateIntOpImage(source, hints, (int) xTrans,
070: (int) yTrans);
071: } else {
072:
073: if (!MediaLibAccessor.isMediaLibCompatible(args, layout)
074: || !MediaLibAccessor.hasSameNumBands(args, layout)
075: ||
076: // Medialib cannot deal with source image having tiles with any
077: // dimension greater than or equal to 32768
078: source.getTileWidth() >= 32768
079: || source.getTileHeight() >= 32768) {
080: return null;
081: }
082:
083: /* Get BorderExtender from hints if any. */
084: BorderExtender extender = RIFUtil
085: .getBorderExtenderHint(hints);
086:
087: /*
088: * Call the Scale operation, since it encapsulates Translate
089: * and is better optimized than Affine.
090: */
091: float xScale = 1.0F;
092: float yScale = 1.0F;
093: if (interp instanceof InterpolationNearest) {
094: return new MlibScaleNearestOpImage(source, extender,
095: hints, layout, xScale, yScale, xTrans, yTrans,
096: interp);
097: } else if (interp instanceof InterpolationBilinear) {
098: return new MlibScaleBilinearOpImage(source, extender,
099: hints, layout, xScale, yScale, xTrans, yTrans,
100: interp);
101: } else if (interp instanceof InterpolationBicubic
102: || interp instanceof InterpolationBicubic2) {
103: return new MlibScaleBicubicOpImage(source, extender,
104: hints, layout, xScale, yScale, xTrans, yTrans,
105: interp);
106: } else if (interp instanceof InterpolationTable) {
107: return new MlibScaleTableOpImage(source, extender,
108: hints, layout, xScale, yScale, xTrans, yTrans,
109: interp);
110: } else {
111: return null;
112: }
113: }
114: }
115: }
|