ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.4
Committed: Fri Apr 18 13:59:48 1997 UTC (27 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.3: +48 -8 lines
Log Message:
added notion of conversion packages with private data

File Contents

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