1 18 package org.apache.batik.svggen; 19 20 import java.awt.Dimension ; 21 import java.awt.Graphics2D ; 22 import java.awt.Image ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.RenderedImage ; 26 import java.awt.image.renderable.RenderableImage ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.lang.reflect.Method ; 31 32 import org.w3c.dom.Element ; 33 34 42 public abstract class DefaultCachedImageHandler 43 implements CachedImageHandler, 44 SVGSyntax, 45 ErrorConstants { 46 47 static final String XLINK_NAMESPACE_URI = 50 "http://www.w3.org/1999/xlink"; 51 52 static final AffineTransform IDENTITY = new AffineTransform (); 53 54 private static Method createGraphics = null; 56 private static boolean initDone = false; 57 private final static Class [] paramc = new Class [] {BufferedImage .class}; 58 private static Object [] paramo = null; 59 60 protected ImageCacher imageCacher; 61 62 65 public ImageCacher getImageCacher() { 66 return imageCacher; 67 } 68 69 void setImageCacher(ImageCacher imageCacher) { 70 if (imageCacher == null){ 71 throw new IllegalArgumentException (); 72 } 73 74 DOMTreeManager dtm = null; 76 if (this.imageCacher != null){ 77 dtm = this.imageCacher.getDOMTreeManager(); 78 } 79 80 this.imageCacher = imageCacher; 81 if (dtm != null){ 82 this.imageCacher.setDOMTreeManager(dtm); 83 } 84 } 85 86 90 public void setDOMTreeManager(DOMTreeManager domTreeManager){ 91 imageCacher.setDOMTreeManager(domTreeManager); 92 } 93 94 100 private static Graphics2D createGraphics(BufferedImage buf) { 101 if (!initDone) { 102 try { 103 Class clazz = Class.forName("org.apache.batik.ext.awt.image.GraphicsUtil"); 104 createGraphics = clazz.getMethod("createGraphics", paramc); 105 paramo = new Object [1]; 106 } catch (Throwable t) { 107 } finally { 109 initDone = true; 110 } 111 } 112 if (createGraphics == null) 113 return buf.createGraphics(); 114 else { 115 paramo[0] = buf; 116 Graphics2D g2d = null; 117 try { 118 g2d = (Graphics2D )createGraphics.invoke(null, paramo); 119 } catch (Exception e) { 120 } 122 return g2d; 123 } 124 } 125 126 133 public Element createElement(SVGGeneratorContext generatorContext) { 134 Element imageElement = 136 generatorContext.getDOMFactory().createElementNS 137 (SVG_NAMESPACE_URI, SVG_IMAGE_TAG); 138 139 return imageElement; 140 } 141 142 145 public AffineTransform handleImage(Image image, 146 Element imageElement, 147 int x, int y, 148 int width, int height, 149 SVGGeneratorContext generatorContext) { 150 151 int imageWidth = image.getWidth(null); 152 int imageHeight = image.getHeight(null); 153 AffineTransform af = null; 154 155 if(imageWidth == 0 || imageHeight == 0 || 156 width == 0 || height == 0) { 157 158 handleEmptyImage(imageElement); 160 161 } else { 162 try { 164 handleHREF(image, imageElement, generatorContext); 165 } catch (SVGGraphics2DIOException e) { 166 try { 167 generatorContext.errorHandler.handleError(e); 168 } catch (SVGGraphics2DIOException io) { 169 throw new SVGGraphics2DRuntimeException(io); 172 } 173 } 174 175 af = handleTransform(imageElement, x, y, imageWidth, imageHeight, 179 width, height, generatorContext); 180 } 181 return af; 182 } 183 184 187 public AffineTransform handleImage(RenderedImage image, 188 Element imageElement, 189 int x, int y, 190 int width, int height, 191 SVGGeneratorContext generatorContext) { 192 193 int imageWidth = image.getWidth(); 194 int imageHeight = image.getHeight(); 195 AffineTransform af = null; 196 197 if(imageWidth == 0 || imageHeight == 0 || 198 width == 0 || height == 0) { 199 200 handleEmptyImage(imageElement); 202 203 } else { 204 try { 206 handleHREF(image, imageElement, generatorContext); 207 } catch (SVGGraphics2DIOException e) { 208 try { 209 generatorContext.errorHandler.handleError(e); 210 } catch (SVGGraphics2DIOException io) { 211 throw new SVGGraphics2DRuntimeException(io); 214 } 215 } 216 217 af = handleTransform(imageElement, x, y, imageWidth, imageHeight, 221 width, height, generatorContext); 222 } 223 return af; 224 } 225 226 229 public AffineTransform handleImage(RenderableImage image, 230 Element imageElement, 231 double x, double y, 232 double width, double height, 233 SVGGeneratorContext generatorContext) { 234 235 double imageWidth = image.getWidth(); 236 double imageHeight = image.getHeight(); 237 AffineTransform af = null; 238 239 if(imageWidth == 0 || imageHeight == 0 || 240 width == 0 || height == 0) { 241 242 handleEmptyImage(imageElement); 244 245 } else { 246 try { 248 handleHREF(image, imageElement, generatorContext); 249 } catch (SVGGraphics2DIOException e) { 250 try { 251 generatorContext.errorHandler.handleError(e); 252 } catch (SVGGraphics2DIOException io) { 253 throw new SVGGraphics2DRuntimeException(io); 256 } 257 } 258 259 af = handleTransform(imageElement, x,y, 263 imageWidth, imageHeight, 264 width, height, generatorContext); 265 } 266 return af; 267 } 268 269 274 protected AffineTransform handleTransform(Element imageElement, 275 double x, double y, 276 double srcWidth, 277 double srcHeight, 278 double dstWidth, 279 double dstHeight, 280 SVGGeneratorContext generatorContext) { 281 imageElement.setAttributeNS(null, 285 SVG_X_ATTRIBUTE, 286 generatorContext.doubleString(x)); 287 imageElement.setAttributeNS(null, 288 SVG_Y_ATTRIBUTE, 289 generatorContext.doubleString(y)); 290 imageElement.setAttributeNS(null, 291 SVG_WIDTH_ATTRIBUTE, 292 generatorContext.doubleString(dstWidth)); 293 imageElement.setAttributeNS(null, 294 SVG_HEIGHT_ATTRIBUTE, 295 generatorContext.doubleString(dstHeight)); 296 return null; 297 } 298 299 protected void handleEmptyImage(Element imageElement) { 300 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 301 ATTR_XLINK_HREF, ""); 302 imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "0"); 303 imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "0"); 304 } 305 306 310 public void handleHREF(Image image, Element imageElement, 311 SVGGeneratorContext generatorContext) 312 throws SVGGraphics2DIOException { 313 if (image == null) 314 throw new SVGGraphics2DRuntimeException(ERR_IMAGE_NULL); 315 316 int width = image.getWidth(null); 317 int height = image.getHeight(null); 318 319 if (width==0 || height==0) { 320 handleEmptyImage(imageElement); 321 } else { 322 if (image instanceof RenderedImage ) { 323 handleHREF((RenderedImage )image, imageElement, 324 generatorContext); 325 } else { 326 BufferedImage buf = buildBufferedImage(new Dimension (width, height)); 327 Graphics2D g = createGraphics(buf); 328 g.drawImage(image, 0, 0, null); 329 g.dispose(); 330 handleHREF((RenderedImage )buf, imageElement, 331 generatorContext); 332 } 333 } 334 } 335 336 340 public BufferedImage buildBufferedImage(Dimension size){ 341 return new BufferedImage (size.width, size.height, getBufferedImageType()); 342 } 343 344 348 protected void handleHREF(RenderedImage image, Element imageElement, 349 SVGGeneratorContext generatorContext) 350 throws SVGGraphics2DIOException { 351 BufferedImage buf = null; 355 if (image instanceof BufferedImage 356 && 357 ((BufferedImage )image).getType() == getBufferedImageType()){ 358 buf = (BufferedImage )image; 359 } else { 360 Dimension size = new Dimension (image.getWidth(), image.getHeight()); 361 buf = buildBufferedImage(size); 362 363 Graphics2D g = createGraphics(buf); 364 365 g.drawRenderedImage(image, IDENTITY); 366 g.dispose(); 367 } 368 369 cacheBufferedImage(imageElement, buf, generatorContext); 373 } 374 375 379 protected void handleHREF(RenderableImage image, Element imageElement, 380 SVGGeneratorContext generatorContext) 381 throws SVGGraphics2DIOException { 382 Dimension size = new Dimension ((int)Math.ceil(image.getWidth()), 384 (int)Math.ceil(image.getHeight())); 385 BufferedImage buf = buildBufferedImage(size); 386 387 Graphics2D g = createGraphics(buf); 388 389 g.drawRenderableImage(image, IDENTITY); 390 g.dispose(); 391 392 handleHREF((RenderedImage )buf, imageElement, generatorContext); 393 } 394 395 protected void cacheBufferedImage(Element imageElement, 396 BufferedImage buf, 397 SVGGeneratorContext generatorContext) 398 throws SVGGraphics2DIOException { 399 400 ByteArrayOutputStream os; 401 402 if (generatorContext == null) 403 throw new SVGGraphics2DRuntimeException(ERR_CONTEXT_NULL); 404 405 try { 406 os = new ByteArrayOutputStream (); 407 encodeImage(buf, os); 409 os.flush(); 410 os.close(); 411 } catch (IOException e) { 412 throw new SVGGraphics2DIOException(ERR_UNEXPECTED, e); 414 } 415 416 String ref = imageCacher.lookup(os, 418 buf.getWidth(), 419 buf.getHeight(), 420 generatorContext); 421 422 imageElement.setAttributeNS(XLINK_NAMESPACE_URI, 424 ATTR_XLINK_HREF, 425 getRefPrefix() + ref); 426 } 427 428 432 public abstract String getRefPrefix(); 433 434 438 public abstract void encodeImage(BufferedImage buf, OutputStream os) 439 throws IOException ; 440 441 445 public abstract int getBufferedImageType(); 446 447 } 448 | Popular Tags |