home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / tif_lzw.c < prev    next >
Text File  |  1996-11-18  |  32KB  |  1,232 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_lzw.c,v 1.49 93/08/26 14:25:12 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  * Rev 5.0 Lempel-Ziv & Welch Compression Support
  32.  *
  33.  * This code is derived from the compress program whose code is
  34.  * derived from software contributed to Berkeley by James A. Woods,
  35.  * derived from original work by Spencer Thomas and Joseph Orost.
  36.  *
  37.  * The original Berkeley copyright notice appears below in its entirety.
  38.  */
  39. #include "tiffiop.h"
  40. #include <assert.h>
  41. #include <stdio.h>
  42.  
  43. /*
  44.  * NB: The 5.0 spec describes a different algorithm than Aldus
  45.  *     implements.  Specifically, Aldus does code length transitions
  46.  *     one code earlier than should be done (for real LZW).
  47.  *     Earlier versions of this library implemented the correct
  48.  *     LZW algorithm, but emitted codes in a bit order opposite
  49.  *     to the TIFF spec.  Thus, to maintain compatibility w/ Aldus
  50.  *     we interpret MSB-LSB ordered codes to be images written w/
  51.  *     old versions of this library, but otherwise adhere to the
  52.  *     Aldus "off by one" algorithm.
  53.  *
  54.  * Future revisions to the TIFF spec are expected to "clarify this issue".
  55.  */
  56. #define    LZW_COMPAT        /* include backwards compatibility code */
  57. /*
  58.  * Each strip of data is supposed to be terminated by a CODE_EOI.
  59.  * If the following #define is included, the decoder will also
  60.  * check for end-of-strip w/o seeing this code.  This makes the
  61.  * library more robust, but also slower.
  62.  */
  63. #define    LZW_CHECKEOS        /* include checks for strips w/o EOI code */
  64.  
  65. #define MAXCODE(n)    ((1<<(n))-1)
  66. /*
  67.  * The TIFF spec specifies that encoded bit
  68.  * strings range from 9 to 12 bits.
  69.  */
  70. #define    BITS_MIN    9        /* start with 9 bits */
  71. #define    BITS_MAX    12        /* max of 12 bit strings */
  72. /* predefined codes */
  73. #define    CODE_CLEAR    256        /* code to clear string table */
  74. #define    CODE_EOI    257        /* end-of-information code */
  75. #define CODE_FIRST    258        /* first free code entry */
  76. #define    CODE_MAX    MAXCODE(BITS_MAX)
  77. #define    HSIZE        9001        /* 91% occupancy */
  78. #define    HSHIFT        (13-8)
  79. #ifdef LZW_COMPAT
  80. /* NB: +1024 is for compatibility with old files */
  81. #define    CSIZE        (MAXCODE(BITS_MAX)+1024)
  82. #else
  83. #define    CSIZE        (MAXCODE(BITS_MAX)+1)
  84. #endif
  85.  
  86. typedef    void (*predictorFunc)(char* data, u_long nbytes, int stride);
  87.  
  88. /*
  89.  * State block for each open TIFF
  90.  * file using LZW compression/decompression.
  91.  */
  92. typedef    struct {
  93.     predictorFunc hordiff;        /* horizontal differencing method */
  94.     u_long    rowsize;        /* width of tile/strip/row */
  95.     u_short    stride;            /* horizontal diferencing stride */
  96.     u_short    nbits;            /* # of bits/code */
  97.     u_short    maxcode;        /* maximum code for lzw_nbits */
  98.     u_short    free_ent;        /* next free entry in hash table */
  99.     long    nextdata;        /* next bits of i/o */
  100.     long    nextbits;        /* # of valid bits in lzw_nextdata */
  101. } LZWState;
  102.  
  103. #define    lzw_hordiff    base.hordiff
  104. #define    lzw_rowsize    base.rowsize
  105. #define    lzw_stride    base.stride
  106. #define    lzw_nbits    base.nbits
  107. #define    lzw_maxcode    base.maxcode
  108. #define    lzw_free_ent    base.free_ent
  109. #define    lzw_nextdata    base.nextdata
  110. #define    lzw_nextbits    base.nextbits
  111.  
  112. /*
  113.  * Decoding-specific state.
  114.  */
  115. typedef struct code_ent {
  116.     struct code_ent *next;
  117.     u_short    length;            /* string len, including this token */
  118.     u_char    value;            /* data value */
  119.     u_char    firstchar;        /* first token of string */
  120. } code_t;
  121.  
  122. typedef    int (*decodeFunc)(TIFF*, tidata_t, tsize_t, tsample_t);
  123.  
  124. typedef struct {
  125.     LZWState base;
  126.     long    dec_nbitsmask;        /* lzw_nbits 1 bits, right adjusted */
  127.     long    dec_restart;        /* restart count */
  128. #ifdef LZW_CHECKEOS
  129.     long    dec_bitsleft;        /* available bits in raw data */
  130. #endif
  131.     decodeFunc dec_decode;        /* regular or backwards compatible */
  132.     code_t    *dec_codep;        /* current recognized code */
  133.     code_t    *dec_oldcodep;        /* previously recognized code */
  134.     code_t    *dec_free_entp;        /* next free entry */
  135.     code_t    *dec_maxcodep;        /* max available entry */
  136.     code_t    dec_codetab[CSIZE];
  137. } LZWDecodeState;
  138.  
  139. /*
  140.  * Encoding-specific state.
  141.  */
  142. typedef struct {
  143.     long    hash;
  144.     long    code;
  145. } hash_t;
  146.  
  147. typedef struct {
  148.     LZWState base;
  149.     int    enc_oldcode;        /* last code encountered */
  150.     long    enc_checkpoint;        /* point at which to clear table */
  151. #define CHECK_GAP    10000        /* enc_ratio check interval */
  152.     long    enc_ratio;        /* current compression ratio */
  153.     long    enc_incount;        /* (input) data bytes encoded */
  154.     long    enc_outcount;        /* encoded (output) bytes */
  155.     tidata_t enc_rawlimit;        /* bound on tif_rawdata buffer */
  156.     hash_t    enc_hashtab[HSIZE];
  157. } LZWEncodeState;
  158.  
  159. static    int LZWEncodePredRow(TIFF*, tidata_t, tsize_t, tsample_t);
  160. static    int LZWEncodePredTile(TIFF*, tidata_t, tsize_t, tsample_t);
  161. static    int LZWDecode(TIFF*, tidata_t, tsize_t, tsample_t);
  162. #ifdef LZW_COMPAT
  163. static    int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, tsample_t);
  164. #endif
  165. static    int LZWDecodePredRow(TIFF*, tidata_t, tsize_t, tsample_t);
  166. static    int LZWDecodePredTile(TIFF*, tidata_t, tsize_t, tsample_t);
  167. static    void cl_hash(LZWEncodeState*);
  168.  
  169. static int
  170. LZWCheckPredictor(TIFF* tif,
  171.     LZWState* sp, predictorFunc pred8bit, predictorFunc pred16bit)
  172. {
  173.     TIFFDirectory *td = &tif->tif_dir;
  174.  
  175.     sp->hordiff = NULL;
  176.     switch (td->td_predictor) {
  177.     case 1:
  178.         break;
  179.     case 2:
  180.         sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  181.             td->td_samplesperpixel : 1);
  182.         switch (td->td_bitspersample) {
  183.         case 8:
  184.             sp->hordiff = pred8bit;
  185.             break;
  186.         case 16:
  187.             sp->hordiff = pred16bit;
  188.             break;
  189.         default:
  190.             TIFFError(tif->tif_name,
  191.     "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
  192.                 td->td_bitspersample);
  193.             return (0);
  194.         }
  195.         break;
  196.     default:
  197.         TIFFError(tif->tif_name, "\"Predictor\" value %d not supported",
  198.             td->td_predictor);
  199.         return (0);
  200.     }
  201.     if (sp->hordiff != NULL) {
  202.         /*
  203.          * Calculate the scanline/tile-width size in bytes.
  204.          */
  205.         if (isTiled(tif))
  206.             sp->rowsize = TIFFTileRowSize(tif);
  207.         else
  208.             sp->rowsize = TIFFScanlineSize(tif);
  209.     } else
  210.         sp->rowsize = 0;
  211.     return (1);
  212. }
  213.  
  214. /*
  215.  * LZW Decoder.
  216.  */
  217.  
  218. #ifdef LZW_CHECKEOS
  219. /*
  220.  * This check shouldn't be necessary because each
  221.  * strip is suppose to be terminated with CODE_EOI.
  222.  */
  223. #define    NextCode(tif, sp, bp, code, get) {                \
  224.     if ((sp)->dec_bitsleft < nbits) {                \
  225.         TIFFWarning(tif->tif_name,                \
  226.             "LZWDecode: Strip %d not terminated with EOI code", \
  227.             tif->tif_curstrip);                    \
  228.         code = CODE_EOI;                    \
  229.     } else {                            \
  230.         get(sp, bp, code);                    \
  231.         (sp)->dec_bitsleft -= nbits;                \
  232.     }                                \
  233. }
  234. #else
  235. #define    NextCode(tif, sp, bp, code, get) get(sp, bp, code)
  236. #endif
  237.  
  238. #define REPEAT4(n, op)        \
  239.     switch (n) {        \
  240.     default: { int i; for (i = n-4; i > 0; i--) { op; } } \
  241.     case 4:  op;        \
  242.     case 3:  op;        \
  243.     case 2:  op;        \
  244.     case 1:  op;        \
  245.     case 0:  ;            \
  246.     }
  247. #define XREPEAT4(n, op)        \
  248.     switch (n) {        \
  249.     default: { int i; for (i = n-4; i > 0; i--) { op; } } \
  250.     case 2:  op;        \
  251.     case 1:  op;        \
  252.     case 0:  ;            \
  253.     }
  254.  
  255. static void
  256. horAcc8(register char* cp, register u_long cc, register int stride)
  257. {
  258.     if (cc > stride) {
  259.         cc -= stride;
  260.         /*
  261.          * Pipeline the most common cases.
  262.          */
  263.         if (stride == 3)  {
  264.             u_int cr = cp[0];
  265.             u_int cg = cp[1];
  266.             u_int cb = cp[2];
  267.             do {
  268.                 cc -= 3, cp += 3;
  269.                 cp[0] = (cr += cp[0]);
  270.                 cp[1] = (cg += cp[1]);
  271.                 cp[2] = (cb += cp[2]);
  272.             } while ((long)cc > 0);
  273.         } else if (stride == 4)  {
  274.             u_int cr = cp[0];
  275.             u_int cg = cp[1];
  276.             u_int cb = cp[2];
  277.             u_int ca = cp[3];
  278.             do {
  279.                 cc -= 4, cp += 4;
  280.                 cp[0] = (cr += cp[0]);
  281.                 cp[1] = (cg += cp[1]);
  282.                 cp[2] = (cb += cp[2]);
  283.                 cp[3] = (ca += cp[3]);
  284.             } while ((long)cc > 0);
  285.         } else  {
  286.             do {
  287.                 XREPEAT4(stride, cp[stride] += *cp; cp++)
  288.                 cc -= stride;
  289.             } while ((long)cc > 0);
  290.         }
  291.     }
  292. }
  293.  
  294. static void
  295. swabHorAcc16(char* cp, u_long cc, register int stride)
  296. {
  297.     register uint16* wp = (uint16 *)cp;
  298.     register u_long wc = cc / 2;
  299.  
  300.     if (wc > stride) {
  301.         TIFFSwabArrayOfShort(wp, wc);
  302.         wc -= stride;
  303.         do {
  304.             REPEAT4(stride, wp[stride] += wp[0]; wp++)
  305.             wc -= stride;
  306.         } while (wc > 0);
  307.     }
  308. }
  309.  
  310. static void
  311. horAcc16(char* cp, u_long cc, register int stride)
  312. {
  313.     register uint16* wp = (uint16 *)cp;
  314.     register u_long wc = cc / 2;
  315.  
  316.     if (wc > stride) {
  317.         wc -= stride;
  318.         do {
  319.             REPEAT4(stride, wp[stride] += wp[0]; wp++)
  320.             wc -= stride;
  321.         } while (wc > 0);
  322.     }
  323. }
  324.  
  325. /*
  326.  * Setup state for decoding a strip.
  327.  */
  328. static int
  329. LZWPreDecode(TIFF* tif)
  330. {
  331.     register LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  332.  
  333.     if (sp == NULL) {
  334.         tif->tif_data = _TIFFmalloc(sizeof (LZWDecodeState));
  335.         if (tif->tif_data == NULL) {
  336.             TIFFError("LZWPreDecode",
  337.                 "No space for LZW state block");
  338.             return (0);
  339.         }
  340.         sp = (LZWDecodeState *)tif->tif_data;
  341.         sp->dec_decode = NULL;
  342.         if (!LZWCheckPredictor(tif, &sp->base, horAcc8, horAcc16))
  343.             return (0);
  344.         if (sp->lzw_hordiff) {
  345.             /*
  346.              * Override default decoding method with
  347.              * one that does the predictor stuff.
  348.              */
  349.             tif->tif_decoderow = LZWDecodePredRow;
  350.             tif->tif_decodestrip = LZWDecodePredTile;
  351.             tif->tif_decodetile = LZWDecodePredTile;
  352.             /*
  353.              * If the data is horizontally differenced
  354.              * 16-bit data that requires byte-swapping,
  355.              * then it must be byte swapped before the
  356.              * accumulation step.  We do this with a
  357.              * special-purpose routine and override the
  358.              * normal post decoding logic that the library
  359.              * setup when the directory was read.
  360.              */
  361.             if (tif->tif_flags&TIFF_SWAB) {
  362.                 if (sp->lzw_hordiff == horAcc16) {
  363.                     sp->lzw_hordiff = swabHorAcc16;
  364.                     tif->tif_postdecode = TIFFNoPostDecode;
  365.                 } /* else handle 32-bit case... */
  366.             }
  367.         }
  368.         /*
  369.          * Pre-load the table.
  370.          */
  371.         { int code;
  372.           for (code = 255; code >= 0; code--) {
  373.             sp->dec_codetab[code].value = code;
  374.             sp->dec_codetab[code].firstchar = code;
  375.             sp->dec_codetab[code].length = 1;
  376.             sp->dec_codetab[code].next = NULL;
  377.           }
  378.         }
  379.     }
  380.     /*
  381.      * Check for old bit-reversed codes.
  382.      */
  383.     if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
  384. #ifdef LZW_COMPAT
  385.         if (!sp->dec_decode) {
  386.             if (sp->lzw_hordiff == NULL) {
  387.                 /*
  388.                  * Override default decoding methods with
  389.                  * ones that deal with the old coding.
  390.                  * Otherwise the predictor versions set
  391.                  * above will call the compatibility routines
  392.                  * through the dec_decode method.
  393.                  */
  394.                 tif->tif_decoderow = LZWDecodeCompat;
  395.                 tif->tif_decodestrip = LZWDecodeCompat;
  396.                 tif->tif_decodetile = LZWDecodeCompat;
  397.             }
  398.             TIFFWarning(tif->tif_name,
  399.                 "Old-style LZW codes, convert file");
  400.         }
  401.         sp->lzw_maxcode = MAXCODE(BITS_MIN);
  402.         sp->dec_decode = LZWDecodeCompat;
  403. #else /* !LZW_COMPAT */
  404.         if (!sp->dec_decode) {
  405.             TIFFError(tif->tif_name,
  406.                 "Old-style LZW codes not supported");
  407.             sp->dec_decode = LZWDecode;
  408.         }
  409.         return (0);
  410. #endif/* !LZW_COMPAT */
  411.     } else {
  412.         sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
  413.         sp->dec_decode = LZWDecode;
  414.     }
  415.     sp->lzw_nbits = BITS_MIN;
  416.     sp->lzw_nextbits = 0;
  417.     sp->lzw_nextdata = 0;
  418.  
  419.     sp->dec_restart = 0;
  420.     sp->dec_nbitsmask = MAXCODE(BITS_MIN);
  421. #ifdef LZW_CHECKEOS
  422.     sp->dec_bitsleft = tif->tif_rawdatasize << 3;
  423. #endif
  424.     sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
  425.     sp->dec_oldcodep = &sp->dec_codetab[-1];
  426.     sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
  427.     return (1);
  428. }
  429.  
  430. /*
  431.  * Decode a "hunk of data".
  432.  */
  433. #define    GetNextCode(sp, bp, code) {                \
  434.     nextdata = (nextdata<<8) | *(bp)++;            \
  435.     nextbits += 8;                        \
  436.     if (nextbits < nbits) {                    \
  437.         nextdata = (nextdata<<8) | *(bp)++;        \
  438.         nextbits += 8;                    \
  439.     }                            \
  440.     code = (nextdata >> (nextbits-nbits)) & nbitsmask;    \
  441.     nextbits -= nbits;                    \
  442. }
  443.  
  444. static void
  445. codeLoop(TIFF* tif)
  446. {
  447.     TIFFError(tif->tif_name,
  448.         "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
  449.         tif->tif_row);
  450. }
  451.  
  452. static int
  453. LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  454. {
  455.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  456.     char *op = (char*) op0;
  457.     long occ = (long) occ0;
  458.     char *tp;
  459.     u_char *bp;
  460.     int code, nbits, nextbits, len;
  461.     long nextdata, nbitsmask;
  462.     code_t *codep, *free_entp, *maxcodep, *oldcodep;
  463.  
  464.     assert(sp != NULL);
  465.     /*
  466.      * Restart interrupted output operation.
  467.      */
  468.     if (sp->dec_restart) {
  469.         int residue;
  470.  
  471.         codep = sp->dec_codep;
  472.         residue = codep->length - sp->dec_restart;
  473.         if (residue > occ) {
  474.             /*
  475.              * Residue from previous decode is sufficient
  476.              * to satisfy decode request.  Skip to the
  477.              * start of the decoded string, place decoded
  478.              * values in the output buffer, and return.
  479.              */
  480.             sp->dec_restart += occ;
  481.             do {
  482.                 codep = codep->next;
  483.             } while (--residue > occ && codep);
  484.             if (codep) {
  485.                 tp = op + occ;
  486.                 do {
  487.                     *--tp = codep->value;
  488.                     codep = codep->next;
  489.                 } while (--occ && codep);
  490.             }
  491.             return (1);
  492.         }
  493.         /*
  494.          * Residue satisfies only part of the decode request.
  495.          */
  496.         op += residue, occ -= residue;
  497.         tp = op;
  498.         do {
  499.             int t;
  500.             --tp;
  501.             t = codep->value;
  502.             codep = codep->next;
  503.             *tp = t;
  504.         } while (--residue && codep);
  505.         sp->dec_restart = 0;
  506.     }
  507.  
  508.     bp = (u_char *)tif->tif_rawcp;
  509.     nbits = sp->lzw_nbits;
  510.     nextdata = sp->lzw_nextdata;
  511.     nextbits = sp->lzw_nextbits;
  512.     nbitsmask = sp->dec_nbitsmask;
  513.     oldcodep = sp->dec_oldcodep;
  514.     free_entp = sp->dec_free_entp;
  515.     maxcodep = sp->dec_maxcodep;
  516.  
  517.     while (occ > 0) {
  518.         NextCode(tif, sp, bp, code, GetNextCode);
  519.         if (code == CODE_EOI)
  520.             break;
  521.         if (code == CODE_CLEAR) {
  522.             free_entp = sp->dec_codetab + CODE_FIRST;
  523.             nbits = BITS_MIN;
  524.             nbitsmask = MAXCODE(BITS_MIN);
  525.             maxcodep = sp->dec_codetab + nbitsmask-1;
  526.             NextCode(tif, sp, bp, code, GetNextCode);
  527.             if (code == CODE_EOI)
  528.                 break;
  529.             *op++ = code, occ--;
  530.             oldcodep = sp->dec_codetab + code;
  531.             continue;
  532.         }
  533.         codep = sp->dec_codetab + code;
  534.  
  535.         /*
  536.           * Add the new entry to the code table.
  537.           */
  538.         assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]);
  539.         free_entp->next = oldcodep;
  540.         free_entp->firstchar = free_entp->next->firstchar;
  541.         free_entp->length = free_entp->next->length+1;
  542.         free_entp->value = (codep < free_entp) ?
  543.             codep->firstchar : free_entp->firstchar;
  544.         if (++free_entp > maxcodep) {
  545.             if (++nbits > BITS_MAX)        /* should not happen */
  546.                 nbits = BITS_MAX;
  547.             nbitsmask = MAXCODE(nbits);
  548.             maxcodep = sp->dec_codetab + nbitsmask-1;
  549.         }
  550.         oldcodep = codep;
  551.         if (code >= 256) {
  552.             /*
  553.               * Code maps to a string, copy string
  554.              * value to output (written in reverse).
  555.               */
  556.             if (codep->length > occ) {
  557.                 /*
  558.                  * String is too long for decode buffer,
  559.                  * locate portion that will fit, copy to
  560.                  * the decode buffer, and setup restart
  561.                  * logic for the next decoding call.
  562.                  */
  563.                 sp->dec_codep = codep;
  564.                 do {
  565.                     codep = codep->next;
  566.                 } while (codep && codep->length > occ);
  567.                 if (codep) {
  568.                     sp->dec_restart = occ;
  569.                     tp = op + occ;
  570.                     do  {
  571.                         *--tp = codep->value;
  572.                         codep = codep->next;
  573.                     }  while (--occ && codep);
  574.                     if (codep)
  575.                         codeLoop(tif);
  576.                 }
  577.                 break;
  578.             }
  579.             len = codep->length;
  580.             tp = op + len;
  581.             do {
  582.                 int t;
  583.                 --tp;
  584.                 t = codep->value;
  585.                 codep = codep->next;
  586.                 *tp = t;
  587.             } while (codep && tp > op);
  588.             if (codep) {
  589.                 codeLoop(tif);
  590.                 break;
  591.             }
  592.             op += len, occ -= len;
  593.         } else
  594.             *op++ = code, occ--;
  595.     }
  596.  
  597.     tif->tif_rawcp = (tidata_t) bp;
  598.     sp->lzw_nbits = nbits;
  599.     sp->lzw_nextdata = nextdata;
  600.     sp->lzw_nextbits = nextbits;
  601.     sp->dec_nbitsmask = nbitsmask;
  602.     sp->dec_oldcodep = oldcodep;
  603.     sp->dec_free_entp = free_entp;
  604.     sp->dec_maxcodep = maxcodep;
  605.  
  606.     if (occ > 0) {
  607.         TIFFError(tif->tif_name,
  608.         "LZWDecode: Not enough data at scanline %d (short %d bytes)",
  609.             tif->tif_row, occ);
  610.         return (0);
  611.     }
  612.     return (1);
  613. }
  614.  
  615. #ifdef LZW_COMPAT
  616. /*
  617.  * Decode a "hunk of data" for old images.
  618.  */
  619. #define    GetNextCodeCompat(sp, bp, code) {            \
  620.     nextdata |= *(bp)++ << nextbits;            \
  621.     nextbits += 8;                        \
  622.     if (nextbits < nbits) {                    \
  623.         nextdata |= *(bp)++ << nextbits;        \
  624.         nextbits += 8;                    \
  625.     }                            \
  626.     code = nextdata & nbitsmask;                \
  627.     nextdata >>= nbits;                    \
  628.     nextbits -= nbits;                    \
  629. }
  630.  
  631. static int
  632. LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  633. {
  634.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  635.     char *op = (char*) op0;
  636.     long occ = (long) occ0;
  637.     char *tp;
  638.     u_char *bp;
  639.     int code, nbits, nextbits;
  640.     long nextdata, nbitsmask;
  641.     code_t *codep, *free_entp, *maxcodep, *oldcodep;
  642.  
  643.     assert(sp != NULL);
  644.     /*
  645.      * Restart interrupted output operation.
  646.      */
  647.     if (sp->dec_restart) {
  648.         int residue;
  649.  
  650.         codep = sp->dec_codep;
  651.         residue = codep->length - sp->dec_restart;
  652.         if (residue > occ) {
  653.             /*
  654.              * Residue from previous decode is sufficient
  655.              * to satisfy decode request.  Skip to the
  656.              * start of the decoded string, place decoded
  657.              * values in the output buffer, and return.
  658.              */
  659.             sp->dec_restart += occ;
  660.             do {
  661.                 codep = codep->next;
  662.             } while (--residue > occ);
  663.             tp = op + occ;
  664.             do {
  665.                 *--tp = codep->value;
  666.                 codep = codep->next;
  667.             } while (--occ);
  668.             return (1);
  669.         }
  670.         /*
  671.          * Residue satisfies only part of the decode request.
  672.          */
  673.         op += residue, occ -= residue;
  674.         tp = op;
  675.         do {
  676.             *--tp = codep->value;
  677.             codep = codep->next;
  678.         } while (--residue);
  679.         sp->dec_restart = 0;
  680.     }
  681.  
  682.     bp = (u_char *)tif->tif_rawcp;
  683.     nbits = sp->lzw_nbits;
  684.     nextdata = sp->lzw_nextdata;
  685.     nextbits = sp->lzw_nextbits;
  686.     nbitsmask = sp->dec_nbitsmask;
  687.     oldcodep = sp->dec_oldcodep;
  688.     free_entp = sp->dec_free_entp;
  689.     maxcodep = sp->dec_maxcodep;
  690.  
  691.     while (occ > 0) {
  692.         NextCode(tif, sp, bp, code, GetNextCodeCompat);
  693.         if (code == CODE_EOI)
  694.             break;
  695.         if (code == CODE_CLEAR) {
  696.             free_entp = sp->dec_codetab + CODE_FIRST;
  697.             nbits = BITS_MIN;
  698.             nbitsmask = MAXCODE(BITS_MIN);
  699.             maxcodep = sp->dec_codetab + nbitsmask;
  700.             NextCode(tif, sp, bp, code, GetNextCodeCompat);
  701.             if (code == CODE_EOI)
  702.                 break;
  703.             *op++ = code, occ--;
  704.             oldcodep = sp->dec_codetab + code;
  705.             continue;
  706.         }
  707.         codep = sp->dec_codetab + code;
  708.  
  709.         /*
  710.           * Add the new entry to the code table.
  711.           */
  712.         assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]);
  713.         free_entp->next = oldcodep;
  714.         free_entp->firstchar = free_entp->next->firstchar;
  715.         free_entp->length = free_entp->next->length+1;
  716.         free_entp->value = (codep < free_entp) ?
  717.             codep->firstchar : free_entp->firstchar;
  718.         if (++free_entp > maxcodep) {
  719.             if (++nbits > BITS_MAX)        /* should not happen */
  720.                 nbits = BITS_MAX;
  721.             nbitsmask = MAXCODE(nbits);
  722.             maxcodep = sp->dec_codetab + nbitsmask;
  723.         }
  724.         oldcodep = codep;
  725.         if (code >= 256) {
  726.             /*
  727.               * Code maps to a string, copy string
  728.              * value to output (written in reverse).
  729.               */
  730.             if (codep->length > occ) {
  731.                 /*
  732.                  * String is too long for decode buffer,
  733.                  * locate portion that will fit, copy to
  734.                  * the decode buffer, and setup restart
  735.                  * logic for the next decoding call.
  736.                  */
  737.                 sp->dec_codep = codep;
  738.                 do {
  739.                     codep = codep->next;
  740.                 } while (codep->length > occ);
  741.                 sp->dec_restart = occ;
  742.                 tp = op + occ;
  743.                 do  {
  744.                     *--tp = codep->value;
  745.                     codep = codep->next;
  746.                 }  while (--occ);
  747.                 break;
  748.             }
  749.             op += codep->length, occ -= codep->length;
  750.             tp = op;
  751.             do {
  752.                 *--tp = codep->value;
  753.             } while (codep = codep->next);
  754.         } else
  755.             *op++ = code, occ--;
  756.     }
  757.  
  758.     tif->tif_rawcp = (tidata_t) bp;
  759.     sp->lzw_nbits = nbits;
  760.     sp->lzw_nextdata = nextdata;
  761.     sp->lzw_nextbits = nextbits;
  762.     sp->dec_nbitsmask = nbitsmask;
  763.     sp->dec_oldcodep = oldcodep;
  764.     sp->dec_free_entp = free_entp;
  765.     sp->dec_maxcodep = maxcodep;
  766.  
  767.     if (occ > 0) {
  768.         TIFFError(tif->tif_name,
  769.         "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
  770.             tif->tif_row, occ);
  771.         return (0);
  772.     }
  773.     return (1);
  774. }
  775. #endif /* LZW_COMPAT */
  776.  
  777. /*
  778.  * Decode a scanline and apply the predictor routine.
  779.  */
  780. static int
  781. LZWDecodePredRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  782. {
  783.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  784.  
  785.     assert(sp != NULL);
  786.     assert(sp->dec_decode != NULL);
  787.     if ((*sp->dec_decode)(tif, op0, occ0, s)) {
  788.         (*sp->lzw_hordiff)((char *)op0, occ0, sp->lzw_stride);
  789.         return (1);
  790.     } else
  791.         return (0);
  792. }
  793.  
  794. /*
  795.  * Decode a tile/strip and apply the predictor routine.
  796.  * Note that horizontal differencing must be done on a
  797.  * row-by-row basis.  The width of a "row" has already
  798.  * been calculated at pre-decode time according to the
  799.  * strip/tile dimensions.
  800.  */
  801. static int
  802. LZWDecodePredTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  803. {
  804.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  805.     u_long rowsize;
  806.  
  807.     assert(sp != NULL);
  808.     assert(sp->dec_decode != NULL);
  809.     if (!(*sp->dec_decode)(tif, op0, occ0, s))
  810.         return (0);
  811.     rowsize = sp->lzw_rowsize;
  812.     assert(rowsize > 0);
  813.     while ((long)occ0 > 0) {
  814.         (*sp->lzw_hordiff)((char*)op0, rowsize, sp->lzw_stride);
  815.         occ0 -= rowsize;
  816.         op0 += rowsize;
  817.     }
  818.     return (1);
  819. }
  820.  
  821. /*
  822.  * LZW Encoding.
  823.  */
  824.  
  825. static void
  826. horDiff8(register char* cp, register u_long cc, register int stride)
  827. {
  828.     if (cc > stride) {
  829.         cc -= stride;
  830.         /*
  831.          * Pipeline the most common cases.
  832.          */
  833.         if (stride == 3) {
  834.             int r1, g1, b1;
  835.             int r2 = cp[0];
  836.             int g2 = cp[1];
  837.             int b2 = cp[2];
  838.             do {
  839.                 r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
  840.                 g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
  841.                 b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
  842.                 cp += 3;
  843.             } while ((long)(cc -= 3) > 0);
  844.         } else if (stride == 4) {
  845.             int r1, g1, b1, a1;
  846.             int r2 = cp[0];
  847.             int g2 = cp[1];
  848.             int b2 = cp[2];
  849.             int a2 = cp[3];
  850.             do {
  851.                 r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
  852.                 g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
  853.                 b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
  854.                 a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
  855.                 cp += 4;
  856.             } while ((long)(cc -= 4) > 0);
  857.         } else {
  858.             cp += cc - 1;
  859.             do {
  860.                 REPEAT4(stride, cp[stride] -= cp[0]; cp--)
  861.             } while ((long)(cc -= stride) > 0);
  862.         }
  863.     }
  864. }
  865.  
  866. static void
  867. horDiff16(char* cp, u_long cc, register int stride)
  868. {
  869.     register int16 *wp = (int16*)cp;
  870.     register u_long wc = cc/2;
  871.  
  872.     if (wc > stride) {
  873.         wc -= stride;
  874.         wp += wc - 1;
  875.         do {
  876.             REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  877.             wc -= stride;
  878.         } while (wc > 0);
  879.     }
  880. }
  881.  
  882. /*
  883.  * Reset encoding state at the start of a strip.
  884.  */
  885. static int
  886. LZWPreEncode(TIFF* tif)
  887. {
  888.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  889.  
  890.     if (sp == NULL) {
  891.         tif->tif_data = _TIFFmalloc(sizeof (LZWEncodeState));
  892.         if (tif->tif_data == NULL) {
  893.             TIFFError("LZWPreEncode",
  894.                 "No space for LZW state block");
  895.             return (0);
  896.         }
  897.         sp = (LZWEncodeState *)tif->tif_data;
  898.         if (!LZWCheckPredictor(tif, &sp->base, horDiff8, horDiff16))
  899.             return (0);
  900.         if (sp->lzw_hordiff != NULL) {
  901.             tif->tif_encoderow = LZWEncodePredRow;
  902.             tif->tif_encodestrip = LZWEncodePredTile;
  903.             tif->tif_encodetile = LZWEncodePredTile;
  904.         }
  905.     }
  906.     sp->lzw_nbits = BITS_MIN;
  907.     sp->lzw_maxcode = MAXCODE(BITS_MIN);
  908.     sp->lzw_free_ent = CODE_FIRST;
  909.     sp->lzw_nextbits = 0;
  910.     sp->lzw_nextdata = 0;
  911.     sp->enc_checkpoint = CHECK_GAP;
  912.     sp->enc_ratio = 0;
  913.     sp->enc_incount = 0;
  914.     sp->enc_outcount = 0;
  915.     /*
  916.      * The 4 here insures there is space for 2 max-sized
  917.      * codes in LZWEncode and LZWPostDecode.
  918.      */
  919.     sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
  920.     cl_hash(sp);        /* clear hash table */
  921.     sp->enc_oldcode = -1;    /* generates CODE_CLEAR in LZWEncode */
  922.     return (1);
  923. }
  924.  
  925. #define    CALCRATIO(sp, rat) {                    \
  926.     if (incount > 0x007fffff) { /* NB: shift will overflow */\
  927.         rat = outcount >> 8;                \
  928.         rat = (rat == 0 ? 0x7fffffff : incount/rat);    \
  929.     } else                            \
  930.         rat = (incount<<8) / outcount;            \
  931. }
  932. #define    PutNextCode(op, c) {                    \
  933.     nextdata = (nextdata << nbits) | c;            \
  934.     nextbits += nbits;                    \
  935.     *op++ = nextdata >> (nextbits-8);            \
  936.     nextbits -= 8;                        \
  937.     if (nextbits >= 8) {                    \
  938.         *op++ = nextdata >> (nextbits-8);        \
  939.         nextbits -= 8;                    \
  940.     }                            \
  941.     outcount += nbits;                    \
  942. }
  943.  
  944. /*
  945.  * Encode a chunk of pixels.
  946.  *
  947.  * Uses an open addressing double hashing (no chaining) on the 
  948.  * prefix code/next character combination.  We do a variant of
  949.  * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
  950.  * relatively-prime secondary probe.  Here, the modular division
  951.  * first probe is gives way to a faster exclusive-or manipulation. 
  952.  * Also do block compression with an adaptive reset, whereby the
  953.  * code table is cleared when the compression ratio decreases,
  954.  * but after the table fills.  The variable-length output codes
  955.  * are re-sized at this point, and a CODE_CLEAR is generated
  956.  * for the decoder. 
  957.  */
  958. static int
  959. LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
  960. {
  961.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  962.     register long fcode;
  963.     register hash_t *hp;
  964.     register int h, c, ent, disp;
  965.     long incount, outcount, checkpoint;
  966.     long nextdata, nextbits;
  967.     int free_ent, maxcode, nbits;
  968.     tidata_t op, limit;
  969.  
  970.     if (sp == NULL)
  971.         return (0);
  972.     /*
  973.      * Load local state.
  974.      */
  975.     incount = sp->enc_incount;
  976.     outcount = sp->enc_outcount;
  977.     checkpoint = sp->enc_checkpoint;
  978.     nextdata = sp->lzw_nextdata;
  979.     nextbits = sp->lzw_nextbits;
  980.     free_ent = sp->lzw_free_ent;
  981.     maxcode = sp->lzw_maxcode;
  982.     nbits = sp->lzw_nbits;
  983.     op = tif->tif_rawcp;
  984.     limit = sp->enc_rawlimit;
  985.     ent = sp->enc_oldcode;
  986.  
  987.     if (ent == -1 && cc > 0) {
  988.         /*
  989.          * NB: This is safe because it can only happen
  990.          *     at the start of a strip where we know there
  991.          *     is space in the data buffer.
  992.          */
  993.         PutNextCode(op, CODE_CLEAR);
  994.         ent = *bp++; cc--; incount++;
  995.     }
  996.     while (cc > 0) {
  997.         c = *bp++; cc--; incount++;
  998.         fcode = ((long)c << BITS_MAX) + ent;
  999.         h = (c << HSHIFT) ^ ent;    /* xor hashing */
  1000.         hp = &sp->enc_hashtab[h];
  1001.         if (hp->hash == fcode) {
  1002.             ent = hp->code;
  1003.             continue;
  1004.         }
  1005.         if (hp->hash >= 0) {
  1006.             /*
  1007.              * Primary hash failed, check secondary hash.
  1008.              */
  1009.             disp = HSIZE - h;
  1010.             if (h == 0)
  1011.                 disp = 1;
  1012.             do {
  1013.                 if ((hp -= disp) < sp->enc_hashtab)
  1014.                     hp += HSIZE;
  1015.                 if (hp->hash == fcode) {
  1016.                     ent = hp->code;
  1017.                     goto hit;
  1018.                 }
  1019.             } while (hp->hash >= 0);
  1020.         }
  1021.         /*
  1022.          * New entry, emit code and add to table.
  1023.          */
  1024.         /*
  1025.          * Verify there is space in the buffer for the code
  1026.          * and any potential Clear code that might be emitted
  1027.          * below.  The value of limit is setup so that there
  1028.          * are at least 4 bytes free--room for 2 codes.
  1029.          */
  1030.         if (op > limit) {
  1031.             tif->tif_rawcc = op - tif->tif_rawdata;
  1032.             TIFFFlushData1(tif);
  1033.             op = tif->tif_rawdata;
  1034.         }
  1035.         PutNextCode(op, ent);
  1036.         ent = c;
  1037.         hp->code = free_ent++;
  1038.         hp->hash = fcode;
  1039.         if (free_ent == CODE_MAX-1) {
  1040.             /* table is full, emit clear code and reset */
  1041.             cl_hash(sp);
  1042.             sp->enc_ratio = 0;
  1043.             incount = 0;
  1044.             outcount = 0;
  1045.             free_ent = CODE_FIRST;
  1046.             PutNextCode(op, CODE_CLEAR);
  1047.             nbits = BITS_MIN;
  1048.             maxcode = MAXCODE(BITS_MIN);
  1049.         } else {
  1050.             /*
  1051.              * If the next entry is going to be too big for
  1052.              * the code size, then increase it, if possible.
  1053.              */
  1054.             if (free_ent > maxcode) {
  1055.                 nbits++;
  1056.                 assert(nbits <= BITS_MAX);
  1057.                 maxcode = MAXCODE(nbits);
  1058.             } else if (incount >= checkpoint) {
  1059.                 long rat;
  1060.                 /*
  1061.                  * Check compression ratio and, if things seem
  1062.                  * to be slipping, clear the hash table and
  1063.                  * reset state.  The compression ratio is a
  1064.                  * 24+8-bit fractional number.
  1065.                  */
  1066.                 checkpoint = incount+CHECK_GAP;
  1067.                 CALCRATIO(sp, rat);
  1068.                 if (rat <= sp->enc_ratio) {
  1069.                     cl_hash(sp);
  1070.                     sp->enc_ratio = 0;
  1071.                     incount = 0;
  1072.                     outcount = 0;
  1073.                     free_ent = CODE_FIRST;
  1074.                     PutNextCode(op, CODE_CLEAR);
  1075.                     nbits = BITS_MIN;
  1076.                     maxcode = MAXCODE(BITS_MIN);
  1077.                 } else
  1078.                     sp->enc_ratio = rat;
  1079.             }
  1080.         }
  1081.     hit:
  1082.         ;
  1083.     }
  1084.  
  1085.     /*
  1086.      * Restore global state.
  1087.      */
  1088.     sp->enc_incount = incount;
  1089.     sp->enc_outcount = outcount;
  1090.     sp->enc_checkpoint = checkpoint;
  1091.     sp->enc_oldcode = ent;
  1092.     sp->lzw_nextdata = nextdata;
  1093.     sp->lzw_nextbits = nextbits;
  1094.     sp->lzw_free_ent = free_ent;
  1095.     sp->lzw_maxcode = maxcode;
  1096.     sp->lzw_nbits = nbits;
  1097.     tif->tif_rawcp = op;
  1098.     return (1);
  1099. }
  1100.  
  1101. static int
  1102. LZWEncodePredRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
  1103. {
  1104.     LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1105.  
  1106.     assert(sp != NULL);
  1107.     assert(sp->lzw_hordiff != NULL);
  1108. /* XXX horizontal differencing alters user's data XXX */
  1109.     (*sp->lzw_hordiff)((char *)bp, cc, sp->lzw_stride);
  1110.     return (LZWEncode(tif, bp, cc, s));
  1111. }
  1112.  
  1113. static int
  1114. LZWEncodePredTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
  1115. {
  1116.     LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1117.     u_long cc = cc0, rowsize;
  1118.     u_char *bp = bp0;
  1119.  
  1120.     assert(sp != NULL);
  1121.     assert(sp->lzw_hordiff != NULL);
  1122.     rowsize = sp->lzw_rowsize;
  1123.     assert(rowsize > 0);
  1124.     while ((long)cc > 0) {
  1125.         (*sp->lzw_hordiff)((char *)bp, rowsize, sp->lzw_stride);
  1126.         cc -= rowsize;
  1127.         bp += rowsize;
  1128.     }
  1129.     return (LZWEncode(tif, bp0, cc0, s));
  1130. }
  1131.  
  1132. /*
  1133.  * Finish off an encoded strip by flushing the last
  1134.  * string and tacking on an End Of Information code.
  1135.  */
  1136. static int
  1137. LZWPostEncode(TIFF* tif)
  1138. {
  1139.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1140.     tidata_t op = tif->tif_rawcp;
  1141.     long nextbits = sp->lzw_nextbits;
  1142.     long nextdata = sp->lzw_nextdata;
  1143.     long outcount = sp->enc_outcount;
  1144.     int nbits = sp->lzw_nbits;
  1145.  
  1146.     if (op > sp->enc_rawlimit) {
  1147.         tif->tif_rawcc = op - tif->tif_rawdata;
  1148.         TIFFFlushData1(tif);
  1149.         op = tif->tif_rawdata;
  1150.     }
  1151.     if (sp->enc_oldcode != -1) {
  1152.         PutNextCode(op, sp->enc_oldcode);
  1153.         sp->enc_oldcode = -1;
  1154.     }
  1155.     PutNextCode(op, CODE_EOI);
  1156.     if (nextbits > 0) 
  1157.         *op++ = nextdata << (8-nextbits);
  1158.     tif->tif_rawcc = op - tif->tif_rawdata;
  1159.     return (1);
  1160. }
  1161.  
  1162. /*
  1163.  * Reset encoding hash table.
  1164.  */
  1165. static void
  1166. cl_hash(LZWEncodeState* sp)
  1167. {
  1168.     register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
  1169.     register long i = HSIZE-8;
  1170.  
  1171.      do {
  1172.         i -= 8;
  1173.         hp[-7].hash = -1;
  1174.         hp[-6].hash = -1;
  1175.         hp[-5].hash = -1;
  1176.         hp[-4].hash = -1;
  1177.         hp[-3].hash = -1;
  1178.         hp[-2].hash = -1;
  1179.         hp[-1].hash = -1;
  1180.         hp[ 0].hash = -1;
  1181.         hp -= 8;
  1182.     } while (i >= 0);
  1183.         for (i += 8; i > 0; i--, hp--)
  1184.         hp->hash = -1;
  1185. }
  1186.  
  1187. static void
  1188. LZWCleanup(TIFF* tif)
  1189. {
  1190.     if (tif->tif_data) {
  1191.         _TIFFfree(tif->tif_data);
  1192.         tif->tif_data = NULL;
  1193.     }
  1194. }
  1195.  
  1196. int
  1197. TIFFInitLZW(TIFF* tif)
  1198. {
  1199.     tif->tif_predecode = LZWPreDecode;
  1200.     tif->tif_decoderow = LZWDecode;
  1201.     tif->tif_decodestrip = LZWDecode;
  1202.     tif->tif_decodetile = LZWDecode;
  1203.     tif->tif_preencode = LZWPreEncode;
  1204.     tif->tif_postencode = LZWPostEncode;
  1205.     tif->tif_encoderow = LZWEncode;
  1206.     tif->tif_encodestrip = LZWEncode;
  1207.     tif->tif_encodetile = LZWEncode;
  1208.     tif->tif_cleanup = LZWCleanup;
  1209.     return (1);
  1210. }
  1211.  
  1212. /*
  1213.  * Copyright (c) 1985, 1986 The Regents of the University of California.
  1214.  * All rights reserved.
  1215.  *
  1216.  * This code is derived from software contributed to Berkeley by
  1217.  * James A. Woods, derived from original work by Spencer Thomas
  1218.  * and Joseph Orost.
  1219.  *
  1220.  * Redistribution and use in source and binary forms are permitted
  1221.  * provided that the above copyright notice and this paragraph are
  1222.  * duplicated in all such forms and that any documentation,
  1223.  * advertising materials, and other materials related to such
  1224.  * distribution and use acknowledge that the software was developed
  1225.  * by the University of California, Berkeley.  The name of the
  1226.  * University may not be used to endorse or promote products derived
  1227.  * from this software without specific prior written permission.
  1228.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1229.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1230.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1231.  */
  1232.