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 */ |
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 |
|
} |
148 |
|
rh->format = FMTCIE; |
149 |
|
else |
150 |
|
rh->format = FMTBAD; |
151 |
< |
return; |
151 |
> |
return(0); |
152 |
|
} |
153 |
|
if (isexpos(s)) { |
154 |
|
rh->expos *= exposval(s); |
155 |
< |
return; |
155 |
> |
return(0); |
156 |
|
} |
157 |
|
if (isprims(s)) { |
158 |
|
primsval(rh->mypri, s); |
159 |
|
rh->primp = rh->mypri; |
160 |
< |
return; |
160 |
> |
return(0); |
161 |
|
} |
162 |
+ |
return(0); |
163 |
|
} |
164 |
|
|
165 |
|
|
186 |
|
*xp = *yp = 0; /* error precaution */ |
187 |
|
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
188 |
|
returnErr(TM_E_BADFILE); |
189 |
+ |
*lpp = NULL; |
190 |
+ |
if (cpp != TM_NOCHROMP) *cpp = NULL; |
191 |
|
info = rhdefault; /* get our header */ |
192 |
< |
getheader(inpf, headline, (char *)&info); |
192 |
> |
getheader(inpf, headline, (MEM_PTR)&info); |
193 |
|
if (info.format == FMTBAD | info.expos <= 0. || |
194 |
|
fgetresolu(xp, yp, inpf) < 0) { |
195 |
|
err = TM_E_BADFILE; goto done; |
230 |
|
if (fp == NULL) |
231 |
|
fclose(inpf); |
232 |
|
if (scanin != NULL) |
233 |
< |
free((char *)scanin); |
234 |
< |
if (err != TM_E_OK) |
233 |
> |
free((MEM_PTR)scanin); |
234 |
> |
if (err != TM_E_OK) { |
235 |
> |
if (*lpp != NULL) |
236 |
> |
free((MEM_PTR)*lpp); |
237 |
> |
if (cpp != TM_NOCHROMP && *cpp != NULL) |
238 |
> |
free((MEM_PTR)*cpp); |
239 |
|
returnErr(err); |
240 |
+ |
} |
241 |
|
returnOK; |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
+ |
#ifdef PCOND |
246 |
|
int /* run pcond to map picture */ |
247 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
248 |
|
BYTE **psp; |
263 |
|
if (setcolrcor(pow, 1./gamval) < 0) |
264 |
|
returnErr(TM_E_NOMEM); |
265 |
|
/* create command */ |
266 |
< |
strcpy(cmdbuf, "pcond "); |
266 |
> |
strcpy(cmdbuf, PCOND); |
267 |
|
if (flags & TM_F_HCONTR) |
268 |
< |
strcat(cmdbuf, "-s "); |
268 |
> |
strcat(cmdbuf, " -s"); |
269 |
|
if (flags & TM_F_MESOPIC) |
270 |
< |
strcat(cmdbuf, "-c "); |
270 |
> |
strcat(cmdbuf, " -c"); |
271 |
|
if (flags & TM_F_LINEAR) |
272 |
< |
strcat(cmdbuf, "-l "); |
272 |
> |
strcat(cmdbuf, " -l"); |
273 |
|
if (flags & TM_F_ACUITY) |
274 |
< |
strcat(cmdbuf, "-a "); |
274 |
> |
strcat(cmdbuf, " -a"); |
275 |
|
if (flags & TM_F_VEIL) |
276 |
< |
strcat(cmdbuf, "-v "); |
276 |
> |
strcat(cmdbuf, " -v"); |
277 |
|
if (flags & TM_F_CWEIGHT) |
278 |
< |
strcat(cmdbuf, "-w "); |
279 |
< |
sprintf(cmdbuf+strlen(cmdbuf), |
280 |
< |
"-p %f %f %f %f %f %f %f %f -d %f -u %f %s", |
281 |
< |
monpri[RED][CIEX], monpri[RED][CIEY], |
282 |
< |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
283 |
< |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
284 |
< |
monpri[WHT][CIEX], monpri[WHT][CIEY], |
285 |
< |
Lddyn, Ldmax, fname); |
278 |
> |
strcat(cmdbuf, " -w"); |
279 |
> |
if (monpri != stdprims) |
280 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f", |
281 |
> |
monpri[RED][CIEX], monpri[RED][CIEY], |
282 |
> |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
283 |
> |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
284 |
> |
monpri[WHT][CIEX], monpri[WHT][CIEY]); |
285 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname); |
286 |
|
/* start pcond */ |
287 |
|
if ((infp = popen(cmdbuf, "r")) == NULL) |
288 |
|
returnErr(TM_E_BADFILE); |
293 |
|
returnErr(TM_E_BADFILE); |
294 |
|
} |
295 |
|
/* allocate arrays */ |
296 |
+ |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
297 |
|
if (flags & TM_F_BW) |
298 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
299 |
|
else |
300 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
269 |
– |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
301 |
|
if ((*psp = rp) == NULL | scan == NULL) { |
302 |
|
pclose(infp); |
303 |
|
returnErr(TM_E_NOMEM); |
306 |
|
for (y = 0; y < *yp; y++) { |
307 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
308 |
|
pclose(infp); |
309 |
+ |
free((MEM_PTR)scan); |
310 |
+ |
free((MEM_PTR)*psp); |
311 |
+ |
*psp = NULL; |
312 |
|
returnErr(TM_E_BADFILE); |
313 |
|
} |
314 |
|
colrs_gambs(scan, *xp); |
322 |
|
*rp++ = scan[x][BLU]; |
323 |
|
} |
324 |
|
} |
325 |
< |
free((char *)scan); |
325 |
> |
free((MEM_PTR)scan); |
326 |
|
pclose(infp); |
327 |
|
returnOK; |
328 |
|
} |
329 |
+ |
#endif |
330 |
|
|
331 |
|
|
332 |
|
int /* map a Radiance picture */ |
340 |
|
FILE *fp; |
341 |
|
{ |
342 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
308 |
– |
FILE *inpf; |
343 |
|
BYTE *cp; |
344 |
|
TMbright *lp; |
345 |
|
int err; |
352 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
353 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
354 |
|
if (flags & TM_F_BW) monpri = stdprims; |
355 |
+ |
#ifdef PCOND |
356 |
|
/* check for pcond run */ |
357 |
|
if (fp == TM_GETFILE && flags & TM_F_UNIMPL) |
358 |
|
return( dopcond(psp, xp, yp, flags, |
359 |
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
360 |
+ |
#endif |
361 |
|
/* initialize tone mapping */ |
362 |
|
if (tmInit(flags, monpri, gamval) == NULL) |
363 |
|
returnErr(TM_E_NOMEM); |
371 |
|
/* allocate space for result */ |
372 |
|
if (flags & TM_F_BW) { |
373 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
374 |
< |
if (*psp == NULL) |
374 |
> |
if (*psp == NULL) { |
375 |
> |
free((MEM_PTR)lp); |
376 |
> |
tmDone(NULL); |
377 |
|
returnErr(TM_E_NOMEM); |
378 |
+ |
} |
379 |
|
cp = TM_NOCHROM; |
380 |
|
} else |
381 |
|
*psp = cp; |
390 |
|
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
391 |
|
|
392 |
|
done: /* clean up */ |
393 |
< |
free((char *)lp); |
393 |
> |
free((MEM_PTR)lp); |
394 |
|
tmDone(NULL); |
395 |
|
if (err != TM_E_OK) { /* free memory on error */ |
396 |
< |
free((char *)*psp); |
396 |
> |
free((MEM_PTR)*psp); |
397 |
|
*psp = NULL; |
398 |
|
returnErr(err); |
399 |
|
} |
400 |
|
returnOK; |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
|
404 |
+ |
static void |
405 |
+ |
colrNewSpace(tms) /* color space changed for tone mapping */ |
406 |
+ |
register struct tmStruct *tms; |
407 |
+ |
{ |
408 |
+ |
register COLRDATA *cd; |
409 |
+ |
double d; |
410 |
+ |
|
411 |
+ |
cd = (COLRDATA *)tms->pd[colrReg]; |
412 |
+ |
cd->clfb[RED] = 256.*tms->clf[RED] + .5; |
413 |
+ |
cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; |
414 |
+ |
cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; |
415 |
+ |
cd->clfb[EXP] = COLXS; |
416 |
+ |
d = TM_BRTSCALE*log(tms->inpsf); |
417 |
+ |
cd->inpsfb = d<0. ? d-.5 : d+.5; |
418 |
+ |
} |
419 |
+ |
|
420 |
+ |
|
421 |
+ |
static MEM_PTR |
422 |
+ |
colrInit(tms) /* initialize private data for tone mapping */ |
423 |
+ |
register struct tmStruct *tms; |
424 |
+ |
{ |
425 |
+ |
register COLRDATA *cd; |
426 |
+ |
register int i; |
427 |
+ |
/* allocate our data */ |
428 |
+ |
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
429 |
+ |
if (cd == NULL) |
430 |
+ |
return(NULL); |
431 |
+ |
tms->pd[colrReg] = (MEM_PTR)cd; |
432 |
+ |
/* compute gamma table */ |
433 |
+ |
for (i = GAMTSZ; i--; ) |
434 |
+ |
cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); |
435 |
+ |
/* compute color and scale factors */ |
436 |
+ |
colrNewSpace(tms); |
437 |
+ |
return((MEM_PTR)cd); |
438 |
|
} |