ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.9
Committed: Thu Oct 6 13:27:16 1994 UTC (29 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +3 -4 lines
Log Message:
made it so -e and +e work well together

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * pvalue.c - program to print pixel values.
9 *
10 * 4/23/86
11 */
12
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 RESOLU picres; /* resolution of picture */
22
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? */
30
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? */
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;
52
53 FILE *fin;
54
55 int (*getval)(), (*putval)();
56
57
58 main(argc, argv)
59 int argc;
60 char **argv;
61 {
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] == '-' || argv[i][0] == '+')
70 switch (argv[i][1]) {
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 = argv[i][0] == '-';
79 break;
80 case 'o': /* original values */
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 = argv[i][0] == '-';
108 break;
109 case 'b': /* brightness values */
110 brightonly = argv[i][0] == '-';
111 break;
112 case 'd': /* data only (no indices) */
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 dataonly = 1;
136 format = 'd';
137 fmtid = "double";
138 break;
139 default:
140 goto unkopt;
141 }
142 break;
143 case 'x': /* x resolution */
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 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:
161 fprintf(stderr, "%s: unknown option: %s\n",
162 progname, argv[i]);
163 quit(1);
164 break;
165 }
166 else
167 break;
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) {
182 if ((fin = fopen(argv[i], "r")) == NULL) {
183 fprintf(stderr, "%s: can't open file \"%s\"\n",
184 progname, argv[i]);
185 quit(1);
186 }
187 } else {
188 fprintf(stderr, "%s: bad # file arguments\n", progname);
189 quit(1);
190 }
191
192 set_io();
193
194 if (reverse) {
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 /* 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, 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 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
252 quit(0);
253 }
254
255
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 (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 doexposure++;
269 } else if (original && iscolcor(line)) {
270 colcorval(ctmp, line);
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 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(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 = 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 < scanlen(&picres); x++) {
302 if (uniq)
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 (doexposure)
313 multcolor(scanln[x], exposure);
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);
327 }
328 }
329 }
330 free((char *)scanln);
331 }
332
333
334 valtopix() /* convert values to a pixel file */
335 {
336 int dogamma;
337 register COLOR *scanln;
338 int y;
339 register int x;
340
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 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, scanlen(&picres), stdout) < 0) {
364 fprintf(stderr, "%s: write error\n", progname);
365 quit(1);
366 }
367 }
368 free((char *)scanln);
369 }
370
371
372 quit(code)
373 int code;
374 {
375 exit(code);
376 }
377
378
379 getcascii(col, fp) /* get an ascii color value from fp */
380 COLOR col;
381 FILE *fp;
382 {
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[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
388 return(0);
389 }
390
391
392 getcdouble(col, fp) /* get a double color value from fp */
393 COLOR col;
394 FILE *fp;
395 {
396 double vd[3];
397
398 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
399 return(-1);
400 setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]);
401 return(0);
402 }
403
404
405 getcfloat(col, fp) /* get a float color value from fp */
406 COLOR col;
407 FILE *fp;
408 {
409 float vf[3];
410
411 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
412 return(-1);
413 setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]);
414 return(0);
415 }
416
417
418 getcint(col, fp) /* get an int color value from fp */
419 COLOR col;
420 FILE *fp;
421 {
422 int vi[3];
423
424 if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
425 return(-1);
426 setcolor(col, (vi[rord[RED]]+.5)/256.,
427 (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.);
428 return(0);
429 }
430
431
432 getcbyte(col, fp) /* get a byte color value from fp */
433 COLOR col;
434 FILE *fp;
435 {
436 BYTE vb[3];
437
438 if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
439 return(-1);
440 setcolor(col, (vb[rord[RED]]+.5)/256.,
441 (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.);
442 return(0);
443 }
444
445
446 getbascii(col, fp) /* get an ascii brightness value from fp */
447 COLOR col;
448 FILE *fp;
449 {
450 double vd;
451
452 if (fscanf(fp, "%lf", &vd) != 1)
453 return(-1);
454 setcolor(col, vd, vd, vd);
455 return(0);
456 }
457
458
459 getbdouble(col, fp) /* get a double brightness value from fp */
460 COLOR col;
461 FILE *fp;
462 {
463 double vd;
464
465 if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
466 return(-1);
467 setcolor(col, vd, vd, vd);
468 return(0);
469 }
470
471
472 getbfloat(col, fp) /* get a float brightness value from fp */
473 COLOR col;
474 FILE *fp;
475 {
476 float vf;
477
478 if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
479 return(-1);
480 setcolor(col, vf, vf, vf);
481 return(0);
482 }
483
484
485 getbint(col, fp) /* get an int brightness value from fp */
486 COLOR col;
487 FILE *fp;
488 {
489 int vi;
490 double d;
491
492 if (fscanf(fp, "%d", &vi) != 1)
493 return(-1);
494 d = (vi+.5)/256.;
495 setcolor(col, d, d, d);
496 return(0);
497 }
498
499
500 getbbyte(col, fp) /* get a byte brightness value from fp */
501 COLOR col;
502 FILE *fp;
503 {
504 BYTE vb;
505 double d;
506
507 if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
508 return(-1);
509 d = (vb+.5)/256.;
510 setcolor(col, d, d, d);
511 return(0);
512 }
513
514
515 putcascii(col, fp) /* put an ascii color to fp */
516 COLOR col;
517 FILE *fp;
518 {
519 fprintf(fp, "%15.3e %15.3e %15.3e\n",
520 colval(col,ord[0]),
521 colval(col,ord[1]),
522 colval(col,ord[2]));
523
524 return(ferror(fp) ? -1 : 0);
525 }
526
527
528 putcfloat(col, fp) /* put a float color to fp */
529 COLOR col;
530 FILE *fp;
531 {
532 float vf[3];
533
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);
540 }
541
542
543 putcdouble(col, fp) /* put a double color to fp */
544 COLOR col;
545 FILE *fp;
546 {
547 double vd[3];
548
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);
555 }
556
557
558 putcint(col, fp) /* put an int color to fp */
559 COLOR col;
560 FILE *fp;
561 {
562 fprintf(fp, "%d %d %d\n",
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 }
569
570
571 putcbyte(col, fp) /* put a byte color to fp */
572 COLOR col;
573 FILE *fp;
574 {
575 register int i;
576 BYTE vb[3];
577
578 i = colval(col,ord[0])*256.;
579 vb[0] = min(i,255);
580 i = colval(col,ord[1])*256.;
581 vb[1] = min(i,255);
582 i = colval(col,ord[2])*256.;
583 vb[2] = min(i,255);
584 fwrite((char *)vb, sizeof(BYTE), 3, fp);
585
586 return(ferror(fp) ? -1 : 0);
587 }
588
589
590 putbascii(col, fp) /* put an ascii brightness to fp */
591 COLOR col;
592 FILE *fp;
593 {
594 fprintf(fp, "%15.3e\n", bright(col));
595
596 return(ferror(fp) ? -1 : 0);
597 }
598
599
600 putbfloat(col, fp) /* put a float brightness to fp */
601 COLOR col;
602 FILE *fp;
603 {
604 float vf;
605
606 vf = bright(col);
607 fwrite((char *)&vf, sizeof(float), 1, fp);
608
609 return(ferror(fp) ? -1 : 0);
610 }
611
612
613 putbdouble(col, fp) /* put a double brightness to fp */
614 COLOR col;
615 FILE *fp;
616 {
617 double vd;
618
619 vd = bright(col);
620 fwrite((char *)&vd, sizeof(double), 1, fp);
621
622 return(ferror(fp) ? -1 : 0);
623 }
624
625
626 putbint(col, fp) /* put an int brightness to fp */
627 COLOR col;
628 FILE *fp;
629 {
630 fprintf(fp, "%d\n", (int)(bright(col)*256.));
631
632 return(ferror(fp) ? -1 : 0);
633 }
634
635
636 putbbyte(col, fp) /* put a byte brightness to fp */
637 COLOR col;
638 FILE *fp;
639 {
640 register int i;
641 BYTE vb;
642
643 i = bright(col)*256.;
644 vb = min(i,255);
645 fwrite((char *)&vb, sizeof(BYTE), 1, fp);
646
647 return(ferror(fp) ? -1 : 0);
648 }
649
650
651 set_io() /* set put and get functions */
652 {
653 switch (format) {
654 case 'a': /* ascii */
655 if (brightonly) {
656 getval = getbascii;
657 putval = putbascii;
658 } else {
659 getval = getcascii;
660 putval = putcascii;
661 }
662 return;
663 case 'f': /* binary float */
664 if (brightonly) {
665 getval = getbfloat;
666 putval = putbfloat;
667 } else {
668 getval = getcfloat;
669 putval = putcfloat;
670 }
671 return;
672 case 'd': /* binary double */
673 if (brightonly) {
674 getval = getbdouble;
675 putval = putbdouble;
676 } else {
677 getval = getcdouble;
678 putval = putcdouble;
679 }
680 return;
681 case 'i': /* integer */
682 if (brightonly) {
683 getval = getbint;
684 putval = putbint;
685 } else {
686 getval = getcint;
687 putval = putcint;
688 }
689 return;
690 case 'b': /* byte */
691 if (brightonly) {
692 getval = getbbyte;
693 putval = putbbyte;
694 } else {
695 getval = getcbyte;
696 putval = putcbyte;
697 }
698 return;
699 }
700 }