Coverage Report

Created: 2026-09-14 07:15

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
  };
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
}
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
7.09k
bool CanOutputToColorEncoding(const ColorEncoding& c_desired) {
128
7.09k
  if (!c_desired.HaveFields()) {
129
5.23k
    return false;
130
5.23k
  }
131
  // TODO(veluca): keep in sync with dec_reconstruct.cc
132
1.86k
  const auto& tf = c_desired.Tf();
133
1.86k
  if (!tf.IsPQ() && !tf.IsSRGB() && !tf.have_gamma && !tf.IsLinear() &&
134
79
      !tf.IsHLG() && !tf.IsDCI() && !tf.Is709()) {
135
0
    return false;
136
0
  }
137
1.86k
  if (c_desired.IsGray() && c_desired.GetWhitePointType() != WhitePoint::kD65) {
138
    // TODO(veluca): figure out what should happen here.
139
11
    return false;
140
11
  }
141
1.85k
  return true;
142
1.86k
}
143
144
7.09k
Status OutputEncodingInfo::SetFromMetadata(const CodecMetadata& metadata) {
145
7.09k
  orig_color_encoding = metadata.m.color_encoding;
146
7.09k
  orig_intensity_target = metadata.m.IntensityTarget();
147
7.09k
  desired_intensity_target = orig_intensity_target;
148
7.09k
  const auto& im = metadata.transform_data.opsin_inverse_matrix;
149
7.09k
  orig_inverse_matrix = im.inverse_matrix;
150
7.09k
  default_transform = im.all_default;
151
7.09k
  xyb_encoded = metadata.m.xyb_encoded;
152
7.09k
  std::copy(std::begin(im.opsin_biases), std::end(im.opsin_biases),
153
7.09k
            opsin_params.opsin_biases);
154
28.3k
  for (int i = 0; i < 3; ++i) {
155
21.2k
    opsin_params.opsin_biases_cbrt[i] = cbrtf(opsin_params.opsin_biases[i]);
156
21.2k
  }
157
7.09k
  opsin_params.opsin_biases_cbrt[3] = opsin_params.opsin_biases[3] = 1;
158
7.09k
  std::copy(std::begin(im.quant_biases), std::end(im.quant_biases),
159
7.09k
            opsin_params.quant_biases);
160
7.09k
  bool orig_ok = CanOutputToColorEncoding(orig_color_encoding);
161
7.09k
  bool orig_grey = orig_color_encoding.IsGray();
162
7.09k
  return SetColorEncoding(!xyb_encoded || orig_ok
163
7.09k
                              ? orig_color_encoding
164
7.09k
                              : ColorEncoding::LinearSRGB(orig_grey));
165
7.09k
}
166
167
Status OutputEncodingInfo::MaybeSetColorEncoding(
168
1.05k
    const ColorEncoding& c_desired) {
169
1.05k
  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
1.05k
  if (!xyb_encoded && !CanOutputToColorEncoding(c_desired)) {
176
0
    return false;
177
0
  }
178
1.05k
  return SetColorEncoding(c_desired);
179
1.05k
}
180
181
8.14k
Status OutputEncodingInfo::SetColorEncoding(const ColorEncoding& c_desired) {
182
8.14k
  color_encoding = c_desired;
183
8.14k
  linear_color_encoding = color_encoding;
184
8.14k
  linear_color_encoding.Tf().SetTransferFunction(TransferFunction::kLinear);
185
8.14k
  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
8.14k
  Matrix3x3 inverse_matrix;
190
8.14k
  bool inverse_matrix_is_default = default_transform;
191
8.14k
  inverse_matrix = orig_inverse_matrix;
192
8.14k
  constexpr Vector3 kSRGBLuminances{0.2126, 0.7152, 0.0722};
193
8.14k
  luminances = kSRGBLuminances;
194
8.14k
  if ((c_desired.GetPrimariesType() != Primaries::kSRGB ||
195
8.10k
       c_desired.GetWhitePointType() != WhitePoint::kD65) &&
196
54
      !c_desired.IsGray()) {
197
43
    Matrix3x3 srgb_to_xyzd50;
198
43
    const auto& srgb = ColorEncoding::SRGB(/*is_gray=*/false);
199
43
    PrimariesCIExy p;
200
43
    JXL_RETURN_IF_ERROR(srgb.GetPrimaries(p));
201
43
    CIExy w = srgb.GetWhitePoint();
202
43
    JXL_RETURN_IF_ERROR(PrimariesToXYZD50(p.r.x, p.r.y, p.g.x, p.g.y, p.b.x,
203
43
                                          p.b.y, w.x, w.y, srgb_to_xyzd50));
204
43
    Matrix3x3 original_to_xyz;
205
43
    JXL_RETURN_IF_ERROR(c_desired.GetPrimaries(p));
206
43
    w = c_desired.GetWhitePoint();
207
43
    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
43
                        original_to_xyz)) {
209
0
      return JXL_FAILURE("PrimariesToXYZ failed");
210
0
    }
211
43
    luminances = original_to_xyz[1];
212
43
    if (xyb_encoded) {
213
38
      Matrix3x3 adapt_to_d50;
214
38
      if (!AdaptToXYZD50(c_desired.GetWhitePoint().x,
215
38
                         c_desired.GetWhitePoint().y, adapt_to_d50)) {
216
0
        return JXL_FAILURE("AdaptToXYZD50 failed");
217
0
      }
218
38
      Matrix3x3 xyzd50_to_original;
219
38
      Mul3x3Matrix(adapt_to_d50, original_to_xyz, xyzd50_to_original);
220
38
      JXL_RETURN_IF_ERROR(Inv3x3Matrix(xyzd50_to_original));
221
38
      Matrix3x3 srgb_to_original;
222
38
      Mul3x3Matrix(xyzd50_to_original, srgb_to_xyzd50, srgb_to_original);
223
38
      Mul3x3Matrix(srgb_to_original, orig_inverse_matrix, inverse_matrix);
224
38
      inverse_matrix_is_default = false;
225
38
    }
226
43
  }
227
228
8.14k
  if (c_desired.IsGray()) {
229
143
    Matrix3x3 tmp_inv_matrix = inverse_matrix;
230
143
    Matrix3x3 srgb_to_luma{luminances, luminances, luminances};
231
143
    Mul3x3Matrix(srgb_to_luma, tmp_inv_matrix, inverse_matrix);
232
143
  }
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
8.14k
  if (xyb_encoded) {
238
2.32k
    InitSIMDInverseMatrix(inverse_matrix, opsin_params.inverse_opsin_matrix,
239
2.32k
                          orig_intensity_target);
240
2.32k
    all_default_opsin = (std::abs(orig_intensity_target - 255.0) <= 0.1f &&
241
2.32k
                         inverse_matrix_is_default);
242
2.32k
  }
243
244
  // Set the inverse gamma based on color space transfer function.
245
8.14k
  const auto& tf = c_desired.Tf();
246
8.14k
  inverse_gamma = (tf.have_gamma ? tf.GetGamma()
247
8.14k
                   : tf.IsDCI()  ? 1.0f / 2.6f
248
8.09k
                                 : 1.0);
249
8.14k
  return true;
250
8.14k
}
251
252
}  // namespace jxl
253
#endif  // HWY_ONCE