source:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp
@
2970
Revision 2970, 1.5 KB checked in by mattausch, 16 years ago (diff) |
---|
Rev | Line | |
---|---|---|
[2965] | 1 | #include "ToneMapper.h" |
2 | #include "FrameBufferObject.h" | |
3 | ||
4 | ||
[2967] | 5 | using namespace std; |
6 | ||
7 | ||
[2965] | 8 | namespace CHCDemoEngine |
9 | { | |
10 | ||
[2966] | 11 | |
12 | ToneMapper::ToneMapper() | |
[2965] | 13 | { |
14 | } | |
15 | ||
16 | ||
[2968] | 17 | float ToneMapper::CalcImageKey(float *pixels, int w, int h) const |
[2965] | 18 | { |
[2967] | 19 | float totalLum = .0f; |
[2966] | 20 | |
[2968] | 21 | int size = w * h * 4; |
[2965] | 22 | |
[2968] | 23 | // get avg log luminance |
24 | for (int i = 0; i < size; i += 4) | |
[2965] | 25 | { |
[2968] | 26 | float r = pixels[i + 0]; |
27 | float g = pixels[i + 1]; | |
28 | float b = pixels[i + 2]; | |
[2965] | 29 | |
[2968] | 30 | float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b; |
[2965] | 31 | |
[2968] | 32 | totalLum += log(pixLum + 1e-6f); |
[2965] | 33 | } |
34 | ||
[2967] | 35 | const float key = exp(totalLum / (float)(w * h)); |
36 | ||
[2966] | 37 | static float previousKey = key; |
[2970] | 38 | const float factor = 0.5f; |
[2965] | 39 | |
[2970] | 40 | float imageKey; |
41 | ||
42 | if (key > 0) | |
43 | imageKey = key * factor + previousKey * (1.0f - factor); | |
44 | else | |
45 | imageKey = previousKey; | |
46 | ||
[2966] | 47 | previousKey = imageKey; |
[2965] | 48 | |
[2970] | 49 | //cout << "key: " << key << endl; |
[2967] | 50 | |
[2966] | 51 | return imageKey; |
[2965] | 52 | } |
53 | ||
[2968] | 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 | ||
[2965] | 92 | } |
Note: See TracBrowser
for help on using the repository browser.