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.2 by greg, Tue Sep 12 13:04:38 1989 UTC vs.
Revision 2.9 by greg, Fri Nov 10 17:04:52 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15  
16   #include  "color.h"
17  
18 < #include  "pic.h"
18 > #include  "resolu.h"
19  
20   #include  "targa.h"
21  
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;
22 > #ifdef MSDOS
23 > #include  <fcntl.h>
24 > #endif
25  
26 < #define  goodpic(h)     (my_imType(h) && my_mapType(h))
27 < #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
26 > #include  <math.h>
27 >
28 > #ifndef  BSD
29 > #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
30 > #endif
31 >
32 > #define  goodpic(h)     (my_imType(h) && my_mapType(h))
33 > #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
34                                  && (h)->dataBits==8 && (h)->imType==0)
35 < #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
35 > #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
36                                  ((h)->CMapBits==24 || (h)->CMapBits==32))
37  
38 < #define  taralloc(h)    (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel))
38 > #define  taralloc(h)    (BYTE *)emalloc((h)->x*(h)->y)
39  
40 < extern pic      *openinput();
40 > BYTE  clrtab[256][3];
41  
42 + extern int      samplefac;
43 +
44   extern char     *ecalloc(), *emalloc();
45  
46   extern long  ftell();
47  
48 < extern double  atof(), pow();
48 > double  gamv = 2.2;                     /* gamv correction */
49  
50 < double  gamma = 2.0;                    /* gamma correction */
50 > int  bradj = 0;                         /* brightness adjustment */
51  
52 pic     *inpic;
53
52   char  *progname;
53  
54   char  errmsg[128];
55  
56 < COLR    *inline;
56 > COLR    *inl;
57  
58 < pixel   *tarData;
58 > BYTE    *tarData;
59  
60   int  xmax, ymax;
61  
# Line 66 | Line 64 | main(argc, argv)
64   int  argc;
65   char  *argv[];
66   {
67 <        colormap  rasmap;
70 <        struct hdStruct  head;
67 >        struct hdStruct  head;
68          int  dither = 1;
69          int  reverse = 0;
70          int  ncolors = 256;
71          int  greyscale = 0;
72          int  i;
73 <        
73 > #ifdef MSDOS
74 >        extern int  _fmode;
75 >        _fmode = O_BINARY;
76 >        setmode(fileno(stdin), O_BINARY);
77 >        setmode(fileno(stdout), O_BINARY);
78 > #endif
79          progname = argv[0];
80 +        samplefac = 0;
81  
82          for (i = 1; i < argc; i++)
83                  if (argv[i][0] == '-')
# Line 83 | Line 86 | char  *argv[];
86                                  dither = !dither;
87                                  break;
88                          case 'g':
89 <                                gamma = atof(argv[++i]);
89 >                                gamv = atof(argv[++i]);
90                                  break;
91                          case 'r':
92                                  reverse = !reverse;
# Line 91 | Line 94 | char  *argv[];
94                          case 'b':
95                                  greyscale = 1;
96                                  break;
97 +                        case 'e':
98 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
99 +                                        goto userr;
100 +                                bradj = atoi(argv[++i]);
101 +                                break;
102                          case 'c':
103                                  ncolors = atoi(argv[++i]);
104                                  break;
105 +                        case 'n':
106 +                                samplefac = atoi(argv[++i]);
107 +                                break;
108                          default:
109                                  goto userr;
110                          }
111                  else
112                          break;
113  
114 +        if (i < argc-2)
115 +                goto userr;
116 +        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
117 +                sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
118 +                quiterr(errmsg);
119 +        }
120 +        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
121 +                sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
122 +                quiterr(errmsg);
123 +        }
124          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                }
125                                          /* get header */
126                  if (getthead(&head, NULL, stdin) < 0)
127                          quiterr("bad targa file");
# Line 114 | Line 129 | char  *argv[];
129                          quiterr("incompatible format");
130                  xmax = head.x;
131                  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                }
132                                          /* put header */
133 <                printargs(argc, argv, stdout);
133 >                newheader("RADIANCE", stdout);
134 >                printargs(i, argv, stdout);
135 >                fputformat(COLRFMT, stdout);
136                  putchar('\n');
137 <                fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
137 >                fprtresolu(xmax, ymax, stdout);
138                                          /* convert file */
139                  tg2ra(&head);
140          } else {
141 <                if (i > argc-1 || i < argc-2)
142 <                        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 <                }
141 >                if (getrhead(&head, stdin) < 0)
142 >                        quiterr("bad Radiance input");
143                                          /* write header */
144                  putthead(&head, NULL, stdout);
145                                          /* convert file */
146                  if (greyscale)
147 <                        biq(dither,ncolors,1,rasmap);
147 >                        getgrey(ncolors);
148                  else
149 <                        ciq(dither,ncolors,1,rasmap);
149 >                        getmapped(ncolors, dither);
150                                          /* write data */
151                  writetarga(&head, tarData, stdout);
152          }
153          quiterr(NULL);
154   userr:
155          fprintf(stderr,
156 <        "Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n",
156 >        "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
157                          progname);
158 <        fprintf(stderr, "   Or: %s -r [-g gamma] [input [output]]\n",
158 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
159                          progname);
160          exit(1);
161   }
# Line 205 | Line 209 | int code;
209  
210  
211   getthead(hp, ip, fp)            /* read header from input */
212 < struct hdStruct  *hp;
212 > struct hdStruct  *hp;
213   char  *ip;
214   register FILE  *fp;
215   {
# Line 227 | Line 231 | register FILE  *fp;
231  
232          if (ip != NULL)
233                  if (nidbytes)
234 <                        fread(ip, nidbytes, 1, fp);
234 >                        fread((char *)ip, nidbytes, 1, fp);
235                  else
236                          *ip = '\0';
237          else if (nidbytes)
# Line 238 | Line 242 | register FILE  *fp;
242  
243  
244   putthead(hp, ip, fp)            /* write header to output */
245 < struct hdStruct  *hp;
245 > struct hdStruct  *hp;
246   char  *ip;
247   register FILE  *fp;
248   {
# Line 265 | Line 269 | register FILE  *fp;
269   }
270  
271  
272 < pic *
269 < openinput(fname, h)             /* open RADIANCE input file */
270 < char  *fname;
272 > getrhead(h, fp)                 /* load RADIANCE input file header */
273   register struct hdStruct  *h;
274 + FILE  *fp;
275   {
276 <        register pic  *p;
277 <
278 <        p = (pic *)emalloc(sizeof(pic));
279 <        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);
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 300 | 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;
303 > struct hdStruct  *hp;
304   {
305          union {
306                  BYTE  c3[256][3];
# Line 320 | Line 311 | struct hdStruct  *hp;
311          register int  i, j;
312  
313                                          /* get color table */
314 <        if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) :
315 <                        fread(map.c4, sizeof(map.c4), 1, stdin)) != 1)
314 >        if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig),
315 >                                3*hp->mapLength,1,stdin) :
316 >                        fread((char *)(map.c4+hp->mapOrig),
317 >                                4*hp->mapLength,1,stdin)) != 1)
318                  quiterr("error reading color table");
319                                          /* convert table */
320          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
321                  if (hp->CMapBits == 24)
322                          setcolr(ctab[i],
323 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
324 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
325 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
323 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
324 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
325 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
326                  else
327                          setcolr(ctab[i],
328 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
329 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
330 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
331 <
328 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
329 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
330 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
331 >        if (bradj)
332 >                shiftcolrs(ctab, 256, bradj);
333                                          /* allocate targa data */
334          tarData = taralloc(hp);
335                                          /* get data */
# Line 354 | Line 348 | struct hdStruct  *hp;
348   }
349  
350  
351 < picreadline3(y, l3)                     /* read in 3-byte scanline */
352 < int  y;
353 < register rgbpixel  *l3;
351 > getmapped(nc, dith)             /* read in and quantize image */
352 > int  nc;                /* number of colors to use */
353 > int  dith;              /* use dithering? */
354   {
355 <        register BYTE   *l4;
356 <        register int    shift, c;
363 <        int     i;
355 >        long  fpos;
356 >        register int  y;
357  
358 <        if (inpic->nexty != y) {                                /* find scanline */
359 <                if (inpic->bytes_line == 0) {
360 <                        if (inpic->pos.y[y] == 0) {
361 <                                while (inpic->nexty < y) {
362 <                                        if (freadcolrs(inline, xmax, inpic->fp) < 0)
363 <                                                quiterr("read error in picreadline3");
364 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
365 <                                }
366 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
367 <                                quiterr("seek error in picreadline3");
368 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
369 <                        quiterr("seek error in picreadline3");
370 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
371 <                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 <                }
358 >        setcolrgam(gamv);
359 >        fpos = ftell(stdin);
360 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
361 >                quiterr("cannot initialized histogram");
362 >        for (y = ymax-1; y >= 0; y--) {
363 >                if (freadcolrs(inl, xmax, stdin) < 0)
364 >                        quiterr("error reading Radiance input");
365 >                if (bradj)
366 >                        shiftcolrs(inl, xmax, bradj);
367 >                colrs_gambs(inl, xmax);
368 >                if (samplefac)
369 >                        neu_colrs(inl, xmax);
370 >                else
371 >                        cnt_colrs(inl, xmax);
372          }
373 +        if (fseek(stdin, fpos, 0) == EOF)
374 +                quiterr("Radiance input must be from a file");
375 +        if (samplefac)                  /* map colors */
376 +                neu_clrtab(nc);
377 +        else
378 +                new_clrtab(nc);
379 +        for (y = ymax-1; y >= 0; y--) {
380 +                if (freadcolrs(inl, xmax, stdin) < 0)
381 +                        quiterr("error reading Radiance input");
382 +                if (bradj)
383 +                        shiftcolrs(inl, xmax, bradj);
384 +                colrs_gambs(inl, xmax);
385 +                if (samplefac)
386 +                        if (dith)
387 +                                neu_dith_colrs(tarData+y*xmax, inl, xmax);
388 +                        else
389 +                                neu_map_colrs(tarData+y*xmax, inl, xmax);
390 +                else
391 +                        if (dith)
392 +                                dith_colrs(tarData+y*xmax, inl, xmax);
393 +                        else
394 +                                map_colrs(tarData+y*xmax, inl, xmax);
395 +        }
396   }
397  
398  
399 < picwriteline(y, l)                      /* save output scanline */
400 < int  y;
411 < pixel  *l;
399 > getgrey(nc)                     /* read in and convert to greyscale image */
400 > int  nc;                /* number of colors to use */
401   {
402 <        bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
402 >        int  y;
403 >        register BYTE  *dp;
404 >        register int  x;
405 >
406 >        setcolrgam(gamv);
407 >        dp = tarData+xmax*ymax;;
408 >        for (y = ymax-1; y >= 0; y--) {
409 >                if (freadcolrs(inl, xmax, stdin) < 0)
410 >                        quiterr("error reading Radiance input");
411 >                if (bradj)
412 >                        shiftcolrs(inl, xmax, bradj);
413 >                x = xmax;
414 >                while (x--)
415 >                        inl[x][GRN] = normbright(inl[x]);
416 >                colrs_gambs(inl, xmax);
417 >                x = xmax;
418 >                if (nc < 256)
419 >                        while (x--)
420 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
421 >                else
422 >                        while (x--)
423 >                                *--dp = inl[x][GRN];
424 >        }
425 >        for (x = 0; x < nc; x++)
426 >                clrtab[x][RED] = clrtab[x][GRN] =
427 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
428   }
429  
430  
431   writetarga(h, d, fp)            /* write out targa data */
432 < struct hdStruct  *h;
433 < pixel  *d;
432 > struct hdStruct  *h;
433 > BYTE  *d;
434   FILE  *fp;
435   {
436 +        register int  i, j;
437 +
438 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
439 +                for (j = 2; j >= 0; j--)
440 +                        putc(clrtab[i][j], fp);
441          if (h->dataType == IM_CMAP) {           /* uncompressed */
442 <                if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y)
442 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
443                          quiterr("error writing targa file");
444                  return;
445          }
# Line 429 | Line 448 | FILE  *fp;
448  
449  
450   readtarga(h, data, fp)          /* read in targa data */
451 < struct hdStruct  *h;
452 < pixel  *data;
451 > struct hdStruct  *h;
452 > BYTE  *data;
453   FILE  *fp;
454   {
455          register int  cnt, c;
456 <        register pixel  *dp;
456 >        register BYTE   *dp;
457  
458          if (h->dataType == IM_CMAP) {           /* uncompressed */
459 <                if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y)
459 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
460                          goto readerr;
461                  return;
462          }
# Line 460 | Line 479 | FILE  *fp;
479          return;
480   readerr:
481          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        }
482   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines