1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
19 |
|
int xres = 0; /* resolution of input */ |
20 |
|
int yres = 0; |
21 |
|
|
22 |
< |
int uniq = 0; /* unique values? */ |
22 |
> |
int uniq = 0; /* print only unique values? */ |
23 |
|
|
24 |
< |
int original = 0; /* original values? */ |
24 |
> |
int original = 0; /* convert back to original values? */ |
25 |
|
|
26 |
|
int dataonly = 0; /* data only format? */ |
27 |
|
|
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 |
< |
double exposure = 1.0; |
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 |
|
|
53 |
|
int argc; |
54 |
|
char **argv; |
55 |
|
{ |
49 |
– |
extern double atof(); |
56 |
|
extern int checkhead(); |
57 |
|
int i; |
58 |
|
|
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; |
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 = argv[i][2]; |
103 |
> |
format = 'd'; |
104 |
> |
fmtid = "double"; |
105 |
|
break; |
106 |
|
default: |
107 |
|
goto unkopt; |
122 |
|
} |
123 |
|
else |
124 |
|
break; |
125 |
< |
|
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) { |
145 |
|
set_io(); |
146 |
|
|
147 |
|
if (reverse) { |
122 |
– |
if (header) /* get header */ |
123 |
– |
copyheader(fin, stdout); |
124 |
– |
/* add to header */ |
125 |
– |
printargs(i, argv, stdout); |
126 |
– |
printf("\n"); |
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); |
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)) { |
179 |
|
} |
180 |
|
if (header) { |
181 |
|
printargs(i, argv, stdout); |
182 |
+ |
fputformat(fmtid, stdout); |
183 |
|
printf("\n"); |
184 |
|
} |
185 |
|
pixtoval(); |
192 |
|
checkhead(line) /* deal with line from header */ |
193 |
|
char *line; |
194 |
|
{ |
195 |
< |
if (header) |
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); |
161 |
– |
if (isexpos(line)) |
162 |
– |
exposure *= exposval(line); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
pixtoval() /* convert picture to values */ |
216 |
|
{ |
217 |
< |
COLOR *scanln; |
217 |
> |
register COLOR *scanln; |
218 |
> |
int dogamma; |
219 |
|
COLOR lastc; |
220 |
|
int y; |
221 |
|
register int x; |
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) { |
234 |
|
} |
235 |
|
for (x = 0; x < xres; x++) { |
236 |
|
if (uniq) |
237 |
< |
if ( scanln[x][RED] == lastc[RED] && |
238 |
< |
scanln[x][GRN] == lastc[GRN] && |
239 |
< |
scanln[x][BLU] == lastc[BLU] ) |
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 |
< |
scalecolor(scanln[x], 1.0/exposure); |
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) { |
264 |
|
|
265 |
|
valtopix() /* convert values to a pixel file */ |
266 |
|
{ |
267 |
< |
COLOR *scanln; |
267 |
> |
int dogamma; |
268 |
> |
register COLOR *scanln; |
269 |
|
int y; |
270 |
|
register int x; |
271 |
|
|
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) |
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 |
227 |
< |
|| fflush(stdout) < 0) { |
292 |
> |
if (fwritescan(scanln, xres, stdout) < 0) { |
293 |
|
fprintf(stderr, "%s: write error\n", progname); |
294 |
|
quit(1); |
295 |
|
} |
324 |
|
{ |
325 |
|
double vd[3]; |
326 |
|
|
327 |
< |
if (fread(vd, sizeof(double), 3, fp) != 3) |
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); |
337 |
|
{ |
338 |
|
float vf[3]; |
339 |
|
|
340 |
< |
if (fread(vf, sizeof(float), 3, fp) != 3) |
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); |
363 |
|
{ |
364 |
|
BYTE vb[3]; |
365 |
|
|
366 |
< |
if (fread(vb, sizeof(BYTE), 3, fp) != 3) |
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); |
389 |
|
{ |
390 |
|
double vd; |
391 |
|
|
392 |
< |
if (fread(&vd, sizeof(double), 1, fp) != 1) |
392 |
> |
if (fread((char *)&vd, sizeof(double), 1, fp) != 1) |
393 |
|
return(-1); |
394 |
|
setcolor(col, vd, vd, vd); |
395 |
|
return(0); |
402 |
|
{ |
403 |
|
float vf; |
404 |
|
|
405 |
< |
if (fread(&vf, sizeof(float), 1, fp) != 1) |
405 |
> |
if (fread((char *)&vf, sizeof(float), 1, fp) != 1) |
406 |
|
return(-1); |
407 |
|
setcolor(col, vf, vf, vf); |
408 |
|
return(0); |
431 |
|
BYTE vb; |
432 |
|
double d; |
433 |
|
|
434 |
< |
if (fread(&vb, sizeof(BYTE), 1, fp) != 1) |
434 |
> |
if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) |
435 |
|
return(-1); |
436 |
|
d = (vb+.5)/256.; |
437 |
|
setcolor(col, d, d, d); |
461 |
|
vf[0] = colval(col,RED); |
462 |
|
vf[1] = colval(col,GRN); |
463 |
|
vf[2] = colval(col,BLU); |
464 |
< |
fwrite(vf, sizeof(float), 3, fp); |
464 |
> |
fwrite((char *)vf, sizeof(float), 3, fp); |
465 |
|
|
466 |
|
return(ferror(fp) ? -1 : 0); |
467 |
|
} |
476 |
|
vd[0] = colval(col,RED); |
477 |
|
vd[1] = colval(col,GRN); |
478 |
|
vd[2] = colval(col,BLU); |
479 |
< |
fwrite(vd, sizeof(double), 3, fp); |
479 |
> |
fwrite((char *)vd, sizeof(double), 3, fp); |
480 |
|
|
481 |
|
return(ferror(fp) ? -1 : 0); |
482 |
|
} |
508 |
|
vb[1] = min(i,255); |
509 |
|
i = colval(col,BLU)*256.; |
510 |
|
vb[2] = min(i,255); |
511 |
< |
fwrite(vb, sizeof(BYTE), 3, fp); |
511 |
> |
fwrite((char *)vb, sizeof(BYTE), 3, fp); |
512 |
|
|
513 |
|
return(ferror(fp) ? -1 : 0); |
514 |
|
} |
531 |
|
float vf; |
532 |
|
|
533 |
|
vf = bright(col); |
534 |
< |
fwrite(&vf, sizeof(float), 1, fp); |
534 |
> |
fwrite((char *)&vf, sizeof(float), 1, fp); |
535 |
|
|
536 |
|
return(ferror(fp) ? -1 : 0); |
537 |
|
} |
544 |
|
double vd; |
545 |
|
|
546 |
|
vd = bright(col); |
547 |
< |
fwrite(&vd, sizeof(double), 1, fp); |
547 |
> |
fwrite((char *)&vd, sizeof(double), 1, fp); |
548 |
|
|
549 |
|
return(ferror(fp) ? -1 : 0); |
550 |
|
} |
569 |
|
|
570 |
|
i = bright(col)*256.; |
571 |
|
vb = min(i,255); |
572 |
< |
fwrite(&vb, sizeof(BYTE), 1, fp); |
572 |
> |
fwrite((char *)&vb, sizeof(BYTE), 1, fp); |
573 |
|
|
574 |
|
return(ferror(fp) ? -1 : 0); |
575 |
|
} |