#include "ToneMapper.h" #include "FrameBufferObject.h" using namespace std; namespace CHCDemoEngine { ToneMapper::ToneMapper() { } float ToneMapper::CalcImageKey(float *pixels, int w, int h) const { float totalLum = .0f; int size = w * h * 4; // get avg log luminance for (int i = 0; i < size; i += 4) { float r = pixels[i + 0]; float g = pixels[i + 1]; float b = pixels[i + 2]; float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b; totalLum += log(pixLum + 1e-6f); } const float key = exp(totalLum / (float)(w * h)); static float previousKey = key; const float factor = 0.5f; float imageKey; if (key > 0) imageKey = key * factor + previousKey * (1.0f - factor); else imageKey = previousKey; previousKey = imageKey; //cout << "key: " << key << endl; return imageKey; } float ToneMapper::CalcMaxLuminance(float *pixels, int w, int h) const { float maxLum = 1e-6f; int size = w * h * 4; for (int i = 0; i < size; i += 4) { float r = pixels[i + 0]; float g = pixels[i + 1]; float b = pixels[i + 2]; float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b; if (pixLum > maxLum) maxLum = pixLum; } /* for (int i = 0; i < h; ++ i) { for (int j = 0; j < w; ++ j) { const int idx = i * w + j; float r = pixels[4 * idx + 0]; float g = pixels[4 * idx + 1]; float b = pixels[4 * idx + 2]; if (pixLum > maxLum) maxLum = pixLum; } } */ return maxLum; } }