Repository navigation
Expand file tree
/
Copy pathisp.cc
More file actions
90 lines (85 loc) · 2.95 KB
/
Copy pathisp.cc
File metadata and controls
90 lines (85 loc) · 2.95 KB
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
#include "ipl/isp.h"
namespace ipl {
void ValidatePattern(const std::string& pattern) {
if (pattern != "RGGB" && pattern != "BGGR" && pattern != "GRBG" &&
pattern != "GBRG") {
throw std::invalid_argument("Unknown Bayer pattern");
}
}
int BayerChannel(const std::string& pattern, int y, int x) {
const char color = pattern[(y % 2) * 2 + x % 2];
return color == 'R' ? 0 : color == 'G' ? 1 : 2;
}
void Demosaic(const Image& in, Image& out, const std::string& pattern,
bool nearest) {
ValidatePattern(pattern);
if (in.channels != 1 || in.rows < 2 || in.cols < 2 || out.channels != 3) {
throw std::invalid_argument(
"Demosaic requires a single-channel mosaic at least 2x2");
}
for (int y = 0; y < in.rows; ++y) {
for (int x = 0; x < in.cols; ++x) {
const int known = BayerChannel(pattern, y, x);
out.At(y, x, known) = in.At(y, x);
for (int c = 0; c < 3; ++c) {
if (c == known) continue;
int distance = 3;
int count = 0;
double sum = 0;
for (int sy = std::max(0, y - 1); sy <= std::min(in.rows - 1, y + 1);
++sy) {
for (int sx = std::max(0, x - 1); sx <= std::min(in.cols - 1, x + 1);
++sx) {
if (BayerChannel(pattern, sy, sx) != c) continue;
const int candidate = std::abs(sy - y) + std::abs(sx - x);
if (candidate < distance) {
distance = candidate;
count = 0;
sum = 0;
}
if (candidate == distance && (!nearest || count == 0)) {
++count;
sum += in.At(sy, sx);
}
}
}
out.At(y, x, c) = sum / count;
}
}
}
}
std::array<double, 3> WhiteBalance(const Image& in, Image& out, bool peak) {
if (in.channels != 3)
throw std::invalid_argument("White balance requires RGB");
std::array<double, 3> stats{};
for (std::size_t i = 0; i < in.pixels.size(); ++i) {
const double value = Unit(in.pixels[i]);
double& stat = stats[i % 3];
stat = peak ? std::max(stat, value) : stat + value;
}
if (!peak) {
for (double& stat : stats) stat /= in.pixels.size() / 3;
}
const double target = peak ? 1.0 : (stats[0] + stats[1] + stats[2]) / 3;
std::array<double, 3> gains;
for (int c = 0; c < 3; ++c)
gains[c] = stats[c] > 1e-12 ? target / stats[c] : 1;
for (std::size_t i = 0; i < in.pixels.size(); ++i) {
out.pixels[i] = Unit(in.pixels[i] * gains[i % 3]);
}
return gains;
}
void ColorMatrix(const Image& in, Image& out,
const std::vector<double>& matrix) {
if (in.channels != 3 || matrix.size() != 9) {
throw std::invalid_argument("Color matrix requires RGB and 9 weights");
}
for (std::size_t i = 0; i < in.pixels.size(); i += 3) {
for (int c = 0; c < 3; ++c) {
double sum = 0;
for (int d = 0; d < 3; ++d) sum += matrix[3 * c + d] * in.pixels[i + d];
out.pixels[i + c] = Unit(sum);
}
}
}
} // namespace ipl