SkScalar.h revision 077910e20cda41d7981084fbd047a108894bc8df
1/* 2 * Copyright (C) 2006 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#ifndef SkScalar_DEFINED 18#define SkScalar_DEFINED 19 20#include "SkFixed.h" 21 22/** \file SkScalar.h 23 24 Types and macros for the data type SkScalar. This is the fractional numeric type 25 that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented 26 either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written 27 to allow the calling code to manipulate SkScalar values without knowing which representation 28 is in effect. 29*/ 30 31#ifdef SK_SCALAR_IS_FLOAT 32 #include "SkFloatingPoint.h" 33 34 /** SkScalar is our type for fractional values and coordinates. Depending on 35 compile configurations, it is either represented as an IEEE float, or 36 as a 16.16 fixed point integer. 37 */ 38 typedef float SkScalar; 39 extern const uint32_t gIEEENotANumber; 40 extern const uint32_t gIEEEInfinity; 41 42 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar 43 */ 44 #define SK_Scalar1 (1.0f) 45 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar 46 */ 47 #define SK_ScalarHalf (0.5f) 48 /** SK_ScalarInfinity is defined to be infinity as an SkScalar 49 */ 50 #define SK_ScalarInfinity (*(const float*)&gIEEEInfinity) 51 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar 52 */ 53 #define SK_ScalarMax (3.4028235e+38f) 54 /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar 55 */ 56 #define SK_ScalarMin (1.1754944e-38f) 57 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar 58 */ 59 #define SK_ScalarNaN (*(const float*)(const void*)&gIEEENotANumber) 60 /** SkScalarIsNaN(n) returns true if argument is not a number 61 */ 62 static inline bool SkScalarIsNaN(float x) { return x != x; } 63 /** Returns true if x is not NaN and not infinite */ 64 static inline bool SkScalarIsFinite(float x) { 65 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts 66 int exponent = bits << 1 >> 24; 67 return exponent != 0xFF; 68 } 69 /** SkIntToScalar(n) returns its integer argument as an SkScalar 70 */ 71 #define SkIntToScalar(n) ((float)(n)) 72 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar 73 */ 74 #define SkFixedToScalar(x) SkFixedToFloat(x) 75 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed 76 */ 77 #define SkScalarToFixed(x) SkFloatToFixed(x) 78 79 #define SkScalarToFloat(n) (n) 80 #define SkFloatToScalar(n) (n) 81 82 #define SkScalarToDouble(n) (double)(n) 83 #define SkDoubleToScalar(n) (float)(n) 84 85 /** SkScalarFraction(x) returns the signed fractional part of the argument 86 */ 87 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) 88 /** Rounds the SkScalar to the nearest integer value 89 */ 90 #define SkScalarRound(x) sk_float_round2int(x) 91 /** Returns the smallest integer that is >= the specified SkScalar 92 */ 93 #define SkScalarCeil(x) sk_float_ceil2int(x) 94 /** Returns the largest integer that is <= the specified SkScalar 95 */ 96 #define SkScalarFloor(x) sk_float_floor2int(x) 97 /** Returns the absolute value of the specified SkScalar 98 */ 99 #define SkScalarAbs(x) sk_float_abs(x) 100 /** Return x with the sign of y 101 */ 102 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) 103 /** Returns the value pinned between 0 and max inclusive 104 */ 105 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 106 return x < 0 ? 0 : x > max ? max : x; 107 } 108 /** Returns the value pinned between min and max inclusive 109 */ 110 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 111 return x < min ? min : x > max ? max : x; 112 } 113 /** Returns the specified SkScalar squared (x*x) 114 */ 115 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 116 /** Returns the product of two SkScalars 117 */ 118 #define SkScalarMul(a, b) ((float)(a) * (b)) 119 /** Returns the product of two SkScalars plus a third SkScalar 120 */ 121 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 122 /** Returns the product of a SkScalar and an int rounded to the nearest integer value 123 */ 124 #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b)) 125 /** Returns the product of a SkScalar and an int promoted to the next larger int 126 */ 127 #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b)) 128 /** Returns the product of a SkScalar and an int truncated to the next smaller int 129 */ 130 #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b)) 131 /** Returns the quotient of two SkScalars (a/b) 132 */ 133 #define SkScalarDiv(a, b) ((float)(a) / (b)) 134 /** Returns the mod of two SkScalars (a mod b) 135 */ 136 #define SkScalarMod(x,y) sk_float_mod(x,y) 137 /** Returns the product of the first two arguments, divided by the third argument 138 */ 139 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 140 /** Returns the multiplicative inverse of the SkScalar (1/x) 141 */ 142 #define SkScalarInvert(x) (SK_Scalar1 / (x)) 143 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 144 /** Returns the square root of the SkScalar 145 */ 146 #define SkScalarSqrt(x) sk_float_sqrt(x) 147 /** Returns the average of two SkScalars (a+b)/2 148 */ 149 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 150 /** Returns the geometric mean of two SkScalars 151 */ 152 #define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b)) 153 /** Returns one half of the specified SkScalar 154 */ 155 #define SkScalarHalf(a) ((a) * 0.5f) 156 157 #define SK_ScalarSqrt2 1.41421356f 158 #define SK_ScalarPI 3.14159265f 159 #define SK_ScalarTanPIOver8 0.414213562f 160 #define SK_ScalarRoot2Over2 0.707106781f 161 162 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 163 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 164 #define SkScalarSin(radians) (float)sk_float_sin(radians) 165 #define SkScalarCos(radians) (float)sk_float_cos(radians) 166 #define SkScalarTan(radians) (float)sk_float_tan(radians) 167 #define SkScalarASin(val) (float)sk_float_asin(val) 168 #define SkScalarACos(val) (float)sk_float_acos(val) 169 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 170 #define SkScalarExp(x) (float)sk_float_exp(x) 171 #define SkScalarLog(x) (float)sk_float_log(x) 172 173 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 174 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 175 176#else 177 typedef SkFixed SkScalar; 178 179 #define SK_Scalar1 SK_Fixed1 180 #define SK_ScalarHalf SK_FixedHalf 181 #define SK_ScalarInfinity SK_FixedMax 182 #define SK_ScalarMax SK_FixedMax 183 #define SK_ScalarMin SK_FixedMin 184 #define SK_ScalarNaN SK_FixedNaN 185 #define SkScalarIsNaN(x) ((x) == SK_FixedNaN) 186 #define SkScalarIsFinite(x) ((x) != SK_FixedNaN) 187 188 #define SkIntToScalar(n) SkIntToFixed(n) 189 #define SkFixedToScalar(x) (x) 190 #define SkScalarToFixed(x) (x) 191 #ifdef SK_CAN_USE_FLOAT 192 #define SkScalarToFloat(n) SkFixedToFloat(n) 193 #define SkFloatToScalar(n) SkFloatToFixed(n) 194 195 #define SkScalarToDouble(n) SkFixedToDouble(n) 196 #define SkDoubleToScalar(n) SkDoubleToFixed(n) 197 #endif 198 #define SkScalarFraction(x) SkFixedFraction(x) 199 #define SkScalarRound(x) SkFixedRound(x) 200 #define SkScalarCeil(x) SkFixedCeil(x) 201 #define SkScalarFloor(x) SkFixedFloor(x) 202 #define SkScalarAbs(x) SkFixedAbs(x) 203 #define SkScalarCopySign(x, y) SkCopySign32(x, y) 204 #define SkScalarClampMax(x, max) SkClampMax(x, max) 205 #define SkScalarPin(x, min, max) SkPin32(x, min, max) 206 #define SkScalarSquare(x) SkFixedSquare(x) 207 #define SkScalarMul(a, b) SkFixedMul(a, b) 208 #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c) 209 #define SkScalarMulRound(a, b) SkFixedMulCommon(a, b, SK_FixedHalf) 210 #define SkScalarMulCeil(a, b) SkFixedMulCommon(a, b, SK_Fixed1 - 1) 211 #define SkScalarMulFloor(a, b) SkFixedMulCommon(a, b, 0) 212 #define SkScalarDiv(a, b) SkFixedDiv(a, b) 213 #define SkScalarMod(a, b) SkFixedMod(a, b) 214 #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c) 215 #define SkScalarInvert(x) SkFixedInvert(x) 216 #define SkScalarFastInvert(x) SkFixedFastInvert(x) 217 #define SkScalarSqrt(x) SkFixedSqrt(x) 218 #define SkScalarAve(a, b) SkFixedAve(a, b) 219 #define SkScalarMean(a, b) SkFixedMean(a, b) 220 #define SkScalarHalf(a) ((a) >> 1) 221 222 #define SK_ScalarSqrt2 SK_FixedSqrt2 223 #define SK_ScalarPI SK_FixedPI 224 #define SK_ScalarTanPIOver8 SK_FixedTanPIOver8 225 #define SK_ScalarRoot2Over2 SK_FixedRoot2Over2 226 227 #define SkDegreesToRadians(degrees) SkFractMul(degrees, SK_FractPIOver180) 228 #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr) 229 #define SkScalarSin(radians) SkFixedSin(radians) 230 #define SkScalarCos(radians) SkFixedCos(radians) 231 #define SkScalarTan(val) SkFixedTan(val) 232 #define SkScalarASin(val) SkFixedASin(val) 233 #define SkScalarACos(val) SkFixedACos(val) 234 #define SkScalarATan2(y, x) SkFixedATan2(y,x) 235 #define SkScalarExp(x) SkFixedExp(x) 236 #define SkScalarLog(x) SkFixedLog(x) 237 238 #define SkMaxScalar(a, b) SkMax32(a, b) 239 #define SkMinScalar(a, b) SkMin32(a, b) 240#endif 241 242#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 243 244/* <= is slower than < for floats, so we use < for our tolerance test 245*/ 246 247static inline bool SkScalarNearlyZero(SkScalar x, 248 SkScalar tolerance = SK_ScalarNearlyZero) { 249 SkASSERT(tolerance > 0); 250 return SkScalarAbs(x) < tolerance; 251} 252 253/** Linearly interpolate between A and B, based on t. 254 If t is 0, return A 255 If t is 1, return B 256 else interpolate. 257 t must be [0..SK_Scalar1] 258*/ 259static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 260 SkASSERT(t >= 0 && t <= SK_Scalar1); 261 return A + SkScalarMul(B - A, t); 262} 263 264/** Interpolate along the function described by (keys[length], values[length]) 265 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 266 clamp to the min or max value. This function was inspired by a desire 267 to change the multiplier for thickness in fakeBold; therefore it assumes 268 the number of pairs (length) will be small, and a linear search is used. 269 Repeated keys are allowed for discontinuous functions (so long as keys is 270 monotonically increasing), and if key is the value of a repeated scalar in 271 keys, the first one will be used. However, that may change if a binary 272 search is used. 273*/ 274SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 275 const SkScalar values[], int length); 276 277#endif 278 279