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 2.1 by greg, Tue Nov 12 16:05:11 1991 UTC vs.
Revision 2.8 by greg, Mon Nov 21 15:55:55 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 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 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #include  "resolu.h"
19  
20 #include  "pic.h"
21
20   #include  "targa.h"
21  
22 < #ifndef  BSD
23 < #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
22 > #ifdef MSDOS
23 > #include  <fcntl.h>
24 > #endif
25 >
26 > #include  <math.h>
27 >
28 > #ifndef  BSD
29 > #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
30   extern char  *memcpy();
31   #endif
28                        /* descriptor for a picture file or frame buffer */
29 typedef struct {
30        char    *name;                  /* file name */
31        FILE    *fp;                    /* file pointer */
32        int     nexty;                  /* file positioning */
33        int     bytes_line;             /* 0 == variable length lines */
34        union {
35                long    b;                      /* initial scanline */
36                long    *y;                     /* individual scanline */
37        } pos;                          /* position(s) */
38 } pic;
32  
33 < #define  goodpic(h)     (my_imType(h) && my_mapType(h))
34 < #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
33 > #define  goodpic(h)     (my_imType(h) && my_mapType(h))
34 > #define  my_imType(h)   (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
35                                  && (h)->dataBits==8 && (h)->imType==0)
36 < #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
36 > #define  my_mapType(h)  ((h)->mapType==CM_HASMAP && \
37                                  ((h)->CMapBits==24 || (h)->CMapBits==32))
38  
39 < #define  taralloc(h)    (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel))
39 > #define  taralloc(h)    (BYTE *)emalloc((h)->x*(h)->y)
40  
41 < extern pic      *openinput();
41 > BYTE  clrtab[256][3];
42  
43 + extern int      samplefac;
44 +
45   extern char     *ecalloc(), *emalloc();
46  
47   extern long  ftell();
48  
49 < extern double  atof(), pow();
49 > double  gamv = 2.2;                     /* gamv correction */
50  
56 double  gamma = 2.2;                    /* gamma correction */
57
51   int  bradj = 0;                         /* brightness adjustment */
52  
60 pic     *inpic;
61
53   char  *progname;
54  
55   char  errmsg[128];
56  
57   COLR    *inl;
58  
59 < pixel   *tarData;
59 > BYTE    *tarData;
60  
61   int  xmax, ymax;
62  
# Line 74 | Line 65 | main(argc, argv)
65   int  argc;
66   char  *argv[];
67   {
68 <        colormap  rasmap;
78 <        struct hdStruct  head;
68 >        struct hdStruct  head;
69          int  dither = 1;
70          int  reverse = 0;
71          int  ncolors = 256;
72          int  greyscale = 0;
73          int  i;
74 <        
74 > #ifdef MSDOS
75 >        extern int  _fmode;
76 >        _fmode = O_BINARY;
77 >        setmode(fileno(stdin), O_BINARY);
78 >        setmode(fileno(stdout), O_BINARY);
79 > #endif
80          progname = argv[0];
81 +        samplefac = 0;
82  
83          for (i = 1; i < argc; i++)
84                  if (argv[i][0] == '-')
# Line 91 | Line 87 | char  *argv[];
87                                  dither = !dither;
88                                  break;
89                          case 'g':
90 <                                gamma = atof(argv[++i]);
90 >                                gamv = atof(argv[++i]);
91                                  break;
92                          case 'r':
93                                  reverse = !reverse;
# Line 107 | Line 103 | char  *argv[];
103                          case 'c':
104                                  ncolors = atoi(argv[++i]);
105                                  break;
106 +                        case 'n':
107 +                                samplefac = atoi(argv[++i]);
108 +                                break;
109                          default:
110                                  goto userr;
111                          }
112                  else
113                          break;
114  
115 +        if (i < argc-2)
116 +                goto userr;
117 +        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
118 +                sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
119 +                quiterr(errmsg);
120 +        }
121 +        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
122 +                sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
123 +                quiterr(errmsg);
124 +        }
125          if (reverse) {
117                if (i < argc-2)
118                        goto userr;
119                if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
120                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
121                        quiterr(errmsg);
122                }
126                                          /* get header */
127                  if (getthead(&head, NULL, stdin) < 0)
128                          quiterr("bad targa file");
# Line 127 | Line 130 | char  *argv[];
130                          quiterr("incompatible format");
131                  xmax = head.x;
132                  ymax = head.y;
130                                        /* open output file */
131                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
132                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
133                        quiterr(errmsg);
134                }
133                                          /* put header */
134 +                newheader("RADIANCE", stdout);
135                  printargs(i, argv, stdout);
136                  fputformat(COLRFMT, stdout);
137                  putchar('\n');
# Line 140 | Line 139 | char  *argv[];
139                                          /* convert file */
140                  tg2ra(&head);
141          } else {
142 <                if (i < argc-2 || (!greyscale && i > argc-1))
143 <                        goto userr;
145 <                if ((inpic = openinput(argv[i], &head)) == NULL) {
146 <                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
147 <                        quiterr(errmsg);
148 <                }
149 <                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
150 <                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
151 <                        quiterr(errmsg);
152 <                }
142 >                if (getrhead(&head, stdin) < 0)
143 >                        quiterr("bad Radiance input");
144                                          /* write header */
145                  putthead(&head, NULL, stdout);
146                                          /* convert file */
147                  if (greyscale)
148 <                        biq(dither,ncolors,1,rasmap);
148 >                        getgrey(ncolors);
149                  else
150 <                        ciq(dither,ncolors,1,rasmap);
150 >                        getmapped(ncolors, dither);
151                                          /* write data */
152                  writetarga(&head, tarData, stdout);
153          }
154          quiterr(NULL);
155   userr:
156          fprintf(stderr,
157 <        "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n",
157 >        "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
158                          progname);
159 <        fprintf(stderr, "   Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n",
159 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
160                          progname);
161          exit(1);
162   }
# Line 219 | Line 210 | int code;
210  
211  
212   getthead(hp, ip, fp)            /* read header from input */
213 < struct hdStruct  *hp;
213 > struct hdStruct  *hp;
214   char  *ip;
215   register FILE  *fp;
216   {
# Line 252 | Line 243 | register FILE  *fp;
243  
244  
245   putthead(hp, ip, fp)            /* write header to output */
246 < struct hdStruct  *hp;
246 > struct hdStruct  *hp;
247   char  *ip;
248   register FILE  *fp;
249   {
# Line 279 | Line 270 | register FILE  *fp;
270   }
271  
272  
273 < pic *
283 < openinput(fname, h)             /* open RADIANCE input file */
284 < char  *fname;
273 > getrhead(h, fp)                 /* load RADIANCE input file header */
274   register struct hdStruct  *h;
275 + FILE  *fp;
276   {
287        register pic  *p;
288
289        p = (pic *)emalloc(sizeof(pic));
290        p->name = fname;
291        if (fname == NULL)
292                p->fp = stdin;
293        else if ((p->fp = fopen(fname, "r")) == NULL)
294                return(NULL);
277                                          /* get header info. */
278 <        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
279 <                        fgetresolu(&xmax, &ymax, p->fp) < 0)
280 <                quiterr("bad picture format");
299 <        p->nexty = 0;
300 <        p->bytes_line = 0;              /* variable length lines */
301 <        p->pos.y = (long *)ecalloc(ymax, sizeof(long));
302 <        p->pos.y[0] = ftell(p->fp);
278 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
279 >                        fgetresolu(&xmax, &ymax, fp) < 0)
280 >                return(-1);
281                                          /* assign header */
282          h->textSize = 0;
283          h->mapType = CM_HASMAP;
# Line 318 | Line 296 | register struct hdStruct  *h;
296                                          /* allocate targa data */
297          tarData = taralloc(h);
298  
299 <        return(p);
299 >        return(0);
300   }
301  
302  
303   tg2ra(hp)                       /* targa file to RADIANCE file */
304 < struct hdStruct  *hp;
304 > struct hdStruct  *hp;
305   {
306          union {
307                  BYTE  c3[256][3];
# Line 343 | Line 321 | struct hdStruct  *hp;
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));
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 */
# Line 371 | Line 349 | struct hdStruct  *hp;
349   }
350  
351  
352 < picreadline3(y, l3)                     /* read in 3-byte scanline */
353 < int  y;
354 < register rgbpixel  *l3;
352 > getmapped(nc, dith)             /* read in and quantize image */
353 > int  nc;                /* number of colors to use */
354 > int  dith;              /* use dithering? */
355   {
356 <        register int    i;
356 >        long  fpos;
357 >        register int  y;
358  
359 <        if (inpic->nexty != y) {                        /* find scanline */
360 <                if (inpic->bytes_line == 0) {
361 <                        if (inpic->pos.y[y] == 0) {
362 <                                while (inpic->nexty < y) {
363 <                                        if (freadcolrs(inl, xmax, inpic->fp) < 0)
364 <                                                quiterr("read error in picreadline3");
365 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
366 <                                }
367 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
368 <                                quiterr("seek error in picreadline3");
369 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
370 <                        quiterr("seek error in picreadline3");
371 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
372 <                inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
394 <        if (freadcolrs(inl, xmax, inpic->fp) < 0)       /* read scanline */
395 <                quiterr("read error in picreadline3");
396 <        inpic->nexty = y+1;
397 <                                                        /* convert scanline */
398 <        normcolrs(inl, xmax, bradj);
399 <        for (i = 0; i < xmax; i++) {
400 <                l3[i].r = inl[i][RED];
401 <                l3[i].g = inl[i][GRN];
402 <                l3[i].b = inl[i][BLU];
359 >        setcolrgam(gamv);
360 >        fpos = ftell(stdin);
361 >        if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
362 >                quiterr("cannot initialized histogram");
363 >        for (y = ymax-1; y >= 0; y--) {
364 >                if (freadcolrs(inl, xmax, stdin) < 0)
365 >                        quiterr("error reading Radiance input");
366 >                if (bradj)
367 >                        shiftcolrs(inl, xmax, bradj);
368 >                colrs_gambs(inl, xmax);
369 >                if (samplefac)
370 >                        neu_colrs(inl, xmax);
371 >                else
372 >                        cnt_colrs(inl, xmax);
373          }
374 +        if (fseek(stdin, fpos, 0) == EOF)
375 +                quiterr("Radiance input must be from a file");
376 +        if (samplefac)                  /* map colors */
377 +                neu_clrtab(nc);
378 +        else
379 +                new_clrtab(nc);
380 +        for (y = ymax-1; y >= 0; y--) {
381 +                if (freadcolrs(inl, xmax, stdin) < 0)
382 +                        quiterr("error reading Radiance input");
383 +                if (bradj)
384 +                        shiftcolrs(inl, xmax, bradj);
385 +                colrs_gambs(inl, xmax);
386 +                if (samplefac)
387 +                        if (dith)
388 +                                neu_dith_colrs(tarData+y*xmax, inl, xmax);
389 +                        else
390 +                                neu_map_colrs(tarData+y*xmax, inl, xmax);
391 +                else
392 +                        if (dith)
393 +                                dith_colrs(tarData+y*xmax, inl, xmax);
394 +                        else
395 +                                map_colrs(tarData+y*xmax, inl, xmax);
396 +        }
397   }
398  
399  
400 < picwriteline(y, l)                      /* save output scanline */
401 < int  y;
409 < pixel  *l;
400 > getgrey(nc)                     /* read in and convert to greyscale image */
401 > int  nc;                /* number of colors to use */
402   {
403 <        bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
403 >        int  y;
404 >        register BYTE  *dp;
405 >        register int  x;
406 >
407 >        setcolrgam(gamv);
408 >        dp = tarData+xmax*ymax;;
409 >        for (y = ymax-1; y >= 0; y--) {
410 >                if (freadcolrs(inl, xmax, stdin) < 0)
411 >                        quiterr("error reading Radiance input");
412 >                if (bradj)
413 >                        shiftcolrs(inl, xmax, bradj);
414 >                x = xmax;
415 >                while (x--)
416 >                        inl[x][GRN] = normbright(inl[x]);
417 >                colrs_gambs(inl, xmax);
418 >                x = xmax;
419 >                if (nc < 256)
420 >                        while (x--)
421 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
422 >                else
423 >                        while (x--)
424 >                                *--dp = inl[x][GRN];
425 >        }
426 >        for (x = 0; x < nc; x++)
427 >                clrtab[x][RED] = clrtab[x][GRN] =
428 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
429   }
430  
431  
432   writetarga(h, d, fp)            /* write out targa data */
433 < struct hdStruct  *h;
434 < pixel  *d;
433 > struct hdStruct  *h;
434 > BYTE  *d;
435   FILE  *fp;
436   {
437 +        register int  i, j;
438 +
439 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
440 +                for (j = 2; j >= 0; j--)
441 +                        putc(clrtab[i][j], fp);
442          if (h->dataType == IM_CMAP) {           /* uncompressed */
443 <                if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y)
443 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
444                          quiterr("error writing targa file");
445                  return;
446          }
# Line 427 | Line 449 | FILE  *fp;
449  
450  
451   readtarga(h, data, fp)          /* read in targa data */
452 < struct hdStruct  *h;
453 < pixel  *data;
452 > struct hdStruct  *h;
453 > BYTE  *data;
454   FILE  *fp;
455   {
456          register int  cnt, c;
457 <        register pixel  *dp;
457 >        register BYTE   *dp;
458  
459          if (h->dataType == IM_CMAP) {           /* uncompressed */
460 <                if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y)
460 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
461                          goto readerr;
462                  return;
463          }
# Line 458 | Line 480 | FILE  *fp;
480          return;
481   readerr:
482          quiterr("error reading targa file");
461 }
462
463
464 picwritecm(cm)                  /* write out color map */
465 colormap  cm;
466 {
467        register int  i, j;
468
469        for (j = 0; j < 256; j++)
470                for (i = 2; i >= 0; i--)
471                        putc(cm[i][j], stdout);
472 }
473
474
475 picreadcm(map)                  /* do gamma correction if requested */
476 colormap  map;
477 {
478        register int  i, val;
479
480        for (i = 0; i < 256; i++) {
481                val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0;
482                map[0][i] = map[1][i] = map[2][i] = val;
483        }
483   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines