📄 pngimage.java
字号:
/*com.sixlegs.image.png - Java package to read and display PNG imagesCopyright (C) 1998, 1999, 2001 Chris NoklebergThis library is free software; you can redistribute it and/ormodify it under the terms of the GNU Library General PublicLicense as published by the Free Software Foundation; eitherversion 2 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULibrary General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, write to theFree Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA.*/package com.sixlegs.image.png;import java.awt.Color;import java.awt.image.ImageConsumer;import java.awt.image.ImageProducer;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;/** * For more information visit <a href="http://www.sixlegs.com/">http://www.sixlegs.com/</a> * @see java.awt.image.ImageProducer * @version 1.2.3 May 14, 2002 * @author Chris Nokleberg <a href="mailto:chris@sixlegs.com"><chris@sixlegs.com></a> */public final class PngImageimplements ImageProducer{ /* package */ static boolean allFatal = false; /* package */ static final int BUFFER_SIZE = 8192; private static boolean progressive = true; private static Hashtable prototypes = new Hashtable(); /* package */ static final String ASCII_ENCODING = "US-ASCII"; /* package */ static final String LATIN1_ENCODING = "8859_1"; /* package */ static final String UTF8_ENCODING = "UTF8"; /* package */ static final long DEFAULT_GAMMA = 45455; private static double DISPLAY_EXPONENT = 2.2; private static double USER_EXPONENT = 1.0; /* package */ Data data = new Data(); private Vector errorList; final class Data { private final Vector consumers = new Vector(); private final Hashtable chunks = new Hashtable(); private int[] pixels; private boolean produceFailed; private boolean useFlush = false; /* package */ IDATInputStream in_idat; /* package */ Chunk_IHDR header; /* package */ Chunk_PLTE palette; /* package */ final int[] gammaTable = new int[256]; /* package */ final Hashtable textChunks = new Hashtable(); /* package */ final Hashtable properties = new Hashtable(); /* package */ final Hashtable palettes = new Hashtable(1); /* package */ final Vector gifExtensions = new Vector(); private Data() {} } /////////////////// start public //////////////////////// public static final int COLOR_TYPE_GRAY = 0; public static final int COLOR_TYPE_GRAY_ALPHA = 4; public static final int COLOR_TYPE_PALETTE = 3; public static final int COLOR_TYPE_RGB = 2; public static final int COLOR_TYPE_RGB_ALPHA = 6; public static final int INTERLACE_TYPE_NONE = 0; public static final int INTERLACE_TYPE_ADAM7 = 1; public static final int FILTER_TYPE_BASE = 0; public static final int FILTER_TYPE_INTRAPIXEL = 64; public static final int COMPRESSION_TYPE_BASE = 0; public static final int UNIT_UNKNOWN = 0; public static final int UNIT_METER = 1; public static final int UNIT_PIXEL = 0; public static final int UNIT_MICROMETER = 1; public static final int UNIT_RADIAN = 2; public static final int SRGB_PERCEPTUAL = 0; public static final int SRGB_RELATIVE_COLORIMETRIC = 1; public static final int SRGB_SATURATION_PRESERVING = 2; public static final int SRGB_ABSOLUTE_COLORIMETRIC = 3; /** * Constructs a PngImage object from a local PNG file. * @param filename full path to local PNG file */ public PngImage(String filename) throws IOException { FileInputStream fs = new FileInputStream(filename); init(new BufferedInputStream(fs, BUFFER_SIZE)); } /** * Constructs a <code>PngImage</code> object from a URL. * @param filename URL of PNG image */ public PngImage(URL url) throws IOException { InputStream is = url.openConnection().getInputStream(); init(new BufferedInputStream(is, BUFFER_SIZE)); } /** * Constructs a <code>PngImage</code> object from an input stream. * Buffer the stream for better performance. * @param filename URL of PNG image * @see java.io.BufferedInputStream */ public PngImage(InputStream is) { init(is); } /** * Adds an <code>ImageConsumer</code> to the list of consumers interested in * data for this image. * @see java.awt.image.ImageConsumer */ public void addConsumer(ImageConsumer ic) { if (data == null) return; if (data.consumers.contains(ic)) return; data.consumers.addElement(ic); } /** * Determine if an <code>ImageConsumer</code> is on the list of consumers currently * interested in data for this image. * @return true if the consumer is on the list, false otherwise. * @see java.awt.image.ImageConsumer */ public boolean isConsumer(ImageConsumer ic) { if (data == null) return false; return data.consumers.contains(ic); } /** * Remove an <code>ImageConsumer</code> from the list of consumers interested in * data for this image. * @see java.awt.image.ImageConsumer */ public void removeConsumer(ImageConsumer ic) { if (data == null) return; data.consumers.removeElement(ic); } /** * Adds an <code>ImageConsumer</code> to the list of consumers interested in * data for this image, and immediately start delivery of the * image data through the consumer/producer interface. * @see java.awt.image.ImageConsumer */ public void startProduction(ImageConsumer ic) { if (data == null) { throw new RuntimeException("Object has been flushed."); } addConsumer(ic); ImageConsumer[] ics = new ImageConsumer[data.consumers.size()]; data.consumers.copyInto(ics); produceHelper(ics); } /** * Requests delivery of image data to the specified <code>ImageConsumer</code> * one more time in top-down, left-right order. * @see #startProduction * @see java.awt.image.ImageConsumer */ public void requestTopDownLeftRightResend(ImageConsumer ic) { if (data == null || data.pixels == null) return; startProduction(ic); } /** * Sets the default desired final user exponent. Ideal setting * depends on user viewing conditions. The default value is 1.0. * Set greater than 1.0 to darken the mid-level tones, or less than * 1.0 to lighten them. * <p> * This method sets the user exponent for new <code>PngImage</code> objects. * It is not possible to change the user exponent of an existing * <code>PngImage</code>. * @param exponent desired user exponent */ static public void setUserExponent(double exponent) { USER_EXPONENT = exponent; } /** * Sets the default display exponent. Depends on monitor and gamma lookup * table settings (if any). Default value is 2.2, which should * work well with most PC displays. If the operating system has * a gamma lookup table (Macintosh) the display exponent should be lower. * <p> * This method sets the display exponent for new <code>PngImage</code> objects. * It is not possible to change the display exponent of an existing * <code>PngImage</code>. * @param exponent desired display exponent */ static public void setDisplayExponent(double exponent) { DISPLAY_EXPONENT = exponent; } /** * Checks if there were errors during image production. * A good time to check this is when you implement the <code>ImageObserver</code> * interface and the <code>ERROR</code> flag is set. * @see java.awt.image.ImageObserver * @see #getErrors */ public boolean hasErrors() { if (errorList == null) return false; return errorList.size() > 0; } public boolean hasFatalError() { return hasErrors() && !(errorList.elementAt(errorList.size() - 1) instanceof PngExceptionSoft); } /** * Returns an <code>Enumeration</code> of all the errors that occurred during * image production. This includes any non-fatal errors. * @see #hasErrors */ public Enumeration getErrors() { return errorList.elements(); } /** * Specifies whether all errors will abort the image production. * Normally, a value error in a non-critical chunk causes the * PNG loader to simply skip the offending chunk. */ static public void setAllErrorsFatal(boolean allFatal) { PngImage.allFatal = allFatal; } /** * Interlaced images can either be displayed when completely * read (default) or progressively. When progressive display is * enabled, a <code>PngImage</code> will call the <code>imageComplete</code> * method of its registered image consumers with a <code>SINGLEFRAMEDONE</code> * status after each pass. This, in turn, will trigger an <code>imageUpdate</code> * with the <code>FRAMEBITS</code> flag set to watching <code>ImageObservers</code>. * <p> * <b>Note:</b> Images are only delivered progressively on the * first production of the image data. Subsequent requests for the * (cached) image data will send the image as a complete single * frame. * @see java.awt.image.ImageConsumer * @see java.awt.image.ImageObserver */ static public void setProgressiveDisplay(boolean progressive) { PngImage.progressive = progressive; } /** * Get a suggested background color (from the bKGD chunk). * @see #getProperty * @return the suggested Color, or null if no valid bKGD was found. */ public Color getBackgroundColor() throws IOException { return (Color)getProperty("background"); } /** * Gets width of image in pixels. * @see #getProperty */ public int getWidth() throws IOException { readToData(); return data.header.width; } /** * Gets height of image in pixels. * @see #getProperty */ public int getHeight() throws IOException { readToData(); return data.header.height; } /** * Gets bit depth of image data. * @see #getProperty * @return 1, 2, 4, 8, or 16. */ public int getBitDepth() throws IOException { readToData();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -