--- ray/src/common/tonemap.h 1997/04/16 20:28:07 3.3 +++ ray/src/common/tonemap.h 1997/11/17 13:54:35 3.7 @@ -8,6 +8,7 @@ /* required non-system header files */ #include "color.h" + /**** Argument Macros ****/ /* Flags of what to do */ #define TM_F_HCONTR 01 /* human contrast sensitivity */ @@ -39,16 +40,26 @@ #define TM_E_TMINVAL 3 /* no valid tone mapping */ #define TM_E_TMFAIL 4 /* cannot compute tone mapping */ #define TM_E_BADFILE 5 /* cannot open or understand file */ +#define TM_E_CODERR1 6 /* code consistency error 1 */ +#define TM_E_CODERR2 7 /* code consistency error 2 */ + /**** Conversion Constants and Table Sizes ****/ #define TM_BRTSCALE 128 /* brightness scale factor (integer) */ -#define TM_GAMTSZ 1024 /* gamma lookup table size */ +#define TM_MAXPKG 8 /* maximum number of color formats */ -/**** Global Data Structures ****/ +/**** Global Data Types and Structures ****/ + +#ifndef MEM_PTR +#define MEM_PTR void * +#endif + extern char *tmErrorMessage[]; /* error messages */ +extern int tmLastError; /* last error incurred by library */ +extern char *tmLastFunction; /* error-generating function name */ typedef short TMbright; /* encoded luminance type */ @@ -57,26 +68,57 @@ extern struct tmStruct { int flags; /* flags of what to do */ RGBPRIMP monpri; /* monitor RGB primaries */ double mongam; /* monitor gamma value (approx.) */ - BYTE gamb[TM_GAMTSZ];/* gamma lookup table from mongam */ COLOR clf; /* computed luminance coefficients */ - COLR clfb; /* normalized version of clf */ + int cdiv[3]; /* computed color divisors */ RGBPRIMP inppri; /* current input primaries */ double inpsf; /* current input scalefactor */ - TMbright inpsfb; /* encoded version of inpsf */ COLORMAT cmat; /* color conversion matrix */ - TMbright brmin, brmax; /* input brightness limits */ + TMbright hbrmin, hbrmax; /* histogram brightness limits */ int *histo; /* input histogram */ + TMbright mbrmin, mbrmax; /* mapped brightness limits */ unsigned short *lumap; /* computed luminance map */ struct tmStruct *tmprev; /* previous tone mapping */ + MEM_PTR pd[TM_MAXPKG]; /* pointers to private data */ } *tmTop; /* current tone mapping stack */ - + + /* conversion package functions */ +#ifdef NOPROTO +struct tmPackage { + MEM_PTR (*Init)(); /* initialize private data */ + void (*NewSpace)(); /* new input color space (optional) */ + void (*Free)(); /* free private data */ +}; +#else +struct tmPackage { + MEM_PTR (*Init)(struct tmStruct *tms); + int (*NewSpace)(struct tmStruct *tms); + void (*Free)(MEM_PTR pp); +}; +#endif + /* our list of conversion packages */ +extern struct tmPackage *tmPkg[TM_MAXPKG]; +extern int tmNumPkgs; /* number of registered packages */ + + /**** Useful Macros ****/ + /* compute luminance from encoded value */ #define tmLuminance(li) exp((li)/(double)TM_BRTSCALE) + /* does tone mapping need color matrix? */ +#define tmNeedMatrix(t) ((t)->monpri != (t)->inppri) + + /* register a conversion package */ +#define tmRegPkg(pf) ( tmNumPkgs >= TM_MAXPKG ? -1 : \ + (tmPkg[tmNumPkgs] = (pf), tmNumPkgs++) ) + + /* get the specific package's data */ +#define tmPkgData(t,i) ((t)->pd[i]!=NULL ? (t)->pd[i] : (*tmPkg[i]->Init)(t)) + + /**** Library Function Calls ****/ -#ifdef NOPROTO +#ifdef NOPROTO extern struct tmStruct *tmInit(), *tmPop(), *tmDup(); extern void tmClearHisto(), tmDone(); @@ -156,7 +198,9 @@ tmAddHisto(TMbright *ls, int len, int wt); extern int tmComputeMapping(double gamval, double Lddyn, double Ldmax); /* - Compute tone mapping function. + Compute tone mapping function. This mapping will be used + in subsequent calls to tmMapPixels() until a new tone mapping + is computed. I.e., calls to tmAddHisto() have no immediate effect. gamval - display gamma response (0. for default). Lddyn - the display's dynamic range (0. for default). @@ -183,6 +227,8 @@ tmLoadPicture(TMbright **lpp, BYTE **cpp, int *xp, int char *fname, FILE *fp); /* Load Radiance picture and convert to tone mapping representation. + Memory for the luminance and chroma arrays is allocated using + malloc(3), and should be freed with free(3) when no longer needed. Calls tmSetSpace() to calibrate input color space. lpp - returned array of encoded luminances, English ordering. @@ -204,6 +250,8 @@ tmMapPicture(BYTE **psp, int *xp, int *yp, int flags, If fp is TM_GETFILE and (flags&TM_F_UNIMPL)!=0, tmMapPicture() calls pcond to perform the actual conversion, which takes longer but gives access to all the TM_F_* features. + Memory for the final pixel array is allocated using malloc(3), + and should be freed with free(3) when it is no longer needed. psp - returned array of tone mapped pixels, English ordering. xp, yp - returned picture dimensions. @@ -267,7 +315,8 @@ tmDone(struct tmStruct *tms); */ #endif - + + /**** Notes ****/ /* General: @@ -276,7 +325,7 @@ tmDone(struct tmStruct *tms); pixel values to chroma and luminance encodings, which can be passed to tmAddHisto() to put into the tone mapping histogram. This histogram is then used along with the display parameters - by tmComputMapping() to compute the luminance mapping function. + by tmComputeMapping() to compute the luminance mapping function. (Colors are tone-mapped as they are converted if TM_F_MESOPIC is set.) The encoded chroma and luminance values may then be passed to tmMapPixels() to apply the computed tone mapping in