ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
(Generate patch)

Comparing ray/src/px/pvalue.c (file contents):
Revision 1.2 by greg, Tue Sep 12 13:04:30 1989 UTC vs.
Revision 2.3 by greg, Fri May 8 10:58:52 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *     4/23/86
11   */
12  
13 < #include  <stdio.h>
13 > #include  "standard.h"
14  
15   #include  "color.h"
16  
17 + #include  "resolu.h"
18 +
19   #define  min(a,b)               ((a)<(b)?(a):(b))
20  
21 < int  xres = 0;                  /* resolution of input */
20 < int  yres = 0;
21 > RESOLU  picres;                 /* resolution of picture */
22  
23 < int  uniq = 0;                  /* unique values? */
23 > int  uniq = 0;                  /* print only unique values? */
24  
25 < int  original = 0;              /* original values? */
25 > int  original = 0;              /* convert back to original values? */
26  
27   int  dataonly = 0;              /* data only format? */
28  
# Line 30 | Line 31 | int  brightonly = 0;           /* only brightness values? */
31   int  reverse = 0;               /* reverse conversion? */
32  
33   int  format = 'a';              /* input/output format */
34 + char  *fmtid = "ascii";         /* format identifier for header */
35  
36 < int  header = 1;                /* do header */
36 > int  header = 1;                /* do header? */
37  
38 < double  exposure = 1.0;
38 > int  resolution = 1;            /* put/get resolution string? */
39  
40 + int  wrongformat = 0;           /* wrong input format? */
41 +
42 + double  gamcor = 1.0;           /* gamma correction */
43 +
44 + COLOR  exposure = WHTCOLOR;
45 +
46   char  *progname;
47  
48   FILE  *fin;
# Line 46 | Line 54 | main(argc, argv)
54   int  argc;
55   char  **argv;
56   {
49        extern double  atof();
57          extern int  checkhead();
58          int  i;
59  
60          progname = argv[0];
61  
62          for (i = 1; i < argc; i++)
63 <                if (argv[i][0] == '-')
63 >                if (argv[i][0] == '-' || argv[i][0] == '+')
64                          switch (argv[i][1]) {
65 <                        case 'h':               /* no header */
66 <                                header = 0;
65 >                        case 'h':               /* header */
66 >                                header = argv[i][0] == '+';
67                                  break;
68 +                        case 'H':               /* resolution string */
69 +                                resolution = argv[i][0] == '+';
70 +                                break;
71                          case 'u':               /* unique values */
72 <                                uniq = 1;
72 >                                uniq = argv[i][0] == '-';
73                                  break;
74                          case 'o':               /* original values */
75 <                                original = 1;
75 >                                original = argv[i][0] == '-';
76                                  break;
77 +                        case 'g':               /* gamma correction */
78 +                                gamcor = atof(argv[++i]);
79 +                                if (argv[i][0] == '+')
80 +                                        gamcor = 1.0/gamcor;
81 +                                break;
82                          case 'r':               /* reverse conversion */
83 <                                reverse = 1;
83 >                                reverse = argv[i][0] == '-';
84                                  break;
85                          case 'b':               /* brightness values */
86 <                                brightonly = 1;
86 >                                brightonly = argv[i][0] == '-';
87                                  break;
88                          case 'd':               /* data only (no indices) */
89 <                                dataonly = 1;
89 >                                dataonly = argv[i][0] == '-';
90                                  switch (argv[i][2]) {
91                                  case '\0':
92                                  case 'a':               /* ascii */
93                                          format = 'a';
94 +                                        fmtid = "ascii";
95                                          break;
96                                  case 'i':               /* integer */
97 +                                        format = 'i';
98 +                                        fmtid = "ascii";
99 +                                        break;
100                                  case 'b':               /* byte */
101 +                                        dataonly = 1;
102 +                                        format = 'b';
103 +                                        fmtid = "byte";
104 +                                        break;
105                                  case 'f':               /* float */
106 +                                        dataonly = 1;
107 +                                        format = 'f';
108 +                                        fmtid = "float";
109 +                                        break;
110                                  case 'd':               /* double */
111 <                                        format = argv[i][2];
111 >                                        dataonly = 1;
112 >                                        format = 'd';
113 >                                        fmtid = "double";
114                                          break;
115                                  default:
116                                          goto unkopt;
117                                  }
118                                  break;
119                          case 'x':               /* x resolution */
120 <                                xres = atoi(argv[++i]);
120 >                                resolution = 0;
121 >                                if (argv[i][0] == '-')
122 >                                        picres.or |= XDECR;
123 >                                picres.xr = atoi(argv[++i]);
124                                  break;
125                          case 'y':               /* y resolution */
126 <                                yres = atoi(argv[++i]);
126 >                                resolution = 0;
127 >                                if (argv[i][0] == '-')
128 >                                        picres.or |= YDECR;
129 >                                if (picres.xr == 0)
130 >                                        picres.or |= YMAJOR;
131 >                                picres.yr = atoi(argv[++i]);
132                                  break;
133                          default:
134   unkopt:
# Line 102 | Line 139 | unkopt:
139                          }
140                  else
141                          break;
142 <                        
142 >                                        /* recognize special formats */
143 >        if (dataonly && format == 'b')
144 >                if (brightonly)
145 >                        fmtid = "8-bit_grey";
146 >                else
147 >                        fmtid = "24-bit_rgb";
148 >
149          if (i == argc) {
150                  fin = stdin;
151          } else if (i == argc-1) {
# Line 119 | Line 162 | unkopt:
162          set_io();
163  
164          if (reverse) {
165 <                if (header)                     /* get header */
166 <                        copyheader(fin, stdout);
167 <                                                /* add to header */
125 <                printargs(i, argv, stdout);
126 <                printf("\n");
127 <                if (yres <= 0 || xres <= 0) {
128 <                        fprintf(stderr, "%s: missing x and y resolution\n",
129 <                                        progname);
165 >                                        /* get header */
166 >                if (header && checkheader(fin, fmtid, stdout) < 0) {
167 >                        fprintf(stderr, "%s: wrong input format\n", progname);
168                          quit(1);
169                  }
170 <                fputresolu(YMAJOR|YDECR, xres, yres, stdout);
170 >                                        /* get resolution */
171 >                if ((resolution && !fgetsresolu(&picres, fin)) ||
172 >                                picres.xr <= 0 || picres.yr <= 0) {
173 >                        fprintf(stderr, "%s: missing resolution\n", progname);
174 >                        quit(1);
175 >                }
176 >                                                /* add to header */
177 >                printargs(i, argv, stdout);
178 >                fputformat(COLRFMT, stdout);
179 >                putchar('\n');
180 >                fputsresolu(&picres, stdout);   /* always put resolution */
181                  valtopix();
182          } else {
183                                                  /* get header */
184 <                getheader(fin, checkhead);
185 <
186 <                if (xres <= 0 || yres <= 0)             /* get picture size */
187 <                        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
188 <                                fprintf(stderr,
189 <                                "%s: missing x and y resolution\n",
190 <                                                progname);
191 <                                quit(1);
192 <                        }
184 >                getheader(fin, checkhead, NULL);
185 >                if (wrongformat) {
186 >                        fprintf(stderr, "%s: input not a Radiance picture\n",
187 >                                        progname);
188 >                        quit(1);
189 >                }
190 >                if (!fgetsresolu(&picres, fin)) {
191 >                        fprintf(stderr, "%s: missing resolution\n", progname);
192 >                        quit(1);
193 >                }
194                  if (header) {
195                          printargs(i, argv, stdout);
196 <                        printf("\n");
196 >                        fputformat(fmtid, stdout);
197 >                        putchar('\n');
198                  }
199 +                if (resolution)                 /* put resolution */
200 +                        fputsresolu(&picres, stdout);
201                  pixtoval();
202          }
203  
# Line 156 | Line 208 | unkopt:
208   checkhead(line)                         /* deal with line from header */
209   char  *line;
210   {
211 <        if (header)
211 >        char    fmt[32];
212 >        double  d;
213 >        COLOR   ctmp;
214 >
215 >        if (isformat(line)) {
216 >                formatval(fmt, line);
217 >                wrongformat = strcmp(fmt, COLRFMT);
218 >        } else if (original && isexpos(line)) {
219 >                d = 1.0/exposval(line);
220 >                scalecolor(exposure, d);
221 >        } else if (original && iscolcor(line)) {
222 >                colcorval(ctmp, line);
223 >                setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
224 >                                colval(exposure,GRN)/colval(ctmp,GRN),
225 >                                colval(exposure,BLU)/colval(ctmp,BLU));
226 >        } else if (header)
227                  fputs(line, stdout);
161        if (!strncmp(line, "EXPOSURE=", 9))
162                exposure *= atof(line+9);
228   }
229  
230  
231   pixtoval()                              /* convert picture to values */
232   {
233 <        COLOR  *scanln;
233 >        register COLOR  *scanln;
234 >        int  dogamma;
235          COLOR  lastc;
236 +        FLOAT  hv[2];
237          int  y;
238          register int  x;
239  
240 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
240 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
241          if (scanln == NULL) {
242                  fprintf(stderr, "%s: out of memory\n", progname);
243                  quit(1);
244          }
245 +        dogamma = gamcor < .95 || gamcor > 1.05;
246          setcolor(lastc, 0.0, 0.0, 0.0);
247 <        for (y = yres-1; y >= 0; y--) {
248 <                if (freadscan(scanln, xres, fin) < 0) {
247 >        for (y = 0; y < numscans(&picres); y++) {
248 >                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
249                          fprintf(stderr, "%s: read error\n", progname);
250                          quit(1);
251                  }
252 <                for (x = 0; x < xres; x++) {
252 >                for (x = 0; x < scanlen(&picres); x++) {
253                          if (uniq)
254 <                                if (    scanln[x][RED] == lastc[RED] &&
255 <                                        scanln[x][GRN] == lastc[GRN] &&
256 <                                        scanln[x][BLU] == lastc[BLU]    )
254 >                                if (    colval(scanln[x],RED) ==
255 >                                                colval(lastc,RED) &&
256 >                                        colval(scanln[x],GRN) ==
257 >                                                colval(lastc,GRN) &&
258 >                                        colval(scanln[x],BLU) ==
259 >                                                colval(lastc,BLU)       )
260                                          continue;
261                                  else
262                                          copycolor(lastc, scanln[x]);
263                          if (original)
264 <                                scalecolor(scanln[x], 1.0/exposure);
265 <                        if (!dataonly)
266 <                                printf("%7d %7d ", x, y);
264 >                                multcolor(scanln[x], exposure);
265 >                        if (dogamma)
266 >                                setcolor(scanln[x],
267 >                                pow(colval(scanln[x],RED), 1.0/gamcor),
268 >                                pow(colval(scanln[x],GRN), 1.0/gamcor),
269 >                                pow(colval(scanln[x],BLU), 1.0/gamcor));
270 >                        if (!dataonly) {
271 >                                pix2loc(hv, &picres, x, y);
272 >                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
273 >                                                (int)(hv[1]*picres.yr));
274 >                        }
275                          if ((*putval)(scanln[x], stdout) < 0) {
276                                  fprintf(stderr, "%s: write error\n", progname);
277                                  quit(1);
# Line 205 | Line 284 | pixtoval()                             /* convert picture to values */
284  
285   valtopix()                      /* convert values to a pixel file */
286   {
287 <        COLOR  *scanln;
287 >        int  dogamma;
288 >        register COLOR  *scanln;
289          int  y;
290          register int  x;
291  
292 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
292 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
293          if (scanln == NULL) {
294                  fprintf(stderr, "%s: out of memory\n", progname);
295                  quit(1);
296          }
297 <        for (y = yres-1; y >= 0; y--) {
298 <                for (x = 0; x < xres; x++) {
297 >        dogamma = gamcor < .95 || gamcor > 1.05;
298 >        for (y = 0; y < numscans(&picres); y++) {
299 >                for (x = 0; x < scanlen(&picres); x++) {
300                          if (!dataonly)
301                                  fscanf(fin, "%*d %*d");
302                          if ((*getval)(scanln[x], fin) < 0) {
303                                  fprintf(stderr, "%s: read error\n", progname);
304                                  quit(1);
305                          }
306 +                        if (dogamma)
307 +                                setcolor(scanln[x],
308 +                                        pow(colval(scanln[x],RED), gamcor),
309 +                                        pow(colval(scanln[x],GRN), gamcor),
310 +                                        pow(colval(scanln[x],BLU), gamcor));
311                  }
312 <                if (fwritescan(scanln, xres, stdout) < 0
227 <                                || fflush(stdout) < 0) {
312 >                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
313                          fprintf(stderr, "%s: write error\n", progname);
314                          quit(1);
315                  }
# Line 259 | Line 344 | FILE  *fp;
344   {
345          double  vd[3];
346  
347 <        if (fread(vd, sizeof(double), 3, fp) != 3)
347 >        if (fread((char *)vd, sizeof(double), 3, fp) != 3)
348                  return(-1);
349          setcolor(col, vd[0], vd[1], vd[2]);
350          return(0);
# Line 272 | Line 357 | FILE  *fp;
357   {
358          float  vf[3];
359  
360 <        if (fread(vf, sizeof(float), 3, fp) != 3)
360 >        if (fread((char *)vf, sizeof(float), 3, fp) != 3)
361                  return(-1);
362          setcolor(col, vf[0], vf[1], vf[2]);
363          return(0);
# Line 298 | Line 383 | FILE  *fp;
383   {
384          BYTE  vb[3];
385  
386 <        if (fread(vb, sizeof(BYTE), 3, fp) != 3)
386 >        if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
387                  return(-1);
388          setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
389          return(0);
# Line 324 | Line 409 | FILE  *fp;
409   {
410          double  vd;
411  
412 <        if (fread(&vd, sizeof(double), 1, fp) != 1)
412 >        if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
413                  return(-1);
414          setcolor(col, vd, vd, vd);
415          return(0);
# Line 337 | Line 422 | FILE  *fp;
422   {
423          float  vf;
424  
425 <        if (fread(&vf, sizeof(float), 1, fp) != 1)
425 >        if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
426                  return(-1);
427          setcolor(col, vf, vf, vf);
428          return(0);
# Line 366 | Line 451 | FILE  *fp;
451          BYTE  vb;
452          double  d;
453  
454 <        if (fread(&vb, sizeof(BYTE), 1, fp) != 1)
454 >        if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
455                  return(-1);
456          d = (vb+.5)/256.;
457          setcolor(col, d, d, d);
# Line 396 | Line 481 | FILE  *fp;
481          vf[0] = colval(col,RED);
482          vf[1] = colval(col,GRN);
483          vf[2] = colval(col,BLU);
484 <        fwrite(vf, sizeof(float), 3, fp);
484 >        fwrite((char *)vf, sizeof(float), 3, fp);
485  
486          return(ferror(fp) ? -1 : 0);
487   }
# Line 411 | Line 496 | FILE  *fp;
496          vd[0] = colval(col,RED);
497          vd[1] = colval(col,GRN);
498          vd[2] = colval(col,BLU);
499 <        fwrite(vd, sizeof(double), 3, fp);
499 >        fwrite((char *)vd, sizeof(double), 3, fp);
500  
501          return(ferror(fp) ? -1 : 0);
502   }
# Line 443 | Line 528 | FILE  *fp;
528          vb[1] = min(i,255);
529          i = colval(col,BLU)*256.;
530          vb[2] = min(i,255);
531 <        fwrite(vb, sizeof(BYTE), 3, fp);
531 >        fwrite((char *)vb, sizeof(BYTE), 3, fp);
532  
533          return(ferror(fp) ? -1 : 0);
534   }
# Line 466 | Line 551 | FILE  *fp;
551          float  vf;
552  
553          vf = bright(col);
554 <        fwrite(&vf, sizeof(float), 1, fp);
554 >        fwrite((char *)&vf, sizeof(float), 1, fp);
555  
556          return(ferror(fp) ? -1 : 0);
557   }
# Line 479 | Line 564 | FILE  *fp;
564          double  vd;
565  
566          vd = bright(col);
567 <        fwrite(&vd, sizeof(double), 1, fp);
567 >        fwrite((char *)&vd, sizeof(double), 1, fp);
568  
569          return(ferror(fp) ? -1 : 0);
570   }
# Line 504 | Line 589 | FILE  *fp;
589  
590          i = bright(col)*256.;
591          vb = min(i,255);
592 <        fwrite(&vb, sizeof(BYTE), 1, fp);
592 >        fwrite((char *)&vb, sizeof(BYTE), 1, fp);
593  
594          return(ferror(fp) ? -1 : 0);
595   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines