KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.Shape JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26 import java.awt.geom.Rectangle2D JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.awt.image.RenderedImage JavaDoc;
29 import java.awt.image.renderable.RenderContext JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32 import org.apache.batik.ext.awt.image.PadMode;
33 import org.apache.batik.ext.awt.image.rendered.BufferedImageCachableRed;
34 import org.apache.batik.ext.awt.image.rendered.CachableRed;
35 import org.apache.batik.ext.awt.image.rendered.MultiplyAlphaRed;
36 import org.apache.batik.ext.awt.image.rendered.PadRed;
37 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed;
38
39 /**
40  * ClipRable implementation
41  *
42  * @author <a HREF="mailto:Thomas.DeWeese@Kodak.com">Thomas DeWeese</a>
43  * @version $Id: ClipRable8Bit.java,v 1.8 2005/03/27 08:58:33 cam Exp $
44  */

45 public class ClipRable8Bit
46     extends AbstractRable
47     implements ClipRable {
48
49     protected boolean useAA;
50
51     /**
52      * The node who's outline specifies our mask.
53      */

54     protected Shape JavaDoc clipPath;
55
56     public ClipRable8Bit(Filter src, Shape JavaDoc clipPath) {
57         super(src, null);
58         setClipPath(clipPath);
59         setUseAntialiasedClip(false);
60     }
61
62     public ClipRable8Bit(Filter src, Shape JavaDoc clipPath, boolean useAA) {
63         super(src, null);
64         setClipPath(clipPath);
65         setUseAntialiasedClip(useAA);
66     }
67
68     /**
69      * The source to be masked by the mask node.
70      * @param src The Image to be masked.
71      */

72     public void setSource(Filter src) {
73         init(src, null);
74     }
75
76     /**
77      * This returns the current image being masked by the mask node.
78      * @return The image to mask
79      */

80     public Filter getSource() {
81         return (Filter)getSources().get(0);
82     }
83
84     /**
85      * Set the default behaviour of anti-aliased clipping.
86      * for this clip object.
87      */

88     public void setUseAntialiasedClip(boolean useAA) {
89         touch();
90         this.useAA = useAA;
91     }
92
93     /**
94      * Resturns true if the default behaviour should be to use
95      * anti-aliased clipping.
96      */

97     public boolean getUseAntialiasedClip() {
98         return useAA;
99     }
100
101
102     /**
103      * Set the clip path to use.
104      * The path will be filled with opaque white.
105      * @param clipPath The clip path to use
106      */

107     public void setClipPath(Shape JavaDoc clipPath) {
108         touch();
109         this.clipPath = clipPath;
110     }
111
112       /**
113        * Returns the Shape that the cliprable will use to
114        * define the clip path.
115        * @return The shape that defines the clip path.
116        */

117     public Shape JavaDoc getClipPath() {
118         return clipPath;
119     }
120
121     /**
122      * Pass-through: returns the source's bounds
123      */

124     public Rectangle2D JavaDoc getBounds2D(){
125         return getSource().getBounds2D();
126     }
127
128     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
129
130         AffineTransform JavaDoc usr2dev = rc.getTransform();
131
132         // Just copy over the rendering hints.
133
RenderingHints JavaDoc rh = rc.getRenderingHints();
134         if (rh == null) rh = new RenderingHints JavaDoc(null);
135
136         Shape JavaDoc aoi = rc.getAreaOfInterest();
137         if (aoi == null) aoi = getBounds2D();
138
139         Rectangle2D JavaDoc rect = getBounds2D();
140         Rectangle2D JavaDoc clipRect = clipPath.getBounds2D();
141         Rectangle2D JavaDoc aoiRect = aoi.getBounds2D();
142         
143         if (rect.intersects(clipRect) == false)
144             return null;
145         Rectangle2D.intersect(rect, clipRect, rect);
146
147         
148         if (rect.intersects(aoiRect) == false)
149             return null;
150         Rectangle2D.intersect(rect, aoi.getBounds2D(), rect);
151
152         Rectangle JavaDoc devR = usr2dev.createTransformedShape(rect).getBounds();
153
154         if ((devR.width == 0) || (devR.height == 0))
155             return null;
156         
157         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(devR.width, devR.height,
158                                              BufferedImage.TYPE_BYTE_GRAY);
159
160         Shape JavaDoc devShape = usr2dev.createTransformedShape(getClipPath());
161         Rectangle JavaDoc devAOIR;
162         devAOIR = usr2dev.createTransformedShape(aoi).getBounds();
163
164         Graphics2D JavaDoc g2d = GraphicsUtil.createGraphics(bi, rh);
165
166         if (false) {
167             java.util.Set JavaDoc s = rh.keySet();
168             java.util.Iterator JavaDoc i = s.iterator();
169             while (i.hasNext()) {
170                 Object JavaDoc o = i.next();
171                 System.out.println("XXX: " + o + " -> " + rh.get(o));
172             }
173         }
174         g2d.translate(-devR.x, -devR.y);
175         g2d.setPaint(Color.white);
176         g2d.fill(devShape);
177         g2d.dispose();
178
179         RenderedImage JavaDoc ri;
180         ri = getSource().createRendering(new RenderContext JavaDoc(usr2dev, rect, rh));
181
182         CachableRed cr, clipCr;
183         cr = RenderedImageCachableRed.wrap(ri);
184         clipCr = new BufferedImageCachableRed(bi, devR.x, devR.y);
185         CachableRed ret = new MultiplyAlphaRed(cr, clipCr);
186
187           // Pad back out to the proper size...
188
ret = new PadRed(ret, devAOIR, PadMode.ZERO_PAD, rh);
189
190         return ret;
191     }
192 }
193
Popular Tags