comparison dwt/internal/image/PngDecodingDataStream.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.internal.image;
12
13
14 import java.io.IOException;
15 import java.io.InputStream;
16
17 import dwt.DWT;
18
19 public class PngDecodingDataStream : InputStream {
20 InputStream stream;
21 byte currentByte;
22 int nextBitIndex;
23
24 PngLzBlockReader lzBlockReader;
25 int adlerValue;
26
27 static final int PRIME = 65521;
28 static final int MAX_BIT = 7;
29
30 PngDecodingDataStream(InputStream stream) throws IOException {
31 super();
32 this.stream = stream;
33 nextBitIndex = MAX_BIT + 1;
34 adlerValue = 1;
35 lzBlockReader = new PngLzBlockReader(this);
36 readCompressedDataHeader();
37 lzBlockReader.readNextBlockHeader();
38 }
39
40 /**
41 * This method should be called when the image decoder thinks
42 * that all of the compressed image data has been read. This
43 * method will ensure that the next data value is an end of
44 * block marker. If there are more blocks after this one,
45 * the method will read them and ensure that they are empty.
46 */
47 void assertImageDataAtEnd() throws IOException {
48 lzBlockReader.assertCompressedDataAtEnd();
49 }
50
51 public void close() throws IOException {
52 assertImageDataAtEnd();
53 checkAdler();
54 }
55
56 int getNextIdatBits(int length) throws IOException {
57 int value = 0;
58 for (int i = 0; i < length; i++) {
59 value |= (getNextIdatBit() << i);
60 }
61 return value;
62 }
63
64 int getNextIdatBit() throws IOException {
65 if (nextBitIndex > MAX_BIT) {
66 currentByte = getNextIdatByte();
67 nextBitIndex = 0;
68 }
69 return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++;
70 }
71
72 byte getNextIdatByte() throws IOException {
73 byte nextByte = (byte)stream.read();
74 nextBitIndex = MAX_BIT + 1;
75 return nextByte;
76 }
77
78 void updateAdler(byte value) {
79 int low = adlerValue & 0xFFFF;
80 int high = (adlerValue >> 16) & 0xFFFF;
81 int valueInt = value & 0xFF;
82 low = (low + valueInt) % PRIME;
83 high = (low + high) % PRIME;
84 adlerValue = (high << 16) | low;
85 }
86
87 public int read() throws IOException {
88 byte nextDecodedByte = lzBlockReader.getNextByte();
89 updateAdler(nextDecodedByte);
90 return nextDecodedByte & 0xFF;
91 }
92
93 public int read(byte[] buffer, int off, int len) throws IOException {
94 for (int i = 0; i < len; i++) {
95 int b = read();
96 if (b is -1) return i;
97 buffer[off + i] = (byte)b;
98 }
99 return len;
100 }
101
102 void error() {
103 DWT.error(DWT.ERROR_INVALID_IMAGE);
104 }
105
106 private void readCompressedDataHeader() throws IOException {
107 byte headerByte1 = getNextIdatByte();
108 byte headerByte2 = getNextIdatByte();
109
110 int number = ((headerByte1 & 0xFF) << 8) | (headerByte2 & 0xFF);
111 if (number % 31 !is 0) error();
112
113 int compressionMethod = headerByte1 & 0x0F;
114 if (compressionMethod !is 8) error();
115
116 int windowSizeHint = (headerByte1 & 0xF0) >> 4;
117 if (windowSizeHint > 7) error();
118 int windowSize = (1 << (windowSizeHint + 8));
119 lzBlockReader.setWindowSize(windowSize);
120
121 int dictionary = (headerByte2 & (1 << 5));
122 if (dictionary !is 0) error();
123
124 // int compressionLevel = (headerByte2 & 0xC0) >> 6;
125 }
126
127 void checkAdler() throws IOException {
128 int storedAdler = ((getNextIdatByte() & 0xFF) << 24)
129 | ((getNextIdatByte() & 0xFF) << 16)
130 | ((getNextIdatByte() & 0xFF) << 8)
131 | (getNextIdatByte() & 0xFF);
132 if (storedAdler !is adlerValue) error();
133 }
134
135 }