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