JPEGView/Independent JPEG Group/wrgif.c

1 line
15 KiB
C
Raw Permalink Normal View History

/* * wrgif.c * * Copyright (C) 1991-1994, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains routines to write output images in GIF format. * * These routines may need modification for non-Unix environments or * specialized applications. As they stand, they assume output to * an ordinary stdio stream. */ /* * This code is loosely based on ppmtogif from the PBMPLUS distribution * of Feb. 1991. That file contains the following copyright notice: * Based on GIFENCODE by David Rowley <mgardi@watdscu.waterloo.edu>. * Lempel-Ziv compression based on "compress" by Spencer W. Thomas et al. * Copyright (C) 1989 by Jef Poskanzer. * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, provided * that the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation. This software is provided "as is" without express or * implied warranty. * * We are also required to state that * "The Graphics Interchange Format(c) is the Copyright property of * CompuServe Incorporated. GIF(sm) is a Service Mark property of * CompuServe Incorporated." */ #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ #ifdef GIF_SUPPORTED #define MAX_LZW_BITS 12 /* maximum LZW code size (4096 symbols) */ typedef INT16 code_int; /* must hold -1 .. 2**MAX_LZW_BITS */ #define LZW_TABLE_SIZE ((code_int) 1 << MAX_LZW_BITS) #define HSIZE 5003 /* hash table size for 80% occupancy */ typedef int hash_int; /* must hold -2*HSIZE..2*HSIZE */ #define MAXCODE(n_bits) (((code_int) 1 << (n_bits)) - 1) /* * The LZW hash table consists of two parallel arrays: * hash_code[i] code of symbol in slot i, or 0 if empty slot * hash_value[i] symbol's value; undefined if empty slot * where slot values (i) range from 0 to HSIZE-1. The symbol value is * its prefix symbol's code concatenated with its suffix character. * * Algorithm: use open addressing double hashing (no chaining) on the * prefix code / suffix character combination. We do a variant of Knuth's * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime * secondary probe. * * The hash_value[] table is allocated from FAR heap space since it would * use up rather a lot of the near data space in a PC. */ typedef INT32 hash_entry; /* must hold (code_int<<8) | byte */ #define HASH_ENTRY(prefix,suffix) ((((hash_entry) (prefix)) << 8) | (suffix)) /* Private version of data destination object */ typedef struct { struct djpeg_dest_struct pub; /* public fields */ j_decompress_ptr cinfo; /* back link saves passing separate parm */ /* State for packing variable-width codes into a bitstream */ int n_bits; /* current number of bits/code */ code_int maxcode; /* maximum code, given n_bits */ int init_bits; /* initial n_bits ... restored after clear */ INT32 cur_accum; /* holds bits not yet output */ int cur_bits; /* # of bits in cur_accum */ /* LZW string construction */ code_int waiting_code; /* symbol not yet output; may be extendable */ boolean first_byte; /* if TRUE, waiting_code is not valid */ /* State for LZW code assignment */ code_int ClearCode; /* clear code (doesn't change) */ code_int EOFCode; /* EOF code (ditto) */ code_int free_code; /* first not-yet-used symbol code */ /* LZW hash table */ code_int *hash_code; /* => hash table of symbol codes */ hash_entry FAR *hash_value; /* => hash table of symbol values */ /* GIF data packet construction buffer */ int bytesinpkt; /* # of bytes in current packet */ char packetbuf[256]; /* workspace for accumulating packet */ } gif_dest_struct; typedef gif_dest_struct * gif_dest_ptr; /* * Routines to package compressed data bytes into GIF data blocks. * A data block consists of a count byte (1..255) an