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.6 by greg, Fri Apr 19 09:00:55 1991 UTC vs.
Revision 2.8 by greg, Sun Feb 27 10:16:48 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 + RESOLU  picres;                 /* resolution of picture */
22 +
23   int  uniq = 0;                  /* print only unique values? */
24  
25 < int  original = 0;              /* convert back to original values? */
25 > int  doexposure = 0;            /* exposure change? (>100 to print) */
26  
27   int  dataonly = 0;              /* data only format? */
28  
# Line 32 | Line 33 | int  reverse = 0;              /* reverse conversion? */
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 49 | Line 59 | main(argc, argv)
59   int  argc;
60   char  **argv;
61   {
52        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 +                                        doexposure = 100;
95 +                                scalecolor(exposure, d);
96 +                                expval *= d;
97 +                                doexposure++;
98 +                                i++;
99 +                                break;
100 +                        case 'R':               /* reverse byte sequence */
101 +                                if (argv[i][0] == '-') {
102 +                                        ord[0]=BLU; ord[1]=GRN; ord[2]=RED;
103 +                                } else {
104 +                                        ord[0]=RED; ord[1]=GRN; ord[2]=BLU;
105 +                                }
106 +                                break;
107                          case 'r':               /* reverse conversion */
108 <                                reverse = 1;
108 >                                reverse = argv[i][0] == '-';
109                                  break;
110                          case 'b':               /* brightness values */
111 <                                brightonly = 1;
111 >                                brightonly = argv[i][0] == '-';
112                                  break;
113                          case 'd':               /* data only (no indices) */
114 <                                dataonly = 1;
114 >                                dataonly = argv[i][0] == '-';
115                                  switch (argv[i][2]) {
116                                  case '\0':
117                                  case 'a':               /* ascii */
# Line 86 | Line 123 | char  **argv;
123                                          fmtid = "ascii";
124                                          break;
125                                  case 'b':               /* byte */
126 +                                        dataonly = 1;
127                                          format = 'b';
128                                          fmtid = "byte";
129                                          break;
130                                  case 'f':               /* float */
131 +                                        dataonly = 1;
132                                          format = 'f';
133                                          fmtid = "float";
134                                          break;
135                                  case 'd':               /* double */
136 +                                        dataonly = 1;
137                                          format = 'd';
138                                          fmtid = "double";
139                                          break;
# Line 102 | Line 142 | char  **argv;
142                                  }
143                                  break;
144                          case 'x':               /* x resolution */
145 <                                xres = atoi(argv[++i]);
145 >                        case 'X':               /* x resolution */
146 >                                resolution = 0;
147 >                                if (argv[i][0] == '-')
148 >                                        picres.or |= XDECR;
149 >                                picres.xr = atoi(argv[++i]);
150                                  break;
151                          case 'y':               /* y resolution */
152 <                                yres = atoi(argv[++i]);
152 >                        case 'Y':               /* y resolution */
153 >                                resolution = 0;
154 >                                if (argv[i][0] == '-')
155 >                                        picres.or |= YDECR;
156 >                                if (picres.xr == 0)
157 >                                        picres.or |= YMAJOR;
158 >                                picres.yr = atoi(argv[++i]);
159                                  break;
160                          default:
161   unkopt:
# Line 122 | Line 172 | unkopt:
172                          fmtid = "8-bit_grey";
173                  else
174                          fmtid = "24-bit_rgb";
175 <
175 >                                        /* assign reverse ordering */
176 >        rord[ord[0]] = 0;
177 >        rord[ord[1]] = 1;
178 >        rord[ord[2]] = 2;
179 >                                        /* get input */
180          if (i == argc) {
181                  fin = stdin;
182          } else if (i == argc-1) {
# Line 139 | Line 193 | unkopt:
193          set_io();
194  
195          if (reverse) {
196 <                if (yres <= 0 || xres <= 0) {
197 <                        fprintf(stderr, "%s: missing x and y resolution\n",
198 <                                        progname);
199 <                        quit(1);
200 <                }
196 > #ifdef MSDOS
197 >                setmode(fileno(stdout), O_BINARY);
198 >                if (format != 'a' && format != 'i')
199 >                        setmode(fileno(fin), O_BINARY);
200 > #endif
201                                          /* get header */
202 <                if (header && checkheader(fin, fmtid, stdout) < 0) {
203 <                        fprintf(stderr, "%s: wrong input format\n", progname);
202 >                if (header) {
203 >                        if (checkheader(fin, fmtid, stdout) < 0) {
204 >                                fprintf(stderr, "%s: wrong input format\n",
205 >                                                progname);
206 >                                quit(1);
207 >                        }
208 >                } else
209 >                        newheader("RADIANCE", stdout);
210 >                                        /* get resolution */
211 >                if ((resolution && !fgetsresolu(&picres, fin)) ||
212 >                                picres.xr <= 0 || picres.yr <= 0) {
213 >                        fprintf(stderr, "%s: missing resolution\n", progname);
214                          quit(1);
215                  }
216                                                  /* add to header */
217                  printargs(i, argv, stdout);
218 +                if (doexposure > 100)
219 +                        fputexpos(expval, stdout);
220                  fputformat(COLRFMT, stdout);
221 <                printf("\n");
222 <                fputresolu(YMAJOR|YDECR, xres, yres, stdout);
221 >                putchar('\n');
222 >                fputsresolu(&picres, stdout);   /* always put resolution */
223                  valtopix();
224          } else {
225 + #ifdef MSDOS
226 +                setmode(fileno(fin), O_BINARY);
227 +                if (format != 'a' && format != 'i')
228 +                        setmode(fileno(stdout), O_BINARY);
229 + #endif
230                                                  /* get header */
231                  getheader(fin, checkhead, NULL);
232                  if (wrongformat) {
# Line 163 | Line 234 | unkopt:
234                                          progname);
235                          quit(1);
236                  }
237 <
238 <                if (xres <= 0 || yres <= 0)             /* get picture size */
239 <                        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
240 <                                fprintf(stderr,
170 <                                "%s: missing x and y resolution\n",
171 <                                                progname);
172 <                                quit(1);
173 <                        }
237 >                if (!fgetsresolu(&picres, fin)) {
238 >                        fprintf(stderr, "%s: missing resolution\n", progname);
239 >                        quit(1);
240 >                }
241                  if (header) {
242                          printargs(i, argv, stdout);
243 +                        if (doexposure > 100)
244 +                                fputexpos(expval, stdout);
245                          fputformat(fmtid, stdout);
246 <                        printf("\n");
246 >                        putchar('\n');
247                  }
248 +                if (resolution)                 /* put resolution */
249 +                        fputsresolu(&picres, stdout);
250                  pixtoval();
251          }
252  
# Line 190 | Line 261 | char  *line;
261          double  d;
262          COLOR   ctmp;
263  
264 <        if (isformat(line)) {
194 <                formatval(fmt, line);
264 >        if (formatval(fmt, line))
265                  wrongformat = strcmp(fmt, COLRFMT);
266 <        } else if (original && isexpos(line)) {
266 >        else if (original && isexpos(line)) {
267                  d = 1.0/exposval(line);
268                  scalecolor(exposure, d);
269 +                doexposure++;
270          } else if (original && iscolcor(line)) {
271                  colcorval(ctmp, line);
272 <                colval(exposure,RED) /= colval(ctmp,RED);
273 <                colval(exposure,GRN) /= colval(ctmp,GRN);
274 <                colval(exposure,BLU) /= colval(ctmp,BLU);
272 >                setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
273 >                                colval(exposure,GRN)/colval(ctmp,GRN),
274 >                                colval(exposure,BLU)/colval(ctmp,BLU));
275 >                doexposure++;
276          } else if (header)
277                  fputs(line, stdout);
278   }
# Line 208 | Line 280 | char  *line;
280  
281   pixtoval()                              /* convert picture to values */
282   {
283 <        COLOR  *scanln;
283 >        register COLOR  *scanln;
284 >        int  dogamma;
285          COLOR  lastc;
286 +        FLOAT  hv[2];
287          int  y;
288          register int  x;
289  
290 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
290 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
291          if (scanln == NULL) {
292                  fprintf(stderr, "%s: out of memory\n", progname);
293                  quit(1);
294          }
295 +        dogamma = gamcor < .95 || gamcor > 1.05;
296          setcolor(lastc, 0.0, 0.0, 0.0);
297 <        for (y = yres-1; y >= 0; y--) {
298 <                if (freadscan(scanln, xres, fin) < 0) {
297 >        for (y = 0; y < numscans(&picres); y++) {
298 >                if (freadscan(scanln, scanlen(&picres), fin) < 0) {
299                          fprintf(stderr, "%s: read error\n", progname);
300                          quit(1);
301                  }
302 <                for (x = 0; x < xres; x++) {
302 >                for (x = 0; x < scanlen(&picres); x++) {
303                          if (uniq)
304 <                                if (    scanln[x][RED] == lastc[RED] &&
305 <                                        scanln[x][GRN] == lastc[GRN] &&
306 <                                        scanln[x][BLU] == lastc[BLU]    )
304 >                                if (    colval(scanln[x],RED) ==
305 >                                                colval(lastc,RED) &&
306 >                                        colval(scanln[x],GRN) ==
307 >                                                colval(lastc,GRN) &&
308 >                                        colval(scanln[x],BLU) ==
309 >                                                colval(lastc,BLU)       )
310                                          continue;
311                                  else
312                                          copycolor(lastc, scanln[x]);
313 <                        if (original)
313 >                        if (doexposure)
314                                  multcolor(scanln[x], exposure);
315 <                        if (!dataonly)
316 <                                printf("%7d %7d ", x, y);
315 >                        if (dogamma)
316 >                                setcolor(scanln[x],
317 >                                pow(colval(scanln[x],RED), 1.0/gamcor),
318 >                                pow(colval(scanln[x],GRN), 1.0/gamcor),
319 >                                pow(colval(scanln[x],BLU), 1.0/gamcor));
320 >                        if (!dataonly) {
321 >                                pix2loc(hv, &picres, x, y);
322 >                                printf("%7d %7d ", (int)(hv[0]*picres.xr),
323 >                                                (int)(hv[1]*picres.yr));
324 >                        }
325                          if ((*putval)(scanln[x], stdout) < 0) {
326                                  fprintf(stderr, "%s: write error\n", progname);
327                                  quit(1);
# Line 248 | Line 334 | pixtoval()                             /* convert picture to values */
334  
335   valtopix()                      /* convert values to a pixel file */
336   {
337 <        COLOR  *scanln;
337 >        int  dogamma;
338 >        register COLOR  *scanln;
339          int  y;
340          register int  x;
341  
342 <        scanln = (COLOR *)malloc(xres*sizeof(COLOR));
342 >        scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
343          if (scanln == NULL) {
344                  fprintf(stderr, "%s: out of memory\n", progname);
345                  quit(1);
346          }
347 <        for (y = yres-1; y >= 0; y--) {
348 <                for (x = 0; x < xres; x++) {
347 >        dogamma = gamcor < .95 || gamcor > 1.05;
348 >        for (y = 0; y < numscans(&picres); y++) {
349 >                for (x = 0; x < scanlen(&picres); x++) {
350                          if (!dataonly)
351                                  fscanf(fin, "%*d %*d");
352                          if ((*getval)(scanln[x], fin) < 0) {
353                                  fprintf(stderr, "%s: read error\n", progname);
354                                  quit(1);
355                          }
356 +                        if (dogamma)
357 +                                setcolor(scanln[x],
358 +                                        pow(colval(scanln[x],RED), gamcor),
359 +                                        pow(colval(scanln[x],GRN), gamcor),
360 +                                        pow(colval(scanln[x],BLU), gamcor));
361 +                        if (doexposure)
362 +                                multcolor(scanln[x], exposure);
363                  }
364 <                if (fwritescan(scanln, xres, stdout) < 0
270 <                                || fflush(stdout) < 0) {
364 >                if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
365                          fprintf(stderr, "%s: write error\n", progname);
366                          quit(1);
367                  }
# Line 287 | Line 381 | getcascii(col, fp)             /* get an ascii color value from f
381   COLOR  col;
382   FILE  *fp;
383   {
384 <        double  vd[3];
384 >        double  vd[3];
385  
386          if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
387                  return(-1);
388 <        setcolor(col, vd[0], vd[1], vd[2]);
388 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
389          return(0);
390   }
391  
# Line 300 | Line 394 | getcdouble(col, fp)            /* get a double color value from
394   COLOR  col;
395   FILE  *fp;
396   {
397 <        double  vd[3];
397 >        double  vd[3];
398  
399          if (fread((char *)vd, sizeof(double), 3, fp) != 3)
400                  return(-1);
401 <        setcolor(col, vd[0], vd[1], vd[2]);
401 >        setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
402          return(0);
403   }
404  
# Line 317 | Line 411 | FILE  *fp;
411  
412          if (fread((char *)vf, sizeof(float), 3, fp) != 3)
413                  return(-1);
414 <        setcolor(col, vf[0], vf[1], vf[2]);
414 >        setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
415          return(0);
416   }
417  
# Line 330 | Line 424 | FILE  *fp;
424  
425          if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
426                  return(-1);
427 <        setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
427 >        setcolor(col, (vi[rord[RED]]+.5)/256.,
428 >                        (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
429          return(0);
430   }
431  
# Line 343 | Line 438 | FILE  *fp;
438  
439          if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
440                  return(-1);
441 <        setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
441 >        setcolor(col, (vb[rord[RED]]+.5)/256.,
442 >                        (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
443          return(0);
444   }
445  
# Line 352 | Line 448 | getbascii(col, fp)             /* get an ascii brightness value f
448   COLOR  col;
449   FILE  *fp;
450   {
451 <        double  vd;
451 >        double  vd;
452  
453          if (fscanf(fp, "%lf", &vd) != 1)
454                  return(-1);
# Line 365 | Line 461 | getbdouble(col, fp)            /* get a double brightness value
461   COLOR  col;
462   FILE  *fp;
463   {
464 <        double  vd;
464 >        double  vd;
465  
466          if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
467                  return(-1);
# Line 392 | Line 488 | COLOR  col;
488   FILE  *fp;
489   {
490          int  vi;
491 <        double  d;
491 >        double  d;
492  
493          if (fscanf(fp, "%d", &vi) != 1)
494                  return(-1);
# Line 407 | Line 503 | COLOR  col;
503   FILE  *fp;
504   {
505          BYTE  vb;
506 <        double  d;
506 >        double  d;
507  
508          if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
509                  return(-1);
# Line 422 | Line 518 | COLOR  col;
518   FILE  *fp;
519   {
520          fprintf(fp, "%15.3e %15.3e %15.3e\n",
521 <                        colval(col,RED),
522 <                        colval(col,GRN),
523 <                        colval(col,BLU));
521 >                        colval(col,ord[0]),
522 >                        colval(col,ord[1]),
523 >                        colval(col,ord[2]));
524  
525          return(ferror(fp) ? -1 : 0);
526   }
# Line 436 | Line 532 | FILE  *fp;
532   {
533          float  vf[3];
534  
535 <        vf[0] = colval(col,RED);
536 <        vf[1] = colval(col,GRN);
537 <        vf[2] = colval(col,BLU);
535 >        vf[0] = colval(col,ord[0]);
536 >        vf[1] = colval(col,ord[1]);
537 >        vf[2] = colval(col,ord[2]);
538          fwrite((char *)vf, sizeof(float), 3, fp);
539  
540          return(ferror(fp) ? -1 : 0);
# Line 449 | Line 545 | putcdouble(col, fp)                    /* put a double color to fp */
545   COLOR  col;
546   FILE  *fp;
547   {
548 <        double  vd[3];
548 >        double  vd[3];
549  
550 <        vd[0] = colval(col,RED);
551 <        vd[1] = colval(col,GRN);
552 <        vd[2] = colval(col,BLU);
550 >        vd[0] = colval(col,ord[0]);
551 >        vd[1] = colval(col,ord[1]);
552 >        vd[2] = colval(col,ord[2]);
553          fwrite((char *)vd, sizeof(double), 3, fp);
554  
555          return(ferror(fp) ? -1 : 0);
# Line 465 | Line 561 | COLOR  col;
561   FILE  *fp;
562   {
563          fprintf(fp, "%d %d %d\n",
564 <                        (int)(colval(col,RED)*256.),
565 <                        (int)(colval(col,GRN)*256.),
566 <                        (int)(colval(col,BLU)*256.));
564 >                        (int)(colval(col,ord[0])*256.),
565 >                        (int)(colval(col,ord[1])*256.),
566 >                        (int)(colval(col,ord[2])*256.));
567  
568          return(ferror(fp) ? -1 : 0);
569   }
# Line 480 | Line 576 | FILE  *fp;
576          register int  i;
577          BYTE  vb[3];
578  
579 <        i = colval(col,RED)*256.;
579 >        i = colval(col,ord[0])*256.;
580          vb[0] = min(i,255);
581 <        i = colval(col,GRN)*256.;
581 >        i = colval(col,ord[1])*256.;
582          vb[1] = min(i,255);
583 <        i = colval(col,BLU)*256.;
583 >        i = colval(col,ord[2])*256.;
584          vb[2] = min(i,255);
585          fwrite((char *)vb, sizeof(BYTE), 3, fp);
586  
# Line 519 | Line 615 | putbdouble(col, fp)                    /* put a double brightness to fp
615   COLOR  col;
616   FILE  *fp;
617   {
618 <        double  vd;
618 >        double  vd;
619  
620          vd = bright(col);
621          fwrite((char *)&vd, sizeof(double), 1, fp);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines