1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Program to convert between RADIANCE RGBE and XYZE formats |
6 |
+ |
* Added white-balance adjustment 10/01 (GW). |
7 |
|
*/ |
8 |
|
|
11 |
– |
#include <stdio.h> |
9 |
|
#include <math.h> |
10 |
+ |
|
11 |
+ |
#include "platform.h" |
12 |
|
#include "color.h" |
13 |
|
#include "resolu.h" |
14 |
+ |
#include "rtio.h" |
15 |
|
|
16 |
– |
#ifdef MSDOS |
17 |
– |
#include <fcntl.h> |
18 |
– |
#endif |
19 |
– |
|
20 |
– |
extern char *malloc(), *strcpy(); |
21 |
– |
|
16 |
|
int rgbinp = -1; /* input is RGBE? */ |
23 |
– |
|
17 |
|
int rgbout = 0; /* output should be RGBE? */ |
25 |
– |
|
18 |
|
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
27 |
– |
|
19 |
|
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
29 |
– |
|
20 |
|
double expcomp = 1.0; /* exposure compensation */ |
31 |
– |
|
21 |
|
int doflat = -1; /* produce flat file? */ |
22 |
< |
|
22 |
> |
double origexp = -1.0; /* original exposure */ |
23 |
|
char *progname; |
24 |
|
|
25 |
+ |
static gethfunc headline; |
26 |
+ |
static void quiterr(char *err); |
27 |
+ |
static void convert(void); |
28 |
|
|
29 |
< |
headline(s) /* process header line */ |
30 |
< |
char *s; |
29 |
> |
|
30 |
> |
|
31 |
> |
static int |
32 |
> |
headline( /* process header line */ |
33 |
> |
char *s, |
34 |
> |
void *p |
35 |
> |
) |
36 |
|
{ |
37 |
< |
char fmt[32]; |
37 |
> |
char fmt[MAXFMTLEN]; |
38 |
|
|
39 |
|
if (formatval(fmt, s)) { /* check if format string */ |
40 |
|
if (!strcmp(fmt,COLRFMT)) |
43 |
|
rgbinp = 0; |
44 |
|
else |
45 |
|
rgbinp = -2; |
46 |
< |
return; /* don't echo */ |
46 |
> |
return(0); /* don't echo */ |
47 |
|
} |
48 |
+ |
if (origexp > 0.0 && isexpos(s)) { |
49 |
+ |
origexp *= exposval(s); |
50 |
+ |
return(0); /* don't echo */ |
51 |
+ |
} |
52 |
|
if (isprims(s)) { /* get input primaries */ |
53 |
|
primsval(inprims, s); |
54 |
< |
return; /* don't echo */ |
54 |
> |
return(0); /* don't echo */ |
55 |
|
} |
56 |
|
/* should I grok colcorr also? */ |
57 |
< |
fputs(s, stdout); |
57 |
> |
return(fputs(s, stdout)); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
< |
main(argc, argv) |
62 |
< |
int argc; |
62 |
< |
char *argv[]; |
61 |
> |
int |
62 |
> |
main(int argc, char *argv[]) |
63 |
|
{ |
64 |
|
int i; |
65 |
< |
#ifdef MSDOS |
66 |
< |
extern int _fmode; |
67 |
< |
_fmode = O_BINARY; |
68 |
< |
setmode(fileno(stdin), O_BINARY); |
69 |
< |
setmode(fileno(stdout), O_BINARY); |
70 |
< |
#endif |
65 |
> |
SET_DEFAULT_BINARY(); |
66 |
> |
SET_FILE_BINARY(stdin); |
67 |
> |
SET_FILE_BINARY(stdout); |
68 |
|
progname = argv[0]; |
69 |
|
|
70 |
|
for (i = 1; i < argc; i++) |
91 |
|
outprims[WHT][CIEX] = atof(argv[++i]); |
92 |
|
outprims[WHT][CIEY] = atof(argv[++i]); |
93 |
|
break; |
94 |
+ |
case 'o': /* original exposure */ |
95 |
+ |
origexp = 1.0; |
96 |
+ |
break; |
97 |
|
case 'e': /* exposure compensation */ |
98 |
|
expcomp = atof(argv[++i]); |
99 |
|
if (argv[i][0] == '+' || argv[i][0] == '-') |
125 |
|
if (rgbinp == -1) |
126 |
|
rgbinp = !rgbout; |
127 |
|
printargs(argc, argv, stdout); /* add to header */ |
128 |
– |
if (expcomp < 0.99 || expcomp > 1.01) |
129 |
– |
fputexpos(expcomp, stdout); |
128 |
|
convert(); /* convert picture */ |
129 |
|
exit(0); |
130 |
|
userr: |
131 |
< |
fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname); |
131 |
> |
fprintf(stderr, "Usage: %s [-r][-o][-e exp][-c|-u]", progname); |
132 |
|
fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n"); |
133 |
|
exit(1); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
< |
quiterr(err) /* print message and exit */ |
138 |
< |
char *err; |
137 |
> |
static void |
138 |
> |
quiterr( /* print message and exit */ |
139 |
> |
char *err |
140 |
> |
) |
141 |
|
{ |
142 |
|
if (err != NULL) { |
143 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
convert() /* convert to XYZE or RGBE picture */ |
150 |
> |
static void |
151 |
> |
convert(void) /* convert to XYZE or RGBE picture */ |
152 |
|
{ |
153 |
|
int order; |
154 |
|
int xmax, ymax; |
155 |
|
COLORMAT xfm; |
156 |
|
register COLOR *scanin; |
157 |
|
register COLR *scanout; |
158 |
+ |
double exp2do = expcomp; |
159 |
+ |
double exp2report = expcomp; |
160 |
|
int y; |
161 |
|
register int x; |
162 |
+ |
/* recover original? */ |
163 |
+ |
if (origexp > 0.0) |
164 |
+ |
exp2do /= origexp; |
165 |
|
/* compute transform */ |
166 |
|
if (rgbout) { |
161 |
– |
double mult; |
167 |
|
if (rgbinp) { /* RGBE -> RGBE */ |
168 |
< |
comprgb2rgbmat(xfm, inprims, outprims); |
164 |
< |
mult = expcomp; |
168 |
> |
comprgb2rgbWBmat(xfm, inprims, outprims); |
169 |
|
} else { /* XYZE -> RGBE */ |
170 |
< |
compxyz2rgbmat(xfm, outprims); |
171 |
< |
mult = expcomp/WHTEFFICACY; |
170 |
> |
compxyz2rgbWBmat(xfm, outprims); |
171 |
> |
if (origexp > 0.0) |
172 |
> |
exp2do /= WHTEFFICACY; |
173 |
> |
else |
174 |
> |
exp2report *= WHTEFFICACY; |
175 |
|
} |
169 |
– |
for (y = 0; y < 3; y++) |
170 |
– |
for (x = 0; x < 3; x++) |
171 |
– |
xfm[y][x] *= mult; |
176 |
|
} else { |
177 |
|
if (rgbinp) { /* RGBE -> XYZE */ |
178 |
< |
comprgb2xyzmat(xfm, inprims); |
179 |
< |
for (y = 0; y < 3; y++) |
180 |
< |
for (x = 0; x < 3; x++) |
181 |
< |
xfm[y][x] *= WHTEFFICACY*expcomp; |
178 |
> |
comprgb2xyzWBmat(xfm, inprims); |
179 |
> |
if (origexp > 0.0) |
180 |
> |
exp2do *= WHTEFFICACY; |
181 |
> |
else |
182 |
> |
exp2report /= WHTEFFICACY; |
183 |
|
} else { /* XYZE -> XYZE */ |
184 |
|
for (y = 0; y < 3; y++) |
185 |
|
for (x = 0; x < 3; x++) |
186 |
< |
xfm[y][x] = x==y ? expcomp : 0.; |
186 |
> |
xfm[y][x] = x==y ? 1. : 0.; |
187 |
|
} |
188 |
|
} |
189 |
+ |
for (y = 0; y < 3; y++) |
190 |
+ |
for (x = 0; x < 3; x++) |
191 |
+ |
xfm[y][x] *= exp2do; |
192 |
|
/* get input resolution */ |
193 |
|
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
194 |
|
quiterr("bad picture format"); |
195 |
|
/* complete output header */ |
196 |
+ |
if ((exp2report < 0.99) | (exp2report > 1.01)) |
197 |
+ |
fputexpos(exp2report, stdout); |
198 |
|
if (rgbout) { |
199 |
|
fputprims(outprims, stdout); |
200 |
|
fputformat(COLRFMT, stdout); |
222 |
|
setcolr(scanout[x], colval(scanin[x],RED), |
223 |
|
colval(scanin[x],GRN), |
224 |
|
colval(scanin[x],BLU)); |
225 |
< |
fwrite((char *)scanout, sizeof(COLR), xmax, stdout); |
225 |
> |
putbinary((char *)scanout, sizeof(COLR), xmax, stdout); |
226 |
|
} else |
227 |
|
fwritescan(scanin, xmax, stdout); |
228 |
|
if (ferror(stdout)) |
229 |
|
quiterr("error writing output picture"); |
230 |
|
} |
231 |
|
/* free scanline */ |
232 |
< |
free((char *)scanin); |
232 |
> |
free((void *)scanin); |
233 |
|
if (scanout != NULL) |
234 |
< |
free((char *)scanout); |
234 |
> |
free((void *)scanout); |
235 |
|
} |