Coverage Report

Created: 2026-06-14 06:57

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