001: /*
002: * $RCSfile: ConstantCRIF.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:19 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.RenderingHints;
015: import java.awt.geom.AffineTransform;
016: import java.awt.geom.Rectangle2D;
017: import java.awt.image.DataBuffer;
018: import java.awt.image.RenderedImage;
019: import java.awt.image.renderable.ParameterBlock;
020: import java.awt.image.renderable.RenderContext;
021: import java.awt.image.renderable.RenderableImage;
022: import javax.media.jai.CRIFImpl;
023: import javax.media.jai.ImageLayout;
024: import java.util.Map;
025:
026: /**
027: * This image factory supports image operator <code>ConstantOpImage</code>
028: * in the rendered and renderable image layers.
029: *
030: * @see ConstantOpImage
031: */
032: public class ConstantCRIF extends CRIFImpl {
033:
034: private static final int DEFAULT_TILE_SIZE = 128;
035:
036: /** Constructor. */
037: public ConstantCRIF() {
038: super ("constant");
039: }
040:
041: /**
042: * Creates a new instance of <code>ConstantOpImage</code>
043: * in the rendered layer. This method satisfies the
044: * implementation of RIF.
045: */
046: public RenderedImage create(ParameterBlock paramBlock,
047: RenderingHints renderHints) {
048: // Get ImageLayout from renderHints if any.
049: ImageLayout layout = RIFUtil.getImageLayoutHint(renderHints);
050:
051: // Get width, height, bandValues from the parameter block
052: int width = Math.round(paramBlock.getFloatParameter(0));
053: int height = Math.round(paramBlock.getFloatParameter(1));
054: Number[] bandValues = (Number[]) paramBlock
055: .getObjectParameter(2);
056:
057: int minX = 0;
058: int minY = 0;
059: int tileWidth = Math.min(width, DEFAULT_TILE_SIZE);
060: int tileHeight = Math.min(height, DEFAULT_TILE_SIZE);
061:
062: // Attempt to get minX, minY, tileWidth, tileHeight
063: // from the ImageLayout hint
064: if (layout != null) {
065: if (layout.isValid(ImageLayout.MIN_X_MASK)) {
066: minX = layout.getMinX(null);
067: }
068: if (layout.isValid(ImageLayout.MIN_Y_MASK)) {
069: minY = layout.getMinY(null);
070: }
071: if (layout.isValid(ImageLayout.TILE_WIDTH_MASK)) {
072: tileWidth = layout.getTileWidth(null);
073: }
074: if (layout.isValid(ImageLayout.TILE_HEIGHT_MASK)) {
075: tileHeight = layout.getTileHeight(null);
076: }
077: }
078:
079: return new ConstantOpImage(minX, minY, width, height,
080: tileWidth, tileHeight, bandValues);
081: }
082:
083: /**
084: * Creates a new instance of <code>ConstantOpImage</code>
085: * in the renderable layer. This method satisfies the
086: * implementation of CRIF.
087: *
088: * @pram renderContext Rendering information.
089: * @param paramBlock The image layout for the output of
090: * <code>ConstantOpImage</code>
091: * and the constant pixel value.
092: */
093: public RenderedImage create(RenderContext renderContext,
094: ParameterBlock paramBlock) {
095: float minX = 0;
096: float minY = 0;
097: float width = paramBlock.getFloatParameter(0);
098: float height = paramBlock.getFloatParameter(1);
099: Number[] bandValues = (Number[]) paramBlock
100: .getObjectParameter(2);
101:
102: AffineTransform trans = renderContext.getTransform();
103: float maxX, maxY;
104: float[] ptSrc = new float[8];
105: float[] ptDst = new float[8];
106:
107: ptSrc[0] = minX;
108: ptSrc[1] = minY;
109: ptSrc[2] = minX + width;
110: ptSrc[3] = minY;
111: ptSrc[4] = minX + width;
112: ptSrc[5] = minY + height;
113: ptSrc[6] = minX;
114: ptSrc[7] = minY + height;
115: trans.transform(ptSrc, 0, ptDst, 0, 4);
116:
117: minX = Math.min(ptDst[0], ptDst[2]);
118: minX = Math.min(minX, ptDst[4]);
119: minX = Math.min(minX, ptDst[6]);
120:
121: maxX = Math.max(ptDst[0], ptDst[2]);
122: maxX = Math.max(maxX, ptDst[4]);
123: maxX = Math.max(maxX, ptDst[6]);
124:
125: minY = Math.min(ptDst[1], ptDst[3]);
126: minY = Math.min(minY, ptDst[5]);
127: minY = Math.min(minY, ptDst[7]);
128:
129: maxY = Math.max(ptDst[1], ptDst[3]);
130: maxY = Math.max(maxY, ptDst[5]);
131: maxY = Math.max(maxY, ptDst[7]);
132:
133: int iMinX = (int) minX;
134: int iMinY = (int) minY;
135: int iWidth = (int) maxX - iMinX;
136: int iHeight = (int) maxY - iMinY;
137:
138: return new ConstantOpImage(iMinX, iMinY, iWidth, iHeight, Math
139: .min(iWidth, DEFAULT_TILE_SIZE), Math.min(iHeight,
140: DEFAULT_TILE_SIZE), bandValues);
141: }
142:
143: /**
144: * Gets the bounding box for the output of <code>ConstantOpImage</code>.
145: * This method satisfies the implementation of CRIF.
146: *
147: * @param paramBlock Image's width, height, and constant pixel values.
148: */
149: public Rectangle2D getBounds2D(ParameterBlock paramBlock) {
150: return new Rectangle2D.Float(0, 0, paramBlock
151: .getFloatParameter(0), paramBlock.getFloatParameter(1));
152: }
153: }
|