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.5 by greg, Fri Jan 26 08:17:39 1990 UTC vs.
Revision 2.17 by greg, Thu Feb 9 21:54:11 2023 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 #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;
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 <
50 < extern long  ftell();
51 <
52 < extern double  atof(), pow();
53 <
54 < double  gamma = 2.0;                    /* gamma correction */
55 <
56 < pic     *inpic;
57 <
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;
59
35   char  errmsg[128];
36 <
37 < COLR    *inline;
63 <
64 < pixel   *tarData;
65 <
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;
74 <        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 87 | 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 95 | 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) {
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                }
114                                          /* get header */
115                  if (getthead(&head, NULL, stdin) < 0)
116                          quiterr("bad targa file");
# Line 118 | Line 118 | char  *argv[];
118                          quiterr("incompatible format");
119                  xmax = head.x;
120                  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                }
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 <                fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
126 >                fprtresolu(xmax, ymax, stdout);
127                                          /* convert file */
128                  tg2ra(&head);
129          } else {
130 <                if (i > argc-1 || i < argc-2)
131 <                        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 <                }
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 >        FILE    *fp
156 > )
157   {
158 <        register int  b1, b2;
158 >        int  b1, b2;
159  
160          if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF)
161                  quiterr("read error");
# Line 174 | 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 >        int  i,
170 >        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 194 | Line 188 | char  *err;
188   }
189  
190  
191 < eputs(s)
192 < char *s;
191 > void
192 > eputs(const char *s)
193   {
194          fputs(s, stderr);
195   }
196  
197  
198 < quit(code)
199 < int code;
198 > void
199 > quit(int code)
200   {
201          exit(code);
202   }
203  
204  
205 < getthead(hp, ip, fp)            /* read header from input */
206 < struct hdStruct  *hp;
207 < char  *ip;
208 < register FILE  *fp;
205 > static int
206 > getthead(               /* read header from input */
207 >        struct hdStruct  *hp,
208 >        char  *ip,
209 >        FILE  *fp
210 > )
211   {
212          int     nidbytes;
213  
# Line 241 | Line 237 | register FILE  *fp;
237   }
238  
239  
240 < putthead(hp, ip, fp)            /* write header to output */
241 < struct hdStruct  *hp;
242 < char  *ip;
243 < register FILE  *fp;
240 > static int
241 > putthead(               /* write header to output */
242 >        struct hdStruct  *hp,
243 >        char  *ip,
244 >        FILE  *fp
245 > )
246   {
247          if (ip != NULL)
248                  putc(strlen(ip), fp);
# Line 269 | Line 267 | register FILE  *fp;
267   }
268  
269  
270 < pic *
271 < openinput(fname, h)             /* open RADIANCE input file */
272 < char  *fname;
273 < register struct hdStruct  *h;
270 > static int
271 > getrhead(                       /* load RADIANCE input file header */
272 >        struct hdStruct  *h,
273 >        FILE  *fp
274 > )
275   {
276 <        register pic  *p;
277 <
278 <        p = (pic *)emalloc(sizeof(pic));
279 <        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);
276 >                                        /* get header info. */
277 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
278 >                        fgetresolu(&xmax, &ymax, fp) < 0)
279 >                return(-1);
280                                          /* assign header */
281          h->textSize = 0;
282          h->mapType = CM_HASMAP;
# Line 304 | Line 291 | register struct hdStruct  *h;
291          h->dataBits = 8;
292          h->imType = 0;
293                                          /* allocate scanline */
294 <        inline = (COLR *)emalloc(xmax*sizeof(COLR));
294 >        inl = (COLR *)emalloc(xmax*sizeof(COLR));
295                                          /* allocate targa data */
296          tarData = taralloc(h);
297  
298 <        return(p);
298 >        return(0);
299   }
300  
301  
302 < tg2ra(hp)                       /* targa file to RADIANCE file */
303 < struct hdStruct  *hp;
302 > static void
303 > tg2ra(                  /* targa file to RADIANCE file */
304 >        struct hdStruct  *hp
305 > )
306   {
307          union {
308 <                BYTE  c3[256][3];
309 <                BYTE  c4[256][4];
308 >                uby8  c3[256][3];
309 >                uby8  c4[256][4];
310          } map;
311          COLR  ctab[256];
312          COLR  *scanline;
313 <        register int  i, j;
313 >        int  i, j;
314  
315                                          /* get color table */
316 <        if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) :
317 <                        fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1)
316 >        if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig),
317 >                                3*hp->mapLength,1,stdin) :
318 >                        fread((char *)(map.c4+hp->mapOrig),
319 >                                4*hp->mapLength,1,stdin)) != 1)
320                  quiterr("error reading color table");
321                                          /* convert table */
322          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
323                  if (hp->CMapBits == 24)
324                          setcolr(ctab[i],
325 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
326 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
327 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
325 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
326 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
327 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
328                  else
329                          setcolr(ctab[i],
330 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
331 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
332 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
333 <
330 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
331 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
332 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
333 >        if (bradj)
334 >                shiftcolrs(ctab, 256, bradj);
335                                          /* allocate targa data */
336          tarData = taralloc(hp);
337                                          /* get data */
# Line 353 | Line 345 | struct hdStruct  *hp;
345                  if (fwritecolrs(scanline, xmax, stdout) < 0)
346                          quiterr("error writing RADIANCE file");
347          }
348 <        free((char *)scanline);
349 <        free((char *)tarData);
348 >        free((void *)scanline);
349 >        free((void *)tarData);
350   }
351  
352  
353 < picreadline3(y, l3)                     /* read in 3-byte scanline */
354 < int  y;
355 < register rgbpixel  *l3;
353 > static void
354 > getmapped(              /* read in and quantize image */
355 >        int  nc,                /* number of colors to use */
356 >        int  dith               /* use dithering? */
357 > )
358   {
359 <        register int    i;
359 >        long  fpos;
360 >        int  y;
361  
362 <        if (inpic->nexty != y) {                        /* find scanline */
363 <                if (inpic->bytes_line == 0) {
364 <                        if (inpic->pos.y[y] == 0) {
365 <                                while (inpic->nexty < y) {
366 <                                        if (freadcolrs(inline, xmax, inpic->fp) < 0)
367 <                                                quiterr("read error in picreadline3");
368 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
369 <                                }
370 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
371 <                                quiterr("seek error in picreadline3");
372 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
373 <                        quiterr("seek error in picreadline3");
374 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
375 <                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];
362 >        setcolrgam(gamv);
363 >        fpos = ftell(stdin);
364 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
365 >                quiterr("cannot initialized histogram");
366 >        for (y = ymax-1; y >= 0; y--) {
367 >                if (freadcolrs(inl, xmax, stdin) < 0)
368 >                        quiterr("error reading Radiance input");
369 >                if (bradj)
370 >                        shiftcolrs(inl, xmax, bradj);
371 >                colrs_gambs(inl, xmax);
372 >                if (samplefac)
373 >                        neu_colrs(inl, xmax);
374 >                else
375 >                        cnt_colrs(inl, xmax);
376          }
377 +        if (fseek(stdin, fpos, 0) == EOF)
378 +                quiterr("Radiance input must be from a file");
379 +        if (samplefac)                  /* map colors */
380 +                neu_clrtab(nc);
381 +        else
382 +                new_clrtab(nc);
383 +        for (y = ymax-1; y >= 0; y--) {
384 +                if (freadcolrs(inl, xmax, stdin) < 0)
385 +                        quiterr("error reading Radiance input");
386 +                if (bradj)
387 +                        shiftcolrs(inl, xmax, bradj);
388 +                colrs_gambs(inl, xmax);
389 +                if (samplefac)
390 +                        if (dith)
391 +                                neu_dith_colrs(tarData+y*xmax, inl, xmax);
392 +                        else
393 +                                neu_map_colrs(tarData+y*xmax, inl, xmax);
394 +                else
395 +                        if (dith)
396 +                                dith_colrs(tarData+y*xmax, inl, xmax);
397 +                        else
398 +                                map_colrs(tarData+y*xmax, inl, xmax);
399 +        }
400   }
401  
402  
403 < picwriteline(y, l)                      /* save output scanline */
404 < int  y;
405 < pixel  *l;
403 > static void
404 > getgrey(                        /* read in and convert to greyscale image */
405 >        int  nc         /* number of colors to use */
406 > )
407   {
408 <        bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
408 >        int  y;
409 >        uby8  *dp;
410 >        int  x;
411 >
412 >        setcolrgam(gamv);
413 >        dp = tarData+xmax*ymax;;
414 >        for (y = ymax-1; y >= 0; y--) {
415 >                if (freadcolrs(inl, xmax, stdin) < 0)
416 >                        quiterr("error reading Radiance input");
417 >                if (bradj)
418 >                        shiftcolrs(inl, xmax, bradj);
419 >                x = xmax;
420 >                while (x--)
421 >                        inl[x][GRN] = normbright(inl[x]);
422 >                colrs_gambs(inl, xmax);
423 >                x = xmax;
424 >                if (nc < 256)
425 >                        while (x--)
426 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
427 >                else
428 >                        while (x--)
429 >                                *--dp = inl[x][GRN];
430 >        }
431 >        for (x = 0; x < nc; x++)
432 >                clrtab[x][RED] = clrtab[x][GRN] =
433 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
434   }
435  
436  
437 < writetarga(h, d, fp)            /* write out targa data */
438 < struct hdStruct  *h;
439 < pixel  *d;
440 < FILE  *fp;
437 > static void
438 > writetarga(             /* write out targa data */
439 >        struct hdStruct  *h,
440 >        uby8  *d,
441 >        FILE  *fp
442 > )
443   {
444 +        int  i, j;
445 +
446 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
447 +                for (j = 2; j >= 0; j--)
448 +                        putc(clrtab[i][j], fp);
449          if (h->dataType == IM_CMAP) {           /* uncompressed */
450 <                if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y)
450 >                if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y)
451                          quiterr("error writing targa file");
452                  return;
453          }
# Line 413 | Line 455 | FILE  *fp;
455   }
456  
457  
458 < readtarga(h, data, fp)          /* read in targa data */
459 < struct hdStruct  *h;
460 < pixel  *data;
461 < FILE  *fp;
458 > static void
459 > readtarga(              /* read in targa data */
460 >        struct hdStruct  *h,
461 >        uby8  *data,
462 >        FILE  *fp
463 > )
464   {
465 <        register int  cnt, c;
466 <        register pixel  *dp;
465 >        int  cnt, c;
466 >        uby8    *dp;
467  
468          if (h->dataType == IM_CMAP) {           /* uncompressed */
469 <                if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y)
469 >                if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y)
470                          goto readerr;
471                  return;
472          }
# Line 445 | Line 489 | FILE  *fp;
489          return;
490   readerr:
491          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        }
492   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines