Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GNASH_AGG_LINEAR_INTERPOLATOR_H
00020 #define GNASH_AGG_LINEAR_INTERPOLATOR_H
00021
00022 #include <cmath>
00023
00024 namespace gnash {
00025
00027 double
00028 linearToSRGB(double s)
00029 {
00030 const double a = 0.055;
00031 if (s <= 0.0031308) return 12.92 * s;
00032 return (1 + a) * std::pow(s, 1 / 2.4) - a;
00033 }
00034
00035 template<typename T>
00036 T
00037 cdiff(T a, T b, double ratio)
00038 {
00039 const int diff = b - a;
00040 const double d = linearToSRGB((diff < 0) ? 1 - ratio : ratio);
00041 if (diff < 0) {
00042 return b - d * diff;
00043 }
00044 return a + d * diff;
00045 }
00046
00048
00052 template<class ColorT>
00053 struct linear_rgb_interpolator
00054 {
00055 public:
00056 typedef ColorT color_type;
00057
00058 linear_rgb_interpolator(const color_type& c1, const color_type& c2,
00059 size_t len)
00060 :
00061 _c1(c1),
00062 _c2(c2),
00063 _len(len),
00064 _count(0)
00065 {}
00066
00067 void operator++() {
00068 ++_count;
00069 }
00070
00071 color_type color() const {
00072 const double ratio = static_cast<double>(_count) / _len;
00073 return color_type(
00074 cdiff(_c1.r, _c2.r, ratio),
00075 cdiff(_c1.g, _c2.g, ratio),
00076 cdiff(_c1.b, _c2.b, ratio),
00077 _c1.a + (_c2.a - _c1.a) * ratio);
00078 }
00079
00080 private:
00081 color_type _c1;
00082 color_type _c2;
00083 size_t _len;
00084 size_t _count;
00085 };
00086
00087 }
00088
00089 #endif