Make sure NaNs clamp to 0 in color look up tables
authorMatt Sarett <msarett@google.com>
Tue, 11 Apr 2017 13:26:24 +0000 (09:26 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Tue, 11 Apr 2017 14:03:48 +0000 (14:03 +0000)
This operation assumes 0-1 input and guarantees 0-1 output.
The old clamp was poorly written, causing the possibility
for NaNs to not be set to 0.

BUG=709941
Change-Id: I691f0494a562a329967f5b0149a1ba04cbeb8464
Reviewed-on: https://skia-review.googlesource.com/13134
Commit-Queue: Matt Sarett <msarett@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
Reviewed-by: Mike Klein <mtklein@chromium.org>
src/core/SkColorLookUpTable.cpp
src/core/SkColorSpaceXformPriv.h

index 8a55018..b8bb346 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "SkColorLookUpTable.h"
+#include "SkColorSpaceXformPriv.h"
 #include "SkFloatingPoint.h"
 
 void SkColorLookUpTable::interp(float* dst, const float* src) const {
@@ -114,11 +115,7 @@ void SkColorLookUpTable::interp3D(float* dst, const float* src) const {
         // look up tables, we don't check for it.
         // And for arbitrary, non-increasing tables, it is easy to see how
         // the output might not be 0-1.  So we clamp here.
-        if (dst[i] > 1.f) {
-            dst[i] = 1.f;
-        } else if (dst[i] < 0.f) {
-            dst[i] = 0.f;
-        }
+        dst[i] = clamp_0_1(dst[i]);
 
         // Increment the table ptr in order to handle the next component.
         // Note that this is the how table is designed: all of nXXX
@@ -158,5 +155,5 @@ float SkColorLookUpTable::interpDimension(const float* src, int inputDimension,
     // and recursively LERP all sub-dimensions with the current dimension fixed to the high point
     const float hi = interpDimension(src, inputDimension - 1, outputDimension, index);
     // then LERP the results based on the current dimension
-    return (1 - diff) * lo + diff * hi;
+    return clamp_0_1((1 - diff) * lo + diff * hi);
 }
index c020a0f..405b1da 100644 (file)
@@ -39,6 +39,8 @@ static inline uint8_t clamp_normalized_float_to_byte(float v) {
 }
 
 static inline float clamp_0_1(float v) {
+    // The ordering of the logic is a little strange here in order
+    // to make sure we convert NaNs to 0.
     if (v >= 1.0f) {
         return 1.0f;
     } else if (v >= 0.0f) {