Coverage Report

Created: 2025-06-16 07:00

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