/* * * GIFENCOD.C - GIF Image compression routines * * Lempel-Ziv compression based on 'compress'. * Original GIF modifications by David Rowley * (mgardi@watdcsu.waterloo.edu) * Converted for use in RAYDSP by John Lowery. * */ #include #include #include #include "display.h" extern int ofil; /* GIF output file handle */ /* * General DEFINEs */ #define CODEBITS 12 #define HSIZE 5003 /* 80% occupancy */ /* * * GIF Image compression - modified 'compress' * * Based on: compress.c - File compression ala IEEE Computer, June 1984. * * By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) * Jim McKie (decvax!mcvax!jim) * Steve Davies (decvax!vax135!petsd!peora!srd) * Ken Turkowski (decvax!decwrl!turtlevax!ken) * James A. Woods (decvax!ihnp4!ames!jaw) * Joe Orost (decvax!vax135!petsd!joe) * */ static int n_bits; /* number of bits/code */ static short maxcode; /* maximum code, given n_bits */ static short maxmaxcode = 1< 0L ) goto probe; nomatch: output ( (short) ent ); ent = c; if ( free_ent < maxmaxcode ) { CodeTabOf (i) = free_ent++; /* code -> hashtable */ HashTabOf (i) = fcode; } else { cl_block(); } } return(disk_error); } int gifFlush() { output( (short) EOFCode ); return (disk_error); } /* * output() * * Output the given code. * Inputs: * code: A n_bits-bit integer. If == -1, then EOF. This assumes * that n_bits =< (long)wordsize - 1. * Outputs: * Outputs code to the file. * Assumptions: * Chars are 8 bits long. * Algorithm: * Maintain a CODEBITS character long buffer (so that 8 codes will * fit in it exactly). When the buffer fills up empty it and start over. */ static unsigned long cur_accum = 0; static int cur_bits = 0; static unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; static void output( code ) short code; { cur_accum &= masks[ cur_bits ]; if( cur_bits > 0 ) cur_accum |= ((long)code << cur_bits); else cur_accum = code; cur_bits += n_bits; while( cur_bits >= 8 ) { char_out( (unsigned int)(cur_accum & 0xff) ); cur_accum >>= 8; cur_bits -= 8; } /* * If the next entry is going to be too big for the code size, * then increase it, if possible. */ if ( free_ent > maxcode || clear_flg ) { if( clear_flg ) { maxcode = MAXCODE (n_bits = g_init_bits); clear_flg = 0; } else { n_bits++; if ( n_bits == CODEBITS ) maxcode = maxmaxcode; else maxcode = MAXCODE(n_bits); } } if( code == EOFCode ) { /* * At EOF, write the rest of the buffer. */ while( cur_bits > 0 ) { char_out( (unsigned int)(cur_accum & 0xff) ); cur_accum >>= 8; cur_bits -= 8; } flush_char(); if (disk_error) writeError(); } } /* * Clear out the hash table */ static void cl_block () /* table clear for block compress */ { cl_hash(); free_ent = ClearCode + 2; clear_flg = 1; output( (short)ClearCode ); /* tell the decoder what we're doing */ } static void cl_hash() /* reset code table */ { memset( htab, (char) 0xFF, sizeof( htab)); } /****************************************************************************** * * GIF Specific routines * ******************************************************************************/ /* * Number of characters so far in this 'packet' */ static int a_count; /* * Set up the 'byte output' routine */ static void char_init() { a_count = 0; } /* * Define the storage for the packet accumulator */ static char accum[ 256 ]; /* * Add a character to the end of the current packet, and if it is 254 * characters, flush the packet to disk. */ static void char_out( c ) int c; { accum[ a_count++ ] = c; if( a_count >= 254 ) flush_char(); } /* * Flush the packet to disk, and reset the accumulator */ static void flush_char() { if( a_count > 0 ) { /* write the low byte only of a_count */ if ( write( ofil, (char *)&a_count, 1 ) != 1 || write( ofil, (char *)accum, a_count ) != a_count ) { disk_error = TRUE; } a_count = 0; } } /* The End */