comparison dwt/internal/image/PngHuffmanTable.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, 2008 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.PngHuffmanTable;
12 14
13 import java.io.IOException; 15 import dwt.internal.image.PngDecodingDataStream;
14 16
15 public class PngHuffmanTable { 17 public class PngHuffmanTable {
16 CodeLengthInfo[] codeLengthInfo; 18 CodeLengthInfo[] codeLengthInfo;
17 int[] codeValues; 19 int[] codeValues;
18 20
19 static final int MAX_CODE_LENGTH = 15; 21 static const int MAX_CODE_LENGTH = 15;
20 static final int BAD_CODE = 0xFFFFFFF; 22 static const int BAD_CODE = 0xFFFFFFF;
21 static final int incs[] = {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1}; 23 static const int incs[] = [1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1];
22 24
23 this (int[] lengths) { 25 this (int[] lengths) {
24 super();
25 initialize(lengths); 26 initialize(lengths);
26 generateTable(lengths); 27 generateTable(lengths);
27 } 28 }
28 29
29 private void initialize(int[] lengths) { 30 private void initialize(int[] lengths) {
30 codeValues = new int[lengths.length]; 31 codeValues = new int[lengths.length];
31 for (int i = 0; i < codeValues.length; i++) { 32 for (int i = 0; i < codeValues.length; i++) {
32 codeValues[i] = i; 33 codeValues[i] = i;
33 } 34 }
34 35
35 // minCodesByLength[n] : The smallest Huffman code of length n + 1. 36 // minCodesByLength[n] : The smallest Huffman code of length n + 1.
36 // maxCodesByLength[n] : The largest Huffman code of length n + 1. 37 // maxCodesByLength[n] : The largest Huffman code of length n + 1.
37 // indexesByLength[n] : Index into the values array. First value with a code of length n + 1. 38 // indexesByLength[n] : Index into the values array. First value with a code of length n + 1.
38 codeLengthInfo = new CodeLengthInfo[MAX_CODE_LENGTH]; 39 codeLengthInfo = new CodeLengthInfo[MAX_CODE_LENGTH];
39 for (int i = 0; i < MAX_CODE_LENGTH; i++) { 40 for (int i = 0; i < MAX_CODE_LENGTH; i++) {
42 codeLengthInfo[i].baseIndex = 0; 43 codeLengthInfo[i].baseIndex = 0;
43 codeLengthInfo[i].min = BAD_CODE; 44 codeLengthInfo[i].min = BAD_CODE;
44 codeLengthInfo[i].max = -1; 45 codeLengthInfo[i].max = -1;
45 } 46 }
46 } 47 }
47 48
48 private void generateTable(int[] lengths) { 49 private void generateTable(int[] lengths) {
49 // Sort the values using shellsort. Primary key is code size. Secondary key is value. 50 // Sort the values using shellsort. Primary key is code size. Secondary key is value.
50 int codeValuesTemp; 51 int codeValuesTemp;
51 for (int k = 0; k < 16; k++) { 52 for (int k = 0; k < 16; k++) {
52 for (int h = incs[k], i = h; i < lengths.length; i++) { 53 for (int h = incs[k], i = h; i < lengths.length; i++) {
77 if (lastLength !is 0) { 78 if (lastLength !is 0) {
78 codes[i] = code; 79 codes[i] = code;
79 code++; 80 code++;
80 } 81 }
81 } 82 }
82 83
83 int last = 0; 84 int last = 0;
84 for (int i = 0; i < lengths.length; i++) { 85 for (int i = 0; i < lengths.length; i++) {
85 if (last !is lengths[i]) { 86 if (last !is lengths[i]) {
86 last = lengths[i]; 87 last = lengths[i];
87 codeLengthInfo[last - 1].baseIndex = i; 88 codeLengthInfo[last - 1].baseIndex = i;
111 112
112 // indexesByLength[codelength] is the first code of length (codelength + 1) 113 // indexesByLength[codelength] is the first code of length (codelength + 1)
113 // so now we can look up the value for the Huffman code in the table. 114 // so now we can look up the value for the Huffman code in the table.
114 int index = codeLengthInfo[codelength].baseIndex + offset; 115 int index = codeLengthInfo[codelength].baseIndex + offset;
115 return codeValues[index]; 116 return codeValues[index];
116 } 117 }
117 118
118 static class CodeLengthInfo { 119 static class CodeLengthInfo {
119 int length; 120 int length;
120 int max; 121 int max;
121 int min; 122 int min;
122 int baseIndex; 123 int baseIndex;