ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.1
Committed: Tue Nov 12 16:04:16 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +0 -0 lines
Log Message:
updated revision number for release 2.0

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 wrongformat = 0; /* wrong input format? */
39
40 double gamcor = 1.0; /* gamma correction */
41
42 COLOR exposure = WHTCOLOR;
43
44 char *progname;
45
46 FILE *fin;
47
48 extern double atof(), pow();
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 'u': /* unique values */
69 uniq = argv[i][0] == '-';
70 break;
71 case 'o': /* original values */
72 original = argv[i][0] == '-';
73 break;
74 case 'g': /* gamma correction */
75 gamcor = atof(argv[++i]);
76 if (argv[i][0] == '+')
77 gamcor = 1.0/gamcor;
78 break;
79 case 'r': /* reverse conversion */
80 reverse = argv[i][0] == '-';
81 break;
82 case 'b': /* brightness values */
83 brightonly = argv[i][0] == '-';
84 break;
85 case 'd': /* data only (no indices) */
86 dataonly = argv[i][0] == '-';
87 switch (argv[i][2]) {
88 case '\0':
89 case 'a': /* ascii */
90 format = 'a';
91 fmtid = "ascii";
92 break;
93 case 'i': /* integer */
94 format = 'i';
95 fmtid = "ascii";
96 break;
97 case 'b': /* byte */
98 dataonly = 1;
99 format = 'b';
100 fmtid = "byte";
101 break;
102 case 'f': /* float */
103 dataonly = 1;
104 format = 'f';
105 fmtid = "float";
106 break;
107 case 'd': /* double */
108 dataonly = 1;
109 format = 'd';
110 fmtid = "double";
111 break;
112 default:
113 goto unkopt;
114 }
115 break;
116 case 'x': /* x resolution */
117 if (argv[i][0] == '-')
118 picres.or |= XDECR;
119 picres.xr = atoi(argv[++i]);
120 break;
121 case 'y': /* y resolution */
122 if (argv[i][0] == '-')
123 picres.or |= YDECR;
124 if (picres.xr == 0)
125 picres.or |= YMAJOR;
126 picres.yr = atoi(argv[++i]);
127 break;
128 default:
129 unkopt:
130 fprintf(stderr, "%s: unknown option: %s\n",
131 progname, argv[i]);
132 quit(1);
133 break;
134 }
135 else
136 break;
137 /* recognize special formats */
138 if (dataonly && format == 'b')
139 if (brightonly)
140 fmtid = "8-bit_grey";
141 else
142 fmtid = "24-bit_rgb";
143
144 if (i == argc) {
145 fin = stdin;
146 } else if (i == argc-1) {
147 if ((fin = fopen(argv[i], "r")) == NULL) {
148 fprintf(stderr, "%s: can't open file \"%s\"\n",
149 progname, argv[i]);
150 quit(1);
151 }
152 } else {
153 fprintf(stderr, "%s: bad # file arguments\n", progname);
154 quit(1);
155 }
156
157 set_io();
158
159 if (reverse) {
160 /* get header */
161 if (header && checkheader(fin, fmtid, stdout) < 0) {
162 fprintf(stderr, "%s: wrong input format\n", progname);
163 quit(1);
164 }
165 if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */
166 if (!fgetsresolu(&picres, fin)) {
167 fprintf(stderr, "%s: missing resolution\n",
168 progname);
169 quit(1);
170 }
171 /* add to header */
172 printargs(i, argv, stdout);
173 fputformat(COLRFMT, stdout);
174 putchar('\n');
175 fputsresolu(&picres, stdout);
176 valtopix();
177 } else {
178 /* get header */
179 getheader(fin, checkhead, NULL);
180 if (wrongformat) {
181 fprintf(stderr, "%s: input not a Radiance picture\n",
182 progname);
183 quit(1);
184 }
185
186 if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */
187 if (!fgetsresolu(&picres, fin)) {
188 fprintf(stderr, "%s: missing resolution\n",
189 progname);
190 quit(1);
191 }
192 if (header) {
193 printargs(i, argv, stdout);
194 fputformat(fmtid, stdout);
195 putchar('\n');
196 }
197 pixtoval();
198 }
199
200 quit(0);
201 }
202
203
204 checkhead(line) /* deal with line from header */
205 char *line;
206 {
207 char fmt[32];
208 double d;
209 COLOR ctmp;
210
211 if (isformat(line)) {
212 formatval(fmt, line);
213 wrongformat = strcmp(fmt, COLRFMT);
214 } else if (original && isexpos(line)) {
215 d = 1.0/exposval(line);
216 scalecolor(exposure, d);
217 } else if (original && iscolcor(line)) {
218 colcorval(ctmp, line);
219 setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED),
220 colval(exposure,GRN)/colval(ctmp,GRN),
221 colval(exposure,BLU)/colval(ctmp,BLU));
222 } else if (header)
223 fputs(line, stdout);
224 }
225
226
227 pixtoval() /* convert picture to values */
228 {
229 register COLOR *scanln;
230 int dogamma;
231 COLOR lastc;
232 FLOAT hv[2];
233 int y;
234 register int x;
235
236 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
237 if (scanln == NULL) {
238 fprintf(stderr, "%s: out of memory\n", progname);
239 quit(1);
240 }
241 dogamma = gamcor < .95 || gamcor > 1.05;
242 setcolor(lastc, 0.0, 0.0, 0.0);
243 for (y = 0; y < numscans(&picres); y++) {
244 if (freadscan(scanln, scanlen(&picres), fin) < 0) {
245 fprintf(stderr, "%s: read error\n", progname);
246 quit(1);
247 }
248 for (x = 0; x < scanlen(&picres); x++) {
249 if (uniq)
250 if ( colval(scanln[x],RED) ==
251 colval(lastc,RED) &&
252 colval(scanln[x],GRN) ==
253 colval(lastc,GRN) &&
254 colval(scanln[x],BLU) ==
255 colval(lastc,BLU) )
256 continue;
257 else
258 copycolor(lastc, scanln[x]);
259 if (original)
260 multcolor(scanln[x], exposure);
261 if (dogamma)
262 setcolor(scanln[x],
263 pow(colval(scanln[x],RED), 1.0/gamcor),
264 pow(colval(scanln[x],GRN), 1.0/gamcor),
265 pow(colval(scanln[x],BLU), 1.0/gamcor));
266 if (!dataonly) {
267 pix2loc(hv, &picres, x, y);
268 printf("%7d %7d ", (int)(hv[0]*picres.xr),
269 (int)(hv[1]*picres.yr));
270 }
271 if ((*putval)(scanln[x], stdout) < 0) {
272 fprintf(stderr, "%s: write error\n", progname);
273 quit(1);
274 }
275 }
276 }
277 free((char *)scanln);
278 }
279
280
281 valtopix() /* convert values to a pixel file */
282 {
283 int dogamma;
284 register COLOR *scanln;
285 int y;
286 register int x;
287
288 scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR));
289 if (scanln == NULL) {
290 fprintf(stderr, "%s: out of memory\n", progname);
291 quit(1);
292 }
293 dogamma = gamcor < .95 || gamcor > 1.05;
294 for (y = 0; y < numscans(&picres); y++) {
295 for (x = 0; x < scanlen(&picres); x++) {
296 if (!dataonly)
297 fscanf(fin, "%*d %*d");
298 if ((*getval)(scanln[x], fin) < 0) {
299 fprintf(stderr, "%s: read error\n", progname);
300 quit(1);
301 }
302 if (dogamma)
303 setcolor(scanln[x],
304 pow(colval(scanln[x],RED), gamcor),
305 pow(colval(scanln[x],GRN), gamcor),
306 pow(colval(scanln[x],BLU), gamcor));
307 }
308 if (fwritescan(scanln, scanlen(&picres), stdout) < 0) {
309 fprintf(stderr, "%s: write error\n", progname);
310 quit(1);
311 }
312 }
313 free((char *)scanln);
314 }
315
316
317 quit(code)
318 int code;
319 {
320 exit(code);
321 }
322
323
324 getcascii(col, fp) /* get an ascii color value from fp */
325 COLOR col;
326 FILE *fp;
327 {
328 double vd[3];
329
330 if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3)
331 return(-1);
332 setcolor(col, vd[0], vd[1], vd[2]);
333 return(0);
334 }
335
336
337 getcdouble(col, fp) /* get a double color value from fp */
338 COLOR col;
339 FILE *fp;
340 {
341 double vd[3];
342
343 if (fread((char *)vd, sizeof(double), 3, fp) != 3)
344 return(-1);
345 setcolor(col, vd[0], vd[1], vd[2]);
346 return(0);
347 }
348
349
350 getcfloat(col, fp) /* get a float color value from fp */
351 COLOR col;
352 FILE *fp;
353 {
354 float vf[3];
355
356 if (fread((char *)vf, sizeof(float), 3, fp) != 3)
357 return(-1);
358 setcolor(col, vf[0], vf[1], vf[2]);
359 return(0);
360 }
361
362
363 getcint(col, fp) /* get an int color value from fp */
364 COLOR col;
365 FILE *fp;
366 {
367 int vi[3];
368
369 if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3)
370 return(-1);
371 setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.);
372 return(0);
373 }
374
375
376 getcbyte(col, fp) /* get a byte color value from fp */
377 COLOR col;
378 FILE *fp;
379 {
380 BYTE vb[3];
381
382 if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3)
383 return(-1);
384 setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.);
385 return(0);
386 }
387
388
389 getbascii(col, fp) /* get an ascii brightness value from fp */
390 COLOR col;
391 FILE *fp;
392 {
393 double vd;
394
395 if (fscanf(fp, "%lf", &vd) != 1)
396 return(-1);
397 setcolor(col, vd, vd, vd);
398 return(0);
399 }
400
401
402 getbdouble(col, fp) /* get a double brightness value from fp */
403 COLOR col;
404 FILE *fp;
405 {
406 double vd;
407
408 if (fread((char *)&vd, sizeof(double), 1, fp) != 1)
409 return(-1);
410 setcolor(col, vd, vd, vd);
411 return(0);
412 }
413
414
415 getbfloat(col, fp) /* get a float brightness value from fp */
416 COLOR col;
417 FILE *fp;
418 {
419 float vf;
420
421 if (fread((char *)&vf, sizeof(float), 1, fp) != 1)
422 return(-1);
423 setcolor(col, vf, vf, vf);
424 return(0);
425 }
426
427
428 getbint(col, fp) /* get an int brightness value from fp */
429 COLOR col;
430 FILE *fp;
431 {
432 int vi;
433 double d;
434
435 if (fscanf(fp, "%d", &vi) != 1)
436 return(-1);
437 d = (vi+.5)/256.;
438 setcolor(col, d, d, d);
439 return(0);
440 }
441
442
443 getbbyte(col, fp) /* get a byte brightness value from fp */
444 COLOR col;
445 FILE *fp;
446 {
447 BYTE vb;
448 double d;
449
450 if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1)
451 return(-1);
452 d = (vb+.5)/256.;
453 setcolor(col, d, d, d);
454 return(0);
455 }
456
457
458 putcascii(col, fp) /* put an ascii color to fp */
459 COLOR col;
460 FILE *fp;
461 {
462 fprintf(fp, "%15.3e %15.3e %15.3e\n",
463 colval(col,RED),
464 colval(col,GRN),
465 colval(col,BLU));
466
467 return(ferror(fp) ? -1 : 0);
468 }
469
470
471 putcfloat(col, fp) /* put a float color to fp */
472 COLOR col;
473 FILE *fp;
474 {
475 float vf[3];
476
477 vf[0] = colval(col,RED);
478 vf[1] = colval(col,GRN);
479 vf[2] = colval(col,BLU);
480 fwrite((char *)vf, sizeof(float), 3, fp);
481
482 return(ferror(fp) ? -1 : 0);
483 }
484
485
486 putcdouble(col, fp) /* put a double color to fp */
487 COLOR col;
488 FILE *fp;
489 {
490 double vd[3];
491
492 vd[0] = colval(col,RED);
493 vd[1] = colval(col,GRN);
494 vd[2] = colval(col,BLU);
495 fwrite((char *)vd, sizeof(double), 3, fp);
496
497 return(ferror(fp) ? -1 : 0);
498 }
499
500
501 putcint(col, fp) /* put an int color to fp */
502 COLOR col;
503 FILE *fp;
504 {
505 fprintf(fp, "%d %d %d\n",
506 (int)(colval(col,RED)*256.),
507 (int)(colval(col,GRN)*256.),
508 (int)(colval(col,BLU)*256.));
509
510 return(ferror(fp) ? -1 : 0);
511 }
512
513
514 putcbyte(col, fp) /* put a byte color to fp */
515 COLOR col;
516 FILE *fp;
517 {
518 register int i;
519 BYTE vb[3];
520
521 i = colval(col,RED)*256.;
522 vb[0] = min(i,255);
523 i = colval(col,GRN)*256.;
524 vb[1] = min(i,255);
525 i = colval(col,BLU)*256.;
526 vb[2] = min(i,255);
527 fwrite((char *)vb, sizeof(BYTE), 3, fp);
528
529 return(ferror(fp) ? -1 : 0);
530 }
531
532
533 putbascii(col, fp) /* put an ascii brightness to fp */
534 COLOR col;
535 FILE *fp;
536 {
537 fprintf(fp, "%15.3e\n", bright(col));
538
539 return(ferror(fp) ? -1 : 0);
540 }
541
542
543 putbfloat(col, fp) /* put a float brightness to fp */
544 COLOR col;
545 FILE *fp;
546 {
547 float vf;
548
549 vf = bright(col);
550 fwrite((char *)&vf, sizeof(float), 1, fp);
551
552 return(ferror(fp) ? -1 : 0);
553 }
554
555
556 putbdouble(col, fp) /* put a double brightness to fp */
557 COLOR col;
558 FILE *fp;
559 {
560 double vd;
561
562 vd = bright(col);
563 fwrite((char *)&vd, sizeof(double), 1, fp);
564
565 return(ferror(fp) ? -1 : 0);
566 }
567
568
569 putbint(col, fp) /* put an int brightness to fp */
570 COLOR col;
571 FILE *fp;
572 {
573 fprintf(fp, "%d\n", (int)(bright(col)*256.));
574
575 return(ferror(fp) ? -1 : 0);
576 }
577
578
579 putbbyte(col, fp) /* put a byte brightness to fp */
580 COLOR col;
581 FILE *fp;
582 {
583 register int i;
584 BYTE vb;
585
586 i = bright(col)*256.;
587 vb = min(i,255);
588 fwrite((char *)&vb, sizeof(BYTE), 1, fp);
589
590 return(ferror(fp) ? -1 : 0);
591 }
592
593
594 set_io() /* set put and get functions */
595 {
596 switch (format) {
597 case 'a': /* ascii */
598 if (brightonly) {
599 getval = getbascii;
600 putval = putbascii;
601 } else {
602 getval = getcascii;
603 putval = putcascii;
604 }
605 return;
606 case 'f': /* binary float */
607 if (brightonly) {
608 getval = getbfloat;
609 putval = putbfloat;
610 } else {
611 getval = getcfloat;
612 putval = putcfloat;
613 }
614 return;
615 case 'd': /* binary double */
616 if (brightonly) {
617 getval = getbdouble;
618 putval = putbdouble;
619 } else {
620 getval = getcdouble;
621 putval = putcdouble;
622 }
623 return;
624 case 'i': /* integer */
625 if (brightonly) {
626 getval = getbint;
627 putval = putbint;
628 } else {
629 getval = getcint;
630 putval = putcint;
631 }
632 return;
633 case 'b': /* byte */
634 if (brightonly) {
635 getval = getbbyte;
636 putval = putbbyte;
637 } else {
638 getval = getcbyte;
639 putval = putcbyte;
640 }
641 return;
642 }
643 }