Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/dec_xyb.cc
Line
Count
Source
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/dec_xyb.h"
7
8
#include <algorithm>
9
#include <cmath>
10
#include <cstdint>
11
#include <cstdlib>
12
#include <cstring>
13
#include <iterator>
14
15
#include "lib/jxl/base/data_parallel.h"
16
#include "lib/jxl/image_metadata.h"
17
#include "lib/jxl/image_ops.h"
18
19
#undef HWY_TARGET_INCLUDE
20
#define HWY_TARGET_INCLUDE "lib/jxl/dec_xyb.cc"
21
#include <hwy/foreach_target.h>
22
#include <hwy/highway.h>
23
24
#include "lib/jxl/base/compiler_specific.h"
25
#include "lib/jxl/base/matrix_ops.h"
26
#include "lib/jxl/base/rect.h"
27
#include "lib/jxl/base/sanitizers.h"
28
#include "lib/jxl/base/status.h"
29
#include "lib/jxl/cms/color_encoding_cms.h"
30
#include "lib/jxl/cms/jxl_cms_internal.h"
31
#include "lib/jxl/cms/opsin_params.h"
32
#include "lib/jxl/color_encoding_internal.h"
33
#include "lib/jxl/common.h"
34
#include "lib/jxl/dec_xyb-inl.h"
35
#include "lib/jxl/image.h"
36
#include "lib/jxl/opsin_params.h"
37
#include "lib/jxl/quantizer.h"
38
HWY_BEFORE_NAMESPACE();
39
namespace jxl {
40
namespace HWY_NAMESPACE {
41
42
// These templates are not found via ADL.
43
using hwy::HWY_NAMESPACE::MulAdd;
44
45
// Same, but not in-place.
46
Status OpsinToLinear(const Image3F& opsin, const Rect& rect, ThreadPool* pool,
47
                     Image3F* JXL_RESTRICT linear,
48
0
                     const OpsinParams& opsin_params) {
49
0
  JXL_ENSURE(SameSize(rect, *linear));
50
0
  JXL_CHECK_IMAGE_INITIALIZED(opsin, rect);
51
52
0
  const auto process_row = [&](const uint32_t task,
53
0
                               size_t /*thread*/) -> Status {
54
0
    const size_t y = static_cast<size_t>(task);
55
56
    // Faster than adding via ByteOffset at end of loop.
57
0
    const float* JXL_RESTRICT row_opsin_0 = rect.ConstPlaneRow(opsin, 0, y);
58
0
    const float* JXL_RESTRICT row_opsin_1 = rect.ConstPlaneRow(opsin, 1, y);
59
0
    const float* JXL_RESTRICT row_opsin_2 = rect.ConstPlaneRow(opsin, 2, y);
60
0
    float* JXL_RESTRICT row_linear_0 = linear->PlaneRow(0, y);
61
0
    float* JXL_RESTRICT row_linear_1 = linear->PlaneRow(1, y);
62
0
    float* JXL_RESTRICT row_linear_2 = linear->PlaneRow(2, y);
63
64
0
    const HWY_FULL(float) d;
65
66
0
    for (size_t x = 0; x < rect.xsize(); x += Lanes(d)) {
67
0
      const auto in_opsin_x = Load(d, row_opsin_0 + x);
68
0
      const auto in_opsin_y = Load(d, row_opsin_1 + x);
69
0
      const auto in_opsin_b = Load(d, row_opsin_2 + x);
70
0
      auto linear_r = Undefined(d);
71
0
      auto linear_g = Undefined(d);
72
0
      auto linear_b = Undefined(d);
73
0
      XybToRgb(d, in_opsin_x, in_opsin_y, in_opsin_b, opsin_params, &linear_r,
74
0
               &linear_g, &linear_b);
75
76
0
      Store(linear_r, d, row_linear_0 + x);
77
0
      Store(linear_g, d, row_linear_1 + x);
78
0
      Store(linear_b, d, row_linear_2 + x);
79
0
    }
80
0
    return true;
81
0
  };
Unexecuted instantiation: dec_xyb.cc:jxl::N_SSE4::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: dec_xyb.cc:jxl::N_AVX2::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: dec_xyb.cc:jxl::N_AVX3::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: dec_xyb.cc:jxl::N_AVX3_ZEN4::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: dec_xyb.cc:jxl::N_AVX3_SPR::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
Unexecuted instantiation: dec_xyb.cc:jxl::N_SSE2::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)::$_0::operator()(unsigned int, unsigned long) const
82
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, static_cast<int>(rect.ysize()),
83
0
                                ThreadPool::NoInit, process_row,
84
0
                                "OpsinToLinear(Rect)"));
85
0
  JXL_CHECK_IMAGE_INITIALIZED(*linear, rect);
86
0
  return true;
87
0
}
Unexecuted instantiation: jxl::N_SSE4::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
Unexecuted instantiation: jxl::N_AVX2::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
Unexecuted instantiation: jxl::N_AVX3::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
Unexecuted instantiation: jxl::N_AVX3_ZEN4::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
Unexecuted instantiation: jxl::N_AVX3_SPR::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
Unexecuted instantiation: jxl::N_SSE2::OpsinToLinear(jxl::Image3<float> const&, jxl::RectT<unsigned long> const&, jxl::ThreadPool*, jxl::Image3<float>*, jxl::OpsinParams const&)
88
89
// NOLINTNEXTLINE(google-readability-namespace-comments)
90
}  // namespace HWY_NAMESPACE
91
}  // namespace jxl
92
HWY_AFTER_NAMESPACE();
93
94
#if HWY_ONCE
95
namespace jxl {
96
97
HWY_EXPORT(OpsinToLinear);
98
Status OpsinToLinear(const Image3F& opsin, const Rect& rect, ThreadPool* pool,
99
                     Image3F* JXL_RESTRICT linear,
100
0
                     const OpsinParams& opsin_params) {
101
0
  return HWY_DYNAMIC_DISPATCH(OpsinToLinear)(opsin, rect, pool, linear,
102
0
                                             opsin_params);
103
0
}
104
105
#if !JXL_HIGH_PRECISION
106
HWY_EXPORT(HasFastXYBTosRGB8);
107
bool HasFastXYBTosRGB8() { return HWY_DYNAMIC_DISPATCH(HasFastXYBTosRGB8)(); }
108
109
HWY_EXPORT(FastXYBTosRGB8);
110
Status FastXYBTosRGB8(const float* input[4], uint8_t* output, bool is_rgba,
111
                      size_t xsize) {
112
  return HWY_DYNAMIC_DISPATCH(FastXYBTosRGB8)(input, output, is_rgba, xsize);
113
}
114
#endif  // !JXL_HIGH_PRECISION
115
116
0
void OpsinParams::Init(float intensity_target) {
117
0
  InitSIMDInverseMatrix(GetOpsinAbsorbanceInverseMatrix(), inverse_opsin_matrix,
118
0
                        intensity_target);
119
0
  memcpy(opsin_biases, jxl::cms::kNegOpsinAbsorbanceBiasRGB.data(),
120
0
         sizeof(jxl::cms::kNegOpsinAbsorbanceBiasRGB));
121
0
  memcpy(quant_biases, kDefaultQuantBias, sizeof(kDefaultQuantBias));
122
0
  for (size_t c = 0; c < 4; c++) {
123
0
    opsin_biases_cbrt[c] = cbrtf(opsin_biases[c]);
124
0
  }
125
0
}
126
127
264k
bool CanOutputToColorEncoding(const ColorEncoding& c_desired) {
128
264k
  if (!c_desired.HaveFields()) {
129
90
    return false;
130
90
  }
131
  // TODO(veluca): keep in sync with dec_reconstruct.cc
132
263k
  const auto& tf = c_desired.Tf();
133
263k
  if (!tf.IsPQ() && !tf.IsSRGB() && !tf.have_gamma && !tf.IsLinear() &&
134
3.92k
      !tf.IsHLG() && !tf.IsDCI() && !tf.Is709()) {
135
0
    return false;
136
0
  }
137
263k
  if (c_desired.IsGray() && c_desired.GetWhitePointType() != WhitePoint::kD65) {
138
    // TODO(veluca): figure out what should happen here.
139
533
    return false;
140
533
  }
141
263k
  return true;
142
263k
}
143
144
264k
Status OutputEncodingInfo::SetFromMetadata(const CodecMetadata& metadata) {
145
264k
  orig_color_encoding = metadata.m.color_encoding;
146
264k
  orig_intensity_target = metadata.m.IntensityTarget();
147
264k
  desired_intensity_target = orig_intensity_target;
148
264k
  const auto& im = metadata.transform_data.opsin_inverse_matrix;
149
264k
  orig_inverse_matrix = im.inverse_matrix;
150
264k
  default_transform = im.all_default;
151
264k
  xyb_encoded = metadata.m.xyb_encoded;
152
264k
  std::copy(std::begin(im.opsin_biases), std::end(im.opsin_biases),
153
264k
            opsin_params.opsin_biases);
154
1.05M
  for (int i = 0; i < 3; ++i) {
155
792k
    opsin_params.opsin_biases_cbrt[i] = cbrtf(opsin_params.opsin_biases[i]);
156
792k
  }
157
264k
  opsin_params.opsin_biases_cbrt[3] = opsin_params.opsin_biases[3] = 1;
158
264k
  std::copy(std::begin(im.quant_biases), std::end(im.quant_biases),
159
264k
            opsin_params.quant_biases);
160
264k
  bool orig_ok = CanOutputToColorEncoding(orig_color_encoding);
161
264k
  bool orig_grey = orig_color_encoding.IsGray();
162
264k
  return SetColorEncoding(!xyb_encoded || orig_ok
163
264k
                              ? orig_color_encoding
164
264k
                              : ColorEncoding::LinearSRGB(orig_grey));
165
264k
}
166
167
Status OutputEncodingInfo::MaybeSetColorEncoding(
168
13.4k
    const ColorEncoding& c_desired) {
169
13.4k
  if (c_desired.GetColorSpace() == ColorSpace::kXYB &&
170
0
      ((color_encoding.GetColorSpace() == ColorSpace::kRGB &&
171
0
        color_encoding.GetPrimariesType() != Primaries::kSRGB) ||
172
0
       color_encoding.Tf().IsPQ())) {
173
0
    return false;
174
0
  }
175
13.4k
  if (!xyb_encoded && !CanOutputToColorEncoding(c_desired)) {
176
0
    return false;
177
0
  }
178
13.4k
  return SetColorEncoding(c_desired);
179
13.4k
}
180
181
277k
Status OutputEncodingInfo::SetColorEncoding(const ColorEncoding& c_desired) {
182
277k
  color_encoding = c_desired;
183
277k
  linear_color_encoding = color_encoding;
184
277k
  linear_color_encoding.Tf().SetTransferFunction(TransferFunction::kLinear);
185
277k
  color_encoding_is_original = orig_color_encoding.SameColorEncoding(c_desired);
186
187
  // Compute the opsin inverse matrix and luminances based on primaries and
188
  // white point.
189
277k
  Matrix3x3 inverse_matrix;
190
277k
  bool inverse_matrix_is_default = default_transform;
191
277k
  inverse_matrix = orig_inverse_matrix;
192
277k
  constexpr Vector3 kSRGBLuminances{0.2126, 0.7152, 0.0722};
193
277k
  luminances = kSRGBLuminances;
194
277k
  if ((c_desired.GetPrimariesType() != Primaries::kSRGB ||
195
276k
       c_desired.GetWhitePointType() != WhitePoint::kD65) &&
196
4.90k
      !c_desired.IsGray()) {
197
4.41k
    Matrix3x3 srgb_to_xyzd50;
198
4.41k
    const auto& srgb = ColorEncoding::SRGB(/*is_gray=*/false);
199
4.41k
    PrimariesCIExy p;
200
4.41k
    JXL_RETURN_IF_ERROR(srgb.GetPrimaries(p));
201
4.41k
    CIExy w = srgb.GetWhitePoint();
202
4.41k
    JXL_RETURN_IF_ERROR(PrimariesToXYZD50(p.r.x, p.r.y, p.g.x, p.g.y, p.b.x,
203
4.41k
                                          p.b.y, w.x, w.y, srgb_to_xyzd50));
204
4.41k
    Matrix3x3 original_to_xyz;
205
4.41k
    JXL_RETURN_IF_ERROR(c_desired.GetPrimaries(p));
206
4.41k
    w = c_desired.GetWhitePoint();
207
4.41k
    if (!PrimariesToXYZ(p.r.x, p.r.y, p.g.x, p.g.y, p.b.x, p.b.y, w.x, w.y,
208
4.41k
                        original_to_xyz)) {
209
0
      return JXL_FAILURE("PrimariesToXYZ failed");
210
0
    }
211
4.41k
    luminances = original_to_xyz[1];
212
4.41k
    if (xyb_encoded) {
213
172
      Matrix3x3 adapt_to_d50;
214
172
      if (!AdaptToXYZD50(c_desired.GetWhitePoint().x,
215
172
                         c_desired.GetWhitePoint().y, adapt_to_d50)) {
216
0
        return JXL_FAILURE("AdaptToXYZD50 failed");
217
0
      }
218
172
      Matrix3x3 xyzd50_to_original;
219
172
      Mul3x3Matrix(adapt_to_d50, original_to_xyz, xyzd50_to_original);
220
172
      JXL_RETURN_IF_ERROR(Inv3x3Matrix(xyzd50_to_original));
221
171
      Matrix3x3 srgb_to_original;
222
171
      Mul3x3Matrix(xyzd50_to_original, srgb_to_xyzd50, srgb_to_original);
223
171
      Mul3x3Matrix(srgb_to_original, orig_inverse_matrix, inverse_matrix);
224
171
      inverse_matrix_is_default = false;
225
171
    }
226
4.41k
  }
227
228
277k
  if (c_desired.IsGray()) {
229
1.04k
    Matrix3x3 tmp_inv_matrix = inverse_matrix;
230
1.04k
    Matrix3x3 srgb_to_luma{luminances, luminances, luminances};
231
1.04k
    Mul3x3Matrix(srgb_to_luma, tmp_inv_matrix, inverse_matrix);
232
1.04k
  }
233
234
  // The internal XYB color space uses absolute luminance, so we scale back the
235
  // opsin inverse matrix to relative luminance where 1.0 corresponds to the
236
  // original intensity target.
237
277k
  if (xyb_encoded) {
238
114k
    InitSIMDInverseMatrix(inverse_matrix, opsin_params.inverse_opsin_matrix,
239
114k
                          orig_intensity_target);
240
114k
    all_default_opsin = (std::abs(orig_intensity_target - 255.0) <= 0.1f &&
241
114k
                         inverse_matrix_is_default);
242
114k
  }
243
244
  // Set the inverse gamma based on color space transfer function.
245
277k
  const auto& tf = c_desired.Tf();
246
277k
  inverse_gamma = (tf.have_gamma ? tf.GetGamma()
247
277k
                   : tf.IsDCI()  ? 1.0f / 2.6f
248
269k
                                 : 1.0);
249
277k
  return true;
250
277k
}
251
252
}  // namespace jxl
253
#endif  // HWY_ONCE