1 18 package org.apache.batik.ext.awt.image.spi; 19 20 import java.awt.geom.Rectangle2D ; 21 import java.awt.image.BufferedImage ; 22 import java.awt.image.ColorModel ; 23 import java.awt.image.WritableRaster ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 import org.apache.batik.ext.awt.image.codec.PNGDecodeParam; 28 import org.apache.batik.ext.awt.image.codec.PNGRed; 29 import org.apache.batik.ext.awt.image.GraphicsUtil; 30 import org.apache.batik.ext.awt.image.renderable.DeferRable; 31 import org.apache.batik.ext.awt.image.renderable.Filter; 32 import org.apache.batik.ext.awt.image.renderable.RedRable; 33 import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed; 34 import org.apache.batik.ext.awt.image.rendered.CachableRed; 35 import org.apache.batik.ext.awt.image.rendered.FormatRed; 36 import org.apache.batik.util.ParsedURL; 37 38 public class PNGRegistryEntry 39 extends MagicNumberRegistryEntry { 40 41 42 static final byte [] signature = {(byte)0x89, 80, 78, 71, 13, 10, 26, 10}; 43 44 public PNGRegistryEntry() { 45 super("PNG", "png", "image/png", 0, signature); 46 } 47 48 57 public Filter handleStream(InputStream inIS, 58 ParsedURL origURL, 59 boolean needRawData) { 60 61 final DeferRable dr = new DeferRable(); 62 final InputStream is = inIS; 63 final boolean raw = needRawData; 64 final String errCode; 65 final Object [] errParam; 66 if (origURL != null) { 67 errCode = ERR_URL_FORMAT_UNREADABLE; 68 errParam = new Object [] {"PNG", origURL}; 69 } else { 70 errCode = ERR_STREAM_FORMAT_UNREADABLE; 71 errParam = new Object [] {"PNG"}; 72 } 73 74 Thread t = new Thread () { 75 public void run() { 76 Filter filt; 77 try { 78 PNGDecodeParam param = new PNGDecodeParam(); 79 param.setExpandPalette(true); 80 81 if (raw) 82 param.setPerformGammaCorrection(false); 83 else { 84 param.setPerformGammaCorrection(true); 85 param.setDisplayExponent(2.2f); } 87 CachableRed cr = new PNGRed(is, param); 88 dr.setBounds(new Rectangle2D.Double 89 (0, 0, cr.getWidth(), cr.getHeight())); 90 91 cr = new Any2sRGBRed(cr); 92 cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre); 93 WritableRaster wr = (WritableRaster )cr.getData(); 94 ColorModel cm = cr.getColorModel(); 95 BufferedImage image; 96 image = new BufferedImage 97 (cm, wr, cm.isAlphaPremultiplied(), null); 98 cr = GraphicsUtil.wrap(image); 99 filt = new RedRable(cr); 100 } catch (IOException ioe) { 101 filt = ImageTagRegistry.getBrokenLinkImage 102 (this, errCode, errParam); 103 } catch (ThreadDeath td) { 104 throw td; 105 } catch (Throwable t) { 106 filt = ImageTagRegistry.getBrokenLinkImage 107 (this, errCode, errParam); 108 } 109 110 dr.setSource(filt); 111 } 112 }; 113 t.start(); 114 return dr; 115 } 116 } 117 | Popular Tags |