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.5 by greg, Fri Dec 14 16:33:19 1990 UTC vs.
Revision 2.9 by greg, Thu Oct 6 13:27:16 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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 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 < #define  min(a,b)               ((a)<(b)?(a):(b))
17 > #include  "resolu.h"
18  
19 < int  xres = 0;                  /* resolution of input */
20 < int  yres = 0;
19 > #define  min(a,b)               ((a)<(b)?(a):(b))
20  
21 < int  uniq = 0;                  /* unique values? */
21 > RESOLU  picres;                 /* resolution of picture */
22  
23 < int  original = 0;              /* original values? */
23 > int  uniq = 0;                  /* print only unique values? */
24  
25 + int  doexposure = 0;            /* exposure change? (>100 to print) */
26 +
27   int  dataonly = 0;              /* data only format? */
28  
29   int  brightonly = 0;            /* only brightness values? */
# 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 + int  resolution = 1;            /* put/get resolution string? */
39 +
40 + int  original = 0;              /* convert to original values? */
41 +
42 + int  wrongformat = 0;           /* wrong input format? */
43 +
44 + double  gamcor = 1.0;           /* gamma correction */
45 +
46 + int  ord[3] = {RED, GRN, BLU};  /* RGB ordering */
47 + int  rord[4];                   /* reverse ordering */
48 +
49   COLOR  exposure = WHTCOLOR;
50  
51   char  *progname;
# Line 46 | Line 59 | main(argc, argv)
59   int  argc;
60   char  **argv;
61   {
49        extern double  atof();
62          extern int  checkhead();
63 +        double  d, expval = 1.0;
64          int  i;
65  
66          progname = argv[0];
67  
68          for (i = 1; i < argc; i++)
69 <                if (argv[i][0] == '-')
69 >                if (argv[i][0] == '-' || argv[i][0] == '+')
70                          switch (argv[i][1]) {
71 <                        case 'h':               /* no header */
72 <                                header = 0;
71 >                        case 'h':               /* header */
72 >                                header = argv[i][0] == '+';
73                                  break;
74 +                        case 'H':               /* resolution string */
75 +                                resolution = argv[i][0] == '+';
76 +                                break;
77                          case 'u':               /* unique values */
78 <                                uniq = 1;
78 >                                uniq = argv[i][0] == '-';
79                                  break;
80                          case 'o':               /* original values */
81 <                                original = 1;
81 >                                original = argv[i][0] == '-';
82                                  break;
83 +                        case 'g':               /* gamma correction */
84 +                                gamcor = atof(argv[i+1]);
85 +                                if (argv[i][0] == '+')
86 +                                        gamcor = 1.0/gamcor;
87 +                                i++;
88 +                                break;
89 +                        case 'e':               /* exposure correction */
90 +                                d = atof(argv[i+1]);
91 +                                if (argv[i+1][0] == '-' || argv[i+1][0] == '+')
92 +                                        d = pow(2.0, d);
93 +                                if (argv[i][0] == '-')
94 +                                        expval *= d;
95 +                                scalecolor(exposure, d);
96 +                                doexposure++;
97 +                                i++;
98 +                                break;
99 +                        case 'R':               /* reverse byte sequence */
100 +                                if (argv[i][0] == '-') {
101 +                                        ord[0]=BLU; ord[1]=GRN; ord[2]=RED;
102 +                                } else {
103 +                                        ord[0]=RED; ord[1]=GRN; ord[2]=BLU;
104 +                                }
105 +                                break;
106                          case 'r':               /* reverse conversion */
107 <                                reverse = 1;
107 >                                reverse = argv[i][0] == '-';
108                                  break;
109                          case 'b':               /* brightness values */
110 <                                brightonly = 1;
110 >                                brightonly = argv[i][0] == '-';
111                                  break;
112                          case 'd':               /* data only (no indices) */
113 <                                dataonly = 1;
113 >                                dataonly = argv[i][0] == '-';
114                                  switch (argv[i][2]) {
115                                  case '\0':
116                                  case 'a':               /* ascii */
117                                          format = 'a';
118 +                                        fmtid = "ascii";
119                                          break;
120                                  case 'i':               /* integer */
121 +                                        format = 'i';
122 +                                        fmtid = "ascii";
123 +                                        break;
124                                  case 'b':               /* byte */
125 +                                        dataonly = 1;
126 +                                        format = 'b';
127 +                                        fmtid = "byte";
128 +                                        break;
129                                  case 'f':               /* float */
130 +                                        dataonly = 1;
131 +                                        format = 'f';
132 +                                        fmtid = "float";
133 +                                        break;
134                                  case 'd':               /* double */
135 <                                        format = argv[i][2];
135 >                                        dataonly = 1;
136 >                                        format = 'd';
137 >                                        fmtid = "double";
138                                          break;
139                                  default:
140                                          goto unkopt;
141                                  }
142                                  break;
143                          case 'x':               /* x resolution */
144 <                                xres = atoi(argv[++i]);
144 >                        case 'X':               /* x resolution */
145 >                                resolution = 0;
146 >                                if (argv[i][0] == '-')
147 >                                        picres.or |= XDECR;
148 >                                picres.xr = atoi(argv[++i]);
149                                  break;
150                          case 'y':               /* y resolution */
151 <                                yres = atoi(argv[++i]);
151 >                        case 'Y':               /* y resolution */
152 >                                resolution = 0;
153 >                                if (argv[i][0] == '-')
154 >                                        picres.or |= YDECR;
155 >                                if (picres.xr == 0)
156 >                                        picres.or |= YMAJOR;
157 >                                picres.yr = atoi(argv[++i]);
158                                  break;
159                          default:
160   unkopt:
# Line 102 | Line 165 | unkopt:
165                          }
166                  else
167                          break;
168 <                        
168 >                                        /* recognize special formats */
169 >        if (dataonly && format == 'b')
170 >                if (brightonly)
171 >                        fmtid = "8-bit_grey";
172 >                else
173 >                        fmtid = "24-bit_rgb";
174 >                                        /* assign reverse ordering */
175 >        rord[ord[0]] = 0;
176 >        rord[ord[1]] = 1;
177 >        rord[ord[2]] = 2;
178 >                                        /* get input */
179          if (i == argc) {
180                  fin = stdin;
181          } else if (i == argc-1) {
# Line 119 | Line 192 | unkopt:
192          set_io();
193  
194          if (reverse) {
195 <                if (header)                     /* get header */
196 <                        copyheader(fin, stdout);
197 <                                                /* add to header */
198 <                printargs(i, argv, stdout);
199 <                printf("\n");
200 <                if (yres <= 0 || xres <= 0) {
201 <                        fprintf(stderr, "%s: missing x and y resolution\n",
202 <                                        progname);
195 > #ifdef MSDOS
196 >                setmode(fileno(stdout), O_BINARY);
197 >                if (format != 'a' && format != 'i')
198 >                        setmode(fileno(fin), O_BINARY);
199 > #endif
200 >                                        /* get header */
201 >                if (header) {
202 >                        if (checkheader(fin, fmtid, stdout) < 0) {
203 >                                fprintf(stderr, "%s: wrong input format\n",
204 >                                                progname);
205 >                                quit(1);
206 >                        }
207 >                } else
208 >                        newheader("RADIANCE", stdout);
209 >                                        /* get resolution */
210 >                if ((resolution && !fgetsresolu(&picres, fin)) ||
211 >                                picres.xr <= 0 || picres.yr <= 0) {
212 >                        fprintf(stderr, "%s: missing resolution\n", progname);
213                          quit(1);
214                  }
215 <                fputresolu(YMAJOR|YDECR, xres, yres, stdout);
215 >                                                /* add to header */
216 >                printargs(i, argv, stdout);
217 >                if (expval < .99 || expval > 1.01)
218 >                        fputexpos(expval, stdout);
219 >                fputformat(COLRFMT, stdout);
220 >                putchar('\n');
221 >                fputsresolu(&picres, stdout);   /* always put resolution */
222                  valtopix();
223          } else {
224 + #ifdef MSDOS
225 +                setmode(fileno(fin), O_BINARY);
226 +                if (format != 'a' && format != 'i')
227 +                        setmode(fileno(stdout), O_BINARY);
228 + #endif
229                                                  /* get header */
230 <                getheader(fin, checkhead);
231 <
232 <                if (xres <= 0 || yres <= 0)             /* get picture size */
233 <                        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
234 <                                fprintf(stderr,
235 <                                "%s: missing x and y resolution\n",
236 <                                                progname);
237 <                                quit(1);
238 <                        }
230 >                getheader(fin, checkhead, NULL);
231 >                if (wrongformat) {
232 >                        fprintf(stderr, "%s: input not a Radiance picture\n",
233 >                                        progname);
234 >                        quit(1);
235 >                }
236 >                if (!fgetsresolu(&picres, fin)) {
237 >                        fprintf(stderr, "%s: missing resolution\n", progname);
238 >                        quit(1);
239 >                }
240                  if (header) {
241                          printargs(i, argv, stdout);
242 <                        printf("\n");
242 >                        if (expval < .99 || expval > 1.01)
243 >                                fputexpos(expval, stdout);
244 >                        fputformat(fmtid, stdout);
245 >                        putchar('\n');
246                  }
247 +                if (resolution)                 /* put resolution */
248 +                        fputsresolu(&picres, stdout);
249                  pixtoval();
250          }
251  
# Line 156 | Line 256 | unkopt:
256   checkhead(line)                         /* deal with line from header */
257   char  *line;
258   {
259 +        char    fmt[32];
260          double  d;
261          COLOR   ctmp;
262  
263 <        if (header)
264 <                fputs(line, stdout);
265 <        if (isexpos(line)) {
263 >        if (formatval(fmt, line))
264 >                wrongformat = strcmp(fmt, COLRFMT);
265 >        else if (original && isexpos(line)) {
266                  d = 1.0/exposval(line);
267                  scalecolor(exposure, d);
268 <        } else if (iscolcor(line)) {
268 >                doexposure++;
269 >        } else if (original && iscolcor(line)) {
270                  colcorval(ctmp, line);
271 <                colval(exposure,RED) /= colval(ctmp,RED);
272 <                colval(exposure,GRN) /= colval(ctmp,GRN);
273 <                colval(exposure,BLU) /= colval(ctmp,BLU);
274 <        }
271 >                setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
272 >                                colval(exposure,GRN)/colval(ctmp,GRN),
273 >                                colval(exposure,BLU)/colval(ctmp,BLU));
274 >                doexposure++;
275 >        } else if (header)
276 >                fputs(line, stdout);
277   }
278  
279  
280   pixtoval()                              /* convert picture to values */
281   {
282 <        COLOR  *scanln;
282 >        register COLOR  *scanln;
283 >        int  dogamma;
284          COLOR  lastc;
285 +        FLOAT  hv[2];
286          int  y;
287          register int  x;
288  
289 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
289 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
290          if (scanln == NULL) {
291                  fprintf(stderr, "%s: out of memory\n", progname);
292                  quit(1);
293          }
294 +        dogamma = gamcor < .95 || gamcor > 1.05;
295          setcolor(lastc, 0.0, 0.0, 0.0);
296 <        for (y = yres-1; y >= 0; y--) {
297 <                if (freadscan(scanln, xres, fin) < 0) {
296 >        for (y = 0; y < numscans(&picres); y++) {
297 >                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
298                          fprintf(stderr, "%s: read error\n", progname);
299                          quit(1);
300                  }
301 <                for (x = 0; x < xres; x++) {
301 >                for (x = 0; x < scanlen(&picres); x++) {
302                          if (uniq)
303 <                                if (    scanln[x][RED] == lastc[RED] &&
304 <                                        scanln[x][GRN] == lastc[GRN] &&
305 <                                        scanln[x][BLU] == lastc[BLU]    )
303 >                                if (    colval(scanln[x],RED) ==
304 >                                                colval(lastc,RED) &&
305 >                                        colval(scanln[x],GRN) ==
306 >                                                colval(lastc,GRN) &&
307 >                                        colval(scanln[x],BLU) ==
308 >                                                colval(lastc,BLU)       )
309                                          continue;
310                                  else
311                                          copycolor(lastc, scanln[x]);
312 <                        if (original)
312 >                        if (doexposure)
313                                  multcolor(scanln[x], exposure);
314 <                        if (!dataonly)
315 <                                printf("%7d %7d ", x, y);
314 >                        if (dogamma)
315 >                                setcolor(scanln[x],
316 >                                pow(colval(scanln[x],RED), 1.0/gamcor),
317 >                                pow(colval(scanln[x],GRN), 1.0/gamcor),
318 >                                pow(colval(scanln[x],BLU), 1.0/gamcor));
319 >                        if (!dataonly) {
320 >                                pix2loc(hv, &picres, x, y);
321 >                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
322 >                                                (int)(hv[1]*picres.yr));
323 >                        }
324                          if ((*putval)(scanln[x], stdout) < 0) {
325                                  fprintf(stderr, "%s: write error\n", progname);
326                                  quit(1);
# Line 215 | Line 333 | pixtoval()                             /* convert picture to values */
333  
334   valtopix()                      /* convert values to a pixel file */
335   {
336 <        COLOR  *scanln;
336 >        int  dogamma;
337 >        register COLOR  *scanln;
338          int  y;
339          register int  x;
340  
341 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
341 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
342          if (scanln == NULL) {
343                  fprintf(stderr, "%s: out of memory\n", progname);
344                  quit(1);
345          }
346 <        for (y = yres-1; y >= 0; y--) {
347 <                for (x = 0; x < xres; x++) {
346 >        dogamma = gamcor < .95 || gamcor > 1.05;
347 >        for (y = 0; y < numscans(&picres); y++) {
348 >                for (x = 0; x < scanlen(&picres); x++) {
349                          if (!dataonly)
350                                  fscanf(fin, "%*d %*d");
351                          if ((*getval)(scanln[x], fin) < 0) {
352                                  fprintf(stderr, "%s: read error\n", progname);
353                                  quit(1);
354                          }
355 +                        if (dogamma)
356 +                                setcolor(scanln[x],
357 +                                        pow(colval(scanln[x],RED), gamcor),
358 +                                        pow(colval(scanln[x],GRN), gamcor),
359 +                                        pow(colval(scanln[x],BLU), gamcor));
360 +                        if (doexposure)
361 +                                multcolor(scanln[x], exposure);
362                  }
363 <                if (fwritescan(scanln, xres, stdout) < 0
237 <                                || fflush(stdout) < 0) {
363 >                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
364                          fprintf(stderr, "%s: write error\n", progname);
365                          quit(1);
366                  }
# Line 254 | Line 380 | getcascii(col, fp)             /* get an ascii color value from f
380   COLOR  col;
381   FILE  *fp;
382   {
383 <        double  vd[3];
383 >        double  vd[3];
384  
385          if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
386                  return(-1);
387 <        setcolor(col, vd[0], vd[1], vd[2]);
387 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
388          return(0);
389   }
390  
# Line 267 | Line 393 | getcdouble(col, fp)            /* get a double color value from
393   COLOR  col;
394   FILE  *fp;
395   {
396 <        double  vd[3];
396 >        double  vd[3];
397  
398          if (fread((char *)vd, sizeof(double), 3, fp) != 3)
399                  return(-1);
400 <        setcolor(col, vd[0], vd[1], vd[2]);
400 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
401          return(0);
402   }
403  
# Line 284 | Line 410 | FILE  *fp;
410  
411          if (fread((char *)vf, sizeof(float), 3, fp) != 3)
412                  return(-1);
413 <        setcolor(col, vf[0], vf[1], vf[2]);
413 >        setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
414          return(0);
415   }
416  
# Line 297 | Line 423 | FILE  *fp;
423  
424          if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
425                  return(-1);
426 <        setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
426 >        setcolor(col, (vi[rord[RED]]+.5)/256.,
427 >                        (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
428          return(0);
429   }
430  
# Line 310 | Line 437 | FILE  *fp;
437  
438          if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
439                  return(-1);
440 <        setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
440 >        setcolor(col, (vb[rord[RED]]+.5)/256.,
441 >                        (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
442          return(0);
443   }
444  
# Line 319 | Line 447 | getbascii(col, fp)             /* get an ascii brightness value f
447   COLOR  col;
448   FILE  *fp;
449   {
450 <        double  vd;
450 >        double  vd;
451  
452          if (fscanf(fp, "%lf", &vd) != 1)
453                  return(-1);
# Line 332 | Line 460 | getbdouble(col, fp)            /* get a double brightness value
460   COLOR  col;
461   FILE  *fp;
462   {
463 <        double  vd;
463 >        double  vd;
464  
465          if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
466                  return(-1);
# Line 359 | Line 487 | COLOR  col;
487   FILE  *fp;
488   {
489          int  vi;
490 <        double  d;
490 >        double  d;
491  
492          if (fscanf(fp, "%d", &vi) != 1)
493                  return(-1);
# Line 374 | Line 502 | COLOR  col;
502   FILE  *fp;
503   {
504          BYTE  vb;
505 <        double  d;
505 >        double  d;
506  
507          if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
508                  return(-1);
# Line 389 | Line 517 | COLOR  col;
517   FILE  *fp;
518   {
519          fprintf(fp, "%15.3e %15.3e %15.3e\n",
520 <                        colval(col,RED),
521 <                        colval(col,GRN),
522 <                        colval(col,BLU));
520 >                        colval(col,ord[0]),
521 >                        colval(col,ord[1]),
522 >                        colval(col,ord[2]));
523  
524          return(ferror(fp) ? -1 : 0);
525   }
# Line 403 | Line 531 | FILE  *fp;
531   {
532          float  vf[3];
533  
534 <        vf[0] = colval(col,RED);
535 <        vf[1] = colval(col,GRN);
536 <        vf[2] = colval(col,BLU);
534 >        vf[0] = colval(col,ord[0]);
535 >        vf[1] = colval(col,ord[1]);
536 >        vf[2] = colval(col,ord[2]);
537          fwrite((char *)vf, sizeof(float), 3, fp);
538  
539          return(ferror(fp) ? -1 : 0);
# Line 416 | Line 544 | putcdouble(col, fp)                    /* put a double color to fp */
544   COLOR  col;
545   FILE  *fp;
546   {
547 <        double  vd[3];
547 >        double  vd[3];
548  
549 <        vd[0] = colval(col,RED);
550 <        vd[1] = colval(col,GRN);
551 <        vd[2] = colval(col,BLU);
549 >        vd[0] = colval(col,ord[0]);
550 >        vd[1] = colval(col,ord[1]);
551 >        vd[2] = colval(col,ord[2]);
552          fwrite((char *)vd, sizeof(double), 3, fp);
553  
554          return(ferror(fp) ? -1 : 0);
# Line 432 | Line 560 | COLOR  col;
560   FILE  *fp;
561   {
562          fprintf(fp, "%d %d %d\n",
563 <                        (int)(colval(col,RED)*256.),
564 <                        (int)(colval(col,GRN)*256.),
565 <                        (int)(colval(col,BLU)*256.));
563 >                        (int)(colval(col,ord[0])*256.),
564 >                        (int)(colval(col,ord[1])*256.),
565 >                        (int)(colval(col,ord[2])*256.));
566  
567          return(ferror(fp) ? -1 : 0);
568   }
# Line 447 | Line 575 | FILE  *fp;
575          register int  i;
576          BYTE  vb[3];
577  
578 <        i = colval(col,RED)*256.;
578 >        i = colval(col,ord[0])*256.;
579          vb[0] = min(i,255);
580 <        i = colval(col,GRN)*256.;
580 >        i = colval(col,ord[1])*256.;
581          vb[1] = min(i,255);
582 <        i = colval(col,BLU)*256.;
582 >        i = colval(col,ord[2])*256.;
583          vb[2] = min(i,255);
584          fwrite((char *)vb, sizeof(BYTE), 3, fp);
585  
# Line 486 | Line 614 | putbdouble(col, fp)                    /* put a double brightness to fp
614   COLOR  col;
615   FILE  *fp;
616   {
617 <        double  vd;
617 >        double  vd;
618  
619          vd = bright(col);
620          fwrite((char *)&vd, sizeof(double), 1, fp);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines