--- ray/src/common/tonemap.h 1997/04/18 13:59:48 3.4 +++ ray/src/common/tonemap.h 2003/02/25 02:47:22 3.12 @@ -1,14 +1,16 @@ -/* Copyright (c) 1997 Regents of the University of California */ - -/* SCCSid "$SunId$ LBL" */ - +/* RCSid $Id: tonemap.h,v 3.12 2003/02/25 02:47:22 greg Exp $ */ /* * Header file for tone mapping functions. + * + * Include after "color.h" */ - /* required non-system header files */ -#include "color.h" +#include "copyright.h" +#ifdef __cplusplus +extern "C" { +#endif + /**** Argument Macros ****/ /* Flags of what to do */ #define TM_F_HCONTR 01 /* human contrast sensitivity */ @@ -32,6 +34,7 @@ #define TM_NOCHROMP (BYTE **)NULL /* indicate no chrominances */ #define TM_GETFILE (FILE *)NULL /* indicate file must be opened */ + /**** Error Return Values ****/ #define TM_E_OK 0 /* normal return status */ @@ -48,6 +51,9 @@ #define TM_BRTSCALE 128 /* brightness scale factor (integer) */ +#define TM_NOBRT (-1<<15) /* bogus brightness value */ +#define TM_NOLUM (1e-17) /* ridiculously small luminance */ + #define TM_MAXPKG 8 /* maximum number of color formats */ @@ -58,6 +64,8 @@ #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 */ @@ -71,8 +79,9 @@ extern struct tmStruct { RGBPRIMP inppri; /* current input primaries */ double inpsf; /* current input scalefactor */ 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 */ @@ -88,7 +97,7 @@ struct tmPackage { #else struct tmPackage { MEM_PTR (*Init)(struct tmStruct *tms); - int (*NewSpace)(struct tmStruct *tms); + void (*NewSpace)(struct tmStruct *tms); void (*Free)(MEM_PTR pp); }; #endif @@ -100,7 +109,7 @@ extern int tmNumPkgs; /* number of registered packages /**** Useful Macros ****/ /* compute luminance from encoded value */ -#define tmLuminance(li) exp((li)/(double)TM_BRTSCALE) +#define tmLuminance(li) exp((li)*(1./TM_BRTSCALE)) /* does tone mapping need color matrix? */ #define tmNeedMatrix(t) ((t)->monpri != (t)->inppri) @@ -118,10 +127,12 @@ extern int tmNumPkgs; /* number of registered packages #ifdef NOPROTO extern struct tmStruct *tmInit(), *tmPop(), *tmDup(); +extern int tmSetSpace(), tmPull(), tmPush(); extern void tmClearHisto(), tmDone(); -extern int tmSetSpace(), tmCvColors(), tmCvColrs(); -extern int tmAddHisto(), tmComputeMapping(), tmMapPixels(); -extern int tmLoadPicture(), tmMapPicture(), tmPull(), tmPush(); +extern int tmAddHisto(); +extern int tmFixedMapping(), tmComputeMapping(), tmMapPixels(); +extern int tmCvColors(), tmCvGrays(), tmCvColrs(); +extern int tmLoadPicture(), tmMapPicture(); #else @@ -155,32 +166,6 @@ tmClearHisto(void); */ extern int -tmCvColors(TMbright *ls, BYTE *cs, COLOR *scan, int len); -/* - Convert RGB/XYZ float scanline to encoded luminance and chrominance. - - ls - returned encoded luminance values. - cs - returned encoded chrominance values (Note 2). - scan - input scanline. - len - scanline length. - - returns - 0 on success, TM_E_* on error. -*/ - -extern int -tmCvColrs(TMbright *ls, BYTE *cs, COLR *scan, int len); -/* - Convert RGBE/XYZE scanline to encoded luminance and chrominance. - - ls - returned encoded luminance values. - cs - returned encoded chrominance values (Note 2). - scan - input scanline. - len - scanline length. - - returns - 0 on success, TM_E_* on error. -*/ - -extern int tmAddHisto(TMbright *ls, int len, int wt); /* Add brightness values to current histogram. @@ -193,9 +178,26 @@ tmAddHisto(TMbright *ls, int len, int wt); */ extern int +tmFixedMapping(double expmult, double gamval); +/* + Assign a fixed, linear tone-mapping using the given multiplier, + which is the ratio of maximum output to uncalibrated input. + This mapping will be used in subsequent calls to tmMapPixels() + until a new tone mapping is computed. + Only the min. and max. values are used from the histogram. + + expmult - the fixed exposure multiplier to use. + gamval - display gamma response (0. for default). + returns - 0 on success, TM_E_* on error. +*/ + +extern int tmComputeMapping(double gamval, double Lddyn, double Ldmax); /* - Compute tone mapping function. + Compute tone mapping function from the current histogram. + 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). @@ -217,46 +219,6 @@ tmMapPixels(BYTE *ps, TMbright *ls, BYTE *cs, int len) returns - 0 on success, TM_E_* on failure. */ -extern int -tmLoadPicture(TMbright **lpp, BYTE **cpp, int *xp, int *yp, - char *fname, FILE *fp); -/* - Load Radiance picture and convert to tone mapping representation. - Calls tmSetSpace() to calibrate input color space. - - lpp - returned array of encoded luminances, English ordering. - cpp - returned array of encoded chrominances (Note 2). - xp, yp - returned picture dimensions. - fname - picture file name. - fp - pointer to open file (Note 3). - - returns - 0 on success, TM_E_* on failure. -*/ - -extern int -tmMapPicture(BYTE **psp, int *xp, int *yp, int flags, - RGBPRIMP monpri, double gamval, double Lddyn, double Ldmax, - char *fname, FILE *fp); -/* - Load and apply tone mapping to Radiance picture. - Stack is restored to its original state upon return. - 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. - - psp - returned array of tone mapped pixels, English ordering. - xp, yp - returned picture dimensions. - flags - TM_F_* flags indicating what is to be done. - monpri - display monitor primaries (Note 1). - gamval - display gamma response. - Lddyn - the display's dynamic range (0. for default). - Ldmax - maximum display luminance in cd/m^2 (0. for default). - fname - picture file name. - fp - pointer to open file (Note 3). - - returns - 0 on success, TM_E_* on failure. -*/ - extern struct tmStruct * tmPop(void); /* @@ -305,6 +267,88 @@ tmDone(struct tmStruct *tms); tms - tone mapping structure to free. */ +extern int +tmCvColors(TMbright *ls, BYTE *cs, COLOR *scan, int len); +/* + Convert RGB/XYZ float scanline to encoded luminance and chrominance. + + ls - returned encoded luminance values. + cs - returned encoded chrominance values (Note 2). + scan - input scanline. + len - scanline length. + + returns - 0 on success, TM_E_* on error. +*/ + +extern int +tmCvGrays(TMbright *ls, float *scan, int len); +/* + Convert gray float scanline to encoded luminance. + + ls - returned encoded luminance values. + scan - input scanline. + len - scanline length. + + returns - 0 on success, TM_E_* on error. +*/ + +extern int +tmCvColrs(TMbright *ls, BYTE *cs, COLR *scan, int len); +/* + Convert RGBE/XYZE scanline to encoded luminance and chrominance. + + ls - returned encoded luminance values. + cs - returned encoded chrominance values (Note 2). + scan - input scanline. + len - scanline length. + + returns - 0 on success, TM_E_* on error. +*/ + +extern int +tmLoadPicture(TMbright **lpp, BYTE **cpp, int *xp, int *yp, + 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, picture ordering. + cpp - returned array of encoded chrominances (Note 2). + xp, yp - returned picture dimensions. + fname - picture file name. + fp - pointer to open file (Note 3). + + returns - 0 on success, TM_E_* on failure. +*/ + +extern int +tmMapPicture(BYTE **psp, int *xp, int *yp, int flags, + RGBPRIMP monpri, double gamval, double Lddyn, double Ldmax, + char *fname, FILE *fp); +/* + Load and apply tone mapping to Radiance picture. + Stack is restored to its original state upon return. + 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, picture ordering. + xp, yp - returned picture dimensions. + flags - TM_F_* flags indicating what is to be done. + monpri - display monitor primaries (Note 1). + gamval - display gamma response. + Lddyn - the display's dynamic range (0. for default). + Ldmax - maximum display luminance in cd/m^2 (0. for default). + fname - picture file name. + fp - pointer to open file (Note 3). + + returns - 0 on success, TM_E_* on failure. +*/ + #endif @@ -316,7 +360,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 @@ -378,3 +422,7 @@ tmDone(struct tmStruct *tms); function will open the file, read its contents and close it before returning, whether or not an error was encountered. */ + +#ifdef __cplusplus +} +#endif