--- ray/src/common/tmapcolrs.c 2011/04/22 17:06:26 3.28 +++ ray/src/common/tmapcolrs.c 2024/11/21 17:15:54 3.39 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tmapcolrs.c,v 3.28 2011/04/22 17:06:26 greg Exp $"; +static const char RCSid[] = "$Id: tmapcolrs.c,v 3.39 2024/11/21 17:15:54 greg Exp $"; #endif /* * Routines for tone mapping on Radiance RGBE and XYZE pictures. @@ -9,28 +9,26 @@ static const char RCSid[] = "$Id: tmapcolrs.c,v 3.28 2 #include "copyright.h" -#include #include -#include #include -#include #ifdef PCOND -#include "rtprocess.h" +#include "paths.h" #endif #include "tmprivat.h" +#include "rtio.h" #include "resolu.h" #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; @@ -46,7 +44,7 @@ int tmCvColrs( /* convert RGBE/XYZE colors */ TMstruct *tms, TMbright *ls, -BYTE *cs, +uby8 *cs, COLR *scan, int len ) @@ -73,14 +71,22 @@ int len 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 */ @@ -116,10 +122,12 @@ 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 = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12; cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; @@ -134,15 +142,18 @@ int len #define FMTRGB 1 /* Input is RGBE */ #define FMTCIE 2 /* Input is CIE XYZE */ -#define FMTUNK 3 /* Input format is unspecified */ -#define FMTBAD 4 /* Input is not a recognized format */ +#define FMTSPEC 3 /* Input is N-component spectral data */ +#define FMTUNK 0 /* Input format is unspecified */ +#define FMTBAD (-1) /* Input is not a recognized format */ static struct radhead { int format; /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */ double expos; /* input exposure value */ RGBPRIMP primp; /* input primaries */ RGBPRIMS mypri; /* custom primaries */ -} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS}; + int ncs; /* number of color samples */ + float wpt[4]; /* spectral partition */ +} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS, 3, {0,0,0,0}}; static int @@ -151,7 +162,7 @@ headline( /* grok a header line */ void *vrh ) { - char fmt[32]; + char fmt[MAXFMTLEN]; struct radhead *rh = vrh; if (formatval(fmt, s)) { @@ -159,6 +170,8 @@ headline( /* grok a header line */ rh->format = FMTRGB; else if (!strcmp(fmt, CIEFMT)) rh->format = FMTCIE; + else if (!strcmp(fmt, SPECFMT)) + rh->format = FMTSPEC; else rh->format = FMTBAD; return(0); @@ -172,6 +185,14 @@ headline( /* grok a header line */ rh->primp = rh->mypri; return(0); } + if (isncomp(s)) { + rh->ncs = ncompval(s); + return(0); + } + if (iswlsplit(s)) { + wlsplitval(rh->wpt, s); + return(0); + } return(0); } @@ -180,7 +201,7 @@ int tmLoadPicture( /* convert Radiance picture */ TMstruct *tms, TMbright **lpp, -BYTE **cpp, +uby8 **cpp, int *xp, int *yp, char *fname, @@ -210,21 +231,28 @@ FILE *fp fgetresolu(xp, yp, inpf) < 0) { err = TM_E_BADFILE; goto done; } - if (info.format == FMTUNK) /* assume RGBE format */ + if (info.format == FMTSPEC) { /* valid spectrum? */ + if (info.ncs <= 3) { + err = TM_E_BADFILE; goto done; + } + if (info.wpt[0] == 0) + memcpy(info.wpt, WLPART, sizeof(info.wpt)); + } else if (info.format == FMTUNK) /* assume RGBE format? */ info.format = FMTRGB; - if (info.format == FMTRGB) - info.expos /= WHTEFFICACY; - else if (info.format == FMTCIE) + + if (info.format == FMTCIE) info.primp = TM_XYZPRIM; + else + info.expos /= WHTEFFICACY; /* prepare library */ - if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK) + if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK) goto done; err = TM_E_NOMEM; /* allocate arrays */ *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); 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; } @@ -233,7 +261,7 @@ FILE *fp goto done; err = TM_E_BADFILE; /* read & convert scanlines */ for (i = 0; i < *yp; i++) { - if (freadcolrs(scanin, *xp, inpf) < 0) { + if (fread2colrs(scanin, *xp, inpf, info.ncs, info.wpt) < 0) { err = TM_E_BADFILE; break; } err = tmCvColrs(tms, *lpp + (i * *xp), @@ -246,12 +274,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; @@ -261,7 +289,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; @@ -273,7 +301,7 @@ char *fname; char cmdbuf[1024]; FILE *infp; COLR *scan; - BYTE *rp; + uby8 *rp; int y; int x; /* set up gamma correction */ @@ -312,9 +340,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); @@ -323,8 +351,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); } @@ -339,7 +367,7 @@ char *fname; *rp++ = scan[x][BLU]; } } - free((MEM_PTR)scan); + free(scan); pclose(infp); returnOK; } @@ -348,7 +376,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; @@ -357,8 +385,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 */ @@ -388,9 +416,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); } @@ -408,10 +436,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); } @@ -439,7 +467,7 @@ TMstruct *tms; } -static MEM_PTR +static void * colrInit(tms) /* initialize private data for tone mapping */ TMstruct *tms; { @@ -449,11 +477,11 @@ TMstruct *tms; 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); }