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> |
10 |
|
|
11 |
|
#include "platform.h" |
12 |
|
#include "color.h" |
13 |
|
#include "resolu.h" |
14 |
+ |
#include "rtio.h" |
15 |
|
|
16 |
< |
int rgbinp = -1; /* input is RGBE? */ |
17 |
< |
|
16 |
> |
enum {InpUNK, InpRGB, InpXYZ, InpSPEC}; /* input format */ |
17 |
> |
int infmt = InpUNK; |
18 |
|
int rgbout = 0; /* output should be RGBE? */ |
21 |
– |
|
19 |
|
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
23 |
– |
|
20 |
|
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
25 |
– |
|
21 |
|
double expcomp = 1.0; /* exposure compensation */ |
27 |
– |
|
22 |
|
int doflat = -1; /* produce flat file? */ |
23 |
< |
|
23 |
> |
double origexp = -1.0; /* original exposure */ |
24 |
|
char *progname; |
25 |
|
|
26 |
+ |
static gethfunc headline; |
27 |
+ |
static void quiterr(char *err); |
28 |
+ |
static void myreadscan(COLOR *scn, int len); |
29 |
+ |
static void convert(void); |
30 |
|
|
31 |
< |
int |
32 |
< |
headline(s) /* process header line */ |
33 |
< |
char *s; |
31 |
> |
|
32 |
> |
|
33 |
> |
static int |
34 |
> |
headline( /* process header line */ |
35 |
> |
char *s, |
36 |
> |
void *p |
37 |
> |
) |
38 |
|
{ |
39 |
< |
char fmt[32]; |
39 |
> |
char fmt[MAXFMTLEN]; |
40 |
|
|
41 |
|
if (formatval(fmt, s)) { /* check if format string */ |
42 |
|
if (!strcmp(fmt,COLRFMT)) |
43 |
< |
rgbinp = 1; |
43 |
> |
infmt = InpRGB; |
44 |
|
else if (!strcmp(fmt,CIEFMT)) |
45 |
< |
rgbinp = 0; |
45 |
> |
infmt = InpXYZ; |
46 |
> |
else if (!strcmp(fmt,SPECFMT)) |
47 |
> |
infmt = InpSPEC; |
48 |
|
else |
49 |
< |
rgbinp = -2; |
49 |
> |
infmt = InpUNK; |
50 |
|
return(0); /* don't echo */ |
51 |
|
} |
52 |
+ |
if (origexp > 0.0 && isexpos(s)) { |
53 |
+ |
origexp *= exposval(s); |
54 |
+ |
return(0); /* don't echo */ |
55 |
+ |
} |
56 |
|
if (isprims(s)) { /* get input primaries */ |
57 |
|
primsval(inprims, s); |
58 |
|
return(0); /* don't echo */ |
59 |
|
} |
60 |
+ |
if (iswlsplit(s)) { /* get wavelength limits */ |
61 |
+ |
wlsplitval(WLPART, s); |
62 |
+ |
return(0); /* don't echo */ |
63 |
+ |
} |
64 |
+ |
if (isncomp(s)) { /* number of color samples */ |
65 |
+ |
NCSAMP = ncompval(s); |
66 |
+ |
return(0); /* don't echo */ |
67 |
+ |
} |
68 |
|
/* should I grok colcorr also? */ |
69 |
|
return(fputs(s, stdout)); |
70 |
|
} |
71 |
|
|
72 |
|
|
73 |
< |
main(argc, argv) |
74 |
< |
int argc; |
59 |
< |
char *argv[]; |
73 |
> |
int |
74 |
> |
main(int argc, char *argv[]) |
75 |
|
{ |
76 |
|
int i; |
77 |
|
SET_DEFAULT_BINARY(); |
103 |
|
outprims[WHT][CIEX] = atof(argv[++i]); |
104 |
|
outprims[WHT][CIEY] = atof(argv[++i]); |
105 |
|
break; |
106 |
+ |
case 'o': /* original exposure */ |
107 |
+ |
origexp = 1.0; |
108 |
+ |
break; |
109 |
|
case 'e': /* exposure compensation */ |
110 |
|
expcomp = atof(argv[++i]); |
111 |
|
if (argv[i][0] == '+' || argv[i][0] == '-') |
132 |
|
exit(1); |
133 |
|
} |
134 |
|
getheader(stdin, headline, NULL); |
135 |
< |
if (rgbinp == -2) |
136 |
< |
quiterr("unrecognized input file format"); |
119 |
< |
if (rgbinp == -1) |
120 |
< |
rgbinp = !rgbout; |
135 |
> |
if (infmt == InpUNK) |
136 |
> |
quiterr("unrecognized/missing input file format"); |
137 |
|
printargs(argc, argv, stdout); /* add to header */ |
138 |
|
convert(); /* convert picture */ |
139 |
|
exit(0); |
140 |
|
userr: |
141 |
< |
fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname); |
141 |
> |
fprintf(stderr, "Usage: %s [-r][-o][-e exp][-c|-u]", progname); |
142 |
|
fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n"); |
143 |
|
exit(1); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
< |
quiterr(err) /* print message and exit */ |
148 |
< |
char *err; |
147 |
> |
static void |
148 |
> |
quiterr( /* print message and exit */ |
149 |
> |
char *err |
150 |
> |
) |
151 |
|
{ |
152 |
|
if (err != NULL) { |
153 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
< |
convert() /* convert to XYZE or RGBE picture */ |
160 |
> |
static void |
161 |
> |
myreadscan(COLOR *scn, int len) |
162 |
|
{ |
163 |
+ |
if (infmt == InpSPEC) { /* read & convert to XYZ */ |
164 |
+ |
SCOLR sclr; |
165 |
+ |
SCOLOR scol; |
166 |
+ |
while (len-- > 0) { |
167 |
+ |
if (getbinary(sclr, LSCOLR, 1, stdin) != 1) |
168 |
+ |
goto readerr; |
169 |
+ |
scolr_scolor(scol, sclr); |
170 |
+ |
scolor_cie(*scn++, scol); |
171 |
+ |
} |
172 |
+ |
return; |
173 |
+ |
} /* else read as RGBE/XYZE */ |
174 |
+ |
if (freadscan(scn, len, stdin) >= 0) |
175 |
+ |
return; |
176 |
+ |
readerr: |
177 |
+ |
quiterr("error reading input picture"); |
178 |
+ |
} |
179 |
+ |
|
180 |
+ |
|
181 |
+ |
static void |
182 |
+ |
convert(void) /* convert to XYZE or RGBE picture */ |
183 |
+ |
{ |
184 |
|
int order; |
185 |
|
int xmax, ymax; |
186 |
|
COLORMAT xfm; |
187 |
< |
register COLOR *scanin; |
188 |
< |
register COLR *scanout; |
189 |
< |
double ourexp = expcomp; |
187 |
> |
COLOR *scanin; |
188 |
> |
COLR *scanout; |
189 |
> |
double exp2do = expcomp; |
190 |
> |
double exp2report = expcomp; |
191 |
|
int y; |
192 |
< |
register int x; |
192 |
> |
int x; |
193 |
> |
/* recover original? */ |
194 |
> |
if (origexp > 0.0) |
195 |
> |
exp2do /= origexp; |
196 |
|
/* compute transform */ |
197 |
|
if (rgbout) { |
198 |
< |
if (rgbinp) { /* RGBE -> RGBE */ |
198 |
> |
if (infmt == InpRGB) { /* RGBE -> RGBE */ |
199 |
|
comprgb2rgbWBmat(xfm, inprims, outprims); |
200 |
< |
} else { /* XYZE -> RGBE */ |
200 |
> |
} else { /* XYZE/Spectral -> RGBE */ |
201 |
|
compxyz2rgbWBmat(xfm, outprims); |
158 |
– |
ourexp *= WHTEFFICACY; |
202 |
|
} |
203 |
+ |
if (infmt == InpXYZ) { |
204 |
+ |
if (origexp > 0.0) |
205 |
+ |
exp2do /= WHTEFFICACY; |
206 |
+ |
else |
207 |
+ |
exp2report *= WHTEFFICACY; |
208 |
+ |
} |
209 |
|
} else { |
210 |
< |
if (rgbinp) { /* RGBE -> XYZE */ |
210 |
> |
if (infmt == InpRGB) { /* RGBE -> XYZE */ |
211 |
|
comprgb2xyzWBmat(xfm, inprims); |
212 |
< |
ourexp /= WHTEFFICACY; |
213 |
< |
} else { /* XYZE -> XYZE */ |
214 |
< |
for (y = 0; y < 3; y++) |
215 |
< |
for (x = 0; x < 3; x++) |
167 |
< |
xfm[y][x] = x==y ? 1. : 0.; |
212 |
> |
} else { /* XYZE/Spectral -> XYZE */ |
213 |
> |
memset(xfm, 0, sizeof(xfm)); |
214 |
> |
for (x = 3; x--; ) |
215 |
> |
xfm[x][x] = 1.; |
216 |
|
} |
217 |
+ |
if (infmt != InpXYZ) { |
218 |
+ |
if (origexp > 0) |
219 |
+ |
exp2do *= WHTEFFICACY; |
220 |
+ |
else |
221 |
+ |
exp2report /= WHTEFFICACY; |
222 |
+ |
} |
223 |
|
} |
224 |
|
for (y = 0; y < 3; y++) |
225 |
|
for (x = 0; x < 3; x++) |
226 |
< |
xfm[y][x] *= expcomp; |
226 |
> |
xfm[y][x] *= exp2do; |
227 |
|
/* get input resolution */ |
228 |
|
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
229 |
|
quiterr("bad picture format"); |
230 |
|
/* complete output header */ |
231 |
< |
if (ourexp < 0.99 || ourexp > 1.01) |
232 |
< |
fputexpos(ourexp, stdout); |
231 |
> |
if ((exp2report < 0.99) | (exp2report > 1.01)) |
232 |
> |
fputexpos(exp2report, stdout); |
233 |
|
if (rgbout) { |
234 |
|
fputprims(outprims, stdout); |
235 |
|
fputformat(COLRFMT, stdout); |
241 |
|
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
242 |
|
if (scanin == NULL) |
243 |
|
quiterr("out of memory in convert"); |
244 |
< |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL; |
244 |
> |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : (COLR *)NULL; |
245 |
|
/* convert image */ |
246 |
|
for (y = 0; y < ymax; y++) { |
247 |
< |
if (freadscan(scanin, xmax, stdin) < 0) |
194 |
< |
quiterr("error reading input picture"); |
247 |
> |
myreadscan(scanin, xmax); |
248 |
|
for (x = 0; x < xmax; x++) { |
249 |
|
colortrans(scanin[x], xfm, scanin[x]); |
250 |
|
if (rgbout) |
256 |
|
setcolr(scanout[x], colval(scanin[x],RED), |
257 |
|
colval(scanin[x],GRN), |
258 |
|
colval(scanin[x],BLU)); |
259 |
< |
fwrite((char *)scanout, sizeof(COLR), xmax, stdout); |
259 |
> |
putbinary(scanout, sizeof(COLR), xmax, stdout); |
260 |
|
} else |
261 |
|
fwritescan(scanin, xmax, stdout); |
262 |
|
if (ferror(stdout)) |
263 |
|
quiterr("error writing output picture"); |
264 |
|
} |
265 |
|
/* free scanline */ |
266 |
< |
free((void *)scanin); |
266 |
> |
free(scanin); |
267 |
|
if (scanout != NULL) |
268 |
< |
free((void *)scanout); |
268 |
> |
free(scanout); |
269 |
|
} |