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