15 |
|
|
16 |
|
#define LOGTABBITS 11 /* log table is 1<<LOGTABBITS long */ |
17 |
|
#define GAMTABBITS 9 /* gamma table is 1<<GAMTABBITS long */ |
18 |
+ |
#define MONGAMTSZ 1024 /* monitor gamma table length */ |
19 |
|
|
20 |
|
static float logtab[1<<LOGTABBITS]; |
21 |
|
static float gamtab[1<<GAMTABBITS]; |
22 |
|
static float gammul[16]; |
23 |
|
static double cur_gam = 0.; |
24 |
+ |
static BYTE mongamtab[MONGAMTSZ]; |
25 |
+ |
static double cur_mongam = 0.; |
26 |
|
|
27 |
|
#define imultpow2(i,s) ((s)>=0 ? (i)<<(s) : (i)>>-(s)) |
28 |
|
|
41 |
|
} |
42 |
|
|
43 |
|
|
44 |
< |
/* Fill our gamma table */ |
44 |
> |
/* Fill our input gamma table */ |
45 |
|
static void |
46 |
|
mkGamTable(double gv) |
47 |
|
{ |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
+ |
/* Fill our monitor gamma table */ |
61 |
+ |
static void |
62 |
+ |
mkMonGamTable() |
63 |
+ |
{ |
64 |
+ |
int i; |
65 |
+ |
|
66 |
+ |
if (tmTop->mongam == cur_mongam) |
67 |
+ |
return; |
68 |
+ |
for (i = MONGAMTSZ; i--; ) |
69 |
+ |
mongamtab[i] = 256.*pow((i+.5)*(1./MONGAMTSZ), 1./tmTop->mongam); |
70 |
+ |
cur_mongam = tmTop->mongam; |
71 |
+ |
} |
72 |
+ |
|
73 |
+ |
|
74 |
|
/* Find normalizing shift value for a 2-byte unsigned integer */ |
75 |
|
static int |
76 |
|
normShift16(int i) |
111 |
|
return; |
112 |
|
} |
113 |
|
/* non-linear case */ |
114 |
< |
/* XXX Uncomment if routine is made public |
114 |
> |
/* XXX Uncomment if this routine is made public |
115 |
|
if (gv != cur_gam) |
116 |
|
mkGamTable(gv); |
117 |
|
*/ |
159 |
|
continue; |
160 |
|
} |
161 |
|
d = logtab[ imultpow2(*scan,LOGTABBITS-15+nshft) & |
162 |
< |
((1L<<LOGTABBITS)-1) ] |
163 |
< |
- M_LN2*nshft; |
162 |
> |
((1L<<LOGTABBITS)-1) ]; |
163 |
> |
d -= M_LN2*nshft; |
164 |
|
d = (double)TM_BRTSCALE * (gv*d + log_inpsf); |
165 |
|
*ls++ = (d>0. ? d+.5 : d-.5); |
166 |
|
scan++; |
183 |
|
returnErr(TM_E_ILLEGAL); |
184 |
|
if (gv <= 0.) |
185 |
|
gv = DEFGAM; |
186 |
< |
/* update gamma table */ |
186 |
> |
/* sync input gamma table */ |
187 |
|
if (gv != 1. & gv != cur_gam) |
188 |
|
mkGamTable(gv); |
172 |
– |
#if 0 |
189 |
|
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
174 |
– |
#else |
175 |
– |
{ |
176 |
– |
#endif |
190 |
|
COLOR *newscan; |
191 |
|
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
192 |
|
if (newscan == NULL) |
195 |
|
rgb48_color(newscan[i], scan[i], gv); |
196 |
|
return(tmCvColors(ls, cs, newscan, len)); |
197 |
|
} |
198 |
< |
#if 0 |
198 |
> |
/* sync monitor gamma table */ |
199 |
> |
if (cs != TM_NOCHROM && tmTop->mongam != cur_mongam) |
200 |
> |
mkMonGamTable(); |
201 |
|
/* initialize log table */ |
202 |
|
if (logtab[0] == 0.f) |
203 |
|
mkLogTable(); |
204 |
|
if (cur_inpsf != tmTop->inpsf) |
205 |
|
log_inpsf = log(cur_inpsf = tmTop->inpsf); |
206 |
+ |
if (tmTop->flags & TM_F_MESOPIC) |
207 |
+ |
tmMkMesofact(); |
208 |
|
/* convert scanline */ |
209 |
|
for (i = len; i--; ) { |
210 |
< |
copycolr(cmon, scan[i]); |
211 |
< |
/* world luminance */ |
212 |
< |
li = ( cd->clfb[RED]*cmon[RED] + |
213 |
< |
cd->clfb[GRN]*cmon[GRN] + |
214 |
< |
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
215 |
< |
bi = BRT2SCALE(cmon[EXP]-COLXS) + |
199 |
< |
logi[li] + cd->inpsfb; |
200 |
< |
if (li <= 0) { |
210 |
> |
int nshft = normShift48(scan[i]); |
211 |
> |
COLOR cmon; |
212 |
> |
double lum; |
213 |
> |
int bi; |
214 |
> |
|
215 |
> |
if (nshft < 0) { |
216 |
|
bi = TM_NOBRT; /* bogus value */ |
217 |
< |
li = 1; /* avoid li==0 */ |
217 |
> |
lum = 1.; |
218 |
> |
} else { |
219 |
> |
int j = GAMTABBITS-16+nshft; |
220 |
> |
int nshft2; |
221 |
> |
double d; |
222 |
> |
/* normalized linear */ |
223 |
> |
setcolor(cmon, gamtab[imultpow2(scan[i][0],j)], |
224 |
> |
gamtab[imultpow2(scan[i][1],j)], |
225 |
> |
gamtab[imultpow2(scan[i][2],j)] ); |
226 |
> |
lum = tmTop->clf[RED]*cmon[RED]; |
227 |
> |
lum += tmTop->clf[GRN]*cmon[GRN]; |
228 |
> |
lum += tmTop->clf[BLU]*cmon[BLU]; |
229 |
> |
/* convert to log Y */ |
230 |
> |
j = lum * (double)(1L<<16); |
231 |
> |
nshft2 = normShift16(j); |
232 |
> |
d = logtab[ imultpow2(j,LOGTABBITS-15+nshft2) & |
233 |
> |
((1L<<LOGTABBITS)-1) ]; |
234 |
> |
d -= M_LN2*(gv*nshft + nshft2); |
235 |
> |
d = (double)TM_BRTSCALE*(d + log_inpsf); |
236 |
> |
bi = (int)(d>0. ? d+.5 : d-.5); |
237 |
|
} |
238 |
+ |
/* world luminance */ |
239 |
|
ls[i] = bi; |
240 |
|
if (cs == TM_NOCHROM) /* no color? */ |
241 |
|
continue; |
242 |
|
/* mesopic adj. */ |
243 |
|
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
244 |
< |
int pf, sli = normscot(cmon); |
244 |
> |
double slum = scotlum(cmon); |
245 |
|
if (bi < BMESLOWER) |
246 |
< |
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
246 |
> |
setcolor(cmon, slum, slum, slum); |
247 |
|
else { |
248 |
+ |
double pf; |
249 |
+ |
pf = (1./256.)*tmMesofact[bi-BMESLOWER]; |
250 |
|
if (tmTop->flags & TM_F_BW) |
251 |
< |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
252 |
< |
pf = tmMesofact[bi-BMESLOWER]; |
253 |
< |
sli *= 256 - pf; |
254 |
< |
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
255 |
< |
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
219 |
< |
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
251 |
> |
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
252 |
> |
slum *= 1. - pf; |
253 |
> |
cmon[RED] = slum + pf*cmon[RED]; |
254 |
> |
cmon[GRN] = slum + pf*cmon[GRN]; |
255 |
> |
cmon[BLU] = slum + pf*cmon[BLU]; |
256 |
|
} |
257 |
|
} else if (tmTop->flags & TM_F_BW) { |
258 |
< |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
258 |
> |
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
259 |
|
} |
260 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
261 |
< |
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
262 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
263 |
< |
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
264 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
265 |
< |
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
260 |
> |
bi = (double)MONGAMTSZ*tmTop->clf[RED]*cmon[RED]/lum; |
261 |
> |
cs[3*i ] = bi>=MONGAMTSZ ? 255 : mongamtab[bi]; |
262 |
> |
bi = (double)MONGAMTSZ*tmTop->clf[GRN]*cmon[GRN]/lum; |
263 |
> |
cs[3*i+1] = bi>=MONGAMTSZ ? 255 : mongamtab[bi]; |
264 |
> |
bi = (double)MONGAMTSZ*tmTop->clf[BLU]*cmon[BLU]/lum; |
265 |
> |
cs[3*i+2] = bi>=MONGAMTSZ ? 255 : mongamtab[bi]; |
266 |
|
} |
267 |
|
returnOK; |
232 |
– |
#endif |
268 |
|
} |