comparison dwt/internal/image/JPEGHuffmanTable.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.JPEGHuffmanTable;
12 14
13 15 import dwt.internal.image.JPEGVariableSizeSegment;
16 import dwt.internal.image.LEDataInputStream;
17 import dwt.internal.image.JPEGFileFormat;
18
19 import dwt.dwthelper.System;
14 /** 20 /**
15 * JPEGHuffmanTable class actually represents two types of object: 21 * JPEGHuffmanTable class actually represents two types of object:
16 * 1) A DHT (Define Huffman Tables) segment, which may represent 22 * 1) A DHT (Define Huffman Tables) segment, which may represent
17 * as many as 4 Huffman tables. In this case, the tables are 23 * as many as 4 Huffman tables. In this case, the tables are
18 * stored in the allTables array. 24 * stored in the allTables array.
29 int[] dhMinCodes; 35 int[] dhMinCodes;
30 int[] dhValPtrs; 36 int[] dhValPtrs;
31 int[] dhValues; 37 int[] dhValues;
32 int[] ehCodes; 38 int[] ehCodes;
33 byte[] ehCodeLengths; 39 byte[] ehCodeLengths;
34 static byte[] DCLuminanceTable = { 40 static byte[] DCLuminanceTable = [
35 cast(byte)255, cast(byte)196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 41 cast(byte)255, cast(byte)196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
36 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 42 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
37 }; 43 ];
38 static byte[] DCChrominanceTable = { 44 static byte[] DCChrominanceTable = [
39 cast(byte)255, cast(byte)196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 45 cast(byte)255, cast(byte)196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
40 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 46 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
41 }; 47 ];
42 static byte[] ACLuminanceTable = { 48 static byte[] ACLuminanceTable = [
43 cast(byte)255, cast(byte)196, 0, cast(byte)181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 49 cast(byte)255, cast(byte)196, 0, cast(byte)181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125,
44 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20,
45 50, cast(byte)129, cast(byte)145, cast(byte)161, 8, 35, 66, cast(byte)177, cast(byte)193, 21, 82, cast(byte)209, cast(byte)240, 36, 51, 98, 51 50, cast(byte)129, cast(byte)145, cast(byte)161, 8, 35, 66, cast(byte)177, cast(byte)193, 21, 82, cast(byte)209, cast(byte)240, 36, 51, 98,
46 114, cast(byte)130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 52 114, cast(byte)130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53,
47 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 53 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87,
48 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 54 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118,
49 119, 120, 121, 122, cast(byte)131, cast(byte)132, cast(byte)133, cast(byte)134, cast(byte)135, cast(byte)136, cast(byte)137, cast(byte)138, cast(byte)146, cast(byte)147, cast(byte)148, 55 119, 120, 121, 122, cast(byte)131, cast(byte)132, cast(byte)133, cast(byte)134, cast(byte)135, cast(byte)136, cast(byte)137, cast(byte)138, cast(byte)146, cast(byte)147, cast(byte)148,
50 cast(byte)149, cast(byte)150, cast(byte)151, cast(byte)152, cast(byte)153, cast(byte)154, cast(byte)162, cast(byte)163, cast(byte)164, cast(byte)165, cast(byte)166, cast(byte)167, cast(byte)168, cast(byte)169, cast(byte)170, 56 cast(byte)149, cast(byte)150, cast(byte)151, cast(byte)152, cast(byte)153, cast(byte)154, cast(byte)162, cast(byte)163, cast(byte)164, cast(byte)165, cast(byte)166, cast(byte)167, cast(byte)168, cast(byte)169, cast(byte)170,
51 cast(byte)178, cast(byte)179, cast(byte)180, cast(byte)181, cast(byte)182, cast(byte)183, cast(byte)184, cast(byte)185, cast(byte)186, cast(byte)194, cast(byte)195, cast(byte)196, cast(byte)197, cast(byte)198, cast(byte)199, 57 cast(byte)178, cast(byte)179, cast(byte)180, cast(byte)181, cast(byte)182, cast(byte)183, cast(byte)184, cast(byte)185, cast(byte)186, cast(byte)194, cast(byte)195, cast(byte)196, cast(byte)197, cast(byte)198, cast(byte)199,
52 cast(byte)200, cast(byte)201, cast(byte)202, cast(byte)210, cast(byte)211, cast(byte)212, cast(byte)213, cast(byte)214, cast(byte)215, cast(byte)216, cast(byte)217, cast(byte)218, cast(byte)225, cast(byte)226, cast(byte)227, 58 cast(byte)200, cast(byte)201, cast(byte)202, cast(byte)210, cast(byte)211, cast(byte)212, cast(byte)213, cast(byte)214, cast(byte)215, cast(byte)216, cast(byte)217, cast(byte)218, cast(byte)225, cast(byte)226, cast(byte)227,
53 cast(byte)228, cast(byte)229, cast(byte)230, cast(byte)231, cast(byte)232, cast(byte)233, cast(byte)234, cast(byte)241, cast(byte)242, cast(byte)243, cast(byte)244, cast(byte)245, cast(byte)246, cast(byte)247, cast(byte)248, 59 cast(byte)228, cast(byte)229, cast(byte)230, cast(byte)231, cast(byte)232, cast(byte)233, cast(byte)234, cast(byte)241, cast(byte)242, cast(byte)243, cast(byte)244, cast(byte)245, cast(byte)246, cast(byte)247, cast(byte)248,
54 cast(byte)249, cast(byte)250 60 cast(byte)249, cast(byte)250
55 }; 61 ];
56 static byte[] ACChrominanceTable = { 62 static byte[] ACChrominanceTable = [
57 cast(byte)255, cast(byte)196, 0, cast(byte)181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 63 cast(byte)255, cast(byte)196, 0, cast(byte)181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0,
58 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 64 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34,
59 50, cast(byte)129, 8, 20, 66, cast(byte)145, cast(byte)161, cast(byte)177, cast(byte)193, 9, 35, 65 50, cast(byte)129, 8, 20, 66, cast(byte)145, cast(byte)161, cast(byte)177, cast(byte)193, 9, 35,
60 51, 82, cast(byte)240, 21, 98, 114, cast(byte)209, 10, 22, 36, 52, cast(byte)225, 37, 66 51, 82, cast(byte)240, 21, 98, 114, cast(byte)209, 10, 22, 36, 52, cast(byte)225, 37,
61 cast(byte)241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 67 cast(byte)241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67,
62 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 68 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102,
63 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, cast(byte)130, 69 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, cast(byte)130,
64 cast(byte)131, cast(byte)132, cast(byte)133, cast(byte)134, cast(byte)135, cast(byte)136, cast(byte)137, 70 cast(byte)131, cast(byte)132, cast(byte)133, cast(byte)134, cast(byte)135, cast(byte)136, cast(byte)137,
65 cast(byte)138, cast(byte)146, cast(byte)147, cast(byte)148, cast(byte)149, cast(byte)150, cast(byte)151, 71 cast(byte)138, cast(byte)146, cast(byte)147, cast(byte)148, cast(byte)149, cast(byte)150, cast(byte)151,
66 cast(byte)152, cast(byte)153, cast(byte)154, cast(byte)162, cast(byte)163, cast(byte)164, cast(byte)165, 72 cast(byte)152, cast(byte)153, cast(byte)154, cast(byte)162, cast(byte)163, cast(byte)164, cast(byte)165,
67 cast(byte)166, cast(byte)167, cast(byte)168, cast(byte)169, cast(byte)170, cast(byte)178, cast(byte)179, 73 cast(byte)166, cast(byte)167, cast(byte)168, cast(byte)169, cast(byte)170, cast(byte)178, cast(byte)179,
68 cast(byte)180, cast(byte)181, cast(byte)182, cast(byte)183, cast(byte)184, cast(byte)185, cast(byte)186, 74 cast(byte)180, cast(byte)181, cast(byte)182, cast(byte)183, cast(byte)184, cast(byte)185, cast(byte)186,
69 cast(byte)194, cast(byte)195, cast(byte)196, cast(byte)197, cast(byte)198, cast(byte)199, cast(byte)200, 75 cast(byte)194, cast(byte)195, cast(byte)196, cast(byte)197, cast(byte)198, cast(byte)199, cast(byte)200,
70 cast(byte)201, cast(byte)202, cast(byte)210, cast(byte)211, cast(byte)212, cast(byte)213, cast(byte)214, 76 cast(byte)201, cast(byte)202, cast(byte)210, cast(byte)211, cast(byte)212, cast(byte)213, cast(byte)214,
71 cast(byte)215, cast(byte)216, cast(byte)217, cast(byte)218, cast(byte)226, cast(byte)227, cast(byte)228, 77 cast(byte)215, cast(byte)216, cast(byte)217, cast(byte)218, cast(byte)226, cast(byte)227, cast(byte)228,
72 cast(byte)229, cast(byte)230, cast(byte)231, cast(byte)232, cast(byte)233, cast(byte)234, cast(byte)242, 78 cast(byte)229, cast(byte)230, cast(byte)231, cast(byte)232, cast(byte)233, cast(byte)234, cast(byte)242,
73 cast(byte)243, cast(byte)244, cast(byte)245, cast(byte)246, cast(byte)247, cast(byte)248, cast(byte)249, 79 cast(byte)243, cast(byte)244, cast(byte)245, cast(byte)246, cast(byte)247, cast(byte)248, cast(byte)249,
74 cast(byte)250 80 cast(byte)250
75 }; 81 ];
76 82
77 public this(byte[] reference) { 83 public this(byte[] reference) {
78 super(reference); 84 super(reference);
79 } 85 }
80 86
81 public this(LEDataInputStream byteStream) { 87 public this(LEDataInputStream byteStream) {
143 int huffTableCount = 0; 149 int huffTableCount = 0;
144 while (totalLength > 0) { 150 while (totalLength > 0) {
145 int tc = (reference[ofs] & 0xFF) >> 4; // table class: AC (1) or DC (0) 151 int tc = (reference[ofs] & 0xFF) >> 4; // table class: AC (1) or DC (0)
146 int tid = reference[ofs] & 0xF; // table id: 0-1 baseline, 0-3 prog/ext 152 int tid = reference[ofs] & 0xF; // table id: 0-1 baseline, 0-3 prog/ext
147 ofs++; 153 ofs++;
148 154
149 /* Read the 16 count bytes and add them together to get the table size. */ 155 /* Read the 16 count bytes and add them together to get the table size. */
150 int count = 0; 156 int count = 0;
151 for (int i = 0; i < bits.length; i++) { 157 for (int i = 0; i < bits.length; i++) {
152 int bCount = reference[ofs + i] & 0xFF; 158 int bCount = reference[ofs + i] & 0xFF;
153 bits[i] = bCount; 159 bits[i] = bCount;
154 count += bCount; 160 count += bCount;
155 } 161 }
156 ofs += 16; 162 ofs += 16;
157 totalLength -= 17; 163 totalLength -= 17;
158 164
159 /* Read the table. */ 165 /* Read the table. */
160 int[] huffVals = new int[count]; 166 int[] huffVals = new int[count];
161 for (int i = 0; i < count; i++) { 167 for (int i = 0; i < count; i++) {
162 huffVals[i] = reference[ofs + i] & 0xFF; 168 huffVals[i] = reference[ofs + i] & 0xFF;
163 } 169 }
164 ofs += count; 170 ofs += count;
165 totalLength -= count; 171 totalLength -= count;
166 172
167 /* Calculate the lengths. */ 173 /* Calculate the lengths. */
168 int[] huffCodeLengths = new int[50]; // start with 50 and increment as needed 174 int[] huffCodeLengths = new int[50]; // start with 50 and increment as needed
169 int huffCodeLengthsIndex = 0; 175 int huffCodeLengthsIndex = 0;
170 for (int i = 0; i < 16; i++) { 176 for (int i = 0; i < 16; i++) {
171 for (int j = 0; j < bits[i]; j++) { 177 for (int j = 0; j < bits[i]; j++) {
176 } 182 }
177 huffCodeLengths[huffCodeLengthsIndex] = i + 1; 183 huffCodeLengths[huffCodeLengthsIndex] = i + 1;
178 huffCodeLengthsIndex++; 184 huffCodeLengthsIndex++;
179 } 185 }
180 } 186 }
181 187
182 /* Truncate huffCodeLengths to the correct size. */ 188 /* Truncate huffCodeLengths to the correct size. */
183 if (huffCodeLengthsIndex < huffCodeLengths.length) { 189 if (huffCodeLengthsIndex < huffCodeLengths.length) {
184 int[] newHuffCodeLengths = new int[huffCodeLengthsIndex]; 190 int[] newHuffCodeLengths = new int[huffCodeLengthsIndex];
185 System.arraycopy(huffCodeLengths, 0, newHuffCodeLengths, 0, huffCodeLengthsIndex); 191 System.arraycopy(huffCodeLengths, 0, newHuffCodeLengths, 0, huffCodeLengthsIndex);
186 huffCodeLengths = newHuffCodeLengths; 192 huffCodeLengths = newHuffCodeLengths;
187 } 193 }
188 194
189 /* Calculate the Huffman codes. */ 195 /* Calculate the Huffman codes. */
190 int[] huffCodes = new int[50]; // start with 50 and increment as needed 196 int[] huffCodes = new int[50]; // start with 50 and increment as needed
191 int huffCodesIndex = 0; 197 int huffCodesIndex = 0;
192 int k = 1; 198 int k = 1;
193 int code = 0; 199 int code = 0;
206 p++; 212 p++;
207 } 213 }
208 code *= 2; 214 code *= 2;
209 si++; 215 si++;
210 } 216 }
211 217
212 /* Truncate huffCodes to the correct size. */ 218 /* Truncate huffCodes to the correct size. */
213 if (huffCodesIndex < huffCodes.length) { 219 if (huffCodesIndex < huffCodes.length) {
214 int[] newHuffCodes = new int[huffCodesIndex]; 220 int[] newHuffCodes = new int[huffCodesIndex];
215 System.arraycopy(huffCodes, 0, newHuffCodes, 0, huffCodesIndex); 221 System.arraycopy(huffCodes, 0, newHuffCodes, 0, huffCodesIndex);
216 huffCodes = newHuffCodes; 222 huffCodes = newHuffCodes;
217 } 223 }
218 224
219 /* Calculate the maximum and minimum codes */ 225 /* Calculate the maximum and minimum codes */
220 k = 0; 226 k = 0;
221 int[] maxCodes = new int[16]; 227 int[] maxCodes = new int[16];
222 int[] minCodes = new int[16]; 228 int[] minCodes = new int[16];
223 int[] valPtrs = new int[16]; 229 int[] valPtrs = new int[16];
230 minCodes[i] = huffCodes[k]; 236 minCodes[i] = huffCodes[k];
231 k += bSize; 237 k += bSize;
232 maxCodes[i] = huffCodes[k - 1]; 238 maxCodes[i] = huffCodes[k - 1];
233 } 239 }
234 } 240 }
235 241
236 /* Calculate the eHuffman codes and lengths. */ 242 /* Calculate the eHuffman codes and lengths. */
237 int[] eHuffCodes = new int[256]; 243 int[] eHuffCodes = new int[256];
238 byte[] eHuffSize = new byte[256]; 244 byte[] eHuffSize = new byte[256];
239 for (int i = 0; i < huffCodesIndex; i++) { 245 for (int i = 0; i < huffCodesIndex; i++) {
240 eHuffCodes[huffVals[i]] = huffCodes[i]; 246 eHuffCodes[huffVals[i]] = huffCodes[i];
241 eHuffSize[huffVals[i]] = cast(byte)huffCodeLengths[i]; 247 eHuffSize[huffVals[i]] = cast(byte)huffCodeLengths[i];
242 } 248 }
243 249
244 /* Create the new JPEGHuffmanTable and add it to the allTables array. */ 250 /* Create the new JPEGHuffmanTable and add it to the allTables array. */
245 JPEGHuffmanTable dhtTable = new JPEGHuffmanTable(reference); 251 JPEGHuffmanTable dhtTable = new JPEGHuffmanTable(reference);
246 dhtTable.tableClass = tc; 252 dhtTable.tableClass = tc;
247 dhtTable.tableIdentifier = tid; 253 dhtTable.tableIdentifier = tid;
248 dhtTable.dhValues = huffVals; 254 dhtTable.dhValues = huffVals;
256 } 262 }
257 allTables = new JPEGHuffmanTable[huffTableCount]; 263 allTables = new JPEGHuffmanTable[huffTableCount];
258 System.arraycopy(huffTables, 0, allTables, 0, huffTableCount); 264 System.arraycopy(huffTables, 0, allTables, 0, huffTableCount);
259 } 265 }
260 266
261 public int signature() { 267 public override int signature() {
262 return JPEGFileFormat.DHT; 268 return JPEGFileFormat.DHT;
263 } 269 }
264 } 270 }