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

Comparing ray/src/px/ra_t8.c (file contents):
Revision 1.12 by greg, Mon Nov 11 14:01:53 1991 UTC vs.
Revision 2.12 by schorsch, Thu Jun 5 19:29:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *  ra_t8.c - program to convert between RADIANCE and
6   *              Targa 8-bit color-mapped images.
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include  <stdio.h>
12 + #include  <time.h>
13 + #include  <math.h>
14  
15 + #include  "platform.h"
16   #include  "color.h"
17
17   #include  "resolu.h"
19
20 #include  "pic.h"
21
18   #include  "targa.h"
19  
24 #ifndef  BSD
25 #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
26 extern char  *memcpy();
27 #endif
28                        /* descriptor for a picture file or frame buffer */
29 typedef struct {
30        char    *name;                  /* file name */
31        FILE    *fp;                    /* file pointer */
32        int     nexty;                  /* file positioning */
33        int     bytes_line;             /* 0 == variable length lines */
34        union {
35                long    b;                      /* initial scanline */
36                long    *y;                     /* individual scanline */
37        } pos;                          /* position(s) */
38 } pic;
20  
21 < #define  goodpic(h)     (my_imType(h) && my_mapType(h))
22 < #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
21 > #define  goodpic(h)     (my_imType(h) && my_mapType(h))
22 > #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
23                                  && (h)->dataBits==8 && (h)->imType==0)
24 < #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
24 > #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
25                                  ((h)->CMapBits==24 || (h)->CMapBits==32))
26  
27 < #define  taralloc(h)    (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel))
27 > #define  taralloc(h)    (BYTE *)emalloc((h)->x*(h)->y)
28  
29 < extern pic      *openinput();
29 > BYTE  clrtab[256][3];
30  
31 + extern int      samplefac;
32 +
33   extern char     *ecalloc(), *emalloc();
34  
35   extern long  ftell();
36  
37 < extern double  atof(), pow();
37 > double  gamv = 2.2;                     /* gamv correction */
38  
56 double  gamma = 2.2;                    /* gamma correction */
57
39   int  bradj = 0;                         /* brightness adjustment */
40  
60 pic     *inpic;
61
41   char  *progname;
42  
43   char  errmsg[128];
44  
45   COLR    *inl;
46  
47 < pixel   *tarData;
47 > BYTE    *tarData;
48  
49   int  xmax, ymax;
50  
# Line 74 | Line 53 | main(argc, argv)
53   int  argc;
54   char  *argv[];
55   {
56 <        colormap  rasmap;
78 <        struct hdStruct  head;
56 >        struct hdStruct  head;
57          int  dither = 1;
58          int  reverse = 0;
59          int  ncolors = 256;
60          int  greyscale = 0;
61          int  i;
62 <        
62 >        SET_DEFAULT_BINARY();
63 >        SET_FILE_BINARY(stdin);
64 >        SET_FILE_BINARY(stdout);
65          progname = argv[0];
66 +        samplefac = 0;
67  
68          for (i = 1; i < argc; i++)
69                  if (argv[i][0] == '-')
# Line 91 | Line 72 | char  *argv[];
72                                  dither = !dither;
73                                  break;
74                          case 'g':
75 <                                gamma = atof(argv[++i]);
75 >                                gamv = atof(argv[++i]);
76                                  break;
77                          case 'r':
78                                  reverse = !reverse;
# Line 107 | Line 88 | char  *argv[];
88                          case 'c':
89                                  ncolors = atoi(argv[++i]);
90                                  break;
91 +                        case 'n':
92 +                                samplefac = atoi(argv[++i]);
93 +                                break;
94                          default:
95                                  goto userr;
96                          }
97                  else
98                          break;
99  
100 +        if (i < argc-2)
101 +                goto userr;
102 +        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
103 +                sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
104 +                quiterr(errmsg);
105 +        }
106 +        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
107 +                sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
108 +                quiterr(errmsg);
109 +        }
110          if (reverse) {
117                if (i < argc-2)
118                        goto userr;
119                if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
120                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
121                        quiterr(errmsg);
122                }
111                                          /* get header */
112                  if (getthead(&head, NULL, stdin) < 0)
113                          quiterr("bad targa file");
# Line 127 | Line 115 | char  *argv[];
115                          quiterr("incompatible format");
116                  xmax = head.x;
117                  ymax = head.y;
130                                        /* open output file */
131                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
132                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
133                        quiterr(errmsg);
134                }
118                                          /* put header */
119 +                newheader("RADIANCE", stdout);
120                  printargs(i, argv, stdout);
121                  fputformat(COLRFMT, stdout);
122                  putchar('\n');
# Line 140 | Line 124 | char  *argv[];
124                                          /* convert file */
125                  tg2ra(&head);
126          } else {
127 <                if (i < argc-2 || (!greyscale && i > argc-1))
128 <                        goto userr;
145 <                if ((inpic = openinput(argv[i], &head)) == NULL) {
146 <                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
147 <                        quiterr(errmsg);
148 <                }
149 <                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
150 <                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
151 <                        quiterr(errmsg);
152 <                }
127 >                if (getrhead(&head, stdin) < 0)
128 >                        quiterr("bad Radiance input");
129                                          /* write header */
130                  putthead(&head, NULL, stdout);
131                                          /* convert file */
132                  if (greyscale)
133 <                        biq(dither,ncolors,1,rasmap);
133 >                        getgrey(ncolors);
134                  else
135 <                        ciq(dither,ncolors,1,rasmap);
135 >                        getmapped(ncolors, dither);
136                                          /* write data */
137                  writetarga(&head, tarData, stdout);
138          }
139          quiterr(NULL);
140   userr:
141          fprintf(stderr,
142 <        "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n",
142 >        "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
143                          progname);
144 <        fprintf(stderr, "   Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n",
144 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
145                          progname);
146          exit(1);
147   }
# Line 204 | Line 180 | char  *err;
180   }
181  
182  
183 + void
184   eputs(s)
185   char *s;
186   {
# Line 211 | Line 188 | char *s;
188   }
189  
190  
191 + void
192   quit(code)
193   int code;
194   {
# Line 219 | Line 197 | int code;
197  
198  
199   getthead(hp, ip, fp)            /* read header from input */
200 < struct hdStruct  *hp;
200 > struct hdStruct  *hp;
201   char  *ip;
202   register FILE  *fp;
203   {
# Line 252 | Line 230 | register FILE  *fp;
230  
231  
232   putthead(hp, ip, fp)            /* write header to output */
233 < struct hdStruct  *hp;
233 > struct hdStruct  *hp;
234   char  *ip;
235   register FILE  *fp;
236   {
# Line 279 | Line 257 | register FILE  *fp;
257   }
258  
259  
260 < pic *
283 < openinput(fname, h)             /* open RADIANCE input file */
284 < char  *fname;
260 > getrhead(h, fp)                 /* load RADIANCE input file header */
261   register struct hdStruct  *h;
262 + FILE  *fp;
263   {
287        register pic  *p;
288
289        p = (pic *)emalloc(sizeof(pic));
290        p->name = fname;
291        if (fname == NULL)
292                p->fp = stdin;
293        else if ((p->fp = fopen(fname, "r")) == NULL)
294                return(NULL);
264                                          /* get header info. */
265 <        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
266 <                        fgetresolu(&xmax, &ymax, p->fp) < 0)
267 <                quiterr("bad picture format");
299 <        p->nexty = 0;
300 <        p->bytes_line = 0;              /* variable length lines */
301 <        p->pos.y = (long *)ecalloc(ymax, sizeof(long));
302 <        p->pos.y[0] = ftell(p->fp);
265 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
266 >                        fgetresolu(&xmax, &ymax, fp) < 0)
267 >                return(-1);
268                                          /* assign header */
269          h->textSize = 0;
270          h->mapType = CM_HASMAP;
# Line 318 | Line 283 | register struct hdStruct  *h;
283                                          /* allocate targa data */
284          tarData = taralloc(h);
285  
286 <        return(p);
286 >        return(0);
287   }
288  
289  
290   tg2ra(hp)                       /* targa file to RADIANCE file */
291 < struct hdStruct  *hp;
291 > struct hdStruct  *hp;
292   {
293          union {
294                  BYTE  c3[256][3];
# Line 343 | Line 308 | struct hdStruct  *hp;
308          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
309                  if (hp->CMapBits == 24)
310                          setcolr(ctab[i],
311 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
312 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
313 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
311 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
312 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
313 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
314                  else
315                          setcolr(ctab[i],
316 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
317 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
318 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
316 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
317 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
318 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
319          if (bradj)
320                  shiftcolrs(ctab, 256, bradj);
321                                          /* allocate targa data */
# Line 366 | Line 331 | struct hdStruct  *hp;
331                  if (fwritecolrs(scanline, xmax, stdout) < 0)
332                          quiterr("error writing RADIANCE file");
333          }
334 <        free((char *)scanline);
335 <        free((char *)tarData);
334 >        free((void *)scanline);
335 >        free((void *)tarData);
336   }
337  
338  
339 < picreadline3(y, l3)                     /* read in 3-byte scanline */
340 < int  y;
341 < register rgbpixel  *l3;
339 > getmapped(nc, dith)             /* read in and quantize image */
340 > int  nc;                /* number of colors to use */
341 > int  dith;              /* use dithering? */
342   {
343 <        register int    i;
343 >        long  fpos;
344 >        register int  y;
345  
346 <        if (inpic->nexty != y) {                        /* find scanline */
347 <                if (inpic->bytes_line == 0) {
348 <                        if (inpic->pos.y[y] == 0) {
349 <                                while (inpic->nexty < y) {
350 <                                        if (freadcolrs(inl, xmax, inpic->fp) < 0)
351 <                                                quiterr("read error in picreadline3");
352 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
353 <                                }
354 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
355 <                                quiterr("seek error in picreadline3");
356 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
357 <                        quiterr("seek error in picreadline3");
358 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
359 <                inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
394 <        if (freadcolrs(inl, xmax, inpic->fp) < 0)       /* read scanline */
395 <                quiterr("read error in picreadline3");
396 <        inpic->nexty = y+1;
397 <                                                        /* convert scanline */
398 <        normcolrs(inl, xmax, bradj);
399 <        for (i = 0; i < xmax; i++) {
400 <                l3[i].r = inl[i][RED];
401 <                l3[i].g = inl[i][GRN];
402 <                l3[i].b = inl[i][BLU];
346 >        setcolrgam(gamv);
347 >        fpos = ftell(stdin);
348 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
349 >                quiterr("cannot initialized histogram");
350 >        for (y = ymax-1; y >= 0; y--) {
351 >                if (freadcolrs(inl, xmax, stdin) < 0)
352 >                        quiterr("error reading Radiance input");
353 >                if (bradj)
354 >                        shiftcolrs(inl, xmax, bradj);
355 >                colrs_gambs(inl, xmax);
356 >                if (samplefac)
357 >                        neu_colrs(inl, xmax);
358 >                else
359 >                        cnt_colrs(inl, xmax);
360          }
361 +        if (fseek(stdin, fpos, 0) == EOF)
362 +                quiterr("Radiance input must be from a file");
363 +        if (samplefac)                  /* map colors */
364 +                neu_clrtab(nc);
365 +        else
366 +                new_clrtab(nc);
367 +        for (y = ymax-1; y >= 0; y--) {
368 +                if (freadcolrs(inl, xmax, stdin) < 0)
369 +                        quiterr("error reading Radiance input");
370 +                if (bradj)
371 +                        shiftcolrs(inl, xmax, bradj);
372 +                colrs_gambs(inl, xmax);
373 +                if (samplefac)
374 +                        if (dith)
375 +                                neu_dith_colrs(tarData+y*xmax, inl, xmax);
376 +                        else
377 +                                neu_map_colrs(tarData+y*xmax, inl, xmax);
378 +                else
379 +                        if (dith)
380 +                                dith_colrs(tarData+y*xmax, inl, xmax);
381 +                        else
382 +                                map_colrs(tarData+y*xmax, inl, xmax);
383 +        }
384   }
385  
386  
387 < picwriteline(y, l)                      /* save output scanline */
388 < int  y;
409 < pixel  *l;
387 > getgrey(nc)                     /* read in and convert to greyscale image */
388 > int  nc;                /* number of colors to use */
389   {
390 <        bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
390 >        int  y;
391 >        register BYTE  *dp;
392 >        register int  x;
393 >
394 >        setcolrgam(gamv);
395 >        dp = tarData+xmax*ymax;;
396 >        for (y = ymax-1; y >= 0; y--) {
397 >                if (freadcolrs(inl, xmax, stdin) < 0)
398 >                        quiterr("error reading Radiance input");
399 >                if (bradj)
400 >                        shiftcolrs(inl, xmax, bradj);
401 >                x = xmax;
402 >                while (x--)
403 >                        inl[x][GRN] = normbright(inl[x]);
404 >                colrs_gambs(inl, xmax);
405 >                x = xmax;
406 >                if (nc < 256)
407 >                        while (x--)
408 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
409 >                else
410 >                        while (x--)
411 >                                *--dp = inl[x][GRN];
412 >        }
413 >        for (x = 0; x < nc; x++)
414 >                clrtab[x][RED] = clrtab[x][GRN] =
415 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
416   }
417  
418  
419   writetarga(h, d, fp)            /* write out targa data */
420 < struct hdStruct  *h;
421 < pixel  *d;
420 > struct hdStruct  *h;
421 > BYTE  *d;
422   FILE  *fp;
423   {
424 +        register int  i, j;
425 +
426 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
427 +                for (j = 2; j >= 0; j--)
428 +                        putc(clrtab[i][j], fp);
429          if (h->dataType == IM_CMAP) {           /* uncompressed */
430 <                if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y)
430 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
431                          quiterr("error writing targa file");
432                  return;
433          }
# Line 427 | Line 436 | FILE  *fp;
436  
437  
438   readtarga(h, data, fp)          /* read in targa data */
439 < struct hdStruct  *h;
440 < pixel  *data;
439 > struct hdStruct  *h;
440 > BYTE  *data;
441   FILE  *fp;
442   {
443          register int  cnt, c;
444 <        register pixel  *dp;
444 >        register BYTE   *dp;
445  
446          if (h->dataType == IM_CMAP) {           /* uncompressed */
447 <                if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y)
447 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
448                          goto readerr;
449                  return;
450          }
# Line 458 | Line 467 | FILE  *fp;
467          return;
468   readerr:
469          quiterr("error reading targa file");
461 }
462
463
464 picwritecm(cm)                  /* write out color map */
465 colormap  cm;
466 {
467        register int  i, j;
468
469        for (j = 0; j < 256; j++)
470                for (i = 2; i >= 0; i--)
471                        putc(cm[i][j], stdout);
472 }
473
474
475 picreadcm(map)                  /* do gamma correction if requested */
476 colormap  map;
477 {
478        register int  i, val;
479
480        for (i = 0; i < 256; i++) {
481                val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0;
482                map[0][i] = map[1][i] = map[2][i] = val;
483        }
470   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines