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