lib/gifdecomp.c File Reference

#include "common.h"
#include "gifcodes.h"

Include dependency graph for gifdecomp.c:

Go to the source code of this file.

Defines

#define MAX_CODES   4095

Functions

int gif_get_byte ()
int gif_out_line ()
static WORD init_exp (WORD size)
static WORD get_next_code ()
static WORD decoder (WORD linewidth, UBYTE *buf, UBYTE *stack, UBYTE *suffix, UWORD *prefix)
int gif_decoder (int linewidth)

Variables

static char const rcsid [] = "$Id: gifdecomp.c,v 1.3 2003/05/06 07:33:42 kate Exp $"
int bad_code_count
static WORD curr_size
static WORD clear
static WORD ending
static WORD newcodes
static WORD top_slot
static WORD slot
static WORD navail_bytes = 0
static WORD nbits_left = 0
static UBYTE b1
static UBYTE gif_byte_buff [256+3]
static UBYTE * pbytes
static long code_mask [13]


Define Documentation

#define MAX_CODES   4095

Definition at line 57 of file gifdecomp.c.

Referenced by gif_decoder().


Function Documentation

static WORD decoder ( WORD  linewidth,
UBYTE *  buf,
UBYTE *  stack,
UBYTE *  suffix,
UWORD *  prefix 
) [static]

Definition at line 187 of file gifdecomp.c.

References bad_code_count, BAD_CODE_SIZE, clear, curr_size, ending, get_next_code(), gif_get_byte(), gif_out_line(), init_exp(), newcodes, slot, top_slot, UBYTE, and WORD.

Referenced by gif_decoder().

00193    {
00194    register UBYTE *sp, *bufptr;
00195    register WORD code, fc, oc, bufcnt;
00196    WORD c, size, ret;
00197 
00198    /* Initialize for decoding a new image...
00199     */
00200    if ((size = gif_get_byte()) < 0)
00201       return(size);
00202    if (size < 2 || 9 < size)
00203       return(BAD_CODE_SIZE);
00204    init_exp(size);
00205 
00206    /* Initialize in case they forgot to put in a clear code.
00207     * (This shouldn't happen, but we'll try and decode it anyway...)
00208     */
00209    oc = fc = 0;
00210 
00211 
00212    /* Set up the stack pointer and decode buffer pointer
00213     */
00214    sp = stack;
00215    bufptr = buf;
00216    bufcnt = linewidth;
00217 
00218    /* This is the main loop.  For each code we get we pass through the
00219     * linked list of prefix codes, pushing the corresponding 'character' for
00220     * each code onto the stack.  When the list reaches a single 'character'
00221     * we push that on the stack too, and then start unstacking each
00222     * character for output in the correct order.  Special handling is
00223     * included for the clear code, and the whole thing ends when we get
00224     * an ending code.
00225     */
00226    while ((c = get_next_code()) != ending)
00227       {
00228 
00229       /* If we had a file error, return without completing the decode
00230        */
00231       if (c < 0)
00232          {
00233          return(c);
00234          }
00235 
00236       /* If the code is a clear code, reinitialize all necessary items.
00237        */
00238       if (c == clear)
00239          {
00240          curr_size = size + 1;
00241          slot = newcodes;
00242          top_slot = 1 << curr_size;
00243 
00244          /* Continue reading codes until we get a non-clear code
00245           * (Another unlikely, but possible case...)
00246           */
00247          while ((c = get_next_code()) == clear)
00248             ;
00249 
00250          /* If we get an ending code immediately after a clear code
00251           * (Yet another unlikely case), then break out of the loop.
00252           */
00253          if (c == ending)
00254             break;
00255 
00256          /* Finally, if the code is beyond the range of already set codes,
00257           * (This one had better NOT happen...  I have no idea what will
00258           * result from this, but I doubt it will look good...) then set it
00259           * to color zero.
00260           */
00261          if (c >= slot)
00262             c = 0;
00263 
00264          oc = fc = c;
00265 
00266          /* And let us not forget to put the char into the buffer... And
00267           * if, on the off chance, we were exactly one pixel from the end
00268           * of the line, we have to send the buffer to the gif_out_line()
00269           * routine...
00270           */
00271          *bufptr++ = c;
00272          if (--bufcnt == 0)
00273             {
00274             if ((ret = gif_out_line(buf, linewidth)) < 0)
00275                {
00276                return(ret);
00277                }
00278             bufptr = buf;
00279             bufcnt = linewidth;
00280             }
00281          }
00282       else
00283          {
00284 
00285          /* In this case, it's not a clear code or an ending code, so
00286           * it must be a code code...  So we can now decode the code into
00287           * a stack of character codes. (Clear as mud, right?)
00288           */
00289          code = c;
00290 
00291          /* Here we go again with one of those off chances...  If, on the
00292           * off chance, the code we got is beyond the range of those already
00293           * set up (Another thing which had better NOT happen...) we trick
00294           * the decoder into thinking it actually got the last code read.
00295           * (Hmmn... I'm not sure why this works...  But it does...)
00296           */
00297          if (code >= slot)
00298             {
00299             if (code > slot)
00300                ++bad_code_count;
00301             code = oc;
00302             *sp++ = fc;
00303             }
00304 
00305          /* Here we scan back along the linked list of prefixes, pushing
00306           * helpless characters (ie. suffixes) onto the stack as we do so.
00307           */
00308          while (code >= newcodes)
00309             {
00310             *sp++ = suffix[code];
00311             code = prefix[code];
00312             }
00313 
00314          /* Push the last character on the stack, and set up the new
00315           * prefix and suffix, and if the required slot number is greater
00316           * than that allowed by the current bit size, increase the bit
00317           * size.  (NOTE - If we are all full, we *don't* save the new
00318           * suffix and prefix...  I'm not certain if this is correct...
00319           * it might be more proper to overwrite the last code...
00320           */
00321          *sp++ = code;
00322          if (slot < top_slot)
00323             {
00324             suffix[slot] = fc = code;
00325             prefix[slot++] = oc;
00326             oc = c;
00327             }
00328          if (slot >= top_slot)
00329             if (curr_size < 12)
00330                {
00331                top_slot <<= 1;
00332                ++curr_size;
00333                } 
00334 
00335          /* Now that we've pushed the decoded string (in reverse order)
00336           * onto the stack, lets pop it off and put it into our decode
00337           * buffer...  And when the decode buffer is full, write another
00338           * line...
00339           */
00340          while (sp > stack)
00341             {
00342             *bufptr++ = *(--sp);
00343             if (--bufcnt == 0)
00344                {
00345                if ((ret = gif_out_line(buf, linewidth)) < 0)
00346                   {
00347                   return(ret);
00348                   }
00349                bufptr = buf;
00350                bufcnt = linewidth;
00351                }
00352             }
00353          }
00354       }
00355    ret = 0;
00356    if (bufcnt != linewidth)
00357       ret = gif_out_line(buf, (linewidth - bufcnt));
00358    return(ret);
00359    }

Here is the call graph for this function:

Here is the caller graph for this function:

static WORD get_next_code (  )  [static]

Definition at line 105 of file gifdecomp.c.

References b1, code_mask, curr_size, gif_byte_buff, gif_get_byte(), navail_bytes, nbits_left, pbytes, and WORD.

Referenced by decoder().

00106    {
00107    WORD i, x;
00108    unsigned long ret;
00109 
00110    if (nbits_left == 0)
00111       {
00112       if (navail_bytes <= 0)
00113          {
00114 
00115          /* Out of bytes in current block, so read next block
00116           */
00117          pbytes = gif_byte_buff;
00118          if ((navail_bytes = gif_get_byte()) < 0)
00119             return(navail_bytes);
00120          else if (navail_bytes)
00121             {
00122             for (i = 0; i < navail_bytes; ++i)
00123                {
00124                if ((x = gif_get_byte()) < 0)
00125                   return(x);
00126                gif_byte_buff[i] = x;
00127                }
00128             }
00129          }
00130       b1 = *pbytes++;
00131       nbits_left = 8;
00132       --navail_bytes;
00133       }
00134 
00135    ret = b1 >> (8 - nbits_left);
00136    while (curr_size > nbits_left)
00137       {
00138       if (navail_bytes <= 0)
00139          {
00140 
00141          /* Out of bytes in current block, so read next block
00142           */
00143          pbytes = gif_byte_buff;
00144          if ((navail_bytes = gif_get_byte()) < 0)
00145             return(navail_bytes);
00146          else if (navail_bytes)
00147             {
00148             for (i = 0; i < navail_bytes; ++i)
00149                {
00150                if ((x = gif_get_byte()) < 0)
00151                   return(x);
00152                gif_byte_buff[i] = x;
00153                }
00154             }
00155          }
00156       b1 = *pbytes++;
00157       ret |= b1 << nbits_left;
00158       nbits_left += 8;
00159       --navail_bytes;
00160       }
00161    nbits_left -= curr_size;
00162    ret &= code_mask[curr_size];
00163    return((WORD)(ret));
00164    }

Here is the call graph for this function:

Here is the caller graph for this function:

int gif_decoder ( int  linewidth  ) 

Definition at line 363 of file gifdecomp.c.

References decoder(), freeMem(), MAX_CODES, needMem(), OUT_OF_MEMORY, UBYTE, and UWORD.

00364 {
00365 UBYTE *buf, *stack, *suffix;
00366 UWORD *prefix;
00367 int ret;
00368 
00369 ret = OUT_OF_MEMORY;
00370 stack = NULL;
00371 suffix = NULL;
00372 prefix = NULL;
00373 /* stack = suffix = (UBYTE *)prefix = NULL; */
00374 if ((buf = (UBYTE *)needMem(linewidth + 1)) == NULL)
00375         goto OUT;
00376 if ((stack = (UBYTE *)needMem(MAX_CODES+1)) == NULL)
00377         goto OUT;
00378 if ((suffix = (UBYTE *)needMem(MAX_CODES+1)) == NULL)
00379         goto OUT;
00380 if ((prefix = (UWORD *)needMem((MAX_CODES+1)*sizeof(UWORD) )) == NULL)
00381         goto OUT;
00382 ret = decoder(linewidth,buf,stack,suffix,prefix);
00383 OUT:
00384 freeMem(buf);
00385 freeMem(stack);
00386 freeMem(prefix);
00387 freeMem(suffix);
00388 return(ret);
00389 }

Here is the call graph for this function:

int gif_get_byte (  ) 

Definition at line 20 of file gifread.c.

References gif_file.

Referenced by decoder(), and get_next_code().

00023 {
00024 return(fgetc(gif_file));
00025 }

Here is the caller graph for this function:

int gif_out_line (  ) 

Referenced by decoder().

Here is the caller graph for this function:

static WORD init_exp ( WORD  size  )  [static]

Definition at line 89 of file gifdecomp.c.

References clear, curr_size, ending, navail_bytes, nbits_left, newcodes, slot, and top_slot.

Referenced by decoder().

00091    {
00092    curr_size = size + 1;
00093    top_slot = 1 << curr_size;
00094    clear = 1 << size;
00095    ending = clear + 1;
00096    slot = newcodes = ending + 1;
00097    navail_bytes = nbits_left = 0;
00098    return(0);
00099    }

Here is the caller graph for this function:


Variable Documentation

UBYTE b1 [static]

Definition at line 72 of file gifdecomp.c.

Referenced by chainCalcScore(), chainCalcScoreSubChain(), and get_next_code().

int bad_code_count

Definition at line 55 of file gifdecomp.c.

Referenced by decoder().

WORD clear [static]

Definition at line 61 of file gifdecomp.c.

Referenced by decoder(), and init_exp().

long code_mask[13] [static]

Initial value:

 {
     0,
     0x0001, 0x0003,
     0x0007, 0x000F,
     0x001F, 0x003F,
     0x007F, 0x00FF,
     0x01FF, 0x03FF,
     0x07FF, 0x0FFF
     }

Definition at line 76 of file gifdecomp.c.

Referenced by get_next_code().

WORD curr_size [static]

Definition at line 60 of file gifdecomp.c.

Referenced by decoder(), get_next_code(), and init_exp().

WORD ending [static]

Definition at line 62 of file gifdecomp.c.

Referenced by decoder(), and init_exp().

UBYTE gif_byte_buff[256+3] [static]

Definition at line 73 of file gifdecomp.c.

WORD navail_bytes = 0 [static]

Definition at line 70 of file gifdecomp.c.

Referenced by get_next_code(), and init_exp().

WORD nbits_left = 0 [static]

Definition at line 71 of file gifdecomp.c.

Referenced by get_next_code(), and init_exp().

WORD newcodes [static]

Definition at line 63 of file gifdecomp.c.

Referenced by decoder(), and init_exp().

UBYTE* pbytes [static]

Definition at line 74 of file gifdecomp.c.

Referenced by get_next_code().

char const rcsid[] = "$Id: gifdecomp.c,v 1.3 2003/05/06 07:33:42 kate Exp $" [static]

Definition at line 22 of file gifdecomp.c.

WORD slot [static]

Definition at line 65 of file gifdecomp.c.

Referenced by decoder(), and init_exp().

WORD top_slot [static]

Definition at line 64 of file gifdecomp.c.

Referenced by decoder(), and init_exp().


Generated on Tue Dec 25 19:52:21 2007 for blat by  doxygen 1.5.2