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