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_next.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  3.6 KB  |  144 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_next.c,v 1.3 2001/03/21 10:41:18 rjs Exp $ */
  27.  
  28. #include "tiffiop.h"
  29. #ifdef NEXT_SUPPORT
  30. /*
  31.  * TIFF Library.
  32.  *
  33.  * NeXT 2-bit Grey Scale Compression Algorithm Support
  34.  */
  35.  
  36. #define SETPIXEL(op, v) {            \
  37.     switch (npixels++ & 3) {        \
  38.     case 0:    op[0]  = (v) << 6; break;    \
  39.     case 1:    op[0] |= (v) << 4; break;    \
  40.     case 2:    op[0] |= (v) << 2; break;    \
  41.     case 3:    *op++ |= (v);       break;    \
  42.     }                    \
  43. }
  44.  
  45. #define LITERALROW    0x00
  46. #define LITERALSPAN    0x40
  47. #define WHITE       ((1<<2)-1)
  48.  
  49. static int
  50. NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
  51. {
  52.     register u_char *bp, *op;
  53.     register tsize_t cc;
  54.     register int n;
  55.     tidata_t row;
  56.     tsize_t scanline;
  57.  
  58.     (void) s;
  59.     /*
  60.      * Each scanline is assumed to start off as all
  61.      * white (we assume a PhotometricInterpretation
  62.      * of ``min-is-black'').
  63.      */
  64.     for (op = buf, cc = occ; cc-- > 0;)
  65.         *op++ = 0xff;
  66.  
  67.     bp = (u_char *)tif->tif_rawcp;
  68.     cc = tif->tif_rawcc;
  69.     scanline = tif->tif_scanlinesize;
  70.     for (row = buf; (long)occ > 0; occ -= scanline, row += scanline) {
  71.         n = *bp++, cc--;
  72.         switch (n) {
  73.         case LITERALROW:
  74.             /*
  75.              * The entire scanline is given as literal values.
  76.              */
  77.             if (cc < scanline)
  78.                 goto bad;
  79.             _TIFFmemcpy(row, bp, scanline);
  80.             bp += scanline;
  81.             cc -= scanline;
  82.             break;
  83.         case LITERALSPAN: {
  84.             int off;
  85.             /*
  86.              * The scanline has a literal span
  87.              * that begins at some offset.
  88.              */
  89.             off = (bp[0] * 256) + bp[1];
  90.             n = (bp[2] * 256) + bp[3];
  91.             if (cc < 4+n)
  92.                 goto bad;
  93.             _TIFFmemcpy(row+off, bp+4, n);
  94.             bp += 4+n;
  95.             cc -= 4+n;
  96.             break;
  97.         }
  98.         default: {
  99.             register int npixels = 0, grey;
  100.             u_long imagewidth = tif->tif_dir.td_imagewidth;
  101.  
  102.             /*
  103.              * The scanline is composed of a sequence
  104.              * of constant color ``runs''.  We shift
  105.              * into ``run mode'' and interpret bytes
  106.              * as codes of the form <color><npixels>
  107.              * until we've filled the scanline.
  108.              */
  109.             op = row;
  110.             for (;;) {
  111.                 grey = (n>>6) & 0x3;
  112.                 n &= 0x3f;
  113.                 while (n-- > 0)
  114.                     SETPIXEL(op, grey);
  115.                 if (npixels >= (int) imagewidth)
  116.                     break;
  117.                 if (cc == 0)
  118.                     goto bad;
  119.                 n = *bp++, cc--;
  120.             }
  121.             break;
  122.         }
  123.         }
  124.     }
  125.     tif->tif_rawcp = (tidata_t) bp;
  126.     tif->tif_rawcc = cc;
  127.     return (1);
  128. bad:
  129.     TIFFError(tif->tif_name, "NeXTDecode: Not enough data for scanline %ld",
  130.         (long) tif->tif_row);
  131.     return (0);
  132. }
  133.  
  134. int
  135. TIFFInitNeXT(TIFF* tif, int scheme)
  136. {
  137.     (void) scheme;
  138.     tif->tif_decoderow = NeXTDecode;
  139.     tif->tif_decodestrip = NeXTDecode;
  140.     tif->tif_decodetile = NeXTDecode;
  141.     return (1);
  142. }
  143. #endif /* NEXT_SUPPORT */
  144.