Debian Codegraph @

src/codec/SkCodecColorProfile.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "modules/skcms/skcms.h"
9 #include "src/codec/SkCodecPriv.h"
10 #include "src/core/SkColorSpacePriv.h"
11
12 #if defined(SK_CODEC_COLOR_PROFILE_PARSE_WITH_RUST)
13 #include "src/codec/SkCodecColorProfileRust.h"
14 #endif
15
16 namespace SkCodecs {
17
18 namespace {
19
20 sk_sp<SkColorSpace> cicp_get_android_sk_color_space(uint8_t color_primaries,
21 uint8_t transfer_characteristics,
22 uint8_t matrix_coefficients,
23 uint8_t full_range_flag) {
24 if (matrix_coefficients != 0) {
25 return nullptr;
26 }
27 if (full_range_flag != 1) {
28 return nullptr;
29 }
30 skcms_Matrix3x3 primaries;
31 if (!SkNamedPrimaries::GetCicp(
32 static_cast<SkNamedPrimaries::CicpId>(color_primaries), primaries)) {
33 return nullptr;
34 }
35 skcms_TransferFunction trfn;
36 if (!SkNamedTransferFn::GetCicp(
37 static_cast<SkNamedTransferFn::CicpId>(transfer_characteristics), trfn)) {
38 return nullptr;
39 }
40 switch (transfer_characteristics) {
41 case 16:
42 // Android expects PQ to match 203 nits to SDR white
43 trfn = {-2.f, -1.55522297832f, 1.86045365631f, 32 / 2523.0f,
44 2413 / 128.0f, -2392 / 128.0f, 8192 / 1305.0f};
45 break;
46 case 18:
47 // Android expects HLG to match 203 nits to SDR white
48 skcms_TransferFunction_makeScaledHLGish(
49 &trfn, 0.314509843f, 2.f, 2.f, 1.f / 0.17883277f, 0.28466892f, 0.55991073f);
50 break;
51 default:
52 break;
53 }
54 return SkColorSpace::MakeRGB(trfn, primaries);
55 }
56
57 } // namespace
58
59 std::unique_ptr<ColorProfile> ColorProfile::MakeICCProfileWithSkCMS(
60 sk_sp<const SkData> data) {
61 if (data) {
62 skcms_ICCProfile profile;
63 if (skcms_Parse(data->data(), data->size(), &profile)) {
64 return std::unique_ptr<ColorProfile>(new ColorProfile(profile, std::move(data)));
65 }
66 }
67 return nullptr;
68 }
69
70 std::unique_ptr<ColorProfile> ColorProfile::MakeICCProfile(
71 sk_sp<const SkData> data) {
72 #if defined(SK_CODEC_COLOR_PROFILE_PARSE_WITH_RUST)
73 return MakeICCProfileWithRust(data);
74 #else
75 return MakeICCProfileWithSkCMS(data);
76 #endif
77 }
78
79 std::unique_ptr<ColorProfile> ColorProfile::Make(sk_sp<SkColorSpace> cs) {
80 if (!cs) {
81 return nullptr;
82 }
83 skcms_ICCProfile profile;
84 cs->toProfile(&profile);
85 return std::unique_ptr<ColorProfile>(new ColorProfile(profile));
86 }
87
88 std::unique_ptr<ColorProfile> ColorProfile::Make(
89 const skcms_TransferFunction& trfn,
90 const skcms_Matrix3x3& toXYZD50) {
91 skcms_ICCProfile profile;
92 skcms_Init(&profile);
93 skcms_SetTransferFunction(&profile, &trfn);
94 skcms_SetXYZD50(&profile, &toXYZD50);
95 return std::unique_ptr<ColorProfile>(new ColorProfile(profile));
96 }
97
98 std::unique_ptr<ColorProfile> ColorProfile::MakeCICP(
99 uint8_t color_primaries,
100 uint8_t transfer_characteristics,
101 uint8_t matrix_coefficients,
102 uint8_t full_range_flag) {
103 skcms_ICCProfile profile;
104 skcms_Init(&profile);
105 profile.has_CICP = true;
106 profile.CICP.color_primaries = color_primaries;
107 profile.CICP.transfer_characteristics = transfer_characteristics;
108 profile.CICP.matrix_coefficients = matrix_coefficients;
109 profile.CICP.video_full_range_flag = full_range_flag;
110 return std::unique_ptr<ColorProfile>(new ColorProfile(profile));
111 }
112
113 std::unique_ptr<ColorProfile> ColorProfile::clone() const {
114 return std::unique_ptr<ColorProfile>(new ColorProfile(fProfile, fData));
115 }
116
117 ColorProfile::DataSpace ColorProfile::dataSpace() const {
118 switch (fProfile.data_color_space) {
119 case skcms_Signature_RGB:
120 return DataSpace::kRGB;
121 case skcms_Signature_CMYK:
122 return DataSpace::kCMYK;
123 case skcms_Signature_Gray:
124 return DataSpace::kGray;
125 default:
126 return DataSpace::kOther;
127 }
128 }
129
130 sk_sp<SkColorSpace> ColorProfile::getExactColorSpace() const {
131 return SkColorSpace::Make(fProfile);
132 }
133
134 sk_sp<SkColorSpace> ColorProfile::getAndroidOutputColorSpace() const {
135 // Prefer CICP information if it exists.
136 if (fProfile.has_CICP) {
137 const auto cicpColorSpace = cicp_get_android_sk_color_space(
138 fProfile.CICP.color_primaries,
139 fProfile.CICP.transfer_characteristics,
140 fProfile.CICP.matrix_coefficients,
141 fProfile.CICP.video_full_range_flag);
142 if (cicpColorSpace) {
143 return cicpColorSpace;
144 }
145 }
146 if (auto encodedSpace = SkColorSpace::Make(fProfile)) {
147 // Leave the pixels in the encoded color space. Color space conversion
148 // will be handled after decode time.
149 return encodedSpace;
150 }
151 if (fProfile.has_toXYZD50) {
152 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
153 fProfile.toXYZD50);
154 }
155 return SkColorSpace::MakeSRGB();
156 }
157
158 ColorProfile::ColorProfile(const skcms_ICCProfile& profile, sk_sp<const SkData> data)
159 : fProfile(profile)
160 , fData(std::move(data))
161 {}
162
163 } // namespace SkCodecs
164