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.png;
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.CachableRed;
034: import org.apache.batik.ext.awt.image.rendered.FormatRed;
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: /**
040: *
041: * @version $Id: PNGRegistryEntry.java 501094 2007-01-29 16:35:37Z deweese $
042: */
043: public class PNGRegistryEntry extends MagicNumberRegistryEntry {
044:
045: static final byte[] signature = { (byte) 0x89, 80, 78, 71, 13, 10,
046: 26, 10 };
047:
048: public PNGRegistryEntry() {
049: super ("PNG", "png", "image/png", 0, signature);
050: }
051:
052: /**
053: * Decode the Stream into a RenderableImage
054: *
055: * @param inIS The input stream that contains the image.
056: * @param origURL The original URL, if any, for documentation
057: * purposes only. This may be null.
058: * @param needRawData If true the image returned should not have
059: * any default color correction the file may
060: * specify applied. */
061: public Filter handleStream(InputStream inIS, ParsedURL origURL,
062: boolean needRawData) {
063:
064: final DeferRable dr = new DeferRable();
065: final InputStream is = inIS;
066: final boolean raw = needRawData;
067: final String errCode;
068: final Object[] errParam;
069: if (origURL != null) {
070: errCode = ERR_URL_FORMAT_UNREADABLE;
071: errParam = new Object[] { "PNG", origURL };
072: } else {
073: errCode = ERR_STREAM_FORMAT_UNREADABLE;
074: errParam = new Object[] { "PNG" };
075: }
076:
077: Thread t = new Thread() {
078: public void run() {
079: Filter filt;
080: try {
081: PNGDecodeParam param = new PNGDecodeParam();
082: param.setExpandPalette(true);
083:
084: if (raw)
085: param.setPerformGammaCorrection(false);
086: else {
087: param.setPerformGammaCorrection(true);
088: param.setDisplayExponent(2.2f); // sRGB gamma
089: }
090: CachableRed cr = new PNGRed(is, param);
091: dr.setBounds(new Rectangle2D.Double(0, 0, cr
092: .getWidth(), cr.getHeight()));
093:
094: cr = new Any2sRGBRed(cr);
095: cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
096: WritableRaster wr = (WritableRaster) cr.getData();
097: ColorModel cm = cr.getColorModel();
098: BufferedImage image;
099: image = new BufferedImage(cm, wr, cm
100: .isAlphaPremultiplied(), null);
101: cr = GraphicsUtil.wrap(image);
102: filt = new RedRable(cr);
103: } catch (IOException ioe) {
104: filt = ImageTagRegistry.getBrokenLinkImage(
105: PNGRegistryEntry.this , errCode, errParam);
106: } catch (ThreadDeath td) {
107: filt = ImageTagRegistry.getBrokenLinkImage(
108: PNGRegistryEntry.this , errCode, errParam);
109: dr.setSource(filt);
110: throw td;
111: } catch (Throwable t) {
112: filt = ImageTagRegistry.getBrokenLinkImage(
113: PNGRegistryEntry.this, errCode, errParam);
114: }
115:
116: dr.setSource(filt);
117: }
118: };
119: t.start();
120: return dr;
121: }
122: }
|