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 |
COLOR exposure = WHTCOLOR; |
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 |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
133 |
valtopix(); |
134 |
} else { |
135 |
/* get header */ |
136 |
getheader(fin, checkhead); |
137 |
|
138 |
if (xres <= 0 || yres <= 0) /* get picture size */ |
139 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
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 |
double d; |
160 |
COLOR ctmp; |
161 |
|
162 |
if (header) |
163 |
fputs(line, stdout); |
164 |
if (isexpos(line)) { |
165 |
d = 1.0/exposval(line); |
166 |
scalecolor(exposure, d); |
167 |
} else if (iscolcor(line)) { |
168 |
colcorval(ctmp, line); |
169 |
colval(exposure,RED) /= colval(ctmp,RED); |
170 |
colval(exposure,GRN) /= colval(ctmp,GRN); |
171 |
colval(exposure,BLU) /= colval(ctmp,BLU); |
172 |
} |
173 |
} |
174 |
|
175 |
|
176 |
pixtoval() /* convert picture to values */ |
177 |
{ |
178 |
COLOR *scanln; |
179 |
COLOR lastc; |
180 |
int y; |
181 |
register int x; |
182 |
|
183 |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
184 |
if (scanln == NULL) { |
185 |
fprintf(stderr, "%s: out of memory\n", progname); |
186 |
quit(1); |
187 |
} |
188 |
setcolor(lastc, 0.0, 0.0, 0.0); |
189 |
for (y = yres-1; y >= 0; y--) { |
190 |
if (freadscan(scanln, xres, fin) < 0) { |
191 |
fprintf(stderr, "%s: read error\n", progname); |
192 |
quit(1); |
193 |
} |
194 |
for (x = 0; x < xres; x++) { |
195 |
if (uniq) |
196 |
if ( scanln[x][RED] == lastc[RED] && |
197 |
scanln[x][GRN] == lastc[GRN] && |
198 |
scanln[x][BLU] == lastc[BLU] ) |
199 |
continue; |
200 |
else |
201 |
copycolor(lastc, scanln[x]); |
202 |
if (original) |
203 |
multcolor(scanln[x], exposure); |
204 |
if (!dataonly) |
205 |
printf("%7d %7d ", x, y); |
206 |
if ((*putval)(scanln[x], stdout) < 0) { |
207 |
fprintf(stderr, "%s: write error\n", progname); |
208 |
quit(1); |
209 |
} |
210 |
} |
211 |
} |
212 |
free((char *)scanln); |
213 |
} |
214 |
|
215 |
|
216 |
valtopix() /* convert values to a pixel file */ |
217 |
{ |
218 |
COLOR *scanln; |
219 |
int y; |
220 |
register int x; |
221 |
|
222 |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
223 |
if (scanln == NULL) { |
224 |
fprintf(stderr, "%s: out of memory\n", progname); |
225 |
quit(1); |
226 |
} |
227 |
for (y = yres-1; y >= 0; y--) { |
228 |
for (x = 0; x < xres; x++) { |
229 |
if (!dataonly) |
230 |
fscanf(fin, "%*d %*d"); |
231 |
if ((*getval)(scanln[x], fin) < 0) { |
232 |
fprintf(stderr, "%s: read error\n", progname); |
233 |
quit(1); |
234 |
} |
235 |
} |
236 |
if (fwritescan(scanln, xres, stdout) < 0 |
237 |
|| fflush(stdout) < 0) { |
238 |
fprintf(stderr, "%s: write error\n", progname); |
239 |
quit(1); |
240 |
} |
241 |
} |
242 |
free((char *)scanln); |
243 |
} |
244 |
|
245 |
|
246 |
quit(code) |
247 |
int code; |
248 |
{ |
249 |
exit(code); |
250 |
} |
251 |
|
252 |
|
253 |
getcascii(col, fp) /* get an ascii color value from fp */ |
254 |
COLOR col; |
255 |
FILE *fp; |
256 |
{ |
257 |
double vd[3]; |
258 |
|
259 |
if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) |
260 |
return(-1); |
261 |
setcolor(col, vd[0], vd[1], vd[2]); |
262 |
return(0); |
263 |
} |
264 |
|
265 |
|
266 |
getcdouble(col, fp) /* get a double color value from fp */ |
267 |
COLOR col; |
268 |
FILE *fp; |
269 |
{ |
270 |
double vd[3]; |
271 |
|
272 |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
273 |
return(-1); |
274 |
setcolor(col, vd[0], vd[1], vd[2]); |
275 |
return(0); |
276 |
} |
277 |
|
278 |
|
279 |
getcfloat(col, fp) /* get a float color value from fp */ |
280 |
COLOR col; |
281 |
FILE *fp; |
282 |
{ |
283 |
float vf[3]; |
284 |
|
285 |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
286 |
return(-1); |
287 |
setcolor(col, vf[0], vf[1], vf[2]); |
288 |
return(0); |
289 |
} |
290 |
|
291 |
|
292 |
getcint(col, fp) /* get an int color value from fp */ |
293 |
COLOR col; |
294 |
FILE *fp; |
295 |
{ |
296 |
int vi[3]; |
297 |
|
298 |
if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) |
299 |
return(-1); |
300 |
setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.); |
301 |
return(0); |
302 |
} |
303 |
|
304 |
|
305 |
getcbyte(col, fp) /* get a byte color value from fp */ |
306 |
COLOR col; |
307 |
FILE *fp; |
308 |
{ |
309 |
BYTE vb[3]; |
310 |
|
311 |
if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3) |
312 |
return(-1); |
313 |
setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); |
314 |
return(0); |
315 |
} |
316 |
|
317 |
|
318 |
getbascii(col, fp) /* get an ascii brightness value from fp */ |
319 |
COLOR col; |
320 |
FILE *fp; |
321 |
{ |
322 |
double vd; |
323 |
|
324 |
if (fscanf(fp, "%lf", &vd) != 1) |
325 |
return(-1); |
326 |
setcolor(col, vd, vd, vd); |
327 |
return(0); |
328 |
} |
329 |
|
330 |
|
331 |
getbdouble(col, fp) /* get a double brightness value from fp */ |
332 |
COLOR col; |
333 |
FILE *fp; |
334 |
{ |
335 |
double vd; |
336 |
|
337 |
if (fread((char *)&vd, sizeof(double), 1, fp) != 1) |
338 |
return(-1); |
339 |
setcolor(col, vd, vd, vd); |
340 |
return(0); |
341 |
} |
342 |
|
343 |
|
344 |
getbfloat(col, fp) /* get a float brightness value from fp */ |
345 |
COLOR col; |
346 |
FILE *fp; |
347 |
{ |
348 |
float vf; |
349 |
|
350 |
if (fread((char *)&vf, sizeof(float), 1, fp) != 1) |
351 |
return(-1); |
352 |
setcolor(col, vf, vf, vf); |
353 |
return(0); |
354 |
} |
355 |
|
356 |
|
357 |
getbint(col, fp) /* get an int brightness value from fp */ |
358 |
COLOR col; |
359 |
FILE *fp; |
360 |
{ |
361 |
int vi; |
362 |
double d; |
363 |
|
364 |
if (fscanf(fp, "%d", &vi) != 1) |
365 |
return(-1); |
366 |
d = (vi+.5)/256.; |
367 |
setcolor(col, d, d, d); |
368 |
return(0); |
369 |
} |
370 |
|
371 |
|
372 |
getbbyte(col, fp) /* get a byte brightness value from fp */ |
373 |
COLOR col; |
374 |
FILE *fp; |
375 |
{ |
376 |
BYTE vb; |
377 |
double d; |
378 |
|
379 |
if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) |
380 |
return(-1); |
381 |
d = (vb+.5)/256.; |
382 |
setcolor(col, d, d, d); |
383 |
return(0); |
384 |
} |
385 |
|
386 |
|
387 |
putcascii(col, fp) /* put an ascii color to fp */ |
388 |
COLOR col; |
389 |
FILE *fp; |
390 |
{ |
391 |
fprintf(fp, "%15.3e %15.3e %15.3e\n", |
392 |
colval(col,RED), |
393 |
colval(col,GRN), |
394 |
colval(col,BLU)); |
395 |
|
396 |
return(ferror(fp) ? -1 : 0); |
397 |
} |
398 |
|
399 |
|
400 |
putcfloat(col, fp) /* put a float color to fp */ |
401 |
COLOR col; |
402 |
FILE *fp; |
403 |
{ |
404 |
float vf[3]; |
405 |
|
406 |
vf[0] = colval(col,RED); |
407 |
vf[1] = colval(col,GRN); |
408 |
vf[2] = colval(col,BLU); |
409 |
fwrite((char *)vf, sizeof(float), 3, fp); |
410 |
|
411 |
return(ferror(fp) ? -1 : 0); |
412 |
} |
413 |
|
414 |
|
415 |
putcdouble(col, fp) /* put a double color to fp */ |
416 |
COLOR col; |
417 |
FILE *fp; |
418 |
{ |
419 |
double vd[3]; |
420 |
|
421 |
vd[0] = colval(col,RED); |
422 |
vd[1] = colval(col,GRN); |
423 |
vd[2] = colval(col,BLU); |
424 |
fwrite((char *)vd, sizeof(double), 3, fp); |
425 |
|
426 |
return(ferror(fp) ? -1 : 0); |
427 |
} |
428 |
|
429 |
|
430 |
putcint(col, fp) /* put an int color to fp */ |
431 |
COLOR col; |
432 |
FILE *fp; |
433 |
{ |
434 |
fprintf(fp, "%d %d %d\n", |
435 |
(int)(colval(col,RED)*256.), |
436 |
(int)(colval(col,GRN)*256.), |
437 |
(int)(colval(col,BLU)*256.)); |
438 |
|
439 |
return(ferror(fp) ? -1 : 0); |
440 |
} |
441 |
|
442 |
|
443 |
putcbyte(col, fp) /* put a byte color to fp */ |
444 |
COLOR col; |
445 |
FILE *fp; |
446 |
{ |
447 |
register int i; |
448 |
BYTE vb[3]; |
449 |
|
450 |
i = colval(col,RED)*256.; |
451 |
vb[0] = min(i,255); |
452 |
i = colval(col,GRN)*256.; |
453 |
vb[1] = min(i,255); |
454 |
i = colval(col,BLU)*256.; |
455 |
vb[2] = min(i,255); |
456 |
fwrite((char *)vb, sizeof(BYTE), 3, fp); |
457 |
|
458 |
return(ferror(fp) ? -1 : 0); |
459 |
} |
460 |
|
461 |
|
462 |
putbascii(col, fp) /* put an ascii brightness to fp */ |
463 |
COLOR col; |
464 |
FILE *fp; |
465 |
{ |
466 |
fprintf(fp, "%15.3e\n", bright(col)); |
467 |
|
468 |
return(ferror(fp) ? -1 : 0); |
469 |
} |
470 |
|
471 |
|
472 |
putbfloat(col, fp) /* put a float brightness to fp */ |
473 |
COLOR col; |
474 |
FILE *fp; |
475 |
{ |
476 |
float vf; |
477 |
|
478 |
vf = bright(col); |
479 |
fwrite((char *)&vf, sizeof(float), 1, fp); |
480 |
|
481 |
return(ferror(fp) ? -1 : 0); |
482 |
} |
483 |
|
484 |
|
485 |
putbdouble(col, fp) /* put a double brightness to fp */ |
486 |
COLOR col; |
487 |
FILE *fp; |
488 |
{ |
489 |
double vd; |
490 |
|
491 |
vd = bright(col); |
492 |
fwrite((char *)&vd, sizeof(double), 1, fp); |
493 |
|
494 |
return(ferror(fp) ? -1 : 0); |
495 |
} |
496 |
|
497 |
|
498 |
putbint(col, fp) /* put an int brightness to fp */ |
499 |
COLOR col; |
500 |
FILE *fp; |
501 |
{ |
502 |
fprintf(fp, "%d\n", (int)(bright(col)*256.)); |
503 |
|
504 |
return(ferror(fp) ? -1 : 0); |
505 |
} |
506 |
|
507 |
|
508 |
putbbyte(col, fp) /* put a byte brightness to fp */ |
509 |
COLOR col; |
510 |
FILE *fp; |
511 |
{ |
512 |
register int i; |
513 |
BYTE vb; |
514 |
|
515 |
i = bright(col)*256.; |
516 |
vb = min(i,255); |
517 |
fwrite((char *)&vb, sizeof(BYTE), 1, fp); |
518 |
|
519 |
return(ferror(fp) ? -1 : 0); |
520 |
} |
521 |
|
522 |
|
523 |
set_io() /* set put and get functions */ |
524 |
{ |
525 |
switch (format) { |
526 |
case 'a': /* ascii */ |
527 |
if (brightonly) { |
528 |
getval = getbascii; |
529 |
putval = putbascii; |
530 |
} else { |
531 |
getval = getcascii; |
532 |
putval = putcascii; |
533 |
} |
534 |
return; |
535 |
case 'f': /* binary float */ |
536 |
if (brightonly) { |
537 |
getval = getbfloat; |
538 |
putval = putbfloat; |
539 |
} else { |
540 |
getval = getcfloat; |
541 |
putval = putcfloat; |
542 |
} |
543 |
return; |
544 |
case 'd': /* binary double */ |
545 |
if (brightonly) { |
546 |
getval = getbdouble; |
547 |
putval = putbdouble; |
548 |
} else { |
549 |
getval = getcdouble; |
550 |
putval = putcdouble; |
551 |
} |
552 |
return; |
553 |
case 'i': /* integer */ |
554 |
if (brightonly) { |
555 |
getval = getbint; |
556 |
putval = putbint; |
557 |
} else { |
558 |
getval = getcint; |
559 |
putval = putcint; |
560 |
} |
561 |
return; |
562 |
case 'b': /* byte */ |
563 |
if (brightonly) { |
564 |
getval = getbbyte; |
565 |
putval = putbbyte; |
566 |
} else { |
567 |
getval = getcbyte; |
568 |
putval = putcbyte; |
569 |
} |
570 |
return; |
571 |
} |
572 |
} |