PICCANTE  0.4
The hottest HDR imaging library!
reinhard_tmo.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_TONE_MAPPING_REINHARD_TMO_HPP
19 #define PIC_TONE_MAPPING_REINHARD_TMO_HPP
20 
21 #include "../base.hpp"
22 #include "../util/string.hpp"
23 #include "../util/math.hpp"
24 #include "../filtering/filter.hpp"
25 #include "../filtering/filter_bilateral_2ds.hpp"
26 #include "../filtering/filter_luminance.hpp"
27 #include "../filtering/filter_sigmoid_tmo.hpp"
28 #include "../tone_mapping/tone_mapping_operator.hpp"
29 
30 namespace pic {
31 
36 {
37 protected:
38 
45  static float sigmoidParam(float x, std::vector< float > &param)
46  {
47  float x_s = x * param[0];
48 
49  return x_s / (x_s + param[1]);
50  }
51 
58  static float sigmoidInvParam(float y, std::vector< float > &param)
59  {
60  float x_s = y * param[1] / (1.0f - y);
61 
62  return x_s / param[0];
63  }
64 
71  Image *ProcessAux(ImageVec imgIn, Image *imgOut)
72  {
73  //luminance image
74  images[0] = flt_lum.Process(imgIn, images[0]);
75 
76  float LMin, LMax, LogAverage;
77  images[0]->getMaxVal(NULL, &LMax);
78  images[0]->getMinVal(NULL, &LMin);
79  images[0]->getLogMeanVal(NULL, &LogAverage);
80 
81  bool bUpdate = false;
82 
83  if(alpha <= 0.0f) {
84  alpha = estimateAlpha(LMin, LMax, LogAverage);
85  bUpdate = true;
86  }
87 
88  if(whitePoint <= 0.0f) {
89  whitePoint = estimateWhitePoint(LMin, LMax);
90  bUpdate = true;
91  }
92 
93  if(bUpdate) {
94  flt_sigmoid.update(this->sig_mode, this->alpha, this->whitePoint, -1.0f, false);
95  }
96 
97  //filter luminance in the sigmoid-space
98  if(phi > 0.0f) {
99  float s_max = 8.0f;
100  float value = powf(2.0f, phi) * alpha / (s_max * s_max);
101 
102  std::vector<float> param;
103  param.push_back(alpha / LogAverage);
104  param.push_back(value);
105 
106  float pEpsilon = 0.05f; //threshold
107  images[0]->applyFunctionParam(sigmoidParam, param);//applyFunction(&sigmoid);
108 
109 
110  flt_bilateral.update(1.6f, pEpsilon / 2.0f);
111 
113 
114  images[0]->applyFunctionParam(sigmoidInvParam, param);
115  images[1]->applyFunctionParam(sigmoidInvParam, param);
116 
117  images[2] = flt_sigmoid.Process(Double(images[0], images[1]), images[2]);
118  } else {
120  }
121 
122  //remove HDR luminance and replacing it with LDR one
123  *imgOut = *imgIn[0];
124  *imgOut /= *images[0];
125  *imgOut *= *images[2];
126 
127  imgOut->removeSpecials();
128 
129  return imgOut;
130  }
131 
137 
138 public:
139 
147  ReinhardTMO(float alpha = 0.18f, float whitePoint = -1.0f, float phi = 8.0f, SIGMOID_MODE sig_mode = SIG_TMO)
148  {
149  images.push_back(NULL);
150  images.push_back(NULL);
151  images.push_back(NULL);
153  }
154 
156  {
157  release();
158  }
159 
167  static float estimateAlpha(float LMin, float LMax, float logAverage)
168  {
169  float log2f = logf(2.0f);
170  float log2Max = logf(LMax + 1e-9f) / log2f;
171  float log2Min = logf(LMin + 1e-9f) / log2f;
172  float log2Average = logf(logAverage + 1e-9f) / log2f;
173 
174  float tmp = (2.0f * log2Average - log2Min - log2Max) / (log2Max - log2Min);
175 
176  return 0.18f * powf(4.0f, tmp);
177  }
178 
185  static float estimateWhitePoint(float LMin, float LMax)
186  {
187  float log2f = logf(2.0f);
188  float log2Max = logf(LMax + 1e-9f) / log2f;
189  float log2Min = logf(LMin + 1e-9f) / log2f;
190 
191  return 1.5f * powf(2.0f, (log2Max - log2Min - 5.0f));
192  }
193 
201  void update(float alpha = 0.18f, float whitePoint = 1e6f, float phi = 8.0f, SIGMOID_MODE sig_mode = SIG_TMO)
202  {
203  this->alpha = alpha;
204  this->whitePoint = whitePoint;
205  this->phi = phi;
206  this->sig_mode = sig_mode;
207 
208  flt_sigmoid.update(SIG_TMO, this->alpha, this->whitePoint, -1.0f, false);
209  }
210 
217  static Image* executeGlobal1(Image *imgIn, Image *imgOut)
218  {
219  ReinhardTMO rtmo(-1.0f, -1.0f, -8.0f, SIG_TMO);
220  return rtmo.Process(Single(imgIn), imgOut);
221  }
222 
229  static Image* executeGlobal2(Image *imgIn, Image *imgOut)
230  {
231  ReinhardTMO rtmo(-1.0f, -1.0f, -8.0f, SIG_TMO_WP);
232  return rtmo.Process(Single(imgIn), imgOut);
233  }
234 
241  static Image* executeLocal1(Image *imgIn, Image *imgOut)
242  {
243  ReinhardTMO rtmo(-1.0f, -1.0f, 8.0f, SIG_TMO);
244  return rtmo.Process(Single(imgIn), imgOut);
245  }
246 
253  static Image* executeLocal2(Image *imgIn, Image *imgOut)
254  {
255  ReinhardTMO rtmo(-1.0f, -1.0f, 8.0f, SIG_TMO_WP);
256  return rtmo.Process(Single(imgIn), imgOut);
257  }
258 };
259 
260 
261 } // end namespace pic
262 
263 #endif /* PIC_TONE_MAPPING_REINHARD_TMO_HPP */
264 
static float sigmoidParam(float x, std::vector< float > &param)
sigmoidParam
Definition: reinhard_tmo.hpp:45
Definition: filter_sigmoid_tmo.hpp:29
static Image * executeGlobal2(Image *imgIn, Image *imgOut)
executeGlobal2
Definition: reinhard_tmo.hpp:229
float phi
Definition: reinhard_tmo.hpp:133
Image * Process(ImageVec imgIn, Image *imgOut=NULL)
Process.
Definition: tone_mapping_operator.hpp:120
static float estimateAlpha(float LMin, float LMax, float logAverage)
estimateAlpha
Definition: reinhard_tmo.hpp:167
The ReinhardTMO class.
Definition: reinhard_tmo.hpp:35
~ReinhardTMO()
Definition: reinhard_tmo.hpp:155
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
ImageVec images
Definition: tone_mapping_operator.hpp:35
static Image * executeLocal1(Image *imgIn, Image *imgOut)
executeLocal1
Definition: reinhard_tmo.hpp:241
The ToneMappingOperator class.
Definition: tone_mapping_operator.hpp:31
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
The FilterLuminance class.
Definition: filter_luminance.hpp:33
FilterBilateral2DS flt_bilateral
Definition: reinhard_tmo.hpp:135
SIGMOID_MODE sig_mode
Definition: reinhard_tmo.hpp:132
Image * ProcessAux(ImageVec imgIn, Image *imgOut)
ProcessAux.
Definition: reinhard_tmo.hpp:71
void update(SIGMOID_MODE type, float alpha, float wp, float epsilon, bool temporal)
update
Definition: filter_sigmoid_tmo.hpp:148
void update(float alpha=0.18f, float whitePoint=1e6f, float phi=8.0f, SIGMOID_MODE sig_mode=SIG_TMO)
update
Definition: reinhard_tmo.hpp:201
void update(float sigma_s, float sigma_r, int mult, SAMPLER_TYPE type)
update
Definition: filter_bilateral_2ds.hpp:232
The FilterSigmoidTMO class.
Definition: filter_sigmoid_tmo.hpp:34
void release()
release
Definition: tone_mapping_operator.hpp:68
PIC_INLINE ImageVec Double(Image *img1, Image *img2)
Double creates an std::vector which contains img1 and img2; this is for filters input.
Definition: image_vec.hpp:49
ReinhardTMO(float alpha=0.18f, float whitePoint=-1.0f, float phi=8.0f, SIGMOID_MODE sig_mode=SIG_TMO)
ReinhardTMO.
Definition: reinhard_tmo.hpp:147
PIC_INLINE float log2f(float x)
log2f logarithm in base 2 for floating point
Definition: math.hpp:375
The Image class stores an image as buffer of float.
Definition: image.hpp:60
void removeSpecials()
removeSpecials removes NaN and +/-Inf values and sets them to 0.0f.
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
Definition: bilateral_separation.hpp:25
static Image * executeLocal2(Image *imgIn, Image *imgOut)
executeLocal2
Definition: reinhard_tmo.hpp:253
static float sigmoidInvParam(float y, std::vector< float > &param)
sigmoidInvParam
Definition: reinhard_tmo.hpp:58
Definition: filter_sigmoid_tmo.hpp:29
static Image * executeGlobal1(Image *imgIn, Image *imgOut)
executeGlobal1
Definition: reinhard_tmo.hpp:217
FilterLuminance flt_lum
Definition: reinhard_tmo.hpp:136
SIGMOID_MODE
Definition: filter_sigmoid_tmo.hpp:29
float alpha
Definition: reinhard_tmo.hpp:133
FilterSigmoidTMO flt_sigmoid
Definition: reinhard_tmo.hpp:134
The FilterBilateral2DS class.
Definition: filter_bilateral_2ds.hpp:36
static float estimateWhitePoint(float LMin, float LMax)
estimateWhitePoint
Definition: reinhard_tmo.hpp:185
float whitePoint
Definition: reinhard_tmo.hpp:133