1 #ifndef TONEMAPPING_HXX
2 #define TONEMAPPING_HXX
3
4 #include "Image.hxx"
5
6 class ToneMapping
7 {
8 protected:
9 float LdMax;
10
11 public:
12 ToneMapping( float pLdMax )
13 {
14 LdMax = pLdMax;
15 }
16
17 /**
18 * Ward '94
19 * compute scaling factor
20 */
21 float scalingfactor(Image& image)
22 {
23 float Lwa, sum = 0.0;
24
25 int resX = image.getWidth();
26 int resY = image.getHeight();
27
28 /**
29 * get average color of all pixel
30 */
31 for (int i = 0; i < resX; i++)
32 for (int j = 0; j < resY; j++)
33 {
34 Vec4f pixel = image.getPixel(i,j);
35 sum += (pixel.x() + pixel.y() + pixel.z()) / 3.0;
36 }
37 Lwa = sum / (resX * resY);
38
39 /**
40 * compute scaling factor (formula from the lecture)
41 */
42 float sf = (1.0/LdMax) * powf((1.219 + powf(LdMax/2.0, 0.4)) / (1.219 + powf(Lwa, 0.4)), 2.5);
43
44 return sf;
45 };
46
47 /**
48 * Ward '94
49 */
50 void map(Image& image)
51 {
52 float sf = scalingfactor(image);
53
54 /**
55 * and adapt every pixel (new_color = sf * old_color)
56 */
57 int resX = image.getWidth();
58 int resY = image.getHeight();
59
60 for (int i = 0; i < resX; i++)
61 for (int j = 0; j < resY; j++)
62 {
63 Vec4f color = Min(Max(image.getPixel(i,j) * sf,Vec3f(0)),Vec3f(1));
64 image.setPixel( color, i, j );
65 }
66 }
67 };
68
69 #endif
70
syntax highlighted by Code2HTML, v. 0.9.1