summaryrefslogtreecommitdiff
path: root/plugins/AdvaImg/src/FreeImage/tmoDrago03.cpp
blob: b820bb3577c010e4f8433e9a00392f3bf5f61628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ==========================================================
// Tone mapping operator (Drago, 2003)
//
// Design and implementation by
// - Hervé Drolon (drolon@infonie.fr)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================

#include "FreeImage.h"
#include "Utilities.h"
#include "ToneMapping.h"

// ----------------------------------------------------------
// Logarithmic mapping operator
// Reference: 
// [1] F. Drago, K. Myszkowski, T. Annen, and N. Chiba, 
// Adaptive Logarithmic Mapping for Displaying High Contrast Scenes, 
// Eurographics 2003.
// ----------------------------------------------------------

/**
Bias function
*/
static inline double 
biasFunction(const double b, const double x) {
	return pow (x, b);		// pow(x, log(bias)/log(0.5)
}

/**
Padé approximation of log(x + 1)
x(6+x)/(6+4x) good if x < 1
x*(6 + 0.7662x)/(5.9897 + 3.7658x) between 1 and 2
See http://www.nezumi.demon.co.uk/consult/logx.htm
*/
static inline double 
pade_log(const double x) {
	if(x < 1) {
		return (x * (6 + x) / (6 + 4 * x));
	} else if(x < 2) {
		return (x * (6 + 0.7662 * x) / (5.9897 + 3.7658 * x));
	}
	return log(x + 1);
}

/**
Log mapping operator
@param dib Input / Output Yxy image
@param maxLum Maximum luminance
@param avgLum Average luminance (world adaptation luminance)
@param biasParam Bias parameter (a zero value default to 0.85)
@param exposure Exposure parameter (default to 0)
@return Returns TRUE if successful, returns FALSE otherwise
*/
static BOOL 
ToneMappingDrago03(FIBITMAP *dib, const float maxLum, const float avgLum, float biasParam, const float exposure) {
	const float LOG05 = -0.693147F;	// log(0.5) 

	double Lmax, divider, interpol, biasP;
	unsigned x, y;
	double L;

	if(FreeImage_GetImageType(dib) != FIT_RGBF)
		return FALSE;

	const unsigned width  = FreeImage_GetWidth(dib);
	const unsigned height = FreeImage_GetHeight(dib);
	const unsigned pitch  = FreeImage_GetPitch(dib);


	// arbitrary Bias Parameter 
	if(biasParam == 0) 
		biasParam = 0.85F;

	// normalize maximum luminance by average luminance
	Lmax = maxLum / avgLum;
	
	divider = log10(Lmax+1);
	biasP = log(biasParam)/LOG05;

#if !defined(DRAGO03_FAST)

	/**
	Normal tone mapping of every pixel
	further acceleration is obtained by a Padé approximation of log(x + 1)
	*/
	BYTE *bits = (BYTE*)FreeImage_GetBits(dib);
	for(y = 0; y < height; y++) {
		FIRGBF *pixel = (FIRGBF*)bits;
		for(x = 0; x < width; x++) {
			double Yw = pixel[x].red / avgLum;
			Yw *= exposure;
			interpol = log(2 + biasFunction(biasP, Yw / Lmax) * 8);
			L = pade_log(Yw);// log(Yw + 1)
			pixel[x].red = (float)((L / interpol) / divider);
		}
		// next line
		bits += pitch;
	}

#else
	unsigned index;
	int i, j;

	unsigned max_width  = width - (width % 3);
	unsigned max_height = height - (height % 3); 
	unsigned fpitch = pitch / sizeof(FIRGBF);

	/**
	fast tone mapping
	split the image into 3x3 pixel tiles and perform the computation for each group of 9 pixels
	further acceleration is obtained by a Padé approximation of log(x + 1)
	=> produce artifacts and not so faster, so the code has been disabled
	*/
#define PIXEL(x, y)	image[y*fpitch + x].red

	FIRGBF *image = (FIRGBF*)FreeImage_GetBits(dib);
	for(y = 0; y < max_height; y += 3) {
		for(x = 0; x < max_width; x += 3) {
			double average = 0;
			for(i = 0; i < 3; i++) {
				for(j = 0; j < 3; j++) {
					index = (y + i)*fpitch + (x + j);
					image[index].red /= (float)avgLum;
					image[index].red *= exposure; 
					average += image[index].red;
				}
			}
			average = average / 9 - PIXEL(x, y);
			if(average > -1 && average < 1) {
				interpol = log(2 + pow(PIXEL(x + 1, y + 1) / Lmax, biasP) * 8);
				for(i = 0; i < 3; i++) {
					for(j = 0; j < 3; j++) {
						index = (y + i)*fpitch + (x + j);
						L = pade_log(image[index].red);// log(image[index].red + 1)
						image[index].red = (float)((L / interpol) / divider);
					}
				}
			}
			else {
				for(i = 0; i < 3; i++) {
					for(j = 0; j < 3; j++) {
						index = (y + i)*fpitch + (x + j);
						interpol = log(2 + pow(image[index].red / Lmax, biasP) * 8);
						L = pade_log(image[index].red);// log(image[index].red + 1)
						image[index].red = (float)((L / interpol) / divider);
					}
				}
			}
		} //x
	} // y

	/**
	Normal tone mapping of every pixel for the remaining right and bottom bands
	*/
	BYTE *bits;

	// right band
	bits = (BYTE*)FreeImage_GetBits(dib);
	for(y = 0; y < height; y++) {
		FIRGBF *pixel = (FIRGBF*)bits;
		for(x = max_width; x < width; x++) {
			double Yw = pixel[x].red / avgLum;
			Yw *= exposure;
			interpol = log(2 + biasFunction(biasP, Yw / Lmax) * 8);
			L = pade_log(Yw);// log(Yw + 1)
			pixel[x].red = (float)((L / interpol) / divider);
		}
		// next line
		bits += pitch;
	}
	// bottom band
	bits = (BYTE*)FreeImage_GetBits(dib);
	for(y = max_height; y < height; y++) {
		FIRGBF *pixel = (FIRGBF*)bits;
		for(x = 0; x < max_width; x++) {
			double Yw = pixel[x].red / avgLum;
			Yw *= exposure;
			interpol = log(2 + biasFunction(biasP, Yw / Lmax) * 8);
			L = pade_log(Yw);// log(Yw + 1)
			pixel[x].red = (float)((L / interpol) / divider);
		}
		// next line
		bits += pitch;
	}

#endif	// DRAGO03_FAST

	return TRUE;
}

/**
Custom gamma correction based on the ITU-R BT.709 standard
@param dib RGBF image to be corrected
@param gammaval Gamma value (2.2 is a good default value)
@return Returns TRUE if successful, returns FALSE otherwise
*/
static BOOL 
REC709GammaCorrection(FIBITMAP *dib, const float gammaval) {
	if(FreeImage_GetImageType(dib) != FIT_RGBF)
		return FALSE;

	float slope = 4.5F;
	float start = 0.018F;
	
	const float fgamma = (float)((0.45 / gammaval) * 2);
	if(gammaval >= 2.1F) {
		start = (float)(0.018 / ((gammaval - 2) * 7.5));
		slope = (float)(4.5 * ((gammaval - 2) * 7.5));
	} else if (gammaval <= 1.9F) {
		start = (float)(0.018 * ((2 - gammaval) * 7.5));
		slope = (float)(4.5 / ((2 - gammaval) * 7.5));
	}

	const unsigned width  = FreeImage_GetWidth(dib);
	const unsigned height = FreeImage_GetHeight(dib);
	const unsigned pitch  = FreeImage_GetPitch(dib);

	BYTE *bits = (BYTE*)FreeImage_GetBits(dib);
	for(unsigned y = 0; y < height; y++) {
		float *pixel = (float*)bits;
		for(unsigned x = 0; x < width; x++) {
			for(int i = 0; i < 3; i++) {
				*pixel = (*pixel <= start) ? *pixel * slope : (1.099F * pow(*pixel, fgamma) - 0.099F);
				pixel++;
			}
		}
		bits += pitch;
	}

	return TRUE;
}

// ----------------------------------------------------------
//  Main algorithm
// ----------------------------------------------------------

/**
Apply the Adaptive Logarithmic Mapping operator to a HDR image and convert to 24-bit RGB
@param src Input RGB16 or RGB[A]F image
@param gamma Gamma correction (gamma > 0). 1 means no correction, 2.2 in the original paper.
@param exposure Exposure parameter (0 means no correction, 0 in the original paper)
@return Returns a 24-bit RGB image if successful, returns NULL otherwise
*/
FIBITMAP* DLL_CALLCONV 
FreeImage_TmoDrago03(FIBITMAP *src, double gamma, double exposure) {
	float maxLum, minLum, avgLum;

	if (!FreeImage_HasPixels(src)) return NULL;

	// working RGBF variable
	FIBITMAP *dib = NULL;

	dib = FreeImage_ConvertToRGBF(src);
	if (!dib) return NULL;

	// default algorithm parameters
	const float biasParam = 0.85F;
	const float expoParam = (float)pow(2.0, exposure); //default exposure is 1, 2^0

	// convert to Yxy
	ConvertInPlaceRGBFToYxy(dib);
	// get the luminance
	LuminanceFromYxy(dib, &maxLum, &minLum, &avgLum);
	// perform the tone mapping
	ToneMappingDrago03(dib, maxLum, avgLum, biasParam, expoParam);
	// convert back to RGBF
	ConvertInPlaceYxyToRGBF(dib);
	if(gamma != 1) {
		// perform gamma correction
		REC709GammaCorrection(dib, (float)gamma);
	}
	// clamp image highest values to display white, then convert to 24-bit RGB
	FIBITMAP *dst = ClampConvertRGBFTo24(dib);

	// clean-up and return
	FreeImage_Unload(dib);

	// copy metadata from src to dst
	FreeImage_CloneMetadata(dst, src);
	
	return dst;
}