Coverage Report

Created: 2025-08-12 07:37

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