--- ray/src/px/ra_gif.c 1994/05/20 11:44:19 2.3 +++ ray/src/px/ra_gif.c 2004/03/28 20:33:14 2.11 @@ -1,26 +1,20 @@ -/* Copyright (c) 1994 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ra_gif.c,v 2.11 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * Convert from Radiance picture file to Compuserve GIF. * Currently, we don't know how to get back. */ #include +#include +#include +#include "platform.h" #include "color.h" - #include "resolu.h" +#include "clrtab.h" -#ifdef MSDOS -#include -#endif - -#include - #define MAXCOLORS 256 int rmap[MAXCOLORS]; @@ -28,45 +22,46 @@ int gmap[MAXCOLORS]; int bmap[MAXCOLORS]; int currow; - -extern long ftell(); - long picstart; +BYTE clrtab[256][3]; -extern BYTE clrtab[][3]; +extern int samplefac; -extern int getgifpix(); COLR *scanln; BYTE *pixscan; int xmax, ymax; /* picture size */ - double gamv = 2.2; /* gamma correction */ - int greyscale = 0; /* convert to B&W? */ - int dither = 1; /* dither colors? */ - int bradj = 0; /* brightness adjustment */ +int ncolors = 0; /* number of colors requested */ char *progname; +typedef int ifun_t(int x, int y); /* required by the GIF code below */ -main(argc, argv) -int argc; -char *argv[]; +static void getrow(int y); +static void mkclrmap(int nc); +static void mkgrymap(int nc); +static ifun_t getgifpix; + +static void GIFEncode(FILE *fp, int GWidth, int GHeight, int GInterlace, + int Background, int BitsPerPixel, int Red[], int Green[], int Blue[], + ifun_t* GetPixel); + + +int +main(int argc, char *argv[]) { - int ncolors = 0; int bitsperpix; int i; -#ifdef MSDOS - extern int _fmode; - _fmode = O_BINARY; - setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); -#endif + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); progname = argv[0]; + samplefac = 0; for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -88,6 +83,9 @@ char *argv[]; goto userr; bradj = atoi(argv[++i]); break; + case 'n': + samplefac = atoi(argv[++i]); + break; default: goto userr; } @@ -121,7 +119,7 @@ char *argv[]; /* set up gamma correction */ setcolrgam(gamv); /* figure out the bits per pixel */ - if (ncolors < 2 | ncolors > MAXCOLORS) + if ((ncolors < 2) | (ncolors > MAXCOLORS)) ncolors = MAXCOLORS; for (bitsperpix = 1; ncolors > 1<>8); if (pixscan != NULL) return(pixscan[x]); - return(map_pixel(scanln[x])); + return(samplefac ? neu_map_pixel(scanln[x]) : map_pixel(scanln[x])); } @@ -221,7 +238,7 @@ int x, y; * SCARY GIF code follows . . . . sorry. * * Based on GIFENCOD by David Rowley .A - * Lempel-Zim compression based on "compress". + * Lempel-Ziv compression based on "compress". * */ @@ -233,7 +250,6 @@ int x, y; * BitsPerPixel, Red, Green, Blue, GetPixel ) * *****************************************************************************/ -typedef int (* ifunptr)(); #define TRUE 1 #define FALSE 0 @@ -246,10 +262,33 @@ int Interlace; unsigned long cur_accum = 0; int cur_bits = 0; +static void BumpPixel(void); +static int GIFNextPixel(ifun_t* getpixel); +static void Putword(int w, FILE *fp); +static void compress(int init_bits, FILE *outfile, ifun_t* ReadValue); + + +/* a code_int must be able to hold 2**CBITS values of type int, and also -1 */ +typedef int code_int; +typedef long int count_int; +typedef unsigned char char_type; + +static void output(code_int code); +static void cl_block(void); +static void cl_hash(count_int hsize); +static void writeerr(void); + +static void char_init(void); +static void char_out(int c); +static void flush_char(void); +static void gammawarp(short *sbuf, float gam, int n); + + /* * Bump the 'curx' and 'cury' to point to the next pixel */ -BumpPixel() +static void +BumpPixel(void) { curx++; if( curx == Width ) { @@ -290,8 +329,10 @@ BumpPixel() /* * Return the next pixel from the image */ -GIFNextPixel( getpixel ) -ifunptr getpixel; +static int +GIFNextPixel( + ifun_t* getpixel +) { int r; @@ -306,15 +347,19 @@ ifunptr getpixel; /* * public GIFEncode */ -GIFEncode( fp, GWidth, GHeight, GInterlace, Background, - BitsPerPixel, Red, Green, Blue, GetPixel ) -FILE *fp; -int GWidth, GHeight; -int GInterlace; -int Background; -int BitsPerPixel; -int Red[], Green[], Blue[]; -ifunptr GetPixel; +static void +GIFEncode( + FILE *fp, + int GWidth, + int GHeight, + int GInterlace, + int Background, + int BitsPerPixel, + int Red[], + int Green[], + int Blue[], + ifun_t* GetPixel +) { int B; int RWidth, RHeight; @@ -324,7 +369,7 @@ ifunptr GetPixel; int InitCodeSize; int i; - long cur_accum = 0; + cur_accum = 0; /* globals */ cur_bits = 0; Interlace = GInterlace; @@ -374,9 +419,11 @@ ifunptr GetPixel; /* * Write out a word to the GIF file */ -Putword( w, fp ) -int w; -FILE *fp; +static void +Putword( + int w, + FILE *fp +) { fputc( w & 0xff, fp ); fputc( (w/256) & 0xff, fp ); @@ -396,13 +443,6 @@ FILE *fp; #define HSIZE 5003 /* 80% occupancy */ /* - * a code_int must be able to hold 2**CBITS values of type int, and also -1 - */ -typedef int code_int; -typedef long int count_int; -typedef unsigned char char_type; - -/* * * GIF Image compression - modified 'compress' * @@ -477,10 +517,12 @@ FILE *g_outfile; int ClearCode; int EOFCode; -compress( init_bits, outfile, ReadValue ) -int init_bits; -FILE *outfile; -ifunptr ReadValue; +static void +compress( + int init_bits, + FILE *outfile, + ifun_t* ReadValue +) { register long fcode; register code_int i = 0; @@ -519,7 +561,7 @@ ifunptr ReadValue; while ( (c = GIFNextPixel( ReadValue )) != EOF ) { in_count++; fcode = (long) (((long) c << maxbits) + ent); - /* i = (((code_int)c << hshift) ~ ent); /* xor hashing */ + /* i = (((code_int)c << hshift) ~ ent); */ /* xor hashing */ i = (((code_int)c << hshift) ^ ent); /* xor hashing */ if ( HashTabOf (i) == fcode ) { ent = CodeTabOf (i); @@ -579,8 +621,10 @@ unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x00 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; -output( code ) -code_int code; +static void +output( + code_int code +) { cur_accum &= masks[ cur_bits ]; if( cur_bits > 0 ) @@ -629,7 +673,8 @@ code_int code; /* * Clear out the hash table */ -cl_block () /* table clear for block compress */ +static void +cl_block (void) /* table clear for block compress */ { cl_hash ( (count_int) hsize ); free_ent = ClearCode + 2; @@ -637,8 +682,10 @@ cl_block () /* table clear for block compr output( (code_int)ClearCode ); } -cl_hash(hsize) /* reset code table */ -register count_int hsize; +static void +cl_hash( /* reset code table */ + register count_int hsize +) { register count_int *htab_p = htab+hsize; register long i; @@ -668,7 +715,8 @@ register count_int hsize; *--htab_p = m1; } -writeerr() +static void +writeerr(void) { printf( "error writing output file\n" ); exit(1); @@ -688,7 +736,8 @@ int a_count; /* * Set up the 'byte output' routine */ -char_init() +static void +char_init(void) { a_count = 0; } @@ -702,8 +751,10 @@ char accum[256]; * Add a character to the end of the current packet, and if it is 254 * characters, flush the packet to disk. */ -char_out( c ) -int c; +static void +char_out( + int c +) { accum[ a_count++ ] = c; if( a_count >= 254 ) @@ -713,7 +764,8 @@ int c; /* * Flush the packet to disk, and reset the accumulator */ -flush_char() +static void +flush_char(void) { if( a_count > 0 ) { fputc( a_count, g_outfile ); @@ -725,13 +777,14 @@ flush_char() static float curgamma; static short gamtab[256]; -gammawarp(sbuf,gam,n) -short *sbuf; -float gam; -int n; +static void +gammawarp( + short *sbuf, + float gam, + int n +) { int i; - float f; if(gam!=curgamma) { for(i=0; i<256; i++)