ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.14
Committed: Fri Jun 27 06:53:22 2003 UTC (20 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.13: +1 -4 lines
Log Message:
Broke standard.h into rtio.h, rterror.h, rtmath.h, and rtmisc.h

File Contents

# User Rev Content
1 greg 3.14 /* RCSid $Id: tonemap.h,v 3.13 2003/06/06 16:38:47 schorsch Exp $ */
2 greg 3.1 /*
3     * Header file for tone mapping functions.
4 gwlarson 3.8 *
5     * Include after "color.h"
6 greg 3.1 */
7 schorsch 3.13 #ifndef _RAD_TONEMAP_H_
8     #define _RAD_TONEMAP_H_
9 greg 3.11 #ifdef __cplusplus
10     extern "C" {
11     #endif
12 schorsch 3.13
13 greg 3.1 /**** Argument Macros ****/
14     /* Flags of what to do */
15     #define TM_F_HCONTR 01 /* human contrast sensitivity */
16     #define TM_F_MESOPIC 02 /* mesopic color sensitivity */
17     #define TM_F_LINEAR 04 /* linear brightness mapping */
18     #define TM_F_ACUITY 010 /* acuity adjustment */
19     #define TM_F_VEIL 020 /* veiling glare */
20     #define TM_F_CWEIGHT 040 /* center weighting */
21     #define TM_F_FOVEAL 0100 /* use foveal sample size */
22     #define TM_F_BW 0200 /* luminance only */
23 greg 3.2 #define TM_F_NOSTDERR 0400 /* don't report errors to stderr */
24 greg 3.1 /* combined modes */
25     #define TM_F_CAMERA 0
26     #define TM_F_HUMAN (TM_F_HCONTR|TM_F_MESOPIC|TM_F_VEIL|\
27     TM_F_ACUITY|TM_F_FOVEAL)
28     #define TM_F_UNIMPL (TM_F_CWEIGHT|TM_F_VEIL|TM_F_ACUITY|TM_F_FOVEAL)
29    
30     /* special pointer values */
31     #define TM_XYZPRIM (RGBPRIMP)NULL /* indicate XYZ primaries (Note 1) */
32     #define TM_NOCHROM (BYTE *)NULL /* indicate no chrominance */
33     #define TM_NOCHROMP (BYTE **)NULL /* indicate no chrominances */
34     #define TM_GETFILE (FILE *)NULL /* indicate file must be opened */
35 gwlarson 3.8
36 greg 3.1
37     /**** Error Return Values ****/
38    
39     #define TM_E_OK 0 /* normal return status */
40     #define TM_E_NOMEM 1 /* out of memory */
41     #define TM_E_ILLEGAL 2 /* illegal argument value */
42     #define TM_E_TMINVAL 3 /* no valid tone mapping */
43     #define TM_E_TMFAIL 4 /* cannot compute tone mapping */
44     #define TM_E_BADFILE 5 /* cannot open or understand file */
45 greg 3.4 #define TM_E_CODERR1 6 /* code consistency error 1 */
46     #define TM_E_CODERR2 7 /* code consistency error 2 */
47 greg 3.1
48 greg 3.4
49 greg 3.1 /**** Conversion Constants and Table Sizes ****/
50    
51     #define TM_BRTSCALE 128 /* brightness scale factor (integer) */
52    
53 gwlarson 3.10 #define TM_NOBRT (-1<<15) /* bogus brightness value */
54 greg 3.11 #define TM_NOLUM (1e-17) /* ridiculously small luminance */
55 gwlarson 3.10
56 greg 3.4 #define TM_MAXPKG 8 /* maximum number of color formats */
57 greg 3.1
58    
59 greg 3.4 /**** Global Data Types and Structures ****/
60    
61     #ifndef MEM_PTR
62     #define MEM_PTR void *
63     #endif
64    
65 greg 3.1 extern char *tmErrorMessage[]; /* error messages */
66 gregl 3.6 extern int tmLastError; /* last error incurred by library */
67     extern char *tmLastFunction; /* error-generating function name */
68 greg 3.1
69     typedef short TMbright; /* encoded luminance type */
70    
71     /* basic tone mapping data structure */
72     extern struct tmStruct {
73     int flags; /* flags of what to do */
74     RGBPRIMP monpri; /* monitor RGB primaries */
75     double mongam; /* monitor gamma value (approx.) */
76     COLOR clf; /* computed luminance coefficients */
77 greg 3.4 int cdiv[3]; /* computed color divisors */
78 greg 3.1 RGBPRIMP inppri; /* current input primaries */
79     double inpsf; /* current input scalefactor */
80     COLORMAT cmat; /* color conversion matrix */
81 gregl 3.7 TMbright hbrmin, hbrmax; /* histogram brightness limits */
82 greg 3.1 int *histo; /* input histogram */
83 gregl 3.7 TMbright mbrmin, mbrmax; /* mapped brightness limits */
84 greg 3.1 unsigned short *lumap; /* computed luminance map */
85     struct tmStruct *tmprev; /* previous tone mapping */
86 greg 3.4 MEM_PTR pd[TM_MAXPKG]; /* pointers to private data */
87 greg 3.1 } *tmTop; /* current tone mapping stack */
88 greg 3.4
89     /* conversion package functions */
90     struct tmPackage {
91     MEM_PTR (*Init)(struct tmStruct *tms);
92 greg 3.11 void (*NewSpace)(struct tmStruct *tms);
93 greg 3.4 void (*Free)(MEM_PTR pp);
94     };
95     /* our list of conversion packages */
96     extern struct tmPackage *tmPkg[TM_MAXPKG];
97     extern int tmNumPkgs; /* number of registered packages */
98    
99    
100 greg 3.1 /**** Useful Macros ****/
101    
102 greg 3.4 /* compute luminance from encoded value */
103 greg 3.11 #define tmLuminance(li) exp((li)*(1./TM_BRTSCALE))
104 greg 3.1
105 greg 3.4 /* does tone mapping need color matrix? */
106     #define tmNeedMatrix(t) ((t)->monpri != (t)->inppri)
107    
108     /* register a conversion package */
109     #define tmRegPkg(pf) ( tmNumPkgs >= TM_MAXPKG ? -1 : \
110     (tmPkg[tmNumPkgs] = (pf), tmNumPkgs++) )
111    
112     /* get the specific package's data */
113     #define tmPkgData(t,i) ((t)->pd[i]!=NULL ? (t)->pd[i] : (*tmPkg[i]->Init)(t))
114    
115    
116 greg 3.1 /**** Library Function Calls ****/
117    
118    
119     extern struct tmStruct *
120     tmInit(int flags, RGBPRIMP monpri, double gamval);
121     /*
122     Initialize new tone mapping and push it onto stack.
123    
124     flags - TM_F_* flags indicating what is to be done.
125     monpri - display monitor primaries (Note 1).
126     gamval - display gamma response (can be approximate).
127    
128     returns - new tmTop, or NULL if insufficient memory.
129     */
130    
131     extern int
132     tmSetSpace(RGBPRIMP pri, double sf);
133     /*
134     Set color primaries and scale factor for incoming scanlines.
135    
136     pri - RGB color input primaries (Note 1).
137     sf - scale factor to get to luminance in cd/m^2.
138    
139     returns - 0 on success, TM_E_* code on failure.
140     */
141    
142     extern void
143     tmClearHisto(void);
144     /*
145     Clear histogram for current tone mapping.
146     */
147    
148     extern int
149     tmAddHisto(TMbright *ls, int len, int wt);
150     /*
151     Add brightness values to current histogram.
152    
153     ls - encoded luminance values.
154     len - number of luminance values.
155     wt - integer weight to use for each value (usually 1 or -1).
156    
157     returns - 0 on success, TM_E_* on error.
158     */
159    
160     extern int
161 greg 3.11 tmFixedMapping(double expmult, double gamval);
162     /*
163     Assign a fixed, linear tone-mapping using the given multiplier,
164     which is the ratio of maximum output to uncalibrated input.
165     This mapping will be used in subsequent calls to tmMapPixels()
166     until a new tone mapping is computed.
167     Only the min. and max. values are used from the histogram.
168    
169     expmult - the fixed exposure multiplier to use.
170     gamval - display gamma response (0. for default).
171     returns - 0 on success, TM_E_* on error.
172     */
173    
174     extern int
175 greg 3.1 tmComputeMapping(double gamval, double Lddyn, double Ldmax);
176     /*
177 greg 3.11 Compute tone mapping function from the current histogram.
178     This mapping will be used in subsequent calls to tmMapPixels()
179     until a new tone mapping is computed.
180     I.e., calls to tmAddHisto() have no immediate effect.
181 greg 3.1
182     gamval - display gamma response (0. for default).
183     Lddyn - the display's dynamic range (0. for default).
184     Ldmax - maximum display luminance in cd/m^2 (0. for default).
185    
186     returns - 0 on success, TM_E_* on failure.
187     */
188    
189     extern int
190     tmMapPixels(BYTE *ps, TMbright *ls, BYTE *cs, int len);
191     /*
192     Apply tone mapping function to pixel values.
193    
194     ps - returned pixel values (Note 2).
195     ls - encoded luminance values.
196     cs - encoded chrominance values (Note 2).
197     len - number of pixels.
198    
199     returns - 0 on success, TM_E_* on failure.
200     */
201    
202     extern struct tmStruct *
203     tmPop(void);
204     /*
205     Pops current tone mapping from stack.
206    
207     returns - pointer to tone mapping structure, or NULL if none.
208     */
209    
210     extern int
211     tmPull(struct tmStruct *tms);
212     /*
213     Pull tone mapping from anywhere in stack.
214    
215     tms - tone mapping structure to find.
216    
217     returns - 1 if found in stack, 0 otherwise.
218     */
219    
220     extern int
221     tmPush(struct tmStruct *tms);
222     /*
223     Make tone mapping active by (pulling it and) pushing it to the top.
224    
225     tms - initialized tone mapping structure.
226    
227     returns - 0 on success, TM_E_* if tms is invalid.
228     */
229    
230 greg 3.3 extern struct tmStruct *
231     tmDup(void);
232     /*
233     Duplicate the current tone mapping into a new structure on the stack.
234    
235     returns - pointer to new top, or NULL on error.
236     */
237    
238 greg 3.1 extern void
239     tmDone(struct tmStruct *tms);
240     /*
241     Free data associated with the given tone mapping structure.
242     Calls tmPull() first to remove it from the stack if present.
243     The calls tmDone(tmPop()), tmDone(tmTop) and tmDone(NULL)
244     all have the same effect, which is to remove and free
245     the current tone mapping.
246    
247     tms - tone mapping structure to free.
248     */
249    
250 greg 3.11 extern int
251     tmCvColors(TMbright *ls, BYTE *cs, COLOR *scan, int len);
252     /*
253     Convert RGB/XYZ float scanline to encoded luminance and chrominance.
254    
255     ls - returned encoded luminance values.
256     cs - returned encoded chrominance values (Note 2).
257     scan - input scanline.
258     len - scanline length.
259    
260     returns - 0 on success, TM_E_* on error.
261     */
262    
263     extern int
264     tmCvGrays(TMbright *ls, float *scan, int len);
265     /*
266     Convert gray float scanline to encoded luminance.
267    
268     ls - returned encoded luminance values.
269     scan - input scanline.
270     len - scanline length.
271    
272     returns - 0 on success, TM_E_* on error.
273     */
274    
275     extern int
276     tmCvColrs(TMbright *ls, BYTE *cs, COLR *scan, int len);
277     /*
278     Convert RGBE/XYZE scanline to encoded luminance and chrominance.
279    
280     ls - returned encoded luminance values.
281     cs - returned encoded chrominance values (Note 2).
282     scan - input scanline.
283     len - scanline length.
284    
285     returns - 0 on success, TM_E_* on error.
286     */
287    
288     extern int
289     tmLoadPicture(TMbright **lpp, BYTE **cpp, int *xp, int *yp,
290     char *fname, FILE *fp);
291     /*
292     Load Radiance picture and convert to tone mapping representation.
293     Memory for the luminance and chroma arrays is allocated using
294     malloc(3), and should be freed with free(3) when no longer needed.
295     Calls tmSetSpace() to calibrate input color space.
296    
297     lpp - returned array of encoded luminances, picture ordering.
298     cpp - returned array of encoded chrominances (Note 2).
299     xp, yp - returned picture dimensions.
300     fname - picture file name.
301     fp - pointer to open file (Note 3).
302    
303     returns - 0 on success, TM_E_* on failure.
304     */
305    
306     extern int
307     tmMapPicture(BYTE **psp, int *xp, int *yp, int flags,
308     RGBPRIMP monpri, double gamval, double Lddyn, double Ldmax,
309     char *fname, FILE *fp);
310     /*
311     Load and apply tone mapping to Radiance picture.
312     Stack is restored to its original state upon return.
313     If fp is TM_GETFILE and (flags&TM_F_UNIMPL)!=0, tmMapPicture()
314     calls pcond to perform the actual conversion, which takes
315     longer but gives access to all the TM_F_* features.
316     Memory for the final pixel array is allocated using malloc(3),
317     and should be freed with free(3) when it is no longer needed.
318    
319     psp - returned array of tone mapped pixels, picture ordering.
320     xp, yp - returned picture dimensions.
321     flags - TM_F_* flags indicating what is to be done.
322     monpri - display monitor primaries (Note 1).
323     gamval - display gamma response.
324     Lddyn - the display's dynamic range (0. for default).
325     Ldmax - maximum display luminance in cd/m^2 (0. for default).
326     fname - picture file name.
327     fp - pointer to open file (Note 3).
328    
329     returns - 0 on success, TM_E_* on failure.
330     */
331    
332 greg 3.4
333    
334 greg 3.1 /**** Notes ****/
335     /*
336     General:
337    
338     The usual sequence after calling tmInit() is to convert some
339     pixel values to chroma and luminance encodings, which can
340     be passed to tmAddHisto() to put into the tone mapping histogram.
341     This histogram is then used along with the display parameters
342 greg 3.5 by tmComputeMapping() to compute the luminance mapping function.
343 greg 3.1 (Colors are tone-mapped as they are converted if TM_F_MESOPIC
344     is set.) The encoded chroma and luminance values may then be
345     passed to tmMapPixels() to apply the computed tone mapping in
346     a second pass.
347    
348     Especially for RGBE colors in the same color space as the
349     target display, the conversion to chroma and luminance values
350     is fast enough that it may be recomputed on the second pass
351     if memory is at a premium. (Since the chroma values are only
352     used during final mapping, setting the cs parameter to TM_NOCHROM
353     on the first pass will save time.) Another memory saving option
354     if third and subsequent passes are not needed is to use the
355 greg 3.3 same array to store the mapped pixels as used to store
356 greg 3.1 the encoded chroma values. This way, only two extra bytes
357     for storing encoded luminances are required per pixel. This
358     is the method employed by tmMapPicture(), for example.
359     */
360     /*
361     Note 1:
362    
363     All RGBPRIMP values should be pointers to static structures.
364     I.e., the same pointer should be used for the same primaries,
365     and those primaries should not change from one call to the next.
366     This is because the routines use the pointer value to identify
367     computed conversion matrices, so the matrices do not have to be
368     rebuilt each time. If you are using the standard primaries,
369     use the standard primary pointer, stdprims.
370    
371     To indicate a set of input primaries are actually CIE XYZ,
372     the TM_XYZPRIM macro may be passed. If the input primaries
373     are unknown, it is wisest to pass tmTop->monpri to tmSetSpace().
374     By default, the input primaries equal the monitor primaries
375     and the scalefactor is set to WHTEFFICACY in tmInit().
376     */
377     /*
378     Note 2:
379    
380     Chrominance is optional in most of these routines, whether
381     TM_F_BW is set or not. Passing a value of TM_NOCHROM (or
382     TM_NOCHROMP for picture reading) indicates that returned
383     color values are not desired.
384    
385     If TM_NOCHROM is passed to a mapping routine expecting chroma
386     input, it will do without it and return luminance channel
387     rather than RGB pixel values. Otherwise, the array for
388     returned pixel values may be the same array used to pass
389     encoded chrominances if no other mappings are desired.
390     */
391     /*
392     Note 3:
393    
394     Picture files may either be opened by the calling routine or by
395     the library function. If opened by the calling routine, the
396     pointer to the stream is passed, which may be a pipe or other
397     non-seekable input as it is only read once. It must be
398     positioned at the beginning when the function is called, and it
399     will be positioned at the end on return. It will not be closed.
400     If the passed stream pointer is TM_GETFILE, then the library
401     function will open the file, read its contents and close it
402     before returning, whether or not an error was encountered.
403     */
404 greg 3.11
405     #ifdef __cplusplus
406     }
407     #endif
408 schorsch 3.13 #endif /* _RAD_TONEMAP_H_ */
409