From addc268423fac866799dab0ef5896dbdfd9e4fbf Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 07:35:46 +0000 Subject: [PATCH] 2003/09/18 08:10:35 darcs-hash:20040130073546-2ba56-f75b52fac9c5f2c7b0c24c45a70fa6b6754086a6.gz --- src/org/xwt/GIF.java | 443 -------------------------------- src/org/xwt/PNG.java | 699 -------------------------------------------------- 2 files changed, 1142 deletions(-) delete mode 100644 src/org/xwt/GIF.java delete mode 100644 src/org/xwt/PNG.java diff --git a/src/org/xwt/GIF.java b/src/org/xwt/GIF.java deleted file mode 100644 index daa38a7..0000000 --- a/src/org/xwt/GIF.java +++ /dev/null @@ -1,443 +0,0 @@ -/* - * This file was adapted from D J Hagberg's GifDecoder.java - * - * This software is copyrighted by D. J. Hagberg, Jr., and other parties. - * The following terms apply to all files associated with the software - * unless explicitly disclaimed in individual files. - * - * The authors hereby grant permission to use, copy, modify, distribute, - * and license this software and its documentation for any purpose, provided - * that existing copyright notices are retained in all copies and that this - * notice is included verbatim in any distributions. No written agreement, - * license, or royalty fee is required for any of the authorized uses. - * Modifications to this software may be copyrighted by their authors - * and need not follow the licensing terms described here, provided that - * the new terms are clearly indicated on the first page of each file where - * they apply. - * - * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY - * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES - * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY - * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE - * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE - * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR - * MODIFICATIONS. - * - * GOVERNMENT USE: If you are acquiring this software on behalf of the - * U.S. government, the Government shall have only "Restricted Rights" - * in the software and related documentation as defined in the Federal - * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you - * are acquiring the software on behalf of the Department of Defense, the - * software shall be classified as "Commercial Computer Software" and the - * Government shall have only "Restricted Rights" as defined in Clause - * 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the - * authors grant the U.S. Government and others acting in its behalf - * permission to use and distribute the software in accordance with the - * terms specified in this license. - * - */ -package org.xwt; - -import org.xwt.util.*; -import java.io.BufferedInputStream; -import java.io.InputStream; -import java.io.IOException; -import java.io.PrintWriter; - -/** Converts an InputStream carrying a GIF image into an ARGB int[] */ -public class GIF extends ImageDecoder { - - // Public Methods ///////////////////////////////////////////////////////// - - public int[] getData() { return data; } - public int getWidth() { return width; } - public int getHeight() { return height; } - - /** Processes an image from InputStream is; returns null if there is an error - @param name A string describing the image; used for error reporting. - */ - public static GIF decode(InputStream is, String name) { - try { - return new GIF(is, name); - } catch (Exception e) { - if (Log.on) Log.log(GIF.class, e); - return null; - } - } - - private GIF(InputStream is, String name) throws IOException { - if (is instanceof BufferedInputStream) _in = (BufferedInputStream)is; - else _in = new BufferedInputStream(is); - decodeAsBufferedImage(0); - } - - // Private Methods ///////////////////////////////////////////////////////// - - private int[] data = null; - - /** Decode a particular frame from the GIF file. Frames must be decoded in order, starting with 0. */ - private void decodeAsBufferedImage(int page) throws IOException, IOException { - - // If the user requested a page already decoded, we cannot go back. - if (page <= index ) return; - - // If we just started reading this stream, initialize the global info. - if (index < 0 ) { - if (!readIntoBuf(6) || _buf[0] != 'G' || _buf[1] != 'I' || _buf[2] != 'F' || - _buf[3] != '8' || !(_buf[4] == '7' || _buf[4] == '9') || _buf[5] != 'a') - throw new IOException("Not a GIF8Xa file."); - if (!readGlobalImageDescriptor()) throw new IOException("Unable to read GIF header."); - } - - // Loop through the blocks in the image. - int block_identifier; - while (true) { - if(!readIntoBuf(1)) throw new IOException("Unexpected EOF(1)"); - block_identifier = _buf[0]; - if (block_identifier == ';') throw new IOException("No image data"); - if (block_identifier == '!') { - if (!readExtensionBlock()) throw new IOException("Unexpected EOF(2)"); - continue; - } - - // not a valid start character -- ignore it. - if (block_identifier != ',') continue; - - if (!readLocalImageDescriptor()) throw new IOException("Unexpected EOF(3)"); - data = new int[width * height]; - readImage(); - - // If we did not decode the requested index, need to go on - // to the next one (future implementations should consider - // caching already-decoded images here to allow for random - // access to animated GIF pages). - index++; - if (index < page) continue; - - // If we did decode the requested index, we can return. - break; - } - - // Return the image thus-far decoded - return; - } - - /** Actually read the image data */ - private void readImage() - throws IOException, IOException { - int len = width; - int rows = height; - int initialCodeSize; - int v; - int xpos = 0, ypos = 0, pass = 0, i; - int prefix[] = new int[(1 << MAX_LWZ_BITS)]; - int append[] = new int[(1 << MAX_LWZ_BITS)]; - int stack[] = new int[(1 << MAX_LWZ_BITS)*2]; - int top_idx; - int codeSize, clearCode, inCode, endCode, oldCode, maxCode, code, firstCode; - - // Initialize the decoder - if (!readIntoBuf(1)) throw new IOException("Unexpected EOF decoding image"); - initialCodeSize = _buf[0]; - - // Look at the right color map, setting up transparency if called for. - int[] cmap = global_color_map; - if (hascmap) cmap = color_map; - if (trans_idx >= 0) cmap[trans_idx] = 0x00000000; - - /* Initialize the decoder */ - /* Set values for "special" numbers: - * clear code reset the decoder - * end code stop decoding - * code size size of the next code to retrieve - * max code next available table position - */ - clearCode = 1 << initialCodeSize; - endCode = clearCode + 1; - codeSize = initialCodeSize + 1; - maxCode = clearCode + 2; - oldCode = -1; - firstCode = -1; - - for (i = 0; i < clearCode; i++) append[i] = i; - top_idx = 0; // top of stack. - - bitsInWindow = 0; - bytes = 0; - window = 0L; - done = false; - c = -1; - - /* Read until we finish the image */ - ypos = 0; - for (i = 0; i < rows; i++) { - for (xpos = 0; xpos < len;) { - - if (top_idx == 0) { - /* Bummer -- our stack is empty. Now we have to work! */ - code = getCode(codeSize); - if (code < 0) return; - - if (code > maxCode || code == endCode) return; - if (code == clearCode) { - codeSize = initialCodeSize + 1; - maxCode = clearCode + 2; - oldCode = -1; - continue; - } - - // Last pass reset the decoder, so the first code we - // see must be a singleton. Seed the stack with it, - // and set up the old/first code pointers for - // insertion into the string table. We can't just - // roll this into the clearCode test above, because - // at that point we have not yet read the next code. - if (oldCode == -1) { - stack[top_idx++] = append[code]; - oldCode = code; - firstCode = code; - continue; - } - - inCode = code; - - // maxCode is always one bigger than our - // highest assigned code. If the code we see - // is equal to maxCode, then we are about to - // add a new string to the table. ??? - if (code == maxCode) { - stack[top_idx++] = firstCode; - code = oldCode; - } - - // Populate the stack by tracing the string in the - // string table from its tail to its head - while (code > clearCode) { - stack[top_idx++] = append[code]; - code = prefix[code]; - } - firstCode = append[code]; - - // If there's no more room in our string table, quit. - // Otherwise, add a new string to the table - if (maxCode >= (1 << MAX_LWZ_BITS)) return; - - // Push the head of the string onto the stack - stack[top_idx++] = firstCode; - - // Add a new string to the string table - prefix[maxCode] = oldCode; - append[maxCode] = firstCode; - maxCode++; - - // maxCode tells us the maximum code value we can accept. - // If we see that we need more bits to represent it than - // we are requesting from the unpacker, we need to increase - // the number we ask for. - if ((maxCode >= (1 << codeSize)) && (maxCode < (1<= rows) { - pass++; - if (pass > 3) return; - ypos = _interlaceStart[pass]; - } - } else ypos++; - } - return; - } - - /** Extract the next compression code from the file. */ - private int getCode(int code_size) throws IOException { - int ret; - - while (bitsInWindow < code_size) { - // Not enough bits in our window to cover the request - if (done) return -1; - - if (bytes == 0) { - // Not enough bytes in our buffer to add to the window - bytes = getDataBlock(); - c = 0; - if (bytes <= 0) { - done = true; - break; - } - } - // Tack another byte onto the window, see if that's enough - window += (_buf[c]) << bitsInWindow; - ++c; - bitsInWindow += 8; - bytes--; - } - - - // The next code will always be the last code_size bits of the window - ret = ((int)window) & ((1 << code_size) - 1); - - // Shift data in the window to put the next code at the end - window >>= code_size; - bitsInWindow -= code_size; - return ret; - } - - /** Read the global image descriptor and optional global color map. Sets global_* variables. */ - private boolean readGlobalImageDescriptor() throws IOException { - int packed; - int aspect; // we ignore this. - int ofs; - - if (!readIntoBuf(7) ) return false; - global_width = _buf[0] | (_buf[1] << 8); - global_height = _buf[2] | (_buf[3] << 8); - packed = _buf[4]; - global_bgcolor = _buf[5]; - aspect = _buf[6]; - global_cmapsize = 2 << (packed & 0x07); - global_hascmap = (packed & GLOBALCOLORMAP) == GLOBALCOLORMAP; - global_color_map = null; - - // Read the color map, if we have one. - if (global_hascmap) { - if (!readColorMap(global_cmapsize,true)) { - return false; - } - } - - return true; - } - - /** Read a local image descriptor and optional local color map. */ - private boolean readLocalImageDescriptor() throws IOException { - int packed; - - if (!readIntoBuf(9) ) return false; - - left = _buf[0] | (_buf[1] << 8); - top = _buf[2] | (_buf[3] << 8); - width = _buf[4] | (_buf[5] << 8); - height = _buf[6] | (_buf[7] << 8); - packed = _buf[8]; - hascmap = (packed & LOCALCOLORMAP) == LOCALCOLORMAP; - cmapsize = 2 << (packed & 0x07); - interlaced = (packed & INTERLACE) == INTERLACE; - color_map = null; - - // Read the local color table, if there is one. - return !(hascmap && !readColorMap(cmapsize,false)); - } - - /** Read a color map (global or local). */ - private boolean readColorMap(int nColors, boolean isGlobal) - throws IOException { - int[] map = new int[nColors]; - for( int i=0; i < nColors; ++i) { - if (!readIntoBuf(3) ) return false; - map[i] = (_buf[0] << 16) | (_buf[1] << 8) | _buf[2] | 0xFF000000; - } - if (isGlobal) global_color_map = map; - else color_map = map; - return true; - } - - /** Read the contents of a GIF89a Graphical Extension Block. */ - private boolean readExtensionBlock() throws IOException { - if (!readIntoBuf(1) ) return false; - int label = _buf[0]; - int count = -1; - switch (label) { - case 0x01: // Plain Text Extension - case 0xff: // Application Extension - case 0xfe: // Comment Extension - break; - case 0xf9: // Graphic Control Extension - count = getDataBlock(); - if (count < 0) return true; - // Check for transparency setting. - if ((_buf[0] & HASTRANSPARENCY) != 0) trans_idx = _buf[3]; - else trans_idx = -1; - } - do { count = getDataBlock(); } while (count > 0); - return true; - } - - /** Read a block of data from the GIF file. */ - private int getDataBlock() throws IOException { - if (!readIntoBuf(1) ) return -1; - int count = _buf[0]; - if (count != 0) if (!readIntoBuf(count) ) return -1; - return count; - } - - /** Read the indicated number of bytes into _buf, our instance-wide buffer. */ - private boolean readIntoBuf(int count) throws IOException { - for(int i = 0; i < count; i++) if ((_buf[i] = _in.read()) == -1) return false; - return true; - } - - // Private Data ////////////////////////////////////////////////////////// - - // State management stuff - private int index = -1; - private BufferedInputStream _in = null; - private int[] _buf = new int[BUFSIZE]; - - // Transparency settings - private int trans_idx = -1; - - // Global image descriptor contents - private int global_width = 0; - private int global_height = 0; - private int global_bgcolor = 0; - private int global_cmapsize = 0; - private boolean global_hascmap = false; - private int[] global_color_map = null; - - // Local image descriptor contents - private int left = 0; - private int top = 0; - private int width = 0; - private int height = 0; - private int cmapsize = 0; - private boolean hascmap = false; - private boolean interlaced = false; - private int[] color_map = null; - - // Variables used in getCode(...) to track sliding bit-window. - private int bytes = 0; - private boolean done; - private int c; - private long window; - private int bitsInWindow = 0; - - // Class-wide constants. - private static final int INTERLACE = 0x40; - private static final int GLOBALCOLORMAP = 0x80; - private static final int LOCALCOLORMAP = 0x80; - private static final int HASTRANSPARENCY = 0x01; - private static final int MAX_LWZ_BITS = 12; - private static final int BUFSIZE = 280; - private static final int[] _interlaceStep = { 8, 8, 4, 2 }; - private static final int[] _interlaceStart = { 0, 4, 2, 1 }; -} - - - diff --git a/src/org/xwt/PNG.java b/src/org/xwt/PNG.java deleted file mode 100644 index 4f5640a..0000000 --- a/src/org/xwt/PNG.java +++ /dev/null @@ -1,699 +0,0 @@ -/* - * This file was adapted from Jason Marshall's PNGImageProducer.java - * - * Copyright (c) 1997, Jason Marshall. All Rights Reserved - * - * The author makes no representations or warranties regarding the suitability, - * reliability or stability of this code. This code is provided AS IS. The - * author shall not be liable for any damages suffered as a result of using, - * modifying or redistributing this software or any derivitives thereof. - * Permission to use, reproduce, modify and/or (re)distribute this software is - * hereby granted. - */ - -package org.xwt; - -import org.xwt.util.*; -import java.io.*; -import java.util.Hashtable; -import java.util.Vector; -import java.util.Enumeration; -import java.util.zip.*; - -/** Converts an InputStream carrying a PNG image into an ARGB int[] */ -public class PNG extends ImageDecoder { - - // Public Methods /////////////////////////////////////////////////////////////////////////////// - - /** returns the ARGB int[] representing the last image processed */ - public final int[] getData() { return data; } - - /** returns the width of the last image processed */ - public final int getWidth() { return width; } - - /** returns the height of the last image processed */ - public final int getHeight() { return height; } - - /** process a PNG as an inputstream; returns null if there is an error - @param name A string describing the image, to be used when logging errors - */ - public static PNG decode(InputStream is, String name) { - try { - return new PNG(is, name); - } catch (Exception e) { - if (Log.on) Log.log(PNG.class, e); - return null; - } - } - - private PNG(InputStream is, String name) throws IOException { - underlyingStream = is; - target_offset = 0; - inputStream = new DataInputStream(underlyingStream); - - // consume the header - if ((inputStream.read() != 137) || (inputStream.read() != 80) || (inputStream.read() != 78) || (inputStream.read() != 71) || - (inputStream.read() != 13) || (inputStream.read() != 10) || (inputStream.read() != 26) || (inputStream.read() != 10)) { - Log.log(this, "PNG: error: input file " + name + " is not a PNG file"); - data = new int[] { }; - width = height = 0; - return; - } - - DONE: while (!error) { - if (needChunkInfo) { - chunkLength = inputStream.readInt(); - chunkType = inputStream.readInt(); - needChunkInfo = false; - } - - switch (chunkType) { - case CHUNK_bKGD: inputStream.skip(chunkLength); break; - case CHUNK_cHRM: inputStream.skip(chunkLength); break; - case CHUNK_gAMA: inputStream.skip(chunkLength); break; - case CHUNK_hIST: inputStream.skip(chunkLength); break; - case CHUNK_pHYs: inputStream.skip(chunkLength); break; - case CHUNK_sBIT: inputStream.skip(chunkLength); break; - case CHUNK_tEXt: inputStream.skip(chunkLength); break; - case CHUNK_zTXt: inputStream.skip(chunkLength); break; - case CHUNK_tIME: inputStream.skip(chunkLength); break; - - case CHUNK_IHDR: handleIHDR(); break; - case CHUNK_PLTE: handlePLTE(); break; - case CHUNK_tRNS: handletRNS(); break; - - case CHUNK_IDAT: handleIDAT(); break; - - case CHUNK_IEND: break DONE; - default: - System.err.println("unrecognized chunk type " + - Integer.toHexString(chunkType) + ". skipping"); - inputStream.skip(chunkLength); - } - - int crc = inputStream.readInt(); - needChunkInfo = true; - } - } - - // Chunk Handlers /////////////////////////////////////////////////////////////////////// - - /** handle data chunk */ - private void handleIDAT() throws IOException { - if (width == -1 || height == -1) throw new IOException("never got image width/height"); - switch (depth) { - case 1: mask = 0x1; break; - case 2: mask = 0x3; break; - case 4: mask = 0xf; break; - case 8: case 16: mask = 0xff; break; - default: mask = 0x0; break; - } - if (depth < 8) smask = mask << depth; - else smask = mask << 8; - - int count = width * height; - - switch (colorType) { - case 0: - case 2: - case 6: - case 4: - ipixels = new int[count]; - pixels = ipixels; - break; - case 3: - bpixels = new byte[count]; - pixels = bpixels; - break; - default: - throw new IOException("Image has unknown color type"); - } - if (interlaceMethod != 0) multipass = true; - readImageData(); - } - - /** handle header chunk */ - private void handleIHDR() throws IOException { - if (headerFound) throw new IOException("Extraneous IHDR chunk encountered."); - if (chunkLength != 13) throw new IOException("IHDR chunk length wrong: " + chunkLength); - width = inputStream.readInt(); - height = inputStream.readInt(); - depth = inputStream.read(); - colorType = inputStream.read(); - compressionMethod = inputStream.read(); - filterMethod = inputStream.read(); - interlaceMethod = inputStream.read(); - } - - /** handle pallette chunk */ - private void handlePLTE() throws IOException { - if (colorType == 3) { - palette = new byte[chunkLength]; - inputStream.readFully(palette); - } else { - // Ignore suggested palette - inputStream.skip(chunkLength); - } - } - - /** handle transparency chunk; modifies palette */ - private void handletRNS() throws IOException { - int chunkLen = chunkLength; - if (palette == null) { - if (Log.on) Log.log(this, "warning: tRNS chunk encountered before pLTE; ignoring alpha channel"); - inputStream.skip(chunkLength); - return; - } - int len = palette.length; - if (colorType == 3) { - transparency = true; - - int transLength = len/3; - byte[] trans = new byte[transLength]; - for (int i = 0; i < transLength; i++) trans[i] = (byte) 0xff; - inputStream.readFully(trans, 0, chunkLength); - - byte[] newPalette = new byte[len + transLength]; - for (int i = newPalette.length; i > 0;) { - newPalette[--i] = trans[--transLength]; - newPalette[--i] = palette[--len]; - newPalette[--i] = palette[--len]; - newPalette[--i] = palette[--len]; - } - palette = newPalette; - - } else { - inputStream.skip(chunkLength); - } - } - - /// Helper functions for IDAT /////////////////////////////////////////////////////////////////////////////////////////// - - /** Read Image data in off of a compression stream */ - private void readImageData() throws IOException { - InputStream dataStream = new SequenceInputStream(new IDATEnumeration(this)); - DataInputStream dis = new DataInputStream(new BufferedInputStream(new InflaterInputStream(dataStream, new Inflater()))); - int bps, filterOffset; - switch (colorType) { - case 0: case 3: bps = depth; break; - case 2: bps = 3 * depth; break; - case 4: bps = depth<<1; break; - case 6: bps = depth<<2; break; - default: throw new IOException("Unknown color type encountered."); - } - - filterOffset = (bps + 7) >> 3; - - for (pass = (multipass ? 1 : 0); pass < 8; pass++) { - int pass = this.pass; - int rInc = rowInc[pass]; - int cInc = colInc[pass]; - int sCol = startingCol[pass]; - int val = (width - sCol + cInc - 1) / cInc; - int samples = val * filterOffset; - int rowSize = (val * bps)>>3; - int sRow = startingRow[pass]; - if (height <= sRow || rowSize == 0) continue; - int sInc = rInc * width; - byte inbuf[] = new byte[rowSize]; - int pix[] = new int[rowSize]; - int upix[] = null; - int temp[] = new int[rowSize]; - int nextY = sRow; // next Y value and number of rows to report to sendPixels - int rows = 0; - int rowStart = sRow * width; - - for (int y = sRow; y < height; y += rInc, rowStart += sInc) { - rows += rInc; - int rowFilter = dis.read(); - dis.readFully(inbuf); - if (!filterRow(inbuf, pix, upix, rowFilter, filterOffset)) throw new IOException("Unknown filter type: " + rowFilter); - insertPixels(pix, rowStart + sCol, samples); - if (multipass && (pass < 6)) blockFill(rowStart); - upix = pix; - pix = temp; - temp = upix; - } - if (!multipass) break; - } - while(dis.read() != -1) System.err.println("Leftover data encountered."); - - // 24-bit color is our native format - if (colorType == 2 || colorType == 6) { - data = (int[])pixels; - if (colorType == 2) { - for(int i=0; i> depth) << (8 - depth); - data[i] = - (alpha << 24) | - (val << 16) | - (val << 8) | - val; - } - } - } - - } - - private void insertGreyPixels(int pix[], int offset, int samples) { - int p = pix[0]; - int ipix[] = ipixels; - int cInc = colInc[pass]; - int rs = 0; - - if (colorType == 0) { - switch (depth) { - case 1: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs--; - else { rs = 7; p = pix[j>>3]; } - ipix[offset] = (p>>rs) & 0x1; - } - break; - case 2: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs -= 2; - else { rs = 6; p = pix[j>>2]; } - ipix[offset] = (p>>rs) & 0x3; - } - break; - case 4: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs = 0; - else { rs = 4; p = pix[j>>1]; } - ipix[offset] = (p>>rs) & 0xf; - } - break; - case 8: - for (int j = 0; j < samples; offset += cInc) ipix[offset] = (byte) pix[j++]; - break; - case 16: - samples = samples<<1; - for (int j = 0; j < samples; j += 2, offset += cInc) ipix[offset] = pix[j]; - break; - default: break; - } - } else if (colorType == 4) { - if (depth == 8) { - for (int j = 0; j < samples; offset += cInc) ipix[offset] = (pix[j++]<<8) | pix[j++]; - } else { - samples = samples<<1; - for (int j = 0; j < samples; j += 2, offset += cInc) ipix[offset] = (pix[j]<<8) | pix[j+=2]; - } - } - } - - private void insertPalettedPixels(int pix[], int offset, int samples) { - int rs = 0; - int p = pix[0]; - byte bpix[] = bpixels; - int cInc = colInc[pass]; - - switch (depth) { - case 1: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs--; - else { rs = 7; p = pix[j>>3]; } - bpix[offset] = (byte) ((p>>rs) & 0x1); - } - break; - case 2: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs -= 2; - else { rs = 6; p = pix[j>>2]; } - bpix[offset] = (byte) ((p>>rs) & 0x3); - } - break; - case 4: - for (int j = 0; j < samples; j++, offset += cInc) { - if (rs != 0) rs = 0; - else { rs = 4; p = pix[j>>1]; } - bpix[offset] = (byte) ((p>>rs) & 0xf); - } - break; - case 8: - for (int j = 0; j < samples; j++, offset += cInc) bpix[offset] = (byte) pix[j]; - break; - } - } - - private void insertPixels(int pix[], int offset, int samples) { - switch (colorType) { - case 0: - case 4: - insertGreyPixels(pix, offset, samples); - break; - case 2: { - int j = 0; - int ipix[] = ipixels; - int cInc = colInc[pass]; - if (depth == 8) { - for (j = 0; j < samples; offset += cInc) - ipix[offset] = (pix[j++]<<16) | (pix[j++]<<8) | pix[j++]; - } else { - samples = samples<<1; - for (j = 0; j < samples; j += 2, offset += cInc) - ipix[offset] = (pix[j]<<16) | (pix[j+=2]<<8) | pix[j+=2]; - } - break; } - case 3: - insertPalettedPixels(pix, offset, samples); - break; - case 6: { - int j = 0; - int ipix[] = ipixels; - int cInc = colInc[pass]; - if (depth == 8) { - for (j = 0; j < samples; offset += cInc) { - ipix[offset] = (pix[j++]<<16) | (pix[j++]<<8) | pix[j++] | - (pix[j++]<<24); - } - } else { - samples = samples<<1; - for (j = 0; j < samples; j += 2, offset += cInc) { - ipix[offset] = (pix[j]<<16) | (pix[j+=2]<<8) | pix[j+=2] | - (pix[j+=2]<<24); - } - } - break; } - default: - break; - } - } - - private void blockFill(int rowStart) { - int counter; - int dw = width; - int pass = this.pass; - int w = blockWidth[pass]; - int sCol = startingCol[pass]; - int cInc = colInc[pass]; - int wInc = cInc - w; - int maxW = rowStart + dw - w; - int len; - int h = blockHeight[pass]; - int maxH = rowStart + (dw * h); - int startPos = rowStart + sCol; - counter = startPos; - - if (colorType == 3) { - byte bpix[] = bpixels; - byte pixel; - len = bpix.length; - for (; counter <= maxW;) { - int end = counter + w; - pixel = bpix[counter++]; - for (; counter < end; counter++) bpix[counter] = pixel; - counter += wInc; - } - maxW += w; - if (counter < maxW) - for (pixel = bpix[counter++]; counter < maxW; counter++) - bpix[counter] = pixel; - if (len < maxH) maxH = len; - for (counter = startPos + dw; counter < maxH; counter += dw) - System.arraycopy(bpix, startPos, bpix, counter, dw - sCol); - } else { - int ipix[] = ipixels; - int pixel; - len = ipix.length; - for (; counter <= maxW;) { - int end = counter + w; - pixel = ipix[counter++]; - for (; counter < end; counter++) - ipix[counter] = pixel; - counter += wInc; - } - maxW += w; - if (counter < maxW) - for (pixel = ipix[counter++]; counter < maxW; counter++) - ipix[counter] = pixel; - if (len < maxH) maxH = len; - for (counter = startPos + dw; counter < maxH; counter += dw) - System.arraycopy(ipix, startPos, ipix, counter, dw - sCol); - } - } - - private boolean filterRow(byte inbuf[], int pix[], int upix[], int rowFilter, int boff) { - int rowWidth = pix.length; - switch (rowFilter) { - case 0: { - for (int x = 0; x < rowWidth; x++) pix[x] = 0xff & inbuf[x]; - break; } - case 1: { - int x = 0; - for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x]; - for ( ; x < rowWidth; x++) pix[x] = 0xff & (inbuf[x] + pix[x - boff]); - break; } - case 2: { - if (upix != null) { - for (int x = 0; x < rowWidth; x++) - pix[x] = 0xff & (upix[x] + inbuf[x]); - } else { - for (int x = 0; x < rowWidth; x++) - pix[x] = 0xff & inbuf[x]; - } - break; } - case 3: { - if (upix != null) { - int x = 0; - for ( ; x < boff; x++) { - int rval = upix[x]; - pix[x] = 0xff & ((rval>>1) + inbuf[x]); - } - for ( ; x < rowWidth; x++) { - int rval = upix[x] + pix[x - boff]; - pix[x] = 0xff & ((rval>>1) + inbuf[x]); - } - } else { - int x = 0; - for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x]; - for ( ; x < rowWidth; x++) { - int rval = pix[x - boff]; - pix[x] = 0xff & ((rval>>1) + inbuf[x]); - } - } - break; } - case 4: { - if (upix != null) { - int x = 0; - for ( ; x < boff; x++) pix[x] = 0xff & (upix[x] + inbuf[x]); - for ( ; x < rowWidth; x++) { - int a, b, c, p, pa, pb, pc, rval; - a = pix[x - boff]; - b = upix[x]; - c = upix[x - boff]; - p = a + b - c; - pa = p > a ? p - a : a - p; - pb = p > b ? p - b : b - p; - pc = p > c ? p - c : c - p; - if ((pa <= pb) && (pa <= pc)) rval = a; - else if (pb <= pc) rval = b; - else rval = c; - pix[x] = 0xff & (rval + inbuf[x]); - } - } else { - int x = 0; - for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x]; - for ( ; x < rowWidth; x++) { - int rval = pix[x - boff]; - pix[x] = 0xff & (rval + inbuf[x]); - } - } - break; } - default: return false; - } - return true; - } - - // Private Data /////////////////////////////////////////////////////////////////////////////////////// - - private int target_offset = 0; - private int width = -1; - private int height = -1; - private int sigmask = 0xffff; - private Object pixels = null; - private int ipixels[] = null; - private byte bpixels[] = null; - private boolean multipass = false; - private boolean complete = false; - private boolean error = false; - - private int[] data = null; - - private InputStream underlyingStream = null; - private DataInputStream inputStream = null; - private Thread controlThread = null; - private boolean infoAvailable = false; - private int updateDelay = 750; - - // Image decoding state variables - private boolean headerFound = false; - private int compressionMethod = -1; - private int depth = -1; - private int colorType = -1; - private int filterMethod = -1; - private int interlaceMethod = -1; - private int pass = 0; - private byte palette[] = null; - private int mask = 0x0; - private int smask = 0x0; - private boolean transparency = false; - - private int chunkLength = 0; - private int chunkType = 0; - private boolean needChunkInfo = true; - - private static final int CHUNK_bKGD = 0x624B4744; // "bKGD" - private static final int CHUNK_cHRM = 0x6348524D; // "cHRM" - private static final int CHUNK_gAMA = 0x67414D41; // "gAMA" - private static final int CHUNK_hIST = 0x68495354; // "hIST" - private static final int CHUNK_IDAT = 0x49444154; // "IDAT" - private static final int CHUNK_IEND = 0x49454E44; // "IEND" - private static final int CHUNK_IHDR = 0x49484452; // "IHDR" - private static final int CHUNK_PLTE = 0x504C5445; // "PLTE" - private static final int CHUNK_pHYs = 0x70485973; // "pHYs" - private static final int CHUNK_sBIT = 0x73424954; // "sBIT" - private static final int CHUNK_tEXt = 0x74455874; // "tEXt" - private static final int CHUNK_tIME = 0x74494D45; // "tIME" - private static final int CHUNK_tRNS = 0x74524E53; // "tIME" - private static final int CHUNK_zTXt = 0x7A545874; // "zTXt" - - private static final int startingRow[] = { 0, 0, 0, 4, 0, 2, 0, 1 }; - private static final int startingCol[] = { 0, 0, 4, 0, 2, 0, 1, 0 }; - private static final int rowInc[] = { 1, 8, 8, 8, 4, 4, 2, 2 }; - private static final int colInc[] = { 1, 8, 8, 4, 4, 2, 2, 1 }; - private static final int blockHeight[] = { 1, 8, 8, 4, 4, 2, 2, 1 }; - private static final int blockWidth[] = { 1, 8, 4, 4, 2, 2, 1, 1 }; - - // Helper Classes //////////////////////////////////////////////////////////////////// - - private static class MeteredInputStream extends FilterInputStream { - int bytesLeft; - int marked; - - public MeteredInputStream(InputStream in, int size) { - super(in); - bytesLeft = size; - } - - public final int read() throws IOException { - if (bytesLeft > 0) { - int val = in.read(); - if (val != -1) bytesLeft--; - return val; - } - return -1; - } - - public final int read(byte b[]) throws IOException { - return read(b, 0, b.length); - } - - public final int read(byte b[], int off, int len) throws IOException { - if (bytesLeft > 0) { - len = (len > bytesLeft ? bytesLeft : len); - int read = in.read(b, off, len); - if (read > 0) bytesLeft -= read; - return read; - } - return -1; - } - - public final long skip(long n) throws IOException { - n = (n > bytesLeft ? bytesLeft : n); - long skipped = in.skip(n); - if (skipped > 0) bytesLeft -= skipped; - return skipped; - } - - public final int available() throws IOException { - int n = in.available(); - return (n > bytesLeft ? bytesLeft : n); - } - - public final void close() throws IOException { /* Eat this */ } - - public final void mark(int readlimit) { - marked = bytesLeft; - in.mark(readlimit); - } - - public final void reset() throws IOException { - in.reset(); - bytesLeft = marked; - } - - public final boolean markSupported() { return in.markSupported(); } - } - - /** Support class, used to eat the IDAT headers dividing up the deflated stream */ - private static class IDATEnumeration implements Enumeration { - InputStream underlyingStream; - PNG owner; - boolean firstStream = true; - - public IDATEnumeration(PNG owner) { - this.owner = owner; - this.underlyingStream = owner.underlyingStream; - } - - public Object nextElement() { - firstStream = false; - return new MeteredInputStream(underlyingStream, owner.chunkLength); - } - - public boolean hasMoreElements() { - DataInputStream dis = new DataInputStream(underlyingStream); - if (!firstStream) { - try { - int crc = dis.readInt(); - owner.needChunkInfo = false; - owner.chunkLength = dis.readInt(); - owner.chunkType = dis.readInt(); - } catch (IOException ioe) { - return false; - } - } - if (owner.chunkType == PNG.CHUNK_IDAT) return true; - return false; - } - } - -} -- 1.7.10.4