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