noise.h (1491B)
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 <array> 15 #include <cmath> 16 #include <utility> 17 18 #include "lib/jxl/base/compiler_specific.h" 19 20 namespace jxl { 21 22 const float kNoisePrecision = 1 << 10; 23 24 struct NoiseParams { 25 // LUT index is an intensity of pixel / mean intensity of patch 26 static constexpr size_t kNumNoisePoints = 8; 27 using Lut = std::array<float, kNumNoisePoints>; 28 29 Lut lut; 30 31 void Clear() { 32 for (float& i : lut) i = 0.f; 33 } 34 bool HasAny() const { 35 for (float i : lut) { 36 if (std::abs(i) > 1e-3f) return true; 37 } 38 return false; 39 } 40 }; 41 42 static inline std::pair<int, float> IndexAndFrac(float x) { 43 constexpr size_t kScaleNumerator = NoiseParams::kNumNoisePoints - 2; 44 // TODO(user): instead of 1, this should be a proper Y range. 45 constexpr float kScale = kScaleNumerator / 1.0f; 46 float scaled_x = std::max(0.f, x * kScale); 47 float floor_x; 48 float frac_x = std::modf(scaled_x, &floor_x); 49 if (JXL_UNLIKELY(scaled_x >= kScaleNumerator + 1)) { 50 floor_x = kScaleNumerator; 51 frac_x = 1.f; 52 } 53 return std::make_pair(static_cast<int>(floor_x), frac_x); 54 } 55 56 struct NoiseLevel { 57 float noise_level; 58 float intensity; 59 }; 60 61 } // namespace jxl 62 63 #endif // LIB_JXL_NOISE_H_