root/dwt/internal/image/JPEGHuffmanTable.d

Revision 213:36f5cb12e1a2, 11.3 kB (checked in by Frank Benoit <benoit@tionex.de>, 8 months ago)

Update to SWT 3.4M7

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