001: /*
002: * $RCSfile: SubsampleAverageCRIF.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:43 $
010: * $State: Exp $
011: */package com.sun.media.jai.opimage;
012:
013: import java.awt.RenderingHints;
014: import java.awt.geom.AffineTransform;
015: import java.awt.geom.Rectangle2D;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.renderable.RenderContext;
018: import java.awt.image.renderable.ParameterBlock;
019: import java.awt.image.renderable.RenderableImage;
020: import javax.media.jai.ImageLayout;
021: import javax.media.jai.CRIFImpl;
022:
023: /**
024: * @see SubsampleAverageOpImage
025: */
026: public class SubsampleAverageCRIF extends CRIFImpl {
027:
028: /** Constructor. */
029: public SubsampleAverageCRIF() {
030: super ("SubsampleAverage");
031: }
032:
033: /**
034: * Creates a new instance of SubsampleAverageOpImage in the rendered layer.
035: * This method satisfies the implementation of RIF.
036: *
037: * @param paramBlock The source image, the X and Y scale factor,
038: * and the interpolation method for resampling.
039: */
040: public RenderedImage create(ParameterBlock paramBlock,
041: RenderingHints renderHints) {
042:
043: // Get ImageLayout from renderHints if any.
044: ImageLayout layout = RIFUtil.getImageLayoutHint(renderHints);
045:
046: RenderedImage source = paramBlock.getRenderedSource(0);
047: double scaleX = paramBlock.getDoubleParameter(0);
048: double scaleY = paramBlock.getDoubleParameter(1);
049:
050: // Check and see if we are scaling by 1.0 in both x and y and no
051: // translations. If so return the source directly.
052: if (scaleX == 1.0 && scaleY == 1.0) {
053: return source;
054: }
055:
056: return new SubsampleAverageOpImage(source, layout, renderHints,
057: scaleX, scaleY);
058: }
059:
060: /**
061: * Creates a new instance of <code>SubsampleAverageOpImage</code>
062: * in the renderable layer. This method satisfies the
063: * implementation of CRIF.
064: */
065: public RenderedImage create(RenderContext renderContext,
066: ParameterBlock paramBlock) {
067: return paramBlock.getRenderedSource(0);
068: }
069:
070: /**
071: * Maps the output RenderContext into the RenderContext for the ith
072: * source.
073: * This method satisfies the implementation of CRIF.
074: *
075: * @param i The index of the source image.
076: * @param renderContext The renderContext being applied to the operation.
077: * @param paramBlock The ParameterBlock containing the sources
078: * and the translation factors.
079: * @param image The RenderableImageOp from which this method
080: * was called.
081: */
082: public RenderContext mapRenderContext(int i,
083: RenderContext renderContext, ParameterBlock paramBlock,
084: RenderableImage image) {
085:
086: double scaleX = paramBlock.getDoubleParameter(0);
087: double scaleY = paramBlock.getDoubleParameter(1);
088:
089: AffineTransform scale = new AffineTransform(scaleX, 0.0, 0.0,
090: scaleY, 0.0, 0.0);
091:
092: RenderContext RC = (RenderContext) renderContext.clone();
093: AffineTransform usr2dev = RC.getTransform();
094: usr2dev.concatenate(scale);
095: RC.setTransform(usr2dev);
096: return RC;
097: }
098:
099: /**
100: * Gets the bounding box for the output of <code>ScaleOpImage</code>.
101: * This method satisfies the implementation of CRIF.
102: */
103: public Rectangle2D getBounds2D(ParameterBlock paramBlock) {
104:
105: RenderableImage source = paramBlock.getRenderableSource(0);
106:
107: double scaleX = paramBlock.getDoubleParameter(0);
108: double scaleY = paramBlock.getDoubleParameter(1);
109:
110: // Get the source dimensions
111: float x0 = (float) source.getMinX();
112: float y0 = (float) source.getMinY();
113: float w = (float) source.getWidth();
114: float h = (float) source.getHeight();
115:
116: // Forward map the source using x0, y0, w and h
117: float d_x0 = (float) (x0 * scaleX);
118: float d_y0 = (float) (y0 * scaleY);
119: float d_w = (float) (w * scaleX);
120: float d_h = (float) (h * scaleY);
121:
122: return new Rectangle2D.Float(d_x0, d_y0, d_w, d_h);
123: }
124:
125: }
|