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]; |
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) |
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 == -1) { /* 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 */ |
110 |
|
} else if (tmTop->flags & TM_F_BW) { |
111 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
112 |
|
} |
113 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[RED]*cmon[RED]/li ) >> 8; |
114 |
< |
cs[3*i ] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
115 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[GRN]*cmon[GRN]/li ) >> 8; |
116 |
< |
cs[3*i+1] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
117 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[BLU]*cmon[BLU]/li ) >> 8; |
118 |
< |
cs[3*i+2] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
113 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
114 |
> |
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
115 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
116 |
> |
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
117 |
> |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
118 |
> |
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
119 |
|
} |
120 |
|
returnOK; |
121 |
|
} |
284 |
|
returnErr(TM_E_BADFILE); |
285 |
|
} |
286 |
|
/* allocate arrays */ |
287 |
+ |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
288 |
|
if (flags & TM_F_BW) |
289 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
290 |
|
else |
291 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
269 |
– |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
292 |
|
if ((*psp = rp) == NULL | scan == NULL) { |
293 |
|
pclose(infp); |
294 |
|
returnErr(TM_E_NOMEM); |
297 |
|
for (y = 0; y < *yp; y++) { |
298 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
299 |
|
pclose(infp); |
300 |
+ |
free((char *)scan); |
301 |
+ |
free((char *)*psp); |
302 |
+ |
*psp = NULL; |
303 |
|
returnErr(TM_E_BADFILE); |
304 |
|
} |
305 |
|
colrs_gambs(scan, *xp); |
360 |
|
/* allocate space for result */ |
361 |
|
if (flags & TM_F_BW) { |
362 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
363 |
< |
if (*psp == NULL) |
363 |
> |
if (*psp == NULL) { |
364 |
> |
free((char *)lp); |
365 |
> |
tmDone(NULL); |
366 |
|
returnErr(TM_E_NOMEM); |
367 |
+ |
} |
368 |
|
cp = TM_NOCHROM; |
369 |
|
} else |
370 |
|
*psp = cp; |
387 |
|
returnErr(err); |
388 |
|
} |
389 |
|
returnOK; |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
|
393 |
+ |
static void |
394 |
+ |
colrNewSpace(tms) /* color space changed for tone mapping */ |
395 |
+ |
register struct tmStruct *tms; |
396 |
+ |
{ |
397 |
+ |
register COLRDATA *cd; |
398 |
+ |
double d; |
399 |
+ |
|
400 |
+ |
cd = (COLRDATA *)tms->pd[colrReg]; |
401 |
+ |
cd->clfb[RED] = 256.*tms->clf[RED] + .5; |
402 |
+ |
cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; |
403 |
+ |
cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; |
404 |
+ |
cd->clfb[EXP] = COLXS; |
405 |
+ |
d = TM_BRTSCALE*log(tms->inpsf); |
406 |
+ |
cd->inpsfb = d<0. ? d-.5 : d+.5; |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
|
410 |
+ |
static MEM_PTR |
411 |
+ |
colrInit(tms) /* initialize private data for tone mapping */ |
412 |
+ |
register struct tmStruct *tms; |
413 |
+ |
{ |
414 |
+ |
register COLRDATA *cd; |
415 |
+ |
register int i; |
416 |
+ |
/* allocate our data */ |
417 |
+ |
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
418 |
+ |
if (cd == NULL) |
419 |
+ |
return(NULL); |
420 |
+ |
tms->pd[colrReg] = (MEM_PTR)cd; |
421 |
+ |
/* compute gamma table */ |
422 |
+ |
for (i = GAMTSZ; i--; ) |
423 |
+ |
cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); |
424 |
+ |
/* compute color and scale factors */ |
425 |
+ |
colrNewSpace(tms); |
426 |
+ |
return((MEM_PTR)cd); |
427 |
|
} |