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.12 by greg, Mon Nov 11 14:01:53 1991 UTC vs.
Revision 2.6 by greg, Sun Feb 27 10:17:23 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 > extern BYTE  clrtab[][3];
42  
43   extern char     *ecalloc(), *emalloc();
44  
45   extern long  ftell();
46  
47 < extern double  atof(), pow();
47 > double  gamv = 2.2;                     /* gamv correction */
48  
56 double  gamma = 2.2;                    /* gamma correction */
57
49   int  bradj = 0;                         /* brightness adjustment */
50  
60 pic     *inpic;
61
51   char  *progname;
52  
53   char  errmsg[128];
54  
55   COLR    *inl;
56  
57 < pixel   *tarData;
57 > BYTE    *tarData;
58  
59   int  xmax, ymax;
60  
# Line 74 | Line 63 | main(argc, argv)
63   int  argc;
64   char  *argv[];
65   {
66 <        colormap  rasmap;
78 <        struct hdStruct  head;
66 >        struct hdStruct  head;
67          int  dither = 1;
68          int  reverse = 0;
69          int  ncolors = 256;
70          int  greyscale = 0;
71          int  i;
72 <        
72 > #ifdef MSDOS
73 >        extern int  _fmode;
74 >        _fmode = O_BINARY;
75 >        setmode(fileno(stdin), O_BINARY);
76 >        setmode(fileno(stdout), O_BINARY);
77 > #endif
78          progname = argv[0];
79  
80          for (i = 1; i < argc; i++)
# Line 91 | Line 84 | char  *argv[];
84                                  dither = !dither;
85                                  break;
86                          case 'g':
87 <                                gamma = atof(argv[++i]);
87 >                                gamv = atof(argv[++i]);
88                                  break;
89                          case 'r':
90                                  reverse = !reverse;
# Line 113 | Line 106 | char  *argv[];
106                  else
107                          break;
108  
109 +        if (i < argc-2)
110 +                goto userr;
111 +        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
112 +                sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
113 +                quiterr(errmsg);
114 +        }
115 +        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
116 +                sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
117 +                quiterr(errmsg);
118 +        }
119          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                }
120                                          /* get header */
121                  if (getthead(&head, NULL, stdin) < 0)
122                          quiterr("bad targa file");
# Line 127 | Line 124 | char  *argv[];
124                          quiterr("incompatible format");
125                  xmax = head.x;
126                  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                }
127                                          /* put header */
128 +                newheader("RADIANCE", stdout);
129                  printargs(i, argv, stdout);
130                  fputformat(COLRFMT, stdout);
131                  putchar('\n');
# Line 140 | Line 133 | char  *argv[];
133                                          /* convert file */
134                  tg2ra(&head);
135          } else {
136 <                if (i < argc-2 || (!greyscale && i > argc-1))
137 <                        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 <                }
136 >                if (getrhead(&head, stdin) < 0)
137 >                        quiterr("bad Radiance input");
138                                          /* write header */
139                  putthead(&head, NULL, stdout);
140                                          /* convert file */
141                  if (greyscale)
142 <                        biq(dither,ncolors,1,rasmap);
142 >                        getgrey(ncolors);
143                  else
144 <                        ciq(dither,ncolors,1,rasmap);
144 >                        getmapped(ncolors, dither);
145                                          /* write data */
146                  writetarga(&head, tarData, stdout);
147          }
148          quiterr(NULL);
149   userr:
150          fprintf(stderr,
151 <        "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n",
151 >        "Usage: %s [-d][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
152                          progname);
153 <        fprintf(stderr, "   Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n",
153 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
154                          progname);
155          exit(1);
156   }
# Line 219 | Line 204 | int code;
204  
205  
206   getthead(hp, ip, fp)            /* read header from input */
207 < struct hdStruct  *hp;
207 > struct hdStruct  *hp;
208   char  *ip;
209   register FILE  *fp;
210   {
# Line 252 | Line 237 | register FILE  *fp;
237  
238  
239   putthead(hp, ip, fp)            /* write header to output */
240 < struct hdStruct  *hp;
240 > struct hdStruct  *hp;
241   char  *ip;
242   register FILE  *fp;
243   {
# Line 279 | Line 264 | register FILE  *fp;
264   }
265  
266  
267 < pic *
283 < openinput(fname, h)             /* open RADIANCE input file */
284 < char  *fname;
267 > getrhead(h, fp)                 /* load RADIANCE input file header */
268   register struct hdStruct  *h;
269 + FILE  *fp;
270   {
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);
271                                          /* get header info. */
272 <        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
273 <                        fgetresolu(&xmax, &ymax, p->fp) < 0)
274 <                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);
272 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
273 >                        fgetresolu(&xmax, &ymax, fp) < 0)
274 >                return(-1);
275                                          /* assign header */
276          h->textSize = 0;
277          h->mapType = CM_HASMAP;
# Line 318 | Line 290 | register struct hdStruct  *h;
290                                          /* allocate targa data */
291          tarData = taralloc(h);
292  
293 <        return(p);
293 >        return(0);
294   }
295  
296  
297   tg2ra(hp)                       /* targa file to RADIANCE file */
298 < struct hdStruct  *hp;
298 > struct hdStruct  *hp;
299   {
300          union {
301                  BYTE  c3[256][3];
# Line 343 | Line 315 | struct hdStruct  *hp;
315          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
316                  if (hp->CMapBits == 24)
317                          setcolr(ctab[i],
318 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
319 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
320 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
318 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
319 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
320 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
321                  else
322                          setcolr(ctab[i],
323 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
324 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
325 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
323 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
324 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
325 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
326          if (bradj)
327                  shiftcolrs(ctab, 256, bradj);
328                                          /* allocate targa data */
# Line 371 | Line 343 | struct hdStruct  *hp;
343   }
344  
345  
346 < picreadline3(y, l3)                     /* read in 3-byte scanline */
347 < int  y;
348 < register rgbpixel  *l3;
346 > getmapped(nc, dith)             /* read in and quantize image */
347 > int  nc;                /* number of colors to use */
348 > int  dith;              /* use dithering? */
349   {
350 <        register int    i;
350 >        long  fpos;
351 >        register int  y;
352  
353 <        if (inpic->nexty != y) {                        /* find scanline */
354 <                if (inpic->bytes_line == 0) {
355 <                        if (inpic->pos.y[y] == 0) {
356 <                                while (inpic->nexty < y) {
357 <                                        if (freadcolrs(inl, xmax, inpic->fp) < 0)
358 <                                                quiterr("read error in picreadline3");
359 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
360 <                                }
361 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
362 <                                quiterr("seek error in picreadline3");
390 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
391 <                        quiterr("seek error in picreadline3");
392 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
393 <                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];
353 >        setcolrgam(gamv);
354 >        fpos = ftell(stdin);
355 >        new_histo();                    /* build histogram */
356 >        for (y = ymax-1; y >= 0; y--) {
357 >                if (freadcolrs(inl, xmax, stdin) < 0)
358 >                        quiterr("error reading Radiance input");
359 >                if (bradj)
360 >                        shiftcolrs(inl, xmax, bradj);
361 >                colrs_gambs(inl, xmax);
362 >                cnt_colrs(inl, xmax);
363          }
364 +        if (fseek(stdin, fpos, 0) == EOF)
365 +                quiterr("Radiance input must be from a file");
366 +        new_clrtab(nc);                 /* map colors */
367 +        for (y = ymax-1; y >= 0; y--) {
368 +                if (freadcolrs(inl, xmax, stdin) < 0)
369 +                        quiterr("error reading Radiance input");
370 +                if (bradj)
371 +                        shiftcolrs(inl, xmax, bradj);
372 +                colrs_gambs(inl, xmax);
373 +                if (dith)
374 +                        dith_colrs(tarData+y*xmax, inl, xmax);
375 +                else
376 +                        map_colrs(tarData+y*xmax, inl, xmax);
377 +        }
378   }
379  
380  
381 < picwriteline(y, l)                      /* save output scanline */
382 < int  y;
409 < pixel  *l;
381 > getgrey(nc)                     /* read in and convert to greyscale image */
382 > int  nc;                /* number of colors to use */
383   {
384 <        bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
384 >        int  y;
385 >        register BYTE  *dp;
386 >        register int  x;
387 >
388 >        setcolrgam(gamv);
389 >        dp = tarData+xmax*ymax;;
390 >        for (y = ymax-1; y >= 0; y--) {
391 >                if (freadcolrs(inl, xmax, stdin) < 0)
392 >                        quiterr("error reading Radiance input");
393 >                if (bradj)
394 >                        shiftcolrs(inl, xmax, bradj);
395 >                x = xmax;
396 >                while (x--)
397 >                        inl[x][GRN] = normbright(inl[x]);
398 >                colrs_gambs(inl, xmax);
399 >                x = xmax;
400 >                if (nc < 256)
401 >                        while (x--)
402 >                                *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
403 >                else
404 >                        while (x--)
405 >                                *--dp = inl[x][GRN];
406 >        }
407 >        for (x = 0; x < nc; x++)
408 >                clrtab[x][RED] = clrtab[x][GRN] =
409 >                        clrtab[x][BLU] = ((long)x*256+128)/nc;
410   }
411  
412  
413   writetarga(h, d, fp)            /* write out targa data */
414 < struct hdStruct  *h;
415 < pixel  *d;
414 > struct hdStruct  *h;
415 > BYTE  *d;
416   FILE  *fp;
417   {
418 +        register int  i, j;
419 +
420 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
421 +                for (j = 2; j >= 0; j--)
422 +                        putc(clrtab[i][j], fp);
423          if (h->dataType == IM_CMAP) {           /* uncompressed */
424 <                if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y)
424 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
425                          quiterr("error writing targa file");
426                  return;
427          }
# Line 427 | Line 430 | FILE  *fp;
430  
431  
432   readtarga(h, data, fp)          /* read in targa data */
433 < struct hdStruct  *h;
434 < pixel  *data;
433 > struct hdStruct  *h;
434 > BYTE  *data;
435   FILE  *fp;
436   {
437          register int  cnt, c;
438 <        register pixel  *dp;
438 >        register BYTE   *dp;
439  
440          if (h->dataType == IM_CMAP) {           /* uncompressed */
441 <                if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y)
441 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
442                          goto readerr;
443                  return;
444          }
# Line 458 | Line 461 | FILE  *fp;
461          return;
462   readerr:
463          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        }
464   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines