• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkScalar.h"
9 
SkScalarInterpFunc(SkScalar searchKey,const SkScalar keys[],const SkScalar values[],int length)10 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
11                             const SkScalar values[], int length) {
12     SkASSERT(length > 0);
13     SkASSERT(keys != nullptr);
14     SkASSERT(values != nullptr);
15 #ifdef SK_DEBUG
16     for (int i = 1; i < length; i++) {
17         SkASSERT(keys[i-1] <= keys[i]);
18     }
19 #endif
20     int right = 0;
21     while (right < length && keys[right] < searchKey) {
22         ++right;
23     }
24     // Could use sentinel values to eliminate conditionals, but since the
25     // tables are taken as input, a simpler format is better.
26     if (right == length) {
27         return values[length-1];
28     }
29     if (right == 0) {
30         return values[0];
31     }
32     // Otherwise, interpolate between right - 1 and right.
33     SkScalar leftKey = keys[right-1];
34     SkScalar rightKey = keys[right];
35     SkScalar fract = (searchKey - leftKey) / (rightKey - leftKey);
36     return SkScalarInterp(values[right-1], values[right], fract);
37 }
38