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"; |
10 |
|
* 4/23/86 |
11 |
|
*/ |
12 |
|
|
13 |
< |
#include <stdio.h> |
13 |
> |
#include "standard.h" |
14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
+ |
#include "resolu.h" |
18 |
+ |
|
19 |
|
#define min(a,b) ((a)<(b)?(a):(b)) |
20 |
|
|
21 |
< |
int xres = 0; /* resolution of input */ |
20 |
< |
int yres = 0; |
21 |
> |
RESOLU picres; /* resolution of picture */ |
22 |
|
|
23 |
< |
int uniq = 0; /* unique values? */ |
23 |
> |
int uniq = 0; /* print only unique values? */ |
24 |
|
|
25 |
< |
int original = 0; /* original values? */ |
25 |
> |
int original = 0; /* convert back to original values? */ |
26 |
|
|
27 |
|
int dataonly = 0; /* data only format? */ |
28 |
|
|
31 |
|
int reverse = 0; /* reverse conversion? */ |
32 |
|
|
33 |
|
int format = 'a'; /* input/output format */ |
34 |
+ |
char *fmtid = "ascii"; /* format identifier for header */ |
35 |
|
|
36 |
|
int header = 1; /* do header */ |
37 |
|
|
38 |
< |
double exposure = 1.0; |
38 |
> |
int wrongformat = 0; /* wrong input format? */ |
39 |
|
|
40 |
+ |
double gamcor = 1.0; /* gamma correction */ |
41 |
+ |
|
42 |
+ |
COLOR exposure = WHTCOLOR; |
43 |
+ |
|
44 |
|
char *progname; |
45 |
|
|
46 |
|
FILE *fin; |
52 |
|
int argc; |
53 |
|
char **argv; |
54 |
|
{ |
49 |
– |
extern double atof(); |
55 |
|
extern int checkhead(); |
56 |
|
int i; |
57 |
|
|
58 |
|
progname = argv[0]; |
59 |
|
|
60 |
|
for (i = 1; i < argc; i++) |
61 |
< |
if (argv[i][0] == '-') |
61 |
> |
if (argv[i][0] == '-' || argv[i][0] == '+') |
62 |
|
switch (argv[i][1]) { |
63 |
< |
case 'h': /* no header */ |
64 |
< |
header = 0; |
63 |
> |
case 'h': /* header */ |
64 |
> |
header = argv[i][0] == '+'; |
65 |
|
break; |
66 |
|
case 'u': /* unique values */ |
67 |
< |
uniq = 1; |
67 |
> |
uniq = argv[i][0] == '-'; |
68 |
|
break; |
69 |
|
case 'o': /* original values */ |
70 |
< |
original = 1; |
70 |
> |
original = argv[i][0] == '-'; |
71 |
|
break; |
72 |
+ |
case 'g': /* gamma correction */ |
73 |
+ |
gamcor = atof(argv[++i]); |
74 |
+ |
if (argv[i][0] == '+') |
75 |
+ |
gamcor = 1.0/gamcor; |
76 |
+ |
break; |
77 |
|
case 'r': /* reverse conversion */ |
78 |
< |
reverse = 1; |
78 |
> |
reverse = argv[i][0] == '-'; |
79 |
|
break; |
80 |
|
case 'b': /* brightness values */ |
81 |
< |
brightonly = 1; |
81 |
> |
brightonly = argv[i][0] == '-'; |
82 |
|
break; |
83 |
|
case 'd': /* data only (no indices) */ |
84 |
< |
dataonly = 1; |
84 |
> |
dataonly = argv[i][0] == '-'; |
85 |
|
switch (argv[i][2]) { |
86 |
|
case '\0': |
87 |
|
case 'a': /* ascii */ |
88 |
|
format = 'a'; |
89 |
+ |
fmtid = "ascii"; |
90 |
|
break; |
91 |
|
case 'i': /* integer */ |
92 |
+ |
format = 'i'; |
93 |
+ |
fmtid = "ascii"; |
94 |
+ |
break; |
95 |
|
case 'b': /* byte */ |
96 |
+ |
dataonly = 1; |
97 |
+ |
format = 'b'; |
98 |
+ |
fmtid = "byte"; |
99 |
+ |
break; |
100 |
|
case 'f': /* float */ |
101 |
+ |
dataonly = 1; |
102 |
+ |
format = 'f'; |
103 |
+ |
fmtid = "float"; |
104 |
+ |
break; |
105 |
|
case 'd': /* double */ |
106 |
< |
format = argv[i][2]; |
106 |
> |
dataonly = 1; |
107 |
> |
format = 'd'; |
108 |
> |
fmtid = "double"; |
109 |
|
break; |
110 |
|
default: |
111 |
|
goto unkopt; |
112 |
|
} |
113 |
|
break; |
114 |
|
case 'x': /* x resolution */ |
115 |
< |
xres = atoi(argv[++i]); |
115 |
> |
if (argv[i][0] == '-') |
116 |
> |
picres.or |= XDECR; |
117 |
> |
picres.xr = atoi(argv[++i]); |
118 |
|
break; |
119 |
|
case 'y': /* y resolution */ |
120 |
< |
yres = atoi(argv[++i]); |
120 |
> |
if (argv[i][0] == '-') |
121 |
> |
picres.or |= YDECR; |
122 |
> |
if (picres.xr == 0) |
123 |
> |
picres.or |= YMAJOR; |
124 |
> |
picres.yr = atoi(argv[++i]); |
125 |
|
break; |
126 |
|
default: |
127 |
|
unkopt: |
132 |
|
} |
133 |
|
else |
134 |
|
break; |
135 |
< |
|
135 |
> |
/* recognize special formats */ |
136 |
> |
if (dataonly && format == 'b') |
137 |
> |
if (brightonly) |
138 |
> |
fmtid = "8-bit_grey"; |
139 |
> |
else |
140 |
> |
fmtid = "24-bit_rgb"; |
141 |
> |
|
142 |
|
if (i == argc) { |
143 |
|
fin = stdin; |
144 |
|
} else if (i == argc-1) { |
155 |
|
set_io(); |
156 |
|
|
157 |
|
if (reverse) { |
158 |
< |
if (header) /* get header */ |
159 |
< |
copyheader(fin, stdout); |
160 |
< |
/* 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); |
158 |
> |
/* get header */ |
159 |
> |
if (header && checkheader(fin, fmtid, stdout) < 0) { |
160 |
> |
fprintf(stderr, "%s: wrong input format\n", progname); |
161 |
|
quit(1); |
162 |
|
} |
163 |
< |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
163 |
> |
if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */ |
164 |
> |
if (!fgetsresolu(&picres, fin)) { |
165 |
> |
fprintf(stderr, "%s: missing resolution\n", |
166 |
> |
progname); |
167 |
> |
quit(1); |
168 |
> |
} |
169 |
> |
/* add to header */ |
170 |
> |
printargs(i, argv, stdout); |
171 |
> |
fputformat(COLRFMT, stdout); |
172 |
> |
putchar('\n'); |
173 |
> |
fputsresolu(&picres, stdout); |
174 |
|
valtopix(); |
175 |
|
} else { |
176 |
|
/* get header */ |
177 |
< |
getheader(fin, checkhead); |
177 |
> |
getheader(fin, checkhead, NULL); |
178 |
> |
if (wrongformat) { |
179 |
> |
fprintf(stderr, "%s: input not a Radiance picture\n", |
180 |
> |
progname); |
181 |
> |
quit(1); |
182 |
> |
} |
183 |
|
|
184 |
< |
if (xres <= 0 || yres <= 0) /* get picture size */ |
185 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
186 |
< |
fprintf(stderr, |
141 |
< |
"%s: missing x and y resolution\n", |
184 |
> |
if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */ |
185 |
> |
if (!fgetsresolu(&picres, fin)) { |
186 |
> |
fprintf(stderr, "%s: missing resolution\n", |
187 |
|
progname); |
188 |
|
quit(1); |
189 |
|
} |
190 |
|
if (header) { |
191 |
|
printargs(i, argv, stdout); |
192 |
< |
printf("\n"); |
192 |
> |
fputformat(fmtid, stdout); |
193 |
> |
putchar('\n'); |
194 |
|
} |
195 |
|
pixtoval(); |
196 |
|
} |
202 |
|
checkhead(line) /* deal with line from header */ |
203 |
|
char *line; |
204 |
|
{ |
205 |
< |
if (header) |
205 |
> |
char fmt[32]; |
206 |
> |
double d; |
207 |
> |
COLOR ctmp; |
208 |
> |
|
209 |
> |
if (isformat(line)) { |
210 |
> |
formatval(fmt, line); |
211 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
212 |
> |
} else if (original && isexpos(line)) { |
213 |
> |
d = 1.0/exposval(line); |
214 |
> |
scalecolor(exposure, d); |
215 |
> |
} else if (original && iscolcor(line)) { |
216 |
> |
colcorval(ctmp, line); |
217 |
> |
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
218 |
> |
colval(exposure,GRN)/colval(ctmp,GRN), |
219 |
> |
colval(exposure,BLU)/colval(ctmp,BLU)); |
220 |
> |
} else if (header) |
221 |
|
fputs(line, stdout); |
161 |
– |
if (isexpos(line)) |
162 |
– |
exposure *= exposval(line); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
pixtoval() /* convert picture to values */ |
226 |
|
{ |
227 |
< |
COLOR *scanln; |
227 |
> |
register COLOR *scanln; |
228 |
> |
int dogamma; |
229 |
|
COLOR lastc; |
230 |
+ |
FLOAT hv[2]; |
231 |
|
int y; |
232 |
|
register int x; |
233 |
|
|
234 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
234 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
235 |
|
if (scanln == NULL) { |
236 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
237 |
|
quit(1); |
238 |
|
} |
239 |
+ |
dogamma = gamcor < .95 || gamcor > 1.05; |
240 |
|
setcolor(lastc, 0.0, 0.0, 0.0); |
241 |
< |
for (y = yres-1; y >= 0; y--) { |
242 |
< |
if (freadscan(scanln, xres, fin) < 0) { |
241 |
> |
for (y = 0; y < numscans(&picres); y++) { |
242 |
> |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
243 |
|
fprintf(stderr, "%s: read error\n", progname); |
244 |
|
quit(1); |
245 |
|
} |
246 |
< |
for (x = 0; x < xres; x++) { |
246 |
> |
for (x = 0; x < scanlen(&picres); x++) { |
247 |
|
if (uniq) |
248 |
< |
if ( scanln[x][RED] == lastc[RED] && |
249 |
< |
scanln[x][GRN] == lastc[GRN] && |
250 |
< |
scanln[x][BLU] == lastc[BLU] ) |
248 |
> |
if ( colval(scanln[x],RED) == |
249 |
> |
colval(lastc,RED) && |
250 |
> |
colval(scanln[x],GRN) == |
251 |
> |
colval(lastc,GRN) && |
252 |
> |
colval(scanln[x],BLU) == |
253 |
> |
colval(lastc,BLU) ) |
254 |
|
continue; |
255 |
|
else |
256 |
|
copycolor(lastc, scanln[x]); |
257 |
|
if (original) |
258 |
< |
scalecolor(scanln[x], 1.0/exposure); |
259 |
< |
if (!dataonly) |
260 |
< |
printf("%7d %7d ", x, y); |
258 |
> |
multcolor(scanln[x], exposure); |
259 |
> |
if (dogamma) |
260 |
> |
setcolor(scanln[x], |
261 |
> |
pow(colval(scanln[x],RED), 1.0/gamcor), |
262 |
> |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
263 |
> |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
264 |
> |
if (!dataonly) { |
265 |
> |
pix2loc(hv, &picres, x, y); |
266 |
> |
printf("%7d %7d ", (int)(hv[0]*picres.xr), |
267 |
> |
(int)(hv[1]*picres.yr)); |
268 |
> |
} |
269 |
|
if ((*putval)(scanln[x], stdout) < 0) { |
270 |
|
fprintf(stderr, "%s: write error\n", progname); |
271 |
|
quit(1); |
278 |
|
|
279 |
|
valtopix() /* convert values to a pixel file */ |
280 |
|
{ |
281 |
< |
COLOR *scanln; |
281 |
> |
int dogamma; |
282 |
> |
register COLOR *scanln; |
283 |
|
int y; |
284 |
|
register int x; |
285 |
|
|
286 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
286 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
287 |
|
if (scanln == NULL) { |
288 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
289 |
|
quit(1); |
290 |
|
} |
291 |
< |
for (y = yres-1; y >= 0; y--) { |
292 |
< |
for (x = 0; x < xres; x++) { |
291 |
> |
dogamma = gamcor < .95 || gamcor > 1.05; |
292 |
> |
for (y = 0; y < numscans(&picres); y++) { |
293 |
> |
for (x = 0; x < scanlen(&picres); x++) { |
294 |
|
if (!dataonly) |
295 |
|
fscanf(fin, "%*d %*d"); |
296 |
|
if ((*getval)(scanln[x], fin) < 0) { |
297 |
|
fprintf(stderr, "%s: read error\n", progname); |
298 |
|
quit(1); |
299 |
|
} |
300 |
+ |
if (dogamma) |
301 |
+ |
setcolor(scanln[x], |
302 |
+ |
pow(colval(scanln[x],RED), gamcor), |
303 |
+ |
pow(colval(scanln[x],GRN), gamcor), |
304 |
+ |
pow(colval(scanln[x],BLU), gamcor)); |
305 |
|
} |
306 |
< |
if (fwritescan(scanln, xres, stdout) < 0 |
227 |
< |
|| fflush(stdout) < 0) { |
306 |
> |
if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { |
307 |
|
fprintf(stderr, "%s: write error\n", progname); |
308 |
|
quit(1); |
309 |
|
} |
338 |
|
{ |
339 |
|
double vd[3]; |
340 |
|
|
341 |
< |
if (fread(vd, sizeof(double), 3, fp) != 3) |
341 |
> |
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
342 |
|
return(-1); |
343 |
|
setcolor(col, vd[0], vd[1], vd[2]); |
344 |
|
return(0); |
351 |
|
{ |
352 |
|
float vf[3]; |
353 |
|
|
354 |
< |
if (fread(vf, sizeof(float), 3, fp) != 3) |
354 |
> |
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
355 |
|
return(-1); |
356 |
|
setcolor(col, vf[0], vf[1], vf[2]); |
357 |
|
return(0); |
377 |
|
{ |
378 |
|
BYTE vb[3]; |
379 |
|
|
380 |
< |
if (fread(vb, sizeof(BYTE), 3, fp) != 3) |
380 |
> |
if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3) |
381 |
|
return(-1); |
382 |
|
setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); |
383 |
|
return(0); |
403 |
|
{ |
404 |
|
double vd; |
405 |
|
|
406 |
< |
if (fread(&vd, sizeof(double), 1, fp) != 1) |
406 |
> |
if (fread((char *)&vd, sizeof(double), 1, fp) != 1) |
407 |
|
return(-1); |
408 |
|
setcolor(col, vd, vd, vd); |
409 |
|
return(0); |
416 |
|
{ |
417 |
|
float vf; |
418 |
|
|
419 |
< |
if (fread(&vf, sizeof(float), 1, fp) != 1) |
419 |
> |
if (fread((char *)&vf, sizeof(float), 1, fp) != 1) |
420 |
|
return(-1); |
421 |
|
setcolor(col, vf, vf, vf); |
422 |
|
return(0); |
445 |
|
BYTE vb; |
446 |
|
double d; |
447 |
|
|
448 |
< |
if (fread(&vb, sizeof(BYTE), 1, fp) != 1) |
448 |
> |
if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) |
449 |
|
return(-1); |
450 |
|
d = (vb+.5)/256.; |
451 |
|
setcolor(col, d, d, d); |
475 |
|
vf[0] = colval(col,RED); |
476 |
|
vf[1] = colval(col,GRN); |
477 |
|
vf[2] = colval(col,BLU); |
478 |
< |
fwrite(vf, sizeof(float), 3, fp); |
478 |
> |
fwrite((char *)vf, sizeof(float), 3, fp); |
479 |
|
|
480 |
|
return(ferror(fp) ? -1 : 0); |
481 |
|
} |
490 |
|
vd[0] = colval(col,RED); |
491 |
|
vd[1] = colval(col,GRN); |
492 |
|
vd[2] = colval(col,BLU); |
493 |
< |
fwrite(vd, sizeof(double), 3, fp); |
493 |
> |
fwrite((char *)vd, sizeof(double), 3, fp); |
494 |
|
|
495 |
|
return(ferror(fp) ? -1 : 0); |
496 |
|
} |
522 |
|
vb[1] = min(i,255); |
523 |
|
i = colval(col,BLU)*256.; |
524 |
|
vb[2] = min(i,255); |
525 |
< |
fwrite(vb, sizeof(BYTE), 3, fp); |
525 |
> |
fwrite((char *)vb, sizeof(BYTE), 3, fp); |
526 |
|
|
527 |
|
return(ferror(fp) ? -1 : 0); |
528 |
|
} |
545 |
|
float vf; |
546 |
|
|
547 |
|
vf = bright(col); |
548 |
< |
fwrite(&vf, sizeof(float), 1, fp); |
548 |
> |
fwrite((char *)&vf, sizeof(float), 1, fp); |
549 |
|
|
550 |
|
return(ferror(fp) ? -1 : 0); |
551 |
|
} |
558 |
|
double vd; |
559 |
|
|
560 |
|
vd = bright(col); |
561 |
< |
fwrite(&vd, sizeof(double), 1, fp); |
561 |
> |
fwrite((char *)&vd, sizeof(double), 1, fp); |
562 |
|
|
563 |
|
return(ferror(fp) ? -1 : 0); |
564 |
|
} |
583 |
|
|
584 |
|
i = bright(col)*256.; |
585 |
|
vb = min(i,255); |
586 |
< |
fwrite(&vb, sizeof(BYTE), 1, fp); |
586 |
> |
fwrite((char *)&vb, sizeof(BYTE), 1, fp); |
587 |
|
|
588 |
|
return(ferror(fp) ? -1 : 0); |
589 |
|
} |