6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <math.h> |
10 |
|
#include <string.h> |
11 |
|
|
12 |
|
#include "platform.h" |
15 |
|
#include "resolu.h" |
16 |
|
#include "bmpfile.h" |
17 |
|
|
18 |
< |
int bradj = 0; /* brightness adjustment */ |
18 |
> |
int bradj = 0; /* brightness adjustment */ |
19 |
|
|
20 |
< |
double gamcor = 2.2; /* gamma correction value */ |
20 |
> |
double gamcor = 2.2; /* gamma correction value */ |
21 |
|
|
22 |
< |
char *progname; |
22 |
> |
char *progname; |
23 |
|
|
24 |
|
static void quiterr(const char *err); |
25 |
|
static void tmap2bmp(char *fnin, char *fnout, char *expec, |
26 |
|
RGBPRIMP monpri, double gamval); |
27 |
< |
static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry); |
27 |
> |
static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri); |
28 |
|
static void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
29 |
|
|
30 |
+ |
static RGBPRIMP rgbinp = stdprims; /* RGB input primitives */ |
31 |
+ |
static RGBPRIMS myinprims; /* custom primitives holder */ |
32 |
|
|
33 |
+ |
static gethfunc headline; |
34 |
+ |
|
35 |
+ |
|
36 |
|
int |
37 |
|
main(int argc, char *argv[]) |
38 |
|
{ |
153 |
|
exit(1); |
154 |
|
} |
155 |
|
/* get header info. */ |
156 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
156 |
> |
if (getheader(stdin, headline, NULL) < 0 || |
157 |
|
!fgetsresolu(&rs, stdin)) |
158 |
|
quiterr("bad Radiance picture format"); |
159 |
|
/* initialize BMP header */ |
177 |
|
if (wtr == NULL) |
178 |
|
quiterr("cannot allocate writer structure"); |
179 |
|
/* convert file */ |
180 |
< |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp==NULL); |
180 |
> |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp); |
181 |
|
/* flush output */ |
182 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
183 |
|
quiterr("error writing BMP output"); |
205 |
|
exit(0); |
206 |
|
} |
207 |
|
|
208 |
+ |
/* process header line (don't echo) */ |
209 |
+ |
static int |
210 |
+ |
headline(char *s, void *p) |
211 |
+ |
{ |
212 |
+ |
char fmt[32]; |
213 |
+ |
|
214 |
+ |
if (formatval(fmt, s)) { /* check if format string */ |
215 |
+ |
if (!strcmp(fmt,COLRFMT)) |
216 |
+ |
return(0); |
217 |
+ |
if (!strcmp(fmt,CIEFMT)) { |
218 |
+ |
rgbinp = TM_XYZPRIM; |
219 |
+ |
return(0); |
220 |
+ |
} |
221 |
+ |
return(-1); |
222 |
+ |
} |
223 |
+ |
if (isprims(s)) { /* get input primaries */ |
224 |
+ |
primsval(myinprims, s); |
225 |
+ |
rgbinp = myinprims; |
226 |
+ |
return(0); |
227 |
+ |
} |
228 |
+ |
/* should I grok colcorr also? */ |
229 |
+ |
return(0); |
230 |
+ |
} |
231 |
+ |
|
232 |
+ |
|
233 |
|
/* convert Radiance picture to BMP */ |
234 |
|
static void |
235 |
< |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) |
235 |
> |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri) |
236 |
|
{ |
237 |
+ |
int usexfm = 0; |
238 |
+ |
COLORMAT xfm; |
239 |
|
COLR *scanin; |
240 |
+ |
COLOR cval; |
241 |
|
int y, yend, ystp; |
242 |
|
int x; |
243 |
|
/* allocate scanline */ |
244 |
|
scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); |
245 |
|
if (scanin == NULL) |
246 |
|
quiterr("out of memory in rad2bmp"); |
247 |
+ |
/* set up color conversion */ |
248 |
+ |
usexfm = (monpri != NULL ? rgbinp != monpri : |
249 |
+ |
rgbinp != TM_XYZPRIM && rgbinp != stdprims); |
250 |
+ |
if (usexfm) { |
251 |
+ |
double expcomp = pow(2.0, (double)bradj); |
252 |
+ |
if (rgbinp == TM_XYZPRIM) |
253 |
+ |
compxyz2rgbWBmat(xfm, monpri); |
254 |
+ |
else |
255 |
+ |
comprgb2rgbWBmat(xfm, rgbinp, monpri); |
256 |
+ |
for (y = 0; y < 3; y++) |
257 |
+ |
for (x = 0; x < 3; x++) |
258 |
+ |
xfm[y][x] *= expcomp; |
259 |
+ |
} |
260 |
|
/* convert image */ |
261 |
|
if (inv) { |
262 |
|
y = bwr->hdr->height - 1; |
269 |
|
for ( ; y != yend; y += ystp) { |
270 |
|
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
271 |
|
quiterr("error reading Radiance picture"); |
272 |
< |
if (bradj) |
272 |
> |
if (usexfm) |
273 |
> |
for (x = bwr->hdr->width; x--; ) { |
274 |
> |
colr_color(cval, scanin[x]); |
275 |
> |
colortrans(cval, xfm, cval); |
276 |
> |
setcolr(scanin[x], colval(cval,RED), |
277 |
> |
colval(cval,GRN), |
278 |
> |
colval(cval,BLU)); |
279 |
> |
} |
280 |
> |
else if (bradj) |
281 |
|
shiftcolrs(scanin, bwr->hdr->width, bradj); |
282 |
< |
for (x = gry ? bwr->hdr->width : 0; x--; ) |
283 |
< |
scanin[x][GRN] = normbright(scanin[x]); |
282 |
> |
if (monpri == NULL && rgbinp != TM_XYZPRIM) |
283 |
> |
for (x = bwr->hdr->width; x--; ) |
284 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
285 |
|
colrs_gambs(scanin, bwr->hdr->width); |
286 |
< |
if (gry) |
286 |
> |
if (monpri == NULL) |
287 |
|
for (x = bwr->hdr->width; x--; ) |
288 |
|
bwr->scanline[x] = scanin[x][GRN]; |
289 |
|
else |