1 |
– |
/* Copyright (c) 1995 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 |
|
|
16 |
– |
#ifdef MSDOS |
17 |
– |
#include <fcntl.h> |
18 |
– |
#endif |
19 |
– |
|
20 |
– |
extern char *malloc(), *strcpy(); |
21 |
– |
|
18 |
|
int rgbinp = -1; /* input is RGBE? */ |
23 |
– |
|
19 |
|
int rgbout = 0; /* output should be RGBE? */ |
25 |
– |
|
20 |
|
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
27 |
– |
|
21 |
|
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
29 |
– |
|
22 |
|
double expcomp = 1.0; /* exposure compensation */ |
31 |
– |
|
23 |
|
int doflat = -1; /* produce flat file? */ |
33 |
– |
|
24 |
|
char *progname; |
25 |
|
|
26 |
+ |
static gethfunc headline; |
27 |
+ |
static void quiterr(char *err); |
28 |
+ |
static void convert(void); |
29 |
|
|
30 |
< |
headline(s) /* process header line */ |
31 |
< |
char *s; |
30 |
> |
|
31 |
> |
|
32 |
> |
static int |
33 |
> |
headline( /* process header line */ |
34 |
> |
char *s, |
35 |
> |
void *p |
36 |
> |
) |
37 |
|
{ |
38 |
|
char fmt[32]; |
39 |
|
|
44 |
|
rgbinp = 0; |
45 |
|
else |
46 |
|
rgbinp = -2; |
47 |
< |
return; /* don't echo */ |
47 |
> |
return(0); /* don't echo */ |
48 |
|
} |
49 |
|
if (isprims(s)) { /* get input primaries */ |
50 |
|
primsval(inprims, s); |
51 |
< |
return; /* don't echo */ |
51 |
> |
return(0); /* don't echo */ |
52 |
|
} |
53 |
|
/* should I grok colcorr also? */ |
54 |
< |
fputs(s, stdout); |
54 |
> |
return(fputs(s, stdout)); |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
< |
main(argc, argv) |
59 |
< |
int argc; |
62 |
< |
char *argv[]; |
58 |
> |
int |
59 |
> |
main(int argc, char *argv[]) |
60 |
|
{ |
61 |
|
int i; |
62 |
< |
#ifdef MSDOS |
63 |
< |
extern int _fmode; |
64 |
< |
_fmode = O_BINARY; |
68 |
< |
setmode(fileno(stdin), O_BINARY); |
69 |
< |
setmode(fileno(stdout), O_BINARY); |
70 |
< |
#endif |
62 |
> |
SET_DEFAULT_BINARY(); |
63 |
> |
SET_FILE_BINARY(stdin); |
64 |
> |
SET_FILE_BINARY(stdout); |
65 |
|
progname = argv[0]; |
66 |
|
|
67 |
|
for (i = 1; i < argc; i++) |
119 |
|
if (rgbinp == -1) |
120 |
|
rgbinp = !rgbout; |
121 |
|
printargs(argc, argv, stdout); /* add to header */ |
128 |
– |
if (expcomp < 0.99 || expcomp > 1.01) |
129 |
– |
fputexpos(expcomp, stdout); |
122 |
|
convert(); /* convert picture */ |
123 |
|
exit(0); |
124 |
|
userr: |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
< |
quiterr(err) /* print message and exit */ |
132 |
< |
char *err; |
131 |
> |
static void |
132 |
> |
quiterr( /* print message and exit */ |
133 |
> |
char *err |
134 |
> |
) |
135 |
|
{ |
136 |
|
if (err != NULL) { |
137 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
convert() /* convert to XYZE or RGBE picture */ |
144 |
> |
static void |
145 |
> |
convert(void) /* convert to XYZE or RGBE picture */ |
146 |
|
{ |
147 |
|
int order; |
148 |
|
int xmax, ymax; |
149 |
|
COLORMAT xfm; |
150 |
|
register COLOR *scanin; |
151 |
|
register COLR *scanout; |
152 |
+ |
double ourexp = expcomp; |
153 |
|
int y; |
154 |
|
register int x; |
155 |
|
/* compute transform */ |
156 |
|
if (rgbout) { |
161 |
– |
double mult; |
157 |
|
if (rgbinp) { /* RGBE -> RGBE */ |
158 |
< |
comprgb2rgbmat(xfm, inprims, outprims); |
164 |
< |
mult = expcomp; |
158 |
> |
comprgb2rgbWBmat(xfm, inprims, outprims); |
159 |
|
} else { /* XYZE -> RGBE */ |
160 |
< |
compxyz2rgbmat(xfm, outprims); |
161 |
< |
mult = expcomp/WHTEFFICACY; |
160 |
> |
compxyz2rgbWBmat(xfm, outprims); |
161 |
> |
ourexp *= WHTEFFICACY; |
162 |
|
} |
169 |
– |
for (y = 0; y < 3; y++) |
170 |
– |
for (x = 0; x < 3; x++) |
171 |
– |
xfm[y][x] *= mult; |
163 |
|
} else { |
164 |
|
if (rgbinp) { /* RGBE -> XYZE */ |
165 |
< |
comprgb2xyzmat(xfm, inprims); |
166 |
< |
for (y = 0; y < 3; y++) |
176 |
< |
for (x = 0; x < 3; x++) |
177 |
< |
xfm[y][x] *= WHTEFFICACY*expcomp; |
165 |
> |
comprgb2xyzWBmat(xfm, inprims); |
166 |
> |
ourexp /= WHTEFFICACY; |
167 |
|
} else { /* XYZE -> XYZE */ |
168 |
|
for (y = 0; y < 3; y++) |
169 |
|
for (x = 0; x < 3; x++) |
170 |
< |
xfm[y][x] = x==y ? expcomp : 0.; |
170 |
> |
xfm[y][x] = x==y ? 1. : 0.; |
171 |
|
} |
172 |
|
} |
173 |
+ |
for (y = 0; y < 3; y++) |
174 |
+ |
for (x = 0; x < 3; x++) |
175 |
+ |
xfm[y][x] *= expcomp; |
176 |
|
/* get input resolution */ |
177 |
|
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
178 |
|
quiterr("bad picture format"); |
179 |
|
/* complete output header */ |
180 |
+ |
if (ourexp < 0.99 || ourexp > 1.01) |
181 |
+ |
fputexpos(ourexp, stdout); |
182 |
|
if (rgbout) { |
183 |
|
fputprims(outprims, stdout); |
184 |
|
fputformat(COLRFMT, stdout); |
195 |
|
for (y = 0; y < ymax; y++) { |
196 |
|
if (freadscan(scanin, xmax, stdin) < 0) |
197 |
|
quiterr("error reading input picture"); |
198 |
< |
for (x = 0; x < xmax; x++) |
198 |
> |
for (x = 0; x < xmax; x++) { |
199 |
|
colortrans(scanin[x], xfm, scanin[x]); |
200 |
+ |
if (rgbout) |
201 |
+ |
clipgamut(scanin[x], bright(scanin[x]), |
202 |
+ |
CGAMUT_LOWER, cblack, cwhite); |
203 |
+ |
} |
204 |
|
if (scanout != NULL) { |
205 |
|
for (x = 0; x < xmax; x++) |
206 |
|
setcolr(scanout[x], colval(scanin[x],RED), |
213 |
|
quiterr("error writing output picture"); |
214 |
|
} |
215 |
|
/* free scanline */ |
216 |
< |
free((char *)scanin); |
216 |
> |
free((void *)scanin); |
217 |
|
if (scanout != NULL) |
218 |
< |
free((char *)scanout); |
218 |
> |
free((void *)scanout); |
219 |
|
} |