⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pngimage.java

📁 源码包含生成 PDF 和 HTML 的类库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright 2003-2008 by Paulo Soares. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ * * This code is based on a series of source files originally released * by SUN in the context of the JAI project. The original code was released  * under the BSD license in a specific wording. In a mail dating from * January 23, 2008, Brian Burkhalter (@sun.com) gave us permission * to use the code under the following version of the BSD license: * * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:  *  * - Redistribution of source code must retain the above copyright  *   notice, this  list of conditions and the following disclaimer. *  * - Redistribution in binary form must reproduce the above copyright *   notice, this list of conditions and the following disclaimer in  *   the documentation and/or other materials provided with the *   distribution. *  * Neither the name of Sun Microsystems, Inc. or the names of  * contributors may be used to endorse or promote products derived  * from this software without specific prior written permission. *  * This software is provided "AS IS," without a warranty of any  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL  * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF  * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR  * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES.  *  * You acknowledge that this software is not designed or intended for  * use in the design, construction, operation or maintenance of any  * nuclear facility. */package com.lowagie.text.pdf.codec;import java.awt.color.ICC_Profile;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.zip.Inflater;import java.util.zip.InflaterInputStream;import com.lowagie.text.ExceptionConverter;import com.lowagie.text.Image;import com.lowagie.text.ImgRaw;import com.lowagie.text.Utilities;import com.lowagie.text.pdf.ByteBuffer;import com.lowagie.text.pdf.PdfArray;import com.lowagie.text.pdf.PdfDictionary;import com.lowagie.text.pdf.PdfLiteral;import com.lowagie.text.pdf.PdfName;import com.lowagie.text.pdf.PdfNumber;import com.lowagie.text.pdf.PdfObject;import com.lowagie.text.pdf.PdfReader;import com.lowagie.text.pdf.PdfString;/** Reads a PNG image. All types of PNG can be read. * <p> * It is based in part in the JAI codec. * * @author  Paulo Soares (psoares@consiste.pt) */public class PngImage {/** Some PNG specific values. */    public static final int[] PNGID = {137, 80, 78, 71, 13, 10, 26, 10};    /** A PNG marker. */    public static final String IHDR = "IHDR";    /** A PNG marker. */    public static final String PLTE = "PLTE";    /** A PNG marker. */    public static final String IDAT = "IDAT";    /** A PNG marker. */    public static final String IEND = "IEND";    /** A PNG marker. */    public static final String tRNS = "tRNS";    /** A PNG marker. */    public static final String pHYs = "pHYs";    /** A PNG marker. */    public static final String gAMA = "gAMA";    /** A PNG marker. */    public static final String cHRM = "cHRM";    /** A PNG marker. */    public static final String sRGB = "sRGB";    /** A PNG marker. */    public static final String iCCP = "iCCP";        private static final int TRANSFERSIZE = 4096;    private static final int PNG_FILTER_NONE = 0;    private static final int PNG_FILTER_SUB = 1;    private static final int PNG_FILTER_UP = 2;    private static final int PNG_FILTER_AVERAGE = 3;    private static final int PNG_FILTER_PAETH = 4;    private static final PdfName intents[] = {PdfName.PERCEPTUAL,        PdfName.RELATIVECALORIMETRIC,PdfName.SATURATION,PdfName.ABSOLUTECALORIMETRIC};        InputStream is;    DataInputStream dataStream;    int width;    int height;    int bitDepth;    int colorType;    int compressionMethod;    int filterMethod;    int interlaceMethod;    PdfDictionary additional = new PdfDictionary();    byte image[];    byte smask[];    byte trans[];    NewByteArrayOutputStream idat = new NewByteArrayOutputStream();    int dpiX;    int dpiY;    float XYRatio;    boolean genBWMask;    boolean palShades;    int transRedGray = -1;    int transGreen = -1;    int transBlue = -1;    int inputBands;    int bytesPerPixel; // number of bytes per input pixel    byte colorTable[];    float gamma = 1f;    boolean hasCHRM = false;    float xW, yW, xR, yR, xG, yG, xB, yB;    PdfName intent;    ICC_Profile icc_profile;            /** Creates a new instance of PngImage */    PngImage(InputStream is) {        this.is = is;    }        /** Reads a PNG from an url.     * @param url the url     * @throws IOException on error     * @return the image     */        public static Image getImage(URL url) throws IOException {        InputStream is = null;        try {            is = url.openStream();            Image img = getImage(is);            img.setUrl(url);            return img;        }        finally {            if (is != null) {                is.close();            }        }    }        /** Reads a PNG from a stream.     * @param is the stream     * @throws IOException on error     * @return the image     */        public static Image getImage(InputStream is) throws IOException {        PngImage png = new PngImage(is);        return png.getImage();    }        /** Reads a PNG from a file.     * @param file the file     * @throws IOException on error     * @return the image     */        public static Image getImage(String file) throws IOException {        return getImage(Utilities.toURL(file));    }        /** Reads a PNG from a byte array.     * @param data the byte array     * @throws IOException on error     * @return the image     */        public static Image getImage(byte data[]) throws IOException {        ByteArrayInputStream is = new ByteArrayInputStream(data);        Image img = getImage(is);        img.setOriginalData(data);        return img;    }        boolean checkMarker(String s) {        if (s.length() != 4)            return false;        for (int k = 0; k < 4; ++k) {            char c = s.charAt(k);            if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z'))                return false;        }        return true;    }        void readPng() throws IOException {        for (int i = 0; i < PNGID.length; i++) {            if (PNGID[i] != is.read())	{                throw new IOException("File is not a valid PNG.");            }        }        byte buffer[] = new byte[TRANSFERSIZE];        while (true) {            int len = getInt(is);            String marker = getString(is);            if (len < 0 || !checkMarker(marker))                throw new IOException("Corrupted PNG file.");            if (IDAT.equals(marker)) {                int size;                while (len != 0) {                    size = is.read(buffer, 0, Math.min(len, TRANSFERSIZE));                    if (size < 0)                        return;                    idat.write(buffer, 0, size);                    len -= size;                }            }            else if (tRNS.equals(marker)) {                switch (colorType) {                    case 0:                        if (len >= 2) {                            len -= 2;                            int gray = getWord(is);                            if (bitDepth == 16)                                transRedGray = gray;                            else                                additional.put(PdfName.MASK, new PdfLiteral("["+gray+" "+gray+"]"));                        }                        break;                    case 2:                        if (len >= 6) {                            len -= 6;                            int red = getWord(is);                            int green = getWord(is);                            int blue = getWord(is);                            if (bitDepth == 16) {                                transRedGray = red;                                transGreen = green;                                transBlue = blue;                            }                            else                                additional.put(PdfName.MASK, new PdfLiteral("["+red+" "+red+" "+green+" "+green+" "+blue+" "+blue+"]"));                        }                        break;                    case 3:                        if (len > 0) {                            trans = new byte[len];                            for (int k = 0; k < len; ++k)                                trans[k] = (byte)is.read();                            len = 0;                        }                        break;                }                Utilities.skip(is, len);            }            else if (IHDR.equals(marker)) {                width = getInt(is);                height = getInt(is);                                bitDepth = is.read();                colorType = is.read();                compressionMethod = is.read();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -