1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
16 |
|
|
17 |
|
#include "resolu.h" |
18 |
|
|
19 |
< |
#define min(a,b) ((a)<(b)?(a):(b)) |
19 |
> |
#define min(a,b) ((a)<(b)?(a):(b)) |
20 |
|
|
21 |
< |
RESOLU picres; /* resolution of picture */ |
21 |
> |
RESOLU picres; /* resolution of picture */ |
22 |
|
|
23 |
|
int uniq = 0; /* print only unique values? */ |
24 |
|
|
25 |
< |
int original = 0; /* convert back to original values? */ |
25 |
> |
int doexposure = 0; /* exposure change? (>100 to print) */ |
26 |
|
|
27 |
|
int dataonly = 0; /* data only format? */ |
28 |
|
|
33 |
|
int format = 'a'; /* input/output format */ |
34 |
|
char *fmtid = "ascii"; /* format identifier for header */ |
35 |
|
|
36 |
< |
int header = 1; /* do header */ |
36 |
> |
int header = 1; /* do header? */ |
37 |
|
|
38 |
+ |
int resolution = 1; /* put/get resolution string? */ |
39 |
+ |
|
40 |
+ |
int original = 0; /* convert to original values? */ |
41 |
+ |
|
42 |
|
int wrongformat = 0; /* wrong input format? */ |
43 |
|
|
44 |
< |
double gamcor = 1.0; /* gamma correction */ |
44 |
> |
double gamcor = 1.0; /* gamma correction */ |
45 |
|
|
46 |
+ |
int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ |
47 |
+ |
int rord[4]; /* reverse ordering */ |
48 |
+ |
|
49 |
|
COLOR exposure = WHTCOLOR; |
50 |
|
|
51 |
|
char *progname; |
52 |
|
|
53 |
|
FILE *fin; |
54 |
|
|
48 |
– |
extern double atof(), pow(); |
49 |
– |
|
55 |
|
int (*getval)(), (*putval)(); |
56 |
|
|
57 |
|
|
60 |
|
char **argv; |
61 |
|
{ |
62 |
|
extern int checkhead(); |
63 |
+ |
double d, expval = 1.0; |
64 |
|
int i; |
65 |
|
|
66 |
|
progname = argv[0]; |
71 |
|
case 'h': /* header */ |
72 |
|
header = argv[i][0] == '+'; |
73 |
|
break; |
74 |
+ |
case 'H': /* resolution string */ |
75 |
+ |
resolution = argv[i][0] == '+'; |
76 |
+ |
break; |
77 |
|
case 'u': /* unique values */ |
78 |
|
uniq = argv[i][0] == '-'; |
79 |
|
break; |
81 |
|
original = argv[i][0] == '-'; |
82 |
|
break; |
83 |
|
case 'g': /* gamma correction */ |
84 |
< |
gamcor = atof(argv[++i]); |
84 |
> |
gamcor = atof(argv[i+1]); |
85 |
|
if (argv[i][0] == '+') |
86 |
|
gamcor = 1.0/gamcor; |
87 |
+ |
i++; |
88 |
|
break; |
89 |
+ |
case 'e': /* exposure correction */ |
90 |
+ |
d = atof(argv[i+1]); |
91 |
+ |
if (argv[i+1][0] == '-' || argv[i+1][0] == '+') |
92 |
+ |
d = pow(2.0, d); |
93 |
+ |
if (argv[i][0] == '-') |
94 |
+ |
doexposure = 100; |
95 |
+ |
scalecolor(exposure, d); |
96 |
+ |
expval *= d; |
97 |
+ |
doexposure++; |
98 |
+ |
i++; |
99 |
+ |
break; |
100 |
+ |
case 'R': /* reverse byte sequence */ |
101 |
+ |
if (argv[i][0] == '-') { |
102 |
+ |
ord[0]=BLU; ord[1]=GRN; ord[2]=RED; |
103 |
+ |
} else { |
104 |
+ |
ord[0]=RED; ord[1]=GRN; ord[2]=BLU; |
105 |
+ |
} |
106 |
+ |
break; |
107 |
|
case 'r': /* reverse conversion */ |
108 |
|
reverse = argv[i][0] == '-'; |
109 |
|
break; |
142 |
|
} |
143 |
|
break; |
144 |
|
case 'x': /* x resolution */ |
145 |
+ |
case 'X': /* x resolution */ |
146 |
+ |
resolution = 0; |
147 |
|
if (argv[i][0] == '-') |
148 |
|
picres.or |= XDECR; |
149 |
|
picres.xr = atoi(argv[++i]); |
150 |
|
break; |
151 |
|
case 'y': /* y resolution */ |
152 |
+ |
case 'Y': /* y resolution */ |
153 |
+ |
resolution = 0; |
154 |
|
if (argv[i][0] == '-') |
155 |
|
picres.or |= YDECR; |
156 |
|
if (picres.xr == 0) |
172 |
|
fmtid = "8-bit_grey"; |
173 |
|
else |
174 |
|
fmtid = "24-bit_rgb"; |
175 |
< |
|
175 |
> |
/* assign reverse ordering */ |
176 |
> |
rord[ord[0]] = 0; |
177 |
> |
rord[ord[1]] = 1; |
178 |
> |
rord[ord[2]] = 2; |
179 |
> |
/* get input */ |
180 |
|
if (i == argc) { |
181 |
|
fin = stdin; |
182 |
|
} else if (i == argc-1) { |
193 |
|
set_io(); |
194 |
|
|
195 |
|
if (reverse) { |
196 |
+ |
#ifdef MSDOS |
197 |
+ |
setmode(fileno(stdout), O_BINARY); |
198 |
+ |
if (format != 'a' && format != 'i') |
199 |
+ |
setmode(fileno(fin), O_BINARY); |
200 |
+ |
#endif |
201 |
|
/* get header */ |
202 |
< |
if (header && checkheader(fin, fmtid, stdout) < 0) { |
203 |
< |
fprintf(stderr, "%s: wrong input format\n", progname); |
204 |
< |
quit(1); |
164 |
< |
} |
165 |
< |
if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */ |
166 |
< |
if (!fgetsresolu(&picres, fin)) { |
167 |
< |
fprintf(stderr, "%s: missing resolution\n", |
202 |
> |
if (header) { |
203 |
> |
if (checkheader(fin, fmtid, stdout) < 0) { |
204 |
> |
fprintf(stderr, "%s: wrong input format\n", |
205 |
|
progname); |
206 |
|
quit(1); |
207 |
|
} |
208 |
+ |
} else |
209 |
+ |
newheader("RADIANCE", stdout); |
210 |
+ |
/* get resolution */ |
211 |
+ |
if ((resolution && !fgetsresolu(&picres, fin)) || |
212 |
+ |
picres.xr <= 0 || picres.yr <= 0) { |
213 |
+ |
fprintf(stderr, "%s: missing resolution\n", progname); |
214 |
+ |
quit(1); |
215 |
+ |
} |
216 |
|
/* add to header */ |
217 |
|
printargs(i, argv, stdout); |
218 |
+ |
if (doexposure > 100) |
219 |
+ |
fputexpos(expval, stdout); |
220 |
|
fputformat(COLRFMT, stdout); |
221 |
|
putchar('\n'); |
222 |
< |
fputsresolu(&picres, stdout); |
222 |
> |
fputsresolu(&picres, stdout); /* always put resolution */ |
223 |
|
valtopix(); |
224 |
|
} else { |
225 |
+ |
#ifdef MSDOS |
226 |
+ |
setmode(fileno(fin), O_BINARY); |
227 |
+ |
if (format != 'a' && format != 'i') |
228 |
+ |
setmode(fileno(stdout), O_BINARY); |
229 |
+ |
#endif |
230 |
|
/* get header */ |
231 |
|
getheader(fin, checkhead, NULL); |
232 |
|
if (wrongformat) { |
234 |
|
progname); |
235 |
|
quit(1); |
236 |
|
} |
237 |
< |
|
238 |
< |
if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */ |
239 |
< |
if (!fgetsresolu(&picres, fin)) { |
240 |
< |
fprintf(stderr, "%s: missing resolution\n", |
189 |
< |
progname); |
190 |
< |
quit(1); |
191 |
< |
} |
237 |
> |
if (!fgetsresolu(&picres, fin)) { |
238 |
> |
fprintf(stderr, "%s: missing resolution\n", progname); |
239 |
> |
quit(1); |
240 |
> |
} |
241 |
|
if (header) { |
242 |
|
printargs(i, argv, stdout); |
243 |
+ |
if (doexposure > 100) |
244 |
+ |
fputexpos(expval, stdout); |
245 |
|
fputformat(fmtid, stdout); |
246 |
|
putchar('\n'); |
247 |
|
} |
248 |
+ |
if (resolution) /* put resolution */ |
249 |
+ |
fputsresolu(&picres, stdout); |
250 |
|
pixtoval(); |
251 |
|
} |
252 |
|
|
261 |
|
double d; |
262 |
|
COLOR ctmp; |
263 |
|
|
264 |
< |
if (isformat(line)) { |
212 |
< |
formatval(fmt, line); |
264 |
> |
if (formatval(fmt, line)) |
265 |
|
wrongformat = strcmp(fmt, COLRFMT); |
266 |
< |
} else if (original && isexpos(line)) { |
266 |
> |
else if (original && isexpos(line)) { |
267 |
|
d = 1.0/exposval(line); |
268 |
|
scalecolor(exposure, d); |
269 |
+ |
doexposure++; |
270 |
|
} else if (original && iscolcor(line)) { |
271 |
|
colcorval(ctmp, line); |
272 |
|
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
273 |
|
colval(exposure,GRN)/colval(ctmp,GRN), |
274 |
|
colval(exposure,BLU)/colval(ctmp,BLU)); |
275 |
+ |
doexposure++; |
276 |
|
} else if (header) |
277 |
|
fputs(line, stdout); |
278 |
|
} |
280 |
|
|
281 |
|
pixtoval() /* convert picture to values */ |
282 |
|
{ |
283 |
< |
register COLOR *scanln; |
283 |
> |
register COLOR *scanln; |
284 |
|
int dogamma; |
285 |
|
COLOR lastc; |
286 |
|
FLOAT hv[2]; |
310 |
|
continue; |
311 |
|
else |
312 |
|
copycolor(lastc, scanln[x]); |
313 |
< |
if (original) |
313 |
> |
if (doexposure) |
314 |
|
multcolor(scanln[x], exposure); |
315 |
|
if (dogamma) |
316 |
|
setcolor(scanln[x], |
335 |
|
valtopix() /* convert values to a pixel file */ |
336 |
|
{ |
337 |
|
int dogamma; |
338 |
< |
register COLOR *scanln; |
338 |
> |
register COLOR *scanln; |
339 |
|
int y; |
340 |
|
register int x; |
341 |
|
|
358 |
|
pow(colval(scanln[x],RED), gamcor), |
359 |
|
pow(colval(scanln[x],GRN), gamcor), |
360 |
|
pow(colval(scanln[x],BLU), gamcor)); |
361 |
+ |
if (doexposure) |
362 |
+ |
multcolor(scanln[x], exposure); |
363 |
|
} |
364 |
|
if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { |
365 |
|
fprintf(stderr, "%s: write error\n", progname); |
381 |
|
COLOR col; |
382 |
|
FILE *fp; |
383 |
|
{ |
384 |
< |
double vd[3]; |
384 |
> |
double vd[3]; |
385 |
|
|
386 |
|
if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) |
387 |
|
return(-1); |
388 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
388 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
389 |
|
return(0); |
390 |
|
} |
391 |
|
|
394 |
|
COLOR col; |
395 |
|
FILE *fp; |
396 |
|
{ |
397 |
< |
double vd[3]; |
397 |
> |
double vd[3]; |
398 |
|
|
399 |
|
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
400 |
|
return(-1); |
401 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
401 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
402 |
|
return(0); |
403 |
|
} |
404 |
|
|
411 |
|
|
412 |
|
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
413 |
|
return(-1); |
414 |
< |
setcolor(col, vf[0], vf[1], vf[2]); |
414 |
> |
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
415 |
|
return(0); |
416 |
|
} |
417 |
|
|
424 |
|
|
425 |
|
if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) |
426 |
|
return(-1); |
427 |
< |
setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.); |
427 |
> |
setcolor(col, (vi[rord[RED]]+.5)/256., |
428 |
> |
(vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.); |
429 |
|
return(0); |
430 |
|
} |
431 |
|
|
438 |
|
|
439 |
|
if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3) |
440 |
|
return(-1); |
441 |
< |
setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); |
441 |
> |
setcolor(col, (vb[rord[RED]]+.5)/256., |
442 |
> |
(vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.); |
443 |
|
return(0); |
444 |
|
} |
445 |
|
|
448 |
|
COLOR col; |
449 |
|
FILE *fp; |
450 |
|
{ |
451 |
< |
double vd; |
451 |
> |
double vd; |
452 |
|
|
453 |
|
if (fscanf(fp, "%lf", &vd) != 1) |
454 |
|
return(-1); |
461 |
|
COLOR col; |
462 |
|
FILE *fp; |
463 |
|
{ |
464 |
< |
double vd; |
464 |
> |
double vd; |
465 |
|
|
466 |
|
if (fread((char *)&vd, sizeof(double), 1, fp) != 1) |
467 |
|
return(-1); |
488 |
|
FILE *fp; |
489 |
|
{ |
490 |
|
int vi; |
491 |
< |
double d; |
491 |
> |
double d; |
492 |
|
|
493 |
|
if (fscanf(fp, "%d", &vi) != 1) |
494 |
|
return(-1); |
503 |
|
FILE *fp; |
504 |
|
{ |
505 |
|
BYTE vb; |
506 |
< |
double d; |
506 |
> |
double d; |
507 |
|
|
508 |
|
if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) |
509 |
|
return(-1); |
518 |
|
FILE *fp; |
519 |
|
{ |
520 |
|
fprintf(fp, "%15.3e %15.3e %15.3e\n", |
521 |
< |
colval(col,RED), |
522 |
< |
colval(col,GRN), |
523 |
< |
colval(col,BLU)); |
521 |
> |
colval(col,ord[0]), |
522 |
> |
colval(col,ord[1]), |
523 |
> |
colval(col,ord[2])); |
524 |
|
|
525 |
|
return(ferror(fp) ? -1 : 0); |
526 |
|
} |
532 |
|
{ |
533 |
|
float vf[3]; |
534 |
|
|
535 |
< |
vf[0] = colval(col,RED); |
536 |
< |
vf[1] = colval(col,GRN); |
537 |
< |
vf[2] = colval(col,BLU); |
535 |
> |
vf[0] = colval(col,ord[0]); |
536 |
> |
vf[1] = colval(col,ord[1]); |
537 |
> |
vf[2] = colval(col,ord[2]); |
538 |
|
fwrite((char *)vf, sizeof(float), 3, fp); |
539 |
|
|
540 |
|
return(ferror(fp) ? -1 : 0); |
545 |
|
COLOR col; |
546 |
|
FILE *fp; |
547 |
|
{ |
548 |
< |
double vd[3]; |
548 |
> |
double vd[3]; |
549 |
|
|
550 |
< |
vd[0] = colval(col,RED); |
551 |
< |
vd[1] = colval(col,GRN); |
552 |
< |
vd[2] = colval(col,BLU); |
550 |
> |
vd[0] = colval(col,ord[0]); |
551 |
> |
vd[1] = colval(col,ord[1]); |
552 |
> |
vd[2] = colval(col,ord[2]); |
553 |
|
fwrite((char *)vd, sizeof(double), 3, fp); |
554 |
|
|
555 |
|
return(ferror(fp) ? -1 : 0); |
561 |
|
FILE *fp; |
562 |
|
{ |
563 |
|
fprintf(fp, "%d %d %d\n", |
564 |
< |
(int)(colval(col,RED)*256.), |
565 |
< |
(int)(colval(col,GRN)*256.), |
566 |
< |
(int)(colval(col,BLU)*256.)); |
564 |
> |
(int)(colval(col,ord[0])*256.), |
565 |
> |
(int)(colval(col,ord[1])*256.), |
566 |
> |
(int)(colval(col,ord[2])*256.)); |
567 |
|
|
568 |
|
return(ferror(fp) ? -1 : 0); |
569 |
|
} |
576 |
|
register int i; |
577 |
|
BYTE vb[3]; |
578 |
|
|
579 |
< |
i = colval(col,RED)*256.; |
579 |
> |
i = colval(col,ord[0])*256.; |
580 |
|
vb[0] = min(i,255); |
581 |
< |
i = colval(col,GRN)*256.; |
581 |
> |
i = colval(col,ord[1])*256.; |
582 |
|
vb[1] = min(i,255); |
583 |
< |
i = colval(col,BLU)*256.; |
583 |
> |
i = colval(col,ord[2])*256.; |
584 |
|
vb[2] = min(i,255); |
585 |
|
fwrite((char *)vb, sizeof(BYTE), 3, fp); |
586 |
|
|
615 |
|
COLOR col; |
616 |
|
FILE *fp; |
617 |
|
{ |
618 |
< |
double vd; |
618 |
> |
double vd; |
619 |
|
|
620 |
|
vd = bright(col); |
621 |
|
fwrite((char *)&vd, sizeof(double), 1, fp); |