| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 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 "include/core/SkColorSpace.h" |
| 9 |
#include "include/core/SkData.h" |
| 10 |
#include "include/private/base/SkTemplates.h" |
| 11 |
#include "modules/skcms/skcms.h" |
| 12 |
#include "src/core/SkChecksum.h" |
| 13 |
#include "src/core/SkColorSpacePriv.h" |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
#include <cstring> |
| 17 |
|
| 18 |
static bool xyz_almost_equal(const skcms_Matrix3x3& mA, const skcms_Matrix3x3& mB) { |
| 19 |
for (int r = 0; r < 3; ++r) { |
| 20 |
for (int c = 0; c < 3; ++c) { |
| 21 |
if (!color_space_almost_equal(mA.vals[r][c], mB.vals[r][c])) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
namespace SkNamedPrimaries { |
| 31 |
|
| 32 |
// Rec. ITU-T H.273, Table 2. |
| 33 |
struct TableEntry { |
| 34 |
CicpId cicp_id; |
| 35 |
SkColorSpacePrimaries sk_primaries; |
| 36 |
const skcms_Matrix3x3* to_xyzd50 = nullptr; |
| 37 |
}; |
| 38 |
const static TableEntry cicp_table[] = { |
| 39 |
{ CicpId::kRec709, kRec709, &SkNamedGamut::kSRGB }, |
| 40 |
{ CicpId::kRec470SystemM, kRec470SystemM }, |
| 41 |
{ CicpId::kRec470SystemBG, kRec470SystemBG }, |
| 42 |
{ CicpId::kRec601, kRec601 }, |
| 43 |
{ CicpId::kSMPTE_ST_240, kSMPTE_ST_240 }, |
| 44 |
{ CicpId::kGenericFilm, kGenericFilm }, |
| 45 |
{ CicpId::kRec2020, kRec2020, &SkNamedGamut::kRec2020 }, |
| 46 |
{ CicpId::kSMPTE_ST_428_1, kSMPTE_ST_428_1 }, |
| 47 |
{ CicpId::kSMPTE_RP_431_2, kSMPTE_RP_431_2 }, |
| 48 |
{ CicpId::kSMPTE_EG_432_1, kSMPTE_EG_432_1, &SkNamedGamut::kDisplayP3 }, |
| 49 |
{ CicpId::kITU_T_H273_Value22, kITU_T_H273_Value22 }, |
| 50 |
}; |
| 51 |
// Value 2 indicates "characteristics are unknown or are determined by the application". In |
| 52 |
// practice, this means we will delegate the primaries to the toXYZD50 tags. |
| 53 |
constexpr uint8_t kCicpIdApplicationDefined = 2; |
| 54 |
|
| 55 |
bool GetCicp(CicpId primaries, skcms_Matrix3x3& toXYZD50) { |
| 56 |
for (const auto& table_entry : cicp_table) { |
| 57 |
if (primaries != table_entry.cicp_id) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
if (table_entry.to_xyzd50) { |
| 61 |
toXYZD50 = *table_entry.to_xyzd50; |
| 62 |
return true; |
| 63 |
} |
| 64 |
if (table_entry.sk_primaries.toXYZD50(&toXYZD50)) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
bool GetCicpFromMatrix(const skcms_Matrix3x3& m, CicpId& primaries) { |
| 72 |
for (const auto& table_entry : cicp_table) { |
| 73 |
skcms_Matrix3x3 table_entry_m; |
| 74 |
if (table_entry.sk_primaries.toXYZD50(&table_entry_m)) { |
| 75 |
if (xyz_almost_equal(m, table_entry_m)) { |
| 76 |
primaries = table_entry.cicp_id; |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
} // namespace SkNamedPrimaries |
| 85 |
|
| 86 |
namespace SkNamedTransferFn { |
| 87 |
|
| 88 |
// Rec. ITU-T H.273, Table 3. |
| 89 |
struct TableEntry { |
| 90 |
CicpId cicp_id; |
| 91 |
skcms_TransferFunction trfn; |
| 92 |
}; |
| 93 |
const static TableEntry cicp_table[] = { |
| 94 |
{ SkNamedTransferFn::CicpId::kRec709, kRec709 }, |
| 95 |
{ SkNamedTransferFn::CicpId::kRec470SystemM, kRec470SystemM }, |
| 96 |
{ SkNamedTransferFn::CicpId::kRec470SystemBG, kRec470SystemBG }, |
| 97 |
{ SkNamedTransferFn::CicpId::kRec601, kRec601 }, |
| 98 |
{ SkNamedTransferFn::CicpId::kSMPTE_ST_240, kSMPTE_ST_240 }, |
| 99 |
{ SkNamedTransferFn::CicpId::kLinear, SkNamedTransferFn::kLinear }, |
| 100 |
{ SkNamedTransferFn::CicpId::kIEC61966_2_4, kIEC61966_2_4 }, |
| 101 |
{ SkNamedTransferFn::CicpId::kIEC61966_2_1, SkNamedTransferFn::kIEC61966_2_1 }, |
| 102 |
{ SkNamedTransferFn::CicpId::kRec2020_10bit, kRec2020_10bit }, |
| 103 |
{ SkNamedTransferFn::CicpId::kRec2020_12bit, kRec2020_12bit }, |
| 104 |
{ SkNamedTransferFn::CicpId::kPQ, SkNamedTransferFn::kPQ }, |
| 105 |
{ SkNamedTransferFn::CicpId::kSMPTE_ST_428_1, kSMPTE_ST_428_1 }, |
| 106 |
{ SkNamedTransferFn::CicpId::kHLG, SkNamedTransferFn::kHLG }, |
| 107 |
}; |
| 108 |
// Value 2 indicates "characteristics are unknown or are determined by the application". In |
| 109 |
// practice, this means we will delegate the primaries to the trc tags. |
| 110 |
constexpr uint8_t kCicpIdApplicationDefined = 2; |
| 111 |
|
| 112 |
bool GetCicp(SkNamedTransferFn::CicpId transfer_characteristics, skcms_TransferFunction& trfn) { |
| 113 |
for (const auto& table_entry : cicp_table) { |
| 114 |
if (transfer_characteristics == table_entry.cicp_id) { |
| 115 |
trfn = table_entry.trfn; |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
} // namespace SkNamedTransferFn |
| 123 |
|
| 124 |
bool SkColorSpacePrimaries::toXYZD50(skcms_Matrix3x3* toXYZ_D50) const { |
| 125 |
return skcms_PrimariesToXYZD50(fRX, fRY, fGX, fGY, fBX, fBY, fWX, fWY, toXYZ_D50); |
| 126 |
} |
| 127 |
|
| 128 |
SkColorSpace::SkColorSpace(const skcms_TransferFunction& transferFn, |
| 129 |
const skcms_Matrix3x3& toXYZD50) |
| 130 |
: fTransferFn(transferFn) |
| 131 |
, fToXYZD50(toXYZD50) { |
| 132 |
fTransferFnHash = SkChecksum::Hash32(&fTransferFn, 7*sizeof(float)); |
| 133 |
fToXYZD50Hash = SkChecksum::Hash32(&fToXYZD50, 9*sizeof(float)); |
| 134 |
} |
| 135 |
|
| 136 |
sk_sp<SkColorSpace> SkColorSpace::MakeRGB(const skcms_TransferFunction& transferFn, |
| 137 |
const skcms_Matrix3x3& toXYZ) { |
| 138 |
if (skcms_TransferFunction_getType(&transferFn) == skcms_TFType_Invalid) { |
| 139 |
return nullptr; |
| 140 |
} |
| 141 |
|
| 142 |
const skcms_TransferFunction* tf = &transferFn; |
| 143 |
|
| 144 |
if (is_almost_srgb(transferFn)) { |
| 145 |
if (xyz_almost_equal(toXYZ, SkNamedGamut::kSRGB)) { |
| 146 |
return SkColorSpace::MakeSRGB(); |
| 147 |
} |
| 148 |
tf = &SkNamedTransferFn::kSRGB; |
| 149 |
} else if (is_almost_2dot2(transferFn)) { |
| 150 |
tf = &SkNamedTransferFn::k2Dot2; |
| 151 |
} else if (is_almost_linear(transferFn)) { |
| 152 |
if (xyz_almost_equal(toXYZ, SkNamedGamut::kSRGB)) { |
| 153 |
return SkColorSpace::MakeSRGBLinear(); |
| 154 |
} |
| 155 |
tf = &SkNamedTransferFn::kLinear; |
| 156 |
} |
| 157 |
|
| 158 |
return sk_sp<SkColorSpace>(new SkColorSpace(*tf, toXYZ)); |
| 159 |
} |
| 160 |
|
| 161 |
sk_sp<SkColorSpace> SkColorSpace::MakeCICP(SkNamedPrimaries::CicpId color_primaries, |
| 162 |
SkNamedTransferFn::CicpId transfer_characteristics) { |
| 163 |
skcms_TransferFunction trfn; |
| 164 |
if (!SkNamedTransferFn::GetCicp(transfer_characteristics, trfn)) { |
| 165 |
return nullptr; |
| 166 |
} |
| 167 |
|
| 168 |
skcms_Matrix3x3 toXYZD50; |
| 169 |
if (!SkNamedPrimaries::GetCicp(color_primaries, toXYZD50)) { |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
return SkColorSpace::MakeRGB(trfn, toXYZD50); |
| 174 |
} |
| 175 |
|
| 176 |
class SkColorSpaceSingletonFactory { |
| 177 |
public: |
| 178 |
static SkColorSpace* Make(const skcms_TransferFunction& transferFn, |
| 179 |
const skcms_Matrix3x3& to_xyz) { |
| 180 |
return new SkColorSpace(transferFn, to_xyz); |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
SkColorSpace* sk_srgb_singleton() { |
| 185 |
static SkColorSpace* cs = SkColorSpaceSingletonFactory::Make(SkNamedTransferFn::kSRGB, |
| 186 |
SkNamedGamut::kSRGB); |
| 187 |
return cs; |
| 188 |
} |
| 189 |
|
| 190 |
SkColorSpace* sk_srgb_linear_singleton() { |
| 191 |
static SkColorSpace* cs = SkColorSpaceSingletonFactory::Make(SkNamedTransferFn::kLinear, |
| 192 |
SkNamedGamut::kSRGB); |
| 193 |
return cs; |
| 194 |
} |
| 195 |
|
| 196 |
sk_sp<SkColorSpace> SkColorSpace::MakeSRGB() { |
| 197 |
return sk_ref_sp(sk_srgb_singleton()); |
| 198 |
} |
| 199 |
|
| 200 |
sk_sp<SkColorSpace> SkColorSpace::MakeSRGBLinear() { |
| 201 |
return sk_ref_sp(sk_srgb_linear_singleton()); |
| 202 |
} |
| 203 |
|
| 204 |
void SkColorSpace::computeLazyDstFields() const { |
| 205 |
fLazyDstFieldsOnce([this] { |
| 206 |
|
| 207 |
// Invert 3x3 gamut, defaulting to sRGB if we can't. |
| 208 |
{ |
| 209 |
if (!skcms_Matrix3x3_invert(&fToXYZD50, &fFromXYZD50)) { |
| 210 |
SkAssertResult(skcms_Matrix3x3_invert(&skcms_sRGB_profile()->toXYZD50, |
| 211 |
&fFromXYZD50)); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Invert transfer function, defaulting to sRGB if we can't. |
| 216 |
{ |
| 217 |
if (!skcms_TransferFunction_invert(&fTransferFn, &fInvTransferFn)) { |
| 218 |
fInvTransferFn = *skcms_sRGB_Inverse_TransferFunction(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
}); |
| 223 |
} |
| 224 |
|
| 225 |
bool SkColorSpace::isNumericalTransferFn(skcms_TransferFunction* coeffs) const { |
| 226 |
// TODO: Change transferFn/invTransferFn to just operate on skcms_TransferFunction (all callers |
| 227 |
// already pass pointers to an skcms struct). Then remove this function, and update the two |
| 228 |
// remaining callers to do the right thing with transferFn and classify. |
| 229 |
this->transferFn(coeffs); |
| 230 |
return skcms_TransferFunction_getType(coeffs) == skcms_TFType_sRGBish; |
| 231 |
} |
| 232 |
|
| 233 |
void SkColorSpace::transferFn(float gabcdef[7]) const { |
| 234 |
memcpy(gabcdef, &fTransferFn, 7*sizeof(float)); |
| 235 |
} |
| 236 |
|
| 237 |
void SkColorSpace::transferFn(skcms_TransferFunction* fn) const { |
| 238 |
*fn = fTransferFn; |
| 239 |
} |
| 240 |
|
| 241 |
void SkColorSpace::invTransferFn(skcms_TransferFunction* fn) const { |
| 242 |
this->computeLazyDstFields(); |
| 243 |
*fn = fInvTransferFn; |
| 244 |
} |
| 245 |
|
| 246 |
bool SkColorSpace::toXYZD50(skcms_Matrix3x3* toXYZD50) const { |
| 247 |
*toXYZD50 = fToXYZD50; |
| 248 |
return true; |
| 249 |
} |
| 250 |
|
| 251 |
void SkColorSpace::gamutTransformTo(const SkColorSpace* dst, skcms_Matrix3x3* src_to_dst) const { |
| 252 |
dst->computeLazyDstFields(); |
| 253 |
*src_to_dst = skcms_Matrix3x3_concat(&dst->fFromXYZD50, &fToXYZD50); |
| 254 |
} |
| 255 |
|
| 256 |
bool SkColorSpace::isSRGB() const { |
| 257 |
return sk_srgb_singleton() == this; |
| 258 |
} |
| 259 |
|
| 260 |
bool SkColorSpace::gammaCloseToSRGB() const { |
| 261 |
// Nearly-equal transfer functions were snapped at construction time, so just do an exact test |
| 262 |
return memcmp(&fTransferFn, &SkNamedTransferFn::kSRGB, 7*sizeof(float)) == 0; |
| 263 |
} |
| 264 |
|
| 265 |
bool SkColorSpace::gammaIsLinear() const { |
| 266 |
// Nearly-equal transfer functions were snapped at construction time, so just do an exact test |
| 267 |
return memcmp(&fTransferFn, &SkNamedTransferFn::kLinear, 7*sizeof(float)) == 0; |
| 268 |
} |
| 269 |
|
| 270 |
sk_sp<SkColorSpace> SkColorSpace::makeLinearGamma() const { |
| 271 |
if (this->gammaIsLinear()) { |
| 272 |
return sk_ref_sp(const_cast<SkColorSpace*>(this)); |
| 273 |
} |
| 274 |
return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, fToXYZD50); |
| 275 |
} |
| 276 |
|
| 277 |
sk_sp<SkColorSpace> SkColorSpace::makeSRGBGamma() const { |
| 278 |
if (this->gammaCloseToSRGB()) { |
| 279 |
return sk_ref_sp(const_cast<SkColorSpace*>(this)); |
| 280 |
} |
| 281 |
return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, fToXYZD50); |
| 282 |
} |
| 283 |
|
| 284 |
sk_sp<SkColorSpace> SkColorSpace::makeColorSpin() const { |
| 285 |
skcms_Matrix3x3 spin = {{ |
| 286 |
{ 0, 0, 1 }, |
| 287 |
{ 1, 0, 0 }, |
| 288 |
{ 0, 1, 0 }, |
| 289 |
}}; |
| 290 |
|
| 291 |
skcms_Matrix3x3 spun = skcms_Matrix3x3_concat(&fToXYZD50, &spin); |
| 292 |
|
| 293 |
return sk_sp<SkColorSpace>(new SkColorSpace(fTransferFn, spun)); |
| 294 |
} |
| 295 |
|
| 296 |
void SkColorSpace::toProfile(skcms_ICCProfile* profile) const { |
| 297 |
skcms_Init (profile); |
| 298 |
// TODO(https://issues.skia.org/issues/420956739): This value should only be |
| 299 |
// set for sRGB-ish transfer functions. All other values are invalid. |
| 300 |
skcms_SetTransferFunction(profile, &fTransferFn); |
| 301 |
skcms_SetXYZD50 (profile, &fToXYZD50); |
| 302 |
|
| 303 |
switch (skcms_TransferFunction_getType(&fTransferFn)) { |
| 304 |
case skcms_TFType_PQ: |
| 305 |
case skcms_TFType_PQish: |
| 306 |
profile->has_CICP = true; |
| 307 |
profile->CICP.transfer_characteristics = |
| 308 |
static_cast<uint8_t>(SkNamedTransferFn::CicpId::kPQ); |
| 309 |
break; |
| 310 |
case skcms_TFType_HLG: |
| 311 |
case skcms_TFType_HLGish: |
| 312 |
profile->has_CICP = true; |
| 313 |
profile->CICP.transfer_characteristics = |
| 314 |
static_cast<uint8_t>(SkNamedTransferFn::CicpId::kHLG); |
| 315 |
break; |
| 316 |
default: |
| 317 |
break; |
| 318 |
} |
| 319 |
if (profile->has_CICP) { |
| 320 |
profile->CICP.matrix_coefficients = 0; |
| 321 |
profile->CICP.video_full_range_flag = 1; |
| 322 |
SkNamedPrimaries::CicpId primaries_id = SkNamedPrimaries::CicpId::kRec709; |
| 323 |
if (SkNamedPrimaries::GetCicpFromMatrix(fToXYZD50, primaries_id)) { |
| 324 |
profile->CICP.color_primaries = static_cast<uint8_t>(primaries_id); |
| 325 |
} else { |
| 326 |
profile->CICP.color_primaries = SkNamedPrimaries::kCicpIdApplicationDefined; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
sk_sp<SkColorSpace> SkColorSpace::Make(const skcms_ICCProfile& profile) { |
| 332 |
// The CICP values are only valid for full-range, with no matrix. |
| 333 |
bool use_cicp = profile.has_CICP && |
| 334 |
profile.CICP.matrix_coefficients == 0 && |
| 335 |
profile.CICP.video_full_range_flag == 1; |
| 336 |
auto cicp_color_primaries = static_cast<SkNamedPrimaries::CicpId>(profile.CICP.color_primaries); |
| 337 |
auto cicp_transfer_characteristics = |
| 338 |
static_cast<SkNamedTransferFn::CicpId>(profile.CICP.transfer_characteristics); |
| 339 |
|
| 340 |
// Early checks for exact sRGB matches. |
| 341 |
if (use_cicp && |
| 342 |
cicp_color_primaries == SkNamedPrimaries::CicpId::kRec709 && |
| 343 |
cicp_transfer_characteristics == SkNamedTransferFn::CicpId::kIEC61966_2_4) { |
| 344 |
return SkColorSpace::MakeSRGB(); |
| 345 |
} else if (skcms_ApproximatelyEqualProfiles(&profile, skcms_sRGB_profile())) { |
| 346 |
return SkColorSpace::MakeSRGB(); |
| 347 |
} |
| 348 |
|
| 349 |
// Set the toXYZD50 matrix, preferring CICP over the matrix itself. |
| 350 |
skcms_Matrix3x3 toXYZD50; |
| 351 |
bool hasSetToXYZD50 = false; |
| 352 |
if (use_cicp) { |
| 353 |
if (SkNamedPrimaries::GetCicp(cicp_color_primaries, toXYZD50)) { |
| 354 |
hasSetToXYZD50 = true; |
| 355 |
} else if (profile.CICP.color_primaries != SkNamedPrimaries::kCicpIdApplicationDefined) { |
| 356 |
return nullptr; |
| 357 |
} |
| 358 |
} |
| 359 |
if (profile.has_toXYZD50 && !hasSetToXYZD50) { |
| 360 |
// TODO: can we save this work and skip lazily inverting the matrix later? |
| 361 |
skcms_Matrix3x3 inv; |
| 362 |
toXYZD50 = profile.toXYZD50; |
| 363 |
if (skcms_Matrix3x3_invert(&toXYZD50, &inv)) { |
| 364 |
hasSetToXYZD50 = true; |
| 365 |
} |
| 366 |
} |
| 367 |
if (!hasSetToXYZD50) { |
| 368 |
return nullptr; |
| 369 |
} |
| 370 |
|
| 371 |
// Set the transfer function, preferring CICP over the curves. |
| 372 |
skcms_TransferFunction trfn; |
| 373 |
bool hasSetTrfn = false; |
| 374 |
if (use_cicp) { |
| 375 |
if (SkNamedTransferFn::GetCicp(cicp_transfer_characteristics, trfn)) { |
| 376 |
hasSetTrfn = true; |
| 377 |
} else if (profile.CICP.transfer_characteristics != |
| 378 |
SkNamedTransferFn::kCicpIdApplicationDefined) { |
| 379 |
return nullptr; |
| 380 |
} |
| 381 |
} |
| 382 |
if (profile.has_trc && !hasSetTrfn) { |
| 383 |
// We can't work with tables or mismatched parametric curves. |
| 384 |
const skcms_Curve* trc = profile.trc; |
| 385 |
if (trc[0].table_entries == 0 && |
| 386 |
trc[1].table_entries == 0 && |
| 387 |
trc[2].table_entries == 0 && |
| 388 |
0 == memcmp(&trc[0].parametric, &trc[1].parametric, sizeof(trc[0].parametric)) && |
| 389 |
0 == memcmp(&trc[0].parametric, &trc[2].parametric, sizeof(trc[0].parametric))) |
| 390 |
{ |
| 391 |
trfn = profile.trc[0].parametric; |
| 392 |
hasSetTrfn = true; |
| 393 |
} else { |
| 394 |
// If all curves look close enough to sRGB, that's fine. |
| 395 |
// TODO: should we maybe do this unconditionally to snap near-sRGB parametrics to sRGB? |
| 396 |
if (skcms_TRCs_AreApproximateInverse(&profile, skcms_sRGB_Inverse_TransferFunction())) { |
| 397 |
trfn = SkNamedTransferFn::kSRGB; |
| 398 |
hasSetTrfn = true; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
if (!hasSetTrfn) { |
| 403 |
return nullptr; |
| 404 |
} |
| 405 |
|
| 406 |
return SkColorSpace::MakeRGB(trfn, toXYZD50); |
| 407 |
} |
| 408 |
|
| 409 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 410 |
|
| 411 |
enum Version { |
| 412 |
k0_Version, // Initial (deprecated) version, no longer supported |
| 413 |
k1_Version, // Simple header (version tag) + 16 floats |
| 414 |
|
| 415 |
kCurrent_Version = k1_Version, |
| 416 |
}; |
| 417 |
|
| 418 |
struct ColorSpaceHeader { |
| 419 |
uint8_t fVersion = kCurrent_Version; |
| 420 |
|
| 421 |
// Other fields were only used by k0_Version. Could be re-purposed in future versions. |
| 422 |
uint8_t fReserved0 = 0; |
| 423 |
uint8_t fReserved1 = 0; |
| 424 |
uint8_t fReserved2 = 0; |
| 425 |
}; |
| 426 |
|
| 427 |
size_t SkColorSpace::writeToMemory(void* memory) const { |
| 428 |
if (memory) { |
| 429 |
*((ColorSpaceHeader*) memory) = ColorSpaceHeader(); |
| 430 |
memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader)); |
| 431 |
|
| 432 |
memcpy(memory, &fTransferFn, 7 * sizeof(float)); |
| 433 |
memory = SkTAddOffset<void>(memory, 7 * sizeof(float)); |
| 434 |
|
| 435 |
memcpy(memory, &fToXYZD50, 9 * sizeof(float)); |
| 436 |
} |
| 437 |
|
| 438 |
return sizeof(ColorSpaceHeader) + 16 * sizeof(float); |
| 439 |
} |
| 440 |
|
| 441 |
sk_sp<SkData> SkColorSpace::serialize() const { |
| 442 |
sk_sp<SkData> data = SkData::MakeUninitialized(this->writeToMemory(nullptr)); |
| 443 |
this->writeToMemory(data->writable_data()); |
| 444 |
return data; |
| 445 |
} |
| 446 |
|
| 447 |
sk_sp<SkColorSpace> SkColorSpace::Deserialize(const void* data, size_t length) { |
| 448 |
if (length < sizeof(ColorSpaceHeader)) { |
| 449 |
return nullptr; |
| 450 |
} |
| 451 |
|
| 452 |
ColorSpaceHeader header = *((const ColorSpaceHeader*) data); |
| 453 |
data = SkTAddOffset<const void>(data, sizeof(ColorSpaceHeader)); |
| 454 |
length -= sizeof(ColorSpaceHeader); |
| 455 |
if (header.fVersion != k1_Version) { |
| 456 |
return nullptr; |
| 457 |
} |
| 458 |
|
| 459 |
if (length < 16 * sizeof(float)) { |
| 460 |
return nullptr; |
| 461 |
} |
| 462 |
|
| 463 |
skcms_TransferFunction transferFn; |
| 464 |
memcpy(&transferFn, data, 7 * sizeof(float)); |
| 465 |
data = SkTAddOffset<const void>(data, 7 * sizeof(float)); |
| 466 |
|
| 467 |
skcms_Matrix3x3 toXYZ; |
| 468 |
memcpy(&toXYZ, data, 9 * sizeof(float)); |
| 469 |
return SkColorSpace::MakeRGB(transferFn, toXYZ); |
| 470 |
} |
| 471 |
|
| 472 |
bool SkColorSpace::Equals(const SkColorSpace* x, const SkColorSpace* y) { |
| 473 |
if (x == y) { |
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
if (!x || !y) { |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
if (x->hash() == y->hash()) { |
| 482 |
#if defined(SK_DEBUG) |
| 483 |
// Do these floats function equivalently? |
| 484 |
// This returns true more often than simple float comparison (NaN vs. NaN) and, |
| 485 |
// also returns true more often than simple bitwise comparison (+0 vs. -0) and, |
| 486 |
// even returns true more often than those two OR'd together (two different NaNs). |
| 487 |
auto equiv = [](float X, float Y) { |
| 488 |
return (X==Y) |
| 489 |
|| (std::isnan(X) && std::isnan(Y)); |
| 490 |
}; |
| 491 |
|
| 492 |
for (int i = 0; i < 7; i++) { |
| 493 |
float X = (&x->fTransferFn.g)[i], |
| 494 |
Y = (&y->fTransferFn.g)[i]; |
| 495 |
SkASSERTF(equiv(X,Y), "Hash collision at tf[%d], !equiv(%g,%g)\n", i, X,Y); |
| 496 |
} |
| 497 |
for (int r = 0; r < 3; r++) |
| 498 |
for (int c = 0; c < 3; c++) { |
| 499 |
float X = x->fToXYZD50.vals[r][c], |
| 500 |
Y = y->fToXYZD50.vals[r][c]; |
| 501 |
SkASSERTF(equiv(X,Y), "Hash collision at toXYZD50[%d][%d], !equiv(%g,%g)\n", r,c, X,Y); |
| 502 |
} |
| 503 |
#endif |
| 504 |
return true; |
| 505 |
} |
| 506 |
return false; |
| 507 |
} |