home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / jpeg_ls / jrdgif.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  21KB  |  635 lines

  1. /*
  2.  * jrdgif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in GIF format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume input from
  12.  * an ordinary stdio stream.  They further assume that reading begins
  13.  * at the start of the file; input_init may need work if the
  14.  * user interface has already read some data (e.g., to determine that
  15.  * the file is indeed GIF format).
  16.  *
  17.  * These routines are invoked via the methods get_input_row
  18.  * and input_init/term.
  19.  */
  20.  
  21. /*
  22.  * This code is loosely based on giftoppm from the PBMPLUS distribution
  23.  * of Feb. 1991.  That file contains the following copyright notice:
  24.  * +-------------------------------------------------------------------+
  25.  * | Copyright 1990, David Koblas.                                     |
  26.  * |   Permission to use, copy, modify, and distribute this software   |
  27.  * |   and its documentation for any purpose and without fee is hereby |
  28.  * |   granted, provided that the above copyright notice appear in all |
  29.  * |   copies and that both that copyright notice and this permission  |
  30.  * |   notice appear in supporting documentation.  This software is    |
  31.  * |   provided "as is" without express or implied warranty.           |
  32.  * +-------------------------------------------------------------------+
  33.  *
  34.  * We are also required to state that
  35.  *    "The Graphics Interchange Format(c) is the Copyright property of
  36.  *    CompuServe Incorporated. GIF(sm) is a Service Mark property of
  37.  *    CompuServe Incorporated."
  38.  */
  39.  
  40. #include "jinclude.h"
  41.  
  42. #ifdef GIF_SUPPORTED
  43.  
  44.  
  45. #define    MAXCOLORMAPSIZE    256    /* max # of colors in a GIF colormap */
  46. #define NUMCOLORS    3    /* # of colors */
  47. #define CM_RED        0    /* color component numbers */
  48. #define CM_GREEN    1
  49. #define CM_BLUE        2
  50.  
  51. static JSAMPARRAY colormap;    /* the colormap to use */
  52. /* colormap[i][j] = value of i'th color component for pixel value j */
  53.  
  54. #define    MAX_LZW_BITS    12    /* maximum LZW code size */
  55. #define LZW_TABLE_SIZE    (1<<MAX_LZW_BITS) /* # of possible LZW symbols */
  56.  
  57. /* Macros for extracting header data --- note we assume chars may be signed */
  58.  
  59. #define LM_to_uint(a,b)        ((((b)&0xFF) << 8) | ((a)&0xFF))
  60.  
  61. #define BitSet(byte, bit)    ((byte) & (bit))
  62. #define INTERLACE    0x40    /* mask for bit signifying interlaced image */
  63. #define COLORMAPFLAG    0x80    /* mask for bit signifying colormap presence */
  64.  
  65. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  66.  
  67. /* Static vars for GetCode and LZWReadByte */
  68.  
  69. static char code_buf[256+4];    /* current input data block */
  70. static int last_byte;        /* # of bytes in code_buf */
  71. static int last_bit;        /* # of bits in code_buf */
  72. static int cur_bit;        /* next bit index to read */
  73. static boolean out_of_blocks;    /* TRUE if hit terminator data block */
  74.  
  75. static int input_code_size;    /* codesize given in GIF file */
  76. static int clear_code,end_code; /* values for Clear and End codes */
  77.  
  78. static int code_size;        /* current actual code size */
  79. static int limit_code;        /* 2^code_size */
  80. static int max_code;        /* first unused code value */
  81. static boolean first_time;    /* flags first call to LZWReadByte */
  82.  
  83. /* LZW decompression tables:
  84.  *   symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  85.  *   symbol_tail[K] = suffix byte   of any LZW symbol K (0..LZW_TABLE_SIZE-1)
  86.  * Note that entries 0..end_code of the above tables are not used,
  87.  * since those symbols represent raw bytes or special codes.
  88.  *
  89.  * The stack represents the not-yet-used expansion of the last LZW symbol.
  90.  * In the worst case, a symbol could expand to as many bytes as there are
  91.  * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack.
  92.  * (This is conservative since that number includes the raw-byte symbols.)
  93.  *
  94.  * The tables are allocated from FAR heap space since they would use up
  95.  * rather a lot of the near data space in a PC.
  96.  */
  97.  
  98. static UINT16 FAR *symbol_head; /* => table of prefix symbols */
  99. static UINT8  FAR *symbol_tail; /* => table of suffix bytes */
  100. static UINT8  FAR *symbol_stack; /* stack for symbol expansions */
  101. static UINT8  FAR *sp;        /* stack pointer */
  102.  
  103. /* Static state for interlaced image processing */
  104.  
  105. static boolean is_interlaced;    /* TRUE if have interlaced image */
  106. static big_sarray_ptr interlaced_image;    /* full image in interlaced order */
  107. static long cur_row_number;    /* need to know actual row number */
  108. static long pass2_offset;    /* # of pixel rows in pass 1 */
  109. static long pass3_offset;    /* # of pixel rows in passes 1&2 */
  110. static long pass4_offset;    /* # of pixel rows in passes 1,2,3 */
  111.  
  112.  
  113. /* Forward declarations */
  114. METHODDEF void load_interlaced_image PP((decompress_info_ptr cinfo, JSAMPARRAY pixel_row));
  115. METHODDEF void get_interlaced_row PP((decompress_info_ptr cinfo, JSAMPARRAY pixel_row));
  116.  
  117.  
  118.  
  119. LOCAL int
  120. ReadByte (decompress_info_ptr cinfo)
  121. /* Read next byte from GIF file */
  122. {
  123. #ifdef AMIGA_IO
  124.   BPTR infile = cinfo->input_file;
  125. #else
  126.   register FILE * infile = cinfo->input_file;
  127. #endif
  128.   int c;
  129.  
  130.   if ((c = getc(infile)) == EOF)
  131.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  132.   return c;
  133. }
  134.  
  135.  
  136. LOCAL int
  137. GetDataBlock (decompress_info_ptr cinfo, char *buf)
  138. /* Read a GIF data block, which has a leading count byte */
  139. /* A zero-length block marks the end of a data block sequence */
  140. {
  141.   int count;
  142.  
  143.   count = ReadByte(cinfo);
  144.   if (count > 0) {
  145.     if (! ReadOK(cinfo->input_file, buf, count))
  146.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  147.   }
  148.   return count;
  149. }
  150.  
  151.  
  152. LOCAL void
  153. SkipDataBlocks (decompress_info_ptr cinfo)
  154. /* Skip a series of data blocks, until a block terminator is found */
  155. {
  156.   char buf[256];
  157.  
  158.   while (GetDataBlock(cinfo, buf) > 0)
  159.     /* skip */;
  160. }
  161.  
  162.  
  163. LOCAL void
  164. ReInitLZW (void)
  165. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  166. {
  167.   code_size = input_code_size+1;
  168.   limit_code = clear_code << 1;    /* 2^code_size */
  169.   max_code = clear_code + 2;    /* first unused code value */
  170.   sp = symbol_stack;        /* init stack to empty */
  171. }
  172.  
  173.  
  174. LOCAL void
  175. InitLZWCode (void)
  176. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  177. {
  178.   /* GetCode initialization */
  179.   last_byte = 2;        /* make safe to "recopy last two bytes" */
  180.   last_bit = 0;            /* nothing in the buffer */
  181.   cur_bit = 0;            /* force buffer load on first call */
  182.   out_of_blocks = FALSE;
  183.  
  184.   /* LZWReadByte initialization */
  185.   clear_code = 1 << input_code_size; /* compute special code values */
  186.   end_code = clear_code + 1;    /* note that these do not change */
  187.   first_time = TRUE;
  188.   ReInitLZW();
  189. }
  190.  
  191.  
  192. LOCAL int
  193. GetCode (decompress_info_ptr cinfo)
  194. /* Fetch the next code_size bits from the GIF data */
  195. /* We assume code_size is less than 16 */
  196. {
  197.   register INT32 accum;
  198.   int offs, ret, count;
  199.  
  200.   if ( (cur_bit+code_size) > last_bit) {
  201.     /* Time to reload the buffer */
  202.     if (out_of_blocks) {
  203.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  204.       return end_code;        /* fake something useful */
  205.     }
  206.     /* preserve last two bytes of what we have -- assume code_size <= 16 */
  207.     code_buf[0] = code_buf[last_byte-2];
  208.     code_buf[1] = code_buf[last_byte-1];
  209.     /* Load more bytes; set flag if we reach the terminator block */
  210.     if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  211.       out_of_blocks = TRUE;
  212.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  213.       return end_code;        /* fake something useful */
  214.     }
  215.     /* Reset counters */
  216.     cur_bit = (cur_bit - last_bit) + 16;
  217.     last_byte = 2 + count;
  218.     last_bit = last_byte * 8;
  219.   }
  220.  
  221.   /* Form up next 24 bits in accum */
  222.   offs = cur_bit >> 3;        /* byte containing cur_bit */
  223. #ifdef CHAR_IS_UNSIGNED
  224.   accum = code_buf[offs+2];
  225.   accum <<= 8;
  226.   accum |= code_buf[offs+1];
  227.   accum <<= 8;
  228.   accum |= code_buf[offs];
  229. #else
  230.   accum = code_buf[offs+2] & 0xFF;
  231.   accum <<= 8;
  232.   accum |= code_buf[offs+1] & 0xFF;
  233.   accum <<= 8;
  234.   accum |= code_buf[offs] & 0xFF;
  235. #endif
  236.  
  237.   /* Right-align cur_bit in accum, then mask off desired number of bits */
  238.   accum >>= (cur_bit & 7);
  239.   ret = ((int) accum) & ((1 << code_size) - 1);
  240.   
  241.