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.3 by greg, Fri Oct 9 15:24:21 1992 UTC vs.
Revision 2.4 by greg, Tue Oct 13 11:36:13 1992 UTC

# 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   #ifdef MSDOS
# Line 31 | Line 29 | static char SCCSid[] = "$SunId$ LBL";
29   #define  bcopy(s,d,n)           (void)memcpy(d,s,n)
30   extern char  *memcpy();
31   #endif
34                        /* descriptor for a picture file or frame buffer */
35 typedef struct {
36        char    *name;                  /* file name */
37        FILE    *fp;                    /* file pointer */
38        int     nexty;                  /* file positioning */
39        int     bytes_line;             /* 0 == variable length lines */
40        union {
41                long    b;                      /* initial scanline */
42                long    *y;                     /* individual scanline */
43        } pos;                          /* position(s) */
44 } 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) \
# Line 49 | Line 36 | typedef struct {
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 < double  gamma = 2.2;                    /* gamma correction */
47 > double  gamv = 2.2;                     /* gamv correction */
48  
49   int  bradj = 0;                         /* brightness adjustment */
50  
64 pic     *inpic;
65
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 78 | Line 63 | main(argc, argv)
63   int  argc;
64   char  *argv[];
65   {
81        colormap  rasmap;
66          struct hdStruct  head;
67          int  dither = 1;
68          int  reverse = 0;
# Line 100 | 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 122 | 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) {
126                if (i < argc-2)
127                        goto userr;
128                if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
129                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
130                        quiterr(errmsg);
131                }
120                                          /* get header */
121                  if (getthead(&head, NULL, stdin) < 0)
122                          quiterr("bad targa file");
# Line 136 | Line 124 | char  *argv[];
124                          quiterr("incompatible format");
125                  xmax = head.x;
126                  ymax = head.y;
139                                        /* open output file */
140                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
141                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
142                        quiterr(errmsg);
143                }
127                                          /* put header */
128                  printargs(i, argv, stdout);
129                  fputformat(COLRFMT, stdout);
# Line 149 | Line 132 | char  *argv[];
132                                          /* convert file */
133                  tg2ra(&head);
134          } else {
135 <                if (i < argc-2 || (!greyscale && i > argc-1))
136 <                        goto userr;
154 <                if ((inpic = openinput(argv[i], &head)) == NULL) {
155 <                        sprintf(errmsg, "can't open input \"%s\"", argv[i]);
156 <                        quiterr(errmsg);
157 <                }
158 <                if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
159 <                        sprintf(errmsg, "can't open output \"%s\"", argv[i+1]);
160 <                        quiterr(errmsg);
161 <                }
135 >                if (getrhead(&head, stdin) < 0)
136 >                        quiterr("bad Radiance input");
137                                          /* write header */
138                  putthead(&head, NULL, stdout);
139                                          /* convert file */
140                  if (greyscale)
141 <                        biq(dither,ncolors,1,rasmap);
141 >                        getgrey(ncolors);
142                  else
143 <                        ciq(dither,ncolors,1,rasmap);
143 >                        getmapped(ncolors, dither);
144                                          /* write data */
145                  writetarga(&head, tarData, stdout);
146          }
147          quiterr(NULL);
148   userr:
149          fprintf(stderr,
150 <        "Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n",
150 >        "Usage: %s [-d][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
151                          progname);
152 <        fprintf(stderr, "   Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n",
152 >        fprintf(stderr, "   Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
153                          progname);
154          exit(1);
155   }
# Line 288 | Line 263 | register FILE  *fp;
263   }
264  
265  
266 < pic *
292 < openinput(fname, h)             /* open RADIANCE input file */
293 < char  *fname;
266 > getrhead(h, fp)                 /* load RADIANCE input file header */
267   register struct hdStruct  *h;
268 + FILE  *fp;
269   {
296        register pic  *p;
297
298        p = (pic *)emalloc(sizeof(pic));
299        p->name = fname;
300        if (fname == NULL)
301                p->fp = stdin;
302        else if ((p->fp = fopen(fname, "r")) == NULL)
303                return(NULL);
270                                          /* get header info. */
271 <        if (checkheader(p->fp, COLRFMT, NULL) < 0 ||
272 <                        fgetresolu(&xmax, &ymax, p->fp) < 0)
273 <                quiterr("bad picture format");
308 <        p->nexty = 0;
309 <        p->bytes_line = 0;              /* variable length lines */
310 <        p->pos.y = (long *)ecalloc(ymax, sizeof(long));
311 <        p->pos.y[0] = ftell(p->fp);
271 >        if (checkheader(fp, COLRFMT, NULL) < 0 ||
272 >                        fgetresolu(&xmax, &ymax, fp) < 0)
273 >                return(-1);
274                                          /* assign header */
275          h->textSize = 0;
276          h->mapType = CM_HASMAP;
# Line 327 | Line 289 | register struct hdStruct  *h;
289                                          /* allocate targa data */
290          tarData = taralloc(h);
291  
292 <        return(p);
292 >        return(0);
293   }
294  
295  
# Line 352 | Line 314 | struct hdStruct         *hp;
314          for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
315                  if (hp->CMapBits == 24)
316                          setcolr(ctab[i],
317 <                                        pow((map.c3[i][2]+.5)/256.,gamma),
318 <                                        pow((map.c3[i][1]+.5)/256.,gamma),
319 <                                        pow((map.c3[i][0]+.5)/256.,gamma));
317 >                                        pow((map.c3[i][2]+.5)/256.,gamv),
318 >                                        pow((map.c3[i][1]+.5)/256.,gamv),
319 >                                        pow((map.c3[i][0]+.5)/256.,gamv));
320                  else
321                          setcolr(ctab[i],
322 <                                        pow((map.c4[i][3]+.5)/256.,gamma),
323 <                                        pow((map.c4[i][2]+.5)/256.,gamma),
324 <                                        pow((map.c4[i][1]+.5)/256.,gamma));
322 >                                        pow((map.c4[i][3]+.5)/256.,gamv),
323 >                                        pow((map.c4[i][2]+.5)/256.,gamv),
324 >                                        pow((map.c4[i][1]+.5)/256.,gamv));
325          if (bradj)
326                  shiftcolrs(ctab, 256, bradj);
327                                          /* allocate targa data */
# Line 380 | Line 342 | struct hdStruct         *hp;
342   }
343  
344  
345 < picreadline3(y, l3)                     /* read in 3-byte scanline */
346 < int  y;
347 < register rgbpixel  *l3;
345 > getmapped(nc, dith)             /* read in and quantize image */
346 > int  nc;                /* number of colors to use */
347 > int  dith;              /* use dithering? */
348   {
349 <        register int    i;
349 >        long  fpos;
350 >        register int  y;
351  
352 <        if (inpic->nexty != y) {                        /* find scanline */
353 <                if (inpic->bytes_line == 0) {
354 <                        if (inpic->pos.y[y] == 0) {
355 <                                while (inpic->nexty < y) {
356 <                                        if (freadcolrs(inl, xmax, inpic->fp) < 0)
357 <                                                quiterr("read error in picreadline3");
358 <                                        inpic->pos.y[++inpic->nexty] = ftell(inpic->fp);
359 <                                }
360 <                        } else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF)
361 <                                quiterr("seek error in picreadline3");
399 <                } else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF)
400 <                        quiterr("seek error in picreadline3");
401 <        } else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0)
402 <                inpic->pos.y[inpic->nexty] = ftell(inpic->fp);
403 <        if (freadcolrs(inl, xmax, inpic->fp) < 0)       /* read scanline */
404 <                quiterr("read error in picreadline3");
405 <        inpic->nexty = y+1;
406 <                                                        /* convert scanline */
407 <        normcolrs(inl, xmax, bradj);
408 <        for (i = 0; i < xmax; i++) {
409 <                l3[i].r = inl[i][RED];
410 <                l3[i].g = inl[i][GRN];
411 <                l3[i].b = inl[i][BLU];
352 >        setcolrgam(gamv);
353 >        fpos = ftell(stdin);
354 >        new_histo();                    /* build histogram */
355 >        for (y = ymax-1; y >= 0; y--) {
356 >                if (freadcolrs(inl, xmax, stdin) < 0)
357 >                        quiterr("error reading Radiance input");
358 >                if (bradj)
359 >                        shiftcolrs(inl, xmax, bradj);
360 >                colrs_gambs(inl, xmax);
361 >                cnt_colrs(inl, xmax);
362          }
363 +        if (fseek(stdin, fpos, 0) == EOF)
364 +                quiterr("Radiance input must be from a file");
365 +        new_clrtab(nc);                 /* map colors */
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 (dith)
373 +                        dith_colrs(tarData+y*xmax, inl, xmax);
374 +                else
375 +                        map_colrs(tarData+y*xmax, inl, xmax);
376 +        }
377   }
378  
379  
380 < picwriteline(y, l)                      /* save output scanline */
381 < int  y;
418 < pixel  *l;
380 > getgrey(nc)                     /* read in and convert to greyscale image */
381 > int  nc;                /* number of colors to use */
382   {
383 <        bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel));
383 >        int  y;
384 >        register BYTE  *dp;
385 >        register int  x;
386 >
387 >        setcolrgam(gamv);
388 >        dp = tarData+xmax*ymax;;
389 >        for (y = ymax-1; y >= 0; y--) {
390 >                if (freadcolrs(inl, xmax, stdin) < 0)
391 >                        quiterr("error reading Radiance input");
392 >                if (bradj)
393 >                        shiftcolrs(inl, xmax, bradj);
394 >                colrs_gambs(inl, xmax);
395 >                x = xmax;
396 >                if (nc < 256)
397 >                        while (x--)
398 >                                *--dp = ((long)normbright(inl[x])*nc+128)>>8;
399 >                else
400 >                        while (x--)
401 >                                *--dp = normbright(inl[x]);
402 >        }
403 >        for (x = 0; x < nc; x++)
404 >                clrtab[x][RED] = clrtab[x][GRN] =
405 >                        clrtab[x][BLU] = ((long)x*256+nc/2)/nc;
406   }
407  
408  
409   writetarga(h, d, fp)            /* write out targa data */
410   struct hdStruct  *h;
411 < pixel  *d;
411 > BYTE  *d;
412   FILE  *fp;
413   {
414 +        register int  i, j;
415 +
416 +        for (i = 0; i < h->mapLength; i++)      /* write color map */
417 +                for (j = 2; j >= 0; j--)
418 +                        putc(clrtab[i][j], fp);
419          if (h->dataType == IM_CMAP) {           /* uncompressed */
420 <                if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y)
420 >                if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
421                          quiterr("error writing targa file");
422                  return;
423          }
# Line 437 | Line 427 | FILE  *fp;
427  
428   readtarga(h, data, fp)          /* read in targa data */
429   struct hdStruct  *h;
430 < pixel  *data;
430 > BYTE  *data;
431   FILE  *fp;
432   {
433          register int  cnt, c;
434 <        register pixel  *dp;
434 >        register BYTE   *dp;
435  
436          if (h->dataType == IM_CMAP) {           /* uncompressed */
437 <                if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y)
437 >                if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
438                          goto readerr;
439                  return;
440          }
# Line 467 | Line 457 | FILE  *fp;
457          return;
458   readerr:
459          quiterr("error reading targa file");
470 }
471
472
473 picwritecm(cm)                  /* write out color map */
474 colormap  cm;
475 {
476        register int  i, j;
477
478        for (j = 0; j < 256; j++)
479                for (i = 2; i >= 0; i--)
480                        putc(cm[i][j], stdout);
481 }
482
483
484 picreadcm(map)                  /* do gamma correction if requested */
485 colormap  map;
486 {
487        register int  i, val;
488
489        for (i = 0; i < 256; i++) {
490                val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0;
491                map[0][i] = map[1][i] = map[2][i] = val;
492        }
460   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines