HuffTable.java~
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Version 0.1 of F5 Steganography Software by Andreas Westfeld 1999 */
/*********************************************************/
/* JPEG Decoder */
/* Sean Breslin */
/* EE590 Directed Research */
/* Dr. Ortega */
/* Fall 1997 */
/* */
/* HuffTable.class: */
/* Extracts Huffman table from image header */
/* data. Instanciate one class for each table */
/* in the file the header. */
/* */
/* Methods: */
/* getHUFFVAL(), returns HUFFVAL array */
/* getVALPRT(), returns VALPTR array */
/* getMAXCODE(), returns MAXCODE array */
/* getMINCODE(), returns MINCODE array */
/* */
/********************** 11/4/97 **************************/
/* /////////////// DISCLAIMER/////////////////////////////////
This software is provided by the author and
contributors ``as is'' and any express or implied
warranties, including, but not limited to, the
implied warranties of merchantability and
fitness for a particular purpose are dis-
claimed. In no event shall the author or con-
tributors be liable for any direct, indirect,
incidental, special, exemplary, or consequen-
tial damages (including, but not limited to,
procurement of substitute goods or services;
loss of use, data, or profits; or business
interruption) however caused and on any
theory of liability, whether in contract,
strict liability, or tort (including negligence
or otherwise) arising in any way out of the use
of this software, even if advised of the poss-
ibility of such damage.
//////////////////////////////////////////////////////*/
// westfeld
package ortega;
import java.io.*;
public class HuffTable {
// Instance variables
private int[] BITS = new int[17];
private int[] HUFFVAL = new int[256];
private int[] HUFFCODE = new int[257];
private int[] HUFFSIZE = new int[257];
private int[] EHUFCO = new int[257];
private int[] EHUFSI = new int[257];
private int[] MINCODE = new int[17];
private int[] MAXCODE = new int[18];
private int[] VALPTR = new int[17];
private int Ln, SI, I, J, K, LASTK, CODE;
// Constructor Method
public HuffTable(DataInputStream d, int l) {
dis = d;
// System.out.println("Lnge="+l);
// Get table data from input stream
Ln=19+getTableData();
// System.out.println(Ln);
Generate_size_table(); // Flow Chart C.1
Generate_code_table(); // Flow Chart C.2
Order_codes(); // Flow Chart C.3
Decoder_tables(); // Generate decoder tables Flow Chart F.15
}
public int getLen() {
return Ln;
}
// IO MethodS
public int[] getHUFFVAL() { return HUFFVAL; }
public int[] getVALPTR() { return VALPTR; }
public int[] getMAXCODE() { return MAXCODE; }
public int[] getMINCODE() { return MINCODE; }
private int getByte() {
try {
return dis.readUnsignedByte();
}
catch (IOException e) { return -1; }
}
private int getTableData(){
// Get BITS list
int count=0;
for(int x=1;x<17;x++) {
BITS[x] = getByte();
count += BITS[x];
}
// Read in HUFFVAL
for(int x=0;x<count;x++){
//System.out.println(Ln);
HUFFVAL[x] = getByte();
}
return count;
}
private void Generate_size_table() {
// Generate HUFFSIZE table Flow Chart C.1
K=0; I=1; J=1;
while(true) {
if(J>BITS[I]) {
J=1; I++;
if(I > 16)
break;
}
else {
HUFFSIZE[K++]=I;
J++;
}
}
HUFFSIZE[K] = 0;
LASTK = K;
}
private void Generate_code_table() {
// Generate Code table Flow Chart C.2
K = 0; CODE = 0; SI = HUFFSIZE[0];
while(true) {
HUFFCODE[K++] = CODE++;
if(HUFFSIZE[K] == SI)
continue;
if(HUFFSIZE[K] == 0)
break;
while(true){
CODE<<=1;
SI++;
if(HUFFSIZE[K] == SI)
break;
}
}
}
private void Order_codes() {
// Order Codes Flow Chart C.3
K=0;
while(true) {
I = HUFFVAL[K];
EHUFCO[I] = HUFFCODE[K];
EHUFSI[I] = HUFFSIZE[K++];
if(K >= LASTK) break;
}
}
private void Decoder_tables() {
// Decoder table generation Flow Chart F.15
I = 0; J = 0;
while(true) {
if(++I > 16)
return;
if(BITS[I] == 0)
MAXCODE[I] = -1;
else {
VALPTR[I] = J;
MINCODE[I] = HUFFCODE[J];
J = J + BITS[I] - 1;
MAXCODE[I] = HUFFCODE[J++];
}
}
}
// Declare input steam
DataInputStream dis;
}