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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines