KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > PngDecodingDataStream


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 package org.eclipse.swt.internal.image;
12
13
14 import java.io.*;
15
16 import org.eclipse.swt.*;
17
18 public class PngDecodingDataStream extends InputStream {
19     InputStream stream;
20     byte currentByte;
21     int nextBitIndex;
22     
23     PngLzBlockReader lzBlockReader;
24     int adlerValue;
25     
26     static final int PRIME = 65521;
27     static final int MAX_BIT = 7;
28     
29 PngDecodingDataStream(InputStream stream) throws IOException {
30     super();
31     this.stream = stream;
32     nextBitIndex = MAX_BIT + 1;
33     adlerValue = 1;
34     lzBlockReader = new PngLzBlockReader(this);
35     readCompressedDataHeader();
36     lzBlockReader.readNextBlockHeader();
37 }
38
39 /**
40  * This method should be called when the image decoder thinks
41  * that all of the compressed image data has been read. This
42  * method will ensure that the next data value is an end of
43  * block marker. If there are more blocks after this one,
44  * the method will read them and ensure that they are empty.
45  */

46 void assertImageDataAtEnd() throws IOException {
47     lzBlockReader.assertCompressedDataAtEnd();
48 }
49
50 public void close() throws IOException {
51     assertImageDataAtEnd();
52     checkAdler();
53 }
54
55 int getNextIdatBits(int length) throws IOException {
56     int value = 0;
57     for (int i = 0; i < length; i++) {
58         value |= (getNextIdatBit() << i);
59     }
60     return value;
61 }
62
63 int getNextIdatBit() throws IOException {
64     if (nextBitIndex > MAX_BIT) {
65         currentByte = getNextIdatByte();
66         nextBitIndex = 0;
67     }
68     return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++;
69 }
70
71 byte getNextIdatByte() throws IOException {
72     byte nextByte = (byte)stream.read();
73     nextBitIndex = MAX_BIT + 1;
74     return nextByte;
75 }
76
77 void updateAdler(byte value) {
78     int low = adlerValue & 0xFFFF;
79     int high = (adlerValue >> 16) & 0xFFFF;
80     int valueInt = value & 0xFF;
81     low = (low + valueInt) % PRIME;
82     high = (low + high) % PRIME;
83     adlerValue = (high << 16) | low;
84 }
85
86 public int read() throws IOException {
87     byte nextDecodedByte = lzBlockReader.getNextByte();
88     updateAdler(nextDecodedByte);
89     return nextDecodedByte & 0xFF;
90 }
91
92 public int read(byte[] buffer, int off, int len) throws IOException {
93     for (int i = 0; i < len; i++) {
94         int b = read();
95         if (b == -1) return i;
96         buffer[off + i] = (byte)b;
97     }
98     return len;
99 }
100
101 void error() {
102     SWT.error(SWT.ERROR_INVALID_IMAGE);
103 }
104
105 private void readCompressedDataHeader() throws IOException {
106     byte headerByte1 = getNextIdatByte();
107     byte headerByte2 = getNextIdatByte();
108     
109     int number = ((headerByte1 & 0xFF) << 8) | (headerByte2 & 0xFF);
110     if (number % 31 != 0) error();
111     
112     int compressionMethod = headerByte1 & 0x0F;
113     if (compressionMethod != 8) error();
114     
115     int windowSizeHint = (headerByte1 & 0xF0) >> 4;
116     if (windowSizeHint > 7) error();
117     int windowSize = (1 << (windowSizeHint + 8));
118     lzBlockReader.setWindowSize(windowSize);
119     
120     int dictionary = (headerByte2 & (1 << 5));
121     if (dictionary != 0) error();
122     
123 // int compressionLevel = (headerByte2 & 0xC0) >> 6;
124
}
125
126 void checkAdler() throws IOException {
127     int storedAdler = ((getNextIdatByte() & 0xFF) << 24)
128         | ((getNextIdatByte() & 0xFF) << 16)
129         | ((getNextIdatByte() & 0xFF) << 8)
130         | (getNextIdatByte() & 0xFF);
131     if (storedAdler != adlerValue) error();
132 }
133
134 }
135
Popular Tags