Back to index

plt-scheme  4.2.1
wx_gifwr.cc
Go to the documentation of this file.
00001 /*
00002  * xvgifwr.c  -  handles writing of GIF files.  based on flgife.c and
00003  *               flgifc.c from the FBM Library, by Michael Maudlin
00004  *
00005  * Contains: 
00006  *   WriteGIF(fp, pic, w, h, rmap, gmap, bmap, numcols, colorstyle)
00007  *
00008  * Note: slightly brain-damaged, in that it'll only write non-interlaced 
00009  *       GIF files (in the interests of speed, or something)
00010  *
00011  */
00012 
00013 
00014 
00015 /*****************************************************************
00016  * Portions of this code Copyright (C) 1989 by Michael Mauldin.
00017  * Permission is granted to use this file in whole or in part provided
00018  * that you do not sell it for profit and that this copyright notice
00019  * and the names of all authors are retained unchanged.
00020  *
00021  * Authors:  Michael Mauldin (mlm@cs.cmu.edu)
00022  *           David Rowley (mgardi@watdcsu.waterloo.edu)
00023  *
00024  * Based on: compress.c - File compression ala IEEE Computer, June 1984.
00025  *
00026  *     Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
00027  *     Jim McKie               (decvax!mcvax!jim)
00028  *     Steve Davies            (decvax!vax135!petsd!peora!srd)
00029  *     Ken Turkowski           (decvax!decwrl!turtlevax!ken)
00030  *     James A. Woods          (decvax!ihnp4!ames!jaw)
00031  *     Joe Orost               (decvax!vax135!petsd!joe)
00032  *****************************************************************/
00033  
00034 /*
00035  * Copyright 1989, 1990 by the University of Pennsylvania
00036  *
00037  * Permission to use, copy, and distribute for non-commercial purposes,
00038  * is hereby granted without fee, providing that the above copyright
00039  * notice appear in all copies and that both the copyright notice and this
00040  * permission notice appear in supporting documentation.
00041  *
00042  * The software may be modified for your own purposes, but modified versions
00043  * may not be distributed.
00044  *
00045  * This software is provided "as is" without any express or implied warranty.
00046  */
00047 
00048 
00049 #include <stdlib.h>
00050 #include "wx_image.h"
00051 
00052 #ifdef MZ_PRECISE_GC
00053 END_XFORM_ARITH;
00054 #endif
00055 
00056 typedef long int        count_int;
00057 
00058 static int  Width, Height;
00059 static int  curx, cury;
00060 static long CountDown;
00061 static int  Interlace;
00062 static byte bw[2] = {0, 0xff};
00063 
00064 static void putword(int, FILE *);
00065 static void compress(int, FILE *, byte *, int);
00066 static void output(int);
00067 static void cl_block(void);
00068 static void cl_hash(count_int);
00069 static void char_init(void);
00070 static void char_out(int);
00071 static void flush_char(void);
00072 
00073 /*************************************************************/
00074 int wxImage::WriteGIF(FILE *fp, byte *pic, int w, int h, byte *rmap, byte *gmap, byte *bmap, int numcols, int colorstyle)
00075 {
00076   int RWidth, RHeight;
00077   int LeftOfs, TopOfs;
00078   int Resolution, ColorMapSize, InitCodeSize, Background, BitsPerPixel;
00079   int i,j;
00080 
00081 
00082   /* if writing B/W stipple... */
00083   if (colorstyle==2) {
00084     rmap = gmap = bmap = bw;
00085     numcols = 2;
00086   }
00087 
00088   Interlace = 0;
00089   Background = 0;
00090 
00091   /* figure out 'BitsPerPixel' */
00092   for (i=1; i<8; i++) {
00093     if ( (1<<i) >= numcols)
00094       break;
00095   }
00096   
00097   BitsPerPixel = i;
00098 
00099   ColorMapSize = 1 << BitsPerPixel;
00100        
00101   RWidth  = Width  = w;
00102   RHeight = Height = h;
00103   LeftOfs = TopOfs = 0;
00104        
00105   Resolution = BitsPerPixel;
00106 
00107   CountDown = w * h;    /* # of pixels we'll be doing */
00108 
00109   if (BitsPerPixel <= 1) InitCodeSize = 2;
00110                     else InitCodeSize = BitsPerPixel;
00111 
00112   curx = cury = 0;
00113 
00114   if (!fp) {
00115     fprintf(stderr,  "WriteGIF: file not open for writing\n" );
00116     return (1);
00117   }
00118 
00119   if (imgDEBUG) 
00120     fprintf(stderr,"WrGIF: pic=%lx, w,h=%dx%d, numcols=%d, Bits%d,Cmap=%d\n",
00121            (long)pic, w,h,numcols,BitsPerPixel,ColorMapSize);
00122 
00123   fwrite("GIF87a", 1, 6, fp);    /* the GIF magic number */
00124 
00125   putword(RWidth, fp);           /* screen descriptor */
00126   putword(RHeight, fp);
00127 
00128   i = 0x80;                    /* Yes, there is a color map */
00129   i |= (8-1)<<4;                 /* OR in the color resolution (hardwired 8) */
00130   i |= (BitsPerPixel - 1);       /* OR in the # of bits per pixel */
00131   fputc(i,fp);          
00132 
00133   fputc(Background, fp);         /* background color */
00134 
00135   fputc(0, fp);                  /* future expansion byte */
00136 
00137 
00138   if (colorstyle == 1) {         /* greyscale */
00139     for (i=0; i<ColorMapSize; i++) {
00140       j = MONO(rmap[i], gmap[i], bmap[i]);
00141       fputc(j, fp);
00142       fputc(j, fp);
00143       fputc(j, fp);
00144     }
00145   }
00146   else {
00147     for (i=0; i<ColorMapSize; i++) {       /* write out Global colormap */
00148       fputc(rmap[i], fp);
00149       fputc(gmap[i], fp);
00150       fputc(bmap[i], fp);
00151     }
00152   }
00153 
00154   fputc( ',', fp );              /* image separator */
00155 
00156   /* Write the Image header */
00157   putword(LeftOfs, fp);
00158   putword(TopOfs,  fp);
00159   putword(Width,   fp);
00160   putword(Height,  fp);
00161   if (Interlace) fputc(0x40, fp);   /* Use Global Colormap, maybe Interlace */
00162             else fputc(0x00, fp);
00163 
00164   fputc(InitCodeSize, fp);
00165   compress(InitCodeSize+1, fp, pic, w*h);
00166 
00167   fputc(0,fp);                      /* Write out a Zero-length packet (EOF) */
00168   fputc(';',fp);                    /* Write GIF file terminator */
00169 
00170   return (0);
00171 }
00172 
00173 
00174 
00175 
00176 /******************************/
00177 static void putword(int w, FILE *fp)
00178 {
00179   /* writes a 16-bit integer in GIF order (LSB first) */
00180   fputc(w & 0xff, fp);
00181   fputc((w>>8)&0xff, fp);
00182 }
00183 
00184 
00185 
00186 
00187 /***********************************************************************/
00188 
00189 
00190 static unsigned long cur_accum = 0;
00191 static int           cur_bits = 0;
00192 
00193 
00194 
00195 
00196 // #define min(a,b)        ((a>b) ? b : a)
00197 
00198 #define GIFBITS      12
00199 #define MSDOS 1
00200 
00201 #define HSIZE  5003            /* 80% occupancy */
00202 
00203 typedef unsigned char   char_type;
00204 
00205 
00206 static int n_bits;                   /* number of bits/code */
00207 static int maxbits = GIFBITS;           /* user settable max # bits/code */
00208 static int maxcode;                  /* maximum code, given n_bits */
00209 static int maxmaxcode = 1 << GIFBITS;   /* NEVER generate this */
00210 
00211 #define MAXCODE(n_bits)     ( (1 << (n_bits)) - 1)
00212 
00213 static  count_int      htab [HSIZE];
00214 static  unsigned short codetab [HSIZE];
00215 #define HashTabOf(i)   htab[i]
00216 #define CodeTabOf(i)   codetab[i]
00217 
00218 static int hsize = HSIZE;            /* for dynamic table sizing */
00219 
00220 /*
00221  * To save much memory, we overlay the table used by compress() with those
00222  * used by decompress().  The tab_prefix table is the same size and type
00223  * as the codetab.  The tab_suffix table needs 2**GIFBITS characters.  We
00224  * get this from the beginning of htab.  The output stack uses the rest
00225  * of htab, and contains characters.  There is plenty of room for any
00226  * possible stack (stack used to be 8000 characters).
00227  */
00228 
00229 #define tab_prefixof(i) CodeTabOf(i)
00230 #define tab_suffixof(i)        ((char_type *)(htab))[i]
00231 #define de_stack               ((char_type *)&tab_suffixof(1<<GIFBITS))
00232 
00233 static int free_ent = 0;                  /* first unused entry */
00234 
00235 /*
00236  * block compression parameters -- after all codes are used up,
00237  * and compression rate changes, start over.
00238  */
00239 static int clear_flg = 0;
00240 
00241 static long int in_count = 1;            /* length of input */
00242 static long int out_count = 0;           /* # of codes output (for debugging) */
00243 
00244 /*
00245  * compress stdin to stdout
00246  *
00247  * Algorithm:  use open addressing double hashing (no chaining) on the 
00248  * prefix code / next character combination.  We do a variant of Knuth's
00249  * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
00250  * secondary probe.  Here, the modular division first probe is gives way
00251  * to a faster exclusive-or manipulation.  Also do block compression with
00252  * an adaptive reset, whereby the code table is cleared when the compression
00253  * ratio decreases, but after the table fills.  The variable-length output
00254  * codes are re-sized at this point, and a special CLEAR code is generated
00255  * for the decompressor.  Late addition:  construct the table according to
00256  * file size for noticeable speed improvement on small files.  Please direct
00257  * questions about this implementation to ames!jaw.
00258  */
00259 
00260 static int g_init_bits;
00261 static FILE *g_outfile;
00262 
00263 static int ClearCode;
00264 static int EOFCode;
00265 
00266 
00267 /********************************************************/
00268 static void compress(int init_bits, FILE *outfile, byte *data, int len)
00269 {
00270   register long fcode;
00271   register int i = 0;
00272   register int c;
00273   register int ent;
00274   register int disp;
00275   register int hsize_reg;
00276   register int hshift;
00277 
00278   /*
00279    * Set up the globals:  g_init_bits - initial number of bits
00280    *                      g_outfile   - pointer to output file
00281    */
00282   g_init_bits = init_bits;
00283   g_outfile   = outfile;
00284 
00285   /* initialize 'compress' globals */
00286   maxbits = GIFBITS;
00287   maxmaxcode = 1<<GIFBITS;
00288   memset((char *) htab, 0, sizeof(htab));
00289   memset((char *) codetab, 0, sizeof(codetab));
00290   hsize = HSIZE;
00291   free_ent = 0;
00292   clear_flg = 0;
00293   in_count = 1;
00294   out_count = 0;
00295   cur_accum = 0;
00296   cur_bits = 0;
00297 
00298 
00299   /*
00300    * Set up the necessary values
00301    */
00302   out_count = 0;
00303   clear_flg = 0;
00304   in_count = 1;
00305   maxcode = MAXCODE(n_bits = g_init_bits);
00306 
00307   ClearCode = (1 << (init_bits - 1));
00308   EOFCode = ClearCode + 1;
00309   free_ent = ClearCode + 2;
00310 
00311   char_init();
00312   ent = *data++;  len--;
00313 
00314   hshift = 0;
00315   for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L ) {
00316     hshift++;
00317   }
00318   hshift = 8 - hshift;                /* set hash code range bound */
00319 
00320   hsize_reg = hsize;
00321   cl_hash( (count_int) hsize_reg);            /* clear hash table */
00322 
00323   output(ClearCode);
00324     
00325   while (len) {
00326     c = *data++;  len--;
00327     in_count++;
00328 
00329     fcode = (long) ( ( (long) c << maxbits) + ent);
00330     i = (((int) c << hshift) ^ ent);    /* xor hashing */
00331 
00332     if ( HashTabOf (i) == fcode ) {
00333       ent = CodeTabOf (i);
00334       continue;
00335     }
00336 
00337     else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
00338       goto nomatch;
00339 
00340     disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
00341     if ( i == 0 )
00342       disp = 1;
00343 
00344 probe:
00345     if ( (i -= disp) < 0 )
00346       i += hsize_reg;
00347 
00348     if ( HashTabOf (i) == fcode ) {
00349       ent = CodeTabOf (i);
00350       continue;
00351     }
00352 
00353     if ( (long)HashTabOf (i) > 0 ) 
00354       goto probe;
00355 
00356 nomatch:
00357     output(ent);
00358     out_count++;
00359     ent = c;
00360 
00361     if ( free_ent < maxmaxcode ) {
00362       CodeTabOf (i) = free_ent++; /* code -> hashtable */
00363       HashTabOf (i) = fcode;
00364     }
00365     else
00366       cl_block();
00367   }
00368 
00369   /* Put out the final code */
00370   output(ent);
00371   out_count++;
00372   output(EOFCode);
00373 }
00374 
00375 
00376 /*****************************************************************
00377  * TAG( output )
00378  *
00379  * Output the given code.
00380  * Inputs:
00381  *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
00382  *              that n_bits =< (long)wordsize - 1.
00383  * Outputs:
00384  *      Outputs code to the file.
00385  * Assumptions:
00386  *      Chars are 8 bits long.
00387  * Algorithm:
00388  *      Maintain a BITS character long buffer (so that 8 codes will
00389  * fit in it exactly).  Use the VAX insv instruction to insert each
00390  * code in turn.  When the buffer fills up empty it and start over.
00391  */
00392 
00393 static
00394 unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
00395                                   0x001F, 0x003F, 0x007F, 0x00FF,
00396                                   0x01FF, 0x03FF, 0x07FF, 0x0FFF,
00397                                   0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
00398 
00399 static void output(int code)
00400 {
00401   cur_accum &= masks[cur_bits];
00402 
00403   if (cur_bits > 0)
00404     cur_accum |= ((long)code << cur_bits);
00405   else
00406     cur_accum = code;
00407        
00408   cur_bits += n_bits;
00409 
00410   while( cur_bits >= 8 ) {
00411     char_out( (unsigned int) (cur_accum & 0xff) );
00412     cur_accum >>= 8;
00413     cur_bits -= 8;
00414   }
00415 
00416   /*
00417    * If the next entry is going to be too big for the code size,
00418    * then increase it, if possible.
00419    */
00420 
00421   if (free_ent > maxcode || clear_flg) {
00422 
00423     if( clear_flg ) {
00424       maxcode = MAXCODE (n_bits = g_init_bits);
00425       clear_flg = 0;
00426     }
00427     else {
00428       n_bits++;
00429       if ( n_bits == maxbits )
00430        maxcode = maxmaxcode;
00431       else
00432        maxcode = MAXCODE(n_bits);
00433     }
00434   }
00435        
00436   if( code == EOFCode ) {
00437     /* At EOF, write the rest of the buffer */
00438     while( cur_bits > 0 ) {
00439       char_out( (unsigned int)(cur_accum & 0xff) );
00440       cur_accum >>= 8;
00441       cur_bits -= 8;
00442     }
00443 
00444     flush_char();
00445        
00446     fflush( g_outfile );
00447 
00448     if( ferror( g_outfile ) )
00449     {
00450       fprintf(stderr, "Unable to write GIF file\n");
00451       exit(1);
00452     }
00453   }
00454 }
00455 
00456 
00457 /********************************/
00458 static void cl_block ()             /* table clear for block compress */
00459 {
00460   /* Clear out the hash table */
00461 
00462   cl_hash ( (count_int) hsize );
00463   free_ent = ClearCode + 2;
00464   clear_flg = 1;
00465 
00466   output(ClearCode);
00467 }
00468 
00469 
00470 /********************************/
00471 static void cl_hash(register count_int hsize)          /* reset code table */
00472 {
00473   register count_int *htab_p = htab+hsize;
00474   register long i;
00475   register long m1 = -1;
00476 
00477   i = hsize - 16;
00478   do {                            /* might use Sys V memset(3) here */
00479     *(htab_p-16) = m1;
00480     *(htab_p-15) = m1;
00481     *(htab_p-14) = m1;
00482     *(htab_p-13) = m1;
00483     *(htab_p-12) = m1;
00484     *(htab_p-11) = m1;
00485     *(htab_p-10) = m1;
00486     *(htab_p-9) = m1;
00487     *(htab_p-8) = m1;
00488     *(htab_p-7) = m1;
00489     *(htab_p-6) = m1;
00490     *(htab_p-5) = m1;
00491     *(htab_p-4) = m1;
00492     *(htab_p-3) = m1;
00493     *(htab_p-2) = m1;
00494     *(htab_p-1) = m1;
00495     htab_p -= 16;
00496   } while ((i -= 16) >= 0);
00497 
00498   for ( i += 16; i > 0; i-- ) {
00499     *--htab_p = m1;
00500   }
00501 }
00502 
00503 
00504 /******************************************************************************
00505  *
00506  * GIF Specific routines
00507  *
00508  ******************************************************************************/
00509 
00510 /*
00511  * Number of characters so far in this 'packet'
00512  */
00513 static int a_count;
00514 
00515 /*
00516  * Set up the 'byte output' routine
00517  */
00518 static void char_init()
00519 {
00520        a_count = 0;
00521 }
00522 
00523 /*
00524  * Define the storage for the packet accumulator
00525  */
00526 static char accum[ 256 ];
00527 
00528 /*
00529  * Add a character to the end of the current packet, and if it is 254
00530  * characters, flush the packet to disk.
00531  */
00532 static void char_out(int c)
00533 {
00534   accum[ a_count++ ] = c;
00535   if( a_count >= 254 ) 
00536     flush_char();
00537 }
00538 
00539 /*
00540  * Flush the packet to disk, and reset the accumulator
00541  */
00542 static void flush_char()
00543 {
00544   if( a_count > 0 ) {
00545     fputc( a_count, g_outfile );
00546     fwrite( accum, 1, a_count, g_outfile );
00547     a_count = 0;
00548   }
00549 }