ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 1.6
Committed: Fri Apr 19 09:00:55 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +48 -15 lines
Log Message:
added format info to header

File Contents

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