KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenderingHints JavaDoc;
23 import java.awt.Shape JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.awt.image.RenderedImage JavaDoc;
28 import java.awt.image.renderable.RenderContext JavaDoc;
29
30 import org.apache.batik.ext.awt.image.GraphicsUtil;
31 import org.apache.batik.ext.awt.image.PadMode;
32 import org.apache.batik.ext.awt.image.SVGComposite;
33 import org.apache.batik.ext.awt.image.rendered.CachableRed;
34 import org.apache.batik.ext.awt.image.rendered.PadRed;
35
36 /**
37  * Concrete implementation of the PadRable interface.
38  * This pads the image to a specified rectangle in user coord system.
39  *
40  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
41  * @version $Id: PadRable8Bit.java,v 1.11 2005/03/27 08:58:33 cam Exp $
42  */

43 public class PadRable8Bit extends AbstractRable
44     implements PadRable, PaintRable {
45
46     PadMode padMode;
47     Rectangle2D JavaDoc padRect;
48
49     public PadRable8Bit(Filter src,
50                         Rectangle2D JavaDoc padRect,
51                         PadMode padMode) {
52         super.init(src, null);
53         this.padRect = padRect;
54         this.padMode = padMode;
55     }
56
57     /**
58      * Returns the source to be affine.
59      */

60     public Filter getSource() {
61         return (Filter)srcs.get(0);
62     }
63
64     /**
65      * Sets the source to be affine.
66      * @param src image to affine.
67      */

68     public void setSource(Filter src) {
69         super.init(src, null);
70     }
71
72     public Rectangle2D JavaDoc getBounds2D() {
73         return (Rectangle2D JavaDoc)padRect.clone();
74     }
75
76     /**
77      * Set the current rectangle for padding.
78      * @param rect the new rectangle to use for pad.
79      */

80     public void setPadRect(Rectangle2D JavaDoc rect) {
81         touch();
82         this.padRect = rect;
83     }
84
85     /**
86      * Get the current rectangle for padding
87      * @return Rectangle currently in use for pad.
88      */

89     public Rectangle2D JavaDoc getPadRect() {
90         return (Rectangle2D JavaDoc)padRect.clone();
91     }
92
93     /**
94      * Set the current extension mode for pad
95      * @param padMode the new pad mode
96      */

97     public void setPadMode(PadMode padMode) {
98         touch();
99         this.padMode = padMode;
100     }
101
102     /**
103      * Get the current extension mode for pad
104      * @return Mode currently in use for pad
105      */

106     public PadMode getPadMode() {
107         return padMode;
108     }
109
110     /**
111      * Should perform the equivilent action as
112      * createRendering followed by drawing the RenderedImage to
113      * Graphics2D, or return false.
114      *
115      * @param g2d The Graphics2D to draw to.
116      * @return true if the paint call succeeded, false if
117      * for some reason the paint failed (in which
118      * case a createRendering should be used).
119      */

120     public boolean paintRable(Graphics2D JavaDoc g2d) {
121         // This optimization only apply if we are using
122
// SrcOver. Otherwise things break...
123
Composite JavaDoc c = g2d.getComposite();
124         if (!SVGComposite.OVER.equals(c))
125             return false;
126
127         if (getPadMode() != PadMode.ZERO_PAD)
128             return false;
129
130         Rectangle2D JavaDoc padBounds = getPadRect();
131
132         Shape JavaDoc clip = g2d.getClip();
133         g2d.clip(padBounds);
134         GraphicsUtil.drawImage(g2d, getSource());
135         g2d.setClip(clip);
136         return true;
137     }
138
139     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
140         RenderingHints JavaDoc rh = rc.getRenderingHints();
141         if (rh == null) rh = new RenderingHints JavaDoc(null);
142
143         Filter src = getSource();
144         Shape JavaDoc aoi = rc.getAreaOfInterest();
145
146         if(aoi == null){
147             aoi = getBounds2D();
148         }
149
150         AffineTransform JavaDoc usr2dev = rc.getTransform();
151
152         // We only depend on our source for stuff that is inside
153
// our bounds and his bounds (remember our bounds may be
154
// tighter than his in one or both directions).
155
Rectangle2D JavaDoc srect = src.getBounds2D();
156         Rectangle2D JavaDoc rect = getBounds2D();
157         Rectangle2D JavaDoc arect = aoi.getBounds2D();
158         
159         // System.out.println("Rects Src:" + srect +
160
// "My: " + rect +
161
// "AOI: " + arect);
162
if (arect.intersects(rect) == false)
163             return null;
164         Rectangle2D.intersect(arect, rect, arect);
165
166         RenderedImage JavaDoc ri = null;
167         if (arect.intersects(srect) == true) {
168             srect = (Rectangle2D JavaDoc)srect.clone();
169             Rectangle2D.intersect(srect, arect, srect);
170             
171             RenderContext JavaDoc srcRC = new RenderContext JavaDoc(usr2dev, srect, rh);
172             ri = src.createRendering(srcRC);
173
174             // System.out.println("Pad filt: " + src + " R: " +
175
// src.getBounds2D());
176
}
177
178         // No source image so create a 1,1 transparent one...
179
if (ri == null)
180             ri = new BufferedImage JavaDoc(1, 1, BufferedImage.TYPE_INT_ARGB);
181
182         // org.apache.batik.test.gvt.ImageDisplay.showImage("Paded: ", ri);
183
// System.out.println("RI: " + ri + " R: " + srect);
184

185         CachableRed cr = GraphicsUtil.wrap(ri);
186             
187         arect = usr2dev.createTransformedShape(arect).getBounds2D();
188             
189         // System.out.println("Pad rect : " + arect);
190
// Use arect (my bounds intersect area of interest)
191
cr = new PadRed(cr, arect.getBounds(), padMode, rh);
192         return cr;
193     }
194
195     public Shape JavaDoc getDependencyRegion(int srcIndex, Rectangle2D JavaDoc outputRgn) {
196         if (srcIndex != 0)
197             throw new IndexOutOfBoundsException JavaDoc("Affine only has one input");
198
199         // We only depend on our source for stuff that is inside
200
// our bounds and his bounds (remember our bounds may be
201
// tighter than his in one or both directions).
202
Rectangle2D JavaDoc srect = getSource().getBounds2D();
203         if (srect.intersects(outputRgn) == false)
204             return new Rectangle2D.Float JavaDoc();
205         Rectangle2D.intersect(srect, outputRgn, srect);
206
207         Rectangle2D JavaDoc bounds = getBounds2D();
208         if (srect.intersects(bounds) == false)
209             return new Rectangle2D.Float JavaDoc();
210         Rectangle2D.intersect(srect, bounds, srect);
211         return srect;
212     }
213
214     public Shape JavaDoc getDirtyRegion(int srcIndex, Rectangle2D JavaDoc inputRgn) {
215         if (srcIndex != 0)
216             throw new IndexOutOfBoundsException JavaDoc("Affine only has one input");
217
218         inputRgn = (Rectangle2D JavaDoc)inputRgn.clone();
219         Rectangle2D JavaDoc bounds = getBounds2D();
220         // Changes in the input region don't propogate outside our
221
// bounds.
222
if (inputRgn.intersects(bounds) == false)
223             return new Rectangle2D.Float JavaDoc();
224         Rectangle2D.intersect(inputRgn, bounds, inputRgn);
225         return inputRgn;
226     }
227
228 }
229
Popular Tags