Mercurial > projects > dwt-mac
comparison dwt/internal/image/PngDecodingDataStream.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | e831403a80a9 |
children |
comparison
equal
deleted
inserted
replaced
33:965ac0a77267 | 34:5123b17c98ef |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.internal.image; | 13 module dwt.internal.image.PngDecodingDataStream; |
12 | 14 |
13 | 15 |
14 import java.io.IOException; | 16 import dwt.dwthelper.InputStream; |
15 import java.io.InputStream; | |
16 | 17 |
17 import dwt.DWT; | 18 import dwt.DWT; |
19 import dwt.internal.image.PngLzBlockReader; | |
18 | 20 |
19 public class PngDecodingDataStream : InputStream { | 21 public class PngDecodingDataStream : InputStream { |
22 | |
23 alias InputStream.read read; | |
24 | |
20 InputStream stream; | 25 InputStream stream; |
21 byte currentByte; | 26 byte currentByte; |
22 int nextBitIndex; | 27 int nextBitIndex; |
23 | 28 |
24 PngLzBlockReader lzBlockReader; | 29 PngLzBlockReader lzBlockReader; |
25 int adlerValue; | 30 int adlerValue; |
26 | 31 |
27 static final int PRIME = 65521; | 32 static final int PRIME = 65521; |
28 static final int MAX_BIT = 7; | 33 static final int MAX_BIT = 7; |
29 | 34 |
30 this(InputStream stream) { | 35 this(InputStream stream) { |
31 super(); | 36 super(); |
32 this.stream = stream; | 37 this.stream = stream; |
33 nextBitIndex = MAX_BIT + 1; | 38 nextBitIndex = MAX_BIT + 1; |
34 adlerValue = 1; | 39 adlerValue = 1; |
38 } | 43 } |
39 | 44 |
40 /** | 45 /** |
41 * This method should be called when the image decoder thinks | 46 * This method should be called when the image decoder thinks |
42 * that all of the compressed image data has been read. This | 47 * that all of the compressed image data has been read. This |
43 * method will ensure that the next data value is an end of | 48 * method will ensure that the next data value is an end of |
44 * block marker. If there are more blocks after this one, | 49 * block marker. If there are more blocks after this one, |
45 * the method will read them and ensure that they are empty. | 50 * the method will read them and ensure that they are empty. |
46 */ | 51 */ |
47 void assertImageDataAtEnd() { | 52 void assertImageDataAtEnd() { |
48 lzBlockReader.assertCompressedDataAtEnd(); | 53 lzBlockReader.assertCompressedDataAtEnd(); |
49 } | 54 } |
50 | 55 |
51 public void close() { | 56 public override void close() { |
52 assertImageDataAtEnd(); | 57 assertImageDataAtEnd(); |
53 checkAdler(); | 58 checkAdler(); |
54 } | 59 } |
55 | 60 |
56 int getNextIdatBits(int length) { | 61 int getNextIdatBits(int length) { |
67 nextBitIndex = 0; | 72 nextBitIndex = 0; |
68 } | 73 } |
69 return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++; | 74 return (currentByte & (1 << nextBitIndex)) >> nextBitIndex++; |
70 } | 75 } |
71 | 76 |
72 byte getNextIdatByte() { | 77 byte getNextIdatByte() { |
73 byte nextByte = cast(byte)stream.read(); | 78 byte nextByte = cast(byte)stream.read(); |
74 nextBitIndex = MAX_BIT + 1; | 79 nextBitIndex = MAX_BIT + 1; |
75 return nextByte; | 80 return nextByte; |
76 } | 81 } |
77 | 82 |
82 low = (low + valueInt) % PRIME; | 87 low = (low + valueInt) % PRIME; |
83 high = (low + high) % PRIME; | 88 high = (low + high) % PRIME; |
84 adlerValue = (high << 16) | low; | 89 adlerValue = (high << 16) | low; |
85 } | 90 } |
86 | 91 |
87 public int read() { | 92 public override int read() { |
88 byte nextDecodedByte = lzBlockReader.getNextByte(); | 93 byte nextDecodedByte = lzBlockReader.getNextByte(); |
89 updateAdler(nextDecodedByte); | 94 updateAdler(nextDecodedByte); |
90 return nextDecodedByte & 0xFF; | 95 return nextDecodedByte & 0xFF; |
91 } | 96 } |
92 | 97 |
93 public int read(byte[] buffer, int off, int len) { | 98 public override int read(byte[] buffer, int off, int len) { |
94 for (int i = 0; i < len; i++) { | 99 for (int i = 0; i < len; i++) { |
95 int b = read(); | 100 int b = read(); |
96 if (b is -1) return i; | 101 if (b is -1) return i; |
97 buffer[off + i] = cast(byte)b; | 102 buffer[off + i] = cast(byte)b; |
98 } | 103 } |
104 } | 109 } |
105 | 110 |
106 private void readCompressedDataHeader() { | 111 private void readCompressedDataHeader() { |
107 byte headerByte1 = getNextIdatByte(); | 112 byte headerByte1 = getNextIdatByte(); |
108 byte headerByte2 = getNextIdatByte(); | 113 byte headerByte2 = getNextIdatByte(); |
109 | 114 |
110 int number = ((headerByte1 & 0xFF) << 8) | (headerByte2 & 0xFF); | 115 int number = ((headerByte1 & 0xFF) << 8) | (headerByte2 & 0xFF); |
111 if (number % 31 !is 0) error(); | 116 if (number % 31 !is 0) error(); |
112 | 117 |
113 int compressionMethod = headerByte1 & 0x0F; | 118 int compressionMethod = headerByte1 & 0x0F; |
114 if (compressionMethod !is 8) error(); | 119 if (compressionMethod !is 8) error(); |
115 | 120 |
116 int windowSizeHint = (headerByte1 & 0xF0) >> 4; | 121 int windowSizeHint = (headerByte1 & 0xF0) >> 4; |
117 if (windowSizeHint > 7) error(); | 122 if (windowSizeHint > 7) error(); |
118 int windowSize = (1 << (windowSizeHint + 8)); | 123 int windowSize = (1 << (windowSizeHint + 8)); |
119 lzBlockReader.setWindowSize(windowSize); | 124 lzBlockReader.setWindowSize(windowSize); |
120 | 125 |
121 int dictionary = (headerByte2 & (1 << 5)); | 126 int dictionary = (headerByte2 & (1 << 5)); |
122 if (dictionary !is 0) error(); | 127 if (dictionary !is 0) error(); |
123 | 128 |
124 // int compressionLevel = (headerByte2 & 0xC0) >> 6; | 129 // int compressionLevel = (headerByte2 & 0xC0) >> 6; |
125 } | 130 } |
126 | 131 |
127 void checkAdler() { | 132 void checkAdler() { |
128 int storedAdler = ((getNextIdatByte() & 0xFF) << 24) | 133 int storedAdler = 0; |
129 | ((getNextIdatByte() & 0xFF) << 16) | 134 storedAdler |= ((getNextIdatByte() & 0xFF) << 24); |
130 | ((getNextIdatByte() & 0xFF) << 8) | 135 storedAdler |= ((getNextIdatByte() & 0xFF) << 16); |
131 | (getNextIdatByte() & 0xFF); | 136 storedAdler |= ((getNextIdatByte() & 0xFF) << 8); |
137 storedAdler |= (getNextIdatByte() & 0xFF); | |
132 if (storedAdler !is adlerValue) error(); | 138 if (storedAdler !is adlerValue) error(); |
133 } | 139 } |
134 | 140 |
135 } | 141 } |