Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/cms/jxl_cms.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 <jxl/cms.h>
7
8
#ifndef JPEGXL_ENABLE_SKCMS
9
#define JPEGXL_ENABLE_SKCMS 0
10
#endif
11
12
#include <jxl/cms_interface.h>
13
#include <jxl/color_encoding.h>
14
#include <jxl/types.h>
15
16
#include <algorithm>
17
#include <array>
18
#include <cmath>
19
#include <cstddef>
20
#include <cstdint>
21
#include <cstring>
22
#include <memory>
23
#include <utility>
24
#include <vector>
25
26
#undef HWY_TARGET_INCLUDE
27
#define HWY_TARGET_INCLUDE "lib/jxl/cms/jxl_cms.cc"
28
#include <hwy/foreach_target.h>
29
#include <hwy/highway.h>
30
31
#include "lib/jxl/base/common.h"
32
#include "lib/jxl/base/compiler_specific.h"
33
#include "lib/jxl/base/matrix_ops.h"
34
#include "lib/jxl/base/printf_macros.h"
35
#include "lib/jxl/base/status.h"
36
#include "lib/jxl/cms/color_encoding_cms.h"
37
#include "lib/jxl/cms/jxl_cms_internal.h"
38
#include "lib/jxl/cms/transfer_functions-inl.h"
39
#include "lib/jxl/color_encoding_internal.h"
40
#if JPEGXL_ENABLE_SKCMS
41
#include "skcms.h"
42
#else  // JPEGXL_ENABLE_SKCMS
43
#include "lcms2.h"
44
#include "lcms2_plugin.h"
45
#include "lib/jxl/base/span.h"
46
#endif  // JPEGXL_ENABLE_SKCMS
47
48
#define JXL_CMS_VERBOSE 0
49
50
// Define these only once. We can't use HWY_ONCE here because it is defined as
51
// 1 only on the last pass.
52
#ifndef LIB_JXL_JXL_CMS_CC
53
#define LIB_JXL_JXL_CMS_CC
54
55
namespace jxl {
56
namespace {
57
58
using ::jxl::cms::ColorEncoding;
59
60
struct JxlCms {
61
#if JPEGXL_ENABLE_SKCMS
62
  IccBytes icc_src, icc_dst;
63
  skcms_ICCProfile profile_src, profile_dst;
64
#else
65
  void* lcms_transform;
66
#endif
67
68
  // These fields are used when the HLG OOTF or inverse OOTF must be applied.
69
  bool apply_hlg_ootf;
70
  size_t hlg_ootf_num_channels;
71
  // Y component of the primaries.
72
  std::array<float, 3> hlg_ootf_luminances;
73
74
  size_t channels_src;
75
  size_t channels_dst;
76
77
  std::vector<float> src_storage;
78
  std::vector<float*> buf_src;
79
  std::vector<float> dst_storage;
80
  std::vector<float*> buf_dst;
81
82
  float intensity_target;
83
  bool skip_lcms = false;
84
  ExtraTF preprocess = ExtraTF::kNone;
85
  ExtraTF postprocess = ExtraTF::kNone;
86
};
87
88
Status ApplyHlgOotf(JxlCms* t, float* JXL_RESTRICT buf, size_t xsize,
89
                    bool forward);
90
}  // namespace
91
}  // namespace jxl
92
93
#endif  // LIB_JXL_JXL_CMS_CC
94
95
HWY_BEFORE_NAMESPACE();
96
namespace jxl {
97
namespace HWY_NAMESPACE {
98
99
#if JXL_CMS_VERBOSE >= 2
100
const size_t kX = 0;  // pixel index, multiplied by 3 for RGB
101
#endif
102
103
// xform_src = UndoGammaCompression(buf_src).
104
Status BeforeTransform(JxlCms* t, const float* buf_src, float* xform_src,
105
0
                       size_t buf_size) {
106
0
  HWY_FULL(float) df;
107
0
  size_t tail = buf_size % Lanes(df);
108
0
  size_t vec_buf_end = buf_size - tail;
109
0
  switch (t->preprocess) {
110
0
    case ExtraTF::kNone:
111
0
      JXL_ENSURE(false);  // unreachable
112
0
      break;
113
114
0
    case ExtraTF::kPQ: {
115
0
      TF_PQ tf_pq(t->intensity_target);
116
0
      for (size_t i = 0; i < vec_buf_end; i += Lanes(df)) {
117
0
        const auto val = Load(df, buf_src + i);
118
0
        const auto result = tf_pq.DisplayFromEncoded(df, val);
119
0
        Store(result, df, xform_src + i);
120
0
      }
121
0
      if (tail != 0) {
122
0
        const auto val = LoadN(df, buf_src + vec_buf_end, tail);
123
0
        const auto result = tf_pq.DisplayFromEncoded(df, val);
124
0
        StoreN(result, df, xform_src + vec_buf_end, tail);
125
0
      }
126
#if JXL_CMS_VERBOSE >= 2
127
      printf("pre in %.4f %.4f %.4f undoPQ %.4f %.4f %.4f\n", buf_src[3 * kX],
128
             buf_src[3 * kX + 1], buf_src[3 * kX + 2], xform_src[3 * kX],
129
             xform_src[3 * kX + 1], xform_src[3 * kX + 2]);
130
#endif
131
0
      break;
132
0
    }
133
134
0
    case ExtraTF::kHLG:
135
0
      for (size_t i = 0; i < buf_size; ++i) {
136
0
        xform_src[i] = static_cast<float>(
137
0
            TF_HLG_Base::DisplayFromEncoded(static_cast<double>(buf_src[i])));
138
0
      }
139
0
      if (t->apply_hlg_ootf) {
140
0
        JXL_RETURN_IF_ERROR(
141
0
            ApplyHlgOotf(t, xform_src, buf_size, /*forward=*/true));
142
0
      }
143
#if JXL_CMS_VERBOSE >= 2
144
      printf("pre in %.4f %.4f %.4f undoHLG %.4f %.4f %.4f\n", buf_src[3 * kX],
145
             buf_src[3 * kX + 1], buf_src[3 * kX + 2], xform_src[3 * kX],
146
             xform_src[3 * kX + 1], xform_src[3 * kX + 2]);
147
#endif
148
0
      break;
149
150
0
    case ExtraTF::kSRGB:
151
0
      for (size_t i = 0; i < vec_buf_end; i += Lanes(df)) {
152
0
        const auto val = Load(df, buf_src + i);
153
0
        const auto result = TF_SRGB().DisplayFromEncoded(val);
154
0
        Store(result, df, xform_src + i);
155
0
      }
156
0
      if (tail != 0) {
157
0
        const auto val = LoadN(df, buf_src + vec_buf_end, tail);
158
0
        const auto result = TF_SRGB().DisplayFromEncoded(val);
159
0
        StoreN(result, df, xform_src + vec_buf_end, tail);
160
0
      }
161
#if JXL_CMS_VERBOSE >= 2
162
      printf("pre in %.4f %.4f %.4f undoSRGB %.4f %.4f %.4f\n", buf_src[3 * kX],
163
             buf_src[3 * kX + 1], buf_src[3 * kX + 2], xform_src[3 * kX],
164
             xform_src[3 * kX + 1], xform_src[3 * kX + 2]);
165
#endif
166
0
      break;
167
0
  }
168
0
  return true;
169
0
}
Unexecuted instantiation: jxl_cms.cc:jxl::N_SSE4::BeforeTransform(jxl::(anonymous namespace)::JxlCms*, float const*, float*, unsigned long)
Unexecuted instantiation: jxl_cms.cc:jxl::N_AVX2::BeforeTransform(jxl::(anonymous namespace)::JxlCms*, float const*, float*, unsigned long)
Unexecuted instantiation: jxl_cms.cc:jxl::N_SSE2::BeforeTransform(jxl::(anonymous namespace)::JxlCms*, float const*, float*, unsigned long)
170
171
// Applies gamma compression in-place.
172
0
Status AfterTransform(JxlCms* t, float* JXL_RESTRICT buf_dst, size_t buf_size) {
173
0
  HWY_FULL(float) df;
174
0
  size_t tail = buf_size % Lanes(df);
175
0
  size_t vec_buf_end = buf_size - tail;
176
0
  switch (t->postprocess) {
177
0
    case ExtraTF::kNone:
178
0
      JXL_DEBUG_ABORT("Unreachable");
179
0
      break;
180
0
    case ExtraTF::kPQ: {
181
0
      TF_PQ tf_pq(t->intensity_target);
182
0
      for (size_t i = 0; i < vec_buf_end; i += Lanes(df)) {
183
0
        const auto val = Load(df, buf_dst + i);
184
0
        const auto result = tf_pq.EncodedFromDisplay(df, val);
185
0
        Store(result, df, buf_dst + i);
186
0
      }
187
0
      if (tail != 0) {
188
0
        const auto val = LoadN(df, buf_dst + vec_buf_end, tail);
189
0
        const auto result = tf_pq.EncodedFromDisplay(df, val);
190
0
        StoreN(result, df, buf_dst + vec_buf_end, tail);
191
0
      }
192
#if JXL_CMS_VERBOSE >= 2
193
      printf("after PQ enc %.4f %.4f %.4f\n", buf_dst[3 * kX],
194
             buf_dst[3 * kX + 1], buf_dst[3 * kX + 2]);
195
#endif
196
0
      break;
197
0
    }
198
0
    case ExtraTF::kHLG:
199
0
      if (t->apply_hlg_ootf) {
200
0
        JXL_RETURN_IF_ERROR(
201
0
            ApplyHlgOotf(t, buf_dst, buf_size, /*forward=*/false));
202
0
      }
203
0
      for (size_t i = 0; i < buf_size; ++i) {
204
0
        buf_dst[i] = static_cast<float>(
205
0
            TF_HLG_Base::EncodedFromDisplay(static_cast<double>(buf_dst[i])));
206
0
      }
207
#if JXL_CMS_VERBOSE >= 2
208
      printf("after HLG enc %.4f %.4f %.4f\n", buf_dst[3 * kX],
209
             buf_dst[3 * kX + 1], buf_dst[3 * kX + 2]);
210
#endif
211
0
      break;
212
0
    case ExtraTF::kSRGB:
213
0
      for (size_t i = 0; i < vec_buf_end; i += Lanes(df)) {
214
0
        const auto val = Load(df, buf_dst + i);
215
0
        const auto result = TF_SRGB().EncodedFromDisplay(df, val);
216
0
        Store(result, df, buf_dst + i);
217
0
      }
218
0
      if (tail != 0) {
219
0
        const auto val = LoadN(df, buf_dst + vec_buf_end, tail);
220
0
        const auto result = TF_SRGB().EncodedFromDisplay(df, val);
221
0
        StoreN(result, df, buf_dst + vec_buf_end, tail);
222
0
      }
223
#if JXL_CMS_VERBOSE >= 2
224
      printf("after SRGB enc %.4f %.4f %.4f\n", buf_dst[3 * kX],
225
             buf_dst[3 * kX + 1], buf_dst[3 * kX + 2]);
226
#endif
227
0
      break;
228
0
  }
229
0
  return true;
230
0
}
Unexecuted instantiation: jxl_cms.cc:jxl::N_SSE4::AfterTransform(jxl::(anonymous namespace)::JxlCms*, float*, unsigned long)
Unexecuted instantiation: jxl_cms.cc:jxl::N_AVX2::AfterTransform(jxl::(anonymous namespace)::JxlCms*, float*, unsigned long)
Unexecuted instantiation: jxl_cms.cc:jxl::N_SSE2::AfterTransform(jxl::(anonymous namespace)::JxlCms*, float*, unsigned long)
231
232
Status DoColorSpaceTransform(void* cms_data, const size_t thread,
233
                             const float* buf_src, float* buf_dst,
234
0
                             size_t xsize) {
235
  // No lock needed.
236
0
  JxlCms* t = reinterpret_cast<JxlCms*>(cms_data);
237
238
0
  const float* xform_src = buf_src;  // Read-only.
239
0
  if (t->preprocess != ExtraTF::kNone) {
240
0
    float* mutable_xform_src = t->buf_src[thread];  // Writable buffer.
241
0
    JXL_RETURN_IF_ERROR(BeforeTransform(t, buf_src, mutable_xform_src,
242
0
                                        xsize * t->channels_src));
243
0
    xform_src = mutable_xform_src;
244
0
  }
245
246
#if JPEGXL_ENABLE_SKCMS
247
  if (t->channels_src == 1 && !t->skip_lcms) {
248
    // Expand from 1 to 3 channels, starting from the end in case
249
    // xform_src == t->buf_src[thread].
250
    float* mutable_xform_src = t->buf_src[thread];
251
    for (size_t i = 0; i < xsize; ++i) {
252
      const size_t x = xsize - i - 1;
253
      mutable_xform_src[x * 3] = mutable_xform_src[x * 3 + 1] =
254
          mutable_xform_src[x * 3 + 2] = xform_src[x];
255
    }
256
    xform_src = mutable_xform_src;
257
  }
258
#else
259
0
  if (t->channels_src == 4 && !t->skip_lcms) {
260
    // LCMS does CMYK in a weird way: 0 = white, 100 = max ink
261
0
    float* mutable_xform_src = t->buf_src[thread];
262
0
    for (size_t x = 0; x < xsize * 4; ++x) {
263
0
      mutable_xform_src[x] = 100.f - 100.f * mutable_xform_src[x];
264
0
    }
265
0
    xform_src = mutable_xform_src;
266
0
  }
267
0
#endif
268
269
#if JXL_CMS_VERBOSE >= 2
270
  // Save inputs for printing before in-place transforms overwrite them.
271
  const float in0 = xform_src[3 * kX + 0];
272
  const float in1 = xform_src[3 * kX + 1];
273
  const float in2 = xform_src[3 * kX + 2];
274
#endif
275
276
0
  if (t->skip_lcms) {
277
0
    if (buf_dst != xform_src) {
278
0
      memcpy(buf_dst, xform_src, xsize * t->channels_src * sizeof(*buf_dst));
279
0
    }  // else: in-place, no need to copy
280
0
  } else {
281
#if JPEGXL_ENABLE_SKCMS
282
    float* xform_dst = (t->channels_dst == 1) ? t->buf_dst[thread] : buf_dst;
283
    JXL_ENSURE(
284
        skcms_Transform(xform_src,
285
                        (t->channels_src == 4 ? skcms_PixelFormat_RGBA_ffff
286
                                              : skcms_PixelFormat_RGB_fff),
287
                        skcms_AlphaFormat_Opaque, &t->profile_src, xform_dst,
288
                        skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Opaque,
289
                        &t->profile_dst, xsize));
290
    if (t->channels_dst == 1) {
291
      // Contract back from 3 to 1 channel, this time forward.
292
      for (size_t x = 0; x < xsize; ++x) {
293
        buf_dst[x] = xform_dst[x * 3];
294
      }
295
    }
296
#else   // JPEGXL_ENABLE_SKCMS
297
0
    cmsDoTransform(t->lcms_transform, xform_src, buf_dst,
298
0
                   static_cast<cmsUInt32Number>(xsize));
299
0
#endif  // JPEGXL_ENABLE_SKCMS
300
0
  }
301
#if JXL_CMS_VERBOSE >= 2
302
  printf("xform skip%d: %.4f %.4f %.4f (%p) -> (%p) %.4f %.4f %.4f\n",
303
         t->skip_lcms, in0, in1, in2, xform_src, buf_dst, buf_dst[3 * kX],
304
         buf_dst[3 * kX + 1], buf_dst[3 * kX + 2]);
305
#endif
306
307
0
  if (t->postprocess != ExtraTF::kNone) {
308
0
    JXL_RETURN_IF_ERROR(AfterTransform(t, buf_dst, xsize * t->channels_dst));
309
0
  }
310
0
  return true;
311
0
}
Unexecuted instantiation: jxl::N_SSE4::DoColorSpaceTransform(void*, unsigned long, float const*, float*, unsigned long)
Unexecuted instantiation: jxl::N_AVX2::DoColorSpaceTransform(void*, unsigned long, float const*, float*, unsigned long)
Unexecuted instantiation: jxl::N_SSE2::DoColorSpaceTransform(void*, unsigned long, float const*, float*, unsigned long)
312
313
// NOLINTNEXTLINE(google-readability-namespace-comments)
314
}  // namespace HWY_NAMESPACE
315
}  // namespace jxl
316
HWY_AFTER_NAMESPACE();
317
318
#if HWY_ONCE
319
namespace jxl {
320
namespace {
321
322
HWY_EXPORT(DoColorSpaceTransform);
323
int DoColorSpaceTransform(void* t, size_t thread, const float* buf_src,
324
0
                          float* buf_dst, size_t xsize) {
325
0
  return HWY_DYNAMIC_DISPATCH(DoColorSpaceTransform)(t, thread, buf_src,
326
0
                                                     buf_dst, xsize);
327
0
}
328
329
// Define to 1 on OS X as a workaround for older LCMS lacking MD5.
330
#define JXL_CMS_OLD_VERSION 0
331
332
#if JPEGXL_ENABLE_SKCMS
333
334
JXL_MUST_USE_RESULT CIExy CIExyFromXYZ(const Color& XYZ) {
335
  const double factor = 1.0 / static_cast<double>(XYZ[0] + XYZ[1] + XYZ[2]);
336
  CIExy xy;
337
  xy.x = XYZ[0] * factor;
338
  xy.y = XYZ[1] * factor;
339
  return xy;
340
}
341
342
#else  // JPEGXL_ENABLE_SKCMS
343
// (LCMS interface requires xyY but we omit the Y for white points/primaries.)
344
345
0
JXL_MUST_USE_RESULT CIExy CIExyFromxyY(const cmsCIExyY& xyY) {
346
0
  CIExy xy;
347
0
  xy.x = xyY.x;
348
0
  xy.y = xyY.y;
349
0
  return xy;
350
0
}
351
352
0
JXL_MUST_USE_RESULT CIExy CIExyFromXYZ(const cmsCIEXYZ& XYZ) {
353
0
  cmsCIExyY xyY;
354
0
  cmsXYZ2xyY(/*Dest=*/&xyY, /*Source=*/&XYZ);
355
0
  return CIExyFromxyY(xyY);
356
0
}
357
358
0
JXL_MUST_USE_RESULT cmsCIEXYZ D50_XYZ() {
359
  // Quantized D50 as stored in ICC profiles.
360
0
  return {0.96420288, 1.0, 0.82490540};
361
0
}
362
363
// RAII
364
365
struct ContextDeleter {
366
0
  void operator()(void* p) { cmsDeleteContext(static_cast<cmsContext>(p)); }
367
};
368
using Context = std::unique_ptr<void, ContextDeleter>;
369
370
struct ProfileDeleter {
371
0
  void operator()(void* p) { cmsCloseProfile(p); }
372
};
373
using Profile = std::unique_ptr<void, ProfileDeleter>;
374
375
struct TransformDeleter {
376
0
  void operator()(void* p) { cmsDeleteTransform(p); }
377
};
378
using Transform = std::unique_ptr<void, TransformDeleter>;
379
380
struct CurveDeleter {
381
0
  void operator()(cmsToneCurve* p) { cmsFreeToneCurve(p); }
382
};
383
using Curve = std::unique_ptr<cmsToneCurve, CurveDeleter>;
384
385
Status CreateProfileXYZ(const cmsContext context,
386
0
                        Profile* JXL_RESTRICT profile) {
387
0
  profile->reset(cmsCreateXYZProfileTHR(context));
388
0
  if (profile->get() == nullptr) return JXL_FAILURE("Failed to create XYZ");
389
0
  return true;
390
0
}
391
392
#endif  // !JPEGXL_ENABLE_SKCMS
393
394
#if JPEGXL_ENABLE_SKCMS
395
// IMPORTANT: icc must outlive profile.
396
Status DecodeProfile(const uint8_t* icc, size_t size,
397
                     skcms_ICCProfile* const profile) {
398
  if (!skcms_Parse(icc, size, profile)) {
399
    return JXL_FAILURE("Failed to parse ICC profile with %" PRIuS " bytes",
400
                       size);
401
  }
402
  return true;
403
}
404
#else   // JPEGXL_ENABLE_SKCMS
405
Status DecodeProfile(const cmsContext context, Span<const uint8_t> icc,
406
0
                     Profile* profile) {
407
0
  profile->reset(cmsOpenProfileFromMemTHR(context, icc.data(), icc.size()));
408
0
  if (profile->get() == nullptr) {
409
0
    return JXL_FAILURE("Failed to decode profile");
410
0
  }
411
412
  // WARNING: due to the LCMS MD5 issue mentioned above, many existing
413
  // profiles have incorrect MD5, so do not even bother checking them nor
414
  // generating warning clutter.
415
416
0
  return true;
417
0
}
418
#endif  // JPEGXL_ENABLE_SKCMS
419
420
#if JPEGXL_ENABLE_SKCMS
421
422
ColorSpace ColorSpaceFromProfile(const skcms_ICCProfile& profile) {
423
  switch (profile.data_color_space) {
424
    case skcms_Signature_RGB:
425
    case skcms_Signature_CMYK:
426
      // spec says CMYK is encoded as RGB (the kBlack extra channel signals that
427
      // it is actually CMYK)
428
      return ColorSpace::kRGB;
429
    case skcms_Signature_Gray:
430
      return ColorSpace::kGray;
431
    default:
432
      return ColorSpace::kUnknown;
433
  }
434
}
435
436
// vector_out := matmul(matrix, vector_in)
437
void MatrixProduct(const skcms_Matrix3x3& matrix, const Color& vector_in,
438
                   Color& vector_out) {
439
  for (int i = 0; i < 3; ++i) {
440
    vector_out[i] = 0;
441
    for (int j = 0; j < 3; ++j) {
442
      vector_out[i] += matrix.vals[i][j] * vector_in[j];
443
    }
444
  }
445
}
446
447
// Returns white point that was specified when creating the profile.
448
JXL_MUST_USE_RESULT Status UnadaptedWhitePoint(const skcms_ICCProfile& profile,
449
                                               CIExy* out) {
450
  Color media_white_point_XYZ;
451
  if (!skcms_GetWTPT(&profile, media_white_point_XYZ.data())) {
452
    return JXL_FAILURE("ICC profile does not contain WhitePoint tag");
453
  }
454
  skcms_Matrix3x3 CHAD;
455
  if (!skcms_GetCHAD(&profile, &CHAD)) {
456
    // If there is no chromatic adaptation matrix, it means that the white point
457
    // is already unadapted.
458
    *out = CIExyFromXYZ(media_white_point_XYZ);
459
    return true;
460
  }
461
  // Otherwise, it has been adapted to the PCS white point using said matrix,
462
  // and the adaptation needs to be undone.
463
  skcms_Matrix3x3 inverse_CHAD;
464
  if (!skcms_Matrix3x3_invert(&CHAD, &inverse_CHAD)) {
465
    return JXL_FAILURE("Non-invertible ChromaticAdaptation matrix");
466
  }
467
  Color unadapted_white_point_XYZ;
468
  MatrixProduct(inverse_CHAD, media_white_point_XYZ, unadapted_white_point_XYZ);
469
  *out = CIExyFromXYZ(unadapted_white_point_XYZ);
470
  return true;
471
}
472
473
Status IdentifyPrimaries(const skcms_ICCProfile& profile,
474
                         const CIExy& wp_unadapted, ColorEncoding* c) {
475
  if (!c->HasPrimaries()) return true;
476
477
  skcms_Matrix3x3 CHAD;
478
  skcms_Matrix3x3 inverse_CHAD;
479
  if (skcms_GetCHAD(&profile, &CHAD)) {
480
    JXL_RETURN_IF_ERROR(skcms_Matrix3x3_invert(&CHAD, &inverse_CHAD));
481
  } else {
482
    static constexpr skcms_Matrix3x3 kLMSFromXYZ = {
483
        {{0.8951, 0.2664, -0.1614},
484
         {-0.7502, 1.7135, 0.0367},
485
         {0.0389, -0.0685, 1.0296}}};
486
    static constexpr skcms_Matrix3x3 kXYZFromLMS = {
487
        {{0.9869929, -0.1470543, 0.1599627},
488
         {0.4323053, 0.5183603, 0.0492912},
489
         {-0.0085287, 0.0400428, 0.9684867}}};
490
    static constexpr Color kWpD50XYZ{0.96420288, 1.0, 0.82490540};
491
    Color wp_unadapted_XYZ;
492
    JXL_RETURN_IF_ERROR(
493
        CIEXYZFromWhiteCIExy(wp_unadapted.x, wp_unadapted.y, wp_unadapted_XYZ));
494
    Color wp_D50_LMS;
495
    Color wp_unadapted_LMS;
496
    MatrixProduct(kLMSFromXYZ, kWpD50XYZ, wp_D50_LMS);
497
    MatrixProduct(kLMSFromXYZ, wp_unadapted_XYZ, wp_unadapted_LMS);
498
    inverse_CHAD = {{{wp_unadapted_LMS[0] / wp_D50_LMS[0], 0, 0},
499
                     {0, wp_unadapted_LMS[1] / wp_D50_LMS[1], 0},
500
                     {0, 0, wp_unadapted_LMS[2] / wp_D50_LMS[2]}}};
501
    inverse_CHAD = skcms_Matrix3x3_concat(&kXYZFromLMS, &inverse_CHAD);
502
    inverse_CHAD = skcms_Matrix3x3_concat(&inverse_CHAD, &kLMSFromXYZ);
503
  }
504
505
  Color XYZ;
506
  PrimariesCIExy primaries;
507
  CIExy* const chromaticities[] = {&primaries.r, &primaries.g, &primaries.b};
508
  for (int i = 0; i < 3; ++i) {
509
    float RGB[3] = {};
510
    RGB[i] = 1;
511
    skcms_Transform(RGB, skcms_PixelFormat_RGB_fff, skcms_AlphaFormat_Opaque,
512
                    &profile, XYZ.data(), skcms_PixelFormat_RGB_fff,
513
                    skcms_AlphaFormat_Opaque, skcms_XYZD50_profile(), 1);
514
    Color unadapted_XYZ;
515
    MatrixProduct(inverse_CHAD, XYZ, unadapted_XYZ);
516
    *chromaticities[i] = CIExyFromXYZ(unadapted_XYZ);
517
  }
518
  return c->SetPrimaries(primaries);
519
}
520
521
bool IsApproximatelyEqual(const skcms_ICCProfile& profile,
522
                          const ColorEncoding& JXL_RESTRICT c) {
523
  IccBytes bytes;
524
  if (!MaybeCreateProfile(c.ToExternal(), &bytes)) {
525
    return false;
526
  }
527
528
  skcms_ICCProfile profile_test;
529
  if (!DecodeProfile(bytes.data(), bytes.size(), &profile_test)) {
530
    return false;
531
  }
532
533
  if (!skcms_ApproximatelyEqualProfiles(&profile_test, &profile)) {
534
    return false;
535
  }
536
537
  return true;
538
}
539
540
Status DetectTransferFunction(const skcms_ICCProfile& profile,
541
                              ColorEncoding* JXL_RESTRICT c) {
542
  JXL_ENSURE(c->color_space != ColorSpace::kXYB);
543
544
  float gamma[3] = {};
545
  if (profile.has_trc) {
546
    const auto IsGamma = [](const skcms_TransferFunction& tf) {
547
      return tf.a == 1 && tf.b == 0 &&
548
             /* if b and d are zero, it is fine for c not to be */ tf.d == 0 &&
549
             tf.e == 0 && tf.f == 0;
550
    };
551
    for (int i = 0; i < 3; ++i) {
552
      if (profile.trc[i].table_entries == 0 &&
553
          IsGamma(profile.trc->parametric)) {
554
        gamma[i] = 1.f / profile.trc->parametric.g;
555
      } else {
556
        skcms_TransferFunction approximate_tf;
557
        float max_error;
558
        if (skcms_ApproximateCurve(&profile.trc[i], &approximate_tf,
559
                                   &max_error)) {
560
          if (IsGamma(approximate_tf)) {
561
            gamma[i] = 1.f / approximate_tf.g;
562
          }
563
        }
564
      }
565
    }
566
  }
567
  if (gamma[0] != 0 && std::abs(gamma[0] - gamma[1]) < 1e-4f &&
568
      std::abs(gamma[1] - gamma[2]) < 1e-4f) {
569
    if (c->tf.SetGamma(gamma[0])) {
570
      if (IsApproximatelyEqual(profile, *c)) return true;
571
    }
572
  }
573
574
  for (TransferFunction tf : Values<TransferFunction>()) {
575
    // Can only create profile from known transfer function.
576
    if (tf == TransferFunction::kUnknown) continue;
577
    c->tf.SetTransferFunction(tf);
578
    if (IsApproximatelyEqual(profile, *c)) return true;
579
  }
580
581
  c->tf.SetTransferFunction(TransferFunction::kUnknown);
582
  return true;
583
}
584
585
#else  // JPEGXL_ENABLE_SKCMS
586
587
0
uint32_t Type32(const ColorEncoding& c, bool cmyk) {
588
0
  if (cmyk) return TYPE_CMYK_FLT;
589
0
  if (c.color_space == ColorSpace::kGray) return TYPE_GRAY_FLT;
590
0
  return TYPE_RGB_FLT;
591
0
}
592
593
0
uint32_t Type64(const ColorEncoding& c) {
594
0
  if (c.color_space == ColorSpace::kGray) return TYPE_GRAY_DBL;
595
0
  return TYPE_RGB_DBL;
596
0
}
597
598
0
ColorSpace ColorSpaceFromProfile(const Profile& profile) {
599
0
  switch (cmsGetColorSpace(profile.get())) {
600
0
    case cmsSigRgbData:
601
0
    case cmsSigCmykData:
602
0
      return ColorSpace::kRGB;
603
0
    case cmsSigGrayData:
604
0
      return ColorSpace::kGray;
605
0
    default:
606
0
      return ColorSpace::kUnknown;
607
0
  }
608
0
}
609
610
// "profile1" is pre-decoded to save time in DetectTransferFunction.
611
Status ProfileEquivalentToICC(const cmsContext context, const Profile& profile1,
612
0
                              const IccBytes& icc, const ColorEncoding& c) {
613
0
  const uint32_t type_src = Type64(c);
614
615
0
  Profile profile2;
616
0
  JXL_RETURN_IF_ERROR(DecodeProfile(context, Bytes(icc), &profile2));
617
618
0
  Profile profile_xyz;
619
0
  JXL_RETURN_IF_ERROR(CreateProfileXYZ(context, &profile_xyz));
620
621
0
  const uint32_t intent = INTENT_RELATIVE_COLORIMETRIC;
622
0
  const uint32_t flags = cmsFLAGS_NOOPTIMIZE | cmsFLAGS_BLACKPOINTCOMPENSATION |
623
0
                         cmsFLAGS_HIGHRESPRECALC;
624
0
  Transform xform1(cmsCreateTransformTHR(context, profile1.get(), type_src,
625
0
                                         profile_xyz.get(), TYPE_XYZ_DBL,
626
0
                                         intent, flags));
627
0
  Transform xform2(cmsCreateTransformTHR(context, profile2.get(), type_src,
628
0
                                         profile_xyz.get(), TYPE_XYZ_DBL,
629
0
                                         intent, flags));
630
0
  if (xform1 == nullptr || xform2 == nullptr) {
631
0
    return JXL_FAILURE("Failed to create transform");
632
0
  }
633
634
0
  double in[3];
635
0
  double out1[3];
636
0
  double out2[3];
637
638
  // Uniformly spaced samples from very dark to almost fully bright.
639
0
  const double init = 1E-3;
640
0
  const double step = 0.2;
641
642
0
  if (c.color_space == ColorSpace::kGray) {
643
    // Finer sampling and replicate each component.
644
0
    for (in[0] = init; in[0] < 1.0; in[0] += step / 8) {
645
0
      cmsDoTransform(xform1.get(), in, out1, 1);
646
0
      cmsDoTransform(xform2.get(), in, out2, 1);
647
0
      if (!cms::ApproxEq(out1[0], out2[0], 2E-4)) {
648
0
        return false;
649
0
      }
650
0
    }
651
0
  } else {
652
0
    for (in[0] = init; in[0] < 1.0; in[0] += step) {
653
0
      for (in[1] = init; in[1] < 1.0; in[1] += step) {
654
0
        for (in[2] = init; in[2] < 1.0; in[2] += step) {
655
0
          cmsDoTransform(xform1.get(), in, out1, 1);
656
0
          cmsDoTransform(xform2.get(), in, out2, 1);
657
0
          for (size_t i = 0; i < 3; ++i) {
658
0
            if (!cms::ApproxEq(out1[i], out2[i], 2E-4)) {
659
0
              return false;
660
0
            }
661
0
          }
662
0
        }
663
0
      }
664
0
    }
665
0
  }
666
667
0
  return true;
668
0
}
669
670
// Returns white point that was specified when creating the profile.
671
// NOTE: we can't just use cmsSigMediaWhitePointTag because its interpretation
672
// differs between ICC versions.
673
JXL_MUST_USE_RESULT cmsCIEXYZ UnadaptedWhitePoint(const cmsContext context,
674
                                                  const Profile& profile,
675
0
                                                  const ColorEncoding& c) {
676
0
  const cmsCIEXYZ* white_point = static_cast<const cmsCIEXYZ*>(
677
0
      cmsReadTag(profile.get(), cmsSigMediaWhitePointTag));
678
0
  if (white_point != nullptr &&
679
0
      cmsReadTag(profile.get(), cmsSigChromaticAdaptationTag) == nullptr) {
680
    // No chromatic adaptation matrix: the white point is already unadapted.
681
0
    return *white_point;
682
0
  }
683
684
0
  cmsCIEXYZ XYZ = {1.0, 1.0, 1.0};
685
0
  Profile profile_xyz;
686
0
  if (!CreateProfileXYZ(context, &profile_xyz)) return XYZ;
687
  // Array arguments are one per profile.
688
0
  cmsHPROFILE profiles[2] = {profile.get(), profile_xyz.get()};
689
  // Leave white point unchanged - that is what we're trying to extract.
690
0
  cmsUInt32Number intents[2] = {INTENT_ABSOLUTE_COLORIMETRIC,
691
0
                                INTENT_ABSOLUTE_COLORIMETRIC};
692
0
  cmsBool black_compensation[2] = {0, 0};
693
0
  cmsFloat64Number adaption[2] = {0.0, 0.0};
694
  // Only transforming a single pixel, so skip expensive optimizations.
695
0
  cmsUInt32Number flags = cmsFLAGS_NOOPTIMIZE | cmsFLAGS_HIGHRESPRECALC;
696
0
  Transform xform(cmsCreateExtendedTransform(
697
0
      context, 2, profiles, black_compensation, intents, adaption, nullptr, 0,
698
0
      Type64(c), TYPE_XYZ_DBL, flags));
699
0
  if (!xform) return XYZ;  // TODO(lode): return error
700
701
  // xy are relative, so magnitude does not matter if we ignore output Y.
702
0
  const cmsFloat64Number in[3] = {1.0, 1.0, 1.0};
703
0
  cmsDoTransform(xform.get(), in, &XYZ.X, 1);
704
0
  return XYZ;
705
0
}
706
707
Status IdentifyPrimaries(const cmsContext context, const Profile& profile,
708
0
                         const cmsCIEXYZ& wp_unadapted, ColorEncoding* c) {
709
0
  if (!c->HasPrimaries()) return true;
710
0
  if (ColorSpaceFromProfile(profile) == ColorSpace::kUnknown) return true;
711
712
  // These were adapted to the profile illuminant before storing in the profile.
713
0
  const cmsCIEXYZ* adapted_r = static_cast<const cmsCIEXYZ*>(
714
0
      cmsReadTag(profile.get(), cmsSigRedColorantTag));
715
0
  const cmsCIEXYZ* adapted_g = static_cast<const cmsCIEXYZ*>(
716
0
      cmsReadTag(profile.get(), cmsSigGreenColorantTag));
717
0
  const cmsCIEXYZ* adapted_b = static_cast<const cmsCIEXYZ*>(
718
0
      cmsReadTag(profile.get(), cmsSigBlueColorantTag));
719
720
0
  cmsCIEXYZ converted_rgb[3];
721
0
  if (adapted_r == nullptr || adapted_g == nullptr || adapted_b == nullptr) {
722
    // No colorant tag, determine the XYZ coordinates of the primaries by
723
    // converting from the colorspace.
724
0
    Profile profile_xyz;
725
0
    if (!CreateProfileXYZ(context, &profile_xyz)) {
726
0
      return JXL_FAILURE("Failed to retrieve colorants");
727
0
    }
728
    // Array arguments are one per profile.
729
0
    cmsHPROFILE profiles[2] = {profile.get(), profile_xyz.get()};
730
0
    cmsUInt32Number intents[2] = {INTENT_RELATIVE_COLORIMETRIC,
731
0
                                  INTENT_RELATIVE_COLORIMETRIC};
732
0
    cmsBool black_compensation[2] = {0, 0};
733
0
    cmsFloat64Number adaption[2] = {0.0, 0.0};
734
    // Only transforming three pixels, so skip expensive optimizations.
735
0
    cmsUInt32Number flags = cmsFLAGS_NOOPTIMIZE | cmsFLAGS_HIGHRESPRECALC;
736
0
    Transform xform(cmsCreateExtendedTransform(
737
0
        context, 2, profiles, black_compensation, intents, adaption, nullptr, 0,
738
0
        Type64(*c), TYPE_XYZ_DBL, flags));
739
0
    if (!xform) return JXL_FAILURE("Failed to retrieve colorants");
740
741
0
    const cmsFloat64Number in[9] = {1.0, 0.0, 0.0, 0.0, 1.0,
742
0
                                    0.0, 0.0, 0.0, 1.0};
743
0
    cmsDoTransform(xform.get(), in, &converted_rgb->X, 3);
744
0
    adapted_r = &converted_rgb[0];
745
0
    adapted_g = &converted_rgb[1];
746
0
    adapted_b = &converted_rgb[2];
747
0
  }
748
749
  // TODO(janwas): no longer assume Bradford and D50.
750
  // Undo the chromatic adaptation.
751
0
  const cmsCIEXYZ d50 = D50_XYZ();
752
753
0
  cmsCIEXYZ r, g, b;
754
0
  cmsAdaptToIlluminant(&r, &d50, &wp_unadapted, adapted_r);
755
0
  cmsAdaptToIlluminant(&g, &d50, &wp_unadapted, adapted_g);
756
0
  cmsAdaptToIlluminant(&b, &d50, &wp_unadapted, adapted_b);
757
758
0
  const PrimariesCIExy rgb = {CIExyFromXYZ(r), CIExyFromXYZ(g),
759
0
                              CIExyFromXYZ(b)};
760
0
  return c->SetPrimaries(rgb);
761
0
}
762
763
Status DetectTransferFunction(const cmsContext context, const Profile& profile,
764
0
                              ColorEncoding* JXL_RESTRICT c) {
765
0
  JXL_ENSURE(c->color_space != ColorSpace::kXYB);
766
767
0
  float gamma = 0;
768
0
  if (const auto* gray_trc = reinterpret_cast<const cmsToneCurve*>(
769
0
          cmsReadTag(profile.get(), cmsSigGrayTRCTag))) {
770
0
    const double estimated_gamma =
771
0
        cmsEstimateGamma(gray_trc, /*precision=*/1e-4);
772
0
    if (estimated_gamma > 0) {
773
0
      gamma = 1. / estimated_gamma;
774
0
    }
775
0
  } else {
776
0
    float rgb_gamma[3] = {};
777
0
    int i = 0;
778
0
    for (const auto tag :
779
0
         {cmsSigRedTRCTag, cmsSigGreenTRCTag, cmsSigBlueTRCTag}) {
780
0
      if (const auto* trc = reinterpret_cast<const cmsToneCurve*>(
781
0
              cmsReadTag(profile.get(), tag))) {
782
0
        const double estimated_gamma =
783
0
            cmsEstimateGamma(trc, /*precision=*/1e-4);
784
0
        if (estimated_gamma > 0) {
785
0
          rgb_gamma[i] = 1. / estimated_gamma;
786
0
        }
787
0
      }
788
0
      ++i;
789
0
    }
790
0
    if (rgb_gamma[0] != 0 && std::abs(rgb_gamma[0] - rgb_gamma[1]) < 1e-4f &&
791
0
        std::abs(rgb_gamma[1] - rgb_gamma[2]) < 1e-4f) {
792
0
      gamma = rgb_gamma[0];
793
0
    }
794
0
  }
795
796
0
  if (gamma != 0 && c->tf.SetGamma(gamma)) {
797
0
    IccBytes icc_test;
798
0
    if (MaybeCreateProfile(c->ToExternal(), &icc_test) &&
799
0
        ProfileEquivalentToICC(context, profile, icc_test, *c)) {
800
0
      return true;
801
0
    }
802
0
  }
803
804
0
  for (TransferFunction tf : Values<TransferFunction>()) {
805
    // Can only create profile from known transfer function.
806
0
    if (tf == TransferFunction::kUnknown) continue;
807
808
0
    c->tf.SetTransferFunction(tf);
809
810
0
    IccBytes icc_test;
811
0
    if (MaybeCreateProfile(c->ToExternal(), &icc_test) &&
812
0
        ProfileEquivalentToICC(context, profile, icc_test, *c)) {
813
0
      return true;
814
0
    }
815
0
  }
816
817
0
  c->tf.SetTransferFunction(TransferFunction::kUnknown);
818
0
  return true;
819
0
}
820
821
0
void ErrorHandler(cmsContext context, cmsUInt32Number code, const char* text) {
822
0
  JXL_WARNING("LCMS error %u: %s", code, text);
823
0
}
824
825
// Returns a context for the current thread, creating it if necessary.
826
0
cmsContext GetContext() {
827
0
  static thread_local Context context_;
828
0
  if (context_ == nullptr) {
829
0
    context_.reset(cmsCreateContext(nullptr, nullptr));
830
0
    JXL_DASSERT(context_ != nullptr);
831
832
0
    cmsSetLogErrorHandlerTHR(static_cast<cmsContext>(context_.get()), &ErrorHandler);
833
0
  }
834
0
  return static_cast<cmsContext>(context_.get());
835
0
}
836
837
#endif  // JPEGXL_ENABLE_SKCMS
838
839
Status GetPrimariesLuminances(const ColorEncoding& encoding,
840
0
                              float luminances[3]) {
841
  // Explanation:
842
  // We know that the three primaries must sum to white:
843
  //
844
  // [Xr, Xg, Xb;     [1;     [Xw;
845
  //  Yr, Yg, Yb;  ×   1;  =   Yw;
846
  //  Zr, Zg, Zb]      1]      Zw]
847
  //
848
  // By noting that X = x·(X+Y+Z), Y = y·(X+Y+Z) and Z = z·(X+Y+Z) (note the
849
  // lower case indicating chromaticity), and factoring the totals (X+Y+Z) out
850
  // of the left matrix and into the all-ones vector, we get:
851
  //
852
  // [xr, xg, xb;     [Xr + Yr + Zr;     [Xw;
853
  //  yr, yg, yb;  ×   Xg + Yg + Zg;  =   Yw;
854
  //  zr, zg, zb]      Xb + Yb + Zb]      Zw]
855
  //
856
  // Which makes it apparent that we can compute those totals as:
857
  //
858
  //                  [Xr + Yr + Zr;     inv([xr, xg, xb;      [Xw;
859
  //                   Xg + Yg + Zg;  =       yr, yg, yb;   ×   Yw;
860
  //                   Xb + Yb + Zb]          zr, zg, zb])      Zw]
861
  //
862
  // From there, by multiplying each total by its corresponding y, we get Y for
863
  // that primary.
864
865
0
  Color white_XYZ;
866
0
  CIExy wp = encoding.GetWhitePoint();
867
0
  JXL_RETURN_IF_ERROR(CIEXYZFromWhiteCIExy(wp.x, wp.y, white_XYZ));
868
869
0
  PrimariesCIExy primaries;
870
0
  JXL_RETURN_IF_ERROR(encoding.GetPrimaries(primaries));
871
0
  Matrix3x3d chromaticities{
872
0
      {{primaries.r.x, primaries.g.x, primaries.b.x},
873
0
       {primaries.r.y, primaries.g.y, primaries.b.y},
874
0
       {1 - primaries.r.x - primaries.r.y, 1 - primaries.g.x - primaries.g.y,
875
0
        1 - primaries.b.x - primaries.b.y}}};
876
0
  JXL_RETURN_IF_ERROR(Inv3x3Matrix(chromaticities));
877
0
  const double ys[3] = {primaries.r.y, primaries.g.y, primaries.b.y};
878
0
  for (size_t i = 0; i < 3; ++i) {
879
0
    luminances[i] = ys[i] * (chromaticities[i][0] * white_XYZ[0] +
880
0
                             chromaticities[i][1] * white_XYZ[1] +
881
0
                             chromaticities[i][2] * white_XYZ[2]);
882
0
  }
883
0
  return true;
884
0
}
885
886
Status ApplyHlgOotf(JxlCms* t, float* JXL_RESTRICT buf, size_t xsize,
887
0
                    bool forward) {
888
0
  if (295 <= t->intensity_target && t->intensity_target <= 305) {
889
    // The gamma is approximately 1 so this can essentially be skipped.
890
0
    return true;
891
0
  }
892
0
  float gamma = 1.2f * std::pow(1.111f, std::log2(t->intensity_target * 1e-3f));
893
0
  if (!forward) gamma = 1.f / gamma;
894
895
0
  switch (t->hlg_ootf_num_channels) {
896
0
    case 1:
897
0
      for (size_t x = 0; x < xsize; ++x) {
898
0
        buf[x] = std::pow(buf[x], gamma);
899
0
      }
900
0
      break;
901
902
0
    case 3:
903
0
      for (size_t x = 0; x < xsize; x += 3) {
904
0
        const float luminance = buf[x] * t->hlg_ootf_luminances[0] +
905
0
                                buf[x + 1] * t->hlg_ootf_luminances[1] +
906
0
                                buf[x + 2] * t->hlg_ootf_luminances[2];
907
0
        const float ratio = std::pow(luminance, gamma - 1);
908
0
        if (std::isfinite(ratio)) {
909
0
          buf[x] *= ratio;
910
0
          buf[x + 1] *= ratio;
911
0
          buf[x + 2] *= ratio;
912
0
          if (forward && gamma < 1) {
913
            // If gamma < 1, the ratio above will be > 1 which can push bright
914
            // saturated highlights out of gamut. There are several possible
915
            // ways to bring them back in-gamut; this one preserves hue and
916
            // saturation at the slight expense of luminance. If !forward, the
917
            // previously-applied forward OOTF with gamma > 1 already pushed
918
            // those highlights down and we are simply putting them back where
919
            // they were so this is not necessary.
920
0
            const float maximum =
921
0
                std::max(buf[x], std::max(buf[x + 1], buf[x + 2]));
922
0
            if (maximum > 1) {
923
0
              const float normalizer = 1.f / maximum;
924
0
              buf[x] *= normalizer;
925
0
              buf[x + 1] *= normalizer;
926
0
              buf[x + 2] *= normalizer;
927
0
            }
928
0
          }
929
0
        }
930
0
      }
931
0
      break;
932
933
0
    default:
934
0
      return JXL_FAILURE("HLG OOTF not implemented for %" PRIuS " channels",
935
0
                         t->hlg_ootf_num_channels);
936
0
  }
937
0
  return true;
938
0
}
939
940
0
bool IsKnownTransferFunction(jxl::cms::TransferFunction tf) {
941
0
  using TF = jxl::cms::TransferFunction;
942
  // All but kUnknown
943
0
  return tf == TF::k709 || tf == TF::kLinear || tf == TF::kSRGB ||
944
0
         tf == TF::kPQ || tf == TF::kDCI || tf == TF::kHLG;
945
0
}
946
947
constexpr uint8_t kColorPrimariesP3_D65 = 12;
948
949
0
bool IsKnownColorPrimaries(uint8_t color_primaries) {
950
0
  using P = jxl::cms::Primaries;
951
  // All but kCustom
952
0
  if (color_primaries == kColorPrimariesP3_D65) return true;
953
0
  const auto p = static_cast<Primaries>(color_primaries);
954
0
  return p == P::kSRGB || p == P::k2100 || p == P::kP3;
955
0
}
956
957
bool ApplyCICP(const uint8_t color_primaries,
958
               const uint8_t transfer_characteristics,
959
               const uint8_t matrix_coefficients, const uint8_t full_range,
960
0
               ColorEncoding* JXL_RESTRICT c) {
961
0
  if (matrix_coefficients != 0) return false;
962
0
  if (full_range != 1) return false;
963
964
0
  const auto primaries = static_cast<Primaries>(color_primaries);
965
0
  const auto tf = static_cast<TransferFunction>(transfer_characteristics);
966
0
  if (!IsKnownTransferFunction(tf)) return false;
967
0
  if (!IsKnownColorPrimaries(color_primaries)) return false;
968
0
  c->color_space = ColorSpace::kRGB;
969
0
  c->tf.SetTransferFunction(tf);
970
0
  if (primaries == Primaries::kP3) {
971
0
    c->white_point = WhitePoint::kDCI;
972
0
    c->primaries = Primaries::kP3;
973
0
  } else if (color_primaries == kColorPrimariesP3_D65) {
974
0
    c->white_point = WhitePoint::kD65;
975
0
    c->primaries = Primaries::kP3;
976
0
  } else {
977
0
    c->white_point = WhitePoint::kD65;
978
0
    c->primaries = primaries;
979
0
  }
980
0
  return true;
981
0
}
982
983
JXL_BOOL JxlCmsSetFieldsFromICC(void* user_data, const uint8_t* icc_data,
984
                                size_t icc_size, JxlColorEncoding* c,
985
0
                                JXL_BOOL* cmyk) {
986
0
  if (c == nullptr) return JXL_FALSE;
987
0
  if (cmyk == nullptr) return JXL_FALSE;
988
989
0
  *cmyk = JXL_FALSE;
990
991
  // In case parsing fails, mark the ColorEncoding as invalid.
992
0
  c->color_space = JXL_COLOR_SPACE_UNKNOWN;
993
0
  c->transfer_function = JXL_TRANSFER_FUNCTION_UNKNOWN;
994
995
0
  if (icc_size == 0) return JXL_FAILURE("Empty ICC profile");
996
997
0
  ColorEncoding c_enc;
998
999
#if JPEGXL_ENABLE_SKCMS
1000
  if (icc_size < 128) {
1001
    return JXL_FAILURE("ICC file too small");
1002
  }
1003
1004
  skcms_ICCProfile profile;
1005
  JXL_RETURN_IF_ERROR(skcms_Parse(icc_data, icc_size, &profile));
1006
1007
  // skcms does not return the rendering intent, so get it from the file. It
1008
  // should be encoded as big-endian 32-bit integer in bytes 60..63.
1009
  uint32_t big_endian_rendering_intent = icc_data[67] + (icc_data[66] << 8) +
1010
                                         (icc_data[65] << 16) +
1011
                                         (icc_data[64] << 24);
1012
  // Some files encode rendering intent as little endian, which is not spec
1013
  // compliant. However we accept those with a warning.
1014
  uint32_t little_endian_rendering_intent = (icc_data[67] << 24) +
1015
                                            (icc_data[66] << 16) +
1016
                                            (icc_data[65] << 8) + icc_data[64];
1017
  uint32_t candidate_rendering_intent =
1018
      std::min(big_endian_rendering_intent, little_endian_rendering_intent);
1019
  if (candidate_rendering_intent != big_endian_rendering_intent) {
1020
    JXL_WARNING(
1021
        "Invalid rendering intent bytes: [0x%02X 0x%02X 0x%02X 0x%02X], "
1022
        "assuming %u was meant",
1023
        icc_data[64], icc_data[65], icc_data[66], icc_data[67],
1024
        candidate_rendering_intent);
1025
  }
1026
  if (candidate_rendering_intent > 3) {
1027
    return JXL_FAILURE("Invalid rendering intent %u\n",
1028
                       candidate_rendering_intent);
1029
  }
1030
  // ICC and RenderingIntent have the same values (0..3).
1031
  c_enc.rendering_intent =
1032
      static_cast<RenderingIntent>(candidate_rendering_intent);
1033
1034
  if (profile.has_CICP &&
1035
      ApplyCICP(profile.CICP.color_primaries,
1036
                profile.CICP.transfer_characteristics,
1037
                profile.CICP.matrix_coefficients,
1038
                profile.CICP.video_full_range_flag, &c_enc)) {
1039
    *c = c_enc.ToExternal();
1040
    return JXL_TRUE;
1041
  }
1042
1043
  c_enc.color_space = ColorSpaceFromProfile(profile);
1044
  *cmyk = TO_JXL_BOOL(profile.data_color_space == skcms_Signature_CMYK);
1045
1046
  CIExy wp_unadapted;
1047
  JXL_RETURN_IF_ERROR(UnadaptedWhitePoint(profile, &wp_unadapted));
1048
  JXL_RETURN_IF_ERROR(c_enc.SetWhitePoint(wp_unadapted));
1049
1050
  // Relies on color_space.
1051
  JXL_RETURN_IF_ERROR(IdentifyPrimaries(profile, wp_unadapted, &c_enc));
1052
1053
  // Relies on color_space/white point/primaries being set already.
1054
  JXL_RETURN_IF_ERROR(DetectTransferFunction(profile, &c_enc));
1055
#else  // JPEGXL_ENABLE_SKCMS
1056
1057
0
  const cmsContext context = GetContext();
1058
1059
0
  Profile profile;
1060
0
  JXL_RETURN_IF_ERROR(
1061
0
      DecodeProfile(context, Bytes(icc_data, icc_size), &profile));
1062
1063
0
  const cmsUInt32Number rendering_intent32 =
1064
0
      cmsGetHeaderRenderingIntent(profile.get());
1065
0
  if (rendering_intent32 > 3) {
1066
0
    return JXL_FAILURE("Invalid rendering intent %u\n", rendering_intent32);
1067
0
  }
1068
  // ICC and RenderingIntent have the same values (0..3).
1069
0
  c_enc.rendering_intent = static_cast<RenderingIntent>(rendering_intent32);
1070
1071
0
  static constexpr size_t kCICPSize = 12;
1072
0
  static constexpr auto kCICPSignature =
1073
0
      static_cast<cmsTagSignature>(0x63696370);
1074
0
  uint8_t cicp_buffer[kCICPSize];
1075
0
  if (cmsReadRawTag(profile.get(), kCICPSignature, cicp_buffer, kCICPSize) ==
1076
0
          kCICPSize &&
1077
0
      ApplyCICP(cicp_buffer[8], cicp_buffer[9], cicp_buffer[10],
1078
0
                cicp_buffer[11], &c_enc)) {
1079
0
    *c = c_enc.ToExternal();
1080
0
    return JXL_TRUE;
1081
0
  }
1082
1083
0
  c_enc.color_space = ColorSpaceFromProfile(profile);
1084
0
  if (cmsGetColorSpace(profile.get()) == cmsSigCmykData) {
1085
0
    *cmyk = JXL_TRUE;
1086
0
    *c = c_enc.ToExternal();
1087
0
    return JXL_TRUE;
1088
0
  }
1089
1090
0
  const cmsCIEXYZ wp_unadapted = UnadaptedWhitePoint(context, profile, c_enc);
1091
0
  JXL_RETURN_IF_ERROR(c_enc.SetWhitePoint(CIExyFromXYZ(wp_unadapted)));
1092
1093
  // Relies on color_space.
1094
0
  JXL_RETURN_IF_ERROR(
1095
0
      IdentifyPrimaries(context, profile, wp_unadapted, &c_enc));
1096
1097
  // Relies on color_space/white point/primaries being set already.
1098
0
  JXL_RETURN_IF_ERROR(DetectTransferFunction(context, profile, &c_enc));
1099
1100
0
#endif  // JPEGXL_ENABLE_SKCMS
1101
1102
0
  *c = c_enc.ToExternal();
1103
0
  return JXL_TRUE;
1104
0
}
1105
1106
}  // namespace
1107
1108
namespace {
1109
1110
0
void JxlCmsDestroy(void* cms_data) {
1111
0
  if (cms_data == nullptr) return;
1112
0
  JxlCms* t = reinterpret_cast<JxlCms*>(cms_data);
1113
0
#if !JPEGXL_ENABLE_SKCMS
1114
0
  TransformDeleter()(t->lcms_transform);
1115
0
#endif
1116
0
  delete t;
1117
0
}
1118
1119
void AllocateBuffer(size_t length, size_t num_threads,
1120
0
                    std::vector<float>* storage, std::vector<float*>* view) {
1121
0
  constexpr size_t kAlign = 128 / sizeof(float);
1122
0
  size_t stride = RoundUpTo(length, kAlign);
1123
0
  storage->resize(stride * num_threads + kAlign);
1124
0
  intptr_t addr = reinterpret_cast<intptr_t>(storage->data());
1125
0
  size_t offset =
1126
0
      (RoundUpTo(addr, kAlign * sizeof(float)) - addr) / sizeof(float);
1127
0
  view->clear();
1128
0
  view->reserve(num_threads);
1129
0
  for (size_t i = 0; i < num_threads; ++i) {
1130
0
    view->emplace_back(storage->data() + offset + i * stride);
1131
0
  }
1132
0
}
1133
1134
void* JxlCmsInit(void* init_data, size_t num_threads, size_t xsize,
1135
                 const JxlColorProfile* input, const JxlColorProfile* output,
1136
0
                 float intensity_target) {
1137
0
  if (init_data == nullptr) {
1138
0
    JXL_NOTIFY_ERROR("JxlCmsInit: init_data is nullptr");
1139
0
    return nullptr;
1140
0
  }
1141
0
  const auto* cms = static_cast<const JxlCmsInterface*>(init_data);
1142
0
  auto t = jxl::make_unique<JxlCms>();
1143
0
  IccBytes icc_src;
1144
0
  IccBytes icc_dst;
1145
0
  if (input->icc.size == 0) {
1146
0
    JXL_NOTIFY_ERROR("JxlCmsInit: empty input ICC");
1147
0
    return nullptr;
1148
0
  }
1149
0
  if (output->icc.size == 0) {
1150
0
    JXL_NOTIFY_ERROR("JxlCmsInit: empty OUTPUT ICC");
1151
0
    return nullptr;
1152
0
  }
1153
0
  icc_src.assign(input->icc.data, input->icc.data + input->icc.size);
1154
0
  ColorEncoding c_src;
1155
0
  if (!c_src.SetFieldsFromICC(std::move(icc_src), *cms)) {
1156
0
    JXL_NOTIFY_ERROR("JxlCmsInit: failed to parse input ICC");
1157
0
    return nullptr;
1158
0
  }
1159
0
  icc_dst.assign(output->icc.data, output->icc.data + output->icc.size);
1160
0
  ColorEncoding c_dst;
1161
0
  if (!c_dst.SetFieldsFromICC(std::move(icc_dst), *cms)) {
1162
0
    JXL_NOTIFY_ERROR("JxlCmsInit: failed to parse output ICC");
1163
0
    return nullptr;
1164
0
  }
1165
#if JXL_CMS_VERBOSE
1166
  printf("%s -> %s\n", Description(c_src).c_str(), Description(c_dst).c_str());
1167
#endif
1168
1169
#if JPEGXL_ENABLE_SKCMS
1170
  if (!DecodeProfile(input->icc.data, input->icc.size, &t->profile_src)) {
1171
    JXL_NOTIFY_ERROR("JxlCmsInit: skcms failed to parse input ICC");
1172
    return nullptr;
1173
  }
1174
  if (!DecodeProfile(output->icc.data, output->icc.size, &t->profile_dst)) {
1175
    JXL_NOTIFY_ERROR("JxlCmsInit: skcms failed to parse output ICC");
1176
    return nullptr;
1177
  }
1178
#else   // JPEGXL_ENABLE_SKCMS
1179
0
  const cmsContext context = GetContext();
1180
0
  Profile profile_src, profile_dst;
1181
0
  if (!DecodeProfile(context, Bytes(c_src.icc), &profile_src)) {
1182
0
    JXL_NOTIFY_ERROR("JxlCmsInit: lcms failed to parse input ICC");
1183
0
    return nullptr;
1184
0
  }
1185
0
  if (!DecodeProfile(context, Bytes(c_dst.icc), &profile_dst)) {
1186
0
    JXL_NOTIFY_ERROR("JxlCmsInit: lcms failed to parse output ICC");
1187
0
    return nullptr;
1188
0
  }
1189
0
#endif  // JPEGXL_ENABLE_SKCMS
1190
1191
0
  t->skip_lcms = false;
1192
0
  if (c_src.SameColorEncoding(c_dst)) {
1193
0
    t->skip_lcms = true;
1194
#if JXL_CMS_VERBOSE
1195
    printf("Skip CMS\n");
1196
#endif
1197
0
  }
1198
1199
0
  t->apply_hlg_ootf = c_src.tf.IsHLG() != c_dst.tf.IsHLG();
1200
0
  if (t->apply_hlg_ootf) {
1201
0
    const ColorEncoding* c_hlg = c_src.tf.IsHLG() ? &c_src : &c_dst;
1202
0
    t->hlg_ootf_num_channels = c_hlg->Channels();
1203
0
    if (t->hlg_ootf_num_channels == 3 &&
1204
0
        !GetPrimariesLuminances(*c_hlg, t->hlg_ootf_luminances.data())) {
1205
0
      JXL_NOTIFY_ERROR(
1206
0
          "JxlCmsInit: failed to compute the luminances of primaries");
1207
0
      return nullptr;
1208
0
    }
1209
0
  }
1210
1211
  // Special-case SRGB <=> linear if the primaries / white point are the same,
1212
  // or any conversion where PQ or HLG is involved:
1213
0
  bool src_linear = c_src.tf.IsLinear();
1214
0
  const bool dst_linear = c_dst.tf.IsLinear();
1215
1216
0
  if (c_src.tf.IsPQ() || c_src.tf.IsHLG() ||
1217
0
      (c_src.tf.IsSRGB() && dst_linear && c_src.SameColorSpace(c_dst))) {
1218
    // Construct new profile as if the data were already/still linear.
1219
0
    ColorEncoding c_linear_src = c_src;
1220
0
    c_linear_src.tf.SetTransferFunction(TransferFunction::kLinear);
1221
#if JPEGXL_ENABLE_SKCMS
1222
    skcms_ICCProfile new_src;
1223
#else   // JPEGXL_ENABLE_SKCMS
1224
0
    Profile new_src;
1225
0
#endif  // JPEGXL_ENABLE_SKCMS
1226
        // Only enable ExtraTF if profile creation succeeded.
1227
0
    if (MaybeCreateProfile(c_linear_src.ToExternal(), &icc_src) &&
1228
#if JPEGXL_ENABLE_SKCMS
1229
        DecodeProfile(icc_src.data(), icc_src.size(), &new_src)) {
1230
#else   // JPEGXL_ENABLE_SKCMS
1231
0
        DecodeProfile(context, Bytes(icc_src), &new_src)) {
1232
0
#endif  // JPEGXL_ENABLE_SKCMS
1233
#if JXL_CMS_VERBOSE
1234
      printf("Special HLG/PQ/sRGB -> linear\n");
1235
#endif
1236
#if JPEGXL_ENABLE_SKCMS
1237
      t->icc_src = std::move(icc_src);
1238
      t->profile_src = new_src;
1239
#else   // JPEGXL_ENABLE_SKCMS
1240
0
      profile_src.swap(new_src);
1241
0
#endif  // JPEGXL_ENABLE_SKCMS
1242
0
      t->preprocess = c_src.tf.IsSRGB()
1243
0
                          ? ExtraTF::kSRGB
1244
0
                          : (c_src.tf.IsPQ() ? ExtraTF::kPQ : ExtraTF::kHLG);
1245
0
      c_src = c_linear_src;
1246
0
      src_linear = true;
1247
0
    } else {
1248
0
      if (t->apply_hlg_ootf) {
1249
0
        JXL_NOTIFY_ERROR(
1250
0
            "Failed to create extra linear source profile, and HLG OOTF "
1251
0
            "required");
1252
0
        return nullptr;
1253
0
      }
1254
0
      JXL_WARNING("Failed to create extra linear destination profile");
1255
0
    }
1256
0
  }
1257
1258
0
  if (c_dst.tf.IsPQ() || c_dst.tf.IsHLG() ||
1259
0
      (c_dst.tf.IsSRGB() && src_linear && c_src.SameColorSpace(c_dst))) {
1260
0
    ColorEncoding c_linear_dst = c_dst;
1261
0
    c_linear_dst.tf.SetTransferFunction(TransferFunction::kLinear);
1262
#if JPEGXL_ENABLE_SKCMS
1263
    skcms_ICCProfile new_dst;
1264
#else   // JPEGXL_ENABLE_SKCMS
1265
0
    Profile new_dst;
1266
0
#endif  // JPEGXL_ENABLE_SKCMS
1267
    // Only enable ExtraTF if profile creation succeeded.
1268
0
    if (MaybeCreateProfile(c_linear_dst.ToExternal(), &icc_dst) &&
1269
#if JPEGXL_ENABLE_SKCMS
1270
        DecodeProfile(icc_dst.data(), icc_dst.size(), &new_dst)) {
1271
#else   // JPEGXL_ENABLE_SKCMS
1272
0
        DecodeProfile(context, Bytes(icc_dst), &new_dst)) {
1273
0
#endif  // JPEGXL_ENABLE_SKCMS
1274
#if JXL_CMS_VERBOSE
1275
      printf("Special linear -> HLG/PQ/sRGB\n");
1276
#endif
1277
#if JPEGXL_ENABLE_SKCMS
1278
      t->icc_dst = std::move(icc_dst);
1279
      t->profile_dst = new_dst;
1280
#else   // JPEGXL_ENABLE_SKCMS
1281
0
      profile_dst.swap(new_dst);
1282
0
#endif  // JPEGXL_ENABLE_SKCMS
1283
0
      t->postprocess = c_dst.tf.IsSRGB()
1284
0
                           ? ExtraTF::kSRGB
1285
0
                           : (c_dst.tf.IsPQ() ? ExtraTF::kPQ : ExtraTF::kHLG);
1286
0
      c_dst = c_linear_dst;
1287
0
    } else {
1288
0
      if (t->apply_hlg_ootf) {
1289
0
        JXL_NOTIFY_ERROR(
1290
0
            "Failed to create extra linear destination profile, and inverse "
1291
0
            "HLG OOTF required");
1292
0
        return nullptr;
1293
0
      }
1294
0
      JXL_WARNING("Failed to create extra linear destination profile");
1295
0
    }
1296
0
  }
1297
1298
0
  if (c_src.SameColorEncoding(c_dst)) {
1299
#if JXL_CMS_VERBOSE
1300
    printf("Same intermediary linear profiles, skipping CMS\n");
1301
#endif
1302
0
    t->skip_lcms = true;
1303
0
  }
1304
1305
#if JPEGXL_ENABLE_SKCMS
1306
  if (!skcms_MakeUsableAsDestination(&t->profile_dst)) {
1307
    JXL_NOTIFY_ERROR(
1308
        "Failed to make %s usable as a color transform destination",
1309
        ColorEncodingDescription(c_dst.ToExternal()).c_str());
1310
    return nullptr;
1311
  }
1312
#endif  // JPEGXL_ENABLE_SKCMS
1313
1314
  // Not including alpha channel (copied separately).
1315
0
  const size_t channels_src = (c_src.cmyk ? 4 : c_src.Channels());
1316
0
  const size_t channels_dst = c_dst.Channels();
1317
#if JXL_CMS_VERBOSE
1318
  printf("Channels: %" PRIuS "; Threads: %" PRIuS "\n", channels_src,
1319
         num_threads);
1320
#endif
1321
1322
0
#if !JPEGXL_ENABLE_SKCMS
1323
  // Type includes color space (XYZ vs RGB), so can be different.
1324
0
  const uint32_t type_src = Type32(c_src, channels_src == 4);
1325
0
  const uint32_t type_dst = Type32(c_dst, false);
1326
0
  const uint32_t intent = static_cast<uint32_t>(c_dst.rendering_intent);
1327
  // Use cmsFLAGS_NOCACHE to disable the 1-pixel cache and make calling
1328
  // cmsDoTransform() thread-safe.
1329
0
  const uint32_t flags = cmsFLAGS_NOCACHE | cmsFLAGS_BLACKPOINTCOMPENSATION |
1330
0
                         cmsFLAGS_HIGHRESPRECALC;
1331
0
  t->lcms_transform =
1332
0
      cmsCreateTransformTHR(context, profile_src.get(), type_src,
1333
0
                            profile_dst.get(), type_dst, intent, flags);
1334
0
  if (t->lcms_transform == nullptr) {
1335
0
    JXL_NOTIFY_ERROR("Failed to create transform");
1336
0
    return nullptr;
1337
0
  }
1338
0
#endif  // !JPEGXL_ENABLE_SKCMS
1339
1340
  // Ideally LCMS would convert directly from External to Image3. However,
1341
  // cmsDoTransformLineStride only accepts 32-bit BytesPerPlaneIn, whereas our
1342
  // planes can be more than 4 GiB apart. Hence, transform inputs/outputs must
1343
  // be interleaved. Calling cmsDoTransform for each pixel is expensive
1344
  // (indirect call). We therefore transform rows, which requires per-thread
1345
  // buffers. To avoid separate allocations, we use the rows of an image.
1346
  // Because LCMS apparently also cannot handle <= 16 bit inputs and 32-bit
1347
  // outputs (or vice versa), we use floating point input/output.
1348
0
  t->channels_src = channels_src;
1349
0
  t->channels_dst = channels_dst;
1350
0
#if !JPEGXL_ENABLE_SKCMS
1351
0
  size_t actual_channels_src = channels_src;
1352
0
  size_t actual_channels_dst = channels_dst;
1353
#else
1354
  // SkiaCMS doesn't support grayscale float buffers, so we create space for RGB
1355
  // float buffers anyway.
1356
  size_t actual_channels_src = (channels_src == 4 ? 4 : 3);
1357
  size_t actual_channels_dst = 3;
1358
#endif
1359
0
  AllocateBuffer(xsize * actual_channels_src, num_threads, &t->src_storage,
1360
0
                 &t->buf_src);
1361
0
  AllocateBuffer(xsize * actual_channels_dst, num_threads, &t->dst_storage,
1362
0
                 &t->buf_dst);
1363
0
  t->intensity_target = intensity_target;
1364
0
  return t.release();
1365
0
}
1366
1367
0
float* JxlCmsGetSrcBuf(void* cms_data, size_t thread) {
1368
0
  JxlCms* t = reinterpret_cast<JxlCms*>(cms_data);
1369
0
  return t->buf_src[thread];
1370
0
}
1371
1372
0
float* JxlCmsGetDstBuf(void* cms_data, size_t thread) {
1373
0
  JxlCms* t = reinterpret_cast<JxlCms*>(cms_data);
1374
0
  return t->buf_dst[thread];
1375
0
}
1376
1377
}  // namespace
1378
1379
extern "C" {
1380
1381
2.86k
JXL_CMS_EXPORT const JxlCmsInterface* JxlGetDefaultCms() {
1382
2.86k
  static constexpr JxlCmsInterface kInterface = {
1383
2.86k
      /*set_fields_data=*/nullptr,
1384
2.86k
      /*set_fields_from_icc=*/&JxlCmsSetFieldsFromICC,
1385
2.86k
      /*init_data=*/const_cast<void*>(static_cast<const void*>(&kInterface)),
1386
2.86k
      /*init=*/&JxlCmsInit,
1387
2.86k
      /*get_src_buf=*/&JxlCmsGetSrcBuf,
1388
2.86k
      /*get_dst_buf=*/&JxlCmsGetDstBuf,
1389
2.86k
      /*run=*/&DoColorSpaceTransform,
1390
2.86k
      /*destroy=*/&JxlCmsDestroy};
1391
2.86k
  return &kInterface;
1392
2.86k
}
1393
1394
}  // extern "C"
1395
1396
}  // namespace jxl
1397
#endif  // HWY_ONCE