Chromium Code Reviews| Index: src/core/SkBitmapFilter.h |
| diff --git a/src/core/SkBitmapFilter.h b/src/core/SkBitmapFilter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7ea60b81080b9e20e7a04ecce9338bd3d4d90264 |
| --- /dev/null |
| +++ b/src/core/SkBitmapFilter.h |
| @@ -0,0 +1,111 @@ |
| + |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| + |
| +#ifndef SkBitmapFilter_DEFINED |
| +#define SkBitmapFilter_DEFINED |
| + |
| +#include "SkMath.h" |
| + |
| +class SkBitmapFilter { |
| + public: |
| + SkBitmapFilter( float width ) |
| + : fWidth( width ), fInvWidth( 1.f/width ) { |
| + } |
| + |
| + float width() const { return fWidth; } |
| + float invWidth() const { return fInvWidth; } |
| + virtual float Evaluate( float x ) const = 0; |
|
reed1
2013/06/18 20:21:21
non-static methods are lowerCamelCase
humper
2013/06/19 20:49:26
Done.
|
| + protected: |
| + float fWidth; |
| + float fInvWidth; |
| +}; |
| + |
| +class SkMitchellFilter: public SkBitmapFilter { |
| + public: |
| + SkMitchellFilter( float b, float c, float width=2.0 ) |
| + : SkBitmapFilter( width ), B( b ), C( c ) { |
| + } |
| + |
| + virtual float Evaluate( float x ) const { |
|
reed1
2013/06/18 20:21:21
SK_OVERRIDE
humper
2013/06/19 20:49:26
Done.
|
| + x = fabsf(x); |
| + float ret; |
| + if (x > 2.f) { |
| + return 0; |
| + } else if (x > 1.f) { |
| + return ((-B - 6*C) * x*x*x + (6*B + 30*C) * x*x + |
| + (-12*B - 48*C) * x + (8*B + 24*C)) * (1.f/6.f); |
| + } else { |
| + return ((12 - 9*B - 6*C) * x*x*x + |
| + (-18 + 12*B + 6*C) * x*x + |
| + (6 - 2*B)) * (1.f/6.f); |
| + } |
| + } |
| + protected: |
| + float B, C; |
| +}; |
| + |
| +class SkGaussianFilter: public SkBitmapFilter { |
| + public: |
| + SkGaussianFilter( float a, float width=2.0 ) |
| + : SkBitmapFilter( width ), alpha(a), expWidth( expf(-alpha * width * width )) { |
| + } |
| + |
| + virtual float Evaluate( float x ) const { |
| + return SkTMax(0.f, float(expf(-alpha*x*x) - expWidth)); |
| + } |
| + protected: |
| + float alpha, expWidth; |
| +}; |
| + |
| +class SkTriangleFilter: public SkBitmapFilter { |
| + public: |
| + SkTriangleFilter( float width=1 ) |
| + : SkBitmapFilter( width ) { |
| + } |
| + |
| + virtual float Evaluate( float x ) const { |
| + return SkTMax( 0.f, fWidth - fabsf(x) ); |
| + } |
| + protected: |
| +}; |
| + |
| +class SkBoxFilter: public SkBitmapFilter { |
| + public: |
| + SkBoxFilter( float width=.5 ) |
|
reed1
2013/06/18 20:21:21
need .5f to avoid warning
humper
2013/06/19 20:49:26
Done.
|
| + : SkBitmapFilter( width ) { |
| + } |
| + |
| + virtual float Evaluate( float x ) const { |
| + return 1; |
| + } |
| + protected: |
| +}; |
| + |
| + |
| +class SkSincFilter: public SkBitmapFilter { |
| + public: |
| + SkSincFilter( float t, float width=3 ) |
| + : SkBitmapFilter( width ), tau(t) { |
| + } |
| + |
| + virtual float Evaluate( float x ) const { |
| + x = fabsf(x/fWidth); |
|
reed1
2013/06/18 20:21:21
sk_float_abs
reed1
2013/06/18 20:21:21
x * fInvWidth
humper
2013/06/19 20:49:26
Done.
humper
2013/06/19 20:49:26
Done.
humper
2013/06/19 20:49:26
Done.
|
| + if (x < 1e-5) return 1.f; |
|
reed1
2013/06/18 20:21:21
1e-5f for warning I think
humper
2013/06/19 20:49:26
Done.
|
| + if (x > 1.) return 0.f; |
| + x *= M_PI; |
| + float sinc = sinf(x) / x; |
|
reed1
2013/06/18 20:21:21
sk_float_sin
humper
2013/06/19 20:49:26
Done.
|
| + float lanczos = sinf(x * tau) / (x * tau); |
| + return sinc * lanczos; |
| + } |
| + protected: |
| + float tau; |
| +}; |
| + |
| + |
| +#endif |