--- ray/src/common/tmapcolrs.c 2005/01/07 21:41:06 3.18 +++ ray/src/common/tmapcolrs.c 2006/05/18 01:58:18 3.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tmapcolrs.c,v 3.18 2005/01/07 21:41:06 greg Exp $"; +static const char RCSid[] = "$Id: tmapcolrs.c,v 3.24 2006/05/18 01:58:18 greg Exp $"; #endif /* * Routines for tone mapping on Radiance RGBE and XYZE pictures. @@ -16,17 +16,16 @@ static const char RCSid[] = "$Id: tmapcolrs.c,v 3.18 2 #include "tmprivat.h" #include "resolu.h" +#ifdef PCOND #include "rtprocess.h" - -#ifndef TM_PIC_CTRANS -#define TM_PIC_CTRANS 1 /* transform colors? (expensive) */ #endif #define GAMTSZ 1024 typedef struct { BYTE gamb[GAMTSZ]; /* gamma lookup table */ - COLR clfb; /* encoded tm->clf */ + int clfb[3]; /* encoded tm->clf */ + int32 cmatb[3][3]; /* encoded color transform */ TMbright inpsfb; /* encoded tm->inpsf */ } COLRDATA; @@ -53,27 +52,15 @@ int len ) { static const char funcName[] = "tmCvColrs"; - COLR cmon; + int cmon[4]; register COLRDATA *cd; - register int i, bi, li; + register int i, j, li, bi; + int32 vl; if (tms == NULL) returnErr(TM_E_TMINVAL); if ((ls == NULL) | (scan == NULL) | (len < 0)) returnErr(TM_E_ILLEGAL); -#if TM_PIC_CTRANS - if (tmNeedMatrix(tms)) { /* need floating point */ -#else - if (tms->inppri == TM_XYZPRIM) { /* no way around this */ -#endif - register COLOR *newscan; - newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); - if (newscan == NULL) - returnErr(TM_E_NOMEM); - for (i = len; i--; ) - colr_color(newscan[i], scan[i]); - return(tmCvColors(tms, ls, cs, newscan, len)); - } if (colrReg < 0) { /* build tables if necessary */ colrReg = tmRegPkg(&colrPkg); if (colrReg < 0) @@ -87,42 +74,60 @@ int len if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) returnErr(TM_E_NOMEM); for (i = len; i--; ) { - copycolr(cmon, scan[i]); + if (tmNeedMatrix(tms)) { /* apply color xform */ + for (j = 3; j--; ) { + vl = cd->cmatb[j][RED]*(int32)scan[i][RED] + + cd->cmatb[j][GRN]*(int32)scan[i][GRN] + + cd->cmatb[j][BLU]*(int32)scan[i][BLU] ; + if (vl < 0) cmon[j] = vl/0x10000; + else cmon[j] = vl>>16; + } + cmon[EXP] = scan[i][EXP]; + } else + copycolr(cmon, scan[i]); /* world luminance */ - li = ( cd->clfb[RED]*cmon[RED] + + li = cd->clfb[RED]*cmon[RED] + cd->clfb[GRN]*cmon[GRN] + - cd->clfb[BLU]*cmon[BLU] ) >> 8; - bi = BRT2SCALE(cmon[EXP]-COLXS) + - logi[li] + cd->inpsfb; - if (li <= 0) { - bi = TM_NOBRT; /* bogus value */ - li = 1; /* avoid li==0 */ + cd->clfb[BLU]*cmon[BLU] ; + if (li >= 0xfff00) li = 255; + else li >>= 12; + bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb; + if (li > 0) + bi += logi[li]; + else { + bi += logi[0]; + li = 1; /* avoid /0 */ } ls[i] = bi; if (cs == TM_NOCHROM) /* no color? */ continue; /* mesopic adj. */ if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { - register int pf, sli = normscot(cmon); - if (bi < BMESLOWER) + int pf, sli = normscot(cmon); + if (bi < BMESLOWER) { cmon[RED] = cmon[GRN] = cmon[BLU] = sli; - else { + } else { if (tms->flags & TM_F_BW) cmon[RED] = cmon[GRN] = cmon[BLU] = li; pf = tmMesofact[bi-BMESLOWER]; sli *= 256 - pf; - cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; - cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; - cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; + for (j = 3; j--; ) { + cmon[j] = sli + pf*cmon[j]; + if (cmon[j] <= 0) cmon[j] = 0; + else cmon[j] >>= 8; + } } } else if (tms->flags & TM_F_BW) { cmon[RED] = cmon[GRN] = cmon[BLU] = li; + } else { + for (j = 3; j--; ) + if (cmon[j] < 0) cmon[j] = 0; } - bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; + bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12; cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; - bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; + bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12; cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; - bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; + bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12; cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; } returnOK; @@ -197,7 +202,7 @@ FILE *fp ((fname == NULL) & (fp == TM_GETFILE))) returnErr(TM_E_ILLEGAL); *xp = *yp = 0; /* error precaution */ - if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) + if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL) returnErr(TM_E_BADFILE); *lpp = NULL; if (cpp != TM_NOCHROMP) *cpp = NULL; @@ -214,7 +219,7 @@ FILE *fp else if (info.format == FMTCIE) info.primp = TM_XYZPRIM; /* prepare library */ - if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK) + if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK) goto done; err = TM_E_NOMEM; /* allocate arrays */ *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); @@ -422,14 +427,18 @@ register TMstruct *tms; { register COLRDATA *cd; double d; + int i, j; cd = (COLRDATA *)tms->pd[colrReg]; - cd->clfb[RED] = 256.*tms->clf[RED] + .5; - cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; - cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; - cd->clfb[EXP] = COLXS; - d = TM_BRTSCALE*log(tms->inpsf); - cd->inpsfb = d<0. ? d-.5 : d+.5; + for (i = 3; i--; ) + cd->clfb[i] = 0x1000*tms->clf[i] + .5; +fprintf(stderr, "(%d %d %d)\n", cd->clfb[0], cd->clfb[1], cd->clfb[2]); + cd->inpsfb = tmCvLuminance(tms->inpsf); + for (i = 3; i--; ) + for (j = 3; j--; ) { + d = tms->cmat[i][j] / tms->inpsf; + cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5); + } }