ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_gif.c
(Generate patch)

Comparing ray/src/px/ra_gif.c (file contents):
Revision 2.2 by greg, Tue May 3 10:06:32 1994 UTC vs.
Revision 2.12 by greg, Fri May 20 02:06:39 2011 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert from Radiance picture file to Compuserve GIF.
6   * Currently, we don't know how to get back.
7   */
8  
9   #include  <stdio.h>
10 + #include  <time.h>
11 + #include  <math.h>
12  
13 + #include  "platform.h"
14   #include  "color.h"
15
15   #include  "resolu.h"
16 + #include  "clrtab.h"
17  
18 #ifdef MSDOS
19 #include  <fcntl.h>
20 #endif
21
22 #include  <math.h>
23
18   #define MAXCOLORS               256
19  
20   int rmap[MAXCOLORS];
# Line 28 | Line 22 | int gmap[MAXCOLORS];
22   int bmap[MAXCOLORS];
23  
24   int currow;
31
32 extern long  ftell();
33
25   long  picstart;
26 + uby8  clrtab[256][3];
27  
28 < extern BYTE  clrtab[][3];
28 > extern int  samplefac;
29  
38 extern int  getgifpix();
30  
31   COLR    *scanln;
32 < BYTE    *pixscan;
32 > uby8    *pixscan;
33  
34   int  xmax, ymax;                        /* picture size */
44
35   double  gamv = 2.2;                     /* gamma correction */
46
36   int  greyscale = 0;                     /* convert to B&W? */
48
37   int  dither = 1;                        /* dither colors? */
50
38   int  bradj = 0;                         /* brightness adjustment */
39 + int  ncolors = 0;                       /* number of colors requested */
40  
41   char  *progname;
42  
43 + typedef int ifun_t(int x, int y); /* required by the GIF code below */
44  
45 < main(argc, argv)
46 < int  argc;
47 < char  *argv[];
45 > static void getrow(int  y);
46 > static void mkclrmap(int        nc);
47 > static void mkgrymap(int        nc);
48 > static ifun_t getgifpix;
49 >
50 > static void GIFEncode(FILE *fp, int GWidth, int GHeight, int GInterlace,
51 >                int Background, int BitsPerPixel, int Red[], int Green[], int Blue[],
52 >                ifun_t* GetPixel);
53 >
54 >
55 > int
56 > main(int  argc, char  *argv[])
57   {
60        int  ncolors = 0;
58          int  bitsperpix;
59          int  i;
60 < #ifdef MSDOS
61 <        extern int  _fmode;
62 <        _fmode = O_BINARY;
66 <        setmode(fileno(stdin), O_BINARY);
67 <        setmode(fileno(stdout), O_BINARY);
68 < #endif
60 >        SET_DEFAULT_BINARY();
61 >        SET_FILE_BINARY(stdin);
62 >        SET_FILE_BINARY(stdout);
63          progname = argv[0];
64 +        samplefac = 0;
65  
66          for (i = 1; i < argc; i++)
67                  if (argv[i][0] == '-')
# Line 88 | Line 83 | char  *argv[];
83                                          goto userr;
84                                  bradj = atoi(argv[++i]);
85                                  break;
86 +                        case 'n':
87 +                                samplefac = atoi(argv[++i]);
88 +                                break;
89                          default:
90                                  goto userr;
91                          }
# Line 121 | Line 119 | char  *argv[];
119                                  /* set up gamma correction */
120          setcolrgam(gamv);
121                                  /* figure out the bits per pixel */
122 <        if (ncolors < 2 | ncolors > MAXCOLORS)
122 >        if ((ncolors < 2) | (ncolors > MAXCOLORS))
123                  ncolors = MAXCOLORS;
124          for (bitsperpix = 1; ncolors > 1<<bitsperpix; bitsperpix++)
125                  ;
# Line 136 | Line 134 | char  *argv[];
134          exit(0);
135   userr:
136          fprintf(stderr,
137 <        "Usage: %s [-b][-c ncolors][-g gamv][-e +/-stops] input [output]\n",
137 >        "Usage: %s [-b][-d][-n samp][-c ncolors][-g gamv][-e +/-stops] input [output]\n",
138                          progname);
139          exit(1);
140   }
141  
142  
143 < getrow(y)                       /* get the specified row from our image */
144 < int  y;
143 > static void
144 > getrow(                 /* get the specified row from our image */
145 >        int  y
146 > )
147   {
148          if (y == currow)
149                  return;
# Line 155 | Line 155 | int  y;
155                  if (freadcolrs(scanln, xmax, stdin) < 0) {
156                          fprintf(stderr, "%s: error reading picture (y==%d)\n",
157                                          progname, ymax-1-y);
158 +                        exit(1);
159                  }
160          while (++currow < y);
161          if (bradj)
162                  shiftcolrs(scanln, xmax, bradj);
163          colrs_gambs(scanln, xmax);
164 <        if (pixscan != NULL)
165 <                dith_colrs(pixscan, scanln, xmax);
164 >        if (pixscan != NULL) {
165 >                if (samplefac)
166 >                        neu_dith_colrs(pixscan, scanln, xmax);
167 >                else
168 >                        dith_colrs(pixscan, scanln, xmax);
169 >        }
170   }
171  
172  
173 < mkclrmap(nc)                    /* make our color map */
174 < int     nc;
173 > static void
174 > mkclrmap(                       /* make our color map */
175 >        int     nc
176 > )
177   {
178          register int    i;
179  
180 <        new_histo();
180 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
181 >                goto memerr;
182          for (i = 0; i < ymax; i++) {
183                  getrow(i);
184 <                cnt_colrs(scanln, xmax);
184 >                if (samplefac)
185 >                        neu_colrs(scanln, xmax);
186 >                else
187 >                        cnt_colrs(scanln, xmax);
188          }
189 <        new_clrtab(nc);
189 >        if (samplefac)
190 >                neu_clrtab(nc);
191 >        else
192 >                new_clrtab(nc);
193          for (i = 0; i < nc; i++) {
194                  rmap[i] = clrtab[i][RED];
195                  gmap[i] = clrtab[i][GRN];
196                  bmap[i] = clrtab[i][BLU];
197          }
198 <        if (dither && (pixscan = (BYTE *)malloc(xmax)) == NULL) {
199 <                fprintf(stderr, "%s: out of memory\n", progname);
200 <                exit(1);
201 <        }
198 >        if (dither && (pixscan = (uby8 *)malloc(xmax)) == NULL)
199 >                goto memerr;
200 >        return;
201 > memerr:
202 >        fprintf(stderr, "%s: out of memory\n", progname);
203 >        exit(1);
204   }
205  
206  
207 < mkgrymap(nc)
208 < int     nc;
207 > static void
208 > mkgrymap(
209 >        int     nc
210 > )
211   {
212          register int    i;
213  
# Line 201 | Line 219 | int    nc;
219   }
220  
221  
222 < int
223 < getgifpix(x, y)                 /* get a single pixel from our picture */
224 < int  x, y;
222 > static int
223 > getgifpix(                      /* get a single pixel from our picture */
224 >        int  x,
225 >        int  y
226 > )
227   {
208        int pix;
209
228          getrow(y);
229          if (greyscale)
230 <                return(normbright(scanln[x]));
230 >                return((normbright(scanln[x])*ncolors)>>8);
231          if (pixscan != NULL)
232                  return(pixscan[x]);
233 <        return(map_pixel(scanln[x]));
233 >        return(samplefac ? neu_map_pixel(scanln[x]) : map_pixel(scanln[x]));
234   }
235  
236  
# Line 220 | Line 238 | int  x, y;
238   * SCARY GIF code follows . . . . sorry.
239   *
240   * Based on GIFENCOD by David Rowley <[email protected]>.A
241 < * Lempel-Zim compression based on "compress".
241 > * Lempel-Ziv compression based on "compress".
242   *
243   */
244  
# Line 232 | Line 250 | int  x, y;
250   *            BitsPerPixel, Red, Green, Blue, GetPixel )
251   *
252   *****************************************************************************/
235 typedef int (* ifunptr)();
253  
254   #define TRUE 1
255   #define FALSE 0
# Line 245 | Line 262 | int Interlace;
262   unsigned long cur_accum = 0;
263   int cur_bits = 0;
264  
265 + static void BumpPixel(void);
266 + static int GIFNextPixel(ifun_t* getpixel);
267 + static void Putword(int w, FILE *fp);
268 + static void compress(int init_bits, FILE *outfile, ifun_t* ReadValue);
269 +
270 +
271 + /* a code_int must be able to hold 2**CBITS values of type int, and also -1 */
272 + typedef int             code_int;
273 + typedef long int        count_int;
274 + typedef unsigned char   char_type;
275 +
276 + static void output(code_int  code);
277 + static void cl_block(void);
278 + static void cl_hash(count_int hsize);
279 + static void writeerr(void);
280 +
281 + static void char_init(void);
282 + static void char_out(int c);
283 + static void flush_char(void);
284 + static void gammawarp(short *sbuf, float gam, int n);
285 +
286 +
287   /*
288   * Bump the 'curx' and 'cury' to point to the next pixel
289   */
290 < BumpPixel()
290 > static void
291 > BumpPixel(void)
292   {
293      curx++;
294      if( curx == Width ) {
# Line 289 | Line 329 | BumpPixel()
329   /*
330   * Return the next pixel from the image
331   */
332 < GIFNextPixel( getpixel )
333 < ifunptr getpixel;
332 > static int
333 > GIFNextPixel(
334 >        ifun_t* getpixel
335 > )
336   {
337      int r;
338  
# Line 305 | Line 347 | ifunptr getpixel;
347   /*
348   * public GIFEncode
349   */
350 < GIFEncode( fp, GWidth, GHeight, GInterlace, Background,
351 <           BitsPerPixel, Red, Green, Blue, GetPixel )
352 < FILE *fp;
353 < int GWidth, GHeight;
354 < int GInterlace;
355 < int Background;
356 < int BitsPerPixel;
357 < int Red[], Green[], Blue[];
358 < ifunptr GetPixel;
350 > static void
351 > GIFEncode(
352 >        FILE *fp,
353 >        int GWidth,
354 >        int GHeight,
355 >        int GInterlace,
356 >        int Background,
357 >        int BitsPerPixel,
358 >        int Red[],
359 >        int Green[],
360 >        int Blue[],
361 >        ifun_t* GetPixel
362 > )
363   {
364      int B;
365      int RWidth, RHeight;
# Line 323 | Line 369 | ifunptr GetPixel;
369      int InitCodeSize;
370      int i;
371  
372 <    long cur_accum = 0;
372 >    cur_accum = 0; /* globals */
373      cur_bits = 0;
374  
375      Interlace = GInterlace;
# Line 373 | Line 419 | ifunptr GetPixel;
419   /*
420   * Write out a word to the GIF file
421   */
422 < Putword( w, fp )
423 < int w;
424 < FILE *fp;
422 > static void
423 > Putword(
424 >        int w,
425 >        FILE *fp
426 > )
427   {
428      fputc( w & 0xff, fp );
429      fputc( (w/256) & 0xff, fp );
# Line 395 | Line 443 | FILE *fp;
443   #define HSIZE  5003            /* 80% occupancy */
444  
445   /*
398 * a code_int must be able to hold 2**CBITS values of type int, and also -1
399 */
400 typedef int             code_int;
401 typedef long int        count_int;
402 typedef unsigned char   char_type;
403
404 /*
446   *
447   * GIF Image compression - modified 'compress'
448   *
# Line 476 | Line 517 | FILE *g_outfile;
517   int ClearCode;
518   int EOFCode;
519  
520 < compress( init_bits, outfile, ReadValue )
521 < int init_bits;
522 < FILE *outfile;
523 < ifunptr ReadValue;
520 > static void
521 > compress(
522 >        int init_bits,
523 >        FILE *outfile,
524 >        ifun_t* ReadValue
525 > )
526   {
527      register long fcode;
528      register code_int i = 0;
# Line 518 | Line 561 | ifunptr ReadValue;
561      while ( (c = GIFNextPixel( ReadValue )) != EOF ) {
562          in_count++;
563          fcode = (long) (((long) c << maxbits) + ent);
564 <        /* i = (((code_int)c << hshift) ~ ent);    /* xor hashing */
564 >        /* i = (((code_int)c << hshift) ~ ent); */   /* xor hashing */
565          i = (((code_int)c << hshift) ^ ent);    /* xor hashing */
566          if ( HashTabOf (i) == fcode ) {
567              ent = CodeTabOf (i);
# Line 578 | Line 621 | unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x00
621                                    0x01FF, 0x03FF, 0x07FF, 0x0FFF,
622                                    0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
623  
624 < output( code )
625 < code_int  code;
624 > static void
625 > output(
626 >        code_int  code
627 > )
628   {
629      cur_accum &= masks[ cur_bits ];
630      if( cur_bits > 0 )
# Line 628 | Line 673 | code_int  code;
673   /*
674   * Clear out the hash table
675   */
676 < cl_block ()             /* table clear for block compress */
676 > static void
677 > cl_block (void)             /* table clear for block compress */
678   {
679          cl_hash ( (count_int) hsize );
680          free_ent = ClearCode + 2;
# Line 636 | Line 682 | cl_block ()             /* table clear for block compr
682          output( (code_int)ClearCode );
683   }
684  
685 < cl_hash(hsize)          /* reset code table */
686 < register count_int hsize;
685 > static void
686 > cl_hash(          /* reset code table */
687 >        register count_int hsize
688 > )
689   {
690          register count_int *htab_p = htab+hsize;
691          register long i;
# Line 667 | Line 715 | register count_int hsize;
715                  *--htab_p = m1;
716   }
717  
718 < writeerr()
718 > static void
719 > writeerr(void)
720   {
721          printf( "error writing output file\n" );
722          exit(1);
# Line 687 | Line 736 | int a_count;
736   /*
737   * Set up the 'byte output' routine
738   */
739 < char_init()
739 > static void
740 > char_init(void)
741   {
742          a_count = 0;
743   }
# Line 701 | Line 751 | char accum[256];
751   * Add a character to the end of the current packet, and if it is 254
752   * characters, flush the packet to disk.
753   */
754 < char_out( c )
755 < int c;
754 > static void
755 > char_out(
756 >        int c
757 > )
758   {
759          accum[ a_count++ ] = c;
760          if( a_count >= 254 )
# Line 712 | Line 764 | int c;
764   /*
765   * Flush the packet to disk, and reset the accumulator
766   */
767 < flush_char()
767 > static void
768 > flush_char(void)
769   {
770          if( a_count > 0 ) {
771                  fputc( a_count, g_outfile );
# Line 724 | Line 777 | flush_char()
777   static float curgamma;
778   static short gamtab[256];
779  
780 < gammawarp(sbuf,gam,n)
781 < short *sbuf;
782 < float gam;
783 < int n;
780 > static void
781 > gammawarp(
782 >        short *sbuf,
783 >        float gam,
784 >        int n
785 > )
786   {
787      int i;
733    float f;
788  
789      if(gam!=curgamma) {
790          for(i=0; i<256; i++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines