1 18 package org.apache.batik.ext.awt.image.codec.tiff; 19 20 21 class TIFFFaxDecoder { 22 23 private int bitPointer, bytePointer; 24 private byte[] data; 25 private int w, h; 26 private int fillOrder; 27 28 private int changingElemSize = 0; 31 private int prevChangingElems[]; 32 private int currChangingElems[]; 33 34 private int lastChangingElement = 0; 36 37 private int compression = 2; 38 39 private int uncompressedMode = 0; 41 private int fillBits = 0; 42 private int oneD; 43 44 static int table1[] = { 45 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; 55 56 static int table2[] = { 57 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 67 68 static byte flipTable[] = { 70 0, -128, 64, -64, 32, -96, 96, -32, 71 16, -112, 80, -48, 48, -80, 112, -16, 72 8, -120, 72, -56, 40, -88, 104, -24, 73 24, -104, 88, -40, 56, -72, 120, -8, 74 4, -124, 68, -60, 36, -92, 100, -28, 75 20, -108, 84, -44, 52, -76, 116, -12, 76 12, -116, 76, -52, 44, -84, 108, -20, 77 28, -100, 92, -36, 60, -68, 124, -4, 78 2, -126, 66, -62, 34, -94, 98, -30, 79 18, -110, 82, -46, 50, -78, 114, -14, 80 10, -118, 74, -54, 42, -86, 106, -22, 81 26, -102, 90, -38, 58, -70, 122, -6, 82 6, -122, 70, -58, 38, -90, 102, -26, 83 22, -106, 86, -42, 54, -74, 118, -10, 84 14, -114, 78, -50, 46, -82, 110, -18, 85 30, -98, 94, -34, 62, -66, 126, -2, 86 1, -127, 65, -63, 33, -95, 97, -31, 87 17, -111, 81, -47, 49, -79, 113, -15, 88 9, -119, 73, -55, 41, -87, 105, -23, 89 25, -103, 89, -39, 57, -71, 121, -7, 90 5, -123, 69, -59, 37, -91, 101, -27, 91 21, -107, 85, -43, 53, -75, 117, -11, 92 13, -115, 77, -51, 45, -83, 109, -19, 93 29, -99, 93, -35, 61, -67, 125, -3, 94 3, -125, 67, -61, 35, -93, 99, -29, 95 19, -109, 83, -45, 51, -77, 115, -13, 96 11, -117, 75, -53, 43, -85, 107, -21, 97 27, -101, 91, -37, 59, -69, 123, -5, 98 7, -121, 71, -57, 39, -89, 103, -25, 99 23, -105, 87, -41, 55, -73, 119, -9, 100 15, -113, 79, -49, 47, -81, 111, -17, 101 31, -97, 95, -33, 63, -65, 127, -1, 102 }; 103 104 static short white[] = { 106 6430, 6400, 6400, 6400, 3225, 3225, 3225, 3225, 108 944, 944, 944, 944, 976, 976, 976, 976, 110 1456, 1456, 1456, 1456, 1488, 1488, 1488, 1488, 112 718, 718, 718, 718, 718, 718, 718, 718, 114 750, 750, 750, 750, 750, 750, 750, 750, 116 1520, 1520, 1520, 1520, 1552, 1552, 1552, 1552, 118 428, 428, 428, 428, 428, 428, 428, 428, 120 428, 428, 428, 428, 428, 428, 428, 428, 122 654, 654, 654, 654, 654, 654, 654, 654, 124 1072, 1072, 1072, 1072, 1104, 1104, 1104, 1104, 126 1136, 1136, 1136, 1136, 1168, 1168, 1168, 1168, 128 1200, 1200, 1200, 1200, 1232, 1232, 1232, 1232, 130 622, 622, 622, 622, 622, 622, 622, 622, 132 1008, 1008, 1008, 1008, 1040, 1040, 1040, 1040, 134 44, 44, 44, 44, 44, 44, 44, 44, 136 44, 44, 44, 44, 44, 44, 44, 44, 138 396, 396, 396, 396, 396, 396, 396, 396, 140 396, 396, 396, 396, 396, 396, 396, 396, 142 1712, 1712, 1712, 1712, 1744, 1744, 1744, 1744, 144 846, 846, 846, 846, 846, 846, 846, 846, 146 1264, 1264, 1264, 1264, 1296, 1296, 1296, 1296, 148 1328, 1328, 1328, 1328, 1360, 1360, 1360, 1360, 150 1392, 1392, 1392, 1392, 1424, 1424, 1424, 1424, 152 686, 686, 686, 686, 686, 686, 686, 686, 154 910, 910, 910, 910, 910, 910, 910, 910, 156 1968, 1968, 1968, 1968, 2000, 2000, 2000, 2000, 158 2032, 2032, 2032, 2032, 16, 16, 16, 16, 160 10257, 10257, 10257, 10257, 12305, 12305, 12305, 12305, 162 330, 330, 330, 330, 330, 330, 330, 330, 164 330, 330, 330, 330, 330, 330, 330, 330, 166 330, 330, 330, 330, 330, 330, 330, 330, 168 330, 330, 330, 330, 330, 330, 330, 330, 170 362, 362, 362, 362, 362, 362, 362, 362, 172 362, 362, 362, 362, 362, 362, 362, 362, 174 362, 362, 362, 362, 362, 362, 362, 362, 176 362, 362, 362, 362, 362, 362, 362, 362, 178 878, 878, 878, 878, 878, 878, 878, 878, 180 1904, 1904, 1904, 1904, 1936, 1936, 1936, 1936, 182 -18413, -18413, -16365, -16365, -14317, -14317, -10221, -10221, 184 590, 590, 590, 590, 590, 590, 590, 590, 186 782, 782, 782, 782, 782, 782, 782, 782, 188 1584, 1584, 1584, 1584, 1616, 1616, 1616, 1616, 190 1648, 1648, 1648, 1648, 1680, 1680, 1680, 1680, 192 814, 814, 814, 814, 814, 814, 814, 814, 194 1776, 1776, 1776, 1776, 1808, 1808, 1808, 1808, 196 1840, 1840, 1840, 1840, 1872, 1872, 1872, 1872, 198 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157, 200 6157, 6157, 6157, 6157, 6157, 6157, 6157, 6157, 202 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275, 204 -12275, -12275, -12275, -12275, -12275, -12275, -12275, -12275, 206 14353, 14353, 14353, 14353, 16401, 16401, 16401, 16401, 208 22547, 22547, 24595, 24595, 20497, 20497, 20497, 20497, 210 18449, 18449, 18449, 18449, 26643, 26643, 28691, 28691, 212 30739, 30739, -32749, -32749, -30701, -30701, -28653, -28653, 214 -26605, -26605, -24557, -24557, -22509, -22509, -20461, -20461, 216 8207, 8207, 8207, 8207, 8207, 8207, 8207, 8207, 218 72, 72, 72, 72, 72, 72, 72, 72, 220 72, 72, 72, 72, 72, 72, 72, 72, 222 72, 72, 72, 72, 72, 72, 72, 72, 224 72, 72, 72, 72, 72, 72, 72, 72, 226 72, 72, 72, 72, 72, 72, 72, 72, 228 72, 72, 72, 72, 72, 72, 72, 72, 230 72, 72, 72, 72, 72, 72, 72, 72, 232 72, 72, 72, 72, 72, 72, 72, 72, 234 104, 104, 104, 104, 104, 104, 104, 104, 236 104, 104, 104, 104, 104, 104, 104, 104, 238 104, 104, 104, 104, 104, 104, 104, 104, 240 104, 104, 104, 104, 104, 104, 104, 104, 242 104, 104, 104, 104, 104, 104, 104, 104, 244 104, 104, 104, 104, 104, 104, 104, 104, 246 104, 104, 104, 104, 104, 104, 104, 104, 248 104, 104, 104, 104, 104, 104, 104, 104, 250 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 252 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 254 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 256 4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107, 258 266, 266, 266, 266, 266, 266, 266, 266, 260 266, 266, 266, 266, 266, 266, 266, 266, 262 266, 266, 266, 266, 266, 266, 266, 266, 264 266, 266, 266, 266, 266, 266, 266, 266, 266 298, 298, 298, 298, 298, 298, 298, 298, 268 298, 298, 298, 298, 298, 298, 298, 298, 270 298, 298, 298, 298, 298, 298, 298, 298, 272 298, 298, 298, 298, 298, 298, 298, 298, 274 524, 524, 524, 524, 524, 524, 524, 524, 276 524, 524, 524, 524, 524, 524, 524, 524, 278 556, 556, 556, 556, 556, 556, 556, 556, 280 556, 556, 556, 556, 556, 556, 556, 556, 282 136, 136, 136, 136, 136, 136, 136, 136, 284 136, 136, 136, 136, 136, 136, 136, 136, 286 136, 136, 136, 136, 136, 136, 136, 136, 288 136, 136, 136, 136, 136, 136, 136, 136, 290 136, 136, 136, 136, 136, 136, 136, 136, 292 136, 136, 136, 136, 136, 136, 136, 136, 294 136, 136, 136, 136, 136, 136, 136, 136, 296 136, 136, 136, 136, 136, 136, 136, 136, 298 168, 168, 168, 168, 168, 168, 168, 168, 300 168, 168, 168, 168, 168, 168, 168, 168, 302 168, 168, 168, 168, 168, 168, 168, 168, 304 168, 168, 168, 168, 168, 168, 168, 168, 306 168, 168, 168, 168, 168, 168, 168, 168, 308 168, 168, 168, 168, 168, 168, 168, 168, 310 168, 168, 168, 168, 168, 168, 168, 168, 312 168, 168, 168, 168, 168, 168, 168, 168, 314 460, 460, 460, 460, 460, 460, 460, 460, 316 460, 460, 460, 460, 460, 460, 460, 460, 318 492, 492, 492, 492, 492, 492, 492, 492, 320 492, 492, 492, 492, 492, 492, 492, 492, 322 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 324 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 326 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 328 2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059, 330 200, 200, 200, 200, 200, 200, 200, 200, 332 200, 200, 200, 200, 200, 200, 200, 200, 334 200, 200, 200, 200, 200, 200, 200, 200, 336 200, 200, 200, 200, 200, 200, 200, 200, 338 200, 200, 200, 200, 200, 200, 200, 200, 340 200, 200, 200, 200, 200, 200, 200, 200, 342 200, 200, 200, 200, 200, 200, 200, 200, 344 200, 200, 200, 200, 200, 200, 200, 200, 346 232, 232, 232, 232, 232, 232, 232, 232, 348 232, 232, 232, 232, 232, 232, 232, 232, 350 232, 232, 232, 232, 232, 232, 232, 232, 352 232, 232, 232, 232, 232, 232, 232, 232, 354 232, 232, 232, 232, 232, 232, 232, 232, 356 232, 232, 232, 232, 232, 232, 232, 232, 358 232, 232, 232, 232, 232, 232, 232, 232, 360 232, 232, 232, 232, 232, 232, 232, 232, 362 }; 363 364 static short additionalMakeup[] = { 366 28679, 28679, 31752, (short)32777, 367 (short)33801, (short)34825, (short)35849, (short)36873, 368 (short)29703, (short)29703, (short)30727, (short)30727, 369 (short)37897, (short)38921, (short)39945, (short)40969 370 }; 371 372 static short initBlack[] = { 374 3226, 6412, 200, 168, 38, 38, 134, 134, 376 100, 100, 100, 100, 68, 68, 68, 68 378 }; 379 380 static short twoBitBlack[] = {292, 260, 226, 226}; 383 static short black[] = { 385 62, 62, 30, 30, 0, 0, 0, 0, 387 0, 0, 0, 0, 0, 0, 0, 0, 389 0, 0, 0, 0, 0, 0, 0, 0, 391 0, 0, 0, 0, 0, 0, 0, 0, 393 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 395 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 397 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 399 3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225, 401 588, 588, 588, 588, 588, 588, 588, 588, 403 1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776, 405 1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904, 407 1936, 1936, -16365, -14317, 782, 782, 782, 782, 409 814, 814, 814, 814, -12269, -10221, 10257, 10257, 411 12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712, 413 1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605, 415 2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061, 417 424, 424, 424, 424, 424, 424, 424, 424, 419 424, 424, 424, 424, 424, 424, 424, 424, 421 424, 424, 424, 424, 424, 424, 424, 424, 423 424, 424, 424, 424, 424, 424, 424, 424, 425 750, 750, 750, 750, 1616, 1616, 1648, 1648, 427 1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520, 429 1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209, 431 524, 524, 524, 524, 524, 524, 524, 524, 433 556, 556, 556, 556, 556, 556, 556, 556, 435 1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032, 437 976, 976, 1008, 1008, 1040, 1040, 1072, 1072, 439 1296, 1296, 1328, 1328, 718, 718, 718, 718, 441 456, 456, 456, 456, 456, 456, 456, 456, 443 456, 456, 456, 456, 456, 456, 456, 456, 445 456, 456, 456, 456, 456, 456, 456, 456, 447 456, 456, 456, 456, 456, 456, 456, 456, 449 326, 326, 326, 326, 326, 326, 326, 326, 451 326, 326, 326, 326, 326, 326, 326, 326, 453 326, 326, 326, 326, 326, 326, 326, 326, 455 326, 326, 326, 326, 326, 326, 326, 326, 457 326, 326, 326, 326, 326, 326, 326, 326, 459 326, 326, 326, 326, 326, 326, 326, 326, 461 326, 326, 326, 326, 326, 326, 326, 326, 463 326, 326, 326, 326, 326, 326, 326, 326, 465 358, 358, 358, 358, 358, 358, 358, 358, 467 358, 358, 358, 358, 358, 358, 358, 358, 469 358, 358, 358, 358, 358, 358, 358, 358, 471 358, 358, 358, 358, 358, 358, 358, 358, 473 358, 358, 358, 358, 358, 358, 358, 358, 475 358, 358, 358, 358, 358, 358, 358, 358, 477 358, 358, 358, 358, 358, 358, 358, 358, 479 358, 358, 358, 358, 358, 358, 358, 358, 481 490, 490, 490, 490, 490, 490, 490, 490, 483 490, 490, 490, 490, 490, 490, 490, 490, 485 4113, 4113, 6161, 6161, 848, 848, 880, 880, 487 912, 912, 944, 944, 622, 622, 622, 622, 489 654, 654, 654, 654, 1104, 1104, 1136, 1136, 491 1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264, 493 686, 686, 686, 686, 1360, 1360, 1392, 1392, 495 12, 12, 12, 12, 12, 12, 12, 12, 497 390, 390, 390, 390, 390, 390, 390, 390, 499 390, 390, 390, 390, 390, 390, 390, 390, 501 390, 390, 390, 390, 390, 390, 390, 390, 503 390, 390, 390, 390, 390, 390, 390, 390, 505 390, 390, 390, 390, 390, 390, 390, 390, 507 390, 390, 390, 390, 390, 390, 390, 390, 509 390, 390, 390, 390, 390, 390, 390, 390, 511 390, 390, 390, 390, 390, 390, 390, 390, 513 }; 514 515 static byte twoDCodes[] = { 516 80, 88, 23, 71, 30, 30, 62, 62, 518 4, 4, 4, 4, 4, 4, 4, 4, 520 11, 11, 11, 11, 11, 11, 11, 11, 522 11, 11, 11, 11, 11, 11, 11, 11, 524 35, 35, 35, 35, 35, 35, 35, 35, 526 35, 35, 35, 35, 35, 35, 35, 35, 528 51, 51, 51, 51, 51, 51, 51, 51, 530 51, 51, 51, 51, 51, 51, 51, 51, 532 41, 41, 41, 41, 41, 41, 41, 41, 534 41, 41, 41, 41, 41, 41, 41, 41, 536 41, 41, 41, 41, 41, 41, 41, 41, 538 41, 41, 41, 41, 41, 41, 41, 41, 540 41, 41, 41, 41, 41, 41, 41, 41, 542 41, 41, 41, 41, 41, 41, 41, 41, 544 41, 41, 41, 41, 41, 41, 41, 41, 546 41, 41, 41, 41, 41, 41, 41, 41, 548 }; 549 550 556 public TIFFFaxDecoder(int fillOrder, int w, int h) { 557 this.fillOrder = fillOrder; 558 this.w = w; 559 this.h = h; 560 561 this.bitPointer = 0; 562 this.bytePointer = 0; 563 this.prevChangingElems = new int[w]; 564 this.currChangingElems = new int[w]; 565 } 566 567 569 public void decode1D(byte[] buffer, byte[] compData, 570 int startX, int height) { 571 this.data = compData; 572 573 int lineOffset = 0; 574 int scanlineStride = (w + 7)/8; 575 576 bitPointer = 0; 577 bytePointer = 0; 578 579 for (int i = 0; i < height; i++) { 580 decodeNextScanline(buffer, lineOffset, startX); 581 lineOffset += scanlineStride; 582 } 583 } 584 585 public void decodeNextScanline(byte[] buffer, 586 int lineOffset, int bitOffset) { 587 int bits = 0, code = 0, isT = 0; 588 int current, entry, twoBits; 589 boolean isWhite = true; 590 591 changingElemSize = 0; 593 594 while (bitOffset < w) { 596 while (isWhite) { 597 current = nextNBits(10); 599 entry = white[current]; 600 601 isT = entry & 0x0001; 603 bits = (entry >>> 1) & 0x0f; 604 605 if (bits == 12) { twoBits = nextLesserThan8Bits(2); 608 current = ((current << 2) & 0x000c) | twoBits; 610 entry = additionalMakeup[current]; 611 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; bitOffset += code; 615 updatePointer(4 - bits); 616 } else if (bits == 0) { throw new Error ("TIFFFaxDecoder0"); 618 } else if (bits == 15) { throw new Error ("TIFFFaxDecoder1"); 620 } else { 621 code = (entry >>> 5) & 0x07ff; 623 bitOffset += code; 624 625 updatePointer(10 - bits); 626 if (isT == 0) { 627 isWhite = false; 628 currChangingElems[changingElemSize++] = bitOffset; 629 } 630 } 631 } 632 633 if (bitOffset == w) { 636 if (compression == 2) { 637 advancePointer(); 638 } 639 break; 640 } 641 642 while (isWhite == false) { 643 current = nextLesserThan8Bits(4); 645 entry = initBlack[current]; 646 647 isT = entry & 0x0001; 649 bits = (entry >>> 1) & 0x000f; 650 code = (entry >>> 5) & 0x07ff; 651 652 if (code == 100) { 653 current = nextNBits(9); 654 entry = black[current]; 655 656 isT = entry & 0x0001; 658 bits = (entry >>> 1) & 0x000f; 659 code = (entry >>> 5) & 0x07ff; 660 661 if (bits == 12) { 662 updatePointer(5); 664 current = nextLesserThan8Bits(4); 665 entry = additionalMakeup[current]; 666 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; 669 setToBlack(buffer, lineOffset, bitOffset, code); 670 bitOffset += code; 671 672 updatePointer(4 - bits); 673 } else if (bits == 15) { 674 throw new Error ("TIFFFaxDecoder2"); 676 } else { 677 setToBlack(buffer, lineOffset, bitOffset, code); 678 bitOffset += code; 679 680 updatePointer(9 - bits); 681 if (isT == 0) { 682 isWhite = true; 683 currChangingElems[changingElemSize++] = bitOffset; 684 } 685 } 686 } else if (code == 200) { 687 current = nextLesserThan8Bits(2); 689 entry = twoBitBlack[current]; 690 code = (entry >>> 5) & 0x07ff; 691 bits = (entry >>> 1) & 0x0f; 692 693 setToBlack(buffer, lineOffset, bitOffset, code); 694 bitOffset += code; 695 696 updatePointer(2 - bits); 697 isWhite = true; 698 currChangingElems[changingElemSize++] = bitOffset; 699 } else { 700 setToBlack(buffer, lineOffset, bitOffset, code); 702 bitOffset += code; 703 704 updatePointer(4 - bits); 705 isWhite = true; 706 currChangingElems[changingElemSize++] = bitOffset; 707 } 708 } 709 710 if (bitOffset == w) { 712 if (compression == 2) { 713 advancePointer(); 714 } 715 break; 716 } 717 } 718 719 currChangingElems[changingElemSize++] = bitOffset; 720 } 721 722 724 public void decode2D(byte[] buffer, 725 byte compData[], 726 int startX, 727 int height, 728 long tiffT4Options) { 729 this.data = compData; 730 compression = 3; 731 732 bitPointer = 0; 733 bytePointer = 0; 734 735 int scanlineStride = (w + 7)/8; 736 737 int a0, a1, b1, b2; 738 int[] b = new int[2]; 739 int entry, code, bits; 740 boolean isWhite; 741 int currIndex = 0; 742 int temp[]; 743 744 747 749 750 oneD = (int)(tiffT4Options & 0x01); 751 uncompressedMode = (int)((tiffT4Options & 0x02) >> 1); 752 fillBits = (int)((tiffT4Options & 0x04) >> 2); 753 754 if (readEOL() != 1) { 756 throw new Error ("TIFFFaxDecoder3"); 757 } 758 759 int lineOffset = 0; 760 int bitOffset; 761 762 decodeNextScanline(buffer, lineOffset, startX); 765 lineOffset += scanlineStride; 766 767 for (int lines = 1; lines < height; lines++) { 768 769 if (readEOL() == 0) { 772 774 temp = prevChangingElems; 777 prevChangingElems = currChangingElems; 778 currChangingElems = temp; 779 currIndex = 0; 780 781 a0 = -1; 783 isWhite = true; 784 bitOffset = startX; 785 786 lastChangingElement = 0; 787 788 while (bitOffset < w) { 789 getNextChangingElement(a0, isWhite, b); 791 792 b1 = b[0]; 793 b2 = b[1]; 794 795 entry = nextLesserThan8Bits(7); 797 798 entry = (int)(twoDCodes[entry] & 0xff); 800 801 code = (entry & 0x78) >>> 3; 803 bits = entry & 0x07; 804 805 if (code == 0) { 806 if (!isWhite) { 807 setToBlack(buffer, lineOffset, bitOffset, 808 b2 - bitOffset); 809 } 810 bitOffset = a0 = b2; 811 812 updatePointer(7 - bits); 814 } else if (code == 1) { 815 updatePointer(7 - bits); 817 818 int number; 820 if (isWhite) { 821 number = decodeWhiteCodeWord(); 822 bitOffset += number; 823 currChangingElems[currIndex++] = bitOffset; 824 825 number = decodeBlackCodeWord(); 826 setToBlack(buffer, lineOffset, bitOffset, number); 827 bitOffset += number; 828 currChangingElems[currIndex++] = bitOffset; 829 } else { 830 number = decodeBlackCodeWord(); 831 setToBlack(buffer, lineOffset, bitOffset, number); 832 bitOffset += number; 833 currChangingElems[currIndex++] = bitOffset; 834 835 number = decodeWhiteCodeWord(); 836 bitOffset += number; 837 currChangingElems[currIndex++] = bitOffset; 838 } 839 840 a0 = bitOffset; 841 } else if (code <= 8) { 842 a1 = b1 + (code - 5); 844 845 currChangingElems[currIndex++] = a1; 846 847 if (!isWhite) { 850 setToBlack(buffer, lineOffset, bitOffset, 851 a1 - bitOffset); 852 } 853 bitOffset = a0 = a1; 854 isWhite = !isWhite; 855 856 updatePointer(7 - bits); 857 } else { 858 throw new Error ("TIFFFaxDecoder4"); 859 } 860 } 861 862 currChangingElems[currIndex++] = bitOffset; 865 changingElemSize = currIndex; 866 } else { 867 decodeNextScanline(buffer, lineOffset, startX); 869 } 870 871 lineOffset += scanlineStride; 872 } 873 } 874 875 public synchronized void decodeT6(byte[] buffer, 876 byte[] compData, 877 int startX, 878 int height, 879 long tiffT6Options) { 880 this.data = compData; 881 compression = 4; 882 883 bitPointer = 0; 884 bytePointer = 0; 885 886 int scanlineStride = (w + 7)/8; 887 888 int a0, a1, b1, b2; 889 int entry, code, bits; 890 boolean isWhite; 891 int currIndex; 892 int temp[]; 893 894 int[] b = new int[2]; 896 897 900 uncompressedMode = (int)((tiffT6Options & 0x02) >> 1); 901 902 int[] cce = currChangingElems; 904 905 changingElemSize = 0; 909 cce[changingElemSize++] = w; 910 cce[changingElemSize++] = w; 911 912 int lineOffset = 0; 913 int bitOffset; 914 915 for (int lines = 0; lines < height; lines++) { 916 a0 = -1; 918 isWhite = true; 919 920 temp = prevChangingElems; 924 prevChangingElems = currChangingElems; 925 cce = currChangingElems = temp; 926 currIndex = 0; 927 928 bitOffset = startX; 930 931 lastChangingElement = 0; 933 934 while (bitOffset < w) { 936 getNextChangingElement(a0, isWhite, b); 938 b1 = b[0]; 939 b2 = b[1]; 940 941 entry = nextLesserThan8Bits(7); 943 entry = (int)(twoDCodes[entry] & 0xff); 945 946 code = (entry & 0x78) >>> 3; 948 bits = entry & 0x07; 949 950 if (code == 0) { if (!isWhite) { 953 setToBlack(buffer, lineOffset, bitOffset, 954 b2 - bitOffset); 955 } 956 bitOffset = a0 = b2; 957 958 updatePointer(7 - bits); 960 } else if (code == 1) { updatePointer(7 - bits); 963 964 int number; 966 if (isWhite) { 967 number = decodeWhiteCodeWord(); 969 bitOffset += number; 970 cce[currIndex++] = bitOffset; 971 972 number = decodeBlackCodeWord(); 973 setToBlack(buffer, lineOffset, bitOffset, number); 974 bitOffset += number; 975 cce[currIndex++] = bitOffset; 976 } else { 977 number = decodeBlackCodeWord(); 979 setToBlack(buffer, lineOffset, bitOffset, number); 980 bitOffset += number; 981 cce[currIndex++] = bitOffset; 982 983 number = decodeWhiteCodeWord(); 984 bitOffset += number; 985 cce[currIndex++] = bitOffset; 986 } 987 988 a0 = bitOffset; 989 } else if (code <= 8) { a1 = b1 + (code - 5); 991 cce[currIndex++] = a1; 992 993 if (!isWhite) { 996 setToBlack(buffer, lineOffset, bitOffset, 997 a1 - bitOffset); 998 } 999 bitOffset = a0 = a1; 1000 isWhite = !isWhite; 1001 1002 updatePointer(7 - bits); 1003 } else if (code == 11) { 1004 if (nextLesserThan8Bits(3) != 7) { 1005 throw new Error ("TIFFFaxDecoder5"); 1006 } 1007 1008 int zeros = 0; 1009 boolean exit = false; 1010 1011 while (!exit) { 1012 while (nextLesserThan8Bits(1) != 1) { 1013 zeros++; 1014 } 1015 1016 if (zeros > 5) { 1017 1019 zeros = zeros - 6; 1021 1022 if (!isWhite && (zeros > 0)) { 1023 cce[currIndex++] = bitOffset; 1024 } 1025 1026 bitOffset += zeros; 1028 if (zeros > 0) { 1029 isWhite = true; 1031 } 1032 1033 if (nextLesserThan8Bits(1) == 0) { 1036 if (!isWhite) { 1037 cce[currIndex++] = bitOffset; 1038 } 1039 isWhite = true; 1040 } else { 1041 if (isWhite) { 1042 cce[currIndex++] = bitOffset; 1043 } 1044 isWhite = false; 1045 } 1046 1047 exit = true; 1048 } 1049 1050 if (zeros == 5) { 1051 if (!isWhite) { 1052 cce[currIndex++] = bitOffset; 1053 } 1054 bitOffset += zeros; 1055 1056 isWhite = true; 1058 } else { 1059 bitOffset += zeros; 1060 1061 cce[currIndex++] = bitOffset; 1062 setToBlack(buffer, lineOffset, bitOffset, 1); 1063 ++bitOffset; 1064 1065 isWhite = false; 1067 } 1068 1069 } 1070 } else { 1071 throw new Error ("TIFFFaxDecoder5"); 1072 } 1073 } 1074 1075 cce[currIndex++] = bitOffset; 1078 1079 changingElemSize = currIndex; 1081 1082 lineOffset += scanlineStride; 1083 } 1084 } 1085 1086 private void setToBlack(byte[] buffer, 1087 int lineOffset, int bitOffset, 1088 int numBits) { 1089 int bitNum = 8*lineOffset + bitOffset; 1090 int lastBit = bitNum + numBits; 1091 1092 int byteNum = bitNum >> 3; 1093 1094 int shift = bitNum & 0x7; 1096 if (shift > 0) { 1097 int maskVal = 1 << (7 - shift); 1098 byte val = buffer[byteNum]; 1099 while (maskVal > 0 && bitNum < lastBit) { 1100 val |= maskVal; 1101 maskVal >>= 1; 1102 ++bitNum; 1103 } 1104 buffer[byteNum] = val; 1105 } 1106 1107 byteNum = bitNum >> 3; 1109 while (bitNum < lastBit - 7) { 1110 buffer[byteNum++] = (byte)255; 1111 bitNum += 8; 1112 } 1113 1114 while (bitNum < lastBit) { 1116 byteNum = bitNum >> 3; 1117 buffer[byteNum] |= 1 << (7 - (bitNum & 0x7)); 1118 ++bitNum; 1119 } 1120 } 1121 1122 private int decodeWhiteCodeWord() { 1124 int current, entry, bits, isT, twoBits, code = -1; 1125 int runLength = 0; 1126 boolean isWhite = true; 1127 1128 while (isWhite) { 1129 current = nextNBits(10); 1130 entry = white[current]; 1131 1132 isT = entry & 0x0001; 1134 bits = (entry >>> 1) & 0x0f; 1135 1136 if (bits == 12) { twoBits = nextLesserThan8Bits(2); 1139 current = ((current << 2) & 0x000c) | twoBits; 1141 entry = additionalMakeup[current]; 1142 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; runLength += code; 1145 updatePointer(4 - bits); 1146 } else if (bits == 0) { throw new Error ("TIFFFaxDecoder0"); 1148 } else if (bits == 15) { throw new Error ("TIFFFaxDecoder1"); 1150 } else { 1151 code = (entry >>> 5) & 0x07ff; 1153 runLength += code; 1154 updatePointer(10 - bits); 1155 if (isT == 0) { 1156 isWhite = false; 1157 } 1158 } 1159 } 1160 1161 return runLength; 1162 } 1163 1164 private int decodeBlackCodeWord() { 1166 int current, entry, bits, isT, code = -1; 1167 int runLength = 0; 1168 boolean isWhite = false; 1169 1170 while (!isWhite) { 1171 current = nextLesserThan8Bits(4); 1172 entry = initBlack[current]; 1173 1174 isT = entry & 0x0001; 1176 bits = (entry >>> 1) & 0x000f; 1177 code = (entry >>> 5) & 0x07ff; 1178 1179 if (code == 100) { 1180 current = nextNBits(9); 1181 entry = black[current]; 1182 1183 isT = entry & 0x0001; 1185 bits = (entry >>> 1) & 0x000f; 1186 code = (entry >>> 5) & 0x07ff; 1187 1188 if (bits == 12) { 1189 updatePointer(5); 1191 current = nextLesserThan8Bits(4); 1192 entry = additionalMakeup[current]; 1193 bits = (entry >>> 1) & 0x07; code = (entry >>> 4) & 0x0fff; runLength += code; 1196 1197 updatePointer(4 - bits); 1198 } else if (bits == 15) { 1199 throw new Error ("TIFFFaxDecoder2"); 1201 } else { 1202 runLength += code; 1203 updatePointer(9 - bits); 1204 if (isT == 0) { 1205 isWhite = true; 1206 } 1207 } 1208 } else if (code == 200) { 1209 current = nextLesserThan8Bits(2); 1211 entry = twoBitBlack[current]; 1212 code = (entry >>> 5) & 0x07ff; 1213 runLength += code; 1214 bits = (entry >>> 1) & 0x0f; 1215 updatePointer(2 - bits); 1216 isWhite = true; 1217 } else { 1218 runLength += code; 1220 updatePointer(4 - bits); 1221 isWhite = true; 1222 } 1223 } 1224 1225 return runLength; 1226 } 1227 1228 private int readEOL() { 1229 if (fillBits == 0) { 1230 if (nextNBits(12) != 1) { 1231 throw new Error ("TIFFFaxDecoder6"); 1232 } 1233 } else if (fillBits == 1) { 1234 1235 1239 int bitsLeft = 8 - bitPointer; 1240 1241 if (nextNBits(bitsLeft) != 0) { 1242 throw new Error ("TIFFFaxDecoder8"); 1243 } 1244 1245 if (bitsLeft < 4) { 1250 if (nextNBits(8) != 0) { 1251 throw new Error ("TIFFFaxDecoder8"); 1252 } 1253 } 1254 1255 int n; 1259 while ((n = nextNBits(8)) != 1) { 1260 1261 if (n != 0) { 1263 throw new Error ("TIFFFaxDecoder8"); 1264 } 1265 } 1266 } 1267 1268 if (oneD == 0) { 1270 return 1; 1271 } else { 1272 return nextLesserThan8Bits(1); 1275 } 1276 } 1277 1278 private void getNextChangingElement(int a0, boolean isWhite, int[] ret) { 1279 int[] pce = this.prevChangingElems; 1281 int ces = this.changingElemSize; 1282 1283 int start = lastChangingElement > 0 ? lastChangingElement - 1 : 0; 1287 if (isWhite) { 1288 start &= ~0x1; } else { 1290 start |= 0x1; } 1292 1293 int i = start; 1294 for (; i < ces; i += 2) { 1295 int temp = pce[i]; 1296 if (temp > a0) { 1297 lastChangingElement = i; 1298 ret[0] = temp; 1299 break; 1300 } 1301 } 1302 1303 if (i + 1 < ces) { 1304 ret[1] = pce[i + 1]; 1305 } 1306 } 1307 1308 private int nextNBits(int bitsToGet) { 1309 byte b, next, next2next; 1310 int l = data.length - 1; 1311 int bp = this.bytePointer; 1312 1313 if (fillOrder == 1) { 1314 b = data[bp]; 1315 1316 if (bp == l) { 1317 next = 0x00; 1318 next2next = 0x00; 1319 } else if ((bp + 1) == l) { 1320 next = data[bp + 1]; 1321 next2next = 0x00; 1322 } else { 1323 next = data[bp + 1]; 1324 next2next = data[bp + 2]; 1325 } 1326 } else if (fillOrder == 2) { 1327 b = flipTable[data[bp] & 0xff]; 1328 1329 if (bp == l) { 1330 next = 0x00; 1331 next2next = 0x00; 1332 } else if ((bp + 1) == l) { 1333 next = flipTable[data[bp + 1] & 0xff]; 1334 next2next = 0x00; 1335 } else { 1336 next = flipTable[data[bp + 1] & 0xff]; 1337 next2next = flipTable[data[bp + 2] & 0xff]; 1338 } 1339 } else { 1340 throw new Error ("TIFFFaxDecoder7"); 1341 } 1342 1343 int bitsLeft = 8 - bitPointer; 1344 int bitsFromNextByte = bitsToGet - bitsLeft; 1345 int bitsFromNext2NextByte = 0; 1346 if (bitsFromNextByte > 8) { 1347 bitsFromNext2NextByte = bitsFromNextByte - 8; 1348 bitsFromNextByte = 8; 1349 } 1350 1351 bytePointer++; 1352 1353 int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft); 1354 int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte); 1355 1356 int i3 = 0; 1357 if (bitsFromNext2NextByte != 0) { 1358 i2 <<= bitsFromNext2NextByte; 1359 i3 = (next2next & table2[bitsFromNext2NextByte]) >>> 1360 (8 - bitsFromNext2NextByte); 1361 i2 |= i3; 1362 bytePointer++; 1363 bitPointer = bitsFromNext2NextByte; 1364 } else { 1365 if (bitsFromNextByte == 8) { 1366 bitPointer = 0; 1367 bytePointer++; 1368 } else { 1369 bitPointer = bitsFromNextByte; 1370 } 1371 } 1372 1373 int i = i1 | i2; 1374 return i; 1375 } 1376 1377 private int nextLesserThan8Bits(int bitsToGet) { 1378 byte b, next; 1379 int l = data.length - 1; 1380 int bp = this.bytePointer; 1381 1382 if (fillOrder == 1) { 1383 b = data[bp]; 1384 if (bp == l) { 1385 next = 0x00; 1386 } else { 1387 next = data[bp + 1]; 1388 } 1389 } else if (fillOrder == 2) { 1390 b = flipTable[data[bp] & 0xff]; 1391 if (bp == l) { 1392 next = 0x00; 1393 } else { 1394 next = flipTable[data[bp + 1] & 0xff]; 1395 } 1396 } else { 1397 throw new Error ("TIFFFaxDecoder7"); 1398 } 1399 1400 int bitsLeft = 8 - bitPointer; 1401 int bitsFromNextByte = bitsToGet - bitsLeft; 1402 1403 int shift = bitsLeft - bitsToGet; 1404 int i1, i2; 1405 if (shift >= 0) { 1406 i1 = (b & table1[bitsLeft]) >>> shift; 1407 bitPointer += bitsToGet; 1408 if (bitPointer == 8) { 1409 bitPointer = 0; 1410 bytePointer++; 1411 } 1412 } else { 1413 i1 = (b & table1[bitsLeft]) << (-shift); 1414 i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte); 1415 1416 i1 |= i2; 1417 bytePointer++; 1418 bitPointer = bitsFromNextByte; 1419 } 1420 1421 return i1; 1422 } 1423 1424 private void updatePointer(int bitsToMoveBack) { 1426 int i = bitPointer - bitsToMoveBack; 1427 1428 if (i < 0) { 1429 bytePointer--; 1430 bitPointer = 8 + i; 1431 } else { 1432 bitPointer = i; 1433 } 1434 } 1435 1436 private boolean advancePointer() { 1438 if (bitPointer != 0) { 1439 bytePointer++; 1440 bitPointer = 0; 1441 } 1442 1443 return true; 1444 } 1445} 1446 1447 | Popular Tags |