1 18 package org.apache.batik.ext.awt.image.spi; 19 20 import java.awt.Graphics2D ; 21 import java.awt.Image ; 22 import java.awt.Toolkit ; 23 import java.awt.geom.Rectangle2D ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.ImageObserver ; 26 import java.awt.image.RenderedImage ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.HashMap ; 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 41 public class JDKRegistryEntry extends AbstractRegistryEntry 42 implements URLRegistryEntry { 43 44 50 public final static float PRIORITY = 51 1000*MagicNumberRegistryEntry.PRIORITY; 52 53 public JDKRegistryEntry() { 54 super ("JDK", PRIORITY, new String [0], new String [] {"image/gif"}); 55 } 56 57 69 public boolean isCompatibleURL(ParsedURL purl) { 70 try { 71 new URL (purl.toString()); 72 } catch (MalformedURLException mue) { 73 return false; 75 } 76 return true; 77 } 78 79 87 public Filter handleURL(ParsedURL purl, boolean needRawData) { 88 89 final URL url; 90 try { 91 url = new URL (purl.toString()); 92 } catch (MalformedURLException mue) { 93 return null; 94 } 95 96 final DeferRable dr = new DeferRable(); 97 final String errCode; 98 final Object [] errParam; 99 if (purl != null) { 100 errCode = ERR_URL_FORMAT_UNREADABLE; 101 errParam = new Object [] {"JDK", url}; 102 } else { 103 errCode = ERR_STREAM_FORMAT_UNREADABLE; 104 errParam = new Object [] {"JDK"}; 105 } 106 107 Thread t = new Thread () { 108 public void run() { 109 Filter filt = null; 110 111 Toolkit tk = Toolkit.getDefaultToolkit(); 112 Image img = tk.createImage(url); 113 114 if (img != null) { 115 RenderedImage 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 public RenderedImage loadImage(Image img, final DeferRable dr) { 134 if (img instanceof RenderedImage ) 137 return (RenderedImage )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 (0, 0, width, height)); 147 148 BufferedImage bi = new BufferedImage 150 (width, height, BufferedImage.TYPE_INT_ARGB); 151 Graphics2D g2d = bi.createGraphics(); 152 153 observer.waitTilImageDone(); 155 if (observer.imageError) 156 return null; 157 dr.setProperties(new HashMap ()); 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 { 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 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(); 230 } 231 catch(InterruptedException ie) { 232 } 234 } 235 } 236 public synchronized void waitTilWidthDone() { 237 while (!widthDone) { 238 try { 239 wait(); 241 } 242 catch(InterruptedException ie) { 243 } 245 } 246 } 247 public synchronized void waitTilHeightDone() { 248 while (!heightDone) { 249 try { 250 wait(); 252 } 253 catch(InterruptedException ie) { 254 } 256 } 257 } 258 259 public synchronized void waitTilImageDone() { 260 while (!imageDone) { 261 try { 262 wait(); 264 } 265 catch(InterruptedException ie) { 266 } 268 } 269 } 270 } 271 272 } 273 | Popular Tags |