KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc [] exts = {"jpeg", "jpg" };
46     static final String JavaDoc [] 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     /**
56      * Decode the Stream into a RenderableImage
57      *
58      * @param inIS The input stream that contains the image.
59      * @param origURL The original URL, if any, for documentation
60      * purposes only. This may be null.
61      * @param needRawData If true the image returned should not have
62      * any default color correction the file may
63      * specify applied.
64      */

65     public Filter handleStream(InputStream JavaDoc inIS,
66                                ParsedURL origURL,
67                                boolean needRawData) {
68         final DeferRable dr = new DeferRable();
69         final InputStream JavaDoc is = inIS;
70         final String JavaDoc errCode;
71         final Object JavaDoc [] errParam;
72         if (origURL != null) {
73             errCode = ERR_URL_FORMAT_UNREADABLE;
74             errParam = new Object JavaDoc[] {"JPEG", origURL};
75         } else {
76             errCode = ERR_STREAM_FORMAT_UNREADABLE;
77             errParam = new Object JavaDoc[] {"JPEG"};
78         }
79
80         Thread JavaDoc t = new Thread JavaDoc() {
81                 public void run() {
82                     Filter filt;
83                     try{
84                         JPEGImageDecoder decoder;
85                         decoder = JPEGCodec.createJPEGDecoder(is);
86                         BufferedImage JavaDoc image;
87                         try {
88                             image = decoder.decodeAsBufferedImage();
89                         } catch (TruncatedFileException tfe) {
90                             image = tfe.getBufferedImage();
91                             // Should probably draw some indication
92
// that this is a partial image....
93
if (image == null)
94                                 throw new IOException JavaDoc
95                                     ("JPEG File was truncated");
96                         }
97                         dr.setBounds(new Rectangle2D.Double JavaDoc
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 JavaDoc wr = (WritableRaster JavaDoc)cr.getData();
105                         ColorModel JavaDoc cm = cr.getColorModel();
106                         image = new BufferedImage JavaDoc
107                             (cm, wr, cm.isAlphaPremultiplied(), null);
108                         cr = GraphicsUtil.wrap(image);
109                         filt = new RedRable(cr);
110                     } catch (IOException JavaDoc ioe) {
111                         // Something bad happened here...
112
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