ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pvalue.c
Revision: 1.1
Committed: Thu Feb 2 10:49:31 1989 UTC (35 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

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