KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > renderable > FilterChainRable8Bit


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.renderable;
19
20 import java.awt.Composite JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.awt.image.RenderedImage JavaDoc;
24 import java.awt.image.renderable.RenderContext JavaDoc;
25
26 import org.apache.batik.ext.awt.image.GraphicsUtil;
27 import org.apache.batik.ext.awt.image.PadMode;
28 import org.apache.batik.ext.awt.image.SVGComposite;
29
30 /**
31  * Implements a filter chain. A filter chain is defined by its
32  * filter region (i.e., the bounding box of its input/output), its
33  * filter resolution and its source. Its source cannot be null,
34  * but its resolution can. <br />
35  * The filter chain decomposes as follows:
36  * <ul>
37  * <li>A pad operation that makes the input image a big as the
38  * filter region.</li>
39  * <li>If there is a filterResolution specified along at least
40  * one of the axis, a <tt>AffineRable</tt>
41  * </ul>
42  *
43  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
44  * @version $Id: FilterChainRable8Bit.java,v 1.9 2005/03/27 08:58:33 cam Exp $
45  */

46 public class FilterChainRable8Bit extends AbstractRable
47     implements FilterChainRable, PaintRable {
48     /**
49      * Resolution along the X axis
50      */

51     private int filterResolutionX;
52
53     /**
54      * Resolution along the Y axis
55      */

56     private int filterResolutionY;
57
58     /**
59      * The chain's source
60      */

61     private Filter chainSource;
62
63     /**
64      * Scale operation. May be null
65      */

66     private FilterResRable filterRes;
67
68     /**
69      * Crop operation.
70      */

71     private PadRable crop;
72
73     /**
74      * Filter region
75      */

76     private Rectangle2D JavaDoc filterRegion;
77
78     /**
79      * Default constructor.
80      */

81     public FilterChainRable8Bit(Filter source, Rectangle2D JavaDoc filterRegion){
82         if(source == null){
83             throw new IllegalArgumentException JavaDoc();
84         }
85         if(filterRegion == null){
86             throw new IllegalArgumentException JavaDoc();
87         }
88
89         // Build crop with chain source and dummy region (will be lazily evaluated
90
// later on).
91
Rectangle2D JavaDoc padRect = (Rectangle2D JavaDoc)filterRegion.clone();
92         crop = new PadRable8Bit(source, padRect,
93                                     PadMode.ZERO_PAD);
94
95         // Keep a reference to the chain source and filter
96
// regions.
97
this.chainSource = source;
98         this.filterRegion = filterRegion;
99
100         // crop is the real shource for this filter
101
// The filter chain is a simple passthrough to its
102
// crop node.
103
init(crop);
104   
105     }
106
107     /**
108      * Returns the resolution along the X axis.
109      */

110     public int getFilterResolutionX(){
111         return filterResolutionX;
112     }
113
114     /**
115      * Sets the resolution along the X axis, i.e., the maximum
116      * size for intermediate images along that axis.
117      * If filterResolutionX is less than zero, no filter resolution
118      * is forced on the filter chain. If filterResolutionX is zero,
119      * then the filter returns null. If filterResolutionX is positive,
120      * then the filter resolution is applied.
121      */

122     public void setFilterResolutionX(int filterResolutionX){
123         touch();
124         this.filterResolutionX = filterResolutionX;
125
126         setupFilterRes();
127     }
128
129     /**
130      * Returns the resolution along the Y axis.
131      */

132     public int getFilterResolutionY(){
133         return filterResolutionY;
134     }
135
136     /**
137      * Sets the resolution along the Y axis, i.e., the maximum
138      * size for intermediate images along that axis.
139      * If filterResolutionY is zero or less, the value of
140      * filterResolutionX is used.
141      */

142     public void setFilterResolutionY(int filterResolutionY){
143         touch();
144         this.filterResolutionY = filterResolutionY;
145         setupFilterRes();
146     }
147     
148     /**
149      * Implementation. Checks the current value of the
150      * filterResolutionX and filterResolutionY attribute and
151      * setup the filterRes operation accordingly.
152      */

153     private void setupFilterRes(){
154         if(filterResolutionX >=0){
155             if(filterRes == null){
156                 filterRes = new FilterResRable8Bit();
157                 filterRes.setSource(chainSource);
158             }
159             
160             filterRes.setFilterResolutionX(filterResolutionX);
161             filterRes.setFilterResolutionY(filterResolutionY);
162         }
163         else{
164             // X is negative, this disables the resolution filter.
165
filterRes = null;
166         }
167
168         // Now, update the crop source to reflect the filterRes
169
// settings.
170
if(filterRes != null){
171             crop.setSource(filterRes);
172         }
173         else{
174             crop.setSource(chainSource);
175         }
176     }
177     
178     /**
179      * Sets the filter output area, in user space.
180      * A null value is illegal.
181      */

182     public void setFilterRegion(Rectangle2D JavaDoc filterRegion){
183         if(filterRegion == null){
184             throw new IllegalArgumentException JavaDoc();
185         }
186         touch();
187         this.filterRegion = filterRegion;
188      }
189
190     /**
191      * Returns the filter output area, in user space
192      */

193     public Rectangle2D JavaDoc getFilterRegion(){
194         return filterRegion;
195     }
196
197     /**
198      * Returns the source of the chain. Note that a crop and
199      * affine operation may be inserted before the source,
200      * depending on the filterRegion and filterResolution
201      * parameters.
202      */

203     public Filter getSource() {
204         return (Filter)crop;
205     }
206     
207     /**
208      * Sets the source to be src.
209      * @param chainSource image to the chain.
210      */

211     public void setSource(Filter chainSource) {
212         if(chainSource == null){
213             throw new IllegalArgumentException JavaDoc("Null Source for Filter Chain");
214         }
215         touch();
216         this.chainSource = chainSource;
217         
218         if(filterRes == null){
219             crop.setSource(chainSource);
220         }
221         else{
222             filterRes.setSource(chainSource);
223         }
224     }
225
226     /**
227      * Returns this filter's bounds
228      */

229     public Rectangle2D JavaDoc getBounds2D(){
230         return (Rectangle2D JavaDoc)filterRegion.clone();
231     }
232
233     /**
234      * Should perform the equivilent action as
235      * createRendering followed by drawing the RenderedImage to
236      * Graphics2D, or return false.
237      *
238      * @param g2d The Graphics2D to draw to.
239      * @return true if the paint call succeeded, false if
240      * for some reason the paint failed (in which
241      * case a createRendering should be used).
242      */

243     public boolean paintRable(Graphics2D JavaDoc g2d) {
244         // This optimization only apply if we are using
245
// SrcOver. Otherwise things break...
246
Composite JavaDoc c = g2d.getComposite();
247         if (!SVGComposite.OVER.equals(c))
248             return false;
249         
250         GraphicsUtil.drawImage(g2d, getSource());
251
252         return true;
253     }
254
255     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc context){
256         return crop.createRendering(context);
257     }
258 }
259
Popular Tags