2 * This file was adapted from Jason Marshall's PNGImageProducer.java
4 * Copyright (c) 1997, Jason Marshall. All Rights Reserved
6 * The author makes no representations or warranties regarding the suitability,
7 * reliability or stability of this code. This code is provided AS IS. The
8 * author shall not be liable for any damages suffered as a result of using,
9 * modifying or redistributing this software or any derivitives thereof.
10 * Permission to use, reproduce, modify and/or (re)distribute this software is
14 package org.xwt.translators;
17 import org.xwt.util.*;
19 import java.util.Hashtable;
20 import java.util.Vector;
21 import java.util.Enumeration;
22 import java.util.zip.*;
24 /** Converts an InputStream carrying a PNG image into an ARGB int[] */
29 private static Queue instances = new Queue(10);
31 public static void load(InputStream is, Picture p) {
32 PNG g = (PNG)instances.remove(false);
33 if (g == null) g = new PNG();
37 } catch (Exception e) {
38 if (Log.on) Log.log(PNG.class, e);
41 // FIXME: must reset fields
42 // if (instances.size() < 10) instances.append(g);
45 // Public Methods ///////////////////////////////////////////////////////////////////////////////
49 /** process a PNG as an inputstream; returns null if there is an error
50 @param name A string describing the image, to be used when logging errors
52 private void _load(InputStream is, Picture p) throws IOException {
54 if (is instanceof BufferedInputStream) underlyingStream = (BufferedInputStream)is;
55 else underlyingStream = new BufferedInputStream(is);
57 inputStream = new DataInputStream(underlyingStream);
60 if ((inputStream.read() != 137) || (inputStream.read() != 80) || (inputStream.read() != 78) || (inputStream.read() != 71) ||
61 (inputStream.read() != 13) || (inputStream.read() != 10) || (inputStream.read() != 26) || (inputStream.read() != 10)) {
62 Log.log(this, "PNG: error: input file is not a PNG file");
63 data = p.data = new int[] { };
64 p.width = p.height = 0;
68 DONE: while (!error) {
70 chunkLength = inputStream.readInt();
71 chunkType = inputStream.readInt();
72 needChunkInfo = false;
76 case CHUNK_bKGD: inputStream.skip(chunkLength); break;
77 case CHUNK_cHRM: inputStream.skip(chunkLength); break;
78 case CHUNK_gAMA: inputStream.skip(chunkLength); break;
79 case CHUNK_hIST: inputStream.skip(chunkLength); break;
80 case CHUNK_pHYs: inputStream.skip(chunkLength); break;
81 case CHUNK_sBIT: inputStream.skip(chunkLength); break;
82 case CHUNK_tEXt: inputStream.skip(chunkLength); break;
83 case CHUNK_zTXt: inputStream.skip(chunkLength); break;
84 case CHUNK_tIME: inputStream.skip(chunkLength); break;
86 case CHUNK_IHDR: handleIHDR(); break;
87 case CHUNK_PLTE: handlePLTE(); break;
88 case CHUNK_tRNS: handletRNS(); break;
90 case CHUNK_IDAT: handleIDAT(); break;
92 case CHUNK_IEND: break DONE;
94 System.err.println("unrecognized chunk type " +
95 Integer.toHexString(chunkType) + ". skipping");
96 inputStream.skip(chunkLength);
99 int crc = inputStream.readInt();
100 needChunkInfo = true;
105 // Chunk Handlers ///////////////////////////////////////////////////////////////////////
107 /** handle data chunk */
108 private void handleIDAT() throws IOException {
109 if (p.width == -1 || p.height == -1) throw new IOException("never got image width/height");
111 case 1: mask = 0x1; break;
112 case 2: mask = 0x3; break;
113 case 4: mask = 0xf; break;
114 case 8: case 16: mask = 0xff; break;
115 default: mask = 0x0; break;
117 if (depth < 8) smask = mask << depth;
118 else smask = mask << 8;
120 int count = p.width * p.height;
127 ipixels = new int[count];
131 bpixels = new byte[count];
135 throw new IOException("Image has unknown color type");
137 if (interlaceMethod != 0) multipass = true;
141 /** handle header chunk */
142 private void handleIHDR() throws IOException {
143 if (headerFound) throw new IOException("Extraneous IHDR chunk encountered.");
144 if (chunkLength != 13) throw new IOException("IHDR chunk length wrong: " + chunkLength);
145 p.width = inputStream.readInt();
146 p.height = inputStream.readInt();
147 depth = inputStream.read();
148 colorType = inputStream.read();
149 compressionMethod = inputStream.read();
150 filterMethod = inputStream.read();
151 interlaceMethod = inputStream.read();
154 /** handle pallette chunk */
155 private void handlePLTE() throws IOException {
156 if (colorType == 3) {
157 palette = new byte[chunkLength];
158 inputStream.readFully(palette);
160 // Ignore suggested palette
161 inputStream.skip(chunkLength);
165 /** handle transparency chunk; modifies palette */
166 private void handletRNS() throws IOException {
167 int chunkLen = chunkLength;
168 if (palette == null) {
169 if (Log.on) Log.log(this, "warning: tRNS chunk encountered before pLTE; ignoring alpha channel");
170 inputStream.skip(chunkLength);
173 int len = palette.length;
174 if (colorType == 3) {
177 int transLength = len/3;
178 byte[] trans = new byte[transLength];
179 for (int i = 0; i < transLength; i++) trans[i] = (byte) 0xff;
180 inputStream.readFully(trans, 0, chunkLength);
182 byte[] newPalette = new byte[len + transLength];
183 for (int i = newPalette.length; i > 0;) {
184 newPalette[--i] = trans[--transLength];
185 newPalette[--i] = palette[--len];
186 newPalette[--i] = palette[--len];
187 newPalette[--i] = palette[--len];
189 palette = newPalette;
192 inputStream.skip(chunkLength);
196 /// Helper functions for IDAT ///////////////////////////////////////////////////////////////////////////////////////////
198 /** Read Image data in off of a compression stream */
199 private void readImageData() throws IOException {
200 InputStream dataStream = new SequenceInputStream(new IDATEnumeration(this));
201 DataInputStream dis = new DataInputStream(new BufferedInputStream(new InflaterInputStream(dataStream, new Inflater())));
202 int bps, filterOffset;
204 case 0: case 3: bps = depth; break;
205 case 2: bps = 3 * depth; break;
206 case 4: bps = depth<<1; break;
207 case 6: bps = depth<<2; break;
208 default: throw new IOException("Unknown color type encountered.");
211 filterOffset = (bps + 7) >> 3;
213 for (pass = (multipass ? 1 : 0); pass < 8; pass++) {
214 int pass = this.pass;
215 int rInc = rowInc[pass];
216 int cInc = colInc[pass];
217 int sCol = startingCol[pass];
218 int val = (p.width - sCol + cInc - 1) / cInc;
219 int samples = val * filterOffset;
220 int rowSize = (val * bps)>>3;
221 int sRow = startingRow[pass];
222 if (p.height <= sRow || rowSize == 0) continue;
223 int sInc = rInc * p.width;
224 byte inbuf[] = new byte[rowSize];
225 int pix[] = new int[rowSize];
227 int temp[] = new int[rowSize];
228 int nextY = sRow; // next Y value and number of rows to report to sendPixels
230 int rowStart = sRow * p.width;
232 for (int y = sRow; y < p.height; y += rInc, rowStart += sInc) {
234 int rowFilter = dis.read();
235 dis.readFully(inbuf);
236 if (!filterRow(inbuf, pix, upix, rowFilter, filterOffset)) throw new IOException("Unknown filter type: " + rowFilter);
237 insertPixels(pix, rowStart + sCol, samples);
238 if (multipass && (pass < 6)) blockFill(rowStart);
243 if (!multipass) break;
245 while(dis.read() != -1) System.err.println("Leftover data encountered.");
247 // 24-bit color is our native format
248 if (colorType == 2 || colorType == 6) {
249 data = (int[])pixels;
250 if (colorType == 2) {
251 for(int i=0; i<data.length; i++)
252 data[i] |= 0xFF000000;
255 } else if (colorType == 3) {
256 byte[] pix = (byte[])pixels;
257 data = new int[pix.length];
258 for(int i=0; i<pix.length; i++) {
261 ((palette[4 * (pix[i] & 0xff) + 3] & 0xff) << 24) |
262 ((palette[4 * (pix[i] & 0xff) + 0] & 0xff) << 16) |
263 ((palette[4 * (pix[i] & 0xff) + 1] & 0xff) << 8) |
264 (palette[4 * (pix[i] & 0xff) + 2] & 0xff);
268 ((palette[3 * (pix[i] & 0xff) + 0] & 0xff) << 16) |
269 ((palette[3 * (pix[i] & 0xff) + 1] & 0xff) << 8) |
270 (palette[3 * (pix[i] & 0xff) + 2] & 0xff);
274 } else if (colorType == 0 || colorType == 4) {
275 if (depth == 16) depth = 8;
276 int[] pix = (int[])pixels;
277 data = new int[pix.length];
278 for(int i=0; i<pix.length; i ++) {
279 if (colorType == 0) {
280 int val = (pix[i] & 0xff) << (8 - depth);
287 int alpha = (pix[i] & mask) << (8 - depth);
288 int val = ((pix[i] & smask) >> depth) << (8 - depth);
300 private void insertGreyPixels(int pix[], int offset, int samples) {
302 int ipix[] = ipixels;
303 int cInc = colInc[pass];
306 if (colorType == 0) {
309 for (int j = 0; j < samples; j++, offset += cInc) {
311 else { rs = 7; p = pix[j>>3]; }
312 ipix[offset] = (p>>rs) & 0x1;
316 for (int j = 0; j < samples; j++, offset += cInc) {
317 if (rs != 0) rs -= 2;
318 else { rs = 6; p = pix[j>>2]; }
319 ipix[offset] = (p>>rs) & 0x3;
323 for (int j = 0; j < samples; j++, offset += cInc) {
325 else { rs = 4; p = pix[j>>1]; }
326 ipix[offset] = (p>>rs) & 0xf;
330 for (int j = 0; j < samples; offset += cInc) ipix[offset] = (byte) pix[j++];
333 samples = samples<<1;
334 for (int j = 0; j < samples; j += 2, offset += cInc) ipix[offset] = pix[j];
338 } else if (colorType == 4) {
340 for (int j = 0; j < samples; offset += cInc) ipix[offset] = (pix[j++]<<8) | pix[j++];
342 samples = samples<<1;
343 for (int j = 0; j < samples; j += 2, offset += cInc) ipix[offset] = (pix[j]<<8) | pix[j+=2];
348 private void insertPalettedPixels(int pix[], int offset, int samples) {
351 byte bpix[] = bpixels;
352 int cInc = colInc[pass];
356 for (int j = 0; j < samples; j++, offset += cInc) {
358 else { rs = 7; p = pix[j>>3]; }
359 bpix[offset] = (byte) ((p>>rs) & 0x1);
363 for (int j = 0; j < samples; j++, offset += cInc) {
364 if (rs != 0) rs -= 2;
365 else { rs = 6; p = pix[j>>2]; }
366 bpix[offset] = (byte) ((p>>rs) & 0x3);
370 for (int j = 0; j < samples; j++, offset += cInc) {
372 else { rs = 4; p = pix[j>>1]; }
373 bpix[offset] = (byte) ((p>>rs) & 0xf);
377 for (int j = 0; j < samples; j++, offset += cInc) bpix[offset] = (byte) pix[j];
382 private void insertPixels(int pix[], int offset, int samples) {
386 insertGreyPixels(pix, offset, samples);
390 int ipix[] = ipixels;
391 int cInc = colInc[pass];
393 for (j = 0; j < samples; offset += cInc)
394 ipix[offset] = (pix[j++]<<16) | (pix[j++]<<8) | pix[j++];
396 samples = samples<<1;
397 for (j = 0; j < samples; j += 2, offset += cInc)
398 ipix[offset] = (pix[j]<<16) | (pix[j+=2]<<8) | pix[j+=2];
402 insertPalettedPixels(pix, offset, samples);
406 int ipix[] = ipixels;
407 int cInc = colInc[pass];
409 for (j = 0; j < samples; offset += cInc) {
410 ipix[offset] = (pix[j++]<<16) | (pix[j++]<<8) | pix[j++] |
414 samples = samples<<1;
415 for (j = 0; j < samples; j += 2, offset += cInc) {
416 ipix[offset] = (pix[j]<<16) | (pix[j+=2]<<8) | pix[j+=2] |
426 private void blockFill(int rowStart) {
429 int pass = this.pass;
430 int w = blockWidth[pass];
431 int sCol = startingCol[pass];
432 int cInc = colInc[pass];
434 int maxW = rowStart + dw - w;
436 int h = blockHeight[pass];
437 int maxH = rowStart + (dw * h);
438 int startPos = rowStart + sCol;
441 if (colorType == 3) {
442 byte bpix[] = bpixels;
445 for (; counter <= maxW;) {
446 int end = counter + w;
447 pixel = bpix[counter++];
448 for (; counter < end; counter++) bpix[counter] = pixel;
453 for (pixel = bpix[counter++]; counter < maxW; counter++)
454 bpix[counter] = pixel;
455 if (len < maxH) maxH = len;
456 for (counter = startPos + dw; counter < maxH; counter += dw)
457 System.arraycopy(bpix, startPos, bpix, counter, dw - sCol);
459 int ipix[] = ipixels;
462 for (; counter <= maxW;) {
463 int end = counter + w;
464 pixel = ipix[counter++];
465 for (; counter < end; counter++)
466 ipix[counter] = pixel;
471 for (pixel = ipix[counter++]; counter < maxW; counter++)
472 ipix[counter] = pixel;
473 if (len < maxH) maxH = len;
474 for (counter = startPos + dw; counter < maxH; counter += dw)
475 System.arraycopy(ipix, startPos, ipix, counter, dw - sCol);
479 private boolean filterRow(byte inbuf[], int pix[], int upix[], int rowFilter, int boff) {
480 int rowWidth = pix.length;
483 for (int x = 0; x < rowWidth; x++) pix[x] = 0xff & inbuf[x];
487 for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x];
488 for ( ; x < rowWidth; x++) pix[x] = 0xff & (inbuf[x] + pix[x - boff]);
492 for (int x = 0; x < rowWidth; x++)
493 pix[x] = 0xff & (upix[x] + inbuf[x]);
495 for (int x = 0; x < rowWidth; x++)
496 pix[x] = 0xff & inbuf[x];
502 for ( ; x < boff; x++) {
504 pix[x] = 0xff & ((rval>>1) + inbuf[x]);
506 for ( ; x < rowWidth; x++) {
507 int rval = upix[x] + pix[x - boff];
508 pix[x] = 0xff & ((rval>>1) + inbuf[x]);
512 for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x];
513 for ( ; x < rowWidth; x++) {
514 int rval = pix[x - boff];
515 pix[x] = 0xff & ((rval>>1) + inbuf[x]);
522 for ( ; x < boff; x++) pix[x] = 0xff & (upix[x] + inbuf[x]);
523 for ( ; x < rowWidth; x++) {
524 int a, b, c, p, pa, pb, pc, rval;
529 pa = p > a ? p - a : a - p;
530 pb = p > b ? p - b : b - p;
531 pc = p > c ? p - c : c - p;
532 if ((pa <= pb) && (pa <= pc)) rval = a;
533 else if (pb <= pc) rval = b;
535 pix[x] = 0xff & (rval + inbuf[x]);
539 for ( ; x < boff; x++) pix[x] = 0xff & inbuf[x];
540 for ( ; x < rowWidth; x++) {
541 int rval = pix[x - boff];
542 pix[x] = 0xff & (rval + inbuf[x]);
546 default: return false;
551 // Private Data ///////////////////////////////////////////////////////////////////////////////////////
553 private int target_offset = 0;
554 private int sigmask = 0xffff;
555 private Object pixels = null;
556 private int ipixels[] = null;
557 private byte bpixels[] = null;
558 private boolean multipass = false;
559 private boolean complete = false;
560 private boolean error = false;
564 private InputStream underlyingStream = null;
565 private DataInputStream inputStream = null;
566 private Thread controlThread = null;
567 private boolean infoAvailable = false;
568 private int updateDelay = 750;
570 // Image decoding state variables
571 private boolean headerFound = false;
572 private int compressionMethod = -1;
573 private int depth = -1;
574 private int colorType = -1;
575 private int filterMethod = -1;
576 private int interlaceMethod = -1;
577 private int pass = 0;
578 private byte palette[] = null;
579 private int mask = 0x0;
580 private int smask = 0x0;
581 private boolean transparency = false;
583 private int chunkLength = 0;
584 private int chunkType = 0;
585 private boolean needChunkInfo = true;
587 private static final int CHUNK_bKGD = 0x624B4744; // "bKGD"
588 private static final int CHUNK_cHRM = 0x6348524D; // "cHRM"
589 private static final int CHUNK_gAMA = 0x67414D41; // "gAMA"
590 private static final int CHUNK_hIST = 0x68495354; // "hIST"
591 private static final int CHUNK_IDAT = 0x49444154; // "IDAT"
592 private static final int CHUNK_IEND = 0x49454E44; // "IEND"
593 private static final int CHUNK_IHDR = 0x49484452; // "IHDR"
594 private static final int CHUNK_PLTE = 0x504C5445; // "PLTE"
595 private static final int CHUNK_pHYs = 0x70485973; // "pHYs"
596 private static final int CHUNK_sBIT = 0x73424954; // "sBIT"
597 private static final int CHUNK_tEXt = 0x74455874; // "tEXt"
598 private static final int CHUNK_tIME = 0x74494D45; // "tIME"
599 private static final int CHUNK_tRNS = 0x74524E53; // "tIME"
600 private static final int CHUNK_zTXt = 0x7A545874; // "zTXt"
602 private static final int startingRow[] = { 0, 0, 0, 4, 0, 2, 0, 1 };
603 private static final int startingCol[] = { 0, 0, 4, 0, 2, 0, 1, 0 };
604 private static final int rowInc[] = { 1, 8, 8, 8, 4, 4, 2, 2 };
605 private static final int colInc[] = { 1, 8, 8, 4, 4, 2, 2, 1 };
606 private static final int blockHeight[] = { 1, 8, 8, 4, 4, 2, 2, 1 };
607 private static final int blockWidth[] = { 1, 8, 4, 4, 2, 2, 1, 1 };
609 // Helper Classes ////////////////////////////////////////////////////////////////////
611 private static class MeteredInputStream extends FilterInputStream {
615 public MeteredInputStream(InputStream in, int size) {
620 public final int read() throws IOException {
623 if (val != -1) bytesLeft--;
629 public final int read(byte b[]) throws IOException {
630 return read(b, 0, b.length);
633 public final int read(byte b[], int off, int len) throws IOException {
635 len = (len > bytesLeft ? bytesLeft : len);
636 int read = in.read(b, off, len);
637 if (read > 0) bytesLeft -= read;
643 public final long skip(long n) throws IOException {
644 n = (n > bytesLeft ? bytesLeft : n);
645 long skipped = in.skip(n);
646 if (skipped > 0) bytesLeft -= skipped;
650 public final int available() throws IOException {
651 int n = in.available();
652 return (n > bytesLeft ? bytesLeft : n);
655 public final void close() throws IOException { /* Eat this */ }
657 public final void mark(int readlimit) {
662 public final void reset() throws IOException {
667 public final boolean markSupported() { return in.markSupported(); }
670 /** Support class, used to eat the IDAT headers dividing up the deflated stream */
671 private static class IDATEnumeration implements Enumeration {
672 InputStream underlyingStream;
674 boolean firstStream = true;
676 public IDATEnumeration(PNG owner) {
678 this.underlyingStream = owner.underlyingStream;
681 public Object nextElement() {
683 return new MeteredInputStream(underlyingStream, owner.chunkLength);
686 public boolean hasMoreElements() {
687 DataInputStream dis = new DataInputStream(underlyingStream);
690 int crc = dis.readInt();
691 owner.needChunkInfo = false;
692 owner.chunkLength = dis.readInt();
693 owner.chunkType = dis.readInt();
694 } catch (IOException ioe) {
698 if (owner.chunkType == PNG.CHUNK_IDAT) return true;