comparison dwt/internal/image/PngHuffmanTables.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 b903c16b6f48
children
comparison
equal deleted inserted replaced
33:965ac0a77267 34:5123b17c98ef
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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.PngHuffmanTables;
12 14
13 import java.io.IOException; 15 import dwt.internal.image.PngHuffmanTable;
16 import dwt.internal.image.PngDecodingDataStream;
17 import dwt.internal.image.PngLzBlockReader;
14 18
15 public class PngHuffmanTables { 19 public class PngHuffmanTables {
16 PngHuffmanTable literalTable; 20 PngHuffmanTable literalTable;
17 PngHuffmanTable distanceTable; 21 PngHuffmanTable distanceTable;
18 22
19 static PngHuffmanTable FixedLiteralTable; 23 static PngHuffmanTable FixedLiteralTable;
20 static PngHuffmanTable FixedDistanceTable; 24 static PngHuffmanTable FixedDistanceTable;
21 25
22 static final int LiteralTableSize = 288; 26 static final int LiteralTableSize = 288;
23 static final int[] FixedLiteralLengths = { 27 static final int[] FixedLiteralLengths = [
24 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,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 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,
34 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 38 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7,
35 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 39 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
36 }; 40 ];
37 41
38 static final int DistanceTableSize = 32; 42 static final int DistanceTableSize = 32;
39 static final int[] FixedDistanceLengths = { 43 static final int[] FixedDistanceLengths = [
40 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 44 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
41 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 45 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
42 }; 46 ];
43 47
44 static final int LengthCodeTableSize = 19; 48 static final int LengthCodeTableSize = 19;
45 static final int[] LengthCodeOrder = { 49 static final int[] LengthCodeOrder = [
46 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 50 16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
47 11, 4, 12, 3, 13, 2, 14, 1, 15 51 11, 4, 12, 3, 13, 2, 14, 1, 15
48 }; 52 ];
49 53
50 static PngHuffmanTables getDynamicTables(PngDecodingDataStream stream) { 54 static PngHuffmanTables getDynamicTables(PngDecodingDataStream stream) {
51 return new PngHuffmanTables(stream); 55 return new PngHuffmanTables(stream);
52 } 56 }
53 static PngHuffmanTables getFixedTables() { 57 static PngHuffmanTables getFixedTables() {
54 return new PngHuffmanTables(); 58 return new PngHuffmanTables();
55 } 59 }
71 private this () { 75 private this () {
72 literalTable = getFixedLiteralTable(); 76 literalTable = getFixedLiteralTable();
73 distanceTable = getFixedDistanceTable(); 77 distanceTable = getFixedDistanceTable();
74 } 78 }
75 79
76 private this (PngDecodingDataStream stream) { 80 private this (PngDecodingDataStream stream) {
77 int literals = PngLzBlockReader.FIRST_LENGTH_CODE 81 int literals = PngLzBlockReader.FIRST_LENGTH_CODE
78 + stream.getNextIdatBits(5); 82 + stream.getNextIdatBits(5);
79 int distances = PngLzBlockReader.FIRST_DISTANCE_CODE 83 int distances = PngLzBlockReader.FIRST_DISTANCE_CODE
80 + stream.getNextIdatBits(5); 84 + stream.getNextIdatBits(5);
81 int codeLengthCodes = PngLzBlockReader.FIRST_CODE_LENGTH_CODE 85 int codeLengthCodes = PngLzBlockReader.FIRST_CODE_LENGTH_CODE
82 + stream.getNextIdatBits(4); 86 + stream.getNextIdatBits(4);
83 87
84 if (codeLengthCodes > PngLzBlockReader.LAST_CODE_LENGTH_CODE) { 88 if (codeLengthCodes > PngLzBlockReader.LAST_CODE_LENGTH_CODE) {
85 stream.error(); 89 stream.error();
86 } 90 }
87 91
88 /* Tricky, tricky, tricky. The length codes are stored in 92 /* Tricky, tricky, tricky. The length codes are stored in
89 * a very odd order. (For the order, see the definition of 93 * a very odd order. (For the order, see the definition of
90 * the static field lengthCodeOrder.) Also, the data may 94 * the static field lengthCodeOrder.) Also, the data may
91 * not contain values for all the codes. It may just contain 95 * not contain values for all the codes. It may just contain
92 * values for the first X number of codes. The table should 96 * values for the first X number of codes. The table should
93 * be of size <LengthCodeTableSize> regardless of the number 97 * be of size <LengthCodeTableSize> regardless of the number
94 * of values actually given in the table. 98 * of values actually given in the table.
95 */ 99 */
96 int[] lengthCodes = new int[LengthCodeTableSize]; 100 int[] lengthCodes = new int[LengthCodeTableSize];
97 for (int i = 0; i < codeLengthCodes; i++) { 101 for (int i = 0; i < codeLengthCodes; i++) {
98 lengthCodes[LengthCodeOrder[i]] = stream.getNextIdatBits(3); 102 lengthCodes[LengthCodeOrder[i]] = stream.getNextIdatBits(3);
99 } 103 }
100 PngHuffmanTable codeLengthsTable = new PngHuffmanTable(lengthCodes); 104 PngHuffmanTable codeLengthsTable = new PngHuffmanTable(lengthCodes);
101 105
102 int[] literalLengths = readLengths( 106 int[] literalLengths = readLengths(
103 stream, literals, codeLengthsTable, LiteralTableSize); 107 stream, literals, codeLengthsTable, LiteralTableSize);
104 int[] distanceLengths = readLengths( 108 int[] distanceLengths = readLengths(
105 stream, distances, codeLengthsTable, DistanceTableSize); 109 stream, distances, codeLengthsTable, DistanceTableSize);
106 110
107 literalTable = new PngHuffmanTable(literalLengths); 111 literalTable = new PngHuffmanTable(literalLengths);
108 distanceTable = new PngHuffmanTable(distanceLengths); 112 distanceTable = new PngHuffmanTable(distanceLengths);
109 } 113 }
110 114
111 private int [] readLengths (PngDecodingDataStream stream, 115 private int [] readLengths (PngDecodingDataStream stream,
112 int numLengths, 116 int numLengths,
113 PngHuffmanTable lengthsTable, 117 PngHuffmanTable lengthsTable,
114 int tableSize) 118 int tableSize)
115 { 119 {
116 int[] lengths = new int[tableSize]; 120 int[] lengths = new int[tableSize];
117 121
118 for (int index = 0; index < numLengths;) { 122 for (int index = 0; index < numLengths;) {
119 int value = lengthsTable.getNextValue(stream); 123 int value = lengthsTable.getNextValue(stream);
120 if (value < 16) { 124 if (value < 16) {
121 // Literal value 125 // Literal value
122 lengths[index] = value; 126 lengths[index] = value;
147 } 151 }
148 } 152 }
149 return lengths; 153 return lengths;
150 } 154 }
151 155
152 int getNextLiteralValue(PngDecodingDataStream stream) { 156 int getNextLiteralValue(PngDecodingDataStream stream) {
153 return literalTable.getNextValue(stream); 157 return literalTable.getNextValue(stream);
154 } 158 }
155 159
156 int getNextDistanceValue(PngDecodingDataStream stream) { 160 int getNextDistanceValue(PngDecodingDataStream stream) {
157 return distanceTable.getNextValue(stream); 161 return distanceTable.getNextValue(stream);
158 } 162 }
159 163
160 } 164 }