1 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 private int changingElemSize = 0; 45 private int prevChangingElems[]; 46 private int currChangingElems[]; 47 48 private int lastChangingElement = 0; 50 51 private int compression = 2; 52 53 private int uncompressedMode = 0; 55 private int fillBits = 0; 56 private int oneD; 57 58 static int table1[] = { 59 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; 69 70 static int table2[] = { 71 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 81 82 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 static short white[] = { 120 6430, 6400, 6400, 6400, 3225, 3225, 3225, 3225, 122 944, 944, 944, 944, 976, 976, 976, 976, 124 1456, 1456, 1456, 1456, 1488, 1488, 1488, 1488, 126 718, 718, 718, 718, 718, 718, 718, 718, 128 750, 750, 750, 750, 750, 750, 750, 750, 130 1520, 1520, 1520, 1520, 1552, 1552, 1552, 1552, 132 428, 428, 428, 428, 428, 428, 428, 428, 134 428, 428, 428, 428, 428, 428, 428, 428, 136 654, 654, 654, 654, 654, 654, 654, 654, 138 1072, 1072, 1072, 1072, 1104, 1104, 1104, 1104, 140 1136, 1136, 1136, 1136, 1168, 1168, 1168, 1168, 142 1200, 1200, 1200, 1200, 1232, 1232, 1232, 1232, 144 622, 622, 622, 622, 622, 622, 622, 622, 146 1008, 1008, 1008, 1008, 1040, 1040, 1040, 1040, 148 44, 44, 44, 44, 44, 44, 44, 44, 150 44, 44, 44, 44, 44, 44, 44, 44, 152 396, 396, 396, 396, 396, 396, 396, 396, 154 396, 396, 396, 396, 396, 396, 396, 396, 156 1712, 1712, 1712, 1712, 1744, 1744, 1744, 1744, 158 846, 846, 846, 846, 846, 846, 846, 846, 160 1264, 1264, 1264, 1264, 1296, 1296, 1296, 1296, 162 1328, 1328, 1328, 1328, 1360, 1360, 1360, 1360, 164 1392, 1392, 1392, 1392, 1424, 1424, 1424, 1424, 166 686, 686, 686, 686, 686, 686, 686, 686, 168 910, 910, 910, 910, 910, 910, 910, 910, 170 1968, 1968, 1968, 1968, 2000, 2000, 2000, 2000, 172 2032, 2032, 2032, 2032, 16, 16, 16, 16, 174 10257, 10257, 10257, 10257, 12305, 12305, 12305, 12305, 176 330, 330, 330, 330, 330, 330, 330, 330, 178 330, 330, 330, 330, 330, 330, 330, 330, 180 330, 330, 330, 330, 330, 330, 330, 330, 182 330, 330, 330, 330, 330, 330, 330, 330, 184 362, 362, 362, 362, 362, 362, 362, 362, 186 362, 362, 362, 362, 362, 362, 362, 362, 188 362, 362, 362, 362, 362, 362, 362, 362, 190 362, 362, 362, 362, 362, 362, 362, 362, 192 878, 878, 878, 878, 878, 878, 878, 878, 194 1904, 1904, 1904, 1904, 1936, 1936, 1936, 1936, 196 -18413, -18413, -16365, -16365, -14317, -14317, -10221, -10221, 198 590, 590, 590, 590, 590, 590, 590, 590, 200 782, 782, 782, 782, 782, 782, 782, 782, 202 1584, 1584, 1584, 1584, 1616, 1616, 1616, 1616, 204 1648, 1648, 1648, 1648, 1680, 1680, 1680, 1680, 206 814, 814, 814, 814, 814, 814, 814, 814, 208 1776, 1776, 1776, 1776, 1808, 1808, 1808, 1808, 210 1840, 1840, 1840, 1840, 1872, 1872, 1872, 1872, 212 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157, 214 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157, 216 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275, 218 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275, 220 14353, 14353, 14353, 14353, 16401, 16401, 16401, 16401, 222 22547, 22547, 24595, 24595, 20497, 20497, 20497, 20497, 224 18449, 18449, 18449, 18449, 26643, 26643, 28691, 28691, 226 30739, 30739, -32749, -32749, -30701, -30701, -28653, -28653, 228 -26605, -26605, -24557, -24557, -22509, -22509, -20461, -20461, 230 8207, 8207, 8207, 8207, 8207, 8207, 8207, 8207, 232 72, 72, 72, 72, 72, 72, 72, 72, 234 72, 72, 72, 72, 72, 72, 72, 72, 236 72, 72, 72, 72, 72, 72, 72, 72, 238 72, 72, 72, 72, 72, 72, 72, 72, 240 72, 72, 72, 72, 72, 72, 72, 72, 242 72, 72, 72, 72, 72, 72, 72, 72, 244 72, 72, 72, 72, 72, 72, 72, 72, 246 72, 72, 72, 72, 72, 72, 72, 72, 248 104, 104, 104, 104, 104, 104, 104, 104, 250 104, 104, 104, 104, 104, 104, 104, 104, 252 104, 104, 104, 104, 104, 104, 104, 104, 254 104, 104, 104, 104, 104, 104, 104, 104, 256 104, 104, 104, 104, 104, 104, 104, 104, 258 104, 104, 104, 104, 104, 104, 104, 104, 260 104, 104, 104, 104, 104, 104, 104, 104, 262 104, 104, 104, 104, 104, 104, 104, 104, 264 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 266 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 268 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 270 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 272 266, 266, 266, 266, 266, 266, 266, 266, 274 266, 266, 266, 266, 266, 266, 266, 266, 276 266, 266, 266, 266, 266, 266, 266, 266, 278 266, 266, 266, 266, 266, 266, 266, 266, 280 298, 298, 298, 298, 298, 298, 298, 298, 282 298, 298, 298, 298, 298, 298, 298, 298, 284 298, 298, 298, 298, 298, 298, 298, 298, 286 298, 298, 298, 298, 298, 298, 298, 298, 288 524, 524, 524, 524, 524, 524, 524, 524, 290 524, 524, 524, 524, 524, 524, 524, 524, 292 556, 556, 556, 556, 556, 556, 556, 556, 294 556, 556, 556, 556, 556, 556, 556, 556, 296 136, 136, 136, 136, 136, 136, 136, 136, 298 136, 136, 136, 136, 136, 136, 136, 136, 300 136, 136, 136, 136, 136, 136, 136, 136, 302 136, 136, 136, 136, 136, 136, 136, 136, 304 136, 136, 136, 136, 136, 136, 136, 136, 306 136, 136, 136, 136, 136, 136, 136, 136, 308 136, 136, 136, 136, 136, 136, 136, 136, 310 136, 136, 136, 136, 136, 136, 136, 136, 312 168, 168, 168, 168, 168, 168, 168, 168, 314 168, 168, 168, 168, 168, 168, 168, 168, 316 168, 168, 168, 168, 168, 168, 168, 168, 318 168, 168, 168, 168, 168, 168, 168, 168, 320 168, 168, 168, 168, 168, 168, 168, 168, 322 168, 168, 168, 168, 168, 168, 168, 168, 324 168, 168, 168, 168, 168, 168, 168, 168, 326 168, 168, 168, 168, 168, 168, 168, 168, 328 460, 460, 460, 460, 460, 460, 460, 460, 330 460, 460, 460, 460, 460, 460, 460, 460, 332 492, 492, 492, 492, 492, 492, 492, 492, 334 492, 492, 492, 492, 492, 492, 492, 492, 336 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 338 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 340 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 342 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 344 200, 200, 200, 200, 200, 200, 200, 200, 346 200, 200, 200, 200, 200, 200, 200, 200, 348 200, 200, 200, 200, 200, 200, 200, 200, 350 200, 200, 200, 200, 200, 200, 200, 200, 352 200, 200, 200, 200, 200, 200, 200, 200, 354 200, 200, 200, 200, 200, 200, 200, 200, 356 200, 200, 200, 200, 200, 200, 200, 200, 358 200, 200, 200, 200, 200, 200, 200, 200, 360 232, 232, 232, 232, 232, 232, 232, 232, 362 232, 232, 232, 232, 232, 232, 232, 232, 364 232, 232, 232, 232, 232, 232, 232, 232, 366 232, 232, 232, 232, 232, 232, 232, 232, 368 232, 232, 232, 232, 232, 232, 232, 232, 370 232, 232, 232, 232, 232, 232, 232, 232, 372 232, 232, 232, 232, 232, 232, 232, 232, 374 232, 232, 232, 232, 232, 232, 232, 232, 376 }; 377 378 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 static short initBlack[] = { 388 3226, 6412, 200, 168, 38, 38, 134, 134, 390 100, 100, 100, 100, 68, 68, 68, 68 392 }; 393 394 static short twoBitBlack[] = {292, 260, 226, 226}; 397 static short black[] = { 399 62, 62, 30, 30, 0, 0, 0, 0, 401 0, 0, 0, 0, 0, 0, 0, 0, 403 0, 0, 0, 0, 0, 0, 0, 0, 405 0, 0, 0, 0, 0, 0, 0, 0, 407 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 409 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 411 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 413 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 415 588, 588, 588, 588, 588, 588, 588, 588, 417 1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776, 419 1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904, 421 1936, 1936, -16365, -14317, 782, 782, 782, 782, 423 814, 814, 814, 814, -12269, -10221, 10257, 10257, 425 12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712, 427 1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605, 429 2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061, 431 424, 424, 424, 424, 424, 424, 424, 424, 433 424, 424, 424, 424, 424, 424, 424, 424, 435 424, 424, 424, 424, 424, 424, 424, 424, 437 424, 424, 424, 424, 424, 424, 424, 424, 439 750, 750, 750, 750, 1616, 1616, 1648, 1648, 441 1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520, 443 1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209, 445 524, 524, 524, 524, 524, 524, 524, 524, 447 556, 556, 556, 556, 556, 556, 556, 556, 449 1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032, 451 976, 976, 1008, 1008, 1040, 1040, 1072, 1072, 453 1296, 1296, 1328, 1328, 718, 718, 718, 718, 455 456, 456, 456, 456, 456, 456, 456, 456, 457 456, 456, 456, 456, 456, 456, 456, 456, 459 456, 456, 456, 456, 456, 456, 456, 456, 461 456, 456, 456, 456, 456, 456, 456, 456, 463 326, 326, 326, 326, 326, 326, 326, 326, 465 326, 326, 326, 326, 326, 326, 326, 326, 467 326, 326, 326, 326, 326, 326, 326, 326, 469 326, 326, 326, 326, 326, 326, 326, 326, 471 326, 326, 326, 326, 326, 326, 326, 326, 473 326, 326, 326, 326, 326, 326, 326, 326, 475 326, 326, 326, 326, 326, 326, 326, 326, 477 326, 326, 326, 326, 326, 326, 326, 326, 479 358, 358, 358, 358, 358, 358, 358, 358, 481 358, 358, 358, 358, 358, 358, 358, 358, 483 358, 358, 358, 358, 358, 358, 358, 358, 485 358, 358, 358, 358, 358, 358, 358, 358, 487 358, 358, 358, 358, 358, 358, 358, 358, 489 358, 358, 358, 358, 358, 358, 358, 358, 491 358, 358, 358, 358, 358, 358, 358, 358, 493 358, 358, 358, 358, 358, 358, 358, 358, 495 490, 490, 490, 490, 490, 490, 490, 490, 497 490, 490, 490, 490, 490, 490, 490, 490, 499 4113, 4113, 6161, 6161, 848, 848, 880, 880, 501 912, 912, 944, 944, 622, 622, 622, 622, 503 654, 654, 654, 654, 1104, 1104, 1136, 1136, 505 1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264, 507 686, 686, 686, 686, 1360, 1360, 1392, 1392, 509 12, 12, 12, 12, 12, 12, 12, 12, 511 390, 390, 390, 390, 390, 390, 390, 390, 513 390, 390, 390, 390, 390, 390, 390, 390, 515 390, 390, 390, 390, 390, 390, 390, 390, 517 390, 390, 390, 390, 390, 390, 390, 390, 519 390, 390, 390, 390, 390, 390, 390, 390, 521 390, 390, 390, 390, 390, 390, 390, 390, 523 390, 390, 390, 390, 390, 390, 390, 390, 525 390, 390, 390, 390, 390, 390, 390, 390, 527 }; 528 529 static byte twoDCodes[] = { 530 80, 88, 23, 71, 30, 30, 62, 62, 532 4, 4, 4, 4, 4, 4, 4, 4, 534 11, 11, 11, 11, 11, 11, 11, 11, 536 11, 11, 11, 11, 11, 11, 11, 11, 538 35, 35, 35, 35, 35, 35, 35, 35, 540 35, 35, 35, 35, 35, 35, 35, 35, 542 51, 51, 51, 51, 51, 51, 51, 51, 544 51, 51, 51, 51, 51, 51, 51, 51, 546 41, 41, 41, 41, 41, 41, 41, 41, 548 41, 41, 41, 41, 41, 41, 41, 41, 550 41, 41, 41, 41, 41, 41, 41, 41, 552 41, 41, 41, 41, 41, 41, 41, 41, 554 41, 41, 41, 41, 41, 41, 41, 41, 556 41, 41, 41, 41, 41, 41, 41, 41, 558 41, 41, 41, 41, 41, 41, 41, 41, 560 41, 41, 41, 41, 41, 41, 41, 41, 562 }; 563 564 569 public TIFFFaxDecoder(int fillOrder, int w, int h) { 570 this.fillOrder = fillOrder; 571 this.w = w; 572 this.h = h; 573 574 this.bitPointer = 0; 575 this.bytePointer = 0; 576 this.prevChangingElems = new int[w]; 577 this.currChangingElems = new int[w]; 578 } 579 580 582 public void decode1D(byte[] buffer, byte[] compData, 583 int startX, int height) { 584 this.data = compData; 585 586 int lineOffset = 0; 587 int scanlineStride = (w + 7)/8; 588 589 bitPointer = 0; 590 bytePointer = 0; 591 592 for (int i = 0; i < height; i++) { 593 decodeNextScanline(buffer, lineOffset, startX); 594 lineOffset += scanlineStride; 595 } 596 } 597 598 public void decodeNextScanline(byte[] buffer, 599 int lineOffset, int bitOffset) { 600 int bits = 0, code = 0, isT = 0; 601 int current, entry, twoBits; 602 boolean isWhite = true; 603 604 changingElemSize = 0; 606 607 while (bitOffset < w) { 609 while (isWhite) { 610 current = nextNBits(10); 612 entry = white[current]; 613 614 isT = entry & 0x0001; 616 bits = (entry >>> 1) & 0x0f; 617 618 if (bits == 12) { twoBits = nextLesserThan8Bits(2); 621 current = ((current << 2) & 0x000c) | twoBits; 623 entry = additionalMakeup[current]; 624 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; bitOffset += code; 628 updatePointer(4 - bits); 629 } else if (bits == 0) { throw new RuntimeException ("Invalid code encountered."); 631 } else if (bits == 15) { throw new RuntimeException ("EOL code word encountered in White run."); 633 } else { 634 code = (entry >>> 5) & 0x07ff; 636 bitOffset += code; 637 638 updatePointer(10 - bits); 639 if (isT == 0) { 640 isWhite = false; 641 currChangingElems[changingElemSize++] = bitOffset; 642 } 643 } 644 } 645 646 if (bitOffset == w) { 649 if (compression == 2) { 650 advancePointer(); 651 } 652 break; 653 } 654 655 while (!isWhite) { 656 current = nextLesserThan8Bits(4); 658 entry = initBlack[current]; 659 660 isT = entry & 0x0001; 662 bits = (entry >>> 1) & 0x000f; 663 code = (entry >>> 5) & 0x07ff; 664 665 if (code == 100) { 666 current = nextNBits(9); 667 entry = black[current]; 668 669 isT = entry & 0x0001; 671 bits = (entry >>> 1) & 0x000f; 672 code = (entry >>> 5) & 0x07ff; 673 674 if (bits == 12) { 675 updatePointer(5); 677 current = nextLesserThan8Bits(4); 678 entry = additionalMakeup[current]; 679 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; 682 setToBlack(buffer, lineOffset, bitOffset, code); 683 bitOffset += code; 684 685 updatePointer(4 - bits); 686 } else if (bits == 15) { 687 throw new RuntimeException ("EOL code word encountered in Black run."); 689 } else { 690 setToBlack(buffer, lineOffset, bitOffset, code); 691 bitOffset += code; 692 693 updatePointer(9 - bits); 694 if (isT == 0) { 695 isWhite = true; 696 currChangingElems[changingElemSize++] = bitOffset; 697 } 698 } 699 } else if (code == 200) { 700 current = nextLesserThan8Bits(2); 702 entry = twoBitBlack[current]; 703 code = (entry >>> 5) & 0x07ff; 704 bits = (entry >>> 1) & 0x0f; 705 706 setToBlack(buffer, lineOffset, bitOffset, code); 707 bitOffset += code; 708 709 updatePointer(2 - bits); 710 isWhite = true; 711 currChangingElems[changingElemSize++] = bitOffset; 712 } else { 713 setToBlack(buffer, lineOffset, bitOffset, code); 715 bitOffset += code; 716 717 updatePointer(4 - bits); 718 isWhite = true; 719 currChangingElems[changingElemSize++] = bitOffset; 720 } 721 } 722 723 if (bitOffset == w) { 725 if (compression == 2) { 726 advancePointer(); 727 } 728 break; 729 } 730 } 731 732 currChangingElems[changingElemSize++] = bitOffset; 733 } 734 735 737 public void decode2D(byte[] buffer, 738 byte compData[], 739 int startX, 740 int height, 741 long tiffT4Options) { 742 this.data = compData; 743 compression = 3; 744 745 bitPointer = 0; 746 bytePointer = 0; 747 748 int scanlineStride = (w + 7)/8; 749 750 int a0, a1, b1, b2; 751 int[] b = new int[2]; 752 int entry, code, bits; 753 boolean isWhite; 754 int currIndex = 0; 755 int temp[]; 756 757 760 762 763 oneD = (int)(tiffT4Options & 0x01); 764 uncompressedMode = (int)((tiffT4Options & 0x02) >> 1); 765 fillBits = (int)((tiffT4Options & 0x04) >> 2); 766 767 if (readEOL(true) != 1) { 769 throw new RuntimeException ("First scanline must be 1D encoded."); 770 } 771 772 int lineOffset = 0; 773 int bitOffset; 774 775 decodeNextScanline(buffer, lineOffset, startX); 778 lineOffset += scanlineStride; 779 780 for (int lines = 1; lines < height; lines++) { 781 782 if (readEOL(false) == 0) { 785 787 temp = prevChangingElems; 790 prevChangingElems = currChangingElems; 791 currChangingElems = temp; 792 currIndex = 0; 793 794 a0 = -1; 796 isWhite = true; 797 bitOffset = startX; 798 799 lastChangingElement = 0; 800 801 while (bitOffset < w) { 802 getNextChangingElement(a0, isWhite, b); 804 805 b1 = b[0]; 806 b2 = b[1]; 807 808 entry = nextLesserThan8Bits(7); 810 811 entry = (int)(twoDCodes[entry] & 0xff); 813 814 code = (entry & 0x78) >>> 3; 816 bits = entry & 0x07; 817 818 if (code == 0) { 819 if (!isWhite) { 820 setToBlack(buffer, lineOffset, bitOffset, 821 b2 - bitOffset); 822 } 823 bitOffset = a0 = b2; 824 825 updatePointer(7 - bits); 827 } else if (code == 1) { 828 updatePointer(7 - bits); 830 831 int number; 833 if (isWhite) { 834 number = decodeWhiteCodeWord(); 835 bitOffset += number; 836 currChangingElems[currIndex++] = bitOffset; 837 838 number = decodeBlackCodeWord(); 839 setToBlack(buffer, lineOffset, bitOffset, number); 840 bitOffset += number; 841 currChangingElems[currIndex++] = bitOffset; 842 } else { 843 number = decodeBlackCodeWord(); 844 setToBlack(buffer, lineOffset, bitOffset, number); 845 bitOffset += number; 846 currChangingElems[currIndex++] = bitOffset; 847 848 number = decodeWhiteCodeWord(); 849 bitOffset += number; 850 currChangingElems[currIndex++] = bitOffset; 851 } 852 853 a0 = bitOffset; 854 } else if (code <= 8) { 855 a1 = b1 + (code - 5); 857 858 currChangingElems[currIndex++] = a1; 859 860 if (!isWhite) { 863 setToBlack(buffer, lineOffset, bitOffset, 864 a1 - bitOffset); 865 } 866 bitOffset = a0 = a1; 867 isWhite = !isWhite; 868 869 updatePointer(7 - bits); 870 } else { 871 throw new RuntimeException ("Invalid code encountered while decoding 2D group 3 compressed data."); 872 } 873 } 874 875 currChangingElems[currIndex++] = bitOffset; 878 changingElemSize = currIndex; 879 } else { 880 decodeNextScanline(buffer, lineOffset, startX); 882 } 883 884 lineOffset += scanlineStride; 885 } 886 } 887 888 public synchronized void decodeT6(byte[] buffer, 889 byte[] compData, 890 int startX, 891 int height, 892 long tiffT6Options) { 893 this.data = compData; 894 compression = 4; 895 896 bitPointer = 0; 897 bytePointer = 0; 898 899 int scanlineStride = (w + 7)/8; 900 901 int a0, a1, b1, b2; 902 int entry, code, bits; 903 boolean isWhite; 904 int currIndex; 905 int temp[]; 906 907 int[] b = new int[2]; 909 910 913 uncompressedMode = (int)((tiffT6Options & 0x02) >> 1); 914 915 int[] cce = currChangingElems; 917 918 changingElemSize = 0; 922 cce[changingElemSize++] = w; 923 cce[changingElemSize++] = w; 924 925 int lineOffset = 0; 926 int bitOffset; 927 928 for (int lines = 0; lines < height; lines++) { 929 a0 = -1; 931 isWhite = true; 932 933 temp = prevChangingElems; 937 prevChangingElems = currChangingElems; 938 cce = currChangingElems = temp; 939 currIndex = 0; 940 941 bitOffset = startX; 943 944 lastChangingElement = 0; 946 947 while (bitOffset < w) { 949 getNextChangingElement(a0, isWhite, b); 951 b1 = b[0]; 952 b2 = b[1]; 953 954 entry = nextLesserThan8Bits(7); 956 entry = (int)(twoDCodes[entry] & 0xff); 958 959 code = (entry & 0x78) >>> 3; 961 bits = entry & 0x07; 962 963 if (code == 0) { if (!isWhite) { 966 setToBlack(buffer, lineOffset, bitOffset, 967 b2 - bitOffset); 968 } 969 bitOffset = a0 = b2; 970 971 updatePointer(7 - bits); 973 } else if (code == 1) { updatePointer(7 - bits); 976 977 int number; 979 if (isWhite) { 980 number = decodeWhiteCodeWord(); 982 bitOffset += number; 983 cce[currIndex++] = bitOffset; 984 985 number = decodeBlackCodeWord(); 986 setToBlack(buffer, lineOffset, bitOffset, number); 987 bitOffset += number; 988 cce[currIndex++] = bitOffset; 989 } else { 990 number = decodeBlackCodeWord(); 992 setToBlack(buffer, lineOffset, bitOffset, number); 993 bitOffset += number; 994 cce[currIndex++] = bitOffset; 995 996 number = decodeWhiteCodeWord(); 997 bitOffset += number; 998 cce[currIndex++] = bitOffset; 999 } 1000 1001 a0 = bitOffset; 1002 } else if (code <= 8) { a1 = b1 + (code - 5); 1004 cce[currIndex++] = a1; 1005 1006 if (!isWhite) { 1009 setToBlack(buffer, lineOffset, bitOffset, 1010 a1 - bitOffset); 1011 } 1012 bitOffset = a0 = a1; 1013 isWhite = !isWhite; 1014 1015 updatePointer(7 - bits); 1016 } else if (code == 11) { 1017 if (nextLesserThan8Bits(3) != 7) { 1018 throw new RuntimeException ("Invalid code encountered while decoding 2D group 4 compressed data."); 1019 } 1020 1021 int zeros = 0; 1022 boolean exit = false; 1023 1024 while (!exit) { 1025 while (nextLesserThan8Bits(1) != 1) { 1026 zeros++; 1027 } 1028 1029 if (zeros > 5) { 1030 1032 zeros = zeros - 6; 1034 1035 if (!isWhite && (zeros > 0)) { 1036 cce[currIndex++] = bitOffset; 1037 } 1038 1039 bitOffset += zeros; 1041 if (zeros > 0) { 1042 isWhite = true; 1044 } 1045 1046 if (nextLesserThan8Bits(1) == 0) { 1049 if (!isWhite) { 1050 cce[currIndex++] = bitOffset; 1051 } 1052 isWhite = true; 1053 } else { 1054 if (isWhite) { 1055 cce[currIndex++] = bitOffset; 1056 } 1057 isWhite = false; 1058 } 1059 1060 exit = true; 1061 } 1062 1063 if (zeros == 5) { 1064 if (!isWhite) { 1065 cce[currIndex++] = bitOffset; 1066 } 1067 bitOffset += zeros; 1068 1069 isWhite = true; 1071 } else { 1072 bitOffset += zeros; 1073 1074 cce[currIndex++] = bitOffset; 1075 setToBlack(buffer, lineOffset, bitOffset, 1); 1076 ++bitOffset; 1077 1078 isWhite = false; 1080 } 1081 1082 } 1083 } else { 1084 throw new RuntimeException ("Invalid code encountered while decoding 2D group 4 compressed data."); 1085 } 1086 } 1087 1088 if(currIndex < cce.length) 1092 cce[currIndex++] = bitOffset; 1093 1094 changingElemSize = currIndex; 1096 1097 lineOffset += scanlineStride; 1098 } 1099 } 1100 1101 private void setToBlack(byte[] buffer, 1102 int lineOffset, int bitOffset, 1103 int numBits) { 1104 int bitNum = 8*lineOffset + bitOffset; 1105 int lastBit = bitNum + numBits; 1106 1107 int byteNum = bitNum >> 3; 1108 1109 int shift = bitNum & 0x7; 1111 if (shift > 0) { 1112 int maskVal = 1 << (7 - shift); 1113 byte val = buffer[byteNum]; 1114 while (maskVal > 0 && bitNum < lastBit) { 1115 val |= maskVal; 1116 maskVal >>= 1; 1117 ++bitNum; 1118 } 1119 buffer[byteNum] = val; 1120 } 1121 1122 byteNum = bitNum >> 3; 1124 while (bitNum < lastBit - 7) { 1125 buffer[byteNum++] = (byte)255; 1126 bitNum += 8; 1127 } 1128 1129 while (bitNum < lastBit) { 1131 byteNum = bitNum >> 3; 1132 buffer[byteNum] |= 1 << (7 - (bitNum & 0x7)); 1133 ++bitNum; 1134 } 1135 } 1136 1137 private int decodeWhiteCodeWord() { 1139 int current, entry, bits, isT, twoBits, code = -1; 1140 int runLength = 0; 1141 boolean isWhite = true; 1142 1143 while (isWhite) { 1144 current = nextNBits(10); 1145 entry = white[current]; 1146 1147 isT = entry & 0x0001; 1149 bits = (entry >>> 1) & 0x0f; 1150 1151 if (bits == 12) { twoBits = nextLesserThan8Bits(2); 1154 current = ((current << 2) & 0x000c) | twoBits; 1156 entry = additionalMakeup[current]; 1157 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; runLength += code; 1160 updatePointer(4 - bits); 1161 } else if (bits == 0) { throw new RuntimeException ("Invalid code encountered."); 1163 } else if (bits == 15) { throw new RuntimeException ("EOL code word encountered in White run."); 1165 } else { 1166 code = (entry >>> 5) & 0x07ff; 1168 runLength += code; 1169 updatePointer(10 - bits); 1170 if (isT == 0) { 1171 isWhite = false; 1172 } 1173 } 1174 } 1175 1176 return runLength; 1177 } 1178 1179 private int decodeBlackCodeWord() { 1181 int current, entry, bits, isT, code = -1; 1182 int runLength = 0; 1183 boolean isWhite = false; 1184 1185 while (!isWhite) { 1186 current = nextLesserThan8Bits(4); 1187 entry = initBlack[current]; 1188 1189 isT = entry & 0x0001; 1191 bits = (entry >>> 1) & 0x000f; 1192 code = (entry >>> 5) & 0x07ff; 1193 1194 if (code == 100) { 1195 current = nextNBits(9); 1196 entry = black[current]; 1197 1198 isT = entry & 0x0001; 1200 bits = (entry >>> 1) & 0x000f; 1201 code = (entry >>> 5) & 0x07ff; 1202 1203 if (bits == 12) { 1204 updatePointer(5); 1206 current = nextLesserThan8Bits(4); 1207 entry = additionalMakeup[current]; 1208 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; runLength += code; 1211 1212 updatePointer(4 - bits); 1213 } else if (bits == 15) { 1214 throw new RuntimeException ("EOL code word encountered in Black run."); 1216 } else { 1217 runLength += code; 1218 updatePointer(9 - bits); 1219 if (isT == 0) { 1220 isWhite = true; 1221 } 1222 } 1223 } else if (code == 200) { 1224 current = nextLesserThan8Bits(2); 1226 entry = twoBitBlack[current]; 1227 code = (entry >>> 5) & 0x07ff; 1228 runLength += code; 1229 bits = (entry >>> 1) & 0x0f; 1230 updatePointer(2 - bits); 1231 isWhite = true; 1232 } else { 1233 runLength += code; 1235 updatePointer(4 - bits); 1236 isWhite = true; 1237 } 1238 } 1239 1240 return runLength; 1241 } 1242 1243 private int readEOL(boolean isFirstEOL) { 1244 if (fillBits == 0) { 1245 int next12Bits = nextNBits(12); 1246 if (isFirstEOL && next12Bits == 0) { 1247 1248 1253 if(nextNBits(4) == 1) { 1254 1255 1257 fillBits = 1; 1258 return 1; 1259 } 1260 } 1261 if(next12Bits != 1) { 1262 throw new RuntimeException ("Scanline must begin with EOL code word."); 1263 } 1264 } else if (fillBits == 1) { 1265 1266 1270 int bitsLeft = 8 - bitPointer; 1271 1272 if (nextNBits(bitsLeft) != 0) { 1273 throw new RuntimeException ("All fill bits preceding EOL code must be 0."); 1274 } 1275 1276 if (bitsLeft < 4) { 1281 if (nextNBits(8) != 0) { 1282 throw new RuntimeException ("All fill bits preceding EOL code must be 0."); 1283 } 1284 } 1285 1286 int n; 1290 while ((n = nextNBits(8)) != 1) { 1291 1292 if (n != 0) { 1294 throw new RuntimeException ("All fill bits preceding EOL code must be 0."); 1295 } 1296 } 1297 } 1298 1299 if (oneD == 0) { 1301 return 1; 1302 } else { 1303 return nextLesserThan8Bits(1); 1306 } 1307 } 1308 1309 private void getNextChangingElement(int a0, boolean isWhite, int[] ret) { 1310 int[] pce = this.prevChangingElems; 1312 int ces = this.changingElemSize; 1313 1314 int start = lastChangingElement > 0 ? lastChangingElement - 1 : 0; 1318 if (isWhite) { 1319 start &= ~0x1; } else { 1321 start |= 0x1; } 1323 1324 int i = start; 1325 for (; i < ces; i += 2) { 1326 int temp = pce[i]; 1327 if (temp > a0) { 1328 lastChangingElement = i; 1329 ret[0] = temp; 1330 break; 1331 } 1332 } 1333 1334 if (i + 1 < ces) { 1335 ret[1] = pce[i + 1]; 1336 } 1337 } 1338 1339 private int nextNBits(int bitsToGet) { 1340 byte b, next, next2next; 1341 int l = data.length - 1; 1342 int bp = this.bytePointer; 1343 1344 if (fillOrder == 1) { 1345 b = data[bp]; 1346 1347 if (bp == l) { 1348 next = 0x00; 1349 next2next = 0x00; 1350 } else if ((bp + 1) == l) { 1351 next = data[bp + 1]; 1352 next2next = 0x00; 1353 } else { 1354 next = data[bp + 1]; 1355 next2next = data[bp + 2]; 1356 } 1357 } else if (fillOrder == 2) { 1358 b = flipTable[data[bp] & 0xff]; 1359 1360 if (bp == l) { 1361 next = 0x00; 1362 next2next = 0x00; 1363 } else if ((bp + 1) == l) { 1364 next = flipTable[data[bp + 1] & 0xff]; 1365 next2next = 0x00; 1366 } else { 1367 next = flipTable[data[bp + 1] & 0xff]; 1368 next2next = flipTable[data[bp + 2] & 0xff]; 1369 } 1370 } else { 1371 throw new RuntimeException ("TIFF_FILL_ORDER tag must be either 1 or 2."); 1372 } 1373 1374 int bitsLeft = 8 - bitPointer; 1375 int bitsFromNextByte = bitsToGet - bitsLeft; 1376 int bitsFromNext2NextByte = 0; 1377 if (bitsFromNextByte > 8) { 1378 bitsFromNext2NextByte = bitsFromNextByte - 8; 1379 bitsFromNextByte = 8; 1380 } 1381 1382 bytePointer++; 1383 1384 int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft); 1385 int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte); 1386 1387 int i3 = 0; 1388 if (bitsFromNext2NextByte != 0) { 1389 i2 <<= bitsFromNext2NextByte; 1390 i3 = (next2next & table2[bitsFromNext2NextByte]) >>> 1391 (8 - bitsFromNext2NextByte); 1392 i2 |= i3; 1393 bytePointer++; 1394 bitPointer = bitsFromNext2NextByte; 1395 } else { 1396 if (bitsFromNextByte == 8) { 1397 bitPointer = 0; 1398 bytePointer++; 1399 } else { 1400 bitPointer = bitsFromNextByte; 1401 } 1402 } 1403 1404 int i = i1 | i2; 1405 return i; 1406 } 1407 1408 private int nextLesserThan8Bits(int bitsToGet) { 1409 byte b, next; 1410 int l = data.length - 1; 1411 int bp = this.bytePointer; 1412 1413 if (fillOrder == 1) { 1414 b = data[bp]; 1415 if (bp == l) { 1416 next = 0x00; 1417 } else { 1418 next = data[bp + 1]; 1419 } 1420 } else if (fillOrder == 2) { 1421 b = flipTable[data[bp] & 0xff]; 1422 if (bp == l) { 1423 next = 0x00; 1424 } else { 1425 next = flipTable[data[bp + 1] & 0xff]; 1426 } 1427 } else { 1428 throw new RuntimeException ("TIFF_FILL_ORDER tag must be either 1 or 2."); 1429 } 1430 1431 int bitsLeft = 8 - bitPointer; 1432 int bitsFromNextByte = bitsToGet - bitsLeft; 1433 1434 int shift = bitsLeft - bitsToGet; 1435 int i1, i2; 1436 if (shift >= 0) { 1437 i1 = (b & table1[bitsLeft]) >>> shift; 1438 bitPointer += bitsToGet; 1439 if (bitPointer == 8) { 1440 bitPointer = 0; 1441 bytePointer++; 1442 } 1443 } else { 1444 i1 = (b & table1[bitsLeft]) << (-shift); 1445 i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte); 1446 1447 i1 |= i2; 1448 bytePointer++; 1449 bitPointer = bitsFromNextByte; 1450 } 1451 1452 return i1; 1453 } 1454 1455 private void updatePointer(int bitsToMoveBack) { 1457 int i = bitPointer - bitsToMoveBack; 1458 1459 if (i < 0) { 1460 bytePointer--; 1461 bitPointer = 8 + i; 1462 } else { 1463 bitPointer = i; 1464 } 1465 } 1466 1467 private boolean advancePointer() { 1469 if (bitPointer != 0) { 1470 bytePointer++; 1471 bitPointer = 0; 1472 } 1473 1474 return true; 1475 } 1476} 1477 1478 | Popular Tags |