KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > TIFFFaxDecoder


1 /*
2  * Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * -Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * -Redistribution in binary form must reproduct the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
15  * be used to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * This software is provided "AS IS," without a warranty of any kind. ALL
19  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
20  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
22  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
23  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
24  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
25  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
26  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
27  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * You acknowledge that Software is not designed,licensed or intended for use in
31  * the design, construction, operation or maintenance of any nuclear facility.
32  */

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

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

569     public TIFFFaxDecoder(int fillOrder, int w, int h) {
570         this.fillOrder = fillOrder;
571         this.w = w;
572         this.h = h;
573         
574         this.bitPointer = 0;
575         this.bytePointer = 0;
576         this.prevChangingElems = new int[w];
577         this.currChangingElems = new int[w];
578     }
579     
580     // One-dimensional decoding methods
581

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

628                     updatePointer(4 - bits);
629                 } else if (bits == 0) { // ERROR
630
throw new RuntimeException JavaDoc("Invalid code encountered.");
631                 } else if (bits == 15) { // EOL
632
throw new RuntimeException JavaDoc("EOL code word encountered in White run.");
633                 } else {
634                     // 11 bits - 0000 0111 1111 1111 = 0x07ff
635
code = (entry >>> 5) & 0x07ff;
636                     bitOffset += code;
637                     
638                     updatePointer(10 - bits);
639                     if (isT == 0) {
640                         isWhite = false;
641                         currChangingElems[changingElemSize++] = bitOffset;
642                     }
643                 }
644             }
645             
646             // Check whether this run completed one width, if so
647
// advance to next byte boundary for compression = 2.
648
if (bitOffset == w) {
649                 if (compression == 2) {
650                     advancePointer();
651                 }
652                 break;
653             }
654             
655             while (!isWhite) {
656                 // Black run
657
current = nextLesserThan8Bits(4);
658                 entry = initBlack[current];
659                 
660                 // Get the 3 fields from the entry
661
isT = entry & 0x0001;
662                 bits = (entry >>> 1) & 0x000f;
663                 code = (entry >>> 5) & 0x07ff;
664                 
665                 if (code == 100) {
666                     current = nextNBits(9);
667                     entry = black[current];
668                     
669                     // Get the 3 fields from the entry
670
isT = entry & 0x0001;
671                     bits = (entry >>> 1) & 0x000f;
672                     code = (entry >>> 5) & 0x07ff;
673                     
674                     if (bits == 12) {
675                         // Additional makeup codes
676
updatePointer(5);
677                         current = nextLesserThan8Bits(4);
678                         entry = additionalMakeup[current];
679                         bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
680
code = (entry >>> 4) & 0x0fff; // 12 bits
681

682                         setToBlack(buffer, lineOffset, bitOffset, code);
683                         bitOffset += code;
684                         
685                         updatePointer(4 - bits);
686                     } else if (bits == 15) {
687                         // EOL code
688
throw new RuntimeException JavaDoc("EOL code word encountered in Black run.");
689                     } else {
690                         setToBlack(buffer, lineOffset, bitOffset, code);
691                         bitOffset += code;
692                         
693                         updatePointer(9 - bits);
694                         if (isT == 0) {
695                             isWhite = true;
696                             currChangingElems[changingElemSize++] = bitOffset;
697                         }
698                     }
699                 } else if (code == 200) {
700                     // Is a Terminating code
701
current = nextLesserThan8Bits(2);
702                     entry = twoBitBlack[current];
703                     code = (entry >>> 5) & 0x07ff;
704                     bits = (entry >>> 1) & 0x0f;
705                     
706                     setToBlack(buffer, lineOffset, bitOffset, code);
707                     bitOffset += code;
708                     
709                     updatePointer(2 - bits);
710                     isWhite = true;
711                     currChangingElems[changingElemSize++] = bitOffset;
712                 } else {
713                     // Is a Terminating code
714
setToBlack(buffer, lineOffset, bitOffset, code);
715                     bitOffset += code;
716                     
717                     updatePointer(4 - bits);
718                     isWhite = true;
719                     currChangingElems[changingElemSize++] = bitOffset;
720                 }
721             }
722             
723             // Check whether this run completed one width
724
if (bitOffset == w) {
725                 if (compression == 2) {
726                     advancePointer();
727                 }
728                 break;
729             }
730         }
731         
732         currChangingElems[changingElemSize++] = bitOffset;
733     }
734     
735     // Two-dimensional decoding methods
736

737     public void decode2D(byte[] buffer,
738     byte compData[],
739     int startX,
740     int height,
741     long tiffT4Options) {
742         this.data = compData;
743         compression = 3;
744         
745         bitPointer = 0;
746         bytePointer = 0;
747         
748         int scanlineStride = (w + 7)/8;
749         
750         int a0, a1, b1, b2;
751         int[] b = new int[2];
752         int entry, code, bits;
753         boolean isWhite;
754         int currIndex = 0;
755         int temp[];
756         
757         // fillBits - dealt with this in readEOL
758
// 1D/2D encoding - dealt with this in readEOL
759

760         // uncompressedMode - haven't dealt with this yet.
761

762         
763         oneD = (int)(tiffT4Options & 0x01);
764         uncompressedMode = (int)((tiffT4Options & 0x02) >> 1);
765         fillBits = (int)((tiffT4Options & 0x04) >> 2);
766         
767         // The data must start with an EOL code
768
if (readEOL(true) != 1) {
769             throw new RuntimeException JavaDoc("First scanline must be 1D encoded.");
770         }
771         
772         int lineOffset = 0;
773         int bitOffset;
774         
775         // Then the 1D encoded scanline data will occur, changing elements
776
// array gets set.
777
decodeNextScanline(buffer, lineOffset, startX);
778         lineOffset += scanlineStride;
779         
780         for (int lines = 1; lines < height; lines++) {
781             
782             // Every line must begin with an EOL followed by a bit which
783
// indicates whether the following scanline is 1D or 2D encoded.
784
if (readEOL(false) == 0) {
785                 // 2D encoded scanline follows
786

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

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

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

1253                if(nextNBits(4) == 1) {
1254                    
1255                    // EOL must be padded: reset the fillBits flag.
1256

1257                    fillBits = 1;
1258                    return 1;
1259                }
1260            }
1261            if(next12Bits != 1) {
1262                throw new RuntimeException JavaDoc("Scanline must begin with EOL code word.");
1263            }
1264        } else if (fillBits == 1) {
1265            
1266            // First EOL code word xxxx 0000 0000 0001 will occur
1267
// As many fill bits will be present as required to make
1268
// the EOL code of 12 bits end on a byte boundary.
1269

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