/src/libjxl/lib/jxl/noise.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef LIB_JXL_NOISE_H_ |
7 | | #define LIB_JXL_NOISE_H_ |
8 | | |
9 | | // Noise parameters shared by encoder/decoder. |
10 | | |
11 | | #include <stddef.h> |
12 | | |
13 | | #include <algorithm> |
14 | | #include <cmath> |
15 | | #include <utility> |
16 | | |
17 | | #include "lib/jxl/base/compiler_specific.h" |
18 | | |
19 | | namespace jxl { |
20 | | |
21 | | const float kNoisePrecision = 1 << 10; |
22 | | |
23 | | struct NoiseParams { |
24 | | // LUT index is an intensity of pixel / mean intensity of patch |
25 | | static constexpr size_t kNumNoisePoints = 8; |
26 | | float lut[kNumNoisePoints]; |
27 | | |
28 | 0 | void Clear() { |
29 | 0 | for (float& i : lut) i = 0.f; |
30 | 0 | } |
31 | 0 | bool HasAny() const { |
32 | 0 | for (float i : lut) { |
33 | 0 | if (std::abs(i) > 1e-3f) return true; |
34 | 0 | } |
35 | 0 | return false; |
36 | 0 | } |
37 | | }; |
38 | | |
39 | 0 | static inline std::pair<int, float> IndexAndFrac(float x) { |
40 | 0 | constexpr size_t kScaleNumerator = NoiseParams::kNumNoisePoints - 2; |
41 | 0 | // TODO: instead of 1, this should be a proper Y range. |
42 | 0 | constexpr float kScale = kScaleNumerator / 1; |
43 | 0 | float scaled_x = std::max(0.f, x * kScale); |
44 | 0 | float floor_x; |
45 | 0 | float frac_x = std::modf(scaled_x, &floor_x); |
46 | 0 | if (JXL_UNLIKELY(scaled_x >= kScaleNumerator + 1)) { |
47 | 0 | floor_x = kScaleNumerator; |
48 | 0 | frac_x = 1.f; |
49 | 0 | } |
50 | 0 | return std::make_pair(static_cast<int>(floor_x), frac_x); |
51 | 0 | } |
52 | | |
53 | | struct NoiseLevel { |
54 | | float noise_level; |
55 | | float intensity; |
56 | | }; |
57 | | |
58 | | } // namespace jxl |
59 | | |
60 | | #endif // LIB_JXL_NOISE_H_ |