KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.Rectangle2D JavaDoc;
21 import java.awt.image.BufferedImage JavaDoc;
22 import java.awt.image.ColorModel JavaDoc;
23 import java.awt.image.WritableRaster JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
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     /**
49      * Decode the Stream into a RenderableImage
50      *
51      * @param inIS The input stream that contains the image.
52      * @param origURL The original URL, if any, for documentation
53      * purposes only. This may be null.
54      * @param needRawData If true the image returned should not have
55      * any default color correction the file may
56      * specify applied. */

57     public Filter handleStream(InputStream JavaDoc inIS,
58                                ParsedURL origURL,
59                                boolean needRawData) {
60
61         final DeferRable dr = new DeferRable();
62         final InputStream JavaDoc is = inIS;
63         final boolean raw = needRawData;
64         final String JavaDoc errCode;
65         final Object JavaDoc [] errParam;
66         if (origURL != null) {
67             errCode = ERR_URL_FORMAT_UNREADABLE;
68             errParam = new Object JavaDoc[] {"PNG", origURL};
69         } else {
70             errCode = ERR_STREAM_FORMAT_UNREADABLE;
71             errParam = new Object JavaDoc[] {"PNG"};
72         }
73
74         Thread JavaDoc t = new Thread JavaDoc() {
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); // sRGB gamma
86
}
87                         CachableRed cr = new PNGRed(is, param);
88                         dr.setBounds(new Rectangle2D.Double JavaDoc
89                                      (0, 0, cr.getWidth(), cr.getHeight()));
90
91                         cr = new Any2sRGBRed(cr);
92                         cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
93                         WritableRaster JavaDoc wr = (WritableRaster JavaDoc)cr.getData();
94                         ColorModel JavaDoc cm = cr.getColorModel();
95                         BufferedImage JavaDoc image;
96                         image = new BufferedImage JavaDoc
97                             (cm, wr, cm.isAlphaPremultiplied(), null);
98                         cr = GraphicsUtil.wrap(image);
99                         filt = new RedRable(cr);
100                     } catch (IOException JavaDoc ioe) {
101                         filt = ImageTagRegistry.getBrokenLinkImage
102                             (this, errCode, errParam);
103                     } catch (ThreadDeath JavaDoc td) {
104                         throw td;
105                     } catch (Throwable JavaDoc 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