PICCANTE  0.4
The hottest HDR imaging library!
dynamic_range.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_UTIL_DYNAMIC_RANGE_HPP
19 #define PIC_UTIL_DYNAMIC_RANGE_HPP
20 
21 #include "../base.hpp"
22 #include "../util/math.hpp"
23 
24 namespace pic {
25 
30 
39 PIC_INLINE float estimateAverageLuminance(float shutter_speed,
40  float aperture_value = 1.0f,
41  float iso_value = 1.0f,
42  float K_value = 12.5f)
43 {
44  K_value = CLAMPi(K_value, 10.6f, 13.4f);
45 
46  return (iso_value * shutter_speed) / (K_value * aperture_value * aperture_value);
47 }
48 
56 PIC_INLINE bool checkNormalized(const float *data, int size, float delta = 1e-6f)
57 {
58  float thr = 1.0f + delta;
59 
60  for(int i = 0; i < size; i++) {
61  if(data[i] > thr) {
62  return false;
63  }
64  }
65 
66  return true;
67 }
68 
78 PIC_INLINE float *convertLDR2HDR(unsigned char *dataIn, float *dataOut,
79  int size, LDR_type type, float gamma = 2.2f)
80 {
81  if(dataIn == NULL) {
82  return NULL;
83  }
84 
85  if(dataOut == NULL) {
86  dataOut = new float[size];
87  }
88 
89  float LUT[256];
90  for(int i = 0; i < 256; i++) {
91  float i_f = float(i);
92 
93  switch(type) {
94  case LT_NONE: {//simple cast
95  LUT[i] = i_f;
96  }
97  break;
98 
99  case LT_NOR: {//normalize in [0,1]
100  LUT[i] = i_f / 255.0f;
101  }
102  break;
103 
104  case LT_NOR_GAMMA: {//normalize in [0,1] + GAMMA correction removal
105  LUT[i] = powf(i_f / 255.0f, gamma);
106  }
107  break;
108 
109  case LT_LDR: {
110  //do nothing
111  }
112  }
113  }
114 
115  #pragma omp parallel for
116  for(int i = 0; i < size; i++) {
117  dataOut[i] = LUT[dataIn[i]];
118  }
119 
120  return dataOut;
121 }
122 
132 PIC_INLINE unsigned char *convertHDR2LDR(const float *dataIn, unsigned char *dataOut,
133  int size, LDR_type type, float gamma = 2.2f)
134 {
135  if(dataIn == NULL) {
136  return NULL;
137  }
138 
139  if(dataOut == NULL) {
140  dataOut = new unsigned char[size];
141  }
142 
143  gamma = gamma > 0.0f ? gamma : 2.2f;
144 
145  float invGamma = 1.0f / gamma;
146 
147  switch(type) {
148 
149  case LT_NONE: {//simple cast
150  #pragma omp parallel for
151  for(int i = 0; i < size; i++) {
152  dataOut[i] = CLAMPi(int(lround(dataIn[i])), 0, 255);
153  }
154  }
155  break;
156 
157  case LT_NOR: {//convert into 8-bit
158  #pragma omp parallel for
159  for(int i = 0; i < size; i++) {
160  dataOut[i] = CLAMPi(int(lround(dataIn[i] * 255.0f)), 0, 255);
161  }
162  }
163  break;
164 
165  case LT_NOR_GAMMA: {//convert into 8-bit + GAMMA correction application
166  #pragma omp parallel for
167  for(int i = 0; i < size; i++) {
168  float tmp = powf(dataIn[i], invGamma);
169  dataOut[i] = CLAMPi(int(lround(tmp * 255.0f)), 0, 255);
170  }
171  }
172  break;
173 
174  case LT_LDR: {
175  //do nothing
176  }
177  }
178 
179  return dataOut;
180 }
181 
182 } // end namespace pic
183 
184 #endif //PIC_UTIL_DYNAMIC_RANGE_HPP
PIC_INLINE long lround(double x)
lround rounds double numbers properly.
Definition: math.hpp:229
LDR_type
The LDR_type enum.
Definition: dynamic_range.hpp:29
PIC_INLINE bool checkNormalized(const float *data, int size, float delta=1e-6f)
checkNormalized checks if data is in [0,1].
Definition: dynamic_range.hpp:56
#define PIC_INLINE
Definition: base.hpp:33
Definition: dynamic_range.hpp:29
Definition: dynamic_range.hpp:29
Definition: dynamic_range.hpp:29
#define CLAMPi(x, a, b)
Definition: math.hpp:81
Definition: dynamic_range.hpp:29
Definition: bilateral_separation.hpp:25
PIC_INLINE float * convertLDR2HDR(unsigned char *dataIn, float *dataOut, int size, LDR_type type, float gamma=2.2f)
convertLDR2HDR converts a buffer of unsigned char into float.
Definition: dynamic_range.hpp:78
PIC_INLINE unsigned char * convertHDR2LDR(const float *dataIn, unsigned char *dataOut, int size, LDR_type type, float gamma=2.2f)
convertHDR2LDR converts a buffer of float into unsigned char.
Definition: dynamic_range.hpp:132
PIC_INLINE float estimateAverageLuminance(float shutter_speed, float aperture_value=1.0f, float iso_value=1.0f, float K_value=12.5f)
estimateAverageLuminance estimates the average luminance of the shot.
Definition: dynamic_range.hpp:39