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