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.1 by greg, Thu Feb 2 10:49:37 1989 UTC vs.
Revision 2.13 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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  <string.h>
13 + #include  <time.h>
14 + #include  <math.h>
15  
16 + #include  "platform.h"
17 + #include  "rtmisc.h"
18   #include  "color.h"
19 <
20 < #include  "pic.h"
19 <
19 > #include  "resolu.h"
20 > #include  "clrtab.h"
21   #include  "targa.h"
22  
22                        /* descriptor for a picture file or frame buffer */
23 typedef struct {
24        char    *name;                  /* file name */
25        FILE    *fp;                    /* file pointer */
26        int     nexty;                  /* file positioning */
27        int     bytes_line;             /* 0 == variable length lines */
28        union {
29                long    b;                      /* initial scanline */
30                long    *y;                     /* individual scanline */
31        } pos;                          /* position(s) */
32 } pic;
23  
24 < #define  goodpic(h)     (my_imType(h) && my_mapType(h))
25 < #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
24 > #define  goodpic(h)     (my_imType(h) && my_mapType(h))
25 > #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
26                                  && (h)->dataBits==8 && (h)->imType==0)
27 < #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
27 > #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
28                                  ((h)->CMapBits==24 || (h)->CMapBits==32))
29  
30 < #define  taralloc(h)    (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel))
30 > #define  taralloc(h)    (BYTE *)emalloc((h)->x*(h)->y)
31  
32 < extern pic      *openinput();
33 <
34 < extern char     *ecalloc(), *emalloc();
35 <
46 < extern long  ftell();
47 <
48 < extern double  atof(), pow();
49 <
50 < double  gamma = 2.0;                    /* gamma correction */
51 <
52 < pic     *inpic;
53 <
32 > BYTE  clrtab[256][3];
33 > extern int      samplefac;
34 > double  gamv = 2.2;                     /* gamv correction */
35 > int  bradj = 0;                         /* brightness adjustment */
36   char  *progname;
55
37   char  errmsg[128];
38 <
39 < COLR    *inline;
59 <
60 < pixel   *tarData;
61 <
38 > COLR    *inl;
39 > BYTE    *tarData;
40   int  xmax, ymax;
41  
42 + static int getint2(FILE *fp);
43 + static void putint2(int  i, FILE        *fp);
44 + static void quiterr(char  *err);
45 + static int getthead(struct hdStruct      *hp, char  *ip, FILE  *fp);
46 + static int putthead(struct hdStruct      *hp, char  *ip, FILE  *fp);
47 + static int getrhead(struct hdStruct  *h, FILE  *fp);
48 + static void tg2ra(struct hdStruct        *hp);
49 + static void getmapped(int  nc, int  dith);
50 + static void getgrey(int  nc);
51 + static void writetarga(struct hdStruct   *h, BYTE  *d, FILE  *fp);
52 + static void readtarga(struct hdStruct    *h, BYTE  *data, FILE  *fp);
53  
54 < main(argc, argv)
55 < int  argc;
56 < char  *argv[];
54 >
55 > int
56 > main(
57 >        int  argc,
58 >        char  *argv[]
59 > )
60   {
61 <        colormap  rasmap;
70 <        struct hdStruct  head;
61 >        struct hdStruct  head;
62          int  dither = 1;
63          int  reverse = 0;
64          int  ncolors = 256;
65          int  greyscale = 0;
66          int  i;
67 <        
67 >        SET_DEFAULT_BINARY();
68 >        SET_FILE_BINARY(stdin);
69 >        SET_FILE_BINARY(stdout);
70          progname = argv[0];
71 +        samplefac = 0;
72  
73          for (i = 1; i < argc; i++)
74                  if (argv[i][0] == '-')
# Line 83 | Line 77 | char  *argv[];
77                                  dither = !dither;
78                                  break;
79                          case 'g':
80 <                                gamma = atof(argv[++i]);
80 >                                gamv = atof(argv[++i]);
81                                  break;
82                          case 'r':
83                                  reverse = !reverse;
# Line 91 | Line 85 | char  *argv[];
85                          case 'b':
86                                  greyscale = 1;
87                                  break;
88 +                        case 'e':
89 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
90 +                                        goto userr;
91 +                                bradj = atoi(argv[++i]);
92 +                                break;
93                          case 'c':
94                                  ncolors = atoi(argv[++i]);
95                                  break;
96 +                        case 'n':
97 +                                samplefac = atoi(argv[++i]);
98 +                                break;
99                          default:
100                                  goto userr;
101                          }
102                  else
103                          break;
104  
105 +        if (i < argc-2)
106 +                goto userr;
107 +        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
108 +                sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
109 +                quiterr(errmsg);
110 +        }
111 +        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
112 +                sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
113 +                quiterr(errmsg);
114 +        }
115          if (reverse) {
104                if (i < argc-2)
105                        goto userr;
106                if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
107                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
108                        quiterr(errmsg);
109                }
116                                          /* get header */
117                  if (getthead(&head, NULL, stdin) < 0)
118                          quiterr("bad targa file");
# Line 114 | Line 120 | char  *argv[];
120                          quiterr("incompatible format");
121                  xmax = head.x;
122                  ymax = head.y;
117                                        /* open output file */
118                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
119                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
120                        quiterr(errmsg);
121                }
123                                          /* put header */
124 <                printargs(argc, argv, stdout);
124 >                newheader("RADIANCE", stdout);
125 >                printargs(i, argv, stdout);
126 >                fputformat(COLRFMT, stdout);
127                  putchar('\n');
128 <                printf("-Y %d +X %d\n", ymax, xmax);
128 >                fprtresolu(xmax, ymax, stdout);
129                                          /* convert file */
130                  tg2ra(&head);
131          } else {
132 <                if (i > argc-1 || i < argc-2)
133 <                        goto userr;
131 <                if ((inpic = openinput(argv[i], &head)) == NULL) {
132 <                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
133 <                        quiterr(errmsg);
134 <                }
135 <                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
136 <                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
137 <                        quiterr(errmsg);
138 <                }
132 >                if (getrhead(&head, stdin) < 0)
133 >                        quiterr("bad Radiance input");
134                                          /* write header */
135                  putthead(&head, NULL, stdout);
136                                          /* convert file */
137                  if (greyscale)
138 <                        biq(dither,ncolors,1,rasmap);
138 >                        getgrey(ncolors);
139                  else
140 <                        ciq(dither,ncolors,1,rasmap);
140 >                        getmapped(ncolors, dither);
141                                          /* write data */
142                  writetarga(&head, tarData, stdout);
143          }
144          quiterr(NULL);
145   userr:
146          fprintf(stderr,
147 <        "Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n",
147 >        "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
148                          progname);
149 <        fprintf(stderr, "   Or: %s -r [-g gamma] [input [output]]\n",
149 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
150                          progname);
151          exit(1);
152   }
153  
154  
155 < int
156 < getint2(fp)                     /* get a 2-byte positive integer */
157 < register FILE   *fp;
155 > static int
156 > getint2(                        /* get a 2-byte positive integer */
157 >        register FILE   *fp
158 > )
159   {
160          register int  b1, b2;
161  
# Line 170 | Line 166 | register FILE  *fp;
166   }
167  
168  
169 < putint2(i, fp)                  /* put a 2-byte positive integer */
170 < register int  i;
171 < register FILE   *fp;
169 > static void
170 > putint2(                        /* put a 2-byte positive integer */
171 >        register int  i,
172 >        register FILE   *fp
173 > )
174   {
175          putc(i&0xff, fp);
176          putc(i>>8&0xff, fp);
177   }
178  
179  
180 < quiterr(err)            /* print message and exit */
181 < char  *err;
180 > static void
181 > quiterr(                /* print message and exit */
182 >        char  *err
183 > )
184   {
185          if (err != NULL) {
186                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 190 | Line 190 | char  *err;
190   }
191  
192  
193 + void
194   eputs(s)
195   char *s;
196   {
# Line 197 | Line 198 | char *s;
198   }
199  
200  
201 + void
202   quit(code)
203   int code;
204   {
# Line 204 | Line 206 | int code;
206   }
207  
208  
209 < getthead(hp, ip, fp)            /* read header from input */
210 < struct hdStruct  *hp;
211 < char  *ip;
212 < register FILE  *fp;
209 > static int
210 > getthead(               /* read header from input */
211 >        struct hdStruct  *hp,
212 >        char  *ip,
213 >        register FILE  *fp
214 > )
215   {
216          int     nidbytes;
217  
# Line 227 | Line 231 | register FILE  *fp;
231  
232          if (ip != NULL)
233                  if (nidbytes)
234 <                        fread(ip, nidbytes, 1, fp);
234 >                        fread((char *)ip, nidbytes, 1, fp);
235                  else
236                          *ip = '\0';
237          else if (nidbytes)
# Line 237 | Line 241 | register FILE  *fp;
241   }
242  
243  
244 < putthead(hp, ip, fp)            /* write header to output */
245 < struct hdStruct  *hp;
246 < char  *ip;
247 < register FILE  *fp;
244 > static int
245 > putthead(               /* write header to output */
246 >        struct hdStruct  *hp,
247 >        char  *ip,
248 >        register FILE  *fp
249 > )
250   {
251          if (ip != NULL)
252                  putc(strlen(ip), fp);
# Line 265 | Line 271 | register FILE  *fp;
271   }
272  
273  
274 < pic *
275 < openinput(fname, h)             /* open RADIANCE input file */
276 < char  *fname;
277 < register struct hdStruct  *h;
274 > static int
275 > getrhead(                       /* load RADIANCE input file header */
276 >        register struct hdStruct  *h,
277 >        FILE  *fp
278 > )
279   {
280 <        register pic  *p;
281 <
282 <        p = (pic *)emalloc(sizeof(pic));
283 <        p->name = fname;
277 <        if (fname == NULL)
278 <                p->fp = stdin;
279 <        else if ((p->fp = fopen(fname, "r")) == NULL)
280 <                return(NULL);
281 <                                        /* discard header */
282 <        getheader(p->fp, NULL);
283 <        if (fscanf(p->fp, "-Y %d +X %d\n", &ymax, &xmax) != 2)
284 <                quiterr("bad picture size");
285 <        p->nexty = 0;
286 <        p->bytes_line = 0;              /* variable length lines */
287 <        p->pos.y = (long *)ecalloc(ymax, sizeof(long));
288 <        p->pos.y[0] = ftell(p->fp);
280 >                                        /* get header info. */
281 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
282 >                        fgetresolu(&xmax, &ymax, fp) < 0)
283 >                return(-1);
284                                          /* assign header */
285          h->textSize = 0;
286          h->mapType = CM_HASMAP;
# Line 300 | Line 295 | register struct hdStruct  *h;
295          h->dataBits = 8;
296          h->imType = 0;
297                                          /* allocate scanline */
298 <        inline = (COLR *)emalloc(xmax*sizeof(COLR));
298 >        inl = (COLR *)emalloc(xmax*sizeof(COLR));
299                                          /* allocate targa data */
300          tarData = taralloc(h);
301  
302 <        return(p);
302 >        return(0);
303   }
304  
305  
306 < tg2ra(hp)                       /* targa file to RADIANCE file */
307 < struct hdStruct  *hp;
306 > static void
307 > tg2ra(                  /* targa file to RADIANCE file */
308 >        struct hdStruct  *hp
309 > )
310   {
311          union {
312                  BYTE  c3[256][3];
# Line 320 | Line 317 | struct hdStruct  *hp;
317          register int  i, j;
318  
319                                          /* get color table */
320 <        if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) :
321 <                        fread(map.c4, sizeof(map.c4), 1, stdin)) != 1)
320 >        if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig),
321 >                                3*hp->mapLength,1,stdin) :
322 >                        fread((char *)(map.c4+hp->mapOrig),
323 >                                4*hp->mapLength,1,stdin)) != 1)
324                  quiterr("error reading color table");
325                                          /* convert table */
326          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
327                  if (hp->CMapBits == 24)
328                          setcolr(ctab[i],
329 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
330 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
331 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
329 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
330 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
331 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
332                  else
333                          setcolr(ctab[i],
334 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
335 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
336 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
337 <
334 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
335 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
336 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
337 >        if (bradj)
338 >                shiftcolrs(ctab, 256, bradj);
339                                          /* allocate targa data */
340          tarData = taralloc(hp);
341                                          /* get data */
# Line 349 | Line 349 | struct hdStruct  *hp;
349                  if (fwritecolrs(scanline, xmax, stdout) < 0)
350                          quiterr("error writing RADIANCE file");
351          }
352 <        free((char *)scanline);
353 <        free((char *)tarData);
352 >        free((void *)scanline);
353 >        free((void *)tarData);
354   }
355  
356  
357 < picreadline3(y, l3)                     /* read in 3-byte scanline */
358 < int  y;
359 < register rgbpixel  *l3;
357 > static void
358 > getmapped(              /* read in and quantize image */
359 >        int  nc,                /* number of colors to use */
360 >        int  dith               /* use dithering? */
361 > )
362   {
363 <        register BYTE   *l4;
364 <        register int    shift, c;
363 <        int     i;
363 >        long  fpos;
364 >        register int  y;
365  
366 <        if (inpic->nexty != y) {                                /* find scanline */
367 <                if (inpic->bytes_line == 0) {
368 <                        if (inpic->pos.y[y] == 0) {
369 <                                while (inpic->nexty < y) {
370 <                                        if (freadcolrs(inline, xmax, inpic->fp) < 0)
371 <                                                quiterr("read error in picreadline3");
372 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
373 <                                }
374 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
375 <                                quiterr("seek error in picreadline3");
376 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
377 <                        quiterr("seek error in picreadline3");
378 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
379 <                inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
379 <        if (freadcolrs(inline, xmax, inpic->fp) < 0)    /* read scanline */
380 <                quiterr("read error in picreadline3");
381 <        inpic->nexty = y+1;
382 <                                                        /* convert scanline */
383 <        for (l4=inline[0], i=xmax; i--; l4+=4, l3++) {
384 <                shift = l4[EXP] - COLXS;
385 <                if (shift >= 8) {
386 <                        l3->r = l3->g = l3->b = 255;
387 <                } else if (shift <= -8) {
388 <                        l3->r = l3->g = l3->b = 0;
389 <                } else if (shift > 0) {
390 <                        c = l4[RED] << shift;
391 <                        l3->r = c > 255 ? 255 : c;
392 <                        c = l4[GRN] << shift;
393 <                        l3->g = c > 255 ? 255 : c;
394 <                        c = l4[BLU] << shift;
395 <                        l3->b = c > 255 ? 255 : c;
396 <                } else if (shift < 0) {
397 <                        l3->r = l4[RED] >> -shift;
398 <                        l3->g = l4[GRN] >> -shift;
399 <                        l3->b = l4[BLU] >> -shift;
400 <                } else {
401 <                        l3->r = l4[RED];
402 <                        l3->g = l4[GRN];
403 <                        l3->b = l4[BLU];
404 <                }
366 >        setcolrgam(gamv);
367 >        fpos = ftell(stdin);
368 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
369 >                quiterr("cannot initialized histogram");
370 >        for (y = ymax-1; y >= 0; y--) {
371 >                if (freadcolrs(inl, xmax, stdin) < 0)
372 >                        quiterr("error reading Radiance input");
373 >                if (bradj)
374 >                        shiftcolrs(inl, xmax, bradj);
375 >                colrs_gambs(inl, xmax);
376 >                if (samplefac)
377 >                        neu_colrs(inl, xmax);
378 >                else
379 >                        cnt_colrs(inl, xmax);
380          }
381 +        if (fseek(stdin, fpos, 0) == EOF)
382 +                quiterr("Radiance input must be from a file");
383 +        if (samplefac)                  /* map colors */
384 +                neu_clrtab(nc);
385 +        else
386 +                new_clrtab(nc);
387 +        for (y = ymax-1; y >= 0; y--) {
388 +                if (freadcolrs(inl, xmax, stdin) < 0)
389 +                        quiterr("error reading Radiance input");
390 +                if (bradj)
391 +                        shiftcolrs(inl, xmax, bradj);
392 +                colrs_gambs(inl, xmax);
393 +                if (samplefac)
394 +                        if (dith)
395 +                                neu_dith_colrs(tarData+y*xmax, inl, xmax);
396 +                        else
397 +                                neu_map_colrs(tarData+y*xmax, inl, xmax);
398 +                else
399 +                        if (dith)
400 +                                dith_colrs(tarData+y*xmax, inl, xmax);
401 +                        else
402 +                                map_colrs(tarData+y*xmax, inl, xmax);
403 +        }
404   }
405  
406  
407 < picwriteline(y, l)                      /* save output scanline */
408 < int  y;
409 < pixel  *l;
407 > static void
408 > getgrey(                        /* read in and convert to greyscale image */
409 >        int  nc         /* number of colors to use */
410 > )
411   {
412 <        bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
412 >        int  y;
413 >        register BYTE  *dp;
414 >        register int  x;
415 >
416 >        setcolrgam(gamv);
417 >        dp = tarData+xmax*ymax;;
418 >        for (y = ymax-1; y >= 0; y--) {
419 >                if (freadcolrs(inl, xmax, stdin) < 0)
420 >                        quiterr("error reading Radiance input");
421 >                if (bradj)
422 >                        shiftcolrs(inl, xmax, bradj);
423 >                x = xmax;
424 >                while (x--)
425 >                        inl[x][GRN] = normbright(inl[x]);
426 >                colrs_gambs(inl, xmax);
427 >                x = xmax;
428 >                if (nc < 256)
429 >                        while (x--)
430 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
431 >                else
432 >                        while (x--)
433 >                                *--dp = inl[x][GRN];
434 >        }
435 >        for (x = 0; x < nc; x++)
436 >                clrtab[x][RED] = clrtab[x][GRN] =
437 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
438   }
439  
440  
441 < writetarga(h, d, fp)            /* write out targa data */
442 < struct hdStruct  *h;
443 < pixel  *d;
444 < FILE  *fp;
441 > static void
442 > writetarga(             /* write out targa data */
443 >        struct hdStruct  *h,
444 >        BYTE  *d,
445 >        FILE  *fp
446 > )
447   {
448 +        register int  i, j;
449 +
450 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
451 +                for (j = 2; j >= 0; j--)
452 +                        putc(clrtab[i][j], fp);
453          if (h->dataType == IM_CMAP) {           /* uncompressed */
454 <                if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y)
454 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
455                          quiterr("error writing targa file");
456                  return;
457          }
# Line 428 | Line 459 | FILE  *fp;
459   }
460  
461  
462 < readtarga(h, data, fp)          /* read in targa data */
463 < struct hdStruct  *h;
464 < pixel  *data;
465 < FILE  *fp;
462 > static void
463 > readtarga(              /* read in targa data */
464 >        struct hdStruct  *h,
465 >        BYTE  *data,
466 >        FILE  *fp
467 > )
468   {
469          register int  cnt, c;
470 <        register pixel  *dp;
470 >        register BYTE   *dp;
471  
472          if (h->dataType == IM_CMAP) {           /* uncompressed */
473 <                if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y)
473 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
474                          goto readerr;
475                  return;
476          }
# Line 460 | Line 493 | FILE  *fp;
493          return;
494   readerr:
495          quiterr("error reading targa file");
463 }
464
465
466 picwritecm(cm)                  /* write out color map */
467 colormap  cm;
468 {
469        register int  i, j;
470
471        for (j = 0; j < 256; j++)
472                for (i = 2; i >= 0; i--)
473                        putc(cm[i][j], stdout);
474 }
475
476
477 picreadcm(map)                  /* do gamma correction if requested */
478 colormap  map;
479 {
480        register int  i, val;
481
482        for (i = 0; i < 256; i++) {
483                val = pow(i/256.0, 1.0/gamma) * 256.0;
484                map[0][i] = map[1][i] = map[2][i] = val;
485        }
496   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines