KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > tiff > TIFFFaxDecoder


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.codec.tiff;
19
20
21 class TIFFFaxDecoder {
22
23     private int bitPointer, bytePointer;
24     private byte[] data;
25     private int w, h;
26     private int fillOrder;
27
28     // Data structures needed to store changing elements for the previous
29
// and the current scanline
30
private int changingElemSize = 0;
31     private int prevChangingElems[];
32     private int currChangingElems[];
33
34     // Element at which to start search in getNextChangingElement
35
private int lastChangingElement = 0;
36
37     private int compression = 2;
38
39     // Variables set by T4Options
40
private int uncompressedMode = 0;
41     private int fillBits = 0;
42     private int oneD;
43
44     static int table1[] = {
45     0x00, // 0 bits are left in first byte - SHOULD NOT HAPPEN
46
0x01, // 1 bits are left in first byte
47
0x03, // 2 bits are left in first byte
48
0x07, // 3 bits are left in first byte
49
0x0f, // 4 bits are left in first byte
50
0x1f, // 5 bits are left in first byte
51
0x3f, // 6 bits are left in first byte
52
0x7f, // 7 bits are left in first byte
53
0xff // 8 bits are left in first byte
54
};
55     
56     static int table2[] = {
57     0x00, // 0
58
0x80, // 1
59
0xc0, // 2
60
0xe0, // 3
61
0xf0, // 4
62
0xf8, // 5
63
0xfc, // 6
64
0xfe, // 7
65
0xff // 8
66
};
67
68     // Table to be used when fillOrder = 2, for flipping bytes.
69
static byte flipTable[] = {
70      0, -128, 64, -64, 32, -96, 96, -32,
71     16, -112, 80, -48, 48, -80, 112, -16,
72      8, -120, 72, -56, 40, -88, 104, -24,
73     24, -104, 88, -40, 56, -72, 120, -8,
74      4, -124, 68, -60, 36, -92, 100, -28,
75     20, -108, 84, -44, 52, -76, 116, -12,
76     12, -116, 76, -52, 44, -84, 108, -20,
77     28, -100, 92, -36, 60, -68, 124, -4,
78      2, -126, 66, -62, 34, -94, 98, -30,
79     18, -110, 82, -46, 50, -78, 114, -14,
80     10, -118, 74, -54, 42, -86, 106, -22,
81     26, -102, 90, -38, 58, -70, 122, -6,
82      6, -122, 70, -58, 38, -90, 102, -26,
83     22, -106, 86, -42, 54, -74, 118, -10,
84     14, -114, 78, -50, 46, -82, 110, -18,
85     30, -98, 94, -34, 62, -66, 126, -2,
86      1, -127, 65, -63, 33, -95, 97, -31,
87     17, -111, 81, -47, 49, -79, 113, -15,
88      9, -119, 73, -55, 41, -87, 105, -23,
89     25, -103, 89, -39, 57, -71, 121, -7,
90      5, -123, 69, -59, 37, -91, 101, -27,
91     21, -107, 85, -43, 53, -75, 117, -11,
92     13, -115, 77, -51, 45, -83, 109, -19,
93     29, -99, 93, -35, 61, -67, 125, -3,
94      3, -125, 67, -61, 35, -93, 99, -29,
95     19, -109, 83, -45, 51, -77, 115, -13,
96     11, -117, 75, -53, 43, -85, 107, -21,
97     27, -101, 91, -37, 59, -69, 123, -5,
98      7, -121, 71, -57, 39, -89, 103, -25,
99     23, -105, 87, -41, 55, -73, 119, -9,
100     15, -113, 79, -49, 47, -81, 111, -17,
101     31, -97, 95, -33, 63, -65, 127, -1,
102     };
103     
104     // The main 10 bit white runs lookup table
105
static short white[] = {
106     // 0 - 7
107
6430, 6400, 6400, 6400, 3225, 3225, 3225, 3225,
108     // 8 - 15
109
944, 944, 944, 944, 976, 976, 976, 976,
110     // 16 - 23
111
1456, 1456, 1456, 1456, 1488, 1488, 1488, 1488,
112     // 24 - 31
113
718, 718, 718, 718, 718, 718, 718, 718,
114     // 32 - 39
115
750, 750, 750, 750, 750, 750, 750, 750,
116     // 40 - 47
117
1520, 1520, 1520, 1520, 1552, 1552, 1552, 1552,
118     // 48 - 55
119
428, 428, 428, 428, 428, 428, 428, 428,
120     // 56 - 63
121
428, 428, 428, 428, 428, 428, 428, 428,
122     // 64 - 71
123
654, 654, 654, 654, 654, 654, 654, 654,
124     // 72 - 79
125
1072, 1072, 1072, 1072, 1104, 1104, 1104, 1104,
126     // 80 - 87
127
1136, 1136, 1136, 1136, 1168, 1168, 1168, 1168,
128     // 88 - 95
129
1200, 1200, 1200, 1200, 1232, 1232, 1232, 1232,
130     // 96 - 103
131
622, 622, 622, 622, 622, 622, 622, 622,
132     // 104 - 111
133
1008, 1008, 1008, 1008, 1040, 1040, 1040, 1040,
134     // 112 - 119
135
44, 44, 44, 44, 44, 44, 44, 44,
136     // 120 - 127
137
44, 44, 44, 44, 44, 44, 44, 44,
138     // 128 - 135
139
396, 396, 396, 396, 396, 396, 396, 396,
140     // 136 - 143
141
396, 396, 396, 396, 396, 396, 396, 396,
142     // 144 - 151
143
1712, 1712, 1712, 1712, 1744, 1744, 1744, 1744,
144     // 152 - 159
145
846, 846, 846, 846, 846, 846, 846, 846,
146     // 160 - 167
147
1264, 1264, 1264, 1264, 1296, 1296, 1296, 1296,
148     // 168 - 175
149
1328, 1328, 1328, 1328, 1360, 1360, 1360, 1360,
150     // 176 - 183
151
1392, 1392, 1392, 1392, 1424, 1424, 1424, 1424,
152     // 184 - 191
153
686, 686, 686, 686, 686, 686, 686, 686,
154     // 192 - 199
155
910, 910, 910, 910, 910, 910, 910, 910,
156     // 200 - 207
157
1968, 1968, 1968, 1968, 2000, 2000, 2000, 2000,
158     // 208 - 215
159
2032, 2032, 2032, 2032, 16, 16, 16, 16,
160     // 216 - 223
161
10257, 10257, 10257, 10257, 12305, 12305, 12305, 12305,
162     // 224 - 231
163
330, 330, 330, 330, 330, 330, 330, 330,
164     // 232 - 239
165
330, 330, 330, 330, 330, 330, 330, 330,
166     // 240 - 247
167
330, 330, 330, 330, 330, 330, 330, 330,
168     // 248 - 255
169
330, 330, 330, 330, 330, 330, 330, 330,
170     // 256 - 263
171
362, 362, 362, 362, 362, 362, 362, 362,
172     // 264 - 271
173
362, 362, 362, 362, 362, 362, 362, 362,
174     // 272 - 279
175
362, 362, 362, 362, 362, 362, 362, 362,
176     // 280 - 287
177
362, 362, 362, 362, 362, 362, 362, 362,
178     // 288 - 295
179
878, 878, 878, 878, 878, 878, 878, 878,
180     // 296 - 303
181
1904, 1904, 1904, 1904, 1936, 1936, 1936, 1936,
182     // 304 - 311
183
-18413, -18413, -16365, -16365, -14317, -14317, -10221, -10221,
184     // 312 - 319
185
590, 590, 590, 590, 590, 590, 590, 590,
186     // 320 - 327
187
782, 782, 782, 782, 782, 782, 782, 782,
188     // 328 - 335
189
1584, 1584, 1584, 1584, 1616, 1616, 1616, 1616,
190     // 336 - 343
191
1648, 1648, 1648, 1648, 1680, 1680, 1680, 1680,
192     // 344 - 351
193
814, 814, 814, 814, 814, 814, 814, 814,
194     // 352 - 359
195
1776, 1776, 1776, 1776, 1808, 1808, 1808, 1808,
196     // 360 - 367
197
1840, 1840, 1840, 1840, 1872, 1872, 1872, 1872,
198     // 368 - 375
199
6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157,
200     // 376 - 383
201
6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157,
202     // 384 - 391
203
-12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275,
204     // 392 - 399
205
-12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275,
206     // 400 - 407
207
14353, 14353, 14353, 14353, 16401, 16401, 16401, 16401,
208     // 408 - 415
209
22547, 22547, 24595, 24595, 20497, 20497, 20497, 20497,
210     // 416 - 423
211
18449, 18449, 18449, 18449, 26643, 26643, 28691, 28691,
212     // 424 - 431
213
30739, 30739, -32749, -32749, -30701, -30701, -28653, -28653,
214     // 432 - 439
215
-26605, -26605, -24557, -24557, -22509, -22509, -20461, -20461,
216     // 440 - 447
217
8207, 8207, 8207, 8207, 8207, 8207, 8207, 8207,
218     // 448 - 455
219
72, 72, 72, 72, 72, 72, 72, 72,
220     // 456 - 463
221
72, 72, 72, 72, 72, 72, 72, 72,
222     // 464 - 471
223
72, 72, 72, 72, 72, 72, 72, 72,
224     // 472 - 479
225
72, 72, 72, 72, 72, 72, 72, 72,
226     // 480 - 487
227
72, 72, 72, 72, 72, 72, 72, 72,
228     // 488 - 495
229
72, 72, 72, 72, 72, 72, 72, 72,
230     // 496 - 503
231
72, 72, 72, 72, 72, 72, 72, 72,
232     // 504 - 511
233
72, 72, 72, 72, 72, 72, 72, 72,
234     // 512 - 519
235
104, 104, 104, 104, 104, 104, 104, 104,
236     // 520 - 527
237
104, 104, 104, 104, 104, 104, 104, 104,
238     // 528 - 535
239
104, 104, 104, 104, 104, 104, 104, 104,
240     // 536 - 543
241
104, 104, 104, 104, 104, 104, 104, 104,
242     // 544 - 551
243
104, 104, 104, 104, 104, 104, 104, 104,
244     // 552 - 559
245
104, 104, 104, 104, 104, 104, 104, 104,
246     // 560 - 567
247
104, 104, 104, 104, 104, 104, 104, 104,
248     // 568 - 575
249
104, 104, 104, 104, 104, 104, 104, 104,
250     // 576 - 583
251
4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
252     // 584 - 591
253
4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
254     // 592 - 599
255
4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
256     // 600 - 607
257
4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
258     // 608 - 615
259
266, 266, 266, 266, 266, 266, 266, 266,
260     // 616 - 623
261
266, 266, 266, 266, 266, 266, 266, 266,
262     // 624 - 631
263
266, 266, 266, 266, 266, 266, 266, 266,
264     // 632 - 639
265
266, 266, 266, 266, 266, 266, 266, 266,
266     // 640 - 647
267
298, 298, 298, 298, 298, 298, 298, 298,
268     // 648 - 655
269
298, 298, 298, 298, 298, 298, 298, 298,
270     // 656 - 663
271
298, 298, 298, 298, 298, 298, 298, 298,
272     // 664 - 671
273
298, 298, 298, 298, 298, 298, 298, 298,
274     // 672 - 679
275
524, 524, 524, 524, 524, 524, 524, 524,
276     // 680 - 687
277
524, 524, 524, 524, 524, 524, 524, 524,
278     // 688 - 695
279
556, 556, 556, 556, 556, 556, 556, 556,
280     // 696 - 703
281
556, 556, 556, 556, 556, 556, 556, 556,
282     // 704 - 711
283
136, 136, 136, 136, 136, 136, 136, 136,
284     // 712 - 719
285
136, 136, 136, 136, 136, 136, 136, 136,
286     // 720 - 727
287
136, 136, 136, 136, 136, 136, 136, 136,
288     // 728 - 735
289
136, 136, 136, 136, 136, 136, 136, 136,
290     // 736 - 743
291
136, 136, 136, 136, 136, 136, 136, 136,
292     // 744 - 751
293
136, 136, 136, 136, 136, 136, 136, 136,
294     // 752 - 759
295
136, 136, 136, 136, 136, 136, 136, 136,
296     // 760 - 767
297
136, 136, 136, 136, 136, 136, 136, 136,
298     // 768 - 775
299
168, 168, 168, 168, 168, 168, 168, 168,
300     // 776 - 783
301
168, 168, 168, 168, 168, 168, 168, 168,
302     // 784 - 791
303
168, 168, 168, 168, 168, 168, 168, 168,
304     // 792 - 799
305
168, 168, 168, 168, 168, 168, 168, 168,
306     // 800 - 807
307
168, 168, 168, 168, 168, 168, 168, 168,
308     // 808 - 815
309
168, 168, 168, 168, 168, 168, 168, 168,
310     // 816 - 823
311
168, 168, 168, 168, 168, 168, 168, 168,
312     // 824 - 831
313
168, 168, 168, 168, 168, 168, 168, 168,
314     // 832 - 839
315
460, 460, 460, 460, 460, 460, 460, 460,
316     // 840 - 847
317
460, 460, 460, 460, 460, 460, 460, 460,
318     // 848 - 855
319
492, 492, 492, 492, 492, 492, 492, 492,
320     // 856 - 863
321
492, 492, 492, 492, 492, 492, 492, 492,
322     // 864 - 871
323
2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
324     // 872 - 879
325
2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
326     // 880 - 887
327
2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
328     // 888 - 895
329
2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
330     // 896 - 903
331
200, 200, 200, 200, 200, 200, 200, 200,
332     // 904 - 911
333
200, 200, 200, 200, 200, 200, 200, 200,
334     // 912 - 919
335
200, 200, 200, 200, 200, 200, 200, 200,
336     // 920 - 927
337
200, 200, 200, 200, 200, 200, 200, 200,
338     // 928 - 935
339
200, 200, 200, 200, 200, 200, 200, 200,
340     // 936 - 943
341
200, 200, 200, 200, 200, 200, 200, 200,
342     // 944 - 951
343
200, 200, 200, 200, 200, 200, 200, 200,
344     // 952 - 959
345
200, 200, 200, 200, 200, 200, 200, 200,
346     // 960 - 967
347
232, 232, 232, 232, 232, 232, 232, 232,
348     // 968 - 975
349
232, 232, 232, 232, 232, 232, 232, 232,
350     // 976 - 983
351
232, 232, 232, 232, 232, 232, 232, 232,
352     // 984 - 991
353
232, 232, 232, 232, 232, 232, 232, 232,
354     // 992 - 999
355
232, 232, 232, 232, 232, 232, 232, 232,
356     // 1000 - 1007
357
232, 232, 232, 232, 232, 232, 232, 232,
358     // 1008 - 1015
359
232, 232, 232, 232, 232, 232, 232, 232,
360     // 1016 - 1023
361
232, 232, 232, 232, 232, 232, 232, 232,
362     };
363     
364     // Additional make up codes for both White and Black runs
365
static short additionalMakeup[] = {
366     28679, 28679, 31752, (short)32777,
367     (short)33801, (short)34825, (short)35849, (short)36873,
368     (short)29703, (short)29703, (short)30727, (short)30727,
369     (short)37897, (short)38921, (short)39945, (short)40969
370     };
371
372     // Initial black run look up table, uses the first 4 bits of a code
373
static short initBlack[] = {
374     // 0 - 7
375
3226, 6412, 200, 168, 38, 38, 134, 134,
376     // 8 - 15
377
100, 100, 100, 100, 68, 68, 68, 68
378     };
379
380     //
381
static short twoBitBlack[] = {292, 260, 226, 226}; // 0 - 3
382

383     // Main black run table, using the last 9 bits of possible 13 bit code
384
static short black[] = {
385     // 0 - 7
386
62, 62, 30, 30, 0, 0, 0, 0,
387     // 8 - 15
388
0, 0, 0, 0, 0, 0, 0, 0,
389     // 16 - 23
390
0, 0, 0, 0, 0, 0, 0, 0,
391     // 24 - 31
392
0, 0, 0, 0, 0, 0, 0, 0,
393     // 32 - 39
394
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
395     // 40 - 47
396
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
397     // 48 - 55
398
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
399     // 56 - 63
400
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
401     // 64 - 71
402
588, 588, 588, 588, 588, 588, 588, 588,
403     // 72 - 79
404
1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776,
405     // 80 - 87
406
1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904,
407     // 88 - 95
408
1936, 1936, -16365, -14317, 782, 782, 782, 782,
409     // 96 - 103
410
814, 814, 814, 814, -12269, -10221, 10257, 10257,
411     // 104 - 111
412
12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712,
413     // 112 - 119
414
1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605,
415     // 120 - 127
416
2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061,
417     // 128 - 135
418
424, 424, 424, 424, 424, 424, 424, 424,
419     // 136 - 143
420
424, 424, 424, 424, 424, 424, 424, 424,
421     // 144 - 151
422
424, 424, 424, 424, 424, 424, 424, 424,
423     // 152 - 159
424
424, 424, 424, 424, 424, 424, 424, 424,
425     // 160 - 167
426
750, 750, 750, 750, 1616, 1616, 1648, 1648,
427     // 168 - 175
428
1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520,
429     // 176 - 183
430
1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209,
431     // 184 - 191
432
524, 524, 524, 524, 524, 524, 524, 524,
433     // 192 - 199
434
556, 556, 556, 556, 556, 556, 556, 556,
435     // 200 - 207
436
1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032,
437     // 208 - 215
438
976, 976, 1008, 1008, 1040, 1040, 1072, 1072,
439     // 216 - 223
440
1296, 1296, 1328, 1328, 718, 718, 718, 718,
441     // 224 - 231
442
456, 456, 456, 456, 456, 456, 456, 456,
443     // 232 - 239
444
456, 456, 456, 456, 456, 456, 456, 456,
445     // 240 - 247
446
456, 456, 456, 456, 456, 456, 456, 456,
447     // 248 - 255
448
456, 456, 456, 456, 456, 456, 456, 456,
449     // 256 - 263
450
326, 326, 326, 326, 326, 326, 326, 326,
451     // 264 - 271
452
326, 326, 326, 326, 326, 326, 326, 326,
453     // 272 - 279
454
326, 326, 326, 326, 326, 326, 326, 326,
455     // 280 - 287
456
326, 326, 326, 326, 326, 326, 326, 326,
457     // 288 - 295
458
326, 326, 326, 326, 326, 326, 326, 326,
459     // 296 - 303
460
326, 326, 326, 326, 326, 326, 326, 326,
461     // 304 - 311
462
326, 326, 326, 326, 326, 326, 326, 326,
463     // 312 - 319
464
326, 326, 326, 326, 326, 326, 326, 326,
465     // 320 - 327
466
358, 358, 358, 358, 358, 358, 358, 358,
467     // 328 - 335
468
358, 358, 358, 358, 358, 358, 358, 358,
469     // 336 - 343
470
358, 358, 358, 358, 358, 358, 358, 358,
471     // 344 - 351
472
358, 358, 358, 358, 358, 358, 358, 358,
473     // 352 - 359
474
358, 358, 358, 358, 358, 358, 358, 358,
475     // 360 - 367
476
358, 358, 358, 358, 358, 358, 358, 358,
477     // 368 - 375
478
358, 358, 358, 358, 358, 358, 358, 358,
479     // 376 - 383
480
358, 358, 358, 358, 358, 358, 358, 358,
481     // 384 - 391
482
490, 490, 490, 490, 490, 490, 490, 490,
483     // 392 - 399
484
490, 490, 490, 490, 490, 490, 490, 490,
485     // 400 - 407
486
4113, 4113, 6161, 6161, 848, 848, 880, 880,
487     // 408 - 415
488
912, 912, 944, 944, 622, 622, 622, 622,
489     // 416 - 423
490
654, 654, 654, 654, 1104, 1104, 1136, 1136,
491     // 424 - 431
492
1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264,
493     // 432 - 439
494
686, 686, 686, 686, 1360, 1360, 1392, 1392,
495     // 440 - 447
496
12, 12, 12, 12, 12, 12, 12, 12,
497     // 448 - 455
498
390, 390, 390, 390, 390, 390, 390, 390,
499     // 456 - 463
500
390, 390, 390, 390, 390, 390, 390, 390,
501     // 464 - 471
502
390, 390, 390, 390, 390, 390, 390, 390,
503     // 472 - 479
504
390, 390, 390, 390, 390, 390, 390, 390,
505     // 480 - 487
506
390, 390, 390, 390, 390, 390, 390, 390,
507     // 488 - 495
508
390, 390, 390, 390, 390, 390, 390, 390,
509     // 496 - 503
510
390, 390, 390, 390, 390, 390, 390, 390,
511     // 504 - 511
512
390, 390, 390, 390, 390, 390, 390, 390,
513     };
514
515     static byte twoDCodes[] = {
516     // 0 - 7
517
80, 88, 23, 71, 30, 30, 62, 62,
518     // 8 - 15
519
4, 4, 4, 4, 4, 4, 4, 4,
520     // 16 - 23
521
11, 11, 11, 11, 11, 11, 11, 11,
522     // 24 - 31
523
11, 11, 11, 11, 11, 11, 11, 11,
524     // 32 - 39
525
35, 35, 35, 35, 35, 35, 35, 35,
526     // 40 - 47
527
35, 35, 35, 35, 35, 35, 35, 35,
528     // 48 - 55
529
51, 51, 51, 51, 51, 51, 51, 51,
530     // 56 - 63
531
51, 51, 51, 51, 51, 51, 51, 51,
532     // 64 - 71
533
41, 41, 41, 41, 41, 41, 41, 41,
534     // 72 - 79
535
41, 41, 41, 41, 41, 41, 41, 41,
536     // 80 - 87
537
41, 41, 41, 41, 41, 41, 41, 41,
538     // 88 - 95
539
41, 41, 41, 41, 41, 41, 41, 41,
540     // 96 - 103
541
41, 41, 41, 41, 41, 41, 41, 41,
542     // 104 - 111
543
41, 41, 41, 41, 41, 41, 41, 41,
544     // 112 - 119
545
41, 41, 41, 41, 41, 41, 41, 41,
546     // 120 - 127
547
41, 41, 41, 41, 41, 41, 41, 41,
548     };
549
550     /**
551      * @param fillOrder The fill order of the compressed data bytes.
552      * @param compData Array containing compressed data.
553      * @param w
554      * @param h
555      */

556     public TIFFFaxDecoder(int fillOrder, int w, int h) {
557     this.fillOrder = fillOrder;
558     this.w = w;
559     this.h = h;
560
561     this.bitPointer = 0;
562     this.bytePointer = 0;
563     this.prevChangingElems = new int[w];
564     this.currChangingElems = new int[w];
565     }
566
567     // One-dimensional decoding methods
568

569     public void decode1D(byte[] buffer, byte[] compData,
570                          int startX, int height) {
571     this.data = compData;
572
573         int lineOffset = 0;
574         int scanlineStride = (w + 7)/8;
575
576     bitPointer = 0;
577     bytePointer = 0;
578     
579     for (int i = 0; i < height; i++) {
580         decodeNextScanline(buffer, lineOffset, startX);
581             lineOffset += scanlineStride;
582     }
583     }
584
585     public void decodeNextScanline(byte[] buffer,
586                                    int lineOffset, int bitOffset) {
587     int bits = 0, code = 0, isT = 0;
588     int current, entry, twoBits;
589     boolean isWhite = true;
590
591     // Initialize starting of the changing elements array
592
changingElemSize = 0;
593
594     // While scanline not complete
595
while (bitOffset < w) {
596         while (isWhite) {
597         // White run
598
current = nextNBits(10);
599         entry = white[current];
600         
601         // Get the 3 fields from the entry
602
isT = entry & 0x0001;
603         bits = (entry >>> 1) & 0x0f;
604         
605         if (bits == 12) { // Additional Make up code
606
// Get the next 2 bits
607
twoBits = nextLesserThan8Bits(2);
608             // Consolidate the 2 new bits and last 2 bits into 4 bits
609
current = ((current << 2) & 0x000c) | twoBits;
610             entry = additionalMakeup[current];
611             bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
612
code = (entry >>> 4) & 0x0fff; // 12 bits
613
bitOffset += code; // Skip white run
614

615             updatePointer(4 - bits);
616         } else if (bits == 0) { // ERROR
617
throw new Error JavaDoc("TIFFFaxDecoder0");
618         } else if (bits == 15) { // EOL
619
throw new Error JavaDoc("TIFFFaxDecoder1");
620         } else {
621             // 11 bits - 0000 0111 1111 1111 = 0x07ff
622
code = (entry >>> 5) & 0x07ff;
623                     bitOffset += code;
624
625             updatePointer(10 - bits);
626             if (isT == 0) {
627             isWhite = false;
628             currChangingElems[changingElemSize++] = bitOffset;
629             }
630         }
631         }
632
633         // Check whether this run completed one width, if so
634
// advance to next byte boundary for compression = 2.
635
if (bitOffset == w) {
636         if (compression == 2) {
637             advancePointer();
638         }
639         break;
640         }
641
642         while (isWhite == false) {
643         // Black run
644
current = nextLesserThan8Bits(4);
645         entry = initBlack[current];
646         
647         // Get the 3 fields from the entry
648
isT = entry & 0x0001;
649         bits = (entry >>> 1) & 0x000f;
650         code = (entry >>> 5) & 0x07ff;
651
652         if (code == 100) {
653             current = nextNBits(9);
654             entry = black[current];
655             
656             // Get the 3 fields from the entry
657
isT = entry & 0x0001;
658             bits = (entry >>> 1) & 0x000f;
659             code = (entry >>> 5) & 0x07ff;
660
661             if (bits == 12) {
662             // Additional makeup codes
663
updatePointer(5);
664             current = nextLesserThan8Bits(4);
665             entry = additionalMakeup[current];
666             bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
667
code = (entry >>> 4) & 0x0fff; // 12 bits
668

669                         setToBlack(buffer, lineOffset, bitOffset, code);
670                         bitOffset += code;
671
672             updatePointer(4 - bits);
673             } else if (bits == 15) {
674             // EOL code
675
throw new Error JavaDoc("TIFFFaxDecoder2");
676             } else {
677                         setToBlack(buffer, lineOffset, bitOffset, code);
678                         bitOffset += code;
679
680             updatePointer(9 - bits);
681             if (isT == 0) {
682                 isWhite = true;
683                 currChangingElems[changingElemSize++] = bitOffset;
684             }
685             }
686         } else if (code == 200) {
687             // Is a Terminating code
688
current = nextLesserThan8Bits(2);
689             entry = twoBitBlack[current];
690             code = (entry >>> 5) & 0x07ff;
691             bits = (entry >>> 1) & 0x0f;
692
693                     setToBlack(buffer, lineOffset, bitOffset, code);
694                     bitOffset += code;
695
696             updatePointer(2 - bits);
697             isWhite = true;
698             currChangingElems[changingElemSize++] = bitOffset;
699         } else {
700             // Is a Terminating code
701
setToBlack(buffer, lineOffset, bitOffset, code);
702                     bitOffset += code;
703
704             updatePointer(4 - bits);
705             isWhite = true;
706             currChangingElems[changingElemSize++] = bitOffset;
707         }
708         }
709
710         // Check whether this run completed one width
711
if (bitOffset == w) {
712         if (compression == 2) {
713             advancePointer();
714         }
715         break;
716         }
717     }
718
719     currChangingElems[changingElemSize++] = bitOffset;
720     }
721
722     // Two-dimensional decoding methods
723

724     public void decode2D(byte[] buffer,
725                          byte compData[],
726                          int startX,
727                          int height,
728                          long tiffT4Options) {
729     this.data = compData;
730     compression = 3;
731
732     bitPointer = 0;
733     bytePointer = 0;
734
735         int scanlineStride = (w + 7)/8;
736
737     int a0, a1, b1, b2;
738         int[] b = new int[2];
739     int entry, code, bits;
740     boolean isWhite;
741     int currIndex = 0;
742     int temp[];
743     
744     // fillBits - dealt with this in readEOL
745
// 1D/2D encoding - dealt with this in readEOL
746

747     // uncompressedMode - haven't dealt with this yet.
748

749
750     oneD = (int)(tiffT4Options & 0x01);
751     uncompressedMode = (int)((tiffT4Options & 0x02) >> 1);
752     fillBits = (int)((tiffT4Options & 0x04) >> 2);
753
754     // The data must start with an EOL code
755
if (readEOL() != 1) {
756         throw new Error JavaDoc("TIFFFaxDecoder3");
757     }
758
759         int lineOffset = 0;
760         int bitOffset;
761
762     // Then the 1D encoded scanline data will occur, changing elements
763
// array gets set.
764
decodeNextScanline(buffer, lineOffset, startX);
765         lineOffset += scanlineStride;
766
767     for (int lines = 1; lines < height; lines++) {
768
769             // Every line must begin with an EOL followed by a bit which
770
// indicates whether the following scanline is 1D or 2D encoded.
771
if (readEOL() == 0) {
772         // 2D encoded scanline follows
773

774         // Initialize previous scanlines changing elements, and
775
// initialize current scanline's changing elements array
776
temp = prevChangingElems;
777         prevChangingElems = currChangingElems;
778         currChangingElems = temp;
779         currIndex = 0;
780                 
781         // a0 has to be set just before the start of this scanline.
782
a0 = -1;
783         isWhite = true;
784         bitOffset = startX;
785
786                 lastChangingElement = 0;
787
788         while (bitOffset < w) {
789             // Get the next changing element
790
getNextChangingElement(a0, isWhite, b);
791             
792             b1 = b[0];
793             b2 = b[1];
794                     
795             // Get the next seven bits
796
entry = nextLesserThan8Bits(7);
797                     
798             // Run these through the 2DCodes table
799
entry = (int)(twoDCodes[entry] & 0xff);
800                     
801             // Get the code and the number of bits used up
802
code = (entry & 0x78) >>> 3;
803             bits = entry & 0x07;
804                     
805             if (code == 0) {
806                         if (!isWhite) {
807                             setToBlack(buffer, lineOffset, bitOffset,
808                                        b2 - bitOffset);
809                         }
810             bitOffset = a0 = b2;
811                         
812             // Set pointer to consume the correct number of bits.
813
updatePointer(7 - bits);
814             } else if (code == 1) {
815             // Horizontal
816
updatePointer(7 - bits);
817                         
818             // identify the next 2 codes.
819
int number;
820             if (isWhite) {
821                 number = decodeWhiteCodeWord();
822                             bitOffset += number;
823                 currChangingElems[currIndex++] = bitOffset;
824                             
825                 number = decodeBlackCodeWord();
826                             setToBlack(buffer, lineOffset, bitOffset, number);
827                 bitOffset += number;
828                 currChangingElems[currIndex++] = bitOffset;
829             } else {
830                 number = decodeBlackCodeWord();
831                             setToBlack(buffer, lineOffset, bitOffset, number);
832                             bitOffset += number;
833                 currChangingElems[currIndex++] = bitOffset;
834                             
835                 number = decodeWhiteCodeWord();
836                 bitOffset += number;
837                 currChangingElems[currIndex++] = bitOffset;
838             }
839                         
840             a0 = bitOffset;
841             } else if (code <= 8) {
842             // Vertical
843
a1 = b1 + (code - 5);
844                         
845             currChangingElems[currIndex++] = a1;
846                         
847             // We write the current color till a1 - 1 pos,
848
// since a1 is where the next color starts
849
if (!isWhite) {
850                             setToBlack(buffer, lineOffset, bitOffset,
851                                        a1 - bitOffset);
852                         }
853                         bitOffset = a0 = a1;
854                         isWhite = !isWhite;
855                             
856                         updatePointer(7 - bits);
857             } else {
858             throw new Error JavaDoc("TIFFFaxDecoder4");
859             }
860         }
861                 
862         // Add the changing element beyond the current scanline for the
863
// other color too
864
currChangingElems[currIndex++] = bitOffset;
865         changingElemSize = currIndex;
866         } else {
867         // 1D encoded scanline follows
868
decodeNextScanline(buffer, lineOffset, startX);
869         }
870
871             lineOffset += scanlineStride;
872         }
873     }
874
875     public synchronized void decodeT6(byte[] buffer,
876                                       byte[] compData,
877                                       int startX,
878                                       int height,
879                                       long tiffT6Options) {
880     this.data = compData;
881     compression = 4;
882
883     bitPointer = 0;
884     bytePointer = 0;
885
886         int scanlineStride = (w + 7)/8;
887
888     int a0, a1, b1, b2;
889     int entry, code, bits;
890     boolean isWhite;
891     int currIndex;
892     int temp[];
893
894         // Return values from getNextChangingElement
895
int[] b = new int[2];
896     
897     // uncompressedMode - have written some code for this, but this
898
// has not been tested due to lack of test images using this optional
899

900     uncompressedMode = (int)((tiffT6Options & 0x02) >> 1);
901
902         // Local cached reference
903
int[] cce = currChangingElems;
904
905     // Assume invisible preceding row of all white pixels and insert
906
// both black and white changing elements beyond the end of this
907
// imaginary scanline.
908
changingElemSize = 0;
909     cce[changingElemSize++] = w;
910     cce[changingElemSize++] = w;
911
912         int lineOffset = 0;
913         int bitOffset;
914
915     for (int lines = 0; lines < height; lines++) {
916         // a0 has to be set just before the start of the scanline.
917
a0 = -1;
918         isWhite = true;
919     
920         // Assign the changing elements of the previous scanline to
921
// prevChangingElems and start putting this new scanline's
922
// changing elements into the currChangingElems.
923
temp = prevChangingElems;
924         prevChangingElems = currChangingElems;
925         cce = currChangingElems = temp;
926         currIndex = 0;
927
928         // Start decoding the scanline at startX in the raster
929
bitOffset = startX;
930
931             // Reset search start position for getNextChangingElement
932
lastChangingElement = 0;
933
934         // Till one whole scanline is decoded
935
while (bitOffset < w) {
936         // Get the next changing element
937
getNextChangingElement(a0, isWhite, b);
938         b1 = b[0];
939         b2 = b[1];
940
941         // Get the next seven bits
942
entry = nextLesserThan8Bits(7);
943         // Run these through the 2DCodes table
944
entry = (int)(twoDCodes[entry] & 0xff);
945
946         // Get the code and the number of bits used up
947
code = (entry & 0x78) >>> 3;
948         bits = entry & 0x07;
949                 
950         if (code == 0) { // Pass
951
// We always assume WhiteIsZero format for fax.
952
if (!isWhite) {
953                         setToBlack(buffer, lineOffset, bitOffset,
954                                    b2 - bitOffset);
955                     }
956                     bitOffset = a0 = b2;
957
958             // Set pointer to only consume the correct number of bits.
959
updatePointer(7 - bits);
960         } else if (code == 1) { // Horizontal
961
// Set pointer to only consume the correct number of bits.
962
updatePointer(7 - bits);
963             
964             // identify the next 2 alternating color codes.
965
int number;
966             if (isWhite) {
967             // Following are white and black runs
968
number = decodeWhiteCodeWord();
969                         bitOffset += number;
970             cce[currIndex++] = bitOffset;
971
972             number = decodeBlackCodeWord();
973                         setToBlack(buffer, lineOffset, bitOffset, number);
974                         bitOffset += number;
975             cce[currIndex++] = bitOffset;
976             } else {
977             // First a black run and then a white run follows
978
number = decodeBlackCodeWord();
979                         setToBlack(buffer, lineOffset, bitOffset, number);
980                         bitOffset += number;
981             cce[currIndex++] = bitOffset;
982
983             number = decodeWhiteCodeWord();
984                         bitOffset += number;
985             cce[currIndex++] = bitOffset;
986             }
987             
988             a0 = bitOffset;
989         } else if (code <= 8) { // Vertical
990
a1 = b1 + (code - 5);
991             cce[currIndex++] = a1;
992
993             // We write the current color till a1 - 1 pos,
994
// since a1 is where the next color starts
995
if (!isWhite) {
996                         setToBlack(buffer, lineOffset, bitOffset,
997                                    a1 - bitOffset);
998                     }
999                     bitOffset = a0 = a1;
1000                    isWhite = !isWhite;
1001
1002            updatePointer(7 - bits);
1003        } else if (code == 11) {
1004            if (nextLesserThan8Bits(3) != 7) {
1005            throw new Error JavaDoc("TIFFFaxDecoder5");
1006            }
1007
1008            int zeros = 0;
1009            boolean exit = false;
1010
1011            while (!exit) {
1012            while (nextLesserThan8Bits(1) != 1) {
1013                zeros++;
1014            }
1015            
1016            if (zeros > 5) {
1017                // Exit code
1018

1019                // Zeros before exit code
1020
zeros = zeros - 6;
1021
1022                if (!isWhite && (zeros > 0)) {
1023                cce[currIndex++] = bitOffset;
1024                }
1025                
1026                // Zeros before the exit code
1027
bitOffset += zeros;
1028                if (zeros > 0) {
1029                // Some zeros have been written
1030
isWhite = true;
1031                }
1032                            
1033                // Read in the bit which specifies the color of
1034
// the following run
1035
if (nextLesserThan8Bits(1) == 0) {
1036                if (!isWhite) {
1037                    cce[currIndex++] = bitOffset;
1038                }
1039                isWhite = true;
1040                } else {
1041                if (isWhite) {
1042                    cce[currIndex++] = bitOffset;
1043                }
1044                isWhite = false;
1045                }
1046
1047                exit = true;
1048            }
1049
1050                        if (zeros == 5) {
1051                if (!isWhite) {
1052                    cce[currIndex++] = bitOffset;
1053                }
1054                            bitOffset += zeros;
1055
1056                // Last thing written was white
1057
isWhite = true;
1058            } else {
1059                            bitOffset += zeros;
1060
1061                cce[currIndex++] = bitOffset;
1062                            setToBlack(buffer, lineOffset, bitOffset, 1);
1063                            ++bitOffset;
1064
1065                // Last thing written was black
1066
isWhite = false;
1067            }
1068
1069            }
1070        } else {
1071            throw new Error JavaDoc("TIFFFaxDecoder5");
1072        }
1073        }
1074        
1075        // Add the changing element beyond the current scanline for the
1076
// other color too
1077
cce[currIndex++] = bitOffset;
1078
1079        // Number of changing elements in this scanline.
1080
changingElemSize = currIndex;
1081
1082            lineOffset += scanlineStride;
1083    }
1084    }
1085
1086    private void setToBlack(byte[] buffer,
1087                            int lineOffset, int bitOffset,
1088                            int numBits) {
1089        int bitNum = 8*lineOffset + bitOffset;
1090        int lastBit = bitNum + numBits;
1091
1092        int byteNum = bitNum >> 3;
1093
1094        // Handle bits in first byte
1095
int shift = bitNum & 0x7;
1096        if (shift > 0) {
1097            int maskVal = 1 << (7 - shift);
1098            byte val = buffer[byteNum];
1099            while (maskVal > 0 && bitNum < lastBit) {
1100                val |= maskVal;
1101                maskVal >>= 1;
1102                ++bitNum;
1103            }
1104            buffer[byteNum] = val;
1105        }
1106
1107        // Fill in 8 bits at a time
1108
byteNum = bitNum >> 3;
1109        while (bitNum < lastBit - 7) {
1110            buffer[byteNum++] = (byte)255;
1111            bitNum += 8;
1112        }
1113
1114        // Fill in remaining bits
1115
while (bitNum < lastBit) {
1116            byteNum = bitNum >> 3;
1117            buffer[byteNum] |= 1 << (7 - (bitNum & 0x7));
1118            ++bitNum;
1119        }
1120    }
1121
1122    // Returns run length
1123
private int decodeWhiteCodeWord() {
1124    int current, entry, bits, isT, twoBits, code = -1;
1125        int runLength = 0;
1126    boolean isWhite = true;
1127
1128    while (isWhite) {
1129        current = nextNBits(10);
1130        entry = white[current];
1131        
1132        // Get the 3 fields from the entry
1133
isT = entry & 0x0001;
1134        bits = (entry >>> 1) & 0x0f;
1135        
1136        if (bits == 12) { // Additional Make up code
1137
// Get the next 2 bits
1138
twoBits = nextLesserThan8Bits(2);
1139        // Consolidate the 2 new bits and last 2 bits into 4 bits
1140
current = ((current << 2) & 0x000c) | twoBits;
1141        entry = additionalMakeup[current];
1142        bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1143
code = (entry >>> 4) & 0x0fff; // 12 bits
1144
runLength += code;
1145        updatePointer(4 - bits);
1146        } else if (bits == 0) { // ERROR
1147
throw new Error JavaDoc("TIFFFaxDecoder0");
1148        } else if (bits == 15) { // EOL
1149
throw new Error JavaDoc("TIFFFaxDecoder1");
1150        } else {
1151        // 11 bits - 0000 0111 1111 1111 = 0x07ff
1152
code = (entry >>> 5) & 0x07ff;
1153                runLength += code;
1154        updatePointer(10 - bits);
1155        if (isT == 0) {
1156            isWhite = false;
1157        }
1158        }
1159    }
1160
1161    return runLength;
1162    }
1163
1164    // Returns run length
1165
private int decodeBlackCodeWord() {
1166    int current, entry, bits, isT, code = -1;
1167        int runLength = 0;
1168    boolean isWhite = false;
1169    
1170    while (!isWhite) {
1171        current = nextLesserThan8Bits(4);
1172        entry = initBlack[current];
1173        
1174        // Get the 3 fields from the entry
1175
isT = entry & 0x0001;
1176        bits = (entry >>> 1) & 0x000f;
1177        code = (entry >>> 5) & 0x07ff;
1178        
1179        if (code == 100) {
1180        current = nextNBits(9);
1181        entry = black[current];
1182        
1183        // Get the 3 fields from the entry
1184
isT = entry & 0x0001;
1185        bits = (entry >>> 1) & 0x000f;
1186        code = (entry >>> 5) & 0x07ff;
1187        
1188        if (bits == 12) {
1189            // Additional makeup codes
1190
updatePointer(5);
1191            current = nextLesserThan8Bits(4);
1192            entry = additionalMakeup[current];
1193            bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1194
code = (entry >>> 4) & 0x0fff; // 12 bits
1195
runLength += code;
1196            
1197            updatePointer(4 - bits);
1198        } else if (bits == 15) {
1199            // EOL code
1200
throw new Error JavaDoc("TIFFFaxDecoder2");
1201        } else {
1202                    runLength += code;
1203            updatePointer(9 - bits);
1204            if (isT == 0) {
1205            isWhite = true;
1206            }
1207        }
1208        } else if (code == 200) {
1209        // Is a Terminating code
1210
current = nextLesserThan8Bits(2);
1211        entry = twoBitBlack[current];
1212        code = (entry >>> 5) & 0x07ff;
1213                runLength += code;
1214        bits = (entry >>> 1) & 0x0f;
1215        updatePointer(2 - bits);
1216        isWhite = true;
1217        } else {
1218        // Is a Terminating code
1219
runLength += code;
1220        updatePointer(4 - bits);
1221        isWhite = true;
1222        }
1223    }
1224
1225    return runLength;
1226    }
1227
1228    private int readEOL() {
1229    if (fillBits == 0) {
1230        if (nextNBits(12) != 1) {
1231        throw new Error JavaDoc("TIFFFaxDecoder6");
1232        }
1233    } else if (fillBits == 1) {
1234
1235        // First EOL code word xxxx 0000 0000 0001 will occur
1236
// As many fill bits will be present as required to make
1237
// the EOL code of 12 bits end on a byte boundary.
1238

1239        int bitsLeft = 8 - bitPointer;
1240
1241        if (nextNBits(bitsLeft) != 0) {
1242            throw new Error JavaDoc("TIFFFaxDecoder8");
1243        }
1244
1245        // If the number of bitsLeft is less than 8, then to have a 12
1246
// bit EOL sequence, two more bytes are certainly going to be
1247
// required. The first of them has to be all zeros, so ensure
1248
// that.
1249
if (bitsLeft < 4) {
1250        if (nextNBits(8) != 0) {
1251            throw new Error JavaDoc("TIFFFaxDecoder8");
1252        }
1253        }
1254
1255        // There might be a random number of fill bytes with 0s, so
1256
// loop till the EOL of 0000 0001 is found, as long as all
1257
// the bytes preceding it are 0's.
1258
int n;
1259        while ((n = nextNBits(8)) != 1) {
1260        
1261        // If not all zeros
1262
if (n != 0) {
1263            throw new Error JavaDoc("TIFFFaxDecoder8");
1264        }
1265        }
1266    }
1267
1268    // If one dimensional encoding mode, then always return 1
1269
if (oneD == 0) {
1270        return 1;
1271    } else {
1272        // Otherwise for 2D encoding mode,
1273
// The next one bit signifies 1D/2D encoding of next line.
1274
return nextLesserThan8Bits(1);
1275    }
1276    }
1277
1278    private void getNextChangingElement(int a0, boolean isWhite, int[] ret) {
1279        // Local copies of instance variables
1280
int[] pce = this.prevChangingElems;
1281        int ces = this.changingElemSize;
1282
1283        // If the previous match was at an odd element, we still
1284
// have to search the preceeding element.
1285
// int start = lastChangingElement & ~0x1;
1286
int start = lastChangingElement > 0 ? lastChangingElement - 1 : 0;
1287        if (isWhite) {
1288            start &= ~0x1; // Search even numbered elements
1289
} else {
1290            start |= 0x1; // Search odd numbered elements
1291
}
1292
1293        int i = start;
1294        for (; i < ces; i += 2) {
1295            int temp = pce[i];
1296            if (temp > a0) {
1297                lastChangingElement = i;
1298                ret[0] = temp;
1299                break;
1300            }
1301        }
1302
1303        if (i + 1 < ces) {
1304            ret[1] = pce[i + 1];
1305        }
1306    }
1307
1308    private int nextNBits(int bitsToGet) {
1309    byte b, next, next2next;
1310    int l = data.length - 1;
1311        int bp = this.bytePointer;
1312        
1313    if (fillOrder == 1) {
1314        b = data[bp];
1315        
1316        if (bp == l) {
1317        next = 0x00;
1318        next2next = 0x00;
1319        } else if ((bp + 1) == l) {
1320        next = data[bp + 1];
1321        next2next = 0x00;
1322        } else {
1323        next = data[bp + 1];
1324        next2next = data[bp + 2];
1325        }
1326    } else if (fillOrder == 2) {
1327        b = flipTable[data[bp] & 0xff];
1328
1329        if (bp == l) {
1330        next = 0x00;
1331        next2next = 0x00;
1332        } else if ((bp + 1) == l) {
1333        next = flipTable[data[bp + 1] & 0xff];
1334        next2next = 0x00;
1335        } else {
1336        next = flipTable[data[bp + 1] & 0xff];
1337        next2next = flipTable[data[bp + 2] & 0xff];
1338        }
1339    } else {
1340        throw new Error JavaDoc("TIFFFaxDecoder7");
1341    }
1342
1343    int bitsLeft = 8 - bitPointer;
1344    int bitsFromNextByte = bitsToGet - bitsLeft;
1345    int bitsFromNext2NextByte = 0;
1346    if (bitsFromNextByte > 8) {
1347        bitsFromNext2NextByte = bitsFromNextByte - 8;
1348        bitsFromNextByte = 8;
1349    }
1350    
1351    bytePointer++;
1352    
1353    int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft);
1354    int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1355    
1356    int i3 = 0;
1357    if (bitsFromNext2NextByte != 0) {
1358        i2 <<= bitsFromNext2NextByte;
1359        i3 = (next2next & table2[bitsFromNext2NextByte]) >>>
1360        (8 - bitsFromNext2NextByte);
1361        i2 |= i3;
1362        bytePointer++;
1363        bitPointer = bitsFromNext2NextByte;
1364    } else {
1365        if (bitsFromNextByte == 8) {
1366        bitPointer = 0;
1367        bytePointer++;
1368        } else {
1369        bitPointer = bitsFromNextByte;
1370        }
1371    }
1372    
1373    int i = i1 | i2;
1374    return i;
1375    }
1376
1377    private int nextLesserThan8Bits(int bitsToGet) {
1378    byte b, next;
1379    int l = data.length - 1;
1380        int bp = this.bytePointer;
1381
1382    if (fillOrder == 1) {
1383        b = data[bp];
1384        if (bp == l) {
1385        next = 0x00;
1386        } else {
1387        next = data[bp + 1];
1388        }
1389    } else if (fillOrder == 2) {
1390        b = flipTable[data[bp] & 0xff];
1391        if (bp == l) {
1392        next = 0x00;
1393        } else {
1394        next = flipTable[data[bp + 1] & 0xff];
1395        }
1396    } else {
1397        throw new Error JavaDoc("TIFFFaxDecoder7");
1398    }
1399
1400    int bitsLeft = 8 - bitPointer;
1401    int bitsFromNextByte = bitsToGet - bitsLeft;
1402
1403    int shift = bitsLeft - bitsToGet;
1404    int i1, i2;
1405    if (shift >= 0) {
1406        i1 = (b & table1[bitsLeft]) >>> shift;
1407        bitPointer += bitsToGet;
1408        if (bitPointer == 8) {
1409        bitPointer = 0;
1410        bytePointer++;
1411        }
1412    } else {
1413        i1 = (b & table1[bitsLeft]) << (-shift);
1414        i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1415
1416        i1 |= i2;
1417        bytePointer++;
1418        bitPointer = bitsFromNextByte;
1419    }
1420
1421    return i1;
1422    }
1423
1424    // Move pointer backwards by given amount of bits
1425
private void updatePointer(int bitsToMoveBack) {
1426    int i = bitPointer - bitsToMoveBack;
1427    
1428    if (i < 0) {
1429        bytePointer--;
1430        bitPointer = 8 + i;
1431    } else {
1432        bitPointer = i;
1433    }
1434    }
1435    
1436    // Move to the next byte boundary
1437
private boolean advancePointer() {
1438    if (bitPointer != 0) {
1439        bytePointer++;
1440        bitPointer = 0;
1441    }
1442    
1443    return true;
1444    }
1445}
1446
1447
Popular Tags