001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal.image;
011:
012: import java.io.*;
013:
014: import org.eclipse.swt.*;
015:
016: public class PngDecodingDataStream extends InputStream {
017: InputStream stream;
018: byte currentByte;
019: int nextBitIndex;
020:
021: PngLzBlockReader lzBlockReader;
022: int adlerValue;
023:
024: static final int PRIME = 65521;
025: static final int MAX_BIT = 7;
026:
027: PngDecodingDataStream(InputStream stream) throws IOException {
028: super ();
029: this .stream = stream;
030: nextBitIndex = MAX_BIT + 1;
031: adlerValue = 1;
032: lzBlockReader = new PngLzBlockReader(this );
033: readCompressedDataHeader();
034: lzBlockReader.readNextBlockHeader();
035: }
036:
037: /**
038: * This method should be called when the image decoder thinks
039: * that all of the compressed image data has been read. This
040: * method will ensure that the next data value is an end of
041: * block marker. If there are more blocks after this one,
042: * the method will read them and ensure that they are empty.
043: */
044: void assertImageDataAtEnd() throws IOException {
045: lzBlockReader.assertCompressedDataAtEnd();
046: }
047:
048: public void close() throws IOException {
049: assertImageDataAtEnd();
050: checkAdler();
051: }
052:
053: int getNextIdatBits(int length) throws IOException {
054: int value = 0;
055: for (int i = 0; i < length; i++) {
056: value |= (getNextIdatBit() << i);
057: }
058: return value;
059: }
060:
061: int getNextIdatBit() throws IOException {
062: if (nextBitIndex > MAX_BIT) {
063: currentByte = getNextIdatByte();
064: nextBitIndex = 0;
065: }
066: return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++;
067: }
068:
069: byte getNextIdatByte() throws IOException {
070: byte nextByte = (byte) stream.read();
071: nextBitIndex = MAX_BIT + 1;
072: return nextByte;
073: }
074:
075: void updateAdler(byte value) {
076: int low = adlerValue & 0xFFFF;
077: int high = (adlerValue >> 16) & 0xFFFF;
078: int valueInt = value & 0xFF;
079: low = (low + valueInt) % PRIME;
080: high = (low + high) % PRIME;
081: adlerValue = (high << 16) | low;
082: }
083:
084: public int read() throws IOException {
085: byte nextDecodedByte = lzBlockReader.getNextByte();
086: updateAdler(nextDecodedByte);
087: return nextDecodedByte & 0xFF;
088: }
089:
090: public int read(byte[] buffer, int off, int len) throws IOException {
091: for (int i = 0; i < len; i++) {
092: int b = read();
093: if (b == -1)
094: return i;
095: buffer[off + i] = (byte) b;
096: }
097: return len;
098: }
099:
100: void error() {
101: SWT.error(SWT.ERROR_INVALID_IMAGE);
102: }
103:
104: private void readCompressedDataHeader() throws IOException {
105: byte headerByte1 = getNextIdatByte();
106: byte headerByte2 = getNextIdatByte();
107:
108: int number = ((headerByte1 & 0xFF) << 8) | (headerByte2 & 0xFF);
109: if (number % 31 != 0)
110: error();
111:
112: int compressionMethod = headerByte1 & 0x0F;
113: if (compressionMethod != 8)
114: error();
115:
116: int windowSizeHint = (headerByte1 & 0xF0) >> 4;
117: if (windowSizeHint > 7)
118: error();
119: int windowSize = (1 << (windowSizeHint + 8));
120: lzBlockReader.setWindowSize(windowSize);
121:
122: int dictionary = (headerByte2 & (1 << 5));
123: if (dictionary != 0)
124: error();
125:
126: // int compressionLevel = (headerByte2 & 0xC0) >> 6;
127: }
128:
129: void checkAdler() throws IOException {
130: int storedAdler = ((getNextIdatByte() & 0xFF) << 24)
131: | ((getNextIdatByte() & 0xFF) << 16)
132: | ((getNextIdatByte() & 0xFF) << 8)
133: | (getNextIdatByte() & 0xFF);
134: if (storedAdler != adlerValue)
135: error();
136: }
137:
138: }
|