KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > BufferedImageCachableRed


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.rendered;
19
20
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.awt.image.Raster JavaDoc;
24 import java.awt.image.WritableRaster JavaDoc;
25
26 import org.apache.batik.ext.awt.image.GraphicsUtil;
27 /**
28  * This implements CachableRed based on a BufferedImage.
29  * You can use this to wrap a BufferedImage that you want to
30  * appear as a CachableRed.
31  * It essentially ignores the dependency and dirty region methods.
32  *
33  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
34  * @version $Id: BufferedImageCachableRed.java,v 1.5 2004/08/18 07:14:07 vhardy Exp $ */

35 public class BufferedImageCachableRed extends AbstractRed {
36     // The bufferedImage that we wrap...
37
BufferedImage JavaDoc bi;
38
39     /**
40      * Construct an instance of CachableRed around a BufferedImage.
41      */

42     public BufferedImageCachableRed(BufferedImage JavaDoc bi) {
43         super((CachableRed)null,
44               new Rectangle JavaDoc(bi.getMinX(), bi.getMinY(),
45                             bi.getWidth(), bi.getHeight()),
46               bi.getColorModel(), bi.getSampleModel(),
47               bi.getMinX(), bi.getMinY(), null);
48
49         this.bi = bi;
50     }
51
52     public BufferedImageCachableRed(BufferedImage JavaDoc bi,
53                                             int xloc, int yloc) {
54         super((CachableRed)null, new Rectangle JavaDoc(xloc, yloc,
55                                                bi.getWidth(),
56                                                bi.getHeight()),
57               bi.getColorModel(), bi.getSampleModel(), xloc, yloc, null);
58
59         this.bi = bi;
60     }
61
62     public Rectangle JavaDoc getBounds() {
63         return new Rectangle JavaDoc(getMinX(),
64                              getMinY(),
65                              getWidth(),
66                              getHeight());
67     }
68
69     /**
70      * fetch the bufferedImage from this node.
71      */

72     public BufferedImage JavaDoc getBufferedImage() {
73         return bi;
74     }
75
76     public Object JavaDoc getProperty(String JavaDoc name) {
77         return bi.getProperty(name);
78     }
79
80     public String JavaDoc [] getPropertyNames() {
81         return bi.getPropertyNames();
82     }
83
84     public Raster JavaDoc getTile(int tileX, int tileY) {
85         return bi.getTile(tileX,tileY);
86     }
87
88     public Raster JavaDoc getData() {
89         Raster JavaDoc r = bi.getData();
90         return r.createTranslatedChild(getMinX(), getMinY());
91     }
92
93     public Raster JavaDoc getData(Rectangle JavaDoc rect) {
94         Rectangle JavaDoc r = (Rectangle JavaDoc)rect.clone();
95
96         if (r.intersects(getBounds()) == false)
97             return null;
98         r = r.intersection(getBounds());
99         r.translate(-getMinX(), - getMinY());
100
101         Raster JavaDoc ret = bi.getData(r);
102         return ret.createTranslatedChild(ret.getMinX()+getMinX(),
103                                          ret.getMinY()+getMinY());
104     }
105
106     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
107         WritableRaster JavaDoc wr2 = wr.createWritableTranslatedChild
108             (wr.getMinX()-getMinX(),
109              wr.getMinY()-getMinY());
110
111         GraphicsUtil.copyData(bi.getRaster(), wr2);
112
113         /* This was the original code. This is _bad_ since it causes a
114          * multiply and divide of the alpha channel to do the draw
115          * operation. I believe that at some point I switched to
116          * drawImage in order to avoid some issues with
117          * BufferedImage's copyData implementation but I can't
118          * reproduce them now. Anyway I'm now using GraphicsUtil which
119          * should generally be as fast if not faster...
120          */

121         /*
122           BufferedImage dest;
123          dest = new BufferedImage(bi.getColorModel(),
124                                   wr.createWritableTranslatedChild(0,0),
125                                   bi.getColorModel().isAlphaPremultiplied(),
126                                   null);
127          java.awt.Graphics2D g2d = dest.createGraphics();
128          g2d.drawImage(bi, null, getMinX()-wr.getMinX(),
129                        getMinY()-wr.getMinY());
130          g2d.dispose();
131          */

132         return wr;
133     }
134 }
135
Popular Tags