Revision 2970,
1.5 KB
checked in by mattausch, 16 years ago
(diff) |
tone mapper working better
|
Line | |
---|
1 | #include "ToneMapper.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace CHCDemoEngine
|
---|
9 | {
|
---|
10 |
|
---|
11 |
|
---|
12 | ToneMapper::ToneMapper()
|
---|
13 | {
|
---|
14 | }
|
---|
15 |
|
---|
16 |
|
---|
17 | float ToneMapper::CalcImageKey(float *pixels, int w, int h) const
|
---|
18 | {
|
---|
19 | float totalLum = .0f;
|
---|
20 |
|
---|
21 | int size = w * h * 4;
|
---|
22 |
|
---|
23 | // get avg log luminance
|
---|
24 | for (int i = 0; i < size; i += 4)
|
---|
25 | {
|
---|
26 | float r = pixels[i + 0];
|
---|
27 | float g = pixels[i + 1];
|
---|
28 | float b = pixels[i + 2];
|
---|
29 |
|
---|
30 | float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
|
---|
31 |
|
---|
32 | totalLum += log(pixLum + 1e-6f);
|
---|
33 | }
|
---|
34 |
|
---|
35 | const float key = exp(totalLum / (float)(w * h));
|
---|
36 |
|
---|
37 | static float previousKey = key;
|
---|
38 | const float factor = 0.5f;
|
---|
39 |
|
---|
40 | float imageKey;
|
---|
41 |
|
---|
42 | if (key > 0)
|
---|
43 | imageKey = key * factor + previousKey * (1.0f - factor);
|
---|
44 | else
|
---|
45 | imageKey = previousKey;
|
---|
46 |
|
---|
47 | previousKey = imageKey;
|
---|
48 |
|
---|
49 | //cout << "key: " << key << endl;
|
---|
50 |
|
---|
51 | return imageKey;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | float ToneMapper::CalcMaxLuminance(float *pixels, int w, int h) const
|
---|
56 | {
|
---|
57 | float maxLum = 1e-6f;
|
---|
58 |
|
---|
59 | int size = w * h * 4;
|
---|
60 |
|
---|
61 | for (int i = 0; i < size; i += 4)
|
---|
62 | {
|
---|
63 | float r = pixels[i + 0];
|
---|
64 | float g = pixels[i + 1];
|
---|
65 | float b = pixels[i + 2];
|
---|
66 |
|
---|
67 | float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b;
|
---|
68 |
|
---|
69 | if (pixLum > maxLum)
|
---|
70 | maxLum = pixLum;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /* for (int i = 0; i < h; ++ i)
|
---|
74 | {
|
---|
75 | for (int j = 0; j < w; ++ j)
|
---|
76 | {
|
---|
77 | const int idx = i * w + j;
|
---|
78 |
|
---|
79 | float r = pixels[4 * idx + 0];
|
---|
80 | float g = pixels[4 * idx + 1];
|
---|
81 | float b = pixels[4 * idx + 2];
|
---|
82 |
|
---|
83 | if (pixLum > maxLum)
|
---|
84 | maxLum = pixLum;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | */
|
---|
88 | return maxLum;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.