home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / grfx_snd / tifflib / source / tif_next.c < prev    next >
C/C++ Source or Header  |  1993-01-11  |  5KB  |  149 lines

  1. #pragma warn -use
  2. static char     *sccsid = "@(#)TIFF/tif_next.c 1.09, Copyright (c) Sam Leffler, Dieter Linde, "__DATE__;
  3. #pragma warn .use
  4. /*
  5.  * Copyright (c) 1988, 1990 by Sam Leffler, Oct 8 1990
  6.  * All rights reserved.
  7.  *
  8.  * This file is provided for unrestricted use provided that this legend is included on all tape media and as a part of the
  9.  * software program in whole or part.  Users may copy, modify or distribute this file at will.
  10.  *
  11.  * TIFF Library.
  12.  *
  13.  * NeXT 2-bit Grey Scale Compression Algorithm Support
  14.  */
  15. #include "tiffio.h"
  16.  
  17. #define SETPIXEL(op, v)    { \
  18.                     switch (npixels++ & 3) { \
  19.                         case 0: \
  20.                             op[0]  = (v) << 6; \
  21.                             break; \
  22.                         case 1: \
  23.                             op[0] |= (v) << 4; \
  24.                             break; \
  25.                         case 2: \
  26.                             op[0] |= (v) << 2; \
  27.                             break; \
  28.                         case 3: \
  29.                             *op++ |= (v); \
  30.                             break; \
  31.                     } \
  32.                 }
  33.  
  34. #define LITERALROW         0x00
  35. #define LITERALSPAN     0x40
  36. #define WHITE           ((1 << 2) - 1)
  37.  
  38. #pragma warn -par
  39. /****************************************************************************
  40.  *
  41.  */
  42. static int
  43. NeXTEncode(
  44.            TIFF     *tif,
  45.         u_char     *pp,
  46.         int     cc
  47.         )
  48. {
  49.         TIFFError(tif->tif_name, "NeXT encoding is not implemented");
  50.         return (-1);
  51. }
  52. #pragma warn .par
  53.  
  54. /****************************************************************************
  55.  *
  56.  */
  57. static int
  58. NeXTDecode(
  59.            TIFF     *tif,
  60.         u_char     *buf,
  61.         int     occ
  62.         )
  63. {
  64.         register u_char    *bp, *op;
  65.         register int     n;
  66.         register long    cc;
  67.         u_char         *row;
  68.         int         scanline;
  69.  
  70.         /*
  71.          * Each scanline is assumed to start off as all white (we assume a PhotometricInterpretation of ``min-is-black'').
  72.          */
  73.         for (op = buf, cc = occ; cc-- > 0;)
  74.                 *op++ = 0xff;
  75.  
  76.         bp = (u_char *)tif->tif_rawcp;
  77.         cc = tif->tif_rawcc;
  78.         scanline = tif->tif_scanlinesize;
  79.         for (row = buf; occ > 0; occ -= scanline, row += scanline) {
  80.                 n = *bp++, cc--;
  81.                 switch (n) {
  82.                     case LITERALROW:    /* the entire scanline is given as literal values */
  83.                             if (cc < scanline)
  84.                                     goto bad;
  85.                             bcopy(bp, row, scanline);
  86.                             bp += scanline;
  87.                             cc -= scanline;
  88.                             break;
  89.  
  90.                     case LITERALSPAN:    /* the scanline has a literal span that begins at some offset */
  91.                     {
  92.                             int     off;
  93.  
  94.                             off = (bp[0] * 256) + bp[1];
  95.                             n = (bp[2] * 256) + bp[3];
  96.                             if (cc < 4 + n)
  97.                                     goto bad;
  98.                             bcopy(bp + 4, row + off, n);
  99.                             bp += 4 + n;
  100.                             cc -= 4 + n;
  101.                             break;
  102.                     }
  103.                     default: 
  104.                     {
  105.                             register int    npixels = 0;
  106.                             register int    grey;
  107.                             int         imagewidth = tif->tif_dir.td_imagewidth;
  108.  
  109.       /*
  110.          * The scanline is composed of a sequence of constant color "runs".  We shift into "run mode" and interpret bytes
  111.          * as codes of the form <color><npixels> until we've filled the scanline.
  112.          */
  113.                             op = row;
  114.                             for (;;) {
  115.                                     grey = (n >> 6) & 0x3;
  116.                                     n &= 0x3f;
  117.                                     while (n-- > 0)
  118.                                             SETPIXEL(op, grey);
  119.                                     if (npixels >= imagewidth)
  120.                                             break;
  121.                                     if (cc == 0)
  122.                                             goto bad;
  123.                                     n = *bp++, cc--;
  124.                             }
  125.                             break;
  126.                     }
  127.                 }
  128.         }
  129.         tif->tif_rawcp = (u_char *)bp;
  130.         tif->tif_rawcc = cc;
  131.         return(1);
  132. bad:
  133.         TIFFError(tif->tif_name, "NeXTDecode: Not enough data for scanline %d", tif->tif_row);
  134.         return(0);
  135. }
  136.  
  137. /****************************************************************************
  138.  *
  139.  */
  140. int
  141. TIFFInitNeXT(
  142.            TIFF     *tif
  143.            )
  144. {
  145.         tif->tif_decoderow = NeXTDecode;
  146.         tif->tif_encoderow = NeXTEncode;
  147.         return(1);
  148. }
  149.