ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.h
Revision: 3.2
Committed: Tue Apr 15 20:04:41 1997 UTC (27 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.1: +1 -1 lines
Log Message:
changed TM_F_NOERRS to TM_F_NOSTDERR
skipped histogram clipping for TM_F_LINEAR in tmComputeMapping()

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