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.GraphicsUtil; 28 import org.apache.batik.ext.awt.image.renderable.DeferRable; 29 import org.apache.batik.ext.awt.image.renderable.Filter; 30 import org.apache.batik.ext.awt.image.renderable.RedRable; 31 import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed; 32 import org.apache.batik.ext.awt.image.rendered.FormatRed; 33 import org.apache.batik.ext.awt.image.rendered.CachableRed; 34 import org.apache.batik.util.ParsedURL; 35 36 import com.sun.image.codec.jpeg.JPEGCodec; 37 import com.sun.image.codec.jpeg.JPEGImageDecoder; 38 import com.sun.image.codec.jpeg.TruncatedFileException; 39 40 public class JPEGRegistryEntry 41 extends MagicNumberRegistryEntry { 42 43 static final byte [] sigJPEG = {(byte)0xFF, (byte)0xd8, 44 (byte)0xFF}; 45 static final String [] exts = {"jpeg", "jpg" }; 46 static final String [] mimeTypes = {"image/jpeg", "image/jpg" }; 47 static final MagicNumber [] magicNumbers = { 48 new MagicNumber(0, sigJPEG) 49 }; 50 51 public JPEGRegistryEntry() { 52 super("JPEG", exts, mimeTypes, magicNumbers); 53 } 54 55 65 public Filter handleStream(InputStream inIS, 66 ParsedURL origURL, 67 boolean needRawData) { 68 final DeferRable dr = new DeferRable(); 69 final InputStream is = inIS; 70 final String errCode; 71 final Object [] errParam; 72 if (origURL != null) { 73 errCode = ERR_URL_FORMAT_UNREADABLE; 74 errParam = new Object [] {"JPEG", origURL}; 75 } else { 76 errCode = ERR_STREAM_FORMAT_UNREADABLE; 77 errParam = new Object [] {"JPEG"}; 78 } 79 80 Thread t = new Thread () { 81 public void run() { 82 Filter filt; 83 try{ 84 JPEGImageDecoder decoder; 85 decoder = JPEGCodec.createJPEGDecoder(is); 86 BufferedImage image; 87 try { 88 image = decoder.decodeAsBufferedImage(); 89 } catch (TruncatedFileException tfe) { 90 image = tfe.getBufferedImage(); 91 if (image == null) 94 throw new IOException 95 ("JPEG File was truncated"); 96 } 97 dr.setBounds(new Rectangle2D.Double 98 (0, 0, image.getWidth(), 99 image.getHeight())); 100 CachableRed cr; 101 cr = GraphicsUtil.wrap(image); 102 cr = new Any2sRGBRed(cr); 103 cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre); 104 WritableRaster wr = (WritableRaster )cr.getData(); 105 ColorModel cm = cr.getColorModel(); 106 image = new BufferedImage 107 (cm, wr, cm.isAlphaPremultiplied(), null); 108 cr = GraphicsUtil.wrap(image); 109 filt = new RedRable(cr); 110 } catch (IOException ioe) { 111 filt = ImageTagRegistry.getBrokenLinkImage 113 (this, errCode, errParam); 114 } 115 116 dr.setSource(filt); 117 } 118 }; 119 t.start(); 120 return dr; 121 } 122 } 123 | Popular Tags |