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