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