1 /*
2  * $RCSfile: SubsampleAverageCRIF.java,v $
3  *
4  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5  *
6  * Use is subject to license terms.
7  *
8  * $Revision: 1.1 $
9  * $Date: 2005/02/11 04:56:43 $
10  * $State: Exp $
11  */package com.lightcrafts.media.jai.opimage;
12 
13 import java.awt.RenderingHints;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
16 import java.awt.image.RenderedImage;
17 import java.awt.image.renderable.RenderContext;
18 import java.awt.image.renderable.ParameterBlock;
19 import java.awt.image.renderable.RenderableImage;
20 import com.lightcrafts.mediax.jai.ImageLayout;
21 import com.lightcrafts.mediax.jai.CRIFImpl;
22 
23 /**
24  * @see SubsampleAverageOpImage
25  */
26 public class SubsampleAverageCRIF extends CRIFImpl {
27 
28     /** Constructor. */
SubsampleAverageCRIF()29     public SubsampleAverageCRIF() {
30         super("SubsampleAverage");
31     }
32 
33     /**
34      * Creates a new instance of SubsampleAverageOpImage in the rendered layer.
35      * This method satisfies the implementation of RIF.
36      *
37      * @param paramBlock  The source image, the X and Y scale factor,
38      *                    and the interpolation method for resampling.
39      */
create(ParameterBlock paramBlock, RenderingHints renderHints)40     public RenderedImage create(ParameterBlock paramBlock,
41                                 RenderingHints renderHints) {
42 
43         // Get ImageLayout from renderHints if any.
44         ImageLayout layout = RIFUtil.getImageLayoutHint(renderHints);
45 
46         RenderedImage source = paramBlock.getRenderedSource(0);
47         double scaleX = paramBlock.getDoubleParameter(0);
48         double scaleY = paramBlock.getDoubleParameter(1);
49 
50 	// Check and see if we are scaling by 1.0 in both x and y and no
51         // translations. If so return the source directly.
52 	if (scaleX == 1.0 && scaleY == 1.0) {
53 	    return source;
54 	}
55 
56         return new SubsampleAverageOpImage(source, layout, renderHints,
57                                            scaleX, scaleY);
58     }
59 
60     /**
61      * Creates a new instance of <code>SubsampleAverageOpImage</code>
62      * in the renderable layer. This method satisfies the
63      * implementation of CRIF.
64      */
create(RenderContext renderContext, ParameterBlock paramBlock)65     public RenderedImage create(RenderContext renderContext,
66                                 ParameterBlock paramBlock) {
67         return paramBlock.getRenderedSource(0);
68     }
69 
70     /**
71      * Maps the output RenderContext into the RenderContext for the ith
72      * source.
73      * This method satisfies the implementation of CRIF.
74      *
75      * @param i               The index of the source image.
76      * @param renderContext   The renderContext being applied to the operation.
77      * @param paramBlock      The ParameterBlock containing the sources
78      *                        and the translation factors.
79      * @param image           The RenderableImageOp from which this method
80      *                        was called.
81      */
mapRenderContext(int i, RenderContext renderContext, ParameterBlock paramBlock, RenderableImage image)82     public RenderContext mapRenderContext(int i,
83                                           RenderContext renderContext,
84 					  ParameterBlock paramBlock,
85 					  RenderableImage image) {
86 
87         double scaleX = paramBlock.getDoubleParameter(0);
88         double scaleY = paramBlock.getDoubleParameter(1);
89 
90         AffineTransform scale =
91             new AffineTransform(scaleX, 0.0, 0.0, scaleY, 0.0, 0.0);
92 
93         RenderContext RC = (RenderContext)renderContext.clone();
94         AffineTransform usr2dev = RC.getTransform();
95         usr2dev.concatenate(scale);
96 	RC.setTransform(usr2dev);
97 	return RC;
98     }
99 
100     /**
101      * Gets the bounding box for the output of <code>ScaleOpImage</code>.
102      * This method satisfies the implementation of CRIF.
103      */
getBounds2D(ParameterBlock paramBlock)104     public Rectangle2D getBounds2D(ParameterBlock paramBlock) {
105 
106         RenderableImage source = paramBlock.getRenderableSource(0);
107 
108         double scaleX = paramBlock.getDoubleParameter(0);
109         double scaleY = paramBlock.getDoubleParameter(1);
110 
111 	// Get the source dimensions
112 	float x0 = (float)source.getMinX();
113 	float y0 = (float)source.getMinY() ;
114 	float w = (float)source.getWidth();
115 	float h = (float)source.getHeight();
116 
117 	// Forward map the source using x0, y0, w and h
118 	float d_x0 = (float)(x0 * scaleX);
119 	float d_y0 = (float)(y0 * scaleY);
120 	float d_w = (float)(w * scaleX);
121 	float d_h = (float)(h * scaleY);
122 
123 	return new Rectangle2D.Float(d_x0, d_y0, d_w, d_h);
124     }
125 
126 }
127