3 package com.pixelmed.codec.jpeg;
12 private static final String identString =
"@(#) $Header: /userland/cvs/codec/com/pixelmed/codec/jpeg/HuffmanTable.java,v 1.5 2020/03/28 21:05:39 dclunie Exp $";
14 private int TableClass;
15 private int HuffmanTableIdentifier;
16 private int[] nHuffmanCodesOfLengthI;
17 private int[][] ValueOfHuffmanCodeIJ;
19 public HuffmanTable(
int TableClass,
int HuffmanTableIdentifier,
int[] nHuffmanCodesOfLengthI,
int[][] ValueOfHuffmanCodeIJ) {
20 this.TableClass = TableClass;
21 this.HuffmanTableIdentifier = HuffmanTableIdentifier;
22 this.nHuffmanCodesOfLengthI = nHuffmanCodesOfLengthI;
23 this.ValueOfHuffmanCodeIJ = ValueOfHuffmanCodeIJ;
27 private int countNumberOfCodes() {
29 for (
int i=0; i<nHuffmanCodesOfLengthI.length; ++i) {
30 count += nHuffmanCodesOfLengthI[i];
37 private int[] BITS =
new int[17];
39 private int[] HUFFVAL;
40 private int[] HUFFSIZE;
41 private int[] HUFFCODE;
43 private int[] MINCODE =
new int[17];
44 private int[] MAXCODE =
new int[17];
45 private int[] VALPTR =
new int[17];
61 private int EOBCodeLength;
67 private void expand() {
71 for (
int I=1; I<=16; ++I) {
73 BITS[I] = nHuffmanCodesOfLengthI[I-1];
76 int nCodes = countNumberOfCodes();
80 HUFFVAL =
new int[nCodes];
83 for (
int i=0; i<nHuffmanCodesOfLengthI.length; ++i) {
84 int nCodesThisLength = nHuffmanCodesOfLengthI[i];
85 if (nCodesThisLength > 0) {
86 for (
int j=0; j<nCodesThisLength; ++j) {
87 HUFFVAL[J] = ValueOfHuffmanCodeIJ[i][j];
88 if (HUFFVAL[J] > largestValue) {
89 largestValue = HUFFVAL[J];
100 HUFFSIZE =
new int[nCodes+1];
125 HUFFCODE =
new int[nCodes+1];
129 int SI = HUFFSIZE[0];
134 if (SI != HUFFSIZE[K]) {
135 if (HUFFSIZE[K] == 0)
break;
139 }
while (SI != HUFFSIZE[K]);
148 EFUFCO =
new int[largestValue+1];
149 EFUFSI =
new int[largestValue+1];
151 for (
int K=0; K<HUFFVAL.length; ++K) {
153 EFUFCO[I] = HUFFCODE[K];
154 EFUFSI[I] = HUFFSIZE[K];
171 MINCODE[I] = HUFFCODE[J];
173 MAXCODE[I] = HUFFCODE[J];
181 for (
int I=1; I<=16; ++I) {
182 for (
int J = VALPTR[I]; J < VALPTR[I] + BITS[I]; ++J) {
183 if (HUFFVAL[J] == 0) {
184 EOBCode = HUFFCODE[J];
193 StringBuffer buf =
new StringBuffer();
194 buf.append(
"Huffman Table:\n");
195 buf.append(
"\t TableClass = " +TableClass+
"\n");
196 buf.append(
"\t HuffmanTableIdentifier = "+HuffmanTableIdentifier+
"\n");
197 for (
int i=0; i<16; ++i) {
198 buf.append(
"\t\t nHuffmanCodesOfLength "+i+
" = "+nHuffmanCodesOfLengthI[i]+
"\n");
199 for (
int j=0; j<nHuffmanCodesOfLengthI[i];++j) {
200 buf.append(
"\t\t\t ValueOfHuffmanCode "+j+
" = "+ValueOfHuffmanCodeIJ[i][j]+
"\n");
203 buf.append(
"\t Expanded:\n");
204 for (
int I=1; I<=16; ++I) {
205 buf.append(
"\t\t["+I+
"] MINCODE="+Integer.toBinaryString(MINCODE[I])+
" MAXCODE="+Integer.toBinaryString(MAXCODE[I])+
""+
" VALPTR="+VALPTR[I]+
"\n");
207 for (
int J=0; J<HUFFVAL.length; ++J) {
208 buf.append(
"\t\t["+J+
"] HUFFVAL=0x"+Integer.toHexString(HUFFVAL[J])+
"\n");
210 buf.append(
"\t\tEOBCode="+Integer.toBinaryString(EOBCode)+
" 0x"+Integer.toHexString(EOBCode)+
" (length "+EOBCodeLength+
" dec)\n");
211 return buf.toString();