1 /*
2 * Copyright 2003-2009 by Paulo Soares.
3 *
4 * This code was originally released in 2001 by SUN (see class
5 * com.sun.media.imageioimpl.plugins.tiff.TIFFFaxDecompressor.java)
6 * using the BSD license in a specific wording. In a mail dating from
7 * January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
8 * to use the code under the following version of the BSD license:
9 *
10 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * - Redistribution of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistribution in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Sun Microsystems, Inc. or the names of
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * This software is provided "AS IS," without a warranty of any
29 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
30 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
32 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
33 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
34 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
35 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
36 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
37 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
38 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
39 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGES.
41 *
42 * You acknowledge that this software is not designed or intended for
43 * use in the design, construction, operation or maintenance of any
44 * nuclear facility.
45 */
46 package com.lowagie.text.pdf.codec;
47
48 public class TIFFFaxDecoder {
49
50 private int bitPointer, bytePointer;
51 private byte[] data;
52 private int w, h;
53 private int fillOrder;
54
55 // Data structures needed to store changing elements for the previous
56 // and the current scanline
57 private int changingElemSize = 0;
58 private int prevChangingElems[];
59 private int currChangingElems[];
60
61 // Element at which to start search in getNextChangingElement
62 private int lastChangingElement = 0;
63
64 private int compression = 2;
65
66 // Variables set by T4Options
67 private int uncompressedMode = 0;
68 private int fillBits = 0;
69 private int oneD;
70
71 static int table1[] = {
72 0x00, // 0 bits are left in first byte - SHOULD NOT HAPPEN
73 0x01, // 1 bits are left in first byte
74 0x03, // 2 bits are left in first byte
75 0x07, // 3 bits are left in first byte
76 0x0f, // 4 bits are left in first byte
77 0x1f, // 5 bits are left in first byte
78 0x3f, // 6 bits are left in first byte
79 0x7f, // 7 bits are left in first byte
80 0xff // 8 bits are left in first byte
81 };
82
83 static int table2[] = {
84 0x00, // 0
85 0x80, // 1
86 0xc0, // 2
87 0xe0, // 3
88 0xf0, // 4
89 0xf8, // 5
90 0xfc, // 6
91 0xfe, // 7
92 0xff // 8
93 };
94
95 // Table to be used when fillOrder = 2, for flipping bytes.
96 static byte flipTable[] = {
97 0, -128, 64, -64, 32, -96, 96, -32,
98 16, -112, 80, -48, 48, -80, 112, -16,
99 8, -120, 72, -56, 40, -88, 104, -24,
100 24, -104, 88, -40, 56, -72, 120, -8,
101 4, -124, 68, -60, 36, -92, 100, -28,
102 20, -108, 84, -44, 52, -76, 116, -12,
103 12, -116, 76, -52, 44, -84, 108, -20,
104 28, -100, 92, -36, 60, -68, 124, -4,
105 2, -126, 66, -62, 34, -94, 98, -30,
106 18, -110, 82, -46, 50, -78, 114, -14,
107 10, -118, 74, -54, 42, -86, 106, -22,
108 26, -102, 90, -38, 58, -70, 122, -6,
109 6, -122, 70, -58, 38, -90, 102, -26,
110 22, -106, 86, -42, 54, -74, 118, -10,
111 14, -114, 78, -50, 46, -82, 110, -18,
112 30, -98, 94, -34, 62, -66, 126, -2,
113 1, -127, 65, -63, 33, -95, 97, -31,
114 17, -111, 81, -47, 49, -79, 113, -15,
115 9, -119, 73, -55, 41, -87, 105, -23,
116 25, -103, 89, -39, 57, -71, 121, -7,
117 5, -123, 69, -59, 37, -91, 101, -27,
118 21, -107, 85, -43, 53, -75, 117, -11,
119 13, -115, 77, -51, 45, -83, 109, -19,
120 29, -99, 93, -35, 61, -67, 125, -3,
121 3, -125, 67, -61, 35, -93, 99, -29,
122 19, -109, 83, -45, 51, -77, 115, -13,
123 11, -117, 75, -53, 43, -85, 107, -21,
124 27, -101, 91, -37, 59, -69, 123, -5,
125 7, -121, 71, -57, 39, -89, 103, -25,
126 23, -105, 87, -41, 55, -73, 119, -9,
127 15, -113, 79, -49, 47, -81, 111, -17,
128 31, -97, 95, -33, 63, -65, 127, -1,
129 };
130
131 // The main 10 bit white runs lookup table
132 static short white[] = {
133 // 0 - 7
134 6430, 6400, 6400, 6400, 3225, 3225, 3225, 3225,
135 // 8 - 15
136 944, 944, 944, 944, 976, 976, 976, 976,
137 // 16 - 23
138 1456, 1456, 1456, 1456, 1488, 1488, 1488, 1488,
139 // 24 - 31
140 718, 718, 718, 718, 718, 718, 718, 718,
141 // 32 - 39
142 750, 750, 750, 750, 750, 750, 750, 750,
143 // 40 - 47
144 1520, 1520, 1520, 1520, 1552, 1552, 1552, 1552,
145 // 48 - 55
146 428, 428, 428, 428, 428, 428, 428, 428,
147 // 56 - 63
148 428, 428, 428, 428, 428, 428, 428, 428,
149 // 64 - 71
150 654, 654, 654, 654, 654, 654, 654, 654,
151 // 72 - 79
152 1072, 1072, 1072, 1072, 1104, 1104, 1104, 1104,
153 // 80 - 87
154 1136, 1136, 1136, 1136, 1168, 1168, 1168, 1168,
155 // 88 - 95
156 1200, 1200, 1200, 1200, 1232, 1232, 1232, 1232,
157 // 96 - 103
158 622, 622, 622, 622, 622, 622, 622, 622,
159 // 104 - 111
160 1008, 1008, 1008, 1008, 1040, 1040, 1040, 1040,
161 // 112 - 119
162 44, 44, 44, 44, 44, 44, 44, 44,
163 // 120 - 127
164 44, 44, 44, 44, 44, 44, 44, 44,
165 // 128 - 135
166 396, 396, 396, 396, 396, 396, 396, 396,
167 // 136 - 143
168 396, 396, 396, 396, 396, 396, 396, 396,
169 // 144 - 151
170 1712, 1712, 1712, 1712, 1744, 1744, 1744, 1744,
171 // 152 - 159
172 846, 846, 846, 846, 846, 846, 846, 846,
173 // 160 - 167
174 1264, 1264, 1264, 1264, 1296, 1296, 1296, 1296,
175 // 168 - 175
176 1328, 1328, 1328, 1328, 1360, 1360, 1360, 1360,
177 // 176 - 183
178 1392, 1392, 1392, 1392, 1424, 1424, 1424, 1424,
179 // 184 - 191
180 686, 686, 686, 686, 686, 686, 686, 686,
181 // 192 - 199
182 910, 910, 910, 910, 910, 910, 910, 910,
183 // 200 - 207
184 1968, 1968, 1968, 1968, 2000, 2000, 2000, 2000,
185 // 208 - 215
186 2032, 2032, 2032, 2032, 16, 16, 16, 16,
187 // 216 - 223
188 10257, 10257, 10257, 10257, 12305, 12305, 12305, 12305,
189 // 224 - 231
190 330, 330, 330, 330, 330, 330, 330, 330,
191 // 232 - 239
192 330, 330, 330, 330, 330, 330, 330, 330,
193 // 240 - 247
194 330, 330, 330, 330, 330, 330, 330, 330,
195 // 248 - 255
196 330, 330, 330, 330, 330, 330, 330, 330,
197 // 256 - 263
198 362, 362, 362, 362, 362, 362, 362, 362,
199 // 264 - 271
200 362, 362, 362, 362, 362, 362, 362, 362,
201 // 272 - 279
202 362, 362, 362, 362, 362, 362, 362, 362,
203 // 280 - 287
204 362, 362, 362, 362, 362, 362, 362, 362,
205 // 288 - 295
206 878, 878, 878, 878, 878, 878, 878, 878,
207 // 296 - 303
208 1904, 1904, 1904, 1904, 1936, 1936, 1936, 1936,
209 // 304 - 311
210 -18413, -18413, -16365, -16365, -14317, -14317, -10221, -10221,
211 // 312 - 319
212 590, 590, 590, 590, 590, 590, 590, 590,
213 // 320 - 327
214 782, 782, 782, 782, 782, 782, 782, 782,
215 // 328 - 335
216 1584, 1584, 1584, 1584, 1616, 1616, 1616, 1616,
217 // 336 - 343
218 1648, 1648, 1648, 1648, 1680, 1680, 1680, 1680,
219 // 344 - 351
220 814, 814, 814, 814, 814, 814, 814, 814,
221 // 352 - 359
222 1776, 1776, 1776, 1776, 1808, 1808, 1808, 1808,
223 // 360 - 367
224 1840, 1840, 1840, 1840, 1872, 1872, 1872, 1872,
225 // 368 - 375
226 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157,
227 // 376 - 383
228 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157,
229 // 384 - 391
230 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275,
231 // 392 - 399
232 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275,
233 // 400 - 407
234 14353, 14353, 14353, 14353, 16401, 16401, 16401, 16401,
235 // 408 - 415
236 22547, 22547, 24595, 24595, 20497, 20497, 20497, 20497,
237 // 416 - 423
238 18449, 18449, 18449, 18449, 26643, 26643, 28691, 28691,
239 // 424 - 431
240 30739, 30739, -32749, -32749, -30701, -30701, -28653, -28653,
241 // 432 - 439
242 -26605, -26605, -24557, -24557, -22509, -22509, -20461, -20461,
243 // 440 - 447
244 8207, 8207, 8207, 8207, 8207, 8207, 8207, 8207,
245 // 448 - 455
246 72, 72, 72, 72, 72, 72, 72, 72,
247 // 456 - 463
248 72, 72, 72, 72, 72, 72, 72, 72,
249 // 464 - 471
250 72, 72, 72, 72, 72, 72, 72, 72,
251 // 472 - 479
252 72, 72, 72, 72, 72, 72, 72, 72,
253 // 480 - 487
254 72, 72, 72, 72, 72, 72, 72, 72,
255 // 488 - 495
256 72, 72, 72, 72, 72, 72, 72, 72,
257 // 496 - 503
258 72, 72, 72, 72, 72, 72, 72, 72,
259 // 504 - 511
260 72, 72, 72, 72, 72, 72, 72, 72,
261 // 512 - 519
262 104, 104, 104, 104, 104, 104, 104, 104,
263 // 520 - 527
264 104, 104, 104, 104, 104, 104, 104, 104,
265 // 528 - 535
266 104, 104, 104, 104, 104, 104, 104, 104,
267 // 536 - 543
268 104, 104, 104, 104, 104, 104, 104, 104,
269 // 544 - 551
270 104, 104, 104, 104, 104, 104, 104, 104,
271 // 552 - 559
272 104, 104, 104, 104, 104, 104, 104, 104,
273 // 560 - 567
274 104, 104, 104, 104, 104, 104, 104, 104,
275 // 568 - 575
276 104, 104, 104, 104, 104, 104, 104, 104,
277 // 576 - 583
278 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
279 // 584 - 591
280 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
281 // 592 - 599
282 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
283 // 600 - 607
284 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
285 // 608 - 615
286 266, 266, 266, 266, 266, 266, 266, 266,
287 // 616 - 623
288 266, 266, 266, 266, 266, 266, 266, 266,
289 // 624 - 631
290 266, 266, 266, 266, 266, 266, 266, 266,
291 // 632 - 639
292 266, 266, 266, 266, 266, 266, 266, 266,
293 // 640 - 647
294 298, 298, 298, 298, 298, 298, 298, 298,
295 // 648 - 655
296 298, 298, 298, 298, 298, 298, 298, 298,
297 // 656 - 663
298 298, 298, 298, 298, 298, 298, 298, 298,
299 // 664 - 671
300 298, 298, 298, 298, 298, 298, 298, 298,
301 // 672 - 679
302 524, 524, 524, 524, 524, 524, 524, 524,
303 // 680 - 687
304 524, 524, 524, 524, 524, 524, 524, 524,
305 // 688 - 695
306 556, 556, 556, 556, 556, 556, 556, 556,
307 // 696 - 703
308 556, 556, 556, 556, 556, 556, 556, 556,
309 // 704 - 711
310 136, 136, 136, 136, 136, 136, 136, 136,
311 // 712 - 719
312 136, 136, 136, 136, 136, 136, 136, 136,
313 // 720 - 727
314 136, 136, 136, 136, 136, 136, 136, 136,
315 // 728 - 735
316 136, 136, 136, 136, 136, 136, 136, 136,
317 // 736 - 743
318 136, 136, 136, 136, 136, 136, 136, 136,
319 // 744 - 751
320 136, 136, 136, 136, 136, 136, 136, 136,
321 // 752 - 759
322 136, 136, 136, 136, 136, 136, 136, 136,
323 // 760 - 767
324 136, 136, 136, 136, 136, 136, 136, 136,
325 // 768 - 775
326 168, 168, 168, 168, 168, 168, 168, 168,
327 // 776 - 783
328 168, 168, 168, 168, 168, 168, 168, 168,
329 // 784 - 791
330 168, 168, 168, 168, 168, 168, 168, 168,
331 // 792 - 799
332 168, 168, 168, 168, 168, 168, 168, 168,
333 // 800 - 807
334 168, 168, 168, 168, 168, 168, 168, 168,
335 // 808 - 815
336 168, 168, 168, 168, 168, 168, 168, 168,
337 // 816 - 823
338 168, 168, 168, 168, 168, 168, 168, 168,
339 // 824 - 831
340 168, 168, 168, 168, 168, 168, 168, 168,
341 // 832 - 839
342 460, 460, 460, 460, 460, 460, 460, 460,
343 // 840 - 847
344 460, 460, 460, 460, 460, 460, 460, 460,
345 // 848 - 855
346 492, 492, 492, 492, 492, 492, 492, 492,
347 // 856 - 863
348 492, 492, 492, 492, 492, 492, 492, 492,
349 // 864 - 871
350 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
351 // 872 - 879
352 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
353 // 880 - 887
354 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
355 // 888 - 895
356 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
357 // 896 - 903
358 200, 200, 200, 200, 200, 200, 200, 200,
359 // 904 - 911
360 200, 200, 200, 200, 200, 200, 200, 200,
361 // 912 - 919
362 200, 200, 200, 200, 200, 200, 200, 200,
363 // 920 - 927
364 200, 200, 200, 200, 200, 200, 200, 200,
365 // 928 - 935
366 200, 200, 200, 200, 200, 200, 200, 200,
367 // 936 - 943
368 200, 200, 200, 200, 200, 200, 200, 200,
369 // 944 - 951
370 200, 200, 200, 200, 200, 200, 200, 200,
371 // 952 - 959
372 200, 200, 200, 200, 200, 200, 200, 200,
373 // 960 - 967
374 232, 232, 232, 232, 232, 232, 232, 232,
375 // 968 - 975
376 232, 232, 232, 232, 232, 232, 232, 232,
377 // 976 - 983
378 232, 232, 232, 232, 232, 232, 232, 232,
379 // 984 - 991
380 232, 232, 232, 232, 232, 232, 232, 232,
381 // 992 - 999
382 232, 232, 232, 232, 232, 232, 232, 232,
383 // 1000 - 1007
384 232, 232, 232, 232, 232, 232, 232, 232,
385 // 1008 - 1015
386 232, 232, 232, 232, 232, 232, 232, 232,
387 // 1016 - 1023
388 232, 232, 232, 232, 232, 232, 232, 232,
389 };
390
391 // Additional make up codes for both White and Black runs
392 static short additionalMakeup[] = {
393 28679, 28679, 31752, (short)32777,
394 (short)33801, (short)34825, (short)35849, (short)36873,
395 (short)29703, (short)29703, (short)30727, (short)30727,
396 (short)37897, (short)38921, (short)39945, (short)40969
397 };
398
399 // Initial black run look up table, uses the first 4 bits of a code
400 static short initBlack[] = {
401 // 0 - 7
402 3226, 6412, 200, 168, 38, 38, 134, 134,
403 // 8 - 15
404 100, 100, 100, 100, 68, 68, 68, 68
405 };
406
407 //
408 static short twoBitBlack[] = {292, 260, 226, 226}; // 0 - 3
409
410 // Main black run table, using the last 9 bits of possible 13 bit code
411 static short black[] = {
412 // 0 - 7
413 62, 62, 30, 30, 0, 0, 0, 0,
414 // 8 - 15
415 0, 0, 0, 0, 0, 0, 0, 0,
416 // 16 - 23
417 0, 0, 0, 0, 0, 0, 0, 0,
418 // 24 - 31
419 0, 0, 0, 0, 0, 0, 0, 0,
420 // 32 - 39
421 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
422 // 40 - 47
423 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
424 // 48 - 55
425 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
426 // 56 - 63
427 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
428 // 64 - 71
429 588, 588, 588, 588, 588, 588, 588, 588,
430 // 72 - 79
431 1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776,
432 // 80 - 87
433 1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904,
434 // 88 - 95
435 1936, 1936, -16365, -14317, 782, 782, 782, 782,
436 // 96 - 103
437 814, 814, 814, 814, -12269, -10221, 10257, 10257,
438 // 104 - 111
439 12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712,
440 // 112 - 119
441 1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605,
442 // 120 - 127
443 2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061,
444 // 128 - 135
445 424, 424, 424, 424, 424, 424, 424, 424,
446 // 136 - 143
447 424, 424, 424, 424, 424, 424, 424, 424,
448 // 144 - 151
449 424, 424, 424, 424, 424, 424, 424, 424,
450 // 152 - 159
451 424, 424, 424, 424, 424, 424, 424, 424,
452 // 160 - 167
453 750, 750, 750, 750, 1616, 1616, 1648, 1648,
454 // 168 - 175
455 1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520,
456 // 176 - 183
457 1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209,
458 // 184 - 191
459 524, 524, 524, 524, 524, 524, 524, 524,
460 // 192 - 199
461 556, 556, 556, 556, 556, 556, 556, 556,
462 // 200 - 207
463 1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032,
464 // 208 - 215
465 976, 976, 1008, 1008, 1040, 1040, 1072, 1072,
466 // 216 - 223
467 1296, 1296, 1328, 1328, 718, 718, 718, 718,
468 // 224 - 231
469 456, 456, 456, 456, 456, 456, 456, 456,
470 // 232 - 239
471 456, 456, 456, 456, 456, 456, 456, 456,
472 // 240 - 247
473 456, 456, 456, 456, 456, 456, 456, 456,
474 // 248 - 255
475 456, 456, 456, 456, 456, 456, 456, 456,
476 // 256 - 263
477 326, 326, 326, 326, 326, 326, 326, 326,
478 // 264 - 271
479 326, 326, 326, 326, 326, 326, 326, 326,
480 // 272 - 279
481 326, 326, 326, 326, 326, 326, 326, 326,
482 // 280 - 287
483 326, 326, 326, 326, 326, 326, 326, 326,
484 // 288 - 295
485 326, 326, 326, 326, 326, 326, 326, 326,
486 // 296 - 303
487 326, 326, 326, 326, 326, 326, 326, 326,
488 // 304 - 311
489 326, 326, 326, 326, 326, 326, 326, 326,
490 // 312 - 319
491 326, 326, 326, 326, 326, 326, 326, 326,
492 // 320 - 327
493 358, 358, 358, 358, 358, 358, 358, 358,
494 // 328 - 335
495 358, 358, 358, 358, 358, 358, 358, 358,
496 // 336 - 343
497 358, 358, 358, 358, 358, 358, 358, 358,
498 // 344 - 351
499 358, 358, 358, 358, 358, 358, 358, 358,
500 // 352 - 359
501 358, 358, 358, 358, 358, 358, 358, 358,
502 // 360 - 367
503 358, 358, 358, 358, 358, 358, 358, 358,
504 // 368 - 375
505 358, 358, 358, 358, 358, 358, 358, 358,
506 // 376 - 383
507 358, 358, 358, 358, 358, 358, 358, 358,
508 // 384 - 391
509 490, 490, 490, 490, 490, 490, 490, 490,
510 // 392 - 399
511 490, 490, 490, 490, 490, 490, 490, 490,
512 // 400 - 407
513 4113, 4113, 6161, 6161, 848, 848, 880, 880,
514 // 408 - 415
515 912, 912, 944, 944, 622, 622, 622, 622,
516 // 416 - 423
517 654, 654, 654, 654, 1104, 1104, 1136, 1136,
518 // 424 - 431
519 1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264,
520 // 432 - 439
521 686, 686, 686, 686, 1360, 1360, 1392, 1392,
522 // 440 - 447
523 12, 12, 12, 12, 12, 12, 12, 12,
524 // 448 - 455
525 390, 390, 390, 390, 390, 390, 390, 390,
526 // 456 - 463
527 390, 390, 390, 390, 390, 390, 390, 390,
528 // 464 - 471
529 390, 390, 390, 390, 390, 390, 390, 390,
530 // 472 - 479
531 390, 390, 390, 390, 390, 390, 390, 390,
532 // 480 - 487
533 390, 390, 390, 390, 390, 390, 390, 390,
534 // 488 - 495
535 390, 390, 390, 390, 390, 390, 390, 390,
536 // 496 - 503
537 390, 390, 390, 390, 390, 390, 390, 390,
538 // 504 - 511
539 390, 390, 390, 390, 390, 390, 390, 390,
540 };
541
542 static byte twoDCodes[] = {
543 // 0 - 7
544 80, 88, 23, 71, 30, 30, 62, 62,
545 // 8 - 15
546 4, 4, 4, 4, 4, 4, 4, 4,
547 // 16 - 23
548 11, 11, 11, 11, 11, 11, 11, 11,
549 // 24 - 31
550 11, 11, 11, 11, 11, 11, 11, 11,
551 // 32 - 39
552 35, 35, 35, 35, 35, 35, 35, 35,
553 // 40 - 47
554 35, 35, 35, 35, 35, 35, 35, 35,
555 // 48 - 55
556 51, 51, 51, 51, 51, 51, 51, 51,
557 // 56 - 63
558 51, 51, 51, 51, 51, 51, 51, 51,
559 // 64 - 71
560 41, 41, 41, 41, 41, 41, 41, 41,
561 // 72 - 79
562 41, 41, 41, 41, 41, 41, 41, 41,
563 // 80 - 87
564 41, 41, 41, 41, 41, 41, 41, 41,
565 // 88 - 95
566 41, 41, 41, 41, 41, 41, 41, 41,
567 // 96 - 103
568 41, 41, 41, 41, 41, 41, 41, 41,
569 // 104 - 111
570 41, 41, 41, 41, 41, 41, 41, 41,
571 // 112 - 119
572 41, 41, 41, 41, 41, 41, 41, 41,
573 // 120 - 127
574 41, 41, 41, 41, 41, 41, 41, 41,
575 };
576
577 /**
578 * @param fillOrder The fill order of the compressed data bytes.
579 * @param w
580 * @param h
581 */
582 public TIFFFaxDecoder(int fillOrder, int w, int h) {
583 this.fillOrder = fillOrder;
584 this.w = w;
585 this.h = h;
586
587 this.bitPointer = 0;
588 this.bytePointer = 0;
589 this.prevChangingElems = new int[w];
590 this.currChangingElems = new int[w];
591 }
592
593 /**
594 * Reverses the bits in the array
595 * @param b the bits to reverse
596 *
597 * @since 2.0.7
598 */
599 public static void reverseBits(byte[] b) {
600 for (int k = 0; k < b.length; ++k)
601 b[k] = flipTable[b[k] & 0xff];
602 }
603
604 // One-dimensional decoding methods
605
606 public void decode1D(byte[] buffer, byte[] compData,
607 int startX, int height) {
608 this.data = compData;
609
610 int lineOffset = 0;
611 int scanlineStride = (w + 7)/8;
612
613 bitPointer = 0;
614 bytePointer = 0;
615
616 for (int i = 0; i < height; i++) {
617 decodeNextScanline(buffer, lineOffset, startX);
618 lineOffset += scanlineStride;
619 }
620 }
621
622 public void decodeNextScanline(byte[] buffer,
623 int lineOffset, int bitOffset) {
624 int bits = 0, code = 0, isT = 0;
625 int current, entry, twoBits;
626 boolean isWhite = true;
627
628 // Initialize starting of the changing elements array
629 changingElemSize = 0;
630
631 // While scanline not complete
632 while (bitOffset < w) {
633 while (isWhite) {
634 // White run
635 current = nextNBits(10);
636 entry = white[current];
637
638 // Get the 3 fields from the entry
639 isT = entry & 0x0001;
640 bits = (entry >>> 1) & 0x0f;
641
642 if (bits == 12) { // Additional Make up code
643 // Get the next 2 bits
644 twoBits = nextLesserThan8Bits(2);
645 // Consolidate the 2 new bits and last 2 bits into 4 bits
646 current = ((current << 2) & 0x000c) | twoBits;
647 entry = additionalMakeup[current];
648 bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
649 code = (entry >>> 4) & 0x0fff; // 12 bits
650 bitOffset += code; // Skip white run
651
652 updatePointer(4 - bits);
653 } else if (bits == 0) { // ERROR
654 throw new RuntimeException("Invalid code encountered.");
655 } else if (bits == 15) { // EOL
656 throw new RuntimeException("EOL code word encountered in White run.");
657 } else {
658 // 11 bits - 0000 0111 1111 1111 = 0x07ff
659 code = (entry >>> 5) & 0x07ff;
660 bitOffset += code;
661
662 updatePointer(10 - bits);
663 if (isT == 0) {
664 isWhite = false;
665 currChangingElems[changingElemSize++] = bitOffset;
666 }
667 }
668 }
669
670 // Check whether this run completed one width, if so
671 // advance to next byte boundary for compression = 2.
672 if (bitOffset == w) {
673 if (compression == 2) {
674 advancePointer();
675 }
676 break;
677 }
678
679 while (!isWhite) {
680 // Black run
681 current = nextLesserThan8Bits(4);
682 entry = initBlack[current];
683
684 // Get the 3 fields from the entry
685 isT = entry & 0x0001;
686 bits = (entry >>> 1) & 0x000f;
687 code = (entry >>> 5) & 0x07ff;
688
689 if (code == 100) {
690 current = nextNBits(9);
691 entry = black[current];
692
693 // Get the 3 fields from the entry
694 isT = entry & 0x0001;
695 bits = (entry >>> 1) & 0x000f;
696 code = (entry >>> 5) & 0x07ff;
697
698 if (bits == 12) {
699 // Additional makeup codes
700 updatePointer(5);
701 current = nextLesserThan8Bits(4);
702 entry = additionalMakeup[current];
703 bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
704 code = (entry >>> 4) & 0x0fff; // 12 bits
705
706 setToBlack(buffer, lineOffset, bitOffset, code);
707 bitOffset += code;
708
709 updatePointer(4 - bits);
710 } else if (bits == 15) {
711 // EOL code
712 throw new RuntimeException("EOL code word encountered in Black run.");
713 } else {
714 setToBlack(buffer, lineOffset, bitOffset, code);
715 bitOffset += code;
716
717 updatePointer(9 - bits);
718 if (isT == 0) {
719 isWhite = true;
720 currChangingElems[changingElemSize++] = bitOffset;
721 }
722 }
723 } else if (code == 200) {
724 // Is a Terminating code
725 current = nextLesserThan8Bits(2);
726 entry = twoBitBlack[current];
727 code = (entry >>> 5) & 0x07ff;
728 bits = (entry >>> 1) & 0x0f;
729
730 setToBlack(buffer, lineOffset, bitOffset, code);
731 bitOffset += code;
732
733 updatePointer(2 - bits);
734 isWhite = true;
735 currChangingElems[changingElemSize++] = bitOffset;
736 } else {
737 // Is a Terminating code
738 setToBlack(buffer, lineOffset, bitOffset, code);
739 bitOffset += code;
740
741 updatePointer(4 - bits);
742 isWhite = true;
743 currChangingElems[changingElemSize++] = bitOffset;
744 }
745 }
746
747 // Check whether this run completed one width
748 if (bitOffset == w) {
749 if (compression == 2) {
750 advancePointer();
751 }
752 break;
753 }
754 }
755
756 currChangingElems[changingElemSize++] = bitOffset;
757 }
758
759 // Two-dimensional decoding methods
760
761 public void decode2D(byte[] buffer,
762 byte compData[],
763 int startX,
764 int height,
765 long tiffT4Options) {
766 this.data = compData;
767 compression = 3;
768
769 bitPointer = 0;
770 bytePointer = 0;
771
772 int scanlineStride = (w + 7)/8;
773
774 int a0, a1, b1, b2;
775 int[] b = new int[2];
776 int entry, code, bits;
777 boolean isWhite;
778 int currIndex = 0;
779 int temp[];
780
781 // fillBits - dealt with this in readEOL
782 // 1D/2D encoding - dealt with this in readEOL
783
784 // uncompressedMode - haven't dealt with this yet.
785
786
787 oneD = (int)(tiffT4Options & 0x01);
788 uncompressedMode = (int)((tiffT4Options & 0x02) >> 1);
789 fillBits = (int)((tiffT4Options & 0x04) >> 2);
790
791 // The data must start with an EOL code
792 if (readEOL(true) != 1) {
793 throw new RuntimeException("First scanline must be 1D encoded.");
794 }
795
796 int lineOffset = 0;
797 int bitOffset;
798
799 // Then the 1D encoded scanline data will occur, changing elements
800 // array gets set.
801 decodeNextScanline(buffer, lineOffset, startX);
802 lineOffset += scanlineStride;
803
804 for (int lines = 1; lines < height; lines++) {
805
806 // Every line must begin with an EOL followed by a bit which
807 // indicates whether the following scanline is 1D or 2D encoded.
808 if (readEOL(false) == 0) {
809 // 2D encoded scanline follows
810
811 // Initialize previous scanlines changing elements, and
812 // initialize current scanline's changing elements array
813 temp = prevChangingElems;
814 prevChangingElems = currChangingElems;
815 currChangingElems = temp;
816 currIndex = 0;
817
818 // a0 has to be set just before the start of this scanline.
819 a0 = -1;
820 isWhite = true;
821 bitOffset = startX;
822
823 lastChangingElement = 0;
824
825 while (bitOffset < w) {
826 // Get the next changing element
827 getNextChangingElement(a0, isWhite, b);
828
829 b1 = b[0];
830 b2 = b[1];
831
832 // Get the next seven bits
833 entry = nextLesserThan8Bits(7);
834
835 // Run these through the 2DCodes table
836 entry = twoDCodes[entry] & 0xff;
837
838 // Get the code and the number of bits used up
839 code = (entry & 0x78) >>> 3;
840 bits = entry & 0x07;
841
842 if (code == 0) {
843 if (!isWhite) {
844 setToBlack(buffer, lineOffset, bitOffset,
845 b2 - bitOffset);
846 }
847 bitOffset = a0 = b2;
848
849 // Set pointer to consume the correct number of bits.
850 updatePointer(7 - bits);
851 } else if (code == 1) {
852 // Horizontal
853 updatePointer(7 - bits);
854
855 // identify the next 2 codes.
856 int number;
857 if (isWhite) {
858 number = decodeWhiteCodeWord();
859 bitOffset += number;
860 currChangingElems[currIndex++] = bitOffset;
861
862 number = decodeBlackCodeWord();
863 setToBlack(buffer, lineOffset, bitOffset, number);
864 bitOffset += number;
865 currChangingElems[currIndex++] = bitOffset;
866 } else {
867 number = decodeBlackCodeWord();
868 setToBlack(buffer, lineOffset, bitOffset, number);
869 bitOffset += number;
870 currChangingElems[currIndex++] = bitOffset;
871
872 number = decodeWhiteCodeWord();
873 bitOffset += number;
874 currChangingElems[currIndex++] = bitOffset;
875 }
876
877 a0 = bitOffset;
878 } else if (code <= 8) {
879 // Vertical
880 a1 = b1 + (code - 5);
881
882 currChangingElems[currIndex++] = a1;
883
884 // We write the current color till a1 - 1 pos,
885 // since a1 is where the next color starts
886 if (!isWhite) {
887 setToBlack(buffer, lineOffset, bitOffset,
888 a1 - bitOffset);
889 }
890 bitOffset = a0 = a1;
891 isWhite = !isWhite;
892
893 updatePointer(7 - bits);
894 } else {
895 throw new RuntimeException("Invalid code encountered while decoding 2D group 3 compressed data.");
896 }
897 }
898
899 // Add the changing element beyond the current scanline for the
900 // other color too
901 currChangingElems[currIndex++] = bitOffset;
902 changingElemSize = currIndex;
903 } else {
904 // 1D encoded scanline follows
905 decodeNextScanline(buffer, lineOffset, startX);
906 }
907
908 lineOffset += scanlineStride;
909 }
910 }
911
912 public void decodeT6(byte[] buffer,
913 byte[] compData,
914 int startX,
915 int height,
916 long tiffT6Options) {
917 this.data = compData;
918 compression = 4;
919
920 bitPointer = 0;
921 bytePointer = 0;
922
923 int scanlineStride = (w + 7)/8;
924
925 int a0, a1, b1, b2;
926 int entry, code, bits;
927 boolean isWhite;
928 int currIndex;
929 int temp[];
930
931 // Return values from getNextChangingElement
932 int[] b = new int[2];
933
934 // uncompressedMode - have written some code for this, but this
935 // has not been tested due to lack of test images using this optional
936
937 uncompressedMode = (int)((tiffT6Options & 0x02) >> 1);
938
939 // Local cached reference
940 int[] cce = currChangingElems;
941
942 // Assume invisible preceding row of all white pixels and insert
943 // both black and white changing elements beyond the end of this
944 // imaginary scanline.
945 changingElemSize = 0;
946 cce[changingElemSize++] = w;
947 cce[changingElemSize++] = w;
948
949 int lineOffset = 0;
950 int bitOffset;
951
952 for (int lines = 0; lines < height; lines++) {
953 // a0 has to be set just before the start of the scanline.
954 a0 = -1;
955 isWhite = true;
956
957 // Assign the changing elements of the previous scanline to
958 // prevChangingElems and start putting this new scanline's
959 // changing elements into the currChangingElems.
960 temp = prevChangingElems;
961 prevChangingElems = currChangingElems;
962 cce = currChangingElems = temp;
963 currIndex = 0;
964
965 // Start decoding the scanline at startX in the raster
966 bitOffset = startX;
967
968 // Reset search start position for getNextChangingElement
969 lastChangingElement = 0;
970
971 // Till one whole scanline is decoded
972 while (bitOffset < w) {
973 // Get the next changing element
974 getNextChangingElement(a0, isWhite, b);
975 b1 = b[0];
976 b2 = b[1];
977
978 // Get the next seven bits
979 entry = nextLesserThan8Bits(7);
980 // Run these through the 2DCodes table
981 entry = twoDCodes[entry] & 0xff;
982
983 // Get the code and the number of bits used up
984 code = (entry & 0x78) >>> 3;
985 bits = entry & 0x07;
986
987 if (code == 0) { // Pass
988 // We always assume WhiteIsZero format for fax.
989 if (!isWhite) {
990 setToBlack(buffer, lineOffset, bitOffset,
991 b2 - bitOffset);
992 }
993 bitOffset = a0 = b2;
994
995 // Set pointer to only consume the correct number of bits.
996 updatePointer(7 - bits);
997 } else if (code == 1) { // Horizontal
998 // Set pointer to only consume the correct number of bits.
999 updatePointer(7 - bits);
1000
1001 // identify the next 2 alternating color codes.
1002 int number;
1003 if (isWhite) {
1004 // Following are white and black runs
1005 number = decodeWhiteCodeWord();
1006 bitOffset += number;
1007 cce[currIndex++] = bitOffset;
1008
1009 number = decodeBlackCodeWord();
1010 setToBlack(buffer, lineOffset, bitOffset, number);
1011 bitOffset += number;
1012 cce[currIndex++] = bitOffset;
1013 } else {
1014 // First a black run and then a white run follows
1015 number = decodeBlackCodeWord();
1016 setToBlack(buffer, lineOffset, bitOffset, number);
1017 bitOffset += number;
1018 cce[currIndex++] = bitOffset;
1019
1020 number = decodeWhiteCodeWord();
1021 bitOffset += number;
1022 cce[currIndex++] = bitOffset;
1023 }
1024
1025 a0 = bitOffset;
1026 } else if (code <= 8) { // Vertical
1027 a1 = b1 + (code - 5);
1028 cce[currIndex++] = a1;
1029
1030 // We write the current color till a1 - 1 pos,
1031 // since a1 is where the next color starts
1032 if (!isWhite) {
1033 setToBlack(buffer, lineOffset, bitOffset,
1034 a1 - bitOffset);
1035 }
1036 bitOffset = a0 = a1;
1037 isWhite = !isWhite;
1038
1039 updatePointer(7 - bits);
1040 } else if (code == 11) {
1041 if (nextLesserThan8Bits(3) != 7) {
1042 throw new RuntimeException("Invalid code encountered while decoding 2D group 4 compressed data.");
1043 }
1044
1045 int zeros = 0;
1046 boolean exit = false;
1047
1048 while (!exit) {
1049 while (nextLesserThan8Bits(1) != 1) {
1050 zeros++;
1051 }
1052
1053 if (zeros > 5) {
1054 // Exit code
1055
1056 // Zeros before exit code
1057 zeros = zeros - 6;
1058
1059 if (!isWhite && (zeros > 0)) {
1060 cce[currIndex++] = bitOffset;
1061 }
1062
1063 // Zeros before the exit code
1064 bitOffset += zeros;
1065 if (zeros > 0) {
1066 // Some zeros have been written
1067 isWhite = true;
1068 }
1069
1070 // Read in the bit which specifies the color of
1071 // the following run
1072 if (nextLesserThan8Bits(1) == 0) {
1073 if (!isWhite) {
1074 cce[currIndex++] = bitOffset;
1075 }
1076 isWhite = true;
1077 } else {
1078 if (isWhite) {
1079 cce[currIndex++] = bitOffset;
1080 }
1081 isWhite = false;
1082 }
1083
1084 exit = true;
1085 }
1086
1087 if (zeros == 5) {
1088 if (!isWhite) {
1089 cce[currIndex++] = bitOffset;
1090 }
1091 bitOffset += zeros;
1092
1093 // Last thing written was white
1094 isWhite = true;
1095 } else {
1096 bitOffset += zeros;
1097
1098 cce[currIndex++] = bitOffset;
1099 setToBlack(buffer, lineOffset, bitOffset, 1);
1100 ++bitOffset;
1101
1102 // Last thing written was black
1103 isWhite = false;
1104 }
1105
1106 }
1107 } else {
1108 //micah_tessler@yahoo.com
1109 //Microsoft TIFF renderers seem to treat unknown codes as line-breaks
1110 //That is, they give up on the current line and move on to the next one
1111 //set bitOffset to w to move on to the next scan line.
1112 bitOffset = w;
1113 updatePointer(7 - bits);
1114 }
1115 }
1116
1117 // Add the changing element beyond the current scanline for the
1118 // other color too
1119 //make sure that the index does not exceed the bounds of the array
1120 if(currIndex < cce.length)
1121 cce[currIndex++] = bitOffset;
1122
1123 // Number of changing elements in this scanline.
1124 changingElemSize = currIndex;
1125
1126 lineOffset += scanlineStride;
1127 }
1128 }
1129
1130 private void setToBlack(byte[] buffer,
1131 int lineOffset, int bitOffset,
1132 int numBits) {
1133 int bitNum = 8*lineOffset + bitOffset;
1134 int lastBit = bitNum + numBits;
1135
1136 int byteNum = bitNum >> 3;
1137
1138 // Handle bits in first byte
1139 int shift = bitNum & 0x7;
1140 if (shift > 0) {
1141 int maskVal = 1 << (7 - shift);
1142 byte val = buffer[byteNum];
1143 while (maskVal > 0 && bitNum < lastBit) {
1144 val |= maskVal;
1145 maskVal >>= 1;
1146 ++bitNum;
1147 }
1148 buffer[byteNum] = val;
1149 }
1150
1151 // Fill in 8 bits at a time
1152 byteNum = bitNum >> 3;
1153 while (bitNum < lastBit - 7) {
1154 buffer[byteNum++] = (byte)255;
1155 bitNum += 8;
1156 }
1157
1158 // Fill in remaining bits
1159 while (bitNum < lastBit) {
1160 byteNum = bitNum >> 3;
1161 buffer[byteNum] |= 1 << (7 - (bitNum & 0x7));
1162 ++bitNum;
1163 }
1164 }
1165
1166 // Returns run length
1167 private int decodeWhiteCodeWord() {
1168 int current, entry, bits, isT, twoBits, code = -1;
1169 int runLength = 0;
1170 boolean isWhite = true;
1171
1172 while (isWhite) {
1173 current = nextNBits(10);
1174 entry = white[current];
1175
1176 // Get the 3 fields from the entry
1177 isT = entry & 0x0001;
1178 bits = (entry >>> 1) & 0x0f;
1179
1180 if (bits == 12) { // Additional Make up code
1181 // Get the next 2 bits
1182 twoBits = nextLesserThan8Bits(2);
1183 // Consolidate the 2 new bits and last 2 bits into 4 bits
1184 current = ((current << 2) & 0x000c) | twoBits;
1185 entry = additionalMakeup[current];
1186 bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1187 code = (entry >>> 4) & 0x0fff; // 12 bits
1188 runLength += code;
1189 updatePointer(4 - bits);
1190 } else if (bits == 0) { // ERROR
1191 throw new RuntimeException("Invalid code encountered.");
1192 } else if (bits == 15) { // EOL
1193 throw new RuntimeException("EOL code word encountered in White run.");
1194 } else {
1195 // 11 bits - 0000 0111 1111 1111 = 0x07ff
1196 code = (entry >>> 5) & 0x07ff;
1197 runLength += code;
1198 updatePointer(10 - bits);
1199 if (isT == 0) {
1200 isWhite = false;
1201 }
1202 }
1203 }
1204
1205 return runLength;
1206 }
1207
1208 // Returns run length
1209 private int decodeBlackCodeWord() {
1210 int current, entry, bits, isT, code = -1;
1211 int runLength = 0;
1212 boolean isWhite = false;
1213
1214 while (!isWhite) {
1215 current = nextLesserThan8Bits(4);
1216 entry = initBlack[current];
1217
1218 // Get the 3 fields from the entry
1219 isT = entry & 0x0001;
1220 bits = (entry >>> 1) & 0x000f;
1221 code = (entry >>> 5) & 0x07ff;
1222
1223 if (code == 100) {
1224 current = nextNBits(9);
1225 entry = black[current];
1226
1227 // Get the 3 fields from the entry
1228 isT = entry & 0x0001;
1229 bits = (entry >>> 1) & 0x000f;
1230 code = (entry >>> 5) & 0x07ff;
1231
1232 if (bits == 12) {
1233 // Additional makeup codes
1234 updatePointer(5);
1235 current = nextLesserThan8Bits(4);
1236 entry = additionalMakeup[current];
1237 bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1238 code = (entry >>> 4) & 0x0fff; // 12 bits
1239 runLength += code;
1240
1241 updatePointer(4 - bits);
1242 } else if (bits == 15) {
1243 // EOL code
1244 throw new RuntimeException("EOL code word encountered in Black run.");
1245 } else {
1246 runLength += code;
1247 updatePointer(9 - bits);
1248 if (isT == 0) {
1249 isWhite = true;
1250 }
1251 }
1252 } else if (code == 200) {
1253 // Is a Terminating code
1254 current = nextLesserThan8Bits(2);
1255 entry = twoBitBlack[current];
1256 code = (entry >>> 5) & 0x07ff;
1257 runLength += code;
1258 bits = (entry >>> 1) & 0x0f;
1259 updatePointer(2 - bits);
1260 isWhite = true;
1261 } else {
1262 // Is a Terminating code
1263 runLength += code;
1264 updatePointer(4 - bits);
1265 isWhite = true;
1266 }
1267 }
1268
1269 return runLength;
1270 }
1271
1272 private int readEOL(boolean isFirstEOL) {
1273 if (fillBits == 0) {
1274 int next12Bits = nextNBits(12);
1275 if (isFirstEOL && next12Bits == 0) {
1276
1277 // Might have the case of EOL padding being used even
1278 // though it was not flagged in the T4Options field.
1279 // This was observed to be the case in TIFFs produced
1280 // by a well known vendor who shall remain nameless.
1281
1282 if(nextNBits(4) == 1) {
1283
1284 // EOL must be padded: reset the fillBits flag.
1285
1286 fillBits = 1;
1287 return 1;
1288 }
1289 }
1290 if(next12Bits != 1) {
1291 throw new RuntimeException("Scanline must begin with EOL code word.");
1292 }
1293 } else if (fillBits == 1) {
1294
1295 // First EOL code word xxxx 0000 0000 0001 will occur
1296 // As many fill bits will be present as required to make
1297 // the EOL code of 12 bits end on a byte boundary.
1298
1299 int bitsLeft = 8 - bitPointer;
1300
1301 if (nextNBits(bitsLeft) != 0) {
1302 throw new RuntimeException("All fill bits preceding EOL code must be 0.");
1303 }
1304
1305 // If the number of bitsLeft is less than 8, then to have a 12
1306 // bit EOL sequence, two more bytes are certainly going to be
1307 // required. The first of them has to be all zeros, so ensure
1308 // that.
1309 if (bitsLeft < 4) {
1310 if (nextNBits(8) != 0) {
1311 throw new RuntimeException("All fill bits preceding EOL code must be 0.");
1312 }
1313 }
1314
1315 // There might be a random number of fill bytes with 0s, so
1316 // loop till the EOL of 0000 0001 is found, as long as all
1317 // the bytes preceding it are 0's.
1318 int n;
1319 while ((n = nextNBits(8)) != 1) {
1320
1321 // If not all zeros
1322 if (n != 0) {
1323 throw new RuntimeException("All fill bits preceding EOL code must be 0.");
1324 }
1325 }
1326 }
1327
1328 // If one dimensional encoding mode, then always return 1
1329 if (oneD == 0) {
1330 return 1;
1331 } else {
1332 // Otherwise for 2D encoding mode,
1333 // The next one bit signifies 1D/2D encoding of next line.
1334 return nextLesserThan8Bits(1);
1335 }
1336 }
1337
1338 private void getNextChangingElement(int a0, boolean isWhite, int[] ret) {
1339 // Local copies of instance variables
1340 int[] pce = this.prevChangingElems;
1341 int ces = this.changingElemSize;
1342
1343 // If the previous match was at an odd element, we still
1344 // have to search the preceeding element.
1345 // int start = lastChangingElement & ~0x1;
1346 int start = lastChangingElement > 0 ? lastChangingElement - 1 : 0;
1347 if (isWhite) {
1348 start &= ~0x1; // Search even numbered elements
1349 } else {
1350 start |= 0x1; // Search odd numbered elements
1351 }
1352
1353 int i = start;
1354 for (; i < ces; i += 2) {
1355 int temp = pce[i];
1356 if (temp > a0) {
1357 lastChangingElement = i;
1358 ret[0] = temp;
1359 break;
1360 }
1361 }
1362
1363 if (i + 1 < ces) {
1364 ret[1] = pce[i + 1];
1365 }
1366 }
1367
1368 private int nextNBits(int bitsToGet) {
1369 byte b, next, next2next;
1370 int l = data.length - 1;
1371 int bp = this.bytePointer;
1372
1373 if (fillOrder == 1) {
1374 b = data[bp];
1375
1376 if (bp == l) {
1377 next = 0x00;
1378 next2next = 0x00;
1379 } else if ((bp + 1) == l) {
1380 next = data[bp + 1];
1381 next2next = 0x00;
1382 } else {
1383 next = data[bp + 1];
1384 next2next = data[bp + 2];
1385 }
1386 } else if (fillOrder == 2) {
1387 b = flipTable[data[bp] & 0xff];
1388
1389 if (bp == l) {
1390 next = 0x00;
1391 next2next = 0x00;
1392 } else if ((bp + 1) == l) {
1393 next = flipTable[data[bp + 1] & 0xff];
1394 next2next = 0x00;
1395 } else {
1396 next = flipTable[data[bp + 1] & 0xff];
1397 next2next = flipTable[data[bp + 2] & 0xff];
1398 }
1399 } else {
1400 throw new RuntimeException("TIFF_FILL_ORDER tag must be either 1 or 2.");
1401 }
1402
1403 int bitsLeft = 8 - bitPointer;
1404 int bitsFromNextByte = bitsToGet - bitsLeft;
1405 int bitsFromNext2NextByte = 0;
1406 if (bitsFromNextByte > 8) {
1407 bitsFromNext2NextByte = bitsFromNextByte - 8;
1408 bitsFromNextByte = 8;
1409 }
1410
1411 bytePointer++;
1412
1413 int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft);
1414 int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1415
1416 int i3 = 0;
1417 if (bitsFromNext2NextByte != 0) {
1418 i2 <<= bitsFromNext2NextByte;
1419 i3 = (next2next & table2[bitsFromNext2NextByte]) >>>
1420 (8 - bitsFromNext2NextByte);
1421 i2 |= i3;
1422 bytePointer++;
1423 bitPointer = bitsFromNext2NextByte;
1424 } else {
1425 if (bitsFromNextByte == 8) {
1426 bitPointer = 0;
1427 bytePointer++;
1428 } else {
1429 bitPointer = bitsFromNextByte;
1430 }
1431 }
1432
1433 int i = i1 | i2;
1434 return i;
1435 }
1436
1437 private int nextLesserThan8Bits(int bitsToGet) {
1438 byte b, next;
1439 int l = data.length - 1;
1440 int bp = this.bytePointer;
1441
1442 if (fillOrder == 1) {
1443 b = data[bp];
1444 if (bp == l) {
1445 next = 0x00;
1446 } else {
1447 next = data[bp + 1];
1448 }
1449 } else if (fillOrder == 2) {
1450 b = flipTable[data[bp] & 0xff];
1451 if (bp == l) {
1452 next = 0x00;
1453 } else {
1454 next = flipTable[data[bp + 1] & 0xff];
1455 }
1456 } else {
1457 throw new RuntimeException("TIFF_FILL_ORDER tag must be either 1 or 2.");
1458 }
1459
1460 int bitsLeft = 8 - bitPointer;
1461 int bitsFromNextByte = bitsToGet - bitsLeft;
1462
1463 int shift = bitsLeft - bitsToGet;
1464 int i1, i2;
1465 if (shift >= 0) {
1466 i1 = (b & table1[bitsLeft]) >>> shift;
1467 bitPointer += bitsToGet;
1468 if (bitPointer == 8) {
1469 bitPointer = 0;
1470 bytePointer++;
1471 }
1472 } else {
1473 i1 = (b & table1[bitsLeft]) << (-shift);
1474 i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1475
1476 i1 |= i2;
1477 bytePointer++;
1478 bitPointer = bitsFromNextByte;
1479 }
1480
1481 return i1;
1482 }
1483
1484 // Move pointer backwards by given amount of bits
1485 private void updatePointer(int bitsToMoveBack) {
1486 int i = bitPointer - bitsToMoveBack;
1487
1488 if (i < 0) {
1489 bytePointer--;
1490 bitPointer = 8 + i;
1491 } else {
1492 bitPointer = i;
1493 }
1494 }
1495
1496 // Move to the next byte boundary
1497 private boolean advancePointer() {
1498 if (bitPointer != 0) {
1499 bytePointer++;
1500 bitPointer = 0;
1501 }
1502
1503 return true;
1504 }
1505 }
1506