ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.12
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.11: +2 -57 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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