Debian Codegraph @

lib/jxl/cms/color_encoding_cms.h

raw
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 #ifndef LIB_JXL_CMS_COLOR_ENCODING_CMS_H_
7 #define LIB_JXL_CMS_COLOR_ENCODING_CMS_H_
8
9 #include <jxl/cms_interface.h>
10 #include <jxl/color_encoding.h>
11 #include <jxl/types.h>
12
13 #include <cmath>
14 #include <cstdint>
15 #include <cstring>
16 #include <utility>
17 #include <vector>
18
19 #include "lib/jxl/base/status.h"
20
21 namespace jxl {
22 namespace cms {
23
24 using IccBytes = std::vector<uint8_t>;
25
26 // Returns whether the two inputs are approximately equal.
27 static inline bool ApproxEq(const double a, const double b,
28 double max_l1 = 1E-3) {
29 // Threshold should be sufficient for ICC's 15-bit fixed-point numbers.
30 // We have seen differences of 7.1E-5 with lcms2 and 1E-3 with skcms.
31 return std::abs(a - b) <= max_l1;
32 }
33
34 // (All CIE units are for the standard 1931 2 degree observer)
35
36 // Color space the color pixel data is encoded in. The color pixel data is
37 // 3-channel in all cases except in case of kGray, where it uses only 1 channel.
38 // This also determines the amount of channels used in modular encoding.
39 enum class ColorSpace : uint32_t {
40 // Trichromatic color data. This also includes CMYK if a kBlack
41 // ExtraChannelInfo is present. This implies, if there is an ICC profile, that
42 // the ICC profile uses a 3-channel color space if no kBlack extra channel is
43 // present, or uses color space 'CMYK' if a kBlack extra channel is present.
44 kRGB,
45 // Single-channel data. This implies, if there is an ICC profile, that the ICC
46 // profile also represents single-channel data and has the appropriate color
47 // space ('GRAY').
48 kGray,
49 // Like kRGB, but implies fixed values for primaries etc.
50 kXYB,
51 // For non-RGB/gray data, e.g. from non-electro-optical sensors. Otherwise
52 // the same conditions as kRGB apply.
53 kUnknown
54 // NB: don't forget to update EnumBits!
55 };
56
57 // Values from CICP ColourPrimaries.
58 enum class WhitePoint : uint32_t {
59 kD65 = 1, // sRGB/BT.709/Display P3/BT.2020
60 kCustom = 2, // Actual values encoded in separate fields
61 kE = 10, // XYZ
62 kDCI = 11, // DCI-P3
63 // NB: don't forget to update EnumBits!
64 };
65
66 // Values from CICP ColourPrimaries
67 enum class Primaries : uint32_t {
68 kSRGB = 1, // Same as BT.709
69 kCustom = 2, // Actual values encoded in separate fields
70 k2100 = 9, // Same as BT.2020
71 kP3 = 11,
72 // NB: don't forget to update EnumBits!
73 };
74
75 // Values from CICP TransferCharacteristics
76 enum class TransferFunction : uint32_t {
77 k709 = 1,
78 kUnknown = 2,
79 kLinear = 8,
80 kSRGB = 13,
81 kPQ = 16, // from BT.2100
82 kDCI = 17, // from SMPTE RP 431-2 reference projector
83 kHLG = 18, // from BT.2100
84 // NB: don't forget to update EnumBits!
85 };
86
87 enum class RenderingIntent : uint32_t {
88 // Values match ICC sRGB encodings.
89 kPerceptual = 0, // good for photos, requires a profile with LUT.
90 kRelative, // good for logos.
91 kSaturation, // perhaps useful for CG with fully saturated colors.
92 kAbsolute, // leaves white point unchanged; good for proofing.
93 // NB: don't forget to update EnumBits!
94 };
95
96 // Chromaticity (Y is omitted because it is 1 for white points and implicit for
97 // primaries)
98 struct CIExy {
99 CIExy() = default;
100 CIExy(double x, double y) : x(x), y(y) {}
101 double x = 0.0;
102 double y = 0.0;
103 };
104
105 struct PrimariesCIExy {
106 CIExy r;
107 CIExy g;
108 CIExy b;
109 };
110
111 // Serializable form of CIExy.
112 struct Customxy {
113 static constexpr uint32_t kMul = 1000000;
114 static constexpr double kRoughLimit = 4.0;
115 static constexpr int32_t kMin = -0x200000;
116 static constexpr int32_t kMax = 0x1FFFFF;
117
118 int32_t x = 0;
119 int32_t y = 0;
120
121 CIExy GetValue() const {
122 CIExy xy;
123 xy.x = x * (1.0 / kMul);
124 xy.y = y * (1.0 / kMul);
125 return xy;
126 }
127
128 Status SetValue(const CIExy& xy) {
129 bool ok = (std::abs(xy.x) < kRoughLimit) && (std::abs(xy.y) < kRoughLimit);
130 if (!ok) return JXL_FAILURE("X or Y is out of bounds");
131 x = static_cast<int32_t>(roundf(xy.x * kMul));
132 if (x < kMin || x > kMax) return JXL_FAILURE("X is out of bounds");
133 y = static_cast<int32_t>(roundf(xy.y * kMul));
134 if (y < kMin || y > kMax) return JXL_FAILURE("Y is out of bounds");
135 return true;
136 }
137
138 bool IsSame(const Customxy& other) const {
139 return (x == other.x) && (y == other.y);
140 }
141 };
142
143 static inline Status WhitePointFromExternal(const JxlWhitePoint external,
144 WhitePoint* out) {
145 switch (external) {
146 case JXL_WHITE_POINT_D65:
147 *out = WhitePoint::kD65;
148 return true;
149 case JXL_WHITE_POINT_CUSTOM:
150 *out = WhitePoint::kCustom;
151 return true;
152 case JXL_WHITE_POINT_E:
153 *out = WhitePoint::kE;
154 return true;
155 case JXL_WHITE_POINT_DCI:
156 *out = WhitePoint::kDCI;
157 return true;
158 }
159 return JXL_FAILURE("Invalid WhitePoint enum value %d",
160 static_cast<int>(external));
161 }
162
163 static inline Status PrimariesFromExternal(const JxlPrimaries external,
164 Primaries* out) {
165 switch (external) {
166 case JXL_PRIMARIES_SRGB:
167 *out = Primaries::kSRGB;
168 return true;
169 case JXL_PRIMARIES_CUSTOM:
170 *out = Primaries::kCustom;
171 return true;
172 case JXL_PRIMARIES_2100:
173 *out = Primaries::k2100;
174 return true;
175 case JXL_PRIMARIES_P3:
176 *out = Primaries::kP3;
177 return true;
178 }
179 return JXL_FAILURE("Invalid Primaries enum value");
180 }
181
182 static inline Status RenderingIntentFromExternal(
183 const JxlRenderingIntent external, RenderingIntent* out) {
184 switch (external) {
185 case JXL_RENDERING_INTENT_PERCEPTUAL:
186 *out = RenderingIntent::kPerceptual;
187 return true;
188 case JXL_RENDERING_INTENT_RELATIVE:
189 *out = RenderingIntent::kRelative;
190 return true;
191 case JXL_RENDERING_INTENT_SATURATION:
192 *out = RenderingIntent::kSaturation;
193 return true;
194 case JXL_RENDERING_INTENT_ABSOLUTE:
195 *out = RenderingIntent::kAbsolute;
196 return true;
197 }
198 return JXL_FAILURE("Invalid RenderingIntent enum value");
199 }
200
201 struct CustomTransferFunction {
202 // Highest reasonable value for the gamma of a transfer curve.
203 static constexpr uint32_t kMaxGamma = 8192;
204 static constexpr uint32_t kGammaMul = 10000000;
205
206 bool have_gamma = false;
207
208 // OETF exponent to go from linear to gamma-compressed.
209 uint32_t gamma = 0; // Only used if have_gamma_.
210
211 // Can be kUnknown.
212 TransferFunction transfer_function =
213 TransferFunction::kSRGB; // Only used if !have_gamma_.
214
215 TransferFunction GetTransferFunction() const {
216 JXL_DASSERT(!have_gamma);
217 return have_gamma ? TransferFunction::kUnknown : transfer_function;
218 }
219 void SetTransferFunction(const TransferFunction tf) {
220 have_gamma = false;
221 transfer_function = tf;
222 }
223
224 bool IsUnknown() const {
225 return !have_gamma && (transfer_function == TransferFunction::kUnknown);
226 }
227 bool IsSRGB() const {
228 return !have_gamma && (transfer_function == TransferFunction::kSRGB);
229 }
230 bool IsLinear() const {
231 return !have_gamma && (transfer_function == TransferFunction::kLinear);
232 }
233 bool IsPQ() const {
234 return !have_gamma && (transfer_function == TransferFunction::kPQ);
235 }
236 bool IsHLG() const {
237 return !have_gamma && (transfer_function == TransferFunction::kHLG);
238 }
239 bool Is709() const {
240 return !have_gamma && (transfer_function == TransferFunction::k709);
241 }
242 bool IsDCI() const {
243 return !have_gamma && (transfer_function == TransferFunction::kDCI);
244 }
245
246 double GetGamma() const {
247 JXL_DASSERT(have_gamma);
248 if (!have_gamma) return 0.0;
249 return gamma * (1.0 / kGammaMul); // (0, 1)
250 }
251 Status SetGamma(double new_gamma) {
252 if (new_gamma < (1.0 / kMaxGamma) || new_gamma > 1.0) {
253 return JXL_FAILURE("Invalid gamma %f", new_gamma);
254 }
255
256 have_gamma = false;
257 if (ApproxEq(new_gamma, 1.0)) {
258 transfer_function = TransferFunction::kLinear;
259 return true;
260 }
261 if (ApproxEq(new_gamma, 1.0 / 2.6)) {
262 transfer_function = TransferFunction::kDCI;
263 return true;
264 }
265 // Don't translate 0.45.. to kSRGB nor k709 - that might change pixel
266 // values because those curves also have a linear part.
267
268 have_gamma = true;
269 gamma = roundf(new_gamma * kGammaMul);
270 transfer_function = TransferFunction::kUnknown;
271 return true;
272 }
273
274 bool IsSame(const CustomTransferFunction& other) const {
275 if (have_gamma != other.have_gamma) {
276 return false;
277 }
278 if (have_gamma) {
279 if (gamma != other.gamma) {
280 return false;
281 }
282 } else {
283 if (transfer_function != other.transfer_function) {
284 return false;
285 }
286 }
287 return true;
288 }
289 };
290
291 static inline Status ConvertExternalToInternalTransferFunction(
292 const JxlTransferFunction external, TransferFunction* internal) {
293 switch (external) {
294 case JXL_TRANSFER_FUNCTION_709:
295 *internal = TransferFunction::k709;
296 return true;
297 case JXL_TRANSFER_FUNCTION_UNKNOWN:
298 *internal = TransferFunction::kUnknown;
299 return true;
300 case JXL_TRANSFER_FUNCTION_LINEAR:
301 *internal = TransferFunction::kLinear;
302 return true;
303 case JXL_TRANSFER_FUNCTION_SRGB:
304 *internal = TransferFunction::kSRGB;
305 return true;
306 case JXL_TRANSFER_FUNCTION_PQ:
307 *internal = TransferFunction::kPQ;
308 return true;
309 case JXL_TRANSFER_FUNCTION_DCI:
310 *internal = TransferFunction::kDCI;
311 return true;
312 case JXL_TRANSFER_FUNCTION_HLG:
313 *internal = TransferFunction::kHLG;
314 return true;
315 case JXL_TRANSFER_FUNCTION_GAMMA:
316 return JXL_FAILURE("Gamma should be handled separately");
317 }
318 return JXL_FAILURE("Invalid TransferFunction enum value");
319 }
320
321 // Compact encoding of data required to interpret and translate pixels to a
322 // known color space. Stored in Metadata. Thread-compatible.
323 struct ColorEncoding {
324 // Only valid if HaveFields()
325 WhitePoint white_point = WhitePoint::kD65;
326 Primaries primaries = Primaries::kSRGB; // Only valid if HasPrimaries()
327 RenderingIntent rendering_intent = RenderingIntent::kRelative;
328
329 // When false, fields such as white_point and tf are invalid and must not be
330 // used. This occurs after setting a raw bytes-only ICC profile, only the
331 // ICC bytes may be used. The color_space_ field is still valid.
332 bool have_fields = true;
333
334 IccBytes icc; // Valid ICC profile
335
336 ColorSpace color_space = ColorSpace::kRGB; // Can be kUnknown
337 bool cmyk = false;
338
339 // "late sync" fields
340 CustomTransferFunction tf;
341 Customxy white; // Only used if white_point == kCustom
342 Customxy red; // Only used if primaries == kCustom
343 Customxy green; // Only used if primaries == kCustom
344 Customxy blue; // Only used if primaries == kCustom
345
346 // Returns false if the field is invalid and unusable.
347 bool HasPrimaries() const {
348 return (color_space != ColorSpace::kGray) &&
349 (color_space != ColorSpace::kXYB);
350 }
351
352 size_t Channels() const { return (color_space == ColorSpace::kGray) ? 1 : 3; }
353
354 Status GetPrimaries(PrimariesCIExy& xy) const {
355 JXL_ENSURE(have_fields);
356 JXL_ENSURE(HasPrimaries());
357 xy = {};
358 switch (primaries) {
359 case Primaries::kCustom:
360 xy.r = red.GetValue();
361 xy.g = green.GetValue();
362 xy.b = blue.GetValue();
363 break;
364
365 case Primaries::kSRGB:
366 xy.r.x = 0.639998686;
367 xy.r.y = 0.330010138;
368 xy.g.x = 0.300003784;
369 xy.g.y = 0.600003357;
370 xy.b.x = 0.150002046;
371 xy.b.y = 0.059997204;
372 break;
373
374 case Primaries::k2100:
375 xy.r.x = 0.708;
376 xy.r.y = 0.292;
377 xy.g.x = 0.170;
378 xy.g.y = 0.797;
379 xy.b.x = 0.131;
380 xy.b.y = 0.046;
381 break;
382
383 case Primaries::kP3:
384 xy.r.x = 0.680;
385 xy.r.y = 0.320;
386 xy.g.x = 0.265;
387 xy.g.y = 0.690;
388 xy.b.x = 0.150;
389 xy.b.y = 0.060;
390 break;
391
392 default:
393 JXL_DEBUG_ABORT("internal: unexpected Primaries: %d",
394 static_cast<int>(primaries));
395 }
396 return true;
397 }
398
399 Status SetPrimaries(const PrimariesCIExy& xy) {
400 JXL_ENSURE(have_fields);
401 JXL_ENSURE(HasPrimaries());
402 if (xy.r.x == 0.0 || xy.r.y == 0.0 || xy.g.x == 0.0 || xy.g.y == 0.0 ||
403 xy.b.x == 0.0 || xy.b.y == 0.0) {
404 return JXL_FAILURE("Invalid primaries %f %f %f %f %f %f", xy.r.x, xy.r.y,
405 xy.g.x, xy.g.y, xy.b.x, xy.b.y);
406 }
407
408 if (ApproxEq(xy.r.x, 0.64) && ApproxEq(xy.r.y, 0.33) &&
409 ApproxEq(xy.g.x, 0.30) && ApproxEq(xy.g.y, 0.60) &&
410 ApproxEq(xy.b.x, 0.15) && ApproxEq(xy.b.y, 0.06)) {
411 primaries = Primaries::kSRGB;
412 return true;
413 }
414
415 if (ApproxEq(xy.r.x, 0.708) && ApproxEq(xy.r.y, 0.292) &&
416 ApproxEq(xy.g.x, 0.170) && ApproxEq(xy.g.y, 0.797) &&
417 ApproxEq(xy.b.x, 0.131) && ApproxEq(xy.b.y, 0.046)) {
418 primaries = Primaries::k2100;
419 return true;
420 }
421 if (ApproxEq(xy.r.x, 0.680) && ApproxEq(xy.r.y, 0.320) &&
422 ApproxEq(xy.g.x, 0.265) && ApproxEq(xy.g.y, 0.690) &&
423 ApproxEq(xy.b.x, 0.150) && ApproxEq(xy.b.y, 0.060)) {
424 primaries = Primaries::kP3;
425 return true;
426 }
427
428 primaries = Primaries::kCustom;
429 JXL_RETURN_IF_ERROR(red.SetValue(xy.r));
430 JXL_RETURN_IF_ERROR(green.SetValue(xy.g));
431 JXL_RETURN_IF_ERROR(blue.SetValue(xy.b));
432 return true;
433 }
434
435 CIExy GetWhitePoint() const {
436 CIExy xy{};
437 JXL_DASSERT(have_fields);
438 if (!have_fields) return xy;
439 switch (white_point) {
440 case WhitePoint::kCustom:
441 xy = white.GetValue();
442 break;
443
444 case WhitePoint::kD65:
445 xy.x = 0.3127;
446 xy.y = 0.3290;
447 break;
448
449 case WhitePoint::kDCI:
450 // From https://ieeexplore.ieee.org/document/7290729 C.2 page 11
451 xy.x = 0.314;
452 xy.y = 0.351;
453 break;
454
455 case WhitePoint::kE:
456 xy.x = xy.y = 1.0 / 3;
457 break;
458
459 default:
460 JXL_DEBUG_ABORT("internal: unexpected WhitePoint: %d",
461 static_cast<int>(white_point));
462 }
463 return xy;
464 }
465
466 Status SetWhitePoint(const CIExy& xy) {
467 JXL_ENSURE(have_fields);
468 if (xy.x == 0.0 || xy.y == 0.0) {
469 return JXL_FAILURE("Invalid white point %f %f", xy.x, xy.y);
470 }
471 if (ApproxEq(xy.x, 0.3127) && ApproxEq(xy.y, 0.3290)) {
472 white_point = WhitePoint::kD65;
473 return true;
474 }
475 if (ApproxEq(xy.x, 1.0 / 3) && ApproxEq(xy.y, 1.0 / 3)) {
476 white_point = WhitePoint::kE;
477 return true;
478 }
479 if (ApproxEq(xy.x, 0.314) && ApproxEq(xy.y, 0.351)) {
480 white_point = WhitePoint::kDCI;
481 return true;
482 }
483 white_point = WhitePoint::kCustom;
484 return white.SetValue(xy);
485 }
486
487 // Checks if the color spaces (including white point / primaries) are the
488 // same, but ignores the transfer function, rendering intent and ICC bytes.
489 bool SameColorSpace(const ColorEncoding& other) const {
490 if (color_space != other.color_space) return false;
491
492 if (white_point != other.white_point) return false;
493 if (white_point == WhitePoint::kCustom) {
494 if (!white.IsSame(other.white)) {
495 return false;
496 }
497 }
498
499 if (HasPrimaries() != other.HasPrimaries()) return false;
500 if (HasPrimaries()) {
501 if (primaries != other.primaries) return false;
502 if (primaries == Primaries::kCustom) {
503 if (!red.IsSame(other.red)) return false;
504 if (!green.IsSame(other.green)) return false;
505 if (!blue.IsSame(other.blue)) return false;
506 }
507 }
508 return true;
509 }
510
511 // Checks if the color space and transfer function are the same, ignoring
512 // rendering intent and ICC bytes
513 bool SameColorEncoding(const ColorEncoding& other) const {
514 return SameColorSpace(other) && tf.IsSame(other.tf);
515 }
516
517 // Returns true if all fields have been initialized (possibly to kUnknown).
518 // Returns false if the ICC profile is invalid or decoding it fails.
519 Status SetFieldsFromICC(IccBytes&& new_icc, const JxlCmsInterface& cms) {
520 // In case parsing fails, mark the ColorEncoding as invalid.
521 JXL_ENSURE(!new_icc.empty());
522 color_space = ColorSpace::kUnknown;
523 tf.transfer_function = TransferFunction::kUnknown;
524 icc.clear();
525
526 JxlColorEncoding external;
527 JXL_BOOL new_cmyk;
528 JXL_RETURN_IF_ERROR(cms.set_fields_from_icc(cms.set_fields_data,
529 new_icc.data(), new_icc.size(),
530 &external, &new_cmyk));
531 cmyk = static_cast<bool>(new_cmyk);
532 JXL_RETURN_IF_ERROR(FromExternal(external));
533 icc = std::move(new_icc);
534 return true;
535 }
536
537 JxlColorEncoding ToExternal() const {
538 JxlColorEncoding external = {};
539 auto set_error = [&]() {
540 external.color_space = JXL_COLOR_SPACE_UNKNOWN;
541 external.primaries = JXL_PRIMARIES_CUSTOM;
542 external.rendering_intent = JXL_RENDERING_INTENT_PERCEPTUAL; //?
543 external.transfer_function = JXL_TRANSFER_FUNCTION_UNKNOWN;
544 external.white_point = JXL_WHITE_POINT_CUSTOM;
545 };
546 if (!have_fields) {
547 set_error();
548 return external;
549 }
550 external.color_space = static_cast<JxlColorSpace>(color_space);
551
552 external.white_point = static_cast<JxlWhitePoint>(white_point);
553
554 CIExy wp = GetWhitePoint();
555 external.white_point_xy[0] = wp.x;
556 external.white_point_xy[1] = wp.y;
557
558 if (external.color_space == JXL_COLOR_SPACE_RGB ||
559 external.color_space == JXL_COLOR_SPACE_UNKNOWN) {
560 external.primaries = static_cast<JxlPrimaries>(primaries);
561 PrimariesCIExy p;
562 if (!GetPrimaries(p)) {
563 set_error();
564 return external;
565 }
566 external.primaries_red_xy[0] = p.r.x;
567 external.primaries_red_xy[1] = p.r.y;
568 external.primaries_green_xy[0] = p.g.x;
569 external.primaries_green_xy[1] = p.g.y;
570 external.primaries_blue_xy[0] = p.b.x;
571 external.primaries_blue_xy[1] = p.b.y;
572 }
573
574 if (tf.have_gamma) {
575 external.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
576 external.gamma = tf.GetGamma();
577 } else {
578 external.transfer_function =
579 static_cast<JxlTransferFunction>(tf.GetTransferFunction());
580 external.gamma = 0;
581 }
582
583 external.rendering_intent =
584 static_cast<JxlRenderingIntent>(rendering_intent);
585 return external;
586 }
587
588 // NB: does not create ICC.
589 Status FromExternal(const JxlColorEncoding& external) {
590 // TODO(eustas): update non-serializable on call-site
591 color_space = static_cast<ColorSpace>(external.color_space);
592
593 JXL_RETURN_IF_ERROR(
594 WhitePointFromExternal(external.white_point, &white_point));
595 if (external.white_point == JXL_WHITE_POINT_CUSTOM) {
596 CIExy wp;
597 wp.x = external.white_point_xy[0];
598 wp.y = external.white_point_xy[1];
599 JXL_RETURN_IF_ERROR(SetWhitePoint(wp));
600 }
601
602 if (external.color_space == JXL_COLOR_SPACE_RGB ||
603 external.color_space == JXL_COLOR_SPACE_UNKNOWN) {
604 JXL_RETURN_IF_ERROR(
605 PrimariesFromExternal(external.primaries, &primaries));
606 if (external.primaries == JXL_PRIMARIES_CUSTOM) {
607 PrimariesCIExy primaries;
608 primaries.r.x = external.primaries_red_xy[0];
609 primaries.r.y = external.primaries_red_xy[1];
610 primaries.g.x = external.primaries_green_xy[0];
611 primaries.g.y = external.primaries_green_xy[1];
612 primaries.b.x = external.primaries_blue_xy[0];
613 primaries.b.y = external.primaries_blue_xy[1];
614 JXL_RETURN_IF_ERROR(SetPrimaries(primaries));
615 }
616 }
617 CustomTransferFunction tf;
618 if (external.transfer_function == JXL_TRANSFER_FUNCTION_GAMMA) {
619 JXL_RETURN_IF_ERROR(tf.SetGamma(external.gamma));
620 } else {
621 TransferFunction tf_enum;
622 // JXL_TRANSFER_FUNCTION_GAMMA is not handled by this function since
623 // there's no internal enum value for it.
624 JXL_RETURN_IF_ERROR(ConvertExternalToInternalTransferFunction(
625 external.transfer_function, &tf_enum));
626 tf.SetTransferFunction(tf_enum);
627 }
628 this->tf = tf;
629
630 JXL_RETURN_IF_ERROR(RenderingIntentFromExternal(external.rendering_intent,
631 &rendering_intent));
632
633 icc.clear();
634
635 return true;
636 }
637 };
638
639 } // namespace cms
640 } // namespace jxl
641
642 #endif // LIB_JXL_CMS_COLOR_ENCODING_CMS_H_