vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 8 | |
tomhudson@google.com | c12e1b1 | 2011-09-27 18:03:23 +0000 | [diff] [blame] | 9 | #include "SkMath.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 10 | #include "SkScalar.h" |
| 11 | |
| 12 | SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], |
| 13 | const SkScalar values[], int length) { |
| 14 | SkASSERT(length > 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 15 | SkASSERT(keys != nullptr); |
| 16 | SkASSERT(values != nullptr); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 17 | #ifdef SK_DEBUG |
| 18 | for (int i = 1; i < length; i++) |
| 19 | SkASSERT(keys[i] >= keys[i-1]); |
| 20 | #endif |
| 21 | int right = 0; |
| 22 | while (right < length && searchKey > keys[right]) |
| 23 | right++; |
| 24 | // Could use sentinel values to eliminate conditionals, but since the |
| 25 | // tables are taken as input, a simpler format is better. |
| 26 | if (length == right) |
| 27 | return values[length-1]; |
| 28 | if (0 == right) |
| 29 | return values[0]; |
| 30 | // Otherwise, interpolate between right - 1 and right. |
| 31 | SkScalar rightKey = keys[right]; |
| 32 | SkScalar leftKey = keys[right-1]; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 33 | SkScalar fract = (searchKey - leftKey) / (rightKey - leftKey); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 34 | return SkScalarInterp(values[right-1], values[right], fract); |
| 35 | } |