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.11 by greg, Fri Aug 23 13:40:01 1991 UTC vs.
Revision 2.9 by greg, Fri Nov 10 17:04:52 1995 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 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 < #ifndef  BSD
23 < #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
24 < extern char  *memcpy();
22 > #ifdef MSDOS
23 > #include  <fcntl.h>
24   #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;
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  
54 double  gamma = 2.2;                    /* gamma correction */
55
50   int  bradj = 0;                         /* brightness adjustment */
51  
58 pic     *inpic;
59
52   char  *progname;
53  
54   char  errmsg[128];
55  
56   COLR    *inl;
57  
58 < pixel   *tarData;
58 > BYTE    *tarData;
59  
60   int  xmax, ymax;
61  
# Line 72 | Line 64 | main(argc, argv)
64   int  argc;
65   char  *argv[];
66   {
67 <        colormap  rasmap;
76 <        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 89 | 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 105 | Line 102 | char  *argv[];
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) {
115                if (i < argc-2)
116                        goto userr;
117                if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
118                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
119                        quiterr(errmsg);
120                }
125                                          /* get header */
126                  if (getthead(&head, NULL, stdin) < 0)
127                          quiterr("bad targa file");
# Line 125 | Line 129 | char  *argv[];
129                          quiterr("incompatible format");
130                  xmax = head.x;
131                  ymax = head.y;
128                                        /* open output file */
129                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
130                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
131                        quiterr(errmsg);
132                }
132                                          /* put header */
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-2 || (!greyscale && i > argc-1))
142 <                        goto userr;
143 <                if ((inpic = openinput(argv[i], &head)) == NULL) {
144 <                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
145 <                        quiterr(errmsg);
146 <                }
147 <                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
148 <                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
149 <                        quiterr(errmsg);
150 <                }
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][-e +/-stops] 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][-e +/-stops] [input [output]]\n",
158 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
159                          progname);
160          exit(1);
161   }
# Line 217 | 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 250 | 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 277 | Line 269 | register FILE  *fp;
269   }
270  
271  
272 < pic *
281 < openinput(fname, h)             /* open RADIANCE input file */
282 < char  *fname;
272 > getrhead(h, fp)                 /* load RADIANCE input file header */
273   register struct hdStruct  *h;
274 + FILE  *fp;
275   {
285        register pic  *p;
286
287        p = (pic *)emalloc(sizeof(pic));
288        p->name = fname;
289        if (fname == NULL)
290                p->fp = stdin;
291        else if ((p->fp = fopen(fname, "r")) == NULL)
292                return(NULL);
276                                          /* get header info. */
277 <        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
278 <                        fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR))
279 <                quiterr("bad picture format");
297 <        p->nexty = 0;
298 <        p->bytes_line = 0;              /* variable length lines */
299 <        p->pos.y = (long *)ecalloc(ymax, sizeof(long));
300 <        p->pos.y[0] = ftell(p->fp);
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 316 | Line 295 | register struct hdStruct  *h;
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 332 | Line 311 | struct hdStruct  *hp;
311          register int  i, j;
312  
313                                          /* get color table */
314 <        if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) :
315 <                        fread((char *)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));
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 */
# Line 367 | 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 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(inl, 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);
390 <        if (freadcolrs(inl, xmax, inpic->fp) < 0)       /* read scanline */
391 <                quiterr("read error in picreadline3");
392 <        inpic->nexty = y+1;
393 <                                                        /* convert scanline */
394 <        normcolrs(inl, xmax, bradj);
395 <        for (i = 0; i < xmax; i++) {
396 <                l3[i].r = inl[i][RED];
397 <                l3[i].g = inl[i][GRN];
398 <                l3[i].b = inl[i][BLU];
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;
405 < pixel  *l;
399 > getgrey(nc)                     /* read in and convert to greyscale image */
400 > int  nc;                /* number of colors to use */
401   {
402 <        bcopy((char *)l, (char *)&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((char *)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 423 | 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((char *)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 454 | Line 479 | FILE  *fp;
479          return;
480   readerr:
481          quiterr("error reading targa file");
457 }
458
459
460 picwritecm(cm)                  /* write out color map */
461 colormap  cm;
462 {
463        register int  i, j;
464
465        for (j = 0; j < 256; j++)
466                for (i = 2; i >= 0; i--)
467                        putc(cm[i][j], stdout);
468 }
469
470
471 picreadcm(map)                  /* do gamma correction if requested */
472 colormap  map;
473 {
474        register int  i, val;
475
476        for (i = 0; i < 256; i++) {
477                val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0;
478                map[0][i] = map[1][i] = map[2][i] = val;
479        }
482   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines