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