6 |
|
* Added white-balance adjustment 10/01 (GW). |
7 |
|
*/ |
8 |
|
|
9 |
– |
#include <stdio.h> |
10 |
– |
#include <string.h> |
9 |
|
#include <math.h> |
12 |
– |
#include <time.h> |
13 |
– |
|
10 |
|
#include "platform.h" |
11 |
|
#include "color.h" |
12 |
|
#include "resolu.h" |
13 |
|
#include "rtio.h" |
14 |
|
|
15 |
< |
int rgbinp = -1; /* input is RGBE? */ |
15 |
> |
enum {InpUNK, InpRGB, InpXYZ, InpSPEC}; /* input format */ |
16 |
> |
int infmt = InpUNK; |
17 |
|
int rgbout = 0; /* output should be RGBE? */ |
18 |
|
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
19 |
|
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
20 |
|
double expcomp = 1.0; /* exposure compensation */ |
21 |
|
int doflat = -1; /* produce flat file? */ |
22 |
|
double origexp = -1.0; /* original exposure */ |
26 |
– |
char *progname; |
23 |
|
|
24 |
|
static gethfunc headline; |
25 |
|
static void quiterr(char *err); |
26 |
+ |
static void myreadscan(COLOR *scn, int len); |
27 |
|
static void convert(void); |
28 |
|
|
29 |
|
|
38 |
|
|
39 |
|
if (formatval(fmt, s)) { /* check if format string */ |
40 |
|
if (!strcmp(fmt,COLRFMT)) |
41 |
< |
rgbinp = 1; |
41 |
> |
infmt = InpRGB; |
42 |
|
else if (!strcmp(fmt,CIEFMT)) |
43 |
< |
rgbinp = 0; |
43 |
> |
infmt = InpXYZ; |
44 |
> |
else if (!strcmp(fmt,SPECFMT)) |
45 |
> |
infmt = InpSPEC; |
46 |
|
else |
47 |
< |
rgbinp = -2; |
47 |
> |
infmt = InpUNK; |
48 |
|
return(0); /* don't echo */ |
49 |
|
} |
50 |
|
if (origexp > 0.0 && isexpos(s)) { |
55 |
|
primsval(inprims, s); |
56 |
|
return(0); /* don't echo */ |
57 |
|
} |
58 |
+ |
if (iswlsplit(s)) { /* get wavelength limits */ |
59 |
+ |
wlsplitval(WLPART, s); |
60 |
+ |
return(0); /* don't echo */ |
61 |
+ |
} |
62 |
+ |
if (isncomp(s)) { /* number of color samples */ |
63 |
+ |
NCSAMP = ncompval(s); |
64 |
+ |
return(0); /* don't echo */ |
65 |
+ |
} |
66 |
|
/* should I grok colcorr also? */ |
67 |
|
return(fputs(s, stdout)); |
68 |
|
} |
72 |
|
main(int argc, char *argv[]) |
73 |
|
{ |
74 |
|
int i; |
75 |
+ |
|
76 |
|
SET_DEFAULT_BINARY(); |
77 |
|
SET_FILE_BINARY(stdin); |
78 |
|
SET_FILE_BINARY(stdout); |
79 |
< |
progname = argv[0]; |
79 |
> |
fixargv0(argv[0]); |
80 |
|
|
81 |
|
for (i = 1; i < argc; i++) |
82 |
|
if (argv[i][0] == '-') |
131 |
|
exit(1); |
132 |
|
} |
133 |
|
getheader(stdin, headline, NULL); |
134 |
< |
if (rgbinp == -2) |
135 |
< |
quiterr("unrecognized input file format"); |
136 |
< |
if (rgbinp == -1) |
137 |
< |
rgbinp = !rgbout; |
134 |
> |
if (infmt == InpUNK) |
135 |
> |
quiterr("unrecognized/missing input file format"); |
136 |
> |
if (infmt == InpSPEC ? (NCSAMP <= 3) | (NCSAMP > MAXCSAMP) : |
137 |
> |
NCSAMP != 3) |
138 |
> |
quiterr("bad number of color components"); |
139 |
|
printargs(argc, argv, stdout); /* add to header */ |
140 |
|
convert(); /* convert picture */ |
141 |
|
exit(0); |
160 |
|
|
161 |
|
|
162 |
|
static void |
163 |
+ |
myreadscan(COLOR *scn, int len) |
164 |
+ |
{ |
165 |
+ |
if (infmt == InpSPEC) { /* read & convert to XYZ */ |
166 |
+ |
static COLOR *scomp = NULL; |
167 |
+ |
SCOLR sclr; |
168 |
+ |
SCOLOR scol; |
169 |
+ |
COLOR xyz; |
170 |
+ |
int n; |
171 |
+ |
if (scomp == NULL) { /* initialize conversion */ |
172 |
+ |
scomp = (COLOR *)malloc(sizeof(COLOR)*NCSAMP); |
173 |
+ |
if (scomp == NULL) |
174 |
+ |
quiterr("out of memory in myreadscan"); |
175 |
+ |
for (n = NCSAMP; n--; ) |
176 |
+ |
spec_cie(scomp[n], |
177 |
+ |
WLPART[0] + (WLPART[3] - WLPART[0])*(n+1)/NCSAMP, |
178 |
+ |
WLPART[0] + (WLPART[3] - WLPART[0])*n/NCSAMP); |
179 |
+ |
} |
180 |
+ |
while (len-- > 0) { |
181 |
+ |
if (getbinary(sclr, LSCOLR, 1, stdin) != 1) |
182 |
+ |
goto readerr; |
183 |
+ |
scolr_scolor(scol, sclr); |
184 |
+ |
setcolor(*scn, 0, 0, 0); |
185 |
+ |
for (n = NCSAMP; n--; ) { |
186 |
+ |
copycolor(xyz, scomp[n]); |
187 |
+ |
scalecolor(xyz, scol[n]); |
188 |
+ |
addcolor(*scn, xyz); |
189 |
+ |
} |
190 |
+ |
scn++; |
191 |
+ |
} |
192 |
+ |
return; |
193 |
+ |
} /* else read as RGBE/XYZE */ |
194 |
+ |
if (freadscan(scn, len, stdin) >= 0) |
195 |
+ |
return; |
196 |
+ |
readerr: |
197 |
+ |
quiterr("error reading input picture"); |
198 |
+ |
} |
199 |
+ |
|
200 |
+ |
|
201 |
+ |
static void |
202 |
|
convert(void) /* convert to XYZE or RGBE picture */ |
203 |
|
{ |
204 |
|
int order; |
205 |
|
int xmax, ymax; |
206 |
|
COLORMAT xfm; |
207 |
< |
register COLOR *scanin; |
208 |
< |
register COLR *scanout; |
207 |
> |
COLOR *scanin; |
208 |
> |
COLR *scanout; |
209 |
|
double exp2do = expcomp; |
210 |
|
double exp2report = expcomp; |
211 |
|
int y; |
212 |
< |
register int x; |
212 |
> |
int x; |
213 |
|
/* recover original? */ |
214 |
|
if (origexp > 0.0) |
215 |
|
exp2do /= origexp; |
216 |
|
/* compute transform */ |
217 |
|
if (rgbout) { |
218 |
< |
if (rgbinp) { /* RGBE -> RGBE */ |
218 |
> |
if (infmt == InpRGB) { /* RGBE -> RGBE */ |
219 |
|
comprgb2rgbWBmat(xfm, inprims, outprims); |
220 |
< |
} else { /* XYZE -> RGBE */ |
220 |
> |
} else { /* XYZE/Spectral -> RGBE */ |
221 |
|
compxyz2rgbWBmat(xfm, outprims); |
222 |
+ |
} |
223 |
+ |
if (infmt == InpXYZ) { |
224 |
|
if (origexp > 0.0) |
225 |
|
exp2do /= WHTEFFICACY; |
226 |
|
else |
227 |
|
exp2report *= WHTEFFICACY; |
228 |
|
} |
229 |
|
} else { |
230 |
< |
if (rgbinp) { /* RGBE -> XYZE */ |
230 |
> |
if (infmt == InpRGB) { /* RGBE -> XYZE */ |
231 |
|
comprgb2xyzWBmat(xfm, inprims); |
232 |
< |
if (origexp > 0.0) |
232 |
> |
} else { /* XYZE/Spectral -> XYZE */ |
233 |
> |
memset(xfm, 0, sizeof(xfm)); |
234 |
> |
for (x = 3; x--; ) |
235 |
> |
xfm[x][x] = 1.; |
236 |
> |
} |
237 |
> |
if (infmt != InpXYZ) { |
238 |
> |
if (origexp > 0) |
239 |
|
exp2do *= WHTEFFICACY; |
240 |
|
else |
241 |
|
exp2report /= WHTEFFICACY; |
186 |
– |
} else { /* XYZE -> XYZE */ |
187 |
– |
for (y = 0; y < 3; y++) |
188 |
– |
for (x = 0; x < 3; x++) |
189 |
– |
xfm[y][x] = x==y ? 1. : 0.; |
242 |
|
} |
243 |
|
} |
244 |
|
for (y = 0; y < 3; y++) |
261 |
|
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
262 |
|
if (scanin == NULL) |
263 |
|
quiterr("out of memory in convert"); |
264 |
< |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL; |
264 |
> |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : (COLR *)NULL; |
265 |
|
/* convert image */ |
266 |
|
for (y = 0; y < ymax; y++) { |
267 |
< |
if (freadscan(scanin, xmax, stdin) < 0) |
216 |
< |
quiterr("error reading input picture"); |
267 |
> |
myreadscan(scanin, xmax); |
268 |
|
for (x = 0; x < xmax; x++) { |
269 |
|
colortrans(scanin[x], xfm, scanin[x]); |
270 |
|
if (rgbout) |
276 |
|
setcolr(scanout[x], colval(scanin[x],RED), |
277 |
|
colval(scanin[x],GRN), |
278 |
|
colval(scanin[x],BLU)); |
279 |
< |
putbinary((char *)scanout, sizeof(COLR), xmax, stdout); |
279 |
> |
putbinary(scanout, sizeof(COLR), xmax, stdout); |
280 |
|
} else |
281 |
|
fwritescan(scanin, xmax, stdout); |
282 |
|
if (ferror(stdout)) |
283 |
|
quiterr("error writing output picture"); |
284 |
|
} |
285 |
|
/* free scanline */ |
286 |
< |
free((void *)scanin); |
286 |
> |
free(scanin); |
287 |
|
if (scanout != NULL) |
288 |
< |
free((void *)scanout); |
288 |
> |
free(scanout); |
289 |
|
} |