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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines