ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.3
Committed: Fri May 8 10:58:52 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +21 -15 lines
Log Message:
added +/-H option for no/yes to resolution string

File Contents

# Content
1 /* Copyright (c) 1991 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 original = 0; /* convert back to original values? */
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 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;
49
50 int (*getval)(), (*putval)();
51
52
53 main(argc, argv)
54 int argc;
55 char **argv;
56 {
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] == '-' || argv[i][0] == '+')
64 switch (argv[i][1]) {
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 = argv[i][0] == '-';
73 break;
74 case 'o': /* original values */
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 = argv[i][0] == '-';
84 break;
85 case 'b': /* brightness values */
86 brightonly = argv[i][0] == '-';
87 break;
88 case 'd': /* data only (no indices) */
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 dataonly = 1;
112 format = 'd';
113 fmtid = "double";
114 break;
115 default:
116 goto unkopt;
117 }
118 break;
119 case 'x': /* x resolution */
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 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:
135 fprintf(stderr, "%s: unknown option: %s\n",
136 progname, argv[i]);
137 quit(1);
138 break;
139 }
140 else
141 break;
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) {
152 if ((fin = fopen(argv[i], "r")) == NULL) {
153 fprintf(stderr, "%s: can't open file \"%s\"\n",
154 progname, argv[i]);
155 quit(1);
156 }
157 } else {
158 fprintf(stderr, "%s: bad # file arguments\n", progname);
159 quit(1);
160 }
161
162 set_io();
163
164 if (reverse) {
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 /* 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, 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 fputformat(fmtid, stdout);
197 putchar('\n');
198 }
199 if (resolution) /* put resolution */
200 fputsresolu(&picres, stdout);
201 pixtoval();
202 }
203
204 quit(0);
205 }
206
207
208 checkhead(line) /* deal with line from header */
209 char *line;
210 {
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);
228 }
229
230
231 pixtoval() /* convert picture to values */
232 {
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(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 = 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 < scanlen(&picres); x++) {
253 if (uniq)
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 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);
278 }
279 }
280 }
281 free((char *)scanln);
282 }
283
284
285 valtopix() /* convert values to a pixel file */
286 {
287 int dogamma;
288 register COLOR *scanln;
289 int y;
290 register int x;
291
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 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, scanlen(&picres), stdout) < 0) {
313 fprintf(stderr, "%s: write error\n", progname);
314 quit(1);
315 }
316 }
317 free((char *)scanln);
318 }
319
320
321 quit(code)
322 int code;
323 {
324 exit(code);
325 }
326
327
328 getcascii(col, fp) /* get an ascii color value from fp */
329 COLOR col;
330 FILE *fp;
331 {
332 double vd[3];
333
334 if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
335 return(-1);
336 setcolor(col, vd[0], vd[1], vd[2]);
337 return(0);
338 }
339
340
341 getcdouble(col, fp) /* get a double color value from fp */
342 COLOR col;
343 FILE *fp;
344 {
345 double vd[3];
346
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);
351 }
352
353
354 getcfloat(col, fp) /* get a float color value from fp */
355 COLOR col;
356 FILE *fp;
357 {
358 float vf[3];
359
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);
364 }
365
366
367 getcint(col, fp) /* get an int color value from fp */
368 COLOR col;
369 FILE *fp;
370 {
371 int vi[3];
372
373 if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
374 return(-1);
375 setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
376 return(0);
377 }
378
379
380 getcbyte(col, fp) /* get a byte color value from fp */
381 COLOR col;
382 FILE *fp;
383 {
384 BYTE vb[3];
385
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);
390 }
391
392
393 getbascii(col, fp) /* get an ascii brightness value from fp */
394 COLOR col;
395 FILE *fp;
396 {
397 double vd;
398
399 if (fscanf(fp, "%lf", &vd) != 1)
400 return(-1);
401 setcolor(col, vd, vd, vd);
402 return(0);
403 }
404
405
406 getbdouble(col, fp) /* get a double brightness value from fp */
407 COLOR col;
408 FILE *fp;
409 {
410 double vd;
411
412 if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
413 return(-1);
414 setcolor(col, vd, vd, vd);
415 return(0);
416 }
417
418
419 getbfloat(col, fp) /* get a float brightness value from fp */
420 COLOR col;
421 FILE *fp;
422 {
423 float vf;
424
425 if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
426 return(-1);
427 setcolor(col, vf, vf, vf);
428 return(0);
429 }
430
431
432 getbint(col, fp) /* get an int brightness value from fp */
433 COLOR col;
434 FILE *fp;
435 {
436 int vi;
437 double d;
438
439 if (fscanf(fp, "%d", &vi) != 1)
440 return(-1);
441 d = (vi+.5)/256.;
442 setcolor(col, d, d, d);
443 return(0);
444 }
445
446
447 getbbyte(col, fp) /* get a byte brightness value from fp */
448 COLOR col;
449 FILE *fp;
450 {
451 BYTE vb;
452 double d;
453
454 if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
455 return(-1);
456 d = (vb+.5)/256.;
457 setcolor(col, d, d, d);
458 return(0);
459 }
460
461
462 putcascii(col, fp) /* put an ascii color to fp */
463 COLOR col;
464 FILE *fp;
465 {
466 fprintf(fp, "%15.3e %15.3e %15.3e\n",
467 colval(col,RED),
468 colval(col,GRN),
469 colval(col,BLU));
470
471 return(ferror(fp) ? -1 : 0);
472 }
473
474
475 putcfloat(col, fp) /* put a float color to fp */
476 COLOR col;
477 FILE *fp;
478 {
479 float vf[3];
480
481 vf[0] = colval(col,RED);
482 vf[1] = colval(col,GRN);
483 vf[2] = colval(col,BLU);
484 fwrite((char *)vf, sizeof(float), 3, fp);
485
486 return(ferror(fp) ? -1 : 0);
487 }
488
489
490 putcdouble(col, fp) /* put a double color to fp */
491 COLOR col;
492 FILE *fp;
493 {
494 double vd[3];
495
496 vd[0] = colval(col,RED);
497 vd[1] = colval(col,GRN);
498 vd[2] = colval(col,BLU);
499 fwrite((char *)vd, sizeof(double), 3, fp);
500
501 return(ferror(fp) ? -1 : 0);
502 }
503
504
505 putcint(col, fp) /* put an int color to fp */
506 COLOR col;
507 FILE *fp;
508 {
509 fprintf(fp, "%d %d %d\n",
510 (int)(colval(col,RED)*256.),
511 (int)(colval(col,GRN)*256.),
512 (int)(colval(col,BLU)*256.));
513
514 return(ferror(fp) ? -1 : 0);
515 }
516
517
518 putcbyte(col, fp) /* put a byte color to fp */
519 COLOR col;
520 FILE *fp;
521 {
522 register int i;
523 BYTE vb[3];
524
525 i = colval(col,RED)*256.;
526 vb[0] = min(i,255);
527 i = colval(col,GRN)*256.;
528 vb[1] = min(i,255);
529 i = colval(col,BLU)*256.;
530 vb[2] = min(i,255);
531 fwrite((char *)vb, sizeof(BYTE), 3, fp);
532
533 return(ferror(fp) ? -1 : 0);
534 }
535
536
537 putbascii(col, fp) /* put an ascii brightness to fp */
538 COLOR col;
539 FILE *fp;
540 {
541 fprintf(fp, "%15.3e\n", bright(col));
542
543 return(ferror(fp) ? -1 : 0);
544 }
545
546
547 putbfloat(col, fp) /* put a float brightness to fp */
548 COLOR col;
549 FILE *fp;
550 {
551 float vf;
552
553 vf = bright(col);
554 fwrite((char *)&vf, sizeof(float), 1, fp);
555
556 return(ferror(fp) ? -1 : 0);
557 }
558
559
560 putbdouble(col, fp) /* put a double brightness to fp */
561 COLOR col;
562 FILE *fp;
563 {
564 double vd;
565
566 vd = bright(col);
567 fwrite((char *)&vd, sizeof(double), 1, fp);
568
569 return(ferror(fp) ? -1 : 0);
570 }
571
572
573 putbint(col, fp) /* put an int brightness to fp */
574 COLOR col;
575 FILE *fp;
576 {
577 fprintf(fp, "%d\n", (int)(bright(col)*256.));
578
579 return(ferror(fp) ? -1 : 0);
580 }
581
582
583 putbbyte(col, fp) /* put a byte brightness to fp */
584 COLOR col;
585 FILE *fp;
586 {
587 register int i;
588 BYTE vb;
589
590 i = bright(col)*256.;
591 vb = min(i,255);
592 fwrite((char *)&vb, sizeof(BYTE), 1, fp);
593
594 return(ferror(fp) ? -1 : 0);
595 }
596
597
598 set_io() /* set put and get functions */
599 {
600 switch (format) {
601 case 'a': /* ascii */
602 if (brightonly) {
603 getval = getbascii;
604 putval = putbascii;
605 } else {
606 getval = getcascii;
607 putval = putcascii;
608 }
609 return;
610 case 'f': /* binary float */
611 if (brightonly) {
612 getval = getbfloat;
613 putval = putbfloat;
614 } else {
615 getval = getcfloat;
616 putval = putcfloat;
617 }
618 return;
619 case 'd': /* binary double */
620 if (brightonly) {
621 getval = getbdouble;
622 putval = putbdouble;
623 } else {
624 getval = getcdouble;
625 putval = putcdouble;
626 }
627 return;
628 case 'i': /* integer */
629 if (brightonly) {
630 getval = getbint;
631 putval = putbint;
632 } else {
633 getval = getcint;
634 putval = putcint;
635 }
636 return;
637 case 'b': /* byte */
638 if (brightonly) {
639 getval = getbbyte;
640 putval = putbbyte;
641 } else {
642 getval = getcbyte;
643 putval = putcbyte;
644 }
645 return;
646 }
647 }