Repository navigation
Expand file tree
/
Copy pathfixed.cc
More file actions
111 lines (102 loc) · 3.92 KB
/
Copy pathfixed.cc
File metadata and controls
111 lines (102 loc) · 3.92 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "ipl/fixed.h"
namespace ipl {
namespace {
using Word = std::int64_t;
Word Limit(Word value, int bits, std::uint64_t& count) {
const Word half = Word{1} << (bits - 1);
const Word clamped = std::clamp(value, -half, half - 1);
count += clamped != value;
return clamped;
}
void ValidateWord(Word value, int bits) {
if (bits < 2 || bits > 24) throw std::invalid_argument("Invalid word width");
const Word half = Word{1} << (bits - 1);
if (value < -half || value >= half)
throw std::invalid_argument("Word out of range");
}
} // namespace
std::int64_t Quantize(double value, int bits, int fraction) {
if (bits < 2 || bits > 24 || fraction < 0 || fraction >= bits ||
!std::isfinite(value)) {
throw std::invalid_argument("Invalid fixed coefficient format or value");
}
const double half = static_cast<double>(Word{1} << (bits - 1));
return static_cast<Word>(
std::clamp(std::round(std::ldexp(value, fraction)), -half, half - 1));
}
std::int64_t RoundScale(Word value, int fraction) {
if (fraction < 0 || fraction > 31)
throw std::invalid_argument("Invalid shift");
const Word divisor = Word{1} << fraction;
const Word remainder = value % divisor;
const Word quotient = value / divisor;
if (fraction == 0 || std::abs(remainder) < divisor / 2) return quotient;
return quotient + (value < 0 ? -1 : 1);
}
double Dequantize(Word code, int fraction) {
if (fraction < 0 || fraction > 31)
throw std::invalid_argument("Invalid shift");
return std::ldexp(static_cast<double>(code), -fraction);
}
Word AddFixed(Word a, Word b, int bits, std::uint64_t& clips) {
ValidateWord(a, bits);
ValidateWord(b, bits);
return Limit(a + b, bits, clips);
}
Word MultiplyFixed(Word a, Word b, int bits, int fraction,
std::uint64_t& clips) {
ValidateWord(a, bits);
ValidateWord(b, bits);
if (fraction < 0 || fraction >= bits)
throw std::invalid_argument("Invalid fraction");
return Limit(RoundScale(a * b, fraction), bits, clips);
}
std::size_t PrepareFixed(const Image& in, const std::vector<double>& kernel,
int bits, int fraction, std::vector<int>& pixels,
std::vector<Word>& weights) {
pixels.resize(in.pixels.size());
weights.clear();
std::size_t clipped = 0;
for (double value : kernel) {
const auto weight = Quantize(value, bits, fraction);
clipped += std::round(std::ldexp(value, fraction)) != weight;
weights.push_back(weight);
}
for (std::size_t i = 0; i < pixels.size(); ++i) {
pixels[i] = static_cast<int>(std::round(Unit(in.pixels[i]) * 255));
}
return clipped;
}
void FixedCorrelate(const Image& in, Image& out, const std::vector<int>& pixels,
const std::vector<Word>& weights, int rows, int cols,
int fraction, int accumulator_bits, Border border,
FixedStats& stats) {
ValidateKernel(rows, cols, weights.size());
if (accumulator_bits < 2 || accumulator_bits > 48) {
throw std::invalid_argument("Accumulator width must be in [2, 48]");
}
stats = {};
for (int y = 0; y < in.rows; ++y) {
for (int x = 0; x < in.cols; ++x) {
for (int c = 0; c < in.channels; ++c) {
Word sum = 0;
for (int ky = 0; ky < rows; ++ky) {
const int sy = BorderIndex(y + ky - rows / 2, in.rows, border);
if (sy < 0) continue;
for (int kx = 0; kx < cols; ++kx) {
const int sx = BorderIndex(x + kx - cols / 2, in.cols, border);
if (sx < 0) continue;
const Word term =
pixels[in.Index(sy, sx, c)] * weights[ky * cols + kx];
sum = Limit(sum + term, accumulator_bits, stats.overflow);
}
}
const Word rounded = RoundScale(sum, fraction);
const Word code = std::clamp(rounded, Word{0}, Word{255});
stats.saturation += rounded != code;
out.At(y, x, c) = static_cast<double>(code) / 255;
}
}
}
}
} // namespace ipl