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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines