Coverage Report

Created: 2025-06-22 08:04

/src/libjxl/lib/jxl/enc_photon_noise.cc
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
#include "lib/jxl/enc_photon_noise.h"
7
8
#include <algorithm>
9
10
#include "lib/jxl/cms/opsin_params.h"
11
12
namespace jxl {
13
14
namespace {
15
16
// Assumes a daylight-like spectrum.
17
// https://www.strollswithmydog.com/effective-quantum-efficiency-of-sensor/#:~:text=11%2C260%20photons/um%5E2/lx-s
18
constexpr float kPhotonsPerLxSPerUm2 = 11260;
19
20
// Order of magnitude for cameras in the 2010-2020 decade, taking the CFA into
21
// account.
22
constexpr float kEffectiveQuantumEfficiency = 0.20;
23
24
// TODO(sboukortt): reevaluate whether these are good defaults, notably whether
25
// it would be worth making read noise higher at lower ISO settings.
26
constexpr float kPhotoResponseNonUniformity = 0.005;
27
constexpr float kInputReferredReadNoise = 3;
28
29
// Assumes a 35mm sensor.
30
constexpr float kSensorAreaUm2 = 36000.f * 24000;
31
32
template <typename T>
33
0
inline constexpr T Square(const T x) {
34
0
  return x * x;
35
0
}
36
template <typename T>
37
0
inline constexpr T Cube(const T x) {
38
0
  return x * x * x;
39
0
}
40
41
}  // namespace
42
43
NoiseParams SimulatePhotonNoise(const size_t xsize, const size_t ysize,
44
0
                                const float iso) {
45
0
  const float kOpsinAbsorbanceBiasCbrt =
46
0
      std::cbrt(jxl::cms::kOpsinAbsorbanceBias[1]);
47
48
  // Focal plane exposure for 18% of kDefaultIntensityTarget, in lx·s.
49
  // (ISO = 10 lx·s ÷ H)
50
0
  const float h_18 = 10 / iso;
51
52
0
  const float pixel_area_um2 = kSensorAreaUm2 / (xsize * ysize);
53
54
0
  const float electrons_per_pixel_18 = kEffectiveQuantumEfficiency *
55
0
                                       kPhotonsPerLxSPerUm2 * h_18 *
56
0
                                       pixel_area_um2;
57
58
0
  NoiseParams params;
59
60
0
  for (size_t i = 0; i < NoiseParams::kNumNoisePoints; ++i) {
61
0
    const float scaled_index = i / (NoiseParams::kNumNoisePoints - 2.f);
62
    // scaled_index is used for XYB = (0, 2·scaled_index, 2·scaled_index)
63
0
    const float y = 2 * scaled_index;
64
    // 1 = default intensity target
65
0
    const float linear = std::max(0.f, Cube(y - kOpsinAbsorbanceBiasCbrt) +
66
0
                                           jxl::cms::kOpsinAbsorbanceBias[1]);
67
0
    const float electrons_per_pixel = electrons_per_pixel_18 * (linear / 0.18f);
68
    // Quadrature sum of read noise, photon shot noise (sqrt(S) so simply not
69
    // squared here) and photo response non-uniformity.
70
    // https://doi.org/10.1117/3.725073
71
    // Units are electrons rms.
72
0
    const float noise =
73
0
        std::sqrt(Square(kInputReferredReadNoise) + electrons_per_pixel +
74
0
                  Square(kPhotoResponseNonUniformity * electrons_per_pixel));
75
0
    const float linear_noise = noise * (0.18f / electrons_per_pixel_18);
76
0
    const float opsin_derivative =
77
0
        (1.f / 3) /
78
0
        Square(std::cbrt(linear - jxl::cms::kOpsinAbsorbanceBias[1]));
79
0
    const float opsin_noise = linear_noise * opsin_derivative;
80
81
    // TODO(sboukortt): verify more thoroughly whether the denominator is
82
    // correct.
83
0
    params.lut[i] =
84
0
        Clamp1(opsin_noise /
85
0
                   (0.22f             // norm_const
86
0
                    * std::sqrt(2.f)  // red_noise + green_noise
87
0
                    * 1.13f  // standard deviation of a plane of generated noise
88
0
                    ),
89
0
               0.f, 1.f);
90
0
  }
91
92
0
  return params;
93
0
}
94
95
}  // namespace jxl