1 |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Program to convert between RADIANCE RGBE and XYZE formats |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <math.h> |
13 |
#include "color.h" |
14 |
#include "resolu.h" |
15 |
|
16 |
#ifdef MSDOS |
17 |
#include <fcntl.h> |
18 |
#endif |
19 |
|
20 |
extern char *malloc(), *strcpy(); |
21 |
|
22 |
int rgbinp = -1; /* input is RGBE? */ |
23 |
|
24 |
int rgbout = 0; /* output should be RGBE? */ |
25 |
|
26 |
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
27 |
|
28 |
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
29 |
|
30 |
double expcomp = 1.0; /* exposure compensation */ |
31 |
|
32 |
int doflat = -1; /* produce flat file? */ |
33 |
|
34 |
char *progname; |
35 |
|
36 |
|
37 |
headline(s) /* process header line */ |
38 |
char *s; |
39 |
{ |
40 |
char fmt[32]; |
41 |
|
42 |
if (formatval(fmt, s)) { /* check if format string */ |
43 |
if (!strcmp(fmt,COLRFMT)) |
44 |
rgbinp = 1; |
45 |
else if (!strcmp(fmt,CIEFMT)) |
46 |
rgbinp = 0; |
47 |
else |
48 |
rgbinp = -2; |
49 |
return; /* don't echo */ |
50 |
} |
51 |
if (isprims(s)) { /* get input primaries */ |
52 |
primsval(inprims, s); |
53 |
return; /* don't echo */ |
54 |
} |
55 |
/* should I grok colcorr also? */ |
56 |
fputs(s, stdout); |
57 |
} |
58 |
|
59 |
|
60 |
main(argc, argv) |
61 |
int argc; |
62 |
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 |
71 |
progname = argv[0]; |
72 |
|
73 |
for (i = 1; i < argc; i++) |
74 |
if (argv[i][0] == '-') |
75 |
switch (argv[i][1]) { |
76 |
case 'c': /* rle-compressed output */ |
77 |
doflat = 0; |
78 |
break; |
79 |
case 'u': /* flat output */ |
80 |
doflat = 1; |
81 |
break; |
82 |
case 'r': /* RGBE output */ |
83 |
rgbout = 1; |
84 |
break; |
85 |
case 'p': /* RGB primaries */ |
86 |
if (i+8 >= argc) |
87 |
goto userr; |
88 |
outprims[RED][CIEX] = atof(argv[++i]); |
89 |
outprims[RED][CIEY] = atof(argv[++i]); |
90 |
outprims[GRN][CIEX] = atof(argv[++i]); |
91 |
outprims[GRN][CIEY] = atof(argv[++i]); |
92 |
outprims[BLU][CIEX] = atof(argv[++i]); |
93 |
outprims[BLU][CIEY] = atof(argv[++i]); |
94 |
outprims[WHT][CIEX] = atof(argv[++i]); |
95 |
outprims[WHT][CIEY] = atof(argv[++i]); |
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 |
if (expcomp < 0.99 || expcomp > 1.01) |
129 |
fputexpos(expcomp, stdout); |
130 |
convert(); /* convert picture */ |
131 |
exit(0); |
132 |
userr: |
133 |
fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname); |
134 |
fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n"); |
135 |
exit(1); |
136 |
} |
137 |
|
138 |
|
139 |
quiterr(err) /* print message and exit */ |
140 |
char *err; |
141 |
{ |
142 |
if (err != NULL) { |
143 |
fprintf(stderr, "%s: %s\n", progname, err); |
144 |
exit(1); |
145 |
} |
146 |
exit(0); |
147 |
} |
148 |
|
149 |
|
150 |
convert() /* convert to XYZE or RGBE picture */ |
151 |
{ |
152 |
int order; |
153 |
int xmax, ymax; |
154 |
COLORMAT xfm; |
155 |
register COLOR *scanin; |
156 |
register COLR *scanout; |
157 |
int y; |
158 |
register int x; |
159 |
/* compute transform */ |
160 |
if (rgbout) { |
161 |
double mult; |
162 |
if (rgbinp) { /* RGBE -> RGBE */ |
163 |
comprgb2rgbmat(xfm, inprims, outprims); |
164 |
mult = expcomp; |
165 |
} else { /* XYZE -> RGBE */ |
166 |
compxyz2rgbmat(xfm, outprims); |
167 |
mult = expcomp/WHTEFFICACY; |
168 |
} |
169 |
for (y = 0; y < 3; y++) |
170 |
for (x = 0; x < 3; x++) |
171 |
xfm[y][x] *= mult; |
172 |
} else { |
173 |
if (rgbinp) { /* RGBE -> XYZE */ |
174 |
comprgb2xyzmat(xfm, inprims); |
175 |
for (y = 0; y < 3; y++) |
176 |
for (x = 0; x < 3; x++) |
177 |
xfm[y][x] *= WHTEFFICACY*expcomp; |
178 |
} else { /* XYZE -> XYZE */ |
179 |
for (y = 0; y < 3; y++) |
180 |
for (x = 0; x < 3; x++) |
181 |
xfm[y][x] = x==y ? expcomp : 0.; |
182 |
} |
183 |
} |
184 |
/* get input resolution */ |
185 |
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
186 |
quiterr("bad picture format"); |
187 |
/* complete output header */ |
188 |
if (rgbout) { |
189 |
fputprims(outprims, stdout); |
190 |
fputformat(COLRFMT, stdout); |
191 |
} else |
192 |
fputformat(CIEFMT, stdout); |
193 |
putc('\n', stdout); |
194 |
fputresolu(order, xmax, ymax, stdout); |
195 |
/* allocate scanline */ |
196 |
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
197 |
if (scanin == NULL) |
198 |
quiterr("out of memory in convert"); |
199 |
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL; |
200 |
/* convert image */ |
201 |
for (y = 0; y < ymax; y++) { |
202 |
if (freadscan(scanin, xmax, stdin) < 0) |
203 |
quiterr("error reading input picture"); |
204 |
for (x = 0; x < xmax; x++) { |
205 |
colortrans(scanin[x], xfm, scanin[x]); |
206 |
if (rgbout) |
207 |
clipgamut(scanin[x], bright(scanin[x]), |
208 |
CGAMUT_LOWER, cblack, cwhite); |
209 |
} |
210 |
if (scanout != NULL) { |
211 |
for (x = 0; x < xmax; x++) |
212 |
setcolr(scanout[x], colval(scanin[x],RED), |
213 |
colval(scanin[x],GRN), |
214 |
colval(scanin[x],BLU)); |
215 |
fwrite((char *)scanout, sizeof(COLR), xmax, stdout); |
216 |
} else |
217 |
fwritescan(scanin, xmax, stdout); |
218 |
if (ferror(stdout)) |
219 |
quiterr("error writing output picture"); |
220 |
} |
221 |
/* free scanline */ |
222 |
free((char *)scanin); |
223 |
if (scanout != NULL) |
224 |
free((char *)scanout); |
225 |
} |