GIFDECOD.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 1.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 OpenFX Development Team
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 #define MODULE_GIFDECOD
00025 
00026 // This is a standard GIF decoder 
00027 
00028 
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <math.h>
00032 
00033 #ifndef TRUE
00034 #define TRUE 1
00035 #endif
00036 
00037 #ifndef FALSE
00038 #define FALSE 0
00039 #endif
00040 
00041 typedef struct Colour_Struct COLOUR;
00042 typedef struct Colour_Map_Entry COLOUR_MAP_ENTRY;
00043 typedef struct Colour_Map_Struct COLOUR_MAP;
00044 typedef struct Image_Struct IMAGE;
00045 
00046 struct Colour_Struct
00047    {
00048    double Red, Green, Blue, Alpha;
00049    };
00050 
00051 
00052 struct Colour_Map_Entry
00053    {
00054    double start, end;
00055    COLOUR Start_Colour, End_Colour;
00056    };
00057 
00058 
00059 struct Colour_Map_Struct
00060    {
00061    long Number_Of_Entries;
00062    COLOUR_MAP_ENTRY *Colour_Map_Entries;
00063    };
00064 
00065 
00066 struct Image_Struct
00067    {
00068    double width, height;
00069    long iwidth, iheight;
00070    unsigned char *red, *green, *blue;
00071    };
00072 
00073 /* Types for reading IFF files. */
00074 typedef struct {
00075    unsigned short Red, Green, Blue;
00076    } IMAGE_COLOUR;
00077 
00078 #define LOCAL static
00079 #define IMPORT extern
00080 
00081 #define FAST register
00082 
00083 typedef short WORD;
00084 typedef unsigned short UWORD;
00085 typedef char TEXT;
00086 typedef unsigned char UTINY;
00087 typedef long LONG;
00088 typedef unsigned long ULONG;
00089 typedef long INT;
00090 
00091 
00092 /* Various error codes used by decoder
00093  * and my own routines...   It's okay
00094  * for you to define whatever you want,
00095  * as long as it's negative...  It will be
00096  * returned intact up the various subroutine
00097  * levels...
00098  */
00099 #define OUT_OF_MEMORY -10
00100 #define BAD_CODE_SIZE -20
00101 #define WRITE_ERROR -2
00102 #define OPEN_ERROR -3
00103 #define CREATE_ERROR -4
00104 
00105 
00106 /* Prototypes for functions defined in gifdecod.c */
00107 #ifdef _SUNSTYLE
00108 static short init_exp ();
00109 static short get_next_code();
00110 short GIF_decoder();
00111 IMPORT long get_byte();
00112 IMPORT long out_line();
00113 #else
00114 static short init_exp (short size);
00115 static short get_next_code (void);
00116 short GIF_decoder(short linewidth);
00117 IMPORT long get_byte(void);
00118 IMPORT long out_line(unsigned char *pixels, long linelen);
00119 #endif
00120 
00121 /* IMPORT INT bad_code_count;
00122  *
00123  * This value is the only other global required by the using program, and
00124  * is incremented each time an out of range code is read by the decoder.
00125  * When this value is non-zero after a decode, your GIF file is probably
00126  * corrupt in some way...
00127  */
00128 static INT bad_code_count;
00129 
00130 #define MAX_CODES   4095
00131 
00132 /* Static variables */
00133 LOCAL WORD curr_size;                     /* The current code size */
00134 LOCAL WORD clear;                         /* Value for a clear code */
00135 LOCAL WORD ending;                        /* Value for a ending code */
00136 LOCAL WORD newcodes;                      /* First available code */
00137 LOCAL WORD top_slot;                      /* Highest code for current size */
00138 LOCAL WORD slot;                          /* Last read code */
00139 
00140 /* The following static variables are used
00141  * for seperating out codes
00142  */
00143 LOCAL WORD navail_bytes = 0;              /* # bytes left in block */
00144 LOCAL WORD nbits_left = 0;                /* # bits left in current byte */
00145 LOCAL UTINY b1;                           /* Current byte */
00146 LOCAL UTINY byte_buff[257];               /* Current block */
00147 LOCAL UTINY *pbytes;                      /* Pointer to next byte in block */
00148 
00149 LOCAL LONG code_mask[13] = {
00150      0,
00151      0x0001, 0x0003,
00152      0x0007, 0x000F,
00153      0x001F, 0x003F,
00154      0x007F, 0x00FF,
00155      0x01FF, 0x03FF,
00156      0x07FF, 0x0FFF
00157      };
00158 
00159 #ifdef _SUNSTYLE
00160 LOCAL WORD init_exp(size) WORD size; {
00161 #else
00162 LOCAL WORD init_exp(WORD size){
00163 #endif
00164    curr_size = size + 1;
00165    top_slot = 1 << curr_size;
00166    clear = 1 << size;
00167    ending = clear + 1;
00168    slot = newcodes = ending + 1;
00169    navail_bytes = nbits_left = 0;
00170    return(0);
00171    }
00172 
00173 /* get_next_code()
00174  * - gets the next code from the GIF file.  Returns the code, or else
00175  * a negative number in case of file errors...
00176  */
00177 #ifdef _SUNSTYLE
00178 LOCAL WORD get_next_code(){
00179 #else
00180 LOCAL WORD get_next_code(void){
00181 #endif
00182    WORD i, x;
00183    ULONG ret;
00184 
00185    if (nbits_left == 0)
00186       {
00187       if (navail_bytes <= 0)
00188          {
00189 
00190          /* Out of bytes in current block, so read next block
00191           */
00192          pbytes = byte_buff;
00193          if ((navail_bytes = get_byte()) < 0)
00194             return(navail_bytes);
00195          else if (navail_bytes)
00196             {
00197             for (i = 0; i < navail_bytes; ++i)
00198                {
00199                if ((x = get_byte()) < 0)
00200                   return(x);
00201                byte_buff[i] = x;
00202                }
00203             }
00204          }
00205       b1 = *pbytes++;
00206       nbits_left = 8;
00207       --navail_bytes;
00208       }
00209 
00210    ret = b1 >> (8 - nbits_left);
00211    while (curr_size > nbits_left)
00212       {
00213       if (navail_bytes <= 0)
00214          {
00215 
00216          /* Out of bytes in current block, so read next block
00217           */
00218          pbytes = byte_buff;
00219          if ((navail_bytes = get_byte()) < 0)
00220             return(navail_bytes);
00221          else if (navail_bytes)
00222             {
00223             for (i = 0; i < navail_bytes; ++i)
00224                {
00225                if ((x = get_byte()) < 0)
00226                   return(x);
00227                byte_buff[i] = x;
00228                }
00229             }
00230          }
00231       b1 = *pbytes++;
00232       ret |= b1 << nbits_left;
00233       nbits_left += 8;
00234       --navail_bytes;
00235       }
00236    nbits_left -= curr_size;
00237    ret &= code_mask[curr_size];
00238    return((WORD)(ret));
00239    }
00240 
00241 
00242 /* The reason we have these seperated like this instead of using
00243  * a structure like the original Wilhite code did, is because this
00244  * stuff generally produces significantly faster code when compiled...
00245  * This code is full of similar speedups...  (For a good book on writing
00246  * C for speed or for space optomisation, see Efficient C by Tom Plum,
00247  * published by Plum-Hall Associates...)
00248  */
00249 
00250 /*
00251 I removed the LOCAL identifiers in the arrays below and replaced them
00252 with 'extern's so as to declare (and re-use) the space elsewhere.
00253 The arrays are actually declared in the assembler source.
00254                                                     Bert Tyler
00255 */
00256 
00257 LOCAL UTINY dstack[MAX_CODES + 1];      /* Stack for storing pixels */
00258 LOCAL UTINY suffix[MAX_CODES + 1];      /* Suffix table */
00259 LOCAL UWORD prefix[MAX_CODES + 1];      /* Prefix linked list */
00260 extern UTINY *decoderline;              /* decoded line goes here */
00261 
00262 /* WORD GIF_decoder(linewidth)
00263  *    WORD linewidth;               * Pixels per line of image *
00264  *
00265  * - This function decodes an LZW image, according to the method used
00266  * in the GIF spec.  Every *linewidth* "characters" (ie. pixels) decoded
00267  * will generate a call to out_line(), which is a user specific function
00268  * to display a line of pixels.  The function gets its codes from
00269  * get_next_code() which is responsible for reading blocks of data and
00270  * seperating them into the proper size codes.  Finally, get_byte() is
00271  * the global routine to read the next byte from the GIF file.
00272  *
00273  * It is generally a good idea to have linewidth correspond to the actual
00274  * width of a line (as specified in the Image header) to make your own
00275  * code a bit simpler, but it isn't absolutely necessary.
00276  *
00277  * Returns: 0 if successful, else negative.  (See ERRS.H)
00278  *
00279  */
00280 
00281 #ifdef _SUNSTYLE
00282 WORD GIF_decoder(linewidth) WORD linewidth; {
00283 #else
00284 WORD GIF_decoder(WORD linewidth){
00285 #endif
00286    FAST UTINY *sp, *bufptr;
00287    UTINY *buf;
00288    FAST WORD code, fc, oc, bufcnt;
00289    WORD c, size, ret;
00290 
00291    /* Initialize for decoding a new image...
00292     */
00293    if ((size = get_byte()) < 0)
00294       return(size);
00295    if (size < 2 || 9 < size)
00296       return(BAD_CODE_SIZE);
00297    init_exp(size);
00298 
00299    /* Initialize in case they forgot to put in a clear code.
00300     * (This shouldn't happen, but we'll try and decode it anyway...)
00301     */
00302    oc = fc = 0;
00303 
00304    buf = decoderline;
00305 
00306    /* Set up the stack pointer and decode buffer pointer
00307     */
00308    sp = dstack;
00309    bufptr = buf;
00310    bufcnt = linewidth;
00311 
00312    /* This is the main loop.  For each code we get we pass through the
00313     * linked list of prefix codes, pushing the corresponding "character" for
00314     * each code onto the stack.  When the list reaches a single "character"
00315     * we push that on the stack too, and then start unstacking each
00316     * character for output in the correct order.  Special handling is
00317     * included for the clear code, and the whole thing ends when we get
00318     * an ending code.
00319     */
00320    while ((c = get_next_code()) != ending)
00321       {
00322 
00323       /* If we had a file error, return without completing the decode
00324        */
00325       if (c < 0)
00326          return(0);
00327 
00328       /* If the code is a clear code, reinitialize all necessary items.
00329        */
00330       if (c == clear)
00331          {
00332          curr_size = size + 1;
00333          slot = newcodes;
00334          top_slot = 1 << curr_size;
00335 
00336          /* Continue reading codes until we get a non-clear code
00337           * (Another unlikely, but possible case...)
00338           */
00339          while ((c = get_next_code()) == clear)
00340             ;
00341 
00342          /* If we get an ending code immediately after a clear code
00343           * (Yet another unlikely case), then break out of the loop.
00344           */
00345          if (c == ending)
00346             break;
00347 
00348          /* Finally, if the code is beyond the range of already set codes,
00349           * (This one had better NOT happen...  I have no idea what will
00350           * result from this, but I doubt it will look good...) then set it
00351           * to color zero.
00352           */
00353          if (c >= slot)
00354             c = 0;
00355 
00356          oc = fc = c;
00357 
00358          /* And let us not forget to put the char into the buffer... And
00359           * if, on the off chance, we were exactly one pixel from the end
00360           * of the line, we have to send the buffer to the out_line()
00361           * routine...
00362           */
00363          *bufptr++ = c;
00364          if (--bufcnt == 0)
00365             {
00366             if ((ret = out_line(buf, linewidth)) < 0)
00367                return(ret);
00368             bufptr = buf;
00369             bufcnt = linewidth;
00370             }
00371          }
00372       else
00373          {
00374 
00375          /* In this case, it's not a clear code or an ending code, so
00376           * it must be a code code...  So we can now decode the code into
00377           * a stack of character codes. (Clear as mud, right?)
00378           */
00379          code = c;
00380 
00381          /* Here we go again with one of those off chances...  If, on the
00382           * off chance, the code we got is beyond the range of those already
00383           * set up (Another thing which had better NOT happen...) we trick
00384           * the decoder into thinking it actually got the last code read.
00385           * (Hmmn... I'm not sure why this works...  But it does...)
00386           */
00387          if (code >= slot)
00388             {
00389             if (code > slot)
00390                ++bad_code_count;
00391             code = oc;
00392             *sp++ = fc;
00393             }
00394 
00395          /* Here we scan back along the linked list of prefixes, pushing
00396           * helpless characters (ie. suffixes) onto the stack as we do so.
00397           */
00398          while (code >= newcodes)
00399             {
00400             *sp++ = suffix[code];
00401             code = prefix[code];
00402             }
00403 
00404          /* Push the last character on the stack, and set up the new
00405           * prefix and suffix, and if the required slot number is greater
00406           * than that allowed by the current bit size, increase the bit
00407           * size.  (NOTE - If we are all full, we *don't* save the new
00408           * suffix and prefix...  I'm not certain if this is correct...
00409           * it might be more proper to overwrite the last code...
00410           */
00411          *sp++ = code;
00412          if (slot < top_slot)
00413             {
00414             suffix[slot] = fc = code;
00415             prefix[slot++] = oc;
00416             oc = c;
00417             }
00418          if (slot >= top_slot)
00419             if (curr_size < 12)
00420                {
00421                top_slot <<= 1;
00422                ++curr_size;
00423                }
00424 
00425          /* Now that we've pushed the decoded string (in reverse order)
00426           * onto the stack, lets pop it off and put it into our decode
00427           * buffer...  And when the decode buffer is full, write another
00428           * line...
00429           */
00430          while (sp > dstack)
00431             {
00432             *bufptr++ = *(--sp);
00433             if (--bufcnt == 0)
00434                {
00435                if ((ret = out_line(buf, linewidth)) < 0)
00436                   return(ret);
00437                bufptr = buf;
00438                bufcnt = linewidth;
00439                }
00440             }
00441          }
00442       }
00443    ret = 0;
00444    if (bufcnt != linewidth)
00445       ret = out_line(buf, (linewidth - bufcnt));
00446    return(ret);
00447    }
00448 

Generated on Sun Apr 27 14:20:13 2014 for OpenFX by  doxygen 1.5.6