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