
/*
 * $XConsortium$
 *
 * Copyright 1989 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  M.I.T. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors:  Davor Matic, MIT X Consortium
 *           Thomas Wu, MIT Fishbowl
 */

#define MAXCMD 128
#define GIF_ID "GIF87a"

#define ConvertToInt(X) ((X).lsb + ((X).msb << 8))
#define IMAGESEP 0x2c
#define INTERLACEMASK 0x40
#define COLORMAPMASK 0x80

#define Corrupt(f) ((f).corrupt)
#define CurrentByte(f) ((f)->byte_buffer[f->current_byte_offset & 3])
#define NextByte(f) ((f)->byte_buffer[f->current_byte_offset + 1 & 3])
#define NextNextByte(f) ((f)->byte_buffer[f->current_byte_offset + 2 & 3])

#define FixFilePosition(f, new_offset) \
  while((new_offset) > (f)->current_byte_offset) AdvanceByte(f);

#define AdvanceByte(f) \
{ \
    ++(f)->current_byte_offset; \
    if((f)->bytes_left-- == 0) \
      (f)->corrupt = ((f)->bytes_left = getc(f->file_ptr) - 1) < 0; \
    NextNextByte(f) = getc(f->file_ptr); \
}

typedef unsigned char Byte;

typedef struct _twobyte {
  Byte lsb, msb;
} B16int;

typedef struct _RawGifHeader {
  char id[6];
  B16int width;
  B16int height;
  Byte colormap_info;
  Byte background_color;
  Byte null_byte;
} RawGifHeader;

typedef struct _GifHeader {
  char id[6];
  int width;
  int height;
  int colormap_info;
  int background_color;
} GifHeader;

typedef struct _RawColor {
  Byte Red, Green, Blue;
} RawColor;

typedef struct _RawImageSpec {
  Byte image_separator;
  B16int left_offset;
  B16int top_offset;
  B16int width;
  B16int height;
  Byte interlace;
  Byte data_size;
} RawImageSpec;

typedef struct _ImageSpec {
  int left_offset;
  int top_offset;
  int width;
  int height;
  int interlace;
  int data_size;
} ImageSpec;

typedef struct _ByteStream {
  FILE *file_ptr;
  Bool corrupt;
  long current_byte_offset;
  int byte_buffer[4];
  int bytes_left;
} ByteStream;

#define RG_SUCCESS 0
#define RG_BAD_HEADER -1
#define RG_NOT_GIF_FILE -2
#define RG_CORRUPT -3
#define RG_MALLOC_FAILED -4
#define RG_BAD_COLORTABLE -5
#define RG_BAD_RAW_SPECS -6
#define RG_NO_SEPARATOR -7
#define RG_IMAGE_ERROR -8
#define RG_PART_CORRUPT -9
#define RG_ZCAT_ERROR -10
#define RG_FILE_NOT_FOUND -11
