ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 2.2
Committed: Thu Dec 19 14:52:04 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +0 -2 lines
Log Message:
eliminated atof declarations for NeXT

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