KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > filter > GraphicsNodeRed8Bit


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.gvt.filter;
19
20 import java.awt.AlphaComposite JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.RenderingHints 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.ColorModel JavaDoc;
28 import java.awt.image.SampleModel JavaDoc;
29 import java.awt.image.WritableRaster JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32 import org.apache.batik.ext.awt.image.rendered.AbstractRed;
33 import org.apache.batik.ext.awt.image.rendered.AbstractTiledRed;
34 import org.apache.batik.ext.awt.image.rendered.CachableRed;
35 import org.apache.batik.gvt.GraphicsNode;
36
37 /**
38  * This implementation of RenderableImage will render its input
39  * GraphicsNode on demand for tiles.
40  *
41  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
42  * @version $Id: GraphicsNodeRed8Bit.java,v 1.16 2005/02/12 01:48:24 deweese Exp $
43  */

44 public class GraphicsNodeRed8Bit extends AbstractRed {
45
46     /**
47      * GraphicsNode this image can render
48      */

49     private GraphicsNode node;
50
51     private AffineTransform JavaDoc node2dev;
52
53     private RenderingHints JavaDoc hints;
54
55     private boolean usePrimitivePaint;
56
57     public GraphicsNodeRed8Bit(GraphicsNode node,
58                                AffineTransform JavaDoc node2dev,
59                                boolean usePrimitivePaint,
60                                RenderingHints JavaDoc hints) {
61         super(); // We _must_ call init...
62

63         this.node = node;
64         this.node2dev = node2dev;
65         this.hints = hints;
66         this.usePrimitivePaint = usePrimitivePaint;
67
68         // Calculate my bounds by applying the affine transform to
69
// my input data..
70

71         AffineTransform JavaDoc at = node2dev;
72         Rectangle2D JavaDoc bounds2D = node.getPrimitiveBounds();
73         if (bounds2D == null) bounds2D = new Rectangle2D.Float JavaDoc(0,0,1,1);
74         if (!usePrimitivePaint) {
75             // When not using Primitive paint we return our bounds in
76
// the nodes parent's user space. This makes sense since
77
// this is the space that we will draw our selves into
78
// (since paint unlike primitivePaint incorporates the
79
// transform from our user space to our parents user
80
// space).
81
AffineTransform JavaDoc nodeAt = node.getTransform();
82             if (nodeAt != null) {
83                 at = (AffineTransform JavaDoc)at.clone();
84                 at.concatenate(nodeAt);
85             }
86         }
87         Rectangle JavaDoc bounds = at.createTransformedShape(bounds2D).getBounds();
88         // System.out.println("Bounds: " + bounds);
89

90         ColorModel JavaDoc cm = createColorModel();
91
92         int defSz = AbstractTiledRed.getDefaultTileSize();
93
94         // Make tile(0,0) fall on the closest intersection of defaultSz.
95
int tgX = defSz*(int)Math.floor(bounds.x/defSz);
96         int tgY = defSz*(int)Math.floor(bounds.y/defSz);
97
98         int tw = (bounds.x+bounds.width)-tgX;
99         if (tw > defSz) tw = defSz;
100         int th = (bounds.y+bounds.height)-tgY;
101         if (th > defSz) th = defSz;
102         if ((tw <= 0) || (th <= 0)) {
103             tw = 1;
104             th = 1;
105         }
106
107         // fix my sample model so it makes sense given my size.
108
SampleModel JavaDoc sm = cm.createCompatibleSampleModel(tw, th);
109
110         // Finish initializing our base class...
111
init((CachableRed)null, bounds, cm, sm, tgX, tgY, null);
112     }
113
114     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
115         genRect(wr);
116         return wr;
117     }
118
119     public void genRect(WritableRaster JavaDoc wr) {
120         // System.out.println(" Rect: " + wr.getBounds());
121
BufferedImage JavaDoc offScreen
122             = new BufferedImage JavaDoc(cm,
123                                 wr.createWritableTranslatedChild(0,0),
124                                 cm.isAlphaPremultiplied(),
125                                 null);
126
127         Graphics2D JavaDoc g = GraphicsUtil.createGraphics(offScreen, hints);
128         g.setComposite(AlphaComposite.Clear);
129         g.fillRect(0, 0, wr.getWidth(), wr.getHeight());
130         g.setComposite(AlphaComposite.SrcOver);
131         g.translate(-wr.getMinX(), -wr.getMinY());
132
133         // Set transform
134
g.transform(node2dev);
135
136
137         // Invoke primitive paint.
138
if (usePrimitivePaint) {
139             node.primitivePaint(g);
140         }
141         else {
142             node.paint (g);
143         }
144
145         g.dispose();
146     }
147
148     static final boolean onMacOSX;
149     static {
150         // This should be OK for applets.
151
onMacOSX = ("Mac OS X".equals(System.getProperty("os.name")));
152     }
153
154     public ColorModel JavaDoc createColorModel() {
155         if (onMacOSX)
156             return GraphicsUtil.sRGB_Pre;
157         return GraphicsUtil.sRGB_Unpre;
158     }
159 }
160
161
162
163
164
Popular Tags