--- ray/src/common/tmapcolrs.c 2005/11/15 06:53:00 3.23 +++ ray/src/common/tmapcolrs.c 2022/01/15 16:57:46 3.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tmapcolrs.c,v 3.23 2005/11/15 06:53:00 greg Exp $"; +static const char RCSid[] = "$Id: tmapcolrs.c,v 3.37 2022/01/15 16:57:46 greg Exp $"; #endif /* * Routines for tone mapping on Radiance RGBE and XYZE pictures. @@ -9,27 +9,26 @@ static const char RCSid[] = "$Id: tmapcolrs.c,v 3.23 2 #include "copyright.h" -#include -#include +#include #include -#include -#include "tmprivat.h" -#include "resolu.h" #ifdef PCOND -#include "rtprocess.h" +#include "paths.h" #endif +#include "tmprivat.h" +#include "rtio.h" +#include "resolu.h" -#define GAMTSZ 1024 +#define GAMTSZ 4096 typedef struct { - BYTE gamb[GAMTSZ]; /* gamma lookup table */ + uby8 gamb[GAMTSZ]; /* gamma lookup table */ int clfb[3]; /* encoded tm->clf */ int32 cmatb[3][3]; /* encoded color transform */ TMbright inpsfb; /* encoded tm->inpsf */ } COLRDATA; -static MEM_PTR colrInit(TMstruct *); +static void * colrInit(TMstruct *); static void colrNewSpace(TMstruct *); static gethfunc headline; @@ -38,24 +37,23 @@ static struct tmPackage colrPkg = { /* our package fun }; static int colrReg = -1; /* our package registration number */ -#define LOGISZ 260 -static TMbright logi[LOGISZ]; +static TMbright logi[256]; int tmCvColrs( /* convert RGBE/XYZE colors */ TMstruct *tms, TMbright *ls, -BYTE *cs, +uby8 *cs, COLR *scan, int len ) { static const char funcName[] = "tmCvColrs"; int cmon[4]; - register COLRDATA *cd; - register int i, j, li, bi; - int32 vl; + COLRDATA *cd; + int i, j, bi; + int32 li, vl; if (tms == NULL) returnErr(TM_E_TMINVAL); @@ -67,30 +65,36 @@ int len returnErr(TM_E_CODERR1); for (i = 256; i--; ) logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; - for (i = 256; i < LOGISZ; i++) - logi[i] = 0; tmMkMesofact(); } if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) returnErr(TM_E_NOMEM); for (i = len; i--; ) { if (tmNeedMatrix(tms)) { /* apply color xform */ + bi = 0; 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; + if (vl < 0) + cmon[j] = vl/(int32)0x10000; + else if ((cmon[j] = vl>>16) > bi) + bi = cmon[j]; } cmon[EXP] = scan[i][EXP]; + while (bi >= 256) { /* handle overflow */ + cmon[EXP]++; + for (j = 3; j--; ) cmon[j] >>= 1; + bi >>= 1; + } } else copycolr(cmon, scan[i]); /* world luminance */ - li = cd->clfb[RED]*cmon[RED] + - cd->clfb[GRN]*cmon[GRN] + - cd->clfb[BLU]*cmon[BLU] ; - if (li >= 0xff00) li = 255; - else li >>= 8; + li = cd->clfb[RED]*(int32)cmon[RED] + + cd->clfb[GRN]*(int32)cmon[GRN] + + cd->clfb[BLU]*(int32)cmon[BLU] ; + if (li >= 1L<<(12+8)) li = 255; + else li >>= 12; bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb; if (li > 0) bi += logi[li]; @@ -118,16 +122,18 @@ int len } } } else if (tms->flags & TM_F_BW) { - cmon[RED] = cmon[GRN] = cmon[BLU] = li; + for (j = 3; j--; ) + cs[3*i+j] = tms->cdiv[j]/(TM_BRES>>8); + continue; } else { for (j = 3; j--; ) - if (cmon[j] < 0) cmon[j] = 0; + cmon[j] *= (cmon[j] > 0); } - bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; + bi = ( (uint32)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 = ( (uint32)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 = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12; cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; } returnOK; @@ -149,12 +155,12 @@ static struct radhead { static int headline( /* grok a header line */ - register char *s, + char *s, void *vrh ) { - char fmt[32]; - register struct radhead *rh = vrh; + char fmt[MAXFMTLEN]; + struct radhead *rh = vrh; if (formatval(fmt, s)) { if (!strcmp(fmt, COLRFMT)) @@ -182,7 +188,7 @@ int tmLoadPicture( /* convert Radiance picture */ TMstruct *tms, TMbright **lpp, -BYTE **cpp, +uby8 **cpp, int *xp, int *yp, char *fname, @@ -226,7 +232,7 @@ FILE *fp if (*lpp == NULL) goto done; if (cpp != TM_NOCHROMP) { - *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); + *cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); if (*cpp == NULL) goto done; } @@ -248,12 +254,12 @@ done: /* clean up */ if (fp == NULL) fclose(inpf); if (scanin != NULL) - free((MEM_PTR)scanin); + free(scanin); if (err != TM_E_OK) { if (*lpp != NULL) - free((MEM_PTR)*lpp); + free(*lpp); if (cpp != TM_NOCHROMP && *cpp != NULL) - free((MEM_PTR)*cpp); + free(*cpp); returnErr(err); } returnOK; @@ -263,7 +269,7 @@ done: /* clean up */ #ifdef PCOND static int /* run pcond to map picture */ dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) -BYTE **psp; +uby8 **psp; int *xp, *yp; int flags; RGBPRIMP monpri; @@ -274,10 +280,10 @@ char *fname; TMstruct *tms = NULL; char cmdbuf[1024]; FILE *infp; - register COLR *scan; - register BYTE *rp; + COLR *scan; + uby8 *rp; int y; - register int x; + int x; /* set up gamma correction */ if (setcolrcor(pow, 1./gamval) < 0) returnErr(TM_E_NOMEM); @@ -314,9 +320,9 @@ char *fname; /* allocate arrays */ scan = (COLR *)malloc(sizeof(COLR) * *xp); if (flags & TM_F_BW) - rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); + rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); else - rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); + rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); if (((*psp = rp) == NULL) | (scan == NULL)) { pclose(infp); returnErr(TM_E_NOMEM); @@ -325,8 +331,8 @@ char *fname; for (y = 0; y < *yp; y++) { if (freadcolrs(scan, *xp, infp) < 0) { pclose(infp); - free((MEM_PTR)scan); - free((MEM_PTR)*psp); + free(scan); + free(*psp); *psp = NULL; returnErr(TM_E_BADFILE); } @@ -341,7 +347,7 @@ char *fname; *rp++ = scan[x][BLU]; } } - free((MEM_PTR)scan); + free(scan); pclose(infp); returnOK; } @@ -350,7 +356,7 @@ char *fname; int /* map a Radiance picture */ tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp) -BYTE **psp; +uby8 **psp; int *xp, *yp; int flags; RGBPRIMP monpri; @@ -359,8 +365,8 @@ char *fname; FILE *fp; { char *funcName = fname==NULL ? "tmMapPicture" : fname; - TMstruct *tms; - BYTE *cp; + TMstruct *tms = NULL; + uby8 *cp; TMbright *lp; int err; /* check arguments */ @@ -390,9 +396,9 @@ FILE *fp; } /* allocate space for result */ if (flags & TM_F_BW) { - *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); + *psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); if (*psp == NULL) { - free((MEM_PTR)lp); + free(lp); tmDone(tms); returnErr(TM_E_NOMEM); } @@ -410,10 +416,10 @@ FILE *fp; err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); done: /* clean up */ - free((MEM_PTR)lp); + free(lp); tmDone(tms); if (err != TM_E_OK) { /* free memory on error */ - free((MEM_PTR)*psp); + free(*psp); *psp = NULL; returnErr(err); } @@ -423,15 +429,15 @@ done: /* clean up */ static void colrNewSpace(tms) /* color space changed for tone mapping */ -register TMstruct *tms; +TMstruct *tms; { - register COLRDATA *cd; + COLRDATA *cd; double d; int i, j; cd = (COLRDATA *)tms->pd[colrReg]; for (i = 3; i--; ) - cd->clfb[i] = 0x100*tms->clf[i] + .5; + cd->clfb[i] = 0x1000*tms->clf[i] + .5; cd->inpsfb = tmCvLuminance(tms->inpsf); for (i = 3; i--; ) for (j = 3; j--; ) { @@ -441,21 +447,21 @@ register TMstruct *tms; } -static MEM_PTR +static void * colrInit(tms) /* initialize private data for tone mapping */ -register TMstruct *tms; +TMstruct *tms; { - register COLRDATA *cd; - register int i; + COLRDATA *cd; + int i; /* allocate our data */ cd = (COLRDATA *)malloc(sizeof(COLRDATA)); if (cd == NULL) return(NULL); - tms->pd[colrReg] = (MEM_PTR)cd; + tms->pd[colrReg] = (void *)cd; /* compute gamma table */ for (i = GAMTSZ; i--; ) cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); /* compute color and scale factors */ colrNewSpace(tms); - return((MEM_PTR)cd); + return((void *)cd); }