ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.9
Committed: Mon Oct 5 16:49:52 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.8: +2 -2 lines
Log Message:
corrected comment about pixel ordering for tmLoadPicture and tmMapPicture

File Contents

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