lut_inverse_interp16: better handle degenerate TRC curves

Degenerate TRC have runs of 0's / 1's from the left / right edge of
the TRC curve (a curve we want to invert), but the inverse curve is
not well-defined in those regions. Return 0 or 1 for the inverse in
those regions. Also ensure the binary search parameters are bounded
to a valid search range.

BUG=458024

Review URL: https://codereview.chromium.org/1016213002

Cr-Commit-Position: refs/heads/master@{#321930}
diff --git a/third_party/qcms/README.chromium b/third_party/qcms/README.chromium
index fdd921d..1392baa5 100644
--- a/third_party/qcms/README.chromium
+++ b/third_party/qcms/README.chromium
@@ -59,5 +59,7 @@
    - https://bugzilla.mozilla.org/show_bug.cgi?id=1132468
  - lut_inverse_interp16: remove odd whitespace formatting
    - https://code.google.com/p/chromium/issues/detail?id=458024
+ - lut_inverse_interp16: better handle degenerate TRC curves
+   - https://code.google.com/p/chromium/issues/detail?id=458024
 To regenerate google.patch:
   git diff b8456f38 src > google.patch
diff --git a/third_party/qcms/src/transform_util.c b/third_party/qcms/src/transform_util.c
index f616c3f..5eeafa2 100644
--- a/third_party/qcms/src/transform_util.c
+++ b/third_party/qcms/src/transform_util.c
@@ -328,11 +328,16 @@
         // Does the curve belong to this case?
         if (NumZeroes > 1 || NumPoles > 1)
         {
-                int a, b;
+                int a, b, sample;
 
                 // Identify if value fall downto 0 or FFFF zone
                 if (Value == 0) return 0;
-               // if (Value == 0xFFFF) return 0xFFFF;
+                // if (Value == 0xFFFF) return 0xFFFF;
+                sample = (length-1) * ((double) Value * (1./65535.));
+                if (LutTable[sample] == 0)
+                    return 0;
+                if (LutTable[sample] == 0xffff)
+                    return 0xffff;
 
                 // else restrict to valid zone
 
@@ -341,8 +346,14 @@
 
                 l = a - 1;
                 r = b + 1;
-        }
 
+                // Ensure a valid binary search range
+
+                if (l < 1)
+                    l = 1;
+                if (r > 0x10000)
+                    r = 0x10000;
+        }
 
         // Seems not a degenerated case... apply binary search