ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.5
Committed: Mon Apr 21 15:23:42 1997 UTC (27 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.4: +5 -1 lines
Log Message:
small comment changes

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 Memory for the luminance and chroma arrays is allocated using
226 malloc(3), and should be freed with free(3) when no longer needed.
227 Calls tmSetSpace() to calibrate input color space.
228
229 lpp - returned array of encoded luminances, English ordering.
230 cpp - returned array of encoded chrominances (Note 2).
231 xp, yp - returned picture dimensions.
232 fname - picture file name.
233 fp - pointer to open file (Note 3).
234
235 returns - 0 on success, TM_E_* on failure.
236 */
237
238 extern int
239 tmMapPicture(BYTE **psp, int *xp, int *yp, int flags,
240 RGBPRIMP monpri, double gamval, double Lddyn, double Ldmax,
241 char *fname, FILE *fp);
242 /*
243 Load and apply tone mapping to Radiance picture.
244 Stack is restored to its original state upon return.
245 If fp is TM_GETFILE and (flags&TM_F_UNIMPL)!=0, tmMapPicture()
246 calls pcond to perform the actual conversion, which takes
247 longer but gives access to all the TM_F_* features.
248 Memory for the final pixel array is allocated using malloc(3),
249 and should be freed with free(3) when it is no longer needed.
250
251 psp - returned array of tone mapped pixels, English ordering.
252 xp, yp - returned picture dimensions.
253 flags - TM_F_* flags indicating what is to be done.
254 monpri - display monitor primaries (Note 1).
255 gamval - display gamma response.
256 Lddyn - the display's dynamic range (0. for default).
257 Ldmax - maximum display luminance in cd/m^2 (0. for default).
258 fname - picture file name.
259 fp - pointer to open file (Note 3).
260
261 returns - 0 on success, TM_E_* on failure.
262 */
263
264 extern struct tmStruct *
265 tmPop(void);
266 /*
267 Pops current tone mapping from stack.
268
269 returns - pointer to tone mapping structure, or NULL if none.
270 */
271
272 extern int
273 tmPull(struct tmStruct *tms);
274 /*
275 Pull tone mapping from anywhere in stack.
276
277 tms - tone mapping structure to find.
278
279 returns - 1 if found in stack, 0 otherwise.
280 */
281
282 extern int
283 tmPush(struct tmStruct *tms);
284 /*
285 Make tone mapping active by (pulling it and) pushing it to the top.
286
287 tms - initialized tone mapping structure.
288
289 returns - 0 on success, TM_E_* if tms is invalid.
290 */
291
292 extern struct tmStruct *
293 tmDup(void);
294 /*
295 Duplicate the current tone mapping into a new structure on the stack.
296
297 returns - pointer to new top, or NULL on error.
298 */
299
300 extern void
301 tmDone(struct tmStruct *tms);
302 /*
303 Free data associated with the given tone mapping structure.
304 Calls tmPull() first to remove it from the stack if present.
305 The calls tmDone(tmPop()), tmDone(tmTop) and tmDone(NULL)
306 all have the same effect, which is to remove and free
307 the current tone mapping.
308
309 tms - tone mapping structure to free.
310 */
311
312 #endif
313
314
315 /**** Notes ****/
316 /*
317 General:
318
319 The usual sequence after calling tmInit() is to convert some
320 pixel values to chroma and luminance encodings, which can
321 be passed to tmAddHisto() to put into the tone mapping histogram.
322 This histogram is then used along with the display parameters
323 by tmComputeMapping() to compute the luminance mapping function.
324 (Colors are tone-mapped as they are converted if TM_F_MESOPIC
325 is set.) The encoded chroma and luminance values may then be
326 passed to tmMapPixels() to apply the computed tone mapping in
327 a second pass.
328
329 Especially for RGBE colors in the same color space as the
330 target display, the conversion to chroma and luminance values
331 is fast enough that it may be recomputed on the second pass
332 if memory is at a premium. (Since the chroma values are only
333 used during final mapping, setting the cs parameter to TM_NOCHROM
334 on the first pass will save time.) Another memory saving option
335 if third and subsequent passes are not needed is to use the
336 same array to store the mapped pixels as used to store
337 the encoded chroma values. This way, only two extra bytes
338 for storing encoded luminances are required per pixel. This
339 is the method employed by tmMapPicture(), for example.
340 */
341 /*
342 Note 1:
343
344 All RGBPRIMP values should be pointers to static structures.
345 I.e., the same pointer should be used for the same primaries,
346 and those primaries should not change from one call to the next.
347 This is because the routines use the pointer value to identify
348 computed conversion matrices, so the matrices do not have to be
349 rebuilt each time. If you are using the standard primaries,
350 use the standard primary pointer, stdprims.
351
352 To indicate a set of input primaries are actually CIE XYZ,
353 the TM_XYZPRIM macro may be passed. If the input primaries
354 are unknown, it is wisest to pass tmTop->monpri to tmSetSpace().
355 By default, the input primaries equal the monitor primaries
356 and the scalefactor is set to WHTEFFICACY in tmInit().
357 */
358 /*
359 Note 2:
360
361 Chrominance is optional in most of these routines, whether
362 TM_F_BW is set or not. Passing a value of TM_NOCHROM (or
363 TM_NOCHROMP for picture reading) indicates that returned
364 color values are not desired.
365
366 If TM_NOCHROM is passed to a mapping routine expecting chroma
367 input, it will do without it and return luminance channel
368 rather than RGB pixel values. Otherwise, the array for
369 returned pixel values may be the same array used to pass
370 encoded chrominances if no other mappings are desired.
371 */
372 /*
373 Note 3:
374
375 Picture files may either be opened by the calling routine or by
376 the library function. If opened by the calling routine, the
377 pointer to the stream is passed, which may be a pipe or other
378 non-seekable input as it is only read once. It must be
379 positioned at the beginning when the function is called, and it
380 will be positioned at the end on return. It will not be closed.
381 If the passed stream pointer is TM_GETFILE, then the library
382 function will open the file, read its contents and close it
383 before returning, whether or not an error was encountered.
384 */