| 1 |
< |
/* Copyright (c) 1997 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
#endif |
| 6 |
|
|
| 7 |
|
/* |
| 17 |
|
|
| 18 |
|
extern char *tempbuffer(); |
| 19 |
|
|
| 20 |
+ |
#define GAMTSZ 1024 |
| 21 |
+ |
|
| 22 |
+ |
typedef struct { |
| 23 |
+ |
BYTE gamb[GAMTSZ]; /* gamma lookup table */ |
| 24 |
+ |
COLR clfb; /* encoded tm->clf */ |
| 25 |
+ |
TMbright inpsfb; /* encoded tm->inpsf */ |
| 26 |
+ |
} COLRDATA; |
| 27 |
+ |
|
| 28 |
+ |
static MEM_PTR colrInit(); |
| 29 |
+ |
static void colrNewSpace(); |
| 30 |
+ |
extern void free(); |
| 31 |
+ |
static struct tmPackage colrPkg = { /* our package functions */ |
| 32 |
+ |
colrInit, colrNewSpace, free |
| 33 |
+ |
}; |
| 34 |
+ |
static int colrReg = -1; /* our package registration number */ |
| 35 |
+ |
|
| 36 |
|
#define LOGISZ 260 |
| 37 |
|
static TMbright logi[LOGISZ]; |
| 38 |
|
static BYTE photofact[BMESUPPER-BMESLOWER]; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
int |
| 42 |
< |
tmCvColrs(ls, cs, scan, len) /* tone map RGBE/XYZE colors */ |
| 42 |
> |
tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */ |
| 43 |
|
TMbright *ls; |
| 44 |
|
BYTE *cs; |
| 45 |
|
COLR *scan; |
| 47 |
|
{ |
| 48 |
|
static char funcName[] = "tmCvColrs"; |
| 49 |
|
COLR cmon; |
| 50 |
+ |
register COLRDATA *cd; |
| 51 |
|
register int i, bi, li; |
| 52 |
|
|
| 53 |
|
if (tmTop == NULL) |
| 54 |
|
returnErr(TM_E_TMINVAL); |
| 55 |
< |
if (ls == NULL | scan == NULL | len <= 0) |
| 55 |
> |
if (ls == NULL | scan == NULL | len < 0) |
| 56 |
|
returnErr(TM_E_ILLEGAL); |
| 57 |
< |
if (tmTop->flags & TM_F_NEEDMAT) { /* need floating point */ |
| 57 |
> |
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
| 58 |
|
register COLOR *newscan; |
| 59 |
|
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
| 60 |
|
if (newscan == NULL) |
| 63 |
|
colr_color(newscan[i], scan[i]); |
| 64 |
|
return(tmCvColors(ls, cs, newscan, len)); |
| 65 |
|
} |
| 66 |
< |
if (logi[0] == 0) { /* build tables if necessary */ |
| 66 |
> |
if (colrReg < 0) { /* build tables if necessary */ |
| 67 |
> |
colrReg = tmRegPkg(&colrPkg); |
| 68 |
> |
if (colrReg < 0) |
| 69 |
> |
returnErr(TM_E_CODERR1); |
| 70 |
|
for (i = 256; i--; ) |
| 71 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
| 72 |
|
for (i = 256; i < LOGISZ; i++) |
| 76 |
|
(tmLuminance(i) - LMESLOWER) / |
| 77 |
|
(LMESUPPER - LMESLOWER); |
| 78 |
|
} |
| 79 |
+ |
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
| 80 |
+ |
returnErr(TM_E_NOMEM); |
| 81 |
|
for (i = len; i--; ) { |
| 82 |
|
copycolr(cmon, scan[i]); |
| 83 |
|
/* world luminance */ |
| 84 |
< |
li = ( tmTop->clfb[RED]*cmon[RED] + |
| 85 |
< |
tmTop->clfb[GRN]*cmon[GRN] + |
| 86 |
< |
tmTop->clfb[BLU]*cmon[BLU] ) >> 8; |
| 84 |
> |
li = ( cd->clfb[RED]*cmon[RED] + |
| 85 |
> |
cd->clfb[GRN]*cmon[GRN] + |
| 86 |
> |
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
| 87 |
|
bi = BRT2SCALE*(cmon[EXP]-COLXS) + |
| 88 |
< |
logi[li] + tmTop->inpsfb; |
| 88 |
> |
logi[li] + cd->inpsfb; |
| 89 |
|
if (bi < MINBRT) { |
| 90 |
|
bi = MINBRT-1; /* bogus value */ |
| 91 |
|
li++; /* avoid li==0 */ |
| 93 |
|
ls[i] = bi; |
| 94 |
|
if (cs == TM_NOCHROM) /* no color? */ |
| 95 |
|
continue; |
| 96 |
+ |
if (tmTop->flags & TM_F_BW) |
| 97 |
+ |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
| 98 |
|
/* mesopic adj. */ |
| 99 |
|
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
| 100 |
|
register int pf, sli = normscot(cmon); |
| 101 |
|
if (bi < BMESLOWER) |
| 102 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
| 103 |
|
else { |
| 80 |
– |
if (tmTop->flags & TM_F_BW) |
| 81 |
– |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
| 104 |
|
pf = photofact[bi-BMESLOWER]; |
| 105 |
|
sli *= 256 - pf; |
| 106 |
|
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
| 107 |
|
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
| 108 |
|
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
| 109 |
|
} |
| 88 |
– |
} else if (tmTop->flags & TM_F_BW) { |
| 89 |
– |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
| 110 |
|
} |
| 111 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[RED]*cmon[RED]/li ) >> 8; |
| 112 |
< |
cs[3*i ] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
| 113 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[GRN]*cmon[GRN]/li ) >> 8; |
| 114 |
< |
cs[3*i+1] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
| 115 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[BLU]*cmon[BLU]/li ) >> 8; |
| 116 |
< |
cs[3*i+2] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
| 111 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
| 112 |
> |
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
| 113 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
| 114 |
> |
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
| 115 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
| 116 |
> |
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
| 117 |
|
} |
| 118 |
|
returnOK; |
| 119 |
|
} |
| 183 |
|
*xp = *yp = 0; /* error precaution */ |
| 184 |
|
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
| 185 |
|
returnErr(TM_E_BADFILE); |
| 186 |
+ |
*lpp = NULL; |
| 187 |
+ |
if (cpp != TM_NOCHROMP) *cpp = NULL; |
| 188 |
|
info = rhdefault; /* get our header */ |
| 189 |
< |
getheader(inpf, headline, (char *)&info); |
| 189 |
> |
getheader(inpf, headline, (MEM_PTR)&info); |
| 190 |
|
if (info.format == FMTBAD | info.expos <= 0. || |
| 191 |
|
fgetresolu(xp, yp, inpf) < 0) { |
| 192 |
|
err = TM_E_BADFILE; goto done; |
| 227 |
|
if (fp == NULL) |
| 228 |
|
fclose(inpf); |
| 229 |
|
if (scanin != NULL) |
| 230 |
< |
free((char *)scanin); |
| 231 |
< |
if (err != TM_E_OK) |
| 230 |
> |
free((MEM_PTR)scanin); |
| 231 |
> |
if (err != TM_E_OK) { |
| 232 |
> |
if (*lpp != NULL) |
| 233 |
> |
free((MEM_PTR)*lpp); |
| 234 |
> |
if (cpp != TM_NOCHROMP && *cpp != NULL) |
| 235 |
> |
free((MEM_PTR)*cpp); |
| 236 |
|
returnErr(err); |
| 237 |
+ |
} |
| 238 |
|
returnOK; |
| 239 |
|
} |
| 240 |
|
|
| 289 |
|
returnErr(TM_E_BADFILE); |
| 290 |
|
} |
| 291 |
|
/* allocate arrays */ |
| 292 |
+ |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
| 293 |
|
if (flags & TM_F_BW) |
| 294 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
| 295 |
|
else |
| 296 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
| 269 |
– |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
| 297 |
|
if ((*psp = rp) == NULL | scan == NULL) { |
| 298 |
|
pclose(infp); |
| 299 |
|
returnErr(TM_E_NOMEM); |
| 302 |
|
for (y = 0; y < *yp; y++) { |
| 303 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
| 304 |
|
pclose(infp); |
| 305 |
+ |
free((MEM_PTR)scan); |
| 306 |
+ |
free((MEM_PTR)*psp); |
| 307 |
+ |
*psp = NULL; |
| 308 |
|
returnErr(TM_E_BADFILE); |
| 309 |
|
} |
| 310 |
|
colrs_gambs(scan, *xp); |
| 318 |
|
*rp++ = scan[x][BLU]; |
| 319 |
|
} |
| 320 |
|
} |
| 321 |
< |
free((char *)scan); |
| 321 |
> |
free((MEM_PTR)scan); |
| 322 |
|
pclose(infp); |
| 323 |
|
returnOK; |
| 324 |
|
} |
| 335 |
|
FILE *fp; |
| 336 |
|
{ |
| 337 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
| 308 |
– |
FILE *inpf; |
| 338 |
|
BYTE *cp; |
| 339 |
|
TMbright *lp; |
| 340 |
|
int err; |
| 364 |
|
/* allocate space for result */ |
| 365 |
|
if (flags & TM_F_BW) { |
| 366 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
| 367 |
< |
if (*psp == NULL) |
| 367 |
> |
if (*psp == NULL) { |
| 368 |
> |
free((MEM_PTR)lp); |
| 369 |
> |
tmDone(NULL); |
| 370 |
|
returnErr(TM_E_NOMEM); |
| 371 |
+ |
} |
| 372 |
|
cp = TM_NOCHROM; |
| 373 |
|
} else |
| 374 |
|
*psp = cp; |
| 383 |
|
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
| 384 |
|
|
| 385 |
|
done: /* clean up */ |
| 386 |
< |
free((char *)lp); |
| 386 |
> |
free((MEM_PTR)lp); |
| 387 |
|
tmDone(NULL); |
| 388 |
|
if (err != TM_E_OK) { /* free memory on error */ |
| 389 |
< |
free((char *)*psp); |
| 389 |
> |
free((MEM_PTR)*psp); |
| 390 |
|
*psp = NULL; |
| 391 |
|
returnErr(err); |
| 392 |
|
} |
| 393 |
|
returnOK; |
| 394 |
+ |
} |
| 395 |
+ |
|
| 396 |
+ |
|
| 397 |
+ |
static void |
| 398 |
+ |
colrNewSpace(tms) /* color space changed for tone mapping */ |
| 399 |
+ |
register struct tmStruct *tms; |
| 400 |
+ |
{ |
| 401 |
+ |
register COLRDATA *cd; |
| 402 |
+ |
double d; |
| 403 |
+ |
|
| 404 |
+ |
cd = (COLRDATA *)tms->pd[colrReg]; |
| 405 |
+ |
cd->clfb[RED] = 256.*tms->clf[RED] + .5; |
| 406 |
+ |
cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; |
| 407 |
+ |
cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; |
| 408 |
+ |
cd->clfb[EXP] = COLXS; |
| 409 |
+ |
d = TM_BRTSCALE*log(tms->inpsf); |
| 410 |
+ |
cd->inpsfb = d<0. ? d-.5 : d+.5; |
| 411 |
+ |
} |
| 412 |
+ |
|
| 413 |
+ |
|
| 414 |
+ |
static MEM_PTR |
| 415 |
+ |
colrInit(tms) /* initialize private data for tone mapping */ |
| 416 |
+ |
register struct tmStruct *tms; |
| 417 |
+ |
{ |
| 418 |
+ |
register COLRDATA *cd; |
| 419 |
+ |
register int i; |
| 420 |
+ |
/* allocate our data */ |
| 421 |
+ |
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
| 422 |
+ |
if (cd == NULL) |
| 423 |
+ |
return(NULL); |
| 424 |
+ |
tms->pd[colrReg] = (MEM_PTR)cd; |
| 425 |
+ |
/* compute gamma table */ |
| 426 |
+ |
for (i = GAMTSZ; i--; ) |
| 427 |
+ |
cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); |
| 428 |
+ |
/* compute color and scale factors */ |
| 429 |
+ |
colrNewSpace(tms); |
| 430 |
+ |
return((MEM_PTR)cd); |
| 431 |
|
} |