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<