Line data Source code
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 :
9 : #include "SkMath.h"
10 : #include "SkScalar.h"
11 :
12 0 : SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
13 : const SkScalar values[], int length) {
14 0 : SkASSERT(length > 0);
15 0 : SkASSERT(keys != nullptr);
16 0 : SkASSERT(values != nullptr);
17 : #ifdef SK_DEBUG
18 0 : for (int i = 1; i < length; i++)
19 0 : SkASSERT(keys[i] >= keys[i-1]);
20 : #endif
21 0 : int right = 0;
22 0 : while (right < length && searchKey > keys[right])
23 0 : right++;
24 : // Could use sentinel values to eliminate conditionals, but since the
25 : // tables are taken as input, a simpler format is better.
26 0 : if (length == right)
27 0 : return values[length-1];
28 0 : if (0 == right)
29 0 : return values[0];
30 : // Otherwise, interpolate between right - 1 and right.
31 0 : SkScalar rightKey = keys[right];
32 0 : SkScalar leftKey = keys[right-1];
33 0 : SkScalar fract = (searchKey - leftKey) / (rightKey - leftKey);
34 0 : return SkScalarInterp(values[right-1], values[right], fract);
35 : }
|