1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* pvalue.c - program to print pixel values. |
6 |
|
* |
11 |
|
|
12 |
|
#include "color.h" |
13 |
|
|
14 |
+ |
#include <time.h> |
15 |
+ |
|
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
|
#define min(a,b) ((a)<(b)?(a):(b)) |
42 |
|
|
43 |
|
long skipbytes = 0; /* skip bytes in input? */ |
44 |
|
|
45 |
+ |
int interleave = 1; /* file is interleaved? */ |
46 |
+ |
|
47 |
|
int resolution = 1; /* put/get resolution string? */ |
48 |
|
|
49 |
|
int original = 0; /* convert to original values? */ |
64 |
|
|
65 |
|
int (*getval)(), (*putval)(); |
66 |
|
|
67 |
+ |
double |
68 |
+ |
rgb_bright(clr) |
69 |
+ |
COLOR clr; |
70 |
+ |
{ |
71 |
+ |
return(bright(clr)); |
72 |
+ |
} |
73 |
|
|
74 |
+ |
double |
75 |
+ |
xyz_bright(clr) |
76 |
+ |
COLOR clr; |
77 |
+ |
{ |
78 |
+ |
return(clr[CIEY]); |
79 |
+ |
} |
80 |
+ |
|
81 |
+ |
double (*mybright)() = &rgb_bright; |
82 |
+ |
|
83 |
+ |
|
84 |
|
main(argc, argv) |
85 |
|
int argc; |
86 |
|
char **argv; |
136 |
|
case 'r': /* reverse conversion */ |
137 |
|
reverse = argv[i][0] == '-'; |
138 |
|
break; |
139 |
+ |
case 'n': /* non-interleaved RGB */ |
140 |
+ |
interleave = argv[i][0] == '+'; |
141 |
+ |
break; |
142 |
|
case 'b': /* brightness values */ |
143 |
|
putprim = argv[i][0] == '-' ? BRIGHT : ALL; |
144 |
|
break; |
185 |
|
case 'X': /* x resolution */ |
186 |
|
resolution = 0; |
187 |
|
if (argv[i][0] == '-') |
188 |
< |
picres.or |= XDECR; |
188 |
> |
picres.rt |= XDECR; |
189 |
|
picres.xr = atoi(argv[++i]); |
190 |
|
break; |
191 |
|
case 'y': /* y resolution */ |
192 |
|
case 'Y': /* y resolution */ |
193 |
|
resolution = 0; |
194 |
|
if (argv[i][0] == '-') |
195 |
< |
picres.or |= YDECR; |
195 |
> |
picres.rt |= YDECR; |
196 |
|
if (picres.xr == 0) |
197 |
< |
picres.or |= YMAJOR; |
197 |
> |
picres.rt |= YMAJOR; |
198 |
|
picres.yr = atoi(argv[++i]); |
199 |
|
break; |
200 |
|
default: |
225 |
|
progname, argv[i]); |
226 |
|
quit(1); |
227 |
|
} |
208 |
– |
if (skipbytes && fseek(fin, skipbytes, 0)) |
209 |
– |
goto seekerr; |
228 |
|
if (reverse && !brightonly && i == argc-3) { |
229 |
|
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
230 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
236 |
|
progname, argv[i+2]); |
237 |
|
quit(1); |
238 |
|
} |
239 |
< |
if (skipbytes && (fseek(fin2, skipbytes, 0) || |
222 |
< |
fseek(fin3, skipbytes, 0))) |
223 |
< |
goto seekerr; |
239 |
> |
interleave = -1; |
240 |
|
} else if (i != argc-1) |
241 |
|
fin = NULL; |
242 |
+ |
if (reverse && !brightonly && !interleave) { |
243 |
+ |
fin2 = fopen(argv[i], "r"); |
244 |
+ |
fin3 = fopen(argv[i], "r"); |
245 |
+ |
} |
246 |
+ |
if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && |
247 |
+ |
(fseek(fin2, skipbytes, 0) || |
248 |
+ |
fseek(fin3, skipbytes, 0))))) { |
249 |
+ |
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
250 |
+ |
progname, skipbytes); |
251 |
+ |
quit(1); |
252 |
+ |
} |
253 |
|
} |
254 |
|
if (fin == NULL) { |
255 |
|
fprintf(stderr, "%s: bad # file arguments\n", progname); |
256 |
|
quit(1); |
257 |
|
} |
258 |
|
|
232 |
– |
set_io(); |
233 |
– |
|
259 |
|
if (reverse) { |
260 |
|
#ifdef MSDOS |
261 |
|
setmode(fileno(stdout), O_BINARY); |
284 |
|
if (resolution && fin2 != NULL) { |
285 |
|
RESOLU pres2; |
286 |
|
if (!fgetsresolu(&pres2, fin2) || |
287 |
< |
pres2.or != picres.or || |
287 |
> |
pres2.rt != picres.rt || |
288 |
|
pres2.xr != picres.xr || |
289 |
|
pres2.yr != picres.yr || |
290 |
|
!fgetsresolu(&pres2, fin3) || |
291 |
< |
pres2.or != picres.or || |
291 |
> |
pres2.rt != picres.rt || |
292 |
|
pres2.xr != picres.xr || |
293 |
|
pres2.yr != picres.yr) { |
294 |
|
fprintf(stderr, "%s: resolution mismatch\n", |
313 |
|
/* get header */ |
314 |
|
getheader(fin, checkhead, NULL); |
315 |
|
if (wrongformat) { |
316 |
< |
fprintf(stderr, "%s: input not a Radiance picture\n", |
316 |
> |
fprintf(stderr, |
317 |
> |
"%s: input not a Radiance RGBE picture\n", |
318 |
|
progname); |
319 |
|
quit(1); |
320 |
|
} |
335 |
|
} |
336 |
|
|
337 |
|
quit(0); |
312 |
– |
seekerr: |
313 |
– |
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
314 |
– |
progname, skipbytes); |
315 |
– |
quit(1); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
+ |
int |
342 |
|
checkhead(line) /* deal with line from header */ |
343 |
|
char *line; |
344 |
|
{ |
346 |
|
double d; |
347 |
|
COLOR ctmp; |
348 |
|
|
349 |
< |
if (formatval(fmt, line)) |
350 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
351 |
< |
else if (original && isexpos(line)) { |
349 |
> |
if (formatval(fmt, line)) { |
350 |
> |
if (!strcmp(fmt, CIEFMT)) { |
351 |
> |
mybright = &xyz_bright; |
352 |
> |
if (original) { |
353 |
> |
scalecolor(exposure, 1./WHTEFFICACY); |
354 |
> |
doexposure++; |
355 |
> |
} |
356 |
> |
} else if (!strcmp(fmt, COLRFMT)) |
357 |
> |
mybright = &rgb_bright; |
358 |
> |
else |
359 |
> |
wrongformat++; |
360 |
> |
} else if (original && isexpos(line)) { |
361 |
|
d = 1.0/exposval(line); |
362 |
|
scalecolor(exposure, d); |
363 |
|
doexposure++; |
369 |
|
doexposure++; |
370 |
|
} else if (header) |
371 |
|
fputs(line, stdout); |
372 |
+ |
return(0); |
373 |
|
} |
374 |
|
|
375 |
|
|
379 |
|
int dogamma; |
380 |
|
COLOR lastc; |
381 |
|
FLOAT hv[2]; |
382 |
+ |
int startprim, endprim; |
383 |
+ |
long startpos; |
384 |
|
int y; |
385 |
|
register int x; |
386 |
|
|
390 |
|
quit(1); |
391 |
|
} |
392 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
393 |
< |
setcolor(lastc, 0.0, 0.0, 0.0); |
394 |
< |
for (y = 0; y < numscans(&picres); y++) { |
395 |
< |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
396 |
< |
fprintf(stderr, "%s: read error\n", progname); |
393 |
> |
if (putprim == ALL && !interleave) { |
394 |
> |
startprim = RED; endprim = BLU; |
395 |
> |
startpos = ftell(fin); |
396 |
> |
} else { |
397 |
> |
startprim = putprim; endprim = putprim; |
398 |
> |
} |
399 |
> |
for (putprim = startprim; putprim <= endprim; putprim++) { |
400 |
> |
if (putprim != startprim && fseek(fin, startpos, 0)) { |
401 |
> |
fprintf(stderr, "%s: seek error on input file\n", |
402 |
> |
progname); |
403 |
|
quit(1); |
404 |
|
} |
405 |
< |
for (x = 0; x < scanlen(&picres); x++) { |
406 |
< |
if (uniq) |
407 |
< |
if ( colval(scanln[x],RED) == |
408 |
< |
colval(lastc,RED) && |
409 |
< |
colval(scanln[x],GRN) == |
369 |
< |
colval(lastc,GRN) && |
370 |
< |
colval(scanln[x],BLU) == |
371 |
< |
colval(lastc,BLU) ) |
372 |
< |
continue; |
373 |
< |
else |
374 |
< |
copycolor(lastc, scanln[x]); |
375 |
< |
if (doexposure) |
376 |
< |
multcolor(scanln[x], exposure); |
377 |
< |
if (dogamma) |
378 |
< |
setcolor(scanln[x], |
379 |
< |
pow(colval(scanln[x],RED), 1.0/gamcor), |
380 |
< |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
381 |
< |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
382 |
< |
if (!dataonly) { |
383 |
< |
pix2loc(hv, &picres, x, y); |
384 |
< |
printf("%7d %7d ", (int)(hv[0]*picres.xr), |
385 |
< |
(int)(hv[1]*picres.yr)); |
386 |
< |
} |
387 |
< |
if ((*putval)(scanln[x]) < 0) { |
388 |
< |
fprintf(stderr, "%s: write error\n", progname); |
405 |
> |
set_io(); |
406 |
> |
setcolor(lastc, 0.0, 0.0, 0.0); |
407 |
> |
for (y = 0; y < numscans(&picres); y++) { |
408 |
> |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
409 |
> |
fprintf(stderr, "%s: read error\n", progname); |
410 |
|
quit(1); |
411 |
|
} |
412 |
+ |
for (x = 0; x < scanlen(&picres); x++) { |
413 |
+ |
if (uniq) |
414 |
+ |
if ( colval(scanln[x],RED) == |
415 |
+ |
colval(lastc,RED) && |
416 |
+ |
colval(scanln[x],GRN) == |
417 |
+ |
colval(lastc,GRN) && |
418 |
+ |
colval(scanln[x],BLU) == |
419 |
+ |
colval(lastc,BLU) ) |
420 |
+ |
continue; |
421 |
+ |
else |
422 |
+ |
copycolor(lastc, scanln[x]); |
423 |
+ |
if (doexposure) |
424 |
+ |
multcolor(scanln[x], exposure); |
425 |
+ |
if (dogamma) |
426 |
+ |
setcolor(scanln[x], |
427 |
+ |
pow(colval(scanln[x],RED), 1.0/gamcor), |
428 |
+ |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
429 |
+ |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
430 |
+ |
if (!dataonly) { |
431 |
+ |
pix2loc(hv, &picres, x, y); |
432 |
+ |
printf("%7d %7d ", |
433 |
+ |
(int)(hv[0]*picres.xr), |
434 |
+ |
(int)(hv[1]*picres.yr)); |
435 |
+ |
} |
436 |
+ |
if ((*putval)(scanln[x]) < 0) { |
437 |
+ |
fprintf(stderr, "%s: write error\n", |
438 |
+ |
progname); |
439 |
+ |
quit(1); |
440 |
+ |
} |
441 |
+ |
} |
442 |
|
} |
443 |
|
} |
444 |
< |
free((char *)scanln); |
444 |
> |
free((void *)scanln); |
445 |
|
} |
446 |
|
|
447 |
|
|
458 |
|
quit(1); |
459 |
|
} |
460 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
461 |
+ |
set_io(); |
462 |
|
for (y = 0; y < numscans(&picres); y++) { |
463 |
|
for (x = 0; x < scanlen(&picres); x++) { |
464 |
|
if (!dataonly) { |
485 |
|
quit(1); |
486 |
|
} |
487 |
|
} |
488 |
< |
free((char *)scanln); |
488 |
> |
free((void *)scanln); |
489 |
|
} |
490 |
|
|
491 |
|
|
492 |
+ |
void |
493 |
|
quit(code) |
494 |
|
int code; |
495 |
|
{ |
731 |
|
putbascii(col) /* put an ascii brightness to stdout */ |
732 |
|
COLOR col; |
733 |
|
{ |
734 |
< |
fprintf(stdout, "%15.3e\n", bright(col)); |
734 |
> |
fprintf(stdout, "%15.3e\n", (*mybright)(col)); |
735 |
|
|
736 |
|
return(ferror(stdout) ? -1 : 0); |
737 |
|
} |
742 |
|
{ |
743 |
|
float vf; |
744 |
|
|
745 |
< |
vf = bright(col); |
745 |
> |
vf = (*mybright)(col); |
746 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
747 |
|
|
748 |
|
return(ferror(stdout) ? -1 : 0); |
754 |
|
{ |
755 |
|
double vd; |
756 |
|
|
757 |
< |
vd = bright(col); |
757 |
> |
vd = (*mybright)(col); |
758 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
759 |
|
|
760 |
|
return(ferror(stdout) ? -1 : 0); |
764 |
|
putbint(col) /* put an int brightness to stdout */ |
765 |
|
COLOR col; |
766 |
|
{ |
767 |
< |
fprintf(stdout, "%d\n", (int)(bright(col)*256.)); |
767 |
> |
fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); |
768 |
|
|
769 |
|
return(ferror(stdout) ? -1 : 0); |
770 |
|
} |
776 |
|
register int i; |
777 |
|
BYTE vb; |
778 |
|
|
779 |
< |
i = bright(col)*256.; |
779 |
> |
i = (*mybright)(col)*256.; |
780 |
|
vb = min(i,255); |
781 |
|
fwrite((char *)&vb, sizeof(BYTE), 1, stdout); |
782 |
|
|
853 |
|
} else { |
854 |
|
getval = getcascii; |
855 |
|
putval = putcascii; |
856 |
+ |
if (reverse && !interleave) { |
857 |
+ |
fprintf(stderr, |
858 |
+ |
"%s: ASCII input files must be interleaved\n", |
859 |
+ |
progname); |
860 |
+ |
quit(1); |
861 |
+ |
} |
862 |
|
} |
863 |
|
return; |
864 |
|
case 'f': /* binary float */ |
871 |
|
} else { |
872 |
|
getval = getcfloat; |
873 |
|
putval = putcfloat; |
874 |
+ |
if (reverse && !interleave) { |
875 |
+ |
if (fin2 == NULL) |
876 |
+ |
goto namerr; |
877 |
+ |
if (fseek(fin2, |
878 |
+ |
(long)sizeof(float)*picres.xr*picres.yr, 1)) |
879 |
+ |
goto seekerr; |
880 |
+ |
if (fseek(fin3, |
881 |
+ |
(long)sizeof(float)*2*picres.xr*picres.yr, 1)) |
882 |
+ |
goto seekerr; |
883 |
+ |
} |
884 |
|
} |
885 |
|
return; |
886 |
|
case 'd': /* binary double */ |
893 |
|
} else { |
894 |
|
getval = getcdouble; |
895 |
|
putval = putcdouble; |
896 |
+ |
if (reverse && !interleave) { |
897 |
+ |
if (fin2 == NULL) |
898 |
+ |
goto namerr; |
899 |
+ |
if (fseek(fin2, |
900 |
+ |
(long)sizeof(double)*picres.xr*picres.yr, 1)) |
901 |
+ |
goto seekerr; |
902 |
+ |
if (fseek(fin3, |
903 |
+ |
(long)sizeof(double)*2*picres.xr*picres.yr, 1)) |
904 |
+ |
goto seekerr; |
905 |
+ |
} |
906 |
|
} |
907 |
|
return; |
908 |
|
case 'i': /* integer */ |
915 |
|
} else { |
916 |
|
getval = getcint; |
917 |
|
putval = putcint; |
918 |
+ |
if (reverse && !interleave) { |
919 |
+ |
fprintf(stderr, |
920 |
+ |
"%s: integer input files must be interleaved\n", |
921 |
+ |
progname); |
922 |
+ |
quit(1); |
923 |
+ |
} |
924 |
|
} |
925 |
|
return; |
926 |
|
case 'b': /* byte */ |
933 |
|
} else { |
934 |
|
getval = getcbyte; |
935 |
|
putval = putcbyte; |
936 |
+ |
if (reverse && !interleave) { |
937 |
+ |
if (fin2 == NULL) |
938 |
+ |
goto namerr; |
939 |
+ |
if (fseek(fin2, |
940 |
+ |
(long)sizeof(BYTE)*picres.xr*picres.yr, 1)) |
941 |
+ |
goto seekerr; |
942 |
+ |
if (fseek(fin3, |
943 |
+ |
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
944 |
+ |
goto seekerr; |
945 |
+ |
} |
946 |
|
} |
947 |
|
return; |
948 |
|
} |
949 |
+ |
badopt: |
950 |
+ |
fprintf(stderr, "%s: botched file type\n", progname); |
951 |
+ |
quit(1); |
952 |
+ |
namerr: |
953 |
+ |
fprintf(stderr, "%s: non-interleaved file(s) must be named\n", |
954 |
+ |
progname); |
955 |
+ |
quit(1); |
956 |
+ |
seekerr: |
957 |
+ |
fprintf(stderr, "%s: cannot seek on interleaved input file\n", |
958 |
+ |
progname); |
959 |
+ |
quit(1); |
960 |
|
} |