25
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.image.PngHuffmanTables;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import org.eclipse.swt.internal.image.PngHuffmanTable;
|
|
18 import org.eclipse.swt.internal.image.PngDecodingDataStream;
|
|
19 import org.eclipse.swt.internal.image.PngLzBlockReader;
|
|
20
|
|
21 public class PngHuffmanTables {
|
|
22 PngHuffmanTable literalTable;
|
|
23 PngHuffmanTable distanceTable;
|
|
24
|
|
25 static PngHuffmanTable FixedLiteralTable;
|
|
26 static PngHuffmanTable FixedDistanceTable;
|
|
27
|
|
28 static final int LiteralTableSize = 288;
|
|
29 static final int[] FixedLiteralLengths = [
|
|
30 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
31 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
32 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
33 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
34 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
35 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
36 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
37 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
38 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
39 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
|
40 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
41 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
42 ];
|
|
43
|
|
44 static final int DistanceTableSize = 32;
|
|
45 static final int[] FixedDistanceLengths = [
|
|
46 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
47 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
48 ];
|
|
49
|
|
50 static final int LengthCodeTableSize = 19;
|
|
51 static final int[] LengthCodeOrder = [
|
|
52 16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
|
|
53 11, 4, 12, 3, 13, 2, 14, 1, 15
|
|
54 ];
|
|
55
|
|
56 static PngHuffmanTables getDynamicTables(PngDecodingDataStream stream) {
|
|
57 return new PngHuffmanTables(stream);
|
|
58 }
|
|
59 static PngHuffmanTables getFixedTables() {
|
|
60 return new PngHuffmanTables();
|
|
61 }
|
|
62
|
|
63 private PngHuffmanTable getFixedLiteralTable() {
|
|
64 if (FixedLiteralTable is null) {
|
|
65 FixedLiteralTable = new PngHuffmanTable(FixedLiteralLengths);
|
|
66 }
|
|
67 return FixedLiteralTable;
|
|
68 }
|
|
69
|
|
70 private PngHuffmanTable getFixedDistanceTable() {
|
|
71 if (FixedDistanceTable is null) {
|
|
72 FixedDistanceTable = new PngHuffmanTable(FixedDistanceLengths);
|
|
73 }
|
|
74 return FixedDistanceTable;
|
|
75 }
|
|
76
|
|
77 private this () {
|
|
78 literalTable = getFixedLiteralTable();
|
|
79 distanceTable = getFixedDistanceTable();
|
|
80 }
|
|
81
|
|
82 private this (PngDecodingDataStream stream) {
|
|
83 int literals = PngLzBlockReader.FIRST_LENGTH_CODE
|
|
84 + stream.getNextIdatBits(5);
|
|
85 int distances = PngLzBlockReader.FIRST_DISTANCE_CODE
|
|
86 + stream.getNextIdatBits(5);
|
|
87 int codeLengthCodes = PngLzBlockReader.FIRST_CODE_LENGTH_CODE
|
|
88 + stream.getNextIdatBits(4);
|
|
89
|
|
90 if (codeLengthCodes > PngLzBlockReader.LAST_CODE_LENGTH_CODE) {
|
|
91 stream.error();
|
|
92 }
|
|
93
|
|
94 /* Tricky, tricky, tricky. The length codes are stored in
|
|
95 * a very odd order. (For the order, see the definition of
|
|
96 * the static field lengthCodeOrder.) Also, the data may
|
|
97 * not contain values for all the codes. It may just contain
|
|
98 * values for the first X number of codes. The table should
|
|
99 * be of size <LengthCodeTableSize> regardless of the number
|
|
100 * of values actually given in the table.
|
|
101 */
|
|
102 int[] lengthCodes = new int[LengthCodeTableSize];
|
|
103 for (int i = 0; i < codeLengthCodes; i++) {
|
|
104 lengthCodes[LengthCodeOrder[i]] = stream.getNextIdatBits(3);
|
|
105 }
|
|
106 PngHuffmanTable codeLengthsTable = new PngHuffmanTable(lengthCodes);
|
|
107
|
|
108 int[] literalLengths = readLengths(
|
|
109 stream, literals, codeLengthsTable, LiteralTableSize);
|
|
110 int[] distanceLengths = readLengths(
|
|
111 stream, distances, codeLengthsTable, DistanceTableSize);
|
|
112
|
|
113 literalTable = new PngHuffmanTable(literalLengths);
|
|
114 distanceTable = new PngHuffmanTable(distanceLengths);
|
|
115 }
|
|
116
|
|
117 private int [] readLengths (PngDecodingDataStream stream,
|
|
118 int numLengths,
|
|
119 PngHuffmanTable lengthsTable,
|
|
120 int tableSize)
|
|
121 {
|
|
122 int[] lengths = new int[tableSize];
|
|
123
|
|
124 for (int index = 0; index < numLengths;) {
|
|
125 int value = lengthsTable.getNextValue(stream);
|
|
126 if (value < 16) {
|
|
127 // Literal value
|
|
128 lengths[index] = value;
|
|
129 index++;
|
|
130 } else if (value is 16) {
|
|
131 // Repeat the previous code 3-6 times.
|
|
132 int count = stream.getNextIdatBits(2) + 3;
|
|
133 for (int i = 0; i < count; i++) {
|
|
134 lengths[index] = lengths [index - 1];
|
|
135 index++;
|
|
136 }
|
|
137 } else if (value is 17) {
|
|
138 // Repeat 0 3-10 times.
|
|
139 int count = stream.getNextIdatBits(3) + 3;
|
|
140 for (int i = 0; i < count; i++) {
|
|
141 lengths[index] = 0;
|
|
142 index++;
|
|
143 }
|
|
144 } else if (value is 18) {
|
|
145 // Repeat 0 11-138 times.
|
|
146 int count = stream.getNextIdatBits(7) + 11;
|
|
147 for (int i = 0; i < count; i++) {
|
|
148 lengths[index] = 0;
|
|
149 index++;
|
|
150 }
|
|
151 } else {
|
|
152 stream.error();
|
|
153 }
|
|
154 }
|
|
155 return lengths;
|
|
156 }
|
|
157
|
|
158 int getNextLiteralValue(PngDecodingDataStream stream) {
|
|
159 return literalTable.getNextValue(stream);
|
|
160 }
|
|
161
|
|
162 int getNextDistanceValue(PngDecodingDataStream stream) {
|
|
163 return distanceTable.getNextValue(stream);
|
|
164 }
|
|
165
|
|
166 }
|