1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_xyze.c,v 2.12 2018/08/02 18:33:48 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Program to convert between RADIANCE RGBE and XYZE formats |
6 |
* Added white-balance adjustment 10/01 (GW). |
7 |
*/ |
8 |
|
9 |
#include <math.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 |
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 */ |
23 |
char *progname; |
24 |
|
25 |
static gethfunc headline; |
26 |
static void quiterr(char *err); |
27 |
static void convert(void); |
28 |
|
29 |
|
30 |
|
31 |
static int |
32 |
headline( /* process header line */ |
33 |
char *s, |
34 |
void *p |
35 |
) |
36 |
{ |
37 |
char fmt[MAXFMTLEN]; |
38 |
|
39 |
if (formatval(fmt, s)) { /* check if format string */ |
40 |
if (!strcmp(fmt,COLRFMT)) |
41 |
rgbinp = 1; |
42 |
else if (!strcmp(fmt,CIEFMT)) |
43 |
rgbinp = 0; |
44 |
else |
45 |
rgbinp = -2; |
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(0); /* don't echo */ |
55 |
} |
56 |
/* should I grok colcorr also? */ |
57 |
return(fputs(s, stdout)); |
58 |
} |
59 |
|
60 |
|
61 |
int |
62 |
main(int argc, char *argv[]) |
63 |
{ |
64 |
int i; |
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++) |
71 |
if (argv[i][0] == '-') |
72 |
switch (argv[i][1]) { |
73 |
case 'c': /* rle-compressed output */ |
74 |
doflat = 0; |
75 |
break; |
76 |
case 'u': /* flat output */ |
77 |
doflat = 1; |
78 |
break; |
79 |
case 'r': /* RGBE output */ |
80 |
rgbout = 1; |
81 |
break; |
82 |
case 'p': /* RGB primaries */ |
83 |
if (i+8 >= argc) |
84 |
goto userr; |
85 |
outprims[RED][CIEX] = atof(argv[++i]); |
86 |
outprims[RED][CIEY] = atof(argv[++i]); |
87 |
outprims[GRN][CIEX] = atof(argv[++i]); |
88 |
outprims[GRN][CIEY] = atof(argv[++i]); |
89 |
outprims[BLU][CIEX] = atof(argv[++i]); |
90 |
outprims[BLU][CIEY] = atof(argv[++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] == '-') |
100 |
expcomp = pow(2., expcomp); |
101 |
break; |
102 |
default: |
103 |
goto userr; |
104 |
} |
105 |
else |
106 |
break; |
107 |
|
108 |
if (doflat < 0) |
109 |
doflat = !rgbout; |
110 |
if (i < argc-2) |
111 |
goto userr; |
112 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
113 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
114 |
progname, argv[i]); |
115 |
exit(1); |
116 |
} |
117 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
118 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
119 |
progname, argv[i+1]); |
120 |
exit(1); |
121 |
} |
122 |
getheader(stdin, headline, NULL); |
123 |
if (rgbinp == -2) |
124 |
quiterr("unrecognized input file format"); |
125 |
if (rgbinp == -1) |
126 |
rgbinp = !rgbout; |
127 |
printargs(argc, argv, stdout); /* add to header */ |
128 |
convert(); /* convert picture */ |
129 |
exit(0); |
130 |
userr: |
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 |
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); |
144 |
exit(1); |
145 |
} |
146 |
exit(0); |
147 |
} |
148 |
|
149 |
|
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) { |
167 |
if (rgbinp) { /* RGBE -> RGBE */ |
168 |
comprgb2rgbWBmat(xfm, inprims, outprims); |
169 |
} else { /* XYZE -> RGBE */ |
170 |
compxyz2rgbWBmat(xfm, outprims); |
171 |
if (origexp > 0.0) |
172 |
exp2do /= WHTEFFICACY; |
173 |
else |
174 |
exp2report *= WHTEFFICACY; |
175 |
} |
176 |
} else { |
177 |
if (rgbinp) { /* RGBE -> XYZE */ |
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 ? 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); |
201 |
} else |
202 |
fputformat(CIEFMT, stdout); |
203 |
putc('\n', stdout); |
204 |
fputresolu(order, xmax, ymax, stdout); |
205 |
/* allocate scanline */ |
206 |
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
207 |
if (scanin == NULL) |
208 |
quiterr("out of memory in convert"); |
209 |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL; |
210 |
/* convert image */ |
211 |
for (y = 0; y < ymax; y++) { |
212 |
if (freadscan(scanin, xmax, stdin) < 0) |
213 |
quiterr("error reading input picture"); |
214 |
for (x = 0; x < xmax; x++) { |
215 |
colortrans(scanin[x], xfm, scanin[x]); |
216 |
if (rgbout) |
217 |
clipgamut(scanin[x], bright(scanin[x]), |
218 |
CGAMUT_LOWER, cblack, cwhite); |
219 |
} |
220 |
if (scanout != NULL) { |
221 |
for (x = 0; x < xmax; x++) |
222 |
setcolr(scanout[x], colval(scanin[x],RED), |
223 |
colval(scanin[x],GRN), |
224 |
colval(scanin[x],BLU)); |
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((void *)scanin); |
233 |
if (scanout != NULL) |
234 |
free((void *)scanout); |
235 |
} |