-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhdrmerge.h
273 lines (213 loc) · 7.1 KB
/
hdrmerge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#if !defined(__HDRMERGE_H)
#define __HDRMERGE_H
#include "platform.h"
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <stdint.h>
#include <memory>
using std::cout;
using std::cerr;
using std::endl;
/// String map for metadata
typedef std::map<std::string, std::string> StringMap;
/// Rgb color type
typedef float float3[3];
/// Abstract reconstruction filter
class ReconstructionFilter {
public:
virtual float getRadius() const = 0;
virtual float eval(float x) const = 0;
};
/// Records a single RAW exposure
struct Exposure {
std::string filename;
float exposure;
float shown_exposure;
uint16_t *image;
inline Exposure(const std::string &filename)
: filename(filename), exposure(-1), image(NULL) { }
inline ~Exposure() {
release();
}
inline void release() {
if (image) {
delete[] image;
image = NULL;
}
}
/// Return the exposure has a human-readable string
std::string toString() const {
char buf[10];
if (exposure < 1)
snprintf(buf, sizeof(buf), "1/%.4g", 1/exposure);
else
snprintf(buf, sizeof(buf), "%.4g", exposure);
return buf;
}
};
/// Stores a series of exposures, manages demosaicing and subsequent steps
struct ExposureSeries {
std::vector<Exposure> exposures;
StringMap metadata;
/* Width and height of the cropped RAW images */
size_t width, height;
/* Black level and whitepoint as determined by RawSpeed */
int blacklevel, whitepoint;
/* Merged high dynamic range image (no demosaicing yet) */
float *image_merged;
/* Merged and demosaiced image */
float3 *image_demosaiced;
/* dcraw-style color filter array description */
int filter;
/* Saturation threshold */
float saturation;
/* Tables for transforming from sensor values to exposures / weights */
float weight_tbl[0xFFFF], value_tbl[0xFFFF];
inline ExposureSeries() :
image_merged(NULL), image_demosaiced(NULL) { }
~ExposureSeries() {
if (image_merged)
delete[] image_merged;
if (image_demosaiced)
delete[] image_demosaiced;
}
/// Return the color at position (x, y)
inline int fc(int x, int y) const {
return (filter >> (((y << 1 & 14) + (x & 1)) << 1) & 3);
}
/**
* Add a file to the exposure series (or, optionally, a sequence
* such as file_%03i.png expressed using the printf-style format)
*/
void add(const std::string &filename);
/**
* Check that all exposures are valid, and that they satisfy
* a few basic requirements such as:
* - all images use the same ISO speed and aperture setting
* - the images were taken using manual focus and manual exposure mode
* - there are no duplicate exposures.
*
* This also sorts the exposures in case they weren't ordered already
*/
void check();
/**
* Run dcraw on an entire exposure series (in parallel)
* and fill the exposure series with a normalized RGB floating
* point image representation
*/
void load();
/// Initialize the exposure / weight table
void initTables(float saturation);
/// Merge all exposures into a single HDR image and release the RAW data
void merge();
/// Estimate the exposure times in case the EXIF tags can't be trusted
void fitExposureTimes();
/// Perform demosaicing
void demosaic(float *sensor2xyz);
/// Transform the image into the right color space
void transform_color(float *sensor2xyz, bool xyz);
/// Scale the image brightness by a given factor
void scale(float factor);
/// Resample the image to a different resolution
void resample(const ReconstructionFilter &filter, size_t w, size_t h);
/// Crop a rectangular region
void crop(int x, int y, int w, int h);
/// Apply white balancing
void whitebalance(float *scale);
/// Apply white balancing based on a grey patch
void whitebalance(int xoffs, int yoffs, int w, int h);
/// Remove vignetting / calibration routine
void vcal();
/// Correct for vignetting using a radial polynomial 1+ax^2+bx^4+cx^6
void vcorr(float a, float b, float c);
/// Return the number of exposures
inline size_t size() const {
return exposures.size();
}
/// Evaluate a pixel in one of the images
float eval(int img, int x, int y) const {
return value_tbl[exposures[img].image[x + y*width]];
}
};
/// Windowed Lanczos filter
class LanczosSincFilter : public ReconstructionFilter {
public:
LanczosSincFilter(float radius = 3) : m_radius(radius) { }
float getRadius() const { return m_radius; }
float eval(float x) const;
private:
float m_radius;
};
/// Tent filter
class TentFilter : public ReconstructionFilter {
public:
TentFilter(float radius = 1) : m_radius(radius) { }
float getRadius() const { return m_radius; }
float eval(float x) const;
private:
float m_radius;
};
/// Return the number of processors available for multithreading
extern int getProcessorCount();
/**
* Write a lossless floating point OpenEXR file using either half or
* single precision (grayscale or RGB)
*/
extern void writeOpenEXR(const std::string &filename, size_t w, size_t h,
int channels, float *data, const StringMap &metadata, bool writeHalf);
void writeJPEG(const std::string &filename, size_t w, size_t h, float *data, int quality = 100);
/// Generate a uniformly distributed random number in [0, 1)
inline float randf() {
#define RS_SCALE (1.0f / (1.0f + RAND_MAX))
float f;
do {
f = (((rand () * RS_SCALE) + rand ()) * RS_SCALE + rand()) * RS_SCALE;
} while (f >= 1); /* Round off */
return f;
}
inline float clamp(float value, float min, float max) {
if (min > max)
std::swap(min, max);
return std::min(std::max(value, min), max);
}
inline float square(float value) {
return value*value;
}
/// check if a file exists
bool fexists(const std::string& name);
enum ERotateFlipType {
ERotateNoneFlipNone = 0,
ERotate180FlipXY = ERotateNoneFlipNone,
ERotate90FlipNone = 1,
ERotate270FlipXY = ERotate90FlipNone,
ERotate180FlipNone = 2,
ERotateNoneFlipXY = ERotate180FlipNone,
ERotate270FlipNone = 3,
ERotate90FlipXY = ERotate270FlipNone,
ERotateNoneFlipX = 4,
ERotate180FlipY = ERotateNoneFlipX,
ERotate90FlipX = 5,
ERotate270FlipY = ERotate90FlipX,
ERotate180FlipX = 6,
ERotateNoneFlipY = ERotate180FlipX,
ERotate270FlipX = 7,
ERotate90FlipY = ERotate270FlipX
};
// Rotate and/or flip an arbitrary image
extern void rotateFlip(
uint8_t *src, size_t s_width, size_t s_height,
uint8_t *&dst, size_t &t_width, size_t &t_height,
int bypp, ERotateFlipType type);
extern ERotateFlipType flipTypeFromString(int rotation, std::string axes);
enum EColorMode {
ENative,
ESRGB,
EXYZ
};
extern std::istream& operator>>(std::istream& in, EColorMode& unit);
#endif /* __HDRMERGE_H */