ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.15
Committed: Mon Jul 14 04:56:54 2003 UTC (20 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.14: +28 -1 lines
Log Message:
Added tone-mapping support for 16-bit/sample int and IEEE float TIFF images

File Contents

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