|
|
|
//
|
|
|
|
// Tonemapper.hpp
|
|
|
|
// AwesomeAttractorND
|
|
|
|
//
|
|
|
|
// Created by Joshua Moerman on 10/28/11.
|
|
|
|
// Copyright 2011 Vadovas. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef AwesomeAttractorND_Tonemapper_hpp
|
|
|
|
#define AwesomeAttractorND_Tonemapper_hpp
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace Tonemappers {
|
|
|
|
|
|
|
|
struct Normalizer {
|
|
|
|
Normalizer()
|
|
|
|
: max(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
void analyse(C const & canvas){
|
|
|
|
max = *std::max_element(canvas.begin(), canvas.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C, typename I>
|
|
|
|
void process(C const & canvas, I & image){
|
|
|
|
for (typename C::const_iterator it = canvas.begin(); it != canvas.end(); ++it) {
|
|
|
|
const double grayscale = get_value(*it);
|
|
|
|
image << typename I::pixel(grayscale, grayscale, grayscale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
double get_value(unsigned int n){
|
|
|
|
return (double) n / (double) max;
|
|
|
|
}
|
|
|
|
unsigned int max;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GammaCorrector : public Normalizer {
|
|
|
|
template <typename C>
|
|
|
|
void analyse(C const & canvas){
|
|
|
|
Normalizer::analyse(canvas);
|
|
|
|
const unsigned int sum = std::accumulate(canvas.begin(), canvas.end(), 0);
|
|
|
|
const double average = sum / (double) canvas.size();
|
|
|
|
power = -2.5/std::log(average/max);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C, typename I>
|
|
|
|
void process(C const & canvas, I & image){
|
|
|
|
for (typename C::const_iterator it = canvas.begin(); it != canvas.end(); ++it) {
|
|
|
|
const double grayscale = get_value(*it);
|
|
|
|
image << typename I::pixel(grayscale, grayscale, grayscale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
double get_value(unsigned int n){
|
|
|
|
return std::pow(Normalizer::get_value(n), power);
|
|
|
|
}
|
|
|
|
double power;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|