ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.11
Committed: Sat Feb 22 02:07:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.10: +173 -82 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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