001: /*
002: * $RCSfile: CropCRIF.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:21 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.RenderingHints;
015: import java.awt.Rectangle;
016: import java.awt.geom.AffineTransform;
017: import java.awt.geom.Rectangle2D;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.renderable.ParameterBlock;
020: import java.awt.image.renderable.RenderableImage;
021: import java.awt.image.renderable.RenderContext;
022: import javax.media.jai.CRIFImpl;
023: import javax.media.jai.ImageLayout;
024: import javax.media.jai.JAI;
025: import javax.media.jai.PlanarImage;
026: import java.util.Map;
027:
028: /**
029: * A <code>CRIF</code> supporting the "Crop" operation in the rendered
030: * and renderable image layers.
031: *
032: * @see javax.media.jai.operator.CropDescriptor
033: * @see CropOpImage
034: *
035: *
036: * @since EA4
037: */
038: public class CropCRIF extends CRIFImpl {
039:
040: /** Constructor. */
041: public CropCRIF() {
042: super ("crop");
043: }
044:
045: /**
046: * Creates a new instance of <code>CropOpImage</code> in the
047: * rendered layer.
048: *
049: * @param args The source image and bounding rectangle
050: * @param hints Optionally contains destination image layout.
051: */
052: public RenderedImage create(ParameterBlock args,
053: RenderingHints renderHints) {
054: // Get the source image.
055: RenderedImage src = (RenderedImage) args.getRenderedSource(0);
056:
057: // Get the parameters.
058: float originX = args.getFloatParameter(0);
059: float originY = args.getFloatParameter(1);
060: float width = args.getFloatParameter(2);
061: float height = args.getFloatParameter(3);
062:
063: // Return the OpImage.
064: return new CropOpImage(src, originX, originY, width, height);
065: }
066:
067: /**
068: * Creates a <Code>RenderedImage</Code> from the renderable layer.
069: *
070: * @param renderContext The rendering information associated with
071: * this rendering.
072: * @param paramBlock The parameters used to create the image.
073: * @return A <code>RenderedImage</code>.
074: */
075: public RenderedImage create(RenderContext renderContext,
076: ParameterBlock paramBlock) {
077: // Get the destination bounds in rendering-independent coordinates.
078: Rectangle2D dstRect2D = getBounds2D(paramBlock);
079:
080: // Map the destination bounds to rendered coordinates. This method
081: // will cause extra data to be present if there is any rotation or
082: // shear.
083: AffineTransform tf = renderContext.getTransform();
084: Rectangle2D rect = tf.createTransformedShape(dstRect2D)
085: .getBounds2D();
086:
087: // Make sure that the rendered rectangle is non-empty.
088: if (rect.getWidth() < 1.0 || rect.getHeight() < 1.0) {
089: double w = Math.max(rect.getWidth(), 1.0);
090: double h = Math.max(rect.getHeight(), 1.0);
091: rect.setRect(rect.getMinX(), rect.getMinY(), w, h);
092: }
093:
094: // Initialize the rendered ParameterBlock.
095: ParameterBlock pb = new ParameterBlock();
096: pb.addSource(paramBlock.getRenderedSource(0));
097: pb.set((float) rect.getMinX(), 0);
098: pb.set((float) rect.getMinY(), 1);
099: pb.set((float) rect.getWidth(), 2);
100: pb.set((float) rect.getHeight(), 3);
101:
102: // Crop the rendered source.
103: return JAI
104: .create("crop", pb, renderContext.getRenderingHints());
105: }
106:
107: /**
108: * Returns the bounding box for the output of the operation.
109: *
110: * @param paramBlock A <code>ParameterBlock</code> containing the
111: * renderable sources and parameters of the operation.
112: * @return A <code>Rectangle2D</code> specifying the bounding box.
113: */
114: public Rectangle2D getBounds2D(ParameterBlock paramBlock) {
115: // Return a rectangle representing the desired bounds.
116: return new Rectangle2D.Float(paramBlock.getFloatParameter(0),
117: paramBlock.getFloatParameter(1), paramBlock
118: .getFloatParameter(2), paramBlock
119: .getFloatParameter(3));
120: }
121: }
|