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 
ClearNoiseParams28   void Clear() {
29     for (float& i : lut) i = 0.f;
30   }
HasAnyNoiseParams31   bool HasAny() const {
32     for (float i : lut) {
33       if (std::abs(i) > 1e-3f) return true;
34     }
35     return false;
36   }
37 };
38 
IndexAndFrac(float x)39 static inline std::pair<int, float> IndexAndFrac(float x) {
40   constexpr size_t kScaleNumerator = NoiseParams::kNumNoisePoints - 2;
41   // TODO: instead of 1, this should be a proper Y range.
42   constexpr float kScale = kScaleNumerator / 1;
43   float scaled_x = std::max(0.f, x * kScale);
44   float floor_x;
45   float frac_x = std::modf(scaled_x, &floor_x);
46   if (JXL_UNLIKELY(scaled_x >= kScaleNumerator)) {
47     floor_x = kScaleNumerator - 1.f;
48     frac_x = 1.f;
49   }
50   return std::make_pair(static_cast<int>(floor_x), frac_x);
51 }
52 
53 struct NoiseLevel {
54   float noise_level;
55   float intensity;
56 };
57 
58 }  // namespace jxl
59 
60 #endif  // LIB_JXL_NOISE_H_
61