home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / tiff / tif_packbits.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  3.2 KB  |  106 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1988-1997 Sam Leffler
  3.  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and 
  6.  * its documentation for any purpose is hereby granted without fee, provided
  7.  * that (i) the above copyright notices and this permission notice appear in
  8.  * all copies of the software and related documentation, and (ii) the names of
  9.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10.  * publicity relating to the software without the specific, prior written
  11.  * permission of Sam Leffler and Silicon Graphics.
  12.  * 
  13.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  16.  * 
  17.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25.  
  26. /* $Id: tif_packbits.c,v 1.6 2001/03/21 10:41:18 rjs Exp $ */
  27.  
  28. #include "tiffiop.h"
  29. #ifdef PACKBITS_SUPPORT
  30. /*
  31.  * TIFF Library.
  32.  *
  33.  * PackBits Compression Algorithm Support
  34.  */
  35. #include <stdio.h>
  36.  
  37. static int
  38. PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
  39. {
  40.     char *bp;
  41.     tsize_t cc;
  42.     long n;
  43.     int b;
  44.  
  45.     (void) s;
  46.     bp = (char*) tif->tif_rawcp;
  47.     cc = tif->tif_rawcc;
  48.     while (cc > 0 && (long)occ > 0) {
  49.         n = (long) *bp++, cc--;
  50.         /*
  51.          * Watch out for compilers that
  52.          * don't sign extend chars...
  53.          */
  54.         if (n >= 128)
  55.             n -= 256;
  56.         if (n < 0) {        /* replicate next byte -n+1 times */
  57.             if (n == -128)    /* nop */
  58.                 continue;
  59.                         n = -n + 1;
  60.                         if( occ < n )
  61.                         {
  62.                             TIFFWarning(tif->tif_name,
  63.                                         "PackBitsDecode: discarding %d bytes "
  64.                                         "to avoid buffer overrun",
  65.                                         n - occ);
  66.                         }
  67.             occ -= n;
  68.             b = *bp++, cc--;
  69.             while (n-- > 0)
  70.                 *op++ = b;
  71.         } else {        /* copy next n+1 bytes literally */
  72.             if (occ < n + 1)
  73.                         {
  74.                             TIFFWarning(tif->tif_name,
  75.                                         "PackBitsDecode: discarding %d bytes "
  76.                                         "to avoid buffer overrun",
  77.                                         n - occ + 1);
  78.                             n = occ - 1;
  79.                         }
  80.                         _TIFFmemcpy(op, bp, ++n);
  81.             op += n; occ -= n;
  82.             bp += n; cc -= n;
  83.         }
  84.     }
  85.     tif->tif_rawcp = (tidata_t) bp;
  86.     tif->tif_rawcc = cc;
  87.     if (occ > 0) {
  88.         TIFFError(tif->tif_name,
  89.             "PackBitsDecode: Not enough data for scanline %ld",
  90.             (long) tif->tif_row);
  91.         return (0);
  92.     }
  93.     return (1);
  94. }
  95.  
  96. int
  97. TIFFInitPackBits(TIFF* tif, int scheme)
  98. {
  99.     (void) scheme;
  100.     tif->tif_decoderow = PackBitsDecode;
  101.     tif->tif_decodestrip = PackBitsDecode;
  102.     tif->tif_decodetile = PackBitsDecode;
  103.     return (1);
  104. }
  105. #endif /* PACKBITS_SUPPORT */
  106.