KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > PatternPaintContext


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;
19
20 import java.awt.PaintContext JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.RenderingHints JavaDoc;
23 import java.awt.color.ColorSpace 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.Raster JavaDoc;
29 import java.awt.image.RenderedImage JavaDoc;
30 import java.awt.image.WritableRaster JavaDoc;
31 import java.awt.image.renderable.RenderContext JavaDoc;
32
33 import org.apache.batik.ext.awt.image.GraphicsUtil;
34 import org.apache.batik.ext.awt.image.renderable.Filter;
35 import org.apache.batik.ext.awt.image.renderable.TileRable;
36 import org.apache.batik.ext.awt.image.renderable.TileRable8Bit;
37 import org.apache.batik.ext.awt.image.rendered.TileCacheRed;
38
39 /**
40  * <tt>PaintContext</tt> for the <tt>ConcretePatterPaint</tt>
41  * paint implementation.
42  *
43  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
44  * @version $Id: PatternPaintContext.java,v 1.14 2005/03/27 08:58:34 cam Exp $
45  */

46 public class PatternPaintContext implements PaintContext JavaDoc {
47
48     /**
49      * ColorModel for the Rasters created by this Paint
50      */

51     private ColorModel JavaDoc rasterCM;
52
53     /**
54      * Working Raster
55      */

56     private WritableRaster JavaDoc raster;
57
58     /**
59      * Tile
60      */

61     private RenderedImage JavaDoc tiled;
62
63     protected AffineTransform JavaDoc usr2dev;
64
65     public AffineTransform JavaDoc getUsr2Dev() { return usr2dev; }
66
67     private static Rectangle JavaDoc EVERYTHING =
68         new Rectangle JavaDoc(Integer.MIN_VALUE/4, Integer.MIN_VALUE/4,
69                       Integer.MAX_VALUE/2, Integer.MAX_VALUE/2);
70
71     /**
72      * @param destCM ColorModel that receives the paint data
73      * @param usr2dev user space to device space transform
74      * @param hints RenderingHints
75      * @param patternRegion region tiled by this paint. In user space.
76      * @param overflow controls whether the pattern region clips the
77      * pattern tile
78      */

79     public PatternPaintContext(ColorModel JavaDoc destCM,
80                                AffineTransform JavaDoc usr2dev,
81                                RenderingHints JavaDoc hints,
82                                Filter tile,
83                                Rectangle2D JavaDoc patternRegion,
84                                boolean overflow) {
85
86         if(usr2dev == null){
87             throw new IllegalArgumentException JavaDoc();
88         }
89
90         if(hints == null){
91             hints = new RenderingHints JavaDoc(null);
92         }
93
94         if(tile == null){
95             throw new IllegalArgumentException JavaDoc();
96         }
97
98         this.usr2dev = usr2dev;
99
100         // System.out.println("PatB: " + patternRegion);
101
// System.out.println("Tile: " + tile);
102

103         TileRable tileRable = new TileRable8Bit(tile,
104                                                 EVERYTHING,
105                                                 patternRegion,
106                                                 overflow);
107         ColorSpace JavaDoc destCS = destCM.getColorSpace();
108         if (destCS == ColorSpace.getInstance(ColorSpace.CS_sRGB))
109             tileRable.setColorSpaceLinear(false);
110         else if (destCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB))
111             tileRable.setColorSpaceLinear(true);
112
113         RenderContext JavaDoc rc = new RenderContext JavaDoc(usr2dev, EVERYTHING, hints);
114         tiled = tileRable.createRendering(rc);
115         // System.out.println("tileRed: " + tiled);
116
// org.apache.batik.test.gvt.ImageDisplay.showImage("Tiled: ", tiled);
117

118         //System.out.println("Created rendering");
119
if(tiled != null) {
120             Rectangle2D JavaDoc devRgn = usr2dev.createTransformedShape
121                 (patternRegion).getBounds();
122             if ((devRgn.getWidth() > 128) ||
123                 (devRgn.getHeight() > 128))
124                 tiled = new TileCacheRed(GraphicsUtil.wrap(tiled), 256, 64);
125         } else {
126             //System.out.println("Tile was null");
127
rasterCM = ColorModel.getRGBdefault();
128             WritableRaster JavaDoc wr;
129             wr = rasterCM.createCompatibleWritableRaster(32, 32);
130             tiled = GraphicsUtil.wrap
131                 (new BufferedImage JavaDoc(rasterCM, wr, false, null));
132             return;
133         }
134
135         rasterCM = tiled.getColorModel();
136         if (rasterCM.hasAlpha()) {
137             if (destCM.hasAlpha())
138                 rasterCM = GraphicsUtil.coerceColorModel
139                     (rasterCM, destCM.isAlphaPremultiplied());
140             else
141                 rasterCM = GraphicsUtil.coerceColorModel(rasterCM, false);
142         }
143     }
144
145     public void dispose(){
146         raster = null;
147     }
148
149     public ColorModel JavaDoc getColorModel(){
150         return rasterCM;
151     }
152
153     public Raster JavaDoc getRaster(int x, int y, int width, int height){
154
155         // System.out.println("GetRaster: [" + x + ", " + y + ", "
156
// + width + ", " + height + "]");
157
if ((raster == null) ||
158             (raster.getWidth() < width) ||
159             (raster.getHeight() < height)) {
160             raster = rasterCM.createCompatibleWritableRaster(width, height);
161         }
162
163         WritableRaster JavaDoc wr
164             = raster.createWritableChild(0, 0, width, height, x, y, null);
165
166         tiled.copyData(wr);
167         GraphicsUtil.coerceData(wr, tiled.getColorModel(),
168                                 rasterCM.isAlphaPremultiplied());
169
170         // On Mac OS X it always wants the raster at 0,0 if the
171
// requested width and height matches raster we can just
172
// return it. Otherwise we create a translated child that
173
// lives at 0,0.
174
if ((raster.getWidth() == width) &&
175             (raster.getHeight() == height))
176             return raster;
177
178         return wr.createTranslatedChild(0,0);
179     }
180 }
181
Popular Tags