Skip to content

Commit 3f1f86c

Browse files
committed
Fix proper color profile description for sRGB profiles (fix aseprite/aseprite#5297)
We were using the get_color_profile_description() function defined in Skia m102 but after the migration to m124 we lost the function. Here we're just bringing it back from Skia m102.
1 parent 39f6dca commit 3f1f86c

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

os/skia/skia_color_space.cpp

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,91 @@
2121

2222
namespace os {
2323

24+
namespace {
25+
2426
// Copied from skia/src/core/SkColorSpacePriv.h
25-
static constexpr float gSRGB_toXYZD50[]{
27+
constexpr float gSRGB_toXYZD50[]{
2628
0.4360747f, 0.3850649f, 0.1430804f, // Rx, Gx, Bx
2729
0.2225045f, 0.7168786f, 0.0606169f, // Ry, Gy, By
2830
0.0139322f, 0.0971045f, 0.7141733f, // Rz, Gz, Bz
2931
};
3032

33+
// Code for get_color_profile_description() copied from
34+
// skia/src/core/SkICC.cpp from Skia m102
35+
36+
bool nearly_equal(float x, float y)
37+
{
38+
// A note on why I chose this tolerance: transfer_fn_almost_equal() uses a
39+
// tolerance of 0.001f, which doesn't seem to be enough to distinguish
40+
// between similar transfer functions, for example: gamma2.2 and sRGB.
41+
//
42+
// If the tolerance is 0.0f, then this we can't distinguish between two
43+
// different encodings of what is clearly the same colorspace. Some
44+
// experimentation with example files lead to this number:
45+
static constexpr float kTolerance = 1.0f / (1 << 11);
46+
return ::fabsf(x - y) <= kTolerance;
47+
}
48+
49+
bool nearly_equal(const skcms_TransferFunction& u, const skcms_TransferFunction& v)
50+
{
51+
return nearly_equal(u.g, v.g) && nearly_equal(u.a, v.a) && nearly_equal(u.b, v.b) &&
52+
nearly_equal(u.c, v.c) && nearly_equal(u.d, v.d) && nearly_equal(u.e, v.e) &&
53+
nearly_equal(u.f, v.f);
54+
}
55+
56+
bool nearly_equal(const skcms_Matrix3x3& u, const skcms_Matrix3x3& v)
57+
{
58+
for (int r = 0; r < 3; r++) {
59+
for (int c = 0; c < 3; c++) {
60+
if (!nearly_equal(u.vals[r][c], v.vals[r][c])) {
61+
return false;
62+
}
63+
}
64+
}
65+
return true;
66+
}
67+
68+
// Return nullptr if the color profile doen't have a special name.
69+
const char* get_color_profile_description(const skcms_TransferFunction& fn,
70+
const skcms_Matrix3x3& toXYZD50)
71+
{
72+
bool srgb_xfer = nearly_equal(fn, SkNamedTransferFn::kSRGB);
73+
bool srgb_gamut = nearly_equal(toXYZD50, SkNamedGamut::kSRGB);
74+
if (srgb_xfer && srgb_gamut) {
75+
return "sRGB";
76+
}
77+
bool line_xfer = nearly_equal(fn, SkNamedTransferFn::kLinear);
78+
if (line_xfer && srgb_gamut) {
79+
return "Linear Transfer with sRGB Gamut";
80+
}
81+
bool twoDotTwo = nearly_equal(fn, SkNamedTransferFn::k2Dot2);
82+
if (twoDotTwo && srgb_gamut) {
83+
return "2.2 Transfer with sRGB Gamut";
84+
}
85+
if (twoDotTwo && nearly_equal(toXYZD50, SkNamedGamut::kAdobeRGB)) {
86+
return "AdobeRGB";
87+
}
88+
bool display_p3 = nearly_equal(toXYZD50, SkNamedGamut::kDisplayP3);
89+
if (srgb_xfer || line_xfer) {
90+
if (srgb_xfer && display_p3) {
91+
return "sRGB Transfer with Display P3 Gamut";
92+
}
93+
if (line_xfer && display_p3) {
94+
return "Linear Transfer with Display P3 Gamut";
95+
}
96+
bool rec2020 = nearly_equal(toXYZD50, SkNamedGamut::kRec2020);
97+
if (srgb_xfer && rec2020) {
98+
return "sRGB Transfer with Rec-BT-2020 Gamut";
99+
}
100+
if (line_xfer && rec2020) {
101+
return "Linear Transfer with Rec-BT-2020 Gamut";
102+
}
103+
}
104+
return nullptr;
105+
}
106+
107+
} // namespace
108+
31109
SkiaColorSpace::SkiaColorSpace(const gfx::ColorSpaceRef& gfxcs) : m_gfxcs(gfxcs), m_skcs(nullptr)
32110
{
33111
switch (m_gfxcs->type()) {
@@ -109,11 +187,10 @@ SkiaColorSpace::SkiaColorSpace(const gfx::ColorSpaceRef& gfxcs) : m_gfxcs(gfxcs)
109187
skcms_TransferFunction fn;
110188
skcms_Matrix3x3 toXYZD50;
111189
if (m_skcs->isNumericalTransferFn(&fn) && m_skcs->toXYZD50(&toXYZD50)) {
112-
#if 0 // TODO create a description for the color profile
190+
// Create a description for the color profile
113191
const char* desc = get_color_profile_description(fn, toXYZD50);
114192
if (desc)
115193
m_gfxcs->setName(desc);
116-
#endif
117194
}
118195
}
119196

0 commit comments

Comments
 (0)