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

Unified Diff: src/core/SkColorSpace.cpp

Issue 2043803002: Gamma sanity checks (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 6 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 | « no previous file | src/core/SkColorSpace_Base.h » ('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 bf99b474a21f4c5e9da72056f07413f48bcfe26f..c28416efd4e93804dfaaaf9cf49f23432dc0edb9 100644
--- a/src/core/SkColorSpace.cpp
+++ b/src/core/SkColorSpace.cpp
@@ -427,6 +427,10 @@ bool load_gammas(SkGammaCurve* gammas, uint32_t numGammas, const uint8_t* src, s
// The table entry is the gamma (with a bias of 256).
uint16_t value = read_big_endian_short((const uint8_t*) table);
gammas[i].fValue = value / 256.0f;
+ if (0.0f == gammas[i].fValue) {
+ SkColorSpacePrintf("Cannot have zero gamma value");
+ return false;
+ }
SkColorSpacePrintf("gamma %d %g\n", value, gammas[i].fValue);
break;
}
@@ -517,6 +521,10 @@ bool load_gammas(SkGammaCurve* gammas, uint32_t numGammas, const uint8_t* src, s
// Y = 0 otherwise
g = SkFixedToFloat(read_big_endian_int(src + 12));
a = SkFixedToFloat(read_big_endian_int(src + 16));
+ if (0.0f == a) {
+ return false;
+ }
+
b = SkFixedToFloat(read_big_endian_int(src + 20));
d = -b / a;
break;
@@ -532,6 +540,10 @@ bool load_gammas(SkGammaCurve* gammas, uint32_t numGammas, const uint8_t* src, s
// Y = c otherwise
g = SkFixedToFloat(read_big_endian_int(src + 12));
a = SkFixedToFloat(read_big_endian_int(src + 16));
+ if (0.0f == a) {
+ return false;
+ }
+
b = SkFixedToFloat(read_big_endian_int(src + 20));
c = SkFixedToFloat(read_big_endian_int(src + 24));
d = -b / a;
@@ -586,6 +598,27 @@ bool load_gammas(SkGammaCurve* gammas, uint32_t numGammas, const uint8_t* src, s
color_space_almost_equal(2.4000f, g)) {
gammas[i].fValue = 2.2f;
} else {
+ // Fail on invalid gammas.
+ if (d <= 0.0f) {
+ // Y = (aX + b)^g + c for always
+ if (0.0f == a || 0.0f == g) {
+ SkColorSpacePrintf("A or G is zero, constant gamma function "
+ "is nonsense");
+ return false;
+ }
+ } else if (d >= 1.0f) {
+ // Y = eX + f for always
+ if (0.0f == e) {
+ SkColorSpacePrintf("E is zero, constant gamma function is "
+ "nonsense");
+ return false;
+ }
+ } else if ((0.0f == a || 0.0f == g) && 0.0f == e) {
+ SkColorSpacePrintf("A or G, and E are zero, constant gamma function "
+ "is nonsense");
+ return false;
+ }
+
gammas[i].fG = g;
gammas[i].fA = a;
gammas[i].fB = b;
@@ -603,6 +636,9 @@ bool load_gammas(SkGammaCurve* gammas, uint32_t numGammas, const uint8_t* src, s
return false;
}
+ // Ensure that we have successfully read a gamma representation.
+ SkASSERT(gammas[i].isValue() || gammas[i].isTable() || gammas[i].isParametric());
+
// Adjust src and len if there is another gamma curve to load.
if (i != numGammas - 1) {
// Each curve is padded to 4-byte alignment.
« no previous file with comments | « no previous file | src/core/SkColorSpace_Base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698