| 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 */ |
| 160 |
|
if (rgbp == NULL) { |
| 161 |
|
hdr = BMPmappedHeader(scanlen(&rs), |
| 162 |
|
numscans(&rs), 0, 256); |
| 163 |
+ |
/* |
| 164 |
|
if (outfile != NULL) |
| 165 |
|
hdr->compr = BI_RLE8; |
| 166 |
+ |
*/ |
| 167 |
|
} else |
| 168 |
|
hdr = BMPtruecolorHeader(scanlen(&rs), |
| 169 |
|
numscans(&rs), 0); |
| 179 |
|
if (wtr == NULL) |
| 180 |
|
quiterr("cannot allocate writer structure"); |
| 181 |
|
/* convert file */ |
| 182 |
< |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp==NULL); |
| 182 |
> |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp); |
| 183 |
|
/* flush output */ |
| 184 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
| 185 |
|
quiterr("error writing BMP output"); |
| 207 |
|
exit(0); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
+ |
/* process header line (don't echo) */ |
| 211 |
+ |
static int |
| 212 |
+ |
headline(char *s, void *p) |
| 213 |
+ |
{ |
| 214 |
+ |
char fmt[MAXFMTLEN]; |
| 215 |
+ |
|
| 216 |
+ |
if (formatval(fmt, s)) { /* check if format string */ |
| 217 |
+ |
if (!strcmp(fmt,COLRFMT)) |
| 218 |
+ |
return(0); |
| 219 |
+ |
if (!strcmp(fmt,CIEFMT)) { |
| 220 |
+ |
rgbinp = TM_XYZPRIM; |
| 221 |
+ |
return(0); |
| 222 |
+ |
} |
| 223 |
+ |
return(-1); |
| 224 |
+ |
} |
| 225 |
+ |
if (isprims(s)) { /* get input primaries */ |
| 226 |
+ |
primsval(myinprims, s); |
| 227 |
+ |
rgbinp = myinprims; |
| 228 |
+ |
return(0); |
| 229 |
+ |
} |
| 230 |
+ |
/* should I grok colcorr also? */ |
| 231 |
+ |
return(0); |
| 232 |
+ |
} |
| 233 |
+ |
|
| 234 |
+ |
|
| 235 |
|
/* convert Radiance picture to BMP */ |
| 236 |
|
static void |
| 237 |
< |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) |
| 237 |
> |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri) |
| 238 |
|
{ |
| 239 |
+ |
int usexfm = 0; |
| 240 |
+ |
COLORMAT xfm; |
| 241 |
|
COLR *scanin; |
| 242 |
+ |
COLOR cval; |
| 243 |
|
int y, yend, ystp; |
| 244 |
|
int x; |
| 245 |
|
/* allocate scanline */ |
| 246 |
|
scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); |
| 247 |
|
if (scanin == NULL) |
| 248 |
|
quiterr("out of memory in rad2bmp"); |
| 249 |
+ |
/* set up color conversion */ |
| 250 |
+ |
usexfm = (monpri != NULL ? rgbinp != monpri : |
| 251 |
+ |
rgbinp != TM_XYZPRIM && rgbinp != stdprims); |
| 252 |
+ |
if (usexfm) { |
| 253 |
+ |
double expcomp = pow(2.0, (double)bradj); |
| 254 |
+ |
if (rgbinp == TM_XYZPRIM) |
| 255 |
+ |
compxyz2rgbWBmat(xfm, monpri); |
| 256 |
+ |
else |
| 257 |
+ |
comprgb2rgbWBmat(xfm, rgbinp, monpri); |
| 258 |
+ |
for (y = 0; y < 3; y++) |
| 259 |
+ |
for (x = 0; x < 3; x++) |
| 260 |
+ |
xfm[y][x] *= expcomp; |
| 261 |
+ |
} |
| 262 |
|
/* convert image */ |
| 263 |
|
if (inv) { |
| 264 |
|
y = bwr->hdr->height - 1; |
| 271 |
|
for ( ; y != yend; y += ystp) { |
| 272 |
|
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
| 273 |
|
quiterr("error reading Radiance picture"); |
| 274 |
< |
if (bradj) |
| 274 |
> |
if (usexfm) |
| 275 |
> |
for (x = bwr->hdr->width; x--; ) { |
| 276 |
> |
colr_color(cval, scanin[x]); |
| 277 |
> |
colortrans(cval, xfm, cval); |
| 278 |
> |
setcolr(scanin[x], colval(cval,RED), |
| 279 |
> |
colval(cval,GRN), |
| 280 |
> |
colval(cval,BLU)); |
| 281 |
> |
} |
| 282 |
> |
else if (bradj) |
| 283 |
|
shiftcolrs(scanin, bwr->hdr->width, bradj); |
| 284 |
< |
for (x = gry ? bwr->hdr->width : 0; x--; ) |
| 285 |
< |
scanin[x][GRN] = normbright(scanin[x]); |
| 284 |
> |
if (monpri == NULL && rgbinp != TM_XYZPRIM) |
| 285 |
> |
for (x = bwr->hdr->width; x--; ) |
| 286 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
| 287 |
|
colrs_gambs(scanin, bwr->hdr->width); |
| 288 |
< |
if (gry) |
| 288 |
> |
if (monpri == NULL) |
| 289 |
|
for (x = bwr->hdr->width; x--; ) |
| 290 |
|
bwr->scanline[x] = scanin[x][GRN]; |
| 291 |
|
else |
| 352 |
|
BMPWriter *wtr; |
| 353 |
|
FILE *fp; |
| 354 |
|
int xr, yr; |
| 355 |
< |
BYTE *pa; |
| 355 |
> |
uby8 *pa; |
| 356 |
|
int i; |
| 357 |
|
/* check tone-mapping spec */ |
| 358 |
|
i = strlen(expec); |
| 397 |
|
quiterr("cannot allocate writer structure"); |
| 398 |
|
/* write to BMP file */ |
| 399 |
|
while (wtr->yscan < yr) { |
| 400 |
< |
BYTE *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
| 400 |
> |
uby8 *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
| 401 |
|
(yr-1 - wtr->yscan); |
| 402 |
|
if (tmflags & TM_F_BW) |
| 403 |
|
memcpy((void *)wtr->scanline, (void *)scn, xr); |