Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(809)

Unified Diff: src/core/SkColorSpace.cpp

Issue 1945693002: Create a single, unique pointer to sRGB color space (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase + Test Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkColorSpace.cpp
diff --git a/src/core/SkColorSpace.cpp b/src/core/SkColorSpace.cpp
index f7e1ddb88f8108a4e134dcc2b6612f9637bc4aa9..55ab4c177a57f77a57e09989f6ea584436262d54 100644
--- a/src/core/SkColorSpace.cpp
+++ b/src/core/SkColorSpace.cpp
@@ -7,6 +7,11 @@
#include "SkAtomics.h"
#include "SkColorSpace.h"
+#include "SkOncePtr.h"
+
+static bool color_space_almost_equal(float a, float b) {
+ return SkTAbs(a - b) < 0.01f;
+}
void SkFloat3::dump() const {
SkDebugf("[%7.4f %7.4f %7.4f]\n", fVec[0], fVec[1], fVec[2]);
@@ -32,23 +37,50 @@ SkColorSpace::SkColorSpace(SkColorLookUpTable colorLUT, SkGammas gammas,
, fNamed(kUnknown_Named)
{}
-sk_sp<SkColorSpace> SkColorSpace::NewRGB(const SkMatrix44& toXYZD50, SkGammas gammas) {
- return sk_sp<SkColorSpace>(new SkColorSpace(std::move(gammas), toXYZD50, kUnknown_Named));
-}
-
const float gSRGB_toXYZD50[] {
0.4358f, 0.2224f, 0.0139f, // * R
0.3853f, 0.7170f, 0.0971f, // * G
0.1430f, 0.0606f, 0.7139f, // * B
};
+SK_DECLARE_STATIC_ONCE_PTR(SkColorSpace, sRGB);
+
+sk_sp<SkColorSpace> SkColorSpace::NewRGB(SkGammas gammas, const SkMatrix44& toXYZD50) {
+ // Check if we really have sRGB
+ if (color_space_almost_equal(2.2f, gammas.fRed.fValue) &&
+ color_space_almost_equal(2.2f, gammas.fGreen.fValue) &&
+ color_space_almost_equal(2.2f, gammas.fBlue.fValue) &&
+ color_space_almost_equal(toXYZD50.getFloat(0, 0), gSRGB_toXYZD50[0]) &&
+ color_space_almost_equal(toXYZD50.getFloat(0, 1), gSRGB_toXYZD50[1]) &&
+ color_space_almost_equal(toXYZD50.getFloat(0, 2), gSRGB_toXYZD50[2]) &&
+ color_space_almost_equal(toXYZD50.getFloat(1, 0), gSRGB_toXYZD50[3]) &&
+ color_space_almost_equal(toXYZD50.getFloat(1, 1), gSRGB_toXYZD50[4]) &&
+ color_space_almost_equal(toXYZD50.getFloat(1, 2), gSRGB_toXYZD50[5]) &&
+ color_space_almost_equal(toXYZD50.getFloat(2, 0), gSRGB_toXYZD50[6]) &&
+ color_space_almost_equal(toXYZD50.getFloat(2, 1), gSRGB_toXYZD50[7]) &&
+ color_space_almost_equal(toXYZD50.getFloat(2, 2), gSRGB_toXYZD50[8]) &&
+ color_space_almost_equal(toXYZD50.getFloat(0, 3), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(1, 3), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(2, 3), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(3, 0), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(3, 1), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(3, 2), 0.0f) &&
+ color_space_almost_equal(toXYZD50.getFloat(3, 3), 1.0f))
+ {
+ return SkColorSpace::NewNamed(kSRGB_Named);
+ }
+
+ return sk_sp<SkColorSpace>(new SkColorSpace(std::move(gammas), toXYZD50, kUnknown_Named));
+}
+
sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) {
switch (named) {
case kSRGB_Named: {
SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor);
srgbToxyzD50.set3x3ColMajorf(gSRGB_toXYZD50);
- return sk_sp<SkColorSpace>(new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2f), srgbToxyzD50,
- kSRGB_Named));
+ return sk_ref_sp(sRGB.get([=]{
+ return new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2f), srgbToxyzD50, kSRGB_Named);
+ }));
}
default:
break;
@@ -89,10 +121,6 @@ static int32_t read_big_endian_int(const uint8_t* ptr) {
return (int32_t) read_big_endian_uint(ptr);
}
-static bool color_space_almost_equal(float a, float b) {
- return SkTAbs(a - b) < 0.01f;
-}
-
// This is equal to the header size according to the ICC specification (128)
// plus the size of the tag count (4). We include the tag count since we
// always require it to be present anyway.
@@ -577,9 +605,10 @@ sk_sp<SkColorSpace> SkColorSpace::NewICC(const void* base, size_t len) {
b->addr((const uint8_t*) base), b->fLength)) {
SkColorSpacePrintf("Failed to read B gamma tag.\n");
}
+
SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor);
mat.set3x3ColMajorf(toXYZ);
- return SkColorSpace::NewRGB(mat, std::move(gammas));
+ return SkColorSpace::NewRGB(std::move(gammas), mat);
}
// Recognize color profile specified by A2B0 tag.
@@ -593,6 +622,12 @@ sk_sp<SkColorSpace> SkColorSpace::NewICC(const void* base, size_t len) {
return_null("Failed to parse A2B0 tag");
}
+ // If there is no colorLUT, use NewRGB. This allows us to check if the
+ // profile is sRGB.
+ if (!colorLUT.fTable) {
+ return SkColorSpace::NewRGB(std::move(gammas), toXYZ);
+ }
+
return sk_sp<SkColorSpace>(new SkColorSpace(std::move(colorLUT), std::move(gammas),
toXYZ));
}
« no previous file with comments | « src/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698