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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines