Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2010, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15 */
     16 
     17 #include "SkScalar.h"
     18 
     19 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
     20                             const SkScalar values[], int length) {
     21     SkASSERT(length > 0);
     22     SkASSERT(keys != NULL);
     23     SkASSERT(values != NULL);
     24 #ifdef SK_DEBUG
     25     for (int i = 1; i < length; i++)
     26         SkASSERT(keys[i] >= keys[i-1]);
     27 #endif
     28     int right = 0;
     29     while (right < length && searchKey > keys[right])
     30         right++;
     31     // Could use sentinel values to eliminate conditionals, but since the
     32     // tables are taken as input, a simpler format is better.
     33     if (length == right)
     34         return values[length-1];
     35     if (0 == right)
     36         return values[0];
     37     // Otherwise, interpolate between right - 1 and right.
     38     SkScalar rightKey = keys[right];
     39     SkScalar leftKey = keys[right-1];
     40     SkScalar fract = SkScalarDiv(searchKey-leftKey,rightKey-leftKey);
     41     return SkScalarInterp(values[right-1], values[right], fract);
     42 }
     43