forked from jcelaya/hdrmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageStack.cpp
426 lines (391 loc) · 15 KB
/
ImageStack.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* HDRMerge - HDR exposure merging software.
* Copyright 2012 Javier Celaya
*
* This file is part of HDRMerge.
*
* HDRMerge is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HDRMerge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HDRMerge. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <algorithm>
#include "ImageStack.hpp"
#include "Log.hpp"
#include "BoxBlur.hpp"
#include "RawParameters.hpp"
#ifdef __SSE2__
#include <x86intrin.h>
#endif
using namespace std;
using namespace hdrmerge;
int ImageStack::addImage(Image && i) {
if (images.empty()) {
width = i.getWidth();
height = i.getHeight();
}
images.push_back(std::move(i));
int n = images.size() - 1;
while (n > 0 && images[n] < images[n - 1]) {
std::swap(images[n], images[n - 1]);
--n;
}
return n;
}
void ImageStack::calculateSaturationLevel(const RawParameters & params, bool useCustomWl) {
// Calculate max value of brightest image and assume it is saturated
uint16_t maxPerColor[4] = { 0, 0, 0, 0 };
Image & brightest = images.front();
for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
uint16_t v = brightest(x, y);
if (v > maxPerColor[params.FC(x, y)]) {
maxPerColor[params.FC(x, y)] = v;
}
}
}
satThreshold = params.max == 0 ? maxPerColor[0] : params.max;
for (int c = 0; c < 4; ++c) {
if (maxPerColor[c] < satThreshold) {
satThreshold = maxPerColor[c];
}
}
if(!useCustomWl) // only scale when no custom white level was specified
satThreshold *= 0.99;
else
Log::debug( "Using custom white level ", params.max );
for (auto & i : images) {
i.setSaturationThreshold(satThreshold);
}
}
void ImageStack::align() {
if (images.size() > 1) {
Timer t("Align");
size_t errors[images.size()];
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < images.size(); ++i) {
images[i].preScale();
}
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < images.size() - 1; ++i) {
errors[i] = images[i].alignWith(images[i + 1]);
}
for (size_t i = images.size() - 1; i > 0; --i) {
images[i - 1].displace(images[i].getDeltaX(), images[i].getDeltaY());
Log::debug("Image ", i - 1, " displaced to (", images[i - 1].getDeltaX(),
", ", images[i - 1].getDeltaY(), ") with error ", errors[i - 1]);
}
for (auto & i : images) {
i.releaseAlignData();
}
}
}
void ImageStack::crop() {
int dx = 0, dy = 0;
for (auto & i : images) {
int newDx = max(dx, i.getDeltaX());
int bound = min(dx + width, i.getDeltaX() + i.getWidth());
width = bound > newDx ? bound - newDx : 0;
dx = newDx;
int newDy = max(dy, i.getDeltaY());
bound = min(dy + height, i.getDeltaY() + i.getHeight());
height = bound > newDy ? bound - newDy : 0;
dy = newDy;
}
for (auto & i : images) {
i.displace(-dx, -dy);
}
}
void ImageStack::computeResponseFunctions() {
Timer t("Compute response functions");
for (int i = images.size() - 2; i >= 0; --i) {
images[i].computeResponseFunction(images[i + 1]);
}
}
void ImageStack::generateMask() {
Timer t("Generate mask");
mask.resize(width, height);
if(images.size() == 1) {
// single image, fill in zero values
std::fill_n(&mask[0], width*height, 0);
} else {
// multiple images, no need to prefill mask with zeroes. It will be filled correctly on the fly
#pragma omp parallel for schedule(dynamic)
for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
size_t i = 0;
while (i < images.size() - 1 &&
(!images[i].contains(x, y) ||
images[i].isSaturatedAround(x, y))) ++i;
mask(x, y) = i;
}
}
}
// The mask can be used in compose to get the information about saturated pixels
// but the mask can be modified in gui, so we have to make a copy to represent the original state
origMask = mask;
}
double ImageStack::value(size_t x, size_t y) const {
const Image & img = images[mask(x, y)];
return img.exposureAt(x, y);
}
#ifndef __SSE2__
// From The GIMP: app/paint-funcs/paint-funcs.c:fatten_region
static Array2D<uint8_t> fattenMask(const Array2D<uint8_t> & mask, int radius) {
Timer t("Fatten mask");
size_t width = mask.getWidth(), height = mask.getHeight();
Array2D<uint8_t> result(width, height);
int circArray[2 * radius + 1]; // holds the y coords of the filter's mask
// compute_border(circArray, radius)
for (int i = 0; i < radius * 2 + 1; i++) {
double tmp;
if (i > radius)
tmp = (i - radius) - 0.5;
else if (i < radius)
tmp = (radius - i) - 0.5;
else
tmp = 0.0;
circArray[i] = int(std::sqrt(radius*radius - tmp*tmp));
}
// offset the circ pointer by radius so the range of the array
// is [-radius] to [radius]
int * circ = circArray + radius;
const uint8_t * bufArray[height + 2*radius];
for (int i = 0; i < radius; i++) {
bufArray[i] = &mask[0];
}
for (size_t i = 0; i < height; i++) {
bufArray[i + radius] = &mask[i * width];
}
for (int i = 0; i < radius; i++) {
bufArray[i + height + radius] = &mask[(height - 1) * width];
}
// offset the buf pointer
const uint8_t ** buf = bufArray + radius;
#pragma omp parallel
{
unique_ptr<uint8_t[]> buffer(new uint8_t[width * (radius + 1)]);
unique_ptr<uint8_t *[]> maxArray; // caches the largest values for each column
maxArray.reset(new uint8_t *[width + 2 * radius]);
for (int i = 0; i < radius; i++) {
maxArray[i] = buffer.get();
}
for (size_t i = 0; i < width; i++) {
maxArray[i + radius] = &buffer[(radius + 1) * i];
}
for (int i = 0; i < radius; i++) {
maxArray[i + width + radius] = &buffer[(radius + 1) * (width - 1)];
}
// offset the max pointer
uint8_t ** max = maxArray.get() + radius;
#pragma omp for schedule(dynamic)
for (size_t y = 0; y < height; y++) {
uint8_t rowMax = 0;
for (size_t x = 0; x < width; x++) { // compute max array
max[x][0] = buf[y][x];
for (int i = 1; i <= radius; i++) {
max[x][i] = std::max(std::max(max[x][i - 1], buf[y + i][x]), buf[y - i][x]);
rowMax = std::max(max[x][i], rowMax);
}
}
uint8_t last_max = max[0][circ[-1]];
int last_index = 1;
for (size_t x = 0; x < width; x++) { // render scan line
last_index--;
if (last_index >= 0) {
if (last_max == rowMax) {
result(x, y) = rowMax;
} else {
last_max = 0;
for (int i = radius; i >= 0; i--)
if (last_max < max[x + i][circ[i]]) {
last_max = max[x + i][circ[i]];
last_index = i;
}
result(x, y) = last_max;
}
} else {
last_index = radius;
last_max = max[x + radius][circ[radius]];
for (int i = radius - 1; i >= -radius; i--)
if (last_max < max[x + i][circ[i]]) {
last_max = max[x + i][circ[i]];
last_index = i;
}
result(x, y) = last_max;
}
}
}
}
return result;
}
#else // use faster SSE version, crunch 16 bytes at once
// From The GIMP: app/paint-funcs/paint-funcs.c:fatten_region
// SSE version by Ingo Weyrich
static Array2D<uint8_t> fattenMask(const Array2D<uint8_t> & mask, int radius) {
Timer t("Fatten mask (SSE version)");
size_t width = mask.getWidth(), height = mask.getHeight();
Array2D<uint8_t> result(width, height);
int circArray[2 * radius + 1]; // holds the y coords of the filter's mask
// compute_border(circArray, radius)
for (int i = 0; i < radius * 2 + 1; i++) {
double tmp;
if (i > radius)
tmp = (i - radius) - 0.5;
else if (i < radius)
tmp = (radius - i) - 0.5;
else
tmp = 0.0;
circArray[i] = int(std::sqrt(radius*radius - tmp*tmp));
}
// offset the circ pointer by radius so the range of the array
// is [-radius] to [radius]
int * circ = circArray + radius;
const uint8_t * bufArray[height + 2*radius];
for (int i = 0; i < radius; i++) {
bufArray[i] = &mask[0];
}
for (size_t i = 0; i < height; i++) {
bufArray[i + radius] = &mask[i * width];
}
for (int i = 0; i < radius; i++) {
bufArray[i + height + radius] = &mask[(height - 1) * width];
}
// offset the buf pointer
const uint8_t ** buf = bufArray + radius;
#pragma omp parallel
{
uint8_t buffer[width * (radius + 1)];
uint8_t *maxArray[radius+1];
for (int i = 0; i <= radius; i++) {
maxArray[i] = &buffer[i*width];
}
#pragma omp for schedule(dynamic,16)
for (size_t y = 0; y < height; y++) {
size_t x = 0;
for (; x < width-15; x+=16) { // compute max array, use SSE to process 16 bytes at once
__m128i lmax = _mm_loadu_si128((__m128i*)&buf[y][x]);
if(radius<2) // max[0] is only used when radius < 2
_mm_storeu_si128((__m128i*)&maxArray[0][x],lmax);
for (int i = 1; i <= radius; i++) {
lmax = _mm_max_epu8(_mm_loadu_si128((__m128i*)&buf[y + i][x]),lmax);
lmax = _mm_max_epu8(_mm_loadu_si128((__m128i*)&buf[y - i][x]),lmax);
_mm_storeu_si128((__m128i*)&maxArray[i][x],lmax);
}
}
for (; x < width; x++) { // compute max array, remaining columns
uint8_t lmax = buf[y][x];
if(radius<2) // max[0] is only used when radius < 2
maxArray[0][x] = lmax;
for (int i = 1; i <= radius; i++) {
lmax = std::max(std::max(lmax, buf[y + i][x]), buf[y - i][x]);
maxArray[i][x] = lmax;
}
}
for (x = 0; (int)x < radius; x++) { // render scan line, first columns without SSE
uint8_t last_max = maxArray[circ[radius]][x+radius];
for (int i = radius - 1; i >= -(int)x; i--)
last_max = std::max(last_max,maxArray[circ[i]][x + i]);
result(x, y) = last_max;
}
for (; x < width-15-radius+1; x += 16) { // render scan line, use SSE to process 16 bytes at once
__m128i last_maxv = _mm_loadu_si128((__m128i*)&maxArray[circ[radius]][x+radius]);
for (int i = radius - 1; i >= -radius; i--)
last_maxv = _mm_max_epu8(last_maxv,_mm_loadu_si128((__m128i*)&maxArray[circ[i]][x+i]));
_mm_storeu_si128((__m128i*)&result(x,y),last_maxv);
}
for (; x < width; x++) { // render scan line, last columns without SSE
int maxRadius = std::min(radius,(int)((int)width-1-(int)x));
uint8_t last_max = maxArray[circ[maxRadius]][x+maxRadius];
for (int i = maxRadius-1; i >= -radius; i--)
last_max = std::max(last_max,maxArray[circ[i]][x + i]);
result(x, y) = last_max;
}
}
}
return result;
}
#endif
Array2D<float> ImageStack::compose(const RawParameters & params, int featherRadius) const {
int imageMax = images.size() - 1;
BoxBlur map(fattenMask(mask, featherRadius));
measureTime("Blur", [&] () {
map.blur(featherRadius);
});
Timer t("Compose");
Array2D<float> dst(params.rawWidth, params.rawHeight);
dst.displace(-(int)params.leftMargin, -(int)params.topMargin);
dst.fillBorders(0.f);
float max = 0.0;
double saturatedRange = params.max - satThreshold;
#pragma omp parallel
{
float maxthr = 0.0;
#pragma omp for schedule(dynamic,16) nowait
for (size_t y = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x) {
double v, vv;
double p = map(x,y);
p = p < 0.0 ? 0.0 : p;
int j = p;
if (images[j].contains(x, y)) {
p = p - j;
v = images[j].exposureAt(x, y);
// Adjust false highlights
if (j < origMask(x,y)) { // SaturatedAround
v /= params.whiteMultAt(x, y);
if(p > 0.0001) {
uint16_t rawV = images[j].getMaxAround(x, y);
double k = (rawV - satThreshold) / saturatedRange;
if (k > 1.0)
k = 1.0;
p += (1.0 - p) * k;
}
}
} else {
v = 0.0;
p = 1.0;
}
if (p > 0.0001 && j < imageMax && images[j + 1].contains(x, y)) {
vv = images[j + 1].exposureAt(x, y);
if (j + 1 < origMask(x,y)) { // SaturatedAround
vv /= params.whiteMultAt(x, y);
}
} else {
vv = 0.0;
p = 0.0;
}
v -= p * (v - vv);
dst(x, y) = v;
if (v > maxthr) {
maxthr = v;
}
}
}
#pragma omp critical
if (maxthr > max) {
max = maxthr;
}
}
dst.displace(params.leftMargin, params.topMargin);
// Scale to params.max and recover the black levels
float mult = (params.max - params.maxBlack) / max;
#pragma omp parallel for
for (size_t y = 0; y < params.rawHeight; ++y) {
for (size_t x = 0; x < params.rawWidth; ++x) {
dst(x, y) *= mult;
dst(x, y) += params.blackAt(x - params.leftMargin, y - params.topMargin);
}
}
return dst;
}