ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.10
Committed: Sun Dec 20 16:38:29 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.9: +2 -0 lines
Log Message:
added TM_NOBRT macro for undefined brightnesses

File Contents

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