KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > spi > JDKRegistryEntry


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.spi;
19
20 import java.awt.Graphics2D JavaDoc;
21 import java.awt.Image JavaDoc;
22 import java.awt.Toolkit JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.image.ImageObserver JavaDoc;
26 import java.awt.image.RenderedImage JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32 import org.apache.batik.ext.awt.image.renderable.DeferRable;
33 import org.apache.batik.ext.awt.image.renderable.Filter;
34 import org.apache.batik.ext.awt.image.renderable.RedRable;
35 import org.apache.batik.util.ParsedURL;
36
37 /**
38  * This Image tag registy entry is setup to wrap the core JDK
39  * Image stream tools.
40  */

41 public class JDKRegistryEntry extends AbstractRegistryEntry
42     implements URLRegistryEntry {
43
44     /**
45      * The priority of this entry.
46      * This entry should in most cases be the last entry.
47      * but if one wishes one could set a priority higher and be called
48      * afterwords
49      */

50     public final static float PRIORITY =
51         1000*MagicNumberRegistryEntry.PRIORITY;
52
53     public JDKRegistryEntry() {
54         super ("JDK", PRIORITY, new String JavaDoc[0], new String JavaDoc [] {"image/gif"});
55     }
56
57     /**
58      * Check if the Stream references an image that can be handled by
59      * this format handler. The input stream passed in should be
60      * assumed to support mark and reset.
61      *
62      * If this method throws a StreamCorruptedException then the
63      * InputStream will be closed and a new one opened (if possible).
64      *
65      * This method should only throw a StreamCorruptedException if it
66      * is unable to restore the state of the InputStream
67      * (i.e. mark/reset fails basically).
68      */

69     public boolean isCompatibleURL(ParsedURL purl) {
70         try {
71             new URL JavaDoc(purl.toString());
72         } catch (MalformedURLException JavaDoc mue) {
73             // No sense in trying it if we can't build a URL out of it.
74
return false;
75         }
76         return true;
77     }
78
79     /**
80      * Decode the URL into a RenderableImage
81      *
82      * @param purl URL of the image.
83      * @param needRawData If true the image returned should not have
84      * any default color correction the file may
85      * specify applied.
86      */

87     public Filter handleURL(ParsedURL purl, boolean needRawData) {
88         
89         final URL JavaDoc url;
90         try {
91             url = new URL JavaDoc(purl.toString());
92         } catch (MalformedURLException JavaDoc mue) {
93             return null;
94         }
95
96         final DeferRable dr = new DeferRable();
97         final String JavaDoc errCode;
98         final Object JavaDoc [] errParam;
99         if (purl != null) {
100             errCode = ERR_URL_FORMAT_UNREADABLE;
101             errParam = new Object JavaDoc[] {"JDK", url};
102         } else {
103             errCode = ERR_STREAM_FORMAT_UNREADABLE;
104             errParam = new Object JavaDoc[] {"JDK"};
105         }
106
107         Thread JavaDoc t = new Thread JavaDoc() {
108                 public void run() {
109                     Filter filt = null;
110
111                     Toolkit JavaDoc tk = Toolkit.getDefaultToolkit();
112                     Image JavaDoc img = tk.createImage(url);
113
114                     if (img != null) {
115                         RenderedImage JavaDoc ri = loadImage(img, dr);
116                         if (ri != null) {
117                             filt = new RedRable(GraphicsUtil.wrap(ri));
118                         }
119                     }
120
121                     if (filt == null)
122                         filt = ImageTagRegistry.getBrokenLinkImage
123                             (this, errCode, errParam);
124                     
125                     dr.setSource(filt);
126                 }
127             };
128         t.start();
129         return dr;
130     }
131
132     // Stuff for Image Loading.
133
public RenderedImage JavaDoc loadImage(Image JavaDoc img, final DeferRable dr) {
134         // In some cases the image will be a
135
// BufferedImage (subclass of RenderedImage).
136
if (img instanceof RenderedImage JavaDoc)
137             return (RenderedImage JavaDoc)img;
138
139         MyImgObs observer = new MyImgObs();
140         Toolkit.getDefaultToolkit().prepareImage(img, -1, -1, observer);
141         observer.waitTilWidthHeightDone();
142         if (observer.imageError)
143             return null;
144         int width = observer.width;
145         int height = observer.height;
146         dr.setBounds(new Rectangle2D.Double JavaDoc(0, 0, width, height));
147
148         // Build the image to draw into.
149
BufferedImage JavaDoc bi = new BufferedImage JavaDoc
150             (width, height, BufferedImage.TYPE_INT_ARGB);
151         Graphics2D JavaDoc g2d = bi.createGraphics();
152         
153         // Wait till the image is fully loaded.
154
observer.waitTilImageDone();
155         if (observer.imageError)
156             return null;
157         dr.setProperties(new HashMap JavaDoc());
158
159         g2d.drawImage(img, 0, 0, null);
160         g2d.dispose();
161
162         return bi;
163     }
164
165
166     public static class MyImgObs implements ImageObserver JavaDoc {
167         boolean widthDone = false;
168         boolean heightDone = false;
169         boolean imageDone = false;
170         int width = -1;
171         int height = -1;
172         boolean imageError = false;
173
174         int IMG_BITS = ALLBITS|ERROR|ABORT;
175
176         public void clear() {
177             width=-1;
178             height=-1;
179             widthDone = false;
180             heightDone = false;
181             imageDone = false;
182         }
183
184         public boolean imageUpdate(Image JavaDoc img, int infoflags,
185                                    int x, int y, int width, int height) {
186             synchronized (this) {
187                 boolean notify = false;
188
189                 if ((infoflags & WIDTH) != 0) this.width = width;
190                 if ((infoflags & HEIGHT) != 0) this.height = height;
191
192                 if ((infoflags & ALLBITS) != 0) {
193                     this.width = width;
194                     this.height = height;
195                 }
196
197                 if ((infoflags & IMG_BITS) != 0) {
198                     if ((!widthDone) || (!heightDone) || (!imageDone)) {
199                         widthDone = true;
200                         heightDone = true;
201                         imageDone = true;
202                         notify = true;
203                     }
204                     if ((infoflags & ERROR) != 0) {
205                         imageError = true;
206                     }
207                 }
208
209
210                 if ((!widthDone) && (this.width != -1)) {
211                     notify = true;
212                     widthDone = true;
213                 }
214                 if ((!heightDone) && (this.height != -1)) {
215                     notify = true;
216                     heightDone = true;
217                 }
218
219                 if (notify)
220                     notifyAll();
221             }
222             return true;
223         }
224
225         public synchronized void waitTilWidthHeightDone() {
226             while ((!widthDone) || (!heightDone)) {
227                 try {
228                     // Wait for someone to set xxxDone
229
wait();
230                 }
231                 catch(InterruptedException JavaDoc ie) {
232                     // Loop around again see if src is set now...
233
}
234             }
235         }
236         public synchronized void waitTilWidthDone() {
237             while (!widthDone) {
238                 try {
239                     // Wait for someone to set xxxDone
240
wait();
241                 }
242                 catch(InterruptedException JavaDoc ie) {
243                     // Loop around again see if src is set now...
244
}
245             }
246         }
247         public synchronized void waitTilHeightDone() {
248             while (!heightDone) {
249                 try {
250                     // Wait for someone to set xxxDone
251
wait();
252                 }
253                 catch(InterruptedException JavaDoc ie) {
254                     // Loop around again see if src is set now...
255
}
256             }
257         }
258
259         public synchronized void waitTilImageDone() {
260             while (!imageDone) {
261                 try {
262                     // Wait for someone to set xxxDone
263
wait();
264                 }
265                 catch(InterruptedException JavaDoc ie) {
266                     // Loop around again see if src is set now...
267
}
268             }
269         }
270     }
271
272 }
273
Popular Tags