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; |
47 |
|
|
48 |
+ |
extern double atof(), pow(); |
49 |
+ |
|
50 |
|
int (*getval)(), (*putval)(); |
51 |
|
|
52 |
|
|
54 |
|
int argc; |
55 |
|
char **argv; |
56 |
|
{ |
49 |
– |
extern double atof(); |
57 |
|
extern int checkhead(); |
58 |
|
int i; |
59 |
|
|
60 |
|
progname = argv[0]; |
61 |
|
|
62 |
|
for (i = 1; i < argc; i++) |
63 |
< |
if (argv[i][0] == '-') |
63 |
> |
if (argv[i][0] == '-' || argv[i][0] == '+') |
64 |
|
switch (argv[i][1]) { |
65 |
< |
case 'h': /* no header */ |
66 |
< |
header = 0; |
65 |
> |
case 'h': /* header */ |
66 |
> |
header = argv[i][0] == '+'; |
67 |
|
break; |
68 |
|
case 'u': /* unique values */ |
69 |
< |
uniq = 1; |
69 |
> |
uniq = argv[i][0] == '-'; |
70 |
|
break; |
71 |
|
case 'o': /* original values */ |
72 |
< |
original = 1; |
72 |
> |
original = argv[i][0] == '-'; |
73 |
|
break; |
74 |
+ |
case 'g': /* gamma correction */ |
75 |
+ |
gamcor = atof(argv[++i]); |
76 |
+ |
if (argv[i][0] == '+') |
77 |
+ |
gamcor = 1.0/gamcor; |
78 |
+ |
break; |
79 |
|
case 'r': /* reverse conversion */ |
80 |
< |
reverse = 1; |
80 |
> |
reverse = argv[i][0] == '-'; |
81 |
|
break; |
82 |
|
case 'b': /* brightness values */ |
83 |
< |
brightonly = 1; |
83 |
> |
brightonly = argv[i][0] == '-'; |
84 |
|
break; |
85 |
|
case 'd': /* data only (no indices) */ |
86 |
< |
dataonly = 1; |
86 |
> |
dataonly = argv[i][0] == '-'; |
87 |
|
switch (argv[i][2]) { |
88 |
|
case '\0': |
89 |
|
case 'a': /* ascii */ |
90 |
|
format = 'a'; |
91 |
+ |
fmtid = "ascii"; |
92 |
|
break; |
93 |
|
case 'i': /* integer */ |
94 |
+ |
format = 'i'; |
95 |
+ |
fmtid = "ascii"; |
96 |
+ |
break; |
97 |
|
case 'b': /* byte */ |
98 |
+ |
dataonly = 1; |
99 |
+ |
format = 'b'; |
100 |
+ |
fmtid = "byte"; |
101 |
+ |
break; |
102 |
|
case 'f': /* float */ |
103 |
+ |
dataonly = 1; |
104 |
+ |
format = 'f'; |
105 |
+ |
fmtid = "float"; |
106 |
+ |
break; |
107 |
|
case 'd': /* double */ |
108 |
< |
format = argv[i][2]; |
108 |
> |
dataonly = 1; |
109 |
> |
format = 'd'; |
110 |
> |
fmtid = "double"; |
111 |
|
break; |
112 |
|
default: |
113 |
|
goto unkopt; |
114 |
|
} |
115 |
|
break; |
116 |
|
case 'x': /* x resolution */ |
117 |
< |
xres = atoi(argv[++i]); |
117 |
> |
if (argv[i][0] == '-') |
118 |
> |
picres.or |= XDECR; |
119 |
> |
picres.xr = atoi(argv[++i]); |
120 |
|
break; |
121 |
|
case 'y': /* y resolution */ |
122 |
< |
yres = atoi(argv[++i]); |
122 |
> |
if (argv[i][0] == '-') |
123 |
> |
picres.or |= YDECR; |
124 |
> |
if (picres.xr == 0) |
125 |
> |
picres.or |= YMAJOR; |
126 |
> |
picres.yr = atoi(argv[++i]); |
127 |
|
break; |
128 |
|
default: |
129 |
|
unkopt: |
134 |
|
} |
135 |
|
else |
136 |
|
break; |
137 |
< |
|
137 |
> |
/* recognize special formats */ |
138 |
> |
if (dataonly && format == 'b') |
139 |
> |
if (brightonly) |
140 |
> |
fmtid = "8-bit_grey"; |
141 |
> |
else |
142 |
> |
fmtid = "24-bit_rgb"; |
143 |
> |
|
144 |
|
if (i == argc) { |
145 |
|
fin = stdin; |
146 |
|
} else if (i == argc-1) { |
157 |
|
set_io(); |
158 |
|
|
159 |
|
if (reverse) { |
160 |
< |
if (header) /* get header */ |
161 |
< |
copyheader(fin, stdout); |
162 |
< |
/* 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); |
160 |
> |
/* get header */ |
161 |
> |
if (header && checkheader(fin, fmtid, stdout) < 0) { |
162 |
> |
fprintf(stderr, "%s: wrong input format\n", progname); |
163 |
|
quit(1); |
164 |
|
} |
165 |
< |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
165 |
> |
if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */ |
166 |
> |
if (!fgetsresolu(&picres, fin)) { |
167 |
> |
fprintf(stderr, "%s: missing resolution\n", |
168 |
> |
progname); |
169 |
> |
quit(1); |
170 |
> |
} |
171 |
> |
/* add to header */ |
172 |
> |
printargs(i, argv, stdout); |
173 |
> |
fputformat(COLRFMT, stdout); |
174 |
> |
putchar('\n'); |
175 |
> |
fputsresolu(&picres, stdout); |
176 |
|
valtopix(); |
177 |
|
} else { |
178 |
|
/* get header */ |
179 |
< |
getheader(fin, checkhead); |
179 |
> |
getheader(fin, checkhead, NULL); |
180 |
> |
if (wrongformat) { |
181 |
> |
fprintf(stderr, "%s: input not a Radiance picture\n", |
182 |
> |
progname); |
183 |
> |
quit(1); |
184 |
> |
} |
185 |
|
|
186 |
< |
if (xres <= 0 || yres <= 0) /* get picture size */ |
187 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
188 |
< |
fprintf(stderr, |
141 |
< |
"%s: missing x and y resolution\n", |
186 |
> |
if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */ |
187 |
> |
if (!fgetsresolu(&picres, fin)) { |
188 |
> |
fprintf(stderr, "%s: missing resolution\n", |
189 |
|
progname); |
190 |
|
quit(1); |
191 |
|
} |
192 |
|
if (header) { |
193 |
|
printargs(i, argv, stdout); |
194 |
< |
printf("\n"); |
194 |
> |
fputformat(fmtid, stdout); |
195 |
> |
putchar('\n'); |
196 |
|
} |
197 |
|
pixtoval(); |
198 |
|
} |
204 |
|
checkhead(line) /* deal with line from header */ |
205 |
|
char *line; |
206 |
|
{ |
207 |
< |
if (header) |
207 |
> |
char fmt[32]; |
208 |
> |
double d; |
209 |
> |
COLOR ctmp; |
210 |
> |
|
211 |
> |
if (isformat(line)) { |
212 |
> |
formatval(fmt, line); |
213 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
214 |
> |
} else if (original && isexpos(line)) { |
215 |
> |
d = 1.0/exposval(line); |
216 |
> |
scalecolor(exposure, d); |
217 |
> |
} else if (original && iscolcor(line)) { |
218 |
> |
colcorval(ctmp, line); |
219 |
> |
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
220 |
> |
colval(exposure,GRN)/colval(ctmp,GRN), |
221 |
> |
colval(exposure,BLU)/colval(ctmp,BLU)); |
222 |
> |
} else if (header) |
223 |
|
fputs(line, stdout); |
161 |
– |
if (isexpos(line)) |
162 |
– |
exposure *= exposval(line); |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
pixtoval() /* convert picture to values */ |
228 |
|
{ |
229 |
< |
COLOR *scanln; |
229 |
> |
register COLOR *scanln; |
230 |
> |
int dogamma; |
231 |
|
COLOR lastc; |
232 |
+ |
FLOAT hv[2]; |
233 |
|
int y; |
234 |
|
register int x; |
235 |
|
|
236 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
236 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
237 |
|
if (scanln == NULL) { |
238 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
239 |
|
quit(1); |
240 |
|
} |
241 |
+ |
dogamma = gamcor < .95 || gamcor > 1.05; |
242 |
|
setcolor(lastc, 0.0, 0.0, 0.0); |
243 |
< |
for (y = yres-1; y >= 0; y--) { |
244 |
< |
if (freadscan(scanln, xres, fin) < 0) { |
243 |
> |
for (y = 0; y < numscans(&picres); y++) { |
244 |
> |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
245 |
|
fprintf(stderr, "%s: read error\n", progname); |
246 |
|
quit(1); |
247 |
|
} |
248 |
< |
for (x = 0; x < xres; x++) { |
248 |
> |
for (x = 0; x < scanlen(&picres); x++) { |
249 |
|
if (uniq) |
250 |
< |
if ( scanln[x][RED] == lastc[RED] && |
251 |
< |
scanln[x][GRN] == lastc[GRN] && |
252 |
< |
scanln[x][BLU] == lastc[BLU] ) |
250 |
> |
if ( colval(scanln[x],RED) == |
251 |
> |
colval(lastc,RED) && |
252 |
> |
colval(scanln[x],GRN) == |
253 |
> |
colval(lastc,GRN) && |
254 |
> |
colval(scanln[x],BLU) == |
255 |
> |
colval(lastc,BLU) ) |
256 |
|
continue; |
257 |
|
else |
258 |
|
copycolor(lastc, scanln[x]); |
259 |
|
if (original) |
260 |
< |
scalecolor(scanln[x], 1.0/exposure); |
261 |
< |
if (!dataonly) |
262 |
< |
printf("%7d %7d ", x, y); |
260 |
> |
multcolor(scanln[x], exposure); |
261 |
> |
if (dogamma) |
262 |
> |
setcolor(scanln[x], |
263 |
> |
pow(colval(scanln[x],RED), 1.0/gamcor), |
264 |
> |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
265 |
> |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
266 |
> |
if (!dataonly) { |
267 |
> |
pix2loc(hv, &picres, x, y); |
268 |
> |
printf("%7d %7d ", (int)(hv[0]*picres.xr), |
269 |
> |
(int)(hv[1]*picres.yr)); |
270 |
> |
} |
271 |
|
if ((*putval)(scanln[x], stdout) < 0) { |
272 |
|
fprintf(stderr, "%s: write error\n", progname); |
273 |
|
quit(1); |
280 |
|
|
281 |
|
valtopix() /* convert values to a pixel file */ |
282 |
|
{ |
283 |
< |
COLOR *scanln; |
283 |
> |
int dogamma; |
284 |
> |
register COLOR *scanln; |
285 |
|
int y; |
286 |
|
register int x; |
287 |
|
|
288 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
288 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
289 |
|
if (scanln == NULL) { |
290 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
291 |
|
quit(1); |
292 |
|
} |
293 |
< |
for (y = yres-1; y >= 0; y--) { |
294 |
< |
for (x = 0; x < xres; x++) { |
293 |
> |
dogamma = gamcor < .95 || gamcor > 1.05; |
294 |
> |
for (y = 0; y < numscans(&picres); y++) { |
295 |
> |
for (x = 0; x < scanlen(&picres); x++) { |
296 |
|
if (!dataonly) |
297 |
|
fscanf(fin, "%*d %*d"); |
298 |
|
if ((*getval)(scanln[x], fin) < 0) { |
299 |
|
fprintf(stderr, "%s: read error\n", progname); |
300 |
|
quit(1); |
301 |
|
} |
302 |
+ |
if (dogamma) |
303 |
+ |
setcolor(scanln[x], |
304 |
+ |
pow(colval(scanln[x],RED), gamcor), |
305 |
+ |
pow(colval(scanln[x],GRN), gamcor), |
306 |
+ |
pow(colval(scanln[x],BLU), gamcor)); |
307 |
|
} |
308 |
< |
if (fwritescan(scanln, xres, stdout) < 0 |
227 |
< |
|| fflush(stdout) < 0) { |
308 |
> |
if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { |
309 |
|
fprintf(stderr, "%s: write error\n", progname); |
310 |
|
quit(1); |
311 |
|
} |