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