home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / jpeg / jpegv4a.tar / jrdgif.c < prev    next >
C/C++ Source or Header  |  1992-12-02  |  20KB  |  631 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((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  115. METHODDEF void get_interlaced_row PP((compress_info_ptr cinfo, JSAMPARRAY pixel_row));
  116.  
  117.  
  118.  
  119. LOCAL int
  120. ReadByte (compress_info_ptr cinfo)
  121. /* Read next byte from GIF file */
  122. {
  123.   register FILE * infile = cinfo->input_file;
  124.   int c;
  125.  
  126.   if ((c = getc(infile)) == EOF)
  127.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  128.   return c;
  129. }
  130.  
  131.  
  132. LOCAL int
  133. GetDataBlock (compress_info_ptr cinfo, char *buf)
  134. /* Read a GIF data block, which has a leading count byte */
  135. /* A zero-length block marks the end of a data block sequence */
  136. {
  137.   int count;
  138.  
  139.   count = ReadByte(cinfo);
  140.   if (count > 0) {
  141.     if (! ReadOK(cinfo->input_file, buf, count))
  142.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  143.   }
  144.   return count;
  145. }
  146.  
  147.  
  148. LOCAL void
  149. SkipDataBlocks (compress_info_ptr cinfo)
  150. /* Skip a series of data blocks, until a block terminator is found */
  151. {
  152.   char buf[256];
  153.  
  154.   while (GetDataBlock(cinfo, buf) > 0)
  155.     /* skip */;
  156. }
  157.  
  158.  
  159. LOCAL void
  160. ReInitLZW (void)
  161. /* (Re)initialize LZW state; shared code for startup and Clear processing */
  162. {
  163.   code_size = input_code_size+1;
  164.   limit_code = clear_code << 1;    /* 2^code_size */
  165.   max_code = clear_code + 2;    /* first unused code value */
  166.   sp = symbol_stack;        /* init stack to empty */
  167. }
  168.  
  169.  
  170. LOCAL void
  171. InitLZWCode (void)
  172. /* Initialize for a series of LZWReadByte (and hence GetCode) calls */
  173. {
  174.   /* GetCode initialization */
  175.   last_byte = 2;        /* make safe to "recopy last two bytes" */
  176.   last_bit = 0;            /* nothing in the buffer */
  177.   cur_bit = 0;            /* force buffer load on first call */
  178.   out_of_blocks = FALSE;
  179.  
  180.   /* LZWReadByte initialization */
  181.   clear_code = 1 << input_code_size; /* compute special code values */
  182.   end_code = clear_code + 1;    /* note that these do not change */
  183.   first_time = TRUE;
  184.   ReInitLZW();
  185. }
  186.  
  187.  
  188. LOCAL int
  189. GetCode (compress_info_ptr cinfo)
  190. /* Fetch the next code_size bits from the GIF data */
  191. /* We assume code_size is less than 16 */
  192. {
  193.   register INT32 accum;
  194.   int offs, ret, count;
  195.  
  196.   if ( (cur_bit+code_size) > last_bit) {
  197.     /* Time to reload the buffer */
  198.     if (out_of_blocks) {
  199.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  200.       return end_code;        /* fake something useful */
  201.     }
  202.     /* preserve last two bytes of what we have -- assume code_size <= 16 */
  203.     code_buf[0] = code_buf[last_byte-2];
  204.     code_buf[1] = code_buf[last_byte-1];
  205.     /* Load more bytes; set flag if we reach the terminator block */
  206.     if ((count = GetDataBlock(cinfo, &code_buf[2])) == 0) {
  207.       out_of_blocks = TRUE;
  208.       WARNMS(cinfo->emethods, "Ran out of GIF bits");
  209.       return end_code;        /* fake something useful */
  210.     }
  211.     /* Reset counters */
  212.     cur_bit = (cur_bit - last_bit) + 16;
  213.     last_byte = 2 + count;
  214.     last_bit = last_byte * 8;
  215.   }
  216.  
  217.   /* Form up next 24 bits in accum */
  218.   offs = cur_bit >> 3;        /* byte containing cur_bit */
  219. #ifdef CHAR_IS_UNSIGNED
  220.   accum = code_buf[offs+2];
  221.   accum <<= 8;
  222.   accum |= code_buf[offs+1];
  223.   accum <<= 8;
  224.   accum |= code_buf[offs];
  225. #else
  226.   accum = code_buf[offs+2] & 0xFF;
  227.   accum <<= 8;
  228.   accum |= code_buf[offs+1] & 0xFF;
  229.   accum <<= 8;
  230.   accum |= code_buf[offs] & 0xFF;
  231. #endif
  232.  
  233.   /* Right-align cur_bit in accum, then mask off desired number of bits */
  234.   accum >>= (cur_bit & 7);
  235.   ret = ((int) accum) & ((1 << code_size) - 1);
  236.   
  237.   cur_bit += code_size;
  238.   return ret;
  239. }
  240.  
  241.  
  242. LOCAL int
  243. LZWReadByte (compress_info_ptr cinfo)
  244. /* Read an LZW-compressed byte */
  245. {
  246.   static int oldcode;        /* previous LZW symbol */
  247.   static int firstcode;        /* first byte of oldcode's expansion */
  248.   register int code;        /* current working code */
  249.   int incode;            /* saves actual input code */
  250.  
  251.   /* First time, just eat the expected Clear code(s) and return next code, */
  252.   /* which is expected to be a raw byte. */
  253.   if (first_time) {
  254.     first_time = FALSE;
  255.     code = clear_code;        /* enables sharing code with Clear case */
  256.   } else {
  257.  
  258.     /* If any codes are stacked from a previously read symbol, return them */
  259.     if (sp > symbol_stack)
  260.       return (int) *(--sp);
  261.  
  262.     /* Time to read a new symbol */
  263.     code = GetCode(cinfo);
  264.  
  265.   }
  266.  
  267.   if (code == clear_code) {
  268.     /* Reinit static state, swallow any extra Clear codes, and */
  269.     /* return next code, which is expected to be a raw byte. */
  270.     ReInitLZW();
  271.     do {
  272.       code = GetCode(cinfo);
  273.     } while (code == clear_code);
  274.     if (code > clear_code) {    /* make sure it is a raw byte */
  275.       WARNMS(cinfo->emethods, "Corrupt data in GIF file");
  276.       code = 0;            /* use something valid */
  277.     }
  278.     firstcode = oldcode = code;    /* make firstcode, oldcode valid! */
  279.     return code;
  280.   }
  281.  
  282.   if (code == end_code) {
  283.     /* Skip the rest of the image, unless GetCode already read terminator */
  284.     if (! out_of_blocks) {
  285.       SkipDataBlocks(cinfo);
  286.       out_of_blocks = TRUE;
  287.     }
  288.     /* Complain that there's not enough data */
  289.     WARNMS(cinfo->emethods, "Premature end of GIF image");
  290.     /* Pad data with 0's */
  291.     return 0;            /* fake something usable */
  292.   }
  293.  
  294.   /* Got normal raw byte or LZW symbol */
  295.   incode = code;        /* save for a moment */
  296.   
  297.   if (code >= max_code) {    /* special case for not-yet-defined symbol */
  298.     /* code == max_code is OK; anything bigger is bad data */
  299.     if (code > max_code) {
  300.       WARNMS(cinfo->emethods, "Corrupt data in GIF file");
  301.       incode = 0;        /* prevent creation of loops in symbol table */
  302.     }
  303.     *sp++ = (UINT8) firstcode;    /* it will be defined as oldcode/firstcode */
  304.     code = oldcode;
  305.   }
  306.  
  307.   /* If it's a symbol, expand it into the stack */
  308.   while (code >= clear_code) {
  309.     *sp++ = symbol_tail[code];    /* tail of symbol: a simple byte value */
  310.     code = symbol_head[code];    /* head of symbol: another LZW symbol */
  311.   }
  312.   /* At this point code just represents a raw byte */
  313.   firstcode = code;        /* save for possible future use */
  314.  
  315.   /* If there's room in table, */
  316.   if ((code = max_code) < LZW_TABLE_SIZE) {
  317.     /* Define a new symbol = prev sym + head of this sym's expansion */
  318.     symbol_head[code] = oldcode;
  319.     symbol_tail[code] = (UINT8) firstcode;
  320.     max_code++;
  321.     /* Is it time to increase code_size? */
  322.     if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
  323.       code_size++;
  324.       limit_code <<= 1;        /* keep equal to 2^code_size */
  325.     }
  326.   }
  327.   
  328.   oldcode = incode;        /* save last input symbol for future use */
  329.   return firstcode;        /* return first byte of symbol's expansion */
  330. }
  331.  
  332.  
  333. LOCAL void
  334. ReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
  335. /* Read a GIF colormap */
  336. {
  337.   int i;
  338.  
  339.   for (i = 0; i < cmaplen; i++) {
  340.     cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
  341.     cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
  342.     cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
  343.   }
  344. }
  345.  
  346.  
  347. LOCAL void
  348. DoExtension (compress_info_ptr cinfo)
  349. /* Process an extension block */
  350. /* Currently we ignore 'em all */
  351. {
  352.   int extlabel;
  353.  
  354.   /* Read extension label byte */
  355.   extlabel = ReadByte(cinfo);
  356.   TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
  357.        extlabel);
  358.   /* Skip the data block(s) associated with the extension */
  359.   SkipDataBlocks(cinfo);
  360. }
  361.  
  362.  
  363. /*
  364.  * Read the file header; return image size and component count.
  365.  */
  366.  
  367. METHODDEF void
  368. input_init (compress_info_ptr cinfo)
  369. {
  370.   char hdrbuf[10];        /* workspace for reading control blocks */
  371.   UINT16 width, height;        /* image dimensions */
  372.   int colormaplen, aspectRatio;
  373.   int c;
  374.  
  375.   /* Allocate space to store the colormap */
  376.   colormap = (*cinfo->emethods->alloc_small_sarray)
  377.         ((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
  378.  
  379.   /* Read and verify GIF Header */
  380.   if (! ReadOK(cinfo->input_file, hdrbuf, 6))
  381.     ERREXIT(cinfo->emethods, "Not a GIF file");
  382.   if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
  383.     ERREXIT(cinfo->emethods, "Not a GIF file");
  384.   /* Check for expected version numbers.
  385.    * If unknown version, give warning and try to process anyway;
  386.    * this is per recommendation in GIF89a standard.
  387.    */
  388.   if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
  389.       (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
  390.     TRACEMS3(cinfo->emethods, 1,
  391.          "Warning: unexpected GIF version number '%c%c%c'",
  392.          hdrbuf[3], hdrbuf[4], hdrbuf[5]);
  393.  
  394.   /* Read and decipher Logical Screen Descriptor */
  395.   if (! ReadOK(cinfo->input_file, hdrbuf, 7))
  396.     ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  397.   width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
  398.   height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
  399.   colormaplen = 2 << (hdrbuf[4] & 0x07);
  400.   /* we ignore the color resolution, sort flag, and background color index */
  401.   aspectRatio = hdrbuf[6] & 0xFF;
  402.   if (aspectRatio != 0 && aspectRatio != 49)
  403.     TRACEMS(cinfo->emethods, 1, "Warning: nonsquare pixels in input");
  404.  
  405.   /* Read global colormap if header indicates it is present */
  406.   if (BitSet(hdrbuf[4], COLORMAPFLAG))
  407.     ReadColorMap(cinfo, colormaplen, colormap);
  408.  
  409.   /* Scan until we reach start of desired image.
  410.    * We don't currently support skipping images, but could add it easily.
  411.    */
  412.   for (;;) {
  413.     c = ReadByte(cinfo);
  414.  
  415.     if (c == ';')        /* GIF terminator?? */
  416.       ERREXIT(cinfo->emethods, "Too few images in GIF file");
  417.  
  418.     if (c == '!') {        /* Extension */
  419.       DoExtension(cinfo);
  420.       continue;
  421.     }
  422.     
  423.     if (c != ',') {        /* Not an image separator? */
  424.       TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c);
  425.       continue;
  426.     }
  427.  
  428.     /* Read and decipher Local Image Descriptor */
  429.     if (! ReadOK(cinfo->input_file, hdrbuf, 9))
  430.       ERREXIT(cinfo->emethods, "Premature EOF in GIF file");
  431.     /* we ignore top/left position info, also sort flag */
  432.     width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
  433.     height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
  434.     is_interlaced = BitSet(hdrbuf[8], INTERLACE);
  435.  
  436.     /* Read local colormap if header indicates it is present */
  437.     /* Note: if we wanted to support skipping images, */
  438.     /* we'd need to skip rather than read colormap for ignored images */
  439.     if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
  440.       colormaplen = 2 << (hdrbuf[8] & 0x07);
  441.       ReadColorMap(cinfo, colormaplen, colormap);
  442.     }
  443.  
  444.     input_code_size = ReadByte(cinfo); /* get minimum-code-size byte */
  445.     if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
  446.       ERREXIT1(cinfo->emethods, "Bogus codesize %d", input_code_size);
  447.  
  448.     /* Reached desired image, so break out of loop */
  449.     /* If we wanted to skip this image, */
  450.     /* we'd call SkipDataBlocks and then continue the loop */
  451.     break;
  452.   }
  453.  
  454.   /* Prepare to read selected image: first initialize LZW decompressor */
  455.   symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
  456.                 (LZW_TABLE_SIZE * SIZEOF(UINT16));
  457.   symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  458.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  459.   symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
  460.                 (LZW_TABLE_SIZE * SIZEOF(UINT8));
  461.   InitLZWCode();
  462.  
  463.   /*
  464.    * If image is interlaced, we read it into a full-size sample array,
  465.    * decompressing as we go; then get_input_row selects rows from the
  466.    * sample array in the proper order.
  467.    */
  468.   if (is_interlaced) {
  469.     /* We request the big array now, but can't access it until the pipeline
  470.      * controller causes all the big arrays to be allocated.  Hence, the
  471.      * actual work of reading the image is postponed until the first call
  472.      * of get_input_row.
  473.      */
  474.     interlaced_image = (*cinfo->emethods->request_big_sarray)
  475.         ((long) width, (long) height, 1L);
  476.     cinfo->methods->get_input_row = load_interlaced_image;
  477.     cinfo->total_passes++;    /* count file reading as separate pass */
  478.   }
  479.  
  480.   /* Return info about the image. */
  481.   cinfo->input_components = NUMCOLORS;
  482.   cinfo->in_color_space = CS_RGB;
  483.   cinfo->image_width = width;
  484.   cinfo->image_height = height;
  485.   cinfo->data_precision = 8;    /* always, even if 12-bit JSAMPLEs */
  486.  
  487.   TRACEMS3(cinfo->emethods, 1, "%ux%ux%d GIF image",
  488.        (unsigned int) width, (unsigned int) height, colormaplen);
  489. }
  490.  
  491.  
  492. /*
  493.  * Read one row of pixels.
  494.  * This version is used for noninterlaced GIF images:
  495.  * we read directly from the GIF file.
  496.  */
  497.  
  498. METHODDEF void
  499. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  500. {
  501.   register JSAMPROW ptr0, ptr1, ptr2;
  502.   register long col;
  503.   register int c;
  504.   
  505.   ptr0 = pixel_row[0];
  506.   ptr1 = pixel_row[1];
  507.   ptr2 = pixel_row[2];
  508.   for (col = cinfo->image_width; col > 0; col--) {
  509.     c = LZWReadByte(cinfo);
  510.     *ptr0++ = colormap[CM_RED][c];
  511.     *ptr1++ = colormap[CM_GREEN][c];
  512.     *ptr2++ = colormap[CM_BLUE][c];
  513.   }
  514. }
  515.  
  516.  
  517. /*
  518.  * Read one row of pixels.
  519.  * This version is used for the first call on get_input_row when
  520.  * reading an interlaced GIF file: we read the whole image into memory.
  521.  */
  522.  
  523. METHODDEF void
  524. load_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  525. {
  526.   JSAMPARRAY image_ptr;
  527.   register JSAMPROW sptr;
  528.   register long col;
  529.   long row;
  530.  
  531.   /* Read the interlaced image into the big array we've created. */
  532.   for (row = 0; row < cinfo->image_height; row++) {
  533.     (*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
  534.     image_ptr = (*cinfo->emethods->access_big_sarray)
  535.             (interlaced_image, row, TRUE);
  536.     sptr = image_ptr[0];
  537.     for (col = cinfo->image_width; col > 0; col--) {
  538.       *sptr++ = (JSAMPLE) LZWReadByte(cinfo);
  539.     }
  540.   }
  541.   cinfo->completed_passes++;
  542.  
  543.   /* Replace method pointer so subsequent calls don't come here. */
  544.   cinfo->methods->get_input_row = get_interlaced_row;
  545.   /* Initialize for get_interlaced_row, and perform first call on it. */
  546.   cur_row_number = 0;
  547.   pass2_offset = (cinfo->image_height + 7L) / 8L;
  548.   pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
  549.   pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;
  550.  
  551.   get_interlaced_row(cinfo, pixel_row);
  552. }
  553.  
  554.  
  555. /*
  556.  * Read one row of pixels.
  557.  * This version is used for interlaced GIF images:
  558.  * we read from the big in-memory image.
  559.  */
  560.  
  561. METHODDEF void
  562. get_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  563. {
  564.   JSAMPARRAY image_ptr;
  565.   register JSAMPROW sptr, ptr0, ptr1, ptr2;
  566.   register long col;
  567.   register int c;
  568.   long irow;
  569.  
  570.   /* Figure out which row of interlaced image is needed, and access it. */
  571.   switch ((int) (cur_row_number & 7L)) {
  572.   case 0:            /* first-pass row */
  573.     irow = cur_row_number >> 3;
  574.     break;
  575.   case 4:            /* second-pass row */
  576.     irow = (cur_row_number >> 3) + pass2_offset;
  577.     break;
  578.   case 2:            /* third-pass row */
  579.   case 6:
  580.     irow = (cur_row_number >> 2) + pass3_offset;
  581.     break;
  582.   default:            /* fourth-pass row */
  583.     irow = (cur_row_number >> 1) + pass4_offset;
  584.     break;
  585.   }
  586.   image_ptr = (*cinfo->emethods->access_big_sarray)
  587.             (interlaced_image, irow, FALSE);
  588.   /* Scan the row, expand colormap, and output */
  589.   sptr = image_ptr[0];
  590.   ptr0 = pixel_row[0];
  591.   ptr1 = pixel_row[1];
  592.   ptr2 = pixel_row[2];
  593.   for (col = cinfo->image_width; col > 0; col--) {
  594.     c = GETJSAMPLE(*sptr++);
  595.     *ptr0++ = colormap[CM_RED][c];
  596.     *ptr1++ = colormap[CM_GREEN][c];
  597.     *ptr2++ = colormap[CM_BLUE][c];
  598.   }
  599.   cur_row_number++;        /* for next time */
  600. }
  601.  
  602.  
  603. /*
  604.  * Finish up at the end of the file.
  605.  */
  606.  
  607. METHODDEF void
  608. input_term (compress_info_ptr cinfo)
  609. {
  610.   /* no work (we let free_all release the workspace) */
  611. }
  612.  
  613.  
  614. /*
  615.  * The method selection routine for GIF format input.
  616.  * Note that this must be called by the user interface before calling
  617.  * jpeg_compress.  If multiple input formats are supported, the
  618.  * user interface is responsible for discovering the file format and
  619.  * calling the appropriate method selection routine.
  620.  */
  621.  
  622. GLOBAL void
  623. jselrgif (compress_info_ptr cinfo)
  624. {
  625.   cinfo->methods->input_init = input_init;
  626.   cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
  627.   cinfo->methods->input_term = input_term;
  628. }
  629.  
  630. #endif /* GIF_SUPPORTED */
  631.