001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image.codec.jpeg;
020:
021: import java.awt.geom.Rectangle2D;
022: import java.awt.image.BufferedImage;
023: import java.awt.image.ColorModel;
024: import java.awt.image.WritableRaster;
025: import java.io.IOException;
026: import java.io.InputStream;
027:
028: import org.apache.batik.ext.awt.image.GraphicsUtil;
029: import org.apache.batik.ext.awt.image.renderable.DeferRable;
030: import org.apache.batik.ext.awt.image.renderable.Filter;
031: import org.apache.batik.ext.awt.image.renderable.RedRable;
032: import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed;
033: import org.apache.batik.ext.awt.image.rendered.FormatRed;
034: import org.apache.batik.ext.awt.image.rendered.CachableRed;
035: import org.apache.batik.ext.awt.image.spi.ImageTagRegistry;
036: import org.apache.batik.ext.awt.image.spi.MagicNumberRegistryEntry;
037: import org.apache.batik.util.ParsedURL;
038:
039: import com.sun.image.codec.jpeg.JPEGCodec;
040: import com.sun.image.codec.jpeg.JPEGImageDecoder;
041: import com.sun.image.codec.jpeg.TruncatedFileException;
042:
043: /**
044: *
045: * @version $Id: JPEGRegistryEntry.java 501094 2007-01-29 16:35:37Z deweese $
046: */
047: public class JPEGRegistryEntry extends MagicNumberRegistryEntry {
048:
049: static final byte[] sigJPEG = { (byte) 0xFF, (byte) 0xd8,
050: (byte) 0xFF };
051: static final String[] exts = { "jpeg", "jpg" };
052: static final String[] mimeTypes = { "image/jpeg", "image/jpg" };
053: static final MagicNumber[] magicNumbers = { new MagicNumber(0,
054: sigJPEG) };
055:
056: public JPEGRegistryEntry() {
057: super ("JPEG", exts, mimeTypes, magicNumbers);
058: }
059:
060: /**
061: * Decode the Stream into a RenderableImage
062: *
063: * @param inIS The input stream that contains the image.
064: * @param origURL The original URL, if any, for documentation
065: * purposes only. This may be null.
066: * @param needRawData If true the image returned should not have
067: * any default color correction the file may
068: * specify applied.
069: */
070: public Filter handleStream(InputStream inIS, ParsedURL origURL,
071: boolean needRawData) {
072: final DeferRable dr = new DeferRable();
073: final InputStream is = inIS;
074: final String errCode;
075: final Object[] errParam;
076: if (origURL != null) {
077: errCode = ERR_URL_FORMAT_UNREADABLE;
078: errParam = new Object[] { "JPEG", origURL };
079: } else {
080: errCode = ERR_STREAM_FORMAT_UNREADABLE;
081: errParam = new Object[] { "JPEG" };
082: }
083:
084: Thread t = new Thread() {
085: public void run() {
086: Filter filt;
087: try {
088: JPEGImageDecoder decoder;
089: decoder = JPEGCodec.createJPEGDecoder(is);
090: BufferedImage image;
091: try {
092: image = decoder.decodeAsBufferedImage();
093: } catch (TruncatedFileException tfe) {
094: image = tfe.getBufferedImage();
095: // Should probably draw some indication
096: // that this is a partial image....
097: if (image == null)
098: throw new IOException(
099: "JPEG File was truncated");
100: }
101: dr.setBounds(new Rectangle2D.Double(0, 0, image
102: .getWidth(), image.getHeight()));
103: CachableRed cr;
104: cr = GraphicsUtil.wrap(image);
105: cr = new Any2sRGBRed(cr);
106: cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
107: WritableRaster wr = (WritableRaster) cr.getData();
108: ColorModel cm = cr.getColorModel();
109: image = new BufferedImage(cm, wr, cm
110: .isAlphaPremultiplied(), null);
111: cr = GraphicsUtil.wrap(image);
112: filt = new RedRable(cr);
113: } catch (IOException ioe) {
114: // Something bad happened here...
115: filt = ImageTagRegistry.getBrokenLinkImage(
116: JPEGRegistryEntry.this , errCode, errParam);
117: } catch (ThreadDeath td) {
118: filt = ImageTagRegistry.getBrokenLinkImage(
119: JPEGRegistryEntry.this , errCode, errParam);
120: dr.setSource(filt);
121: throw td;
122: } catch (Throwable t) {
123: filt = ImageTagRegistry.getBrokenLinkImage(
124: JPEGRegistryEntry.this, errCode, errParam);
125: }
126:
127: dr.setSource(filt);
128: }
129: };
130: t.start();
131: return dr;
132: }
133: }
|