--- ray/src/common/tmapcolrs.c 2022/01/07 23:01:01 3.36 +++ ray/src/common/tmapcolrs.c 2024/09/10 20:24:42 3.38 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tmapcolrs.c,v 3.36 2022/01/07 23:01:01 greg Exp $"; +static const char RCSid[] = "$Id: tmapcolrs.c,v 3.38 2024/09/10 20:24:42 greg Exp $"; #endif /* * Routines for tone mapping on Radiance RGBE and XYZE pictures. @@ -28,7 +28,7 @@ typedef struct { TMbright inpsfb; /* encoded tm->inpsf */ } COLRDATA; -static MEM_PTR colrInit(TMstruct *); +static void * colrInit(TMstruct *); static void colrNewSpace(TMstruct *); static gethfunc headline; @@ -142,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 @@ -167,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); @@ -180,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); } @@ -218,12 +231,19 @@ 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) goto done; @@ -241,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), @@ -254,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; @@ -331,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); } @@ -347,7 +367,7 @@ char *fname; *rp++ = scan[x][BLU]; } } - free((MEM_PTR)scan); + free(scan); pclose(infp); returnOK; } @@ -398,7 +418,7 @@ FILE *fp; if (flags & TM_F_BW) { *psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); if (*psp == NULL) { - free((MEM_PTR)lp); + free(lp); tmDone(tms); returnErr(TM_E_NOMEM); } @@ -416,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); } @@ -447,7 +467,7 @@ TMstruct *tms; } -static MEM_PTR +static void * colrInit(tms) /* initialize private data for tone mapping */ TMstruct *tms; { @@ -457,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); }