1 //---------------------------------------------------------------------------- 2 // Anti-Grain Geometry - Version 2.2 3 // Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com) 4 // 5 // Permission to copy, use, modify, sell and distribute this software 6 // is granted provided this copyright notice appears in all copies. 7 // This software is provided "as is" without express or implied 8 // warranty, and with no claim as to its suitability for any purpose. 9 // 10 //---------------------------------------------------------------------------- 11 // Contact: mcseem@antigrain.com 12 // mcseemagg@yahoo.com 13 // http://www.antigrain.com 14 //---------------------------------------------------------------------------- 15 16 #ifndef AGG_SPAN_GRADIENT_ALPHA_INCLUDED 17 #define AGG_SPAN_GRADIENT_ALPHA_INCLUDED 18 19 #include "agg_span_gradient.h" 20 21 namespace agg 22 { 23 //======================================================span_gradient_alpha 24 template<class ColorT, 25 class Interpolator, 26 class GradientF, 27 class AlphaF> 28 class span_gradient_alpha 29 { 30 public: 31 typedef Interpolator interpolator_type; 32 typedef ColorT color_type; 33 typedef typename color_type::alpha_type alpha_type; 34 35 enum 36 { 37 base_shift = 8, 38 base_size = 1 << base_shift, 39 base_mask = base_size - 1, 40 downscale_shift = interpolator_type::subpixel_shift - gradient_subpixel_shift 41 }; 42 43 44 //-------------------------------------------------------------------- 45 span_gradient_alpha() {} 46 47 //-------------------------------------------------------------------- 48 span_gradient_alpha(interpolator_type& inter, 49 const GradientF& gradient_function, 50 AlphaF alpha_function, 51 double d1, double d2) : 52 m_interpolator(&inter), 53 m_gradient_function(&gradient_function), 54 m_alpha_function(alpha_function), 55 m_d1(int(d1 * gradient_subpixel_size)), 56 m_d2(int(d2 * gradient_subpixel_size)) 57 {} 58 59 //-------------------------------------------------------------------- 60 interpolator_type& interpolator() { return *m_interpolator; } 61 const GradientF& gradient_function() const { return *m_gradient_function; } 62 const AlphaF alpha_function() const { return m_alpha_function; } 63 double d1() const { return double(m_d1) / gradient_subpixel_size; } 64 double d2() const { return double(m_d2) / gradient_subpixel_size; } 65 66 //-------------------------------------------------------------------- 67 void interpolator(interpolator_type& i) { m_interpolator = &i; } 68 void gradient_function(const GradientF& gf) { m_gradient_function = &gf; } 69 void alpha_function(AlphaF af) { m_alpha_function = af; } 70 void d1(double v) { m_d1 = int(v * gradient_subpixel_size); } 71 void d2(double v) { m_d2 = int(v * gradient_subpixel_size); } 72 73 //-------------------------------------------------------------------- 74 void convert(color_type* span, int x, int y, unsigned len) 75 { 76 int dd = m_d2 - m_d1; 77 if(dd < 1) dd = 1; 78 m_interpolator->begin(x+0.5, y+0.5, len); 79 do 80 { 81 m_interpolator->coordinates(&x, &y); 82 int d = m_gradient_function->calculate(x >> downscale_shift, 83 y >> downscale_shift, dd); 84 d = ((d - m_d1) << base_shift) / dd; 85 if(d < 0) d = 0; 86 if(d > base_mask) d = base_mask; 87 span->a = m_alpha_function[d]; 88 ++span; 89 ++(*m_interpolator); 90 } 91 while(--len); 92 } 93 94 private: 95 interpolator_type* m_interpolator; 96 const GradientF* m_gradient_function; 97 AlphaF m_alpha_function; 98 int m_d1; 99 int m_d2; 100 }; 101 102 103 //=======================================================gradient_alpha_x 104 template<class ColorT> struct gradient_alpha_x 105 { 106 typedef typename ColorT::alpha_type alpha_type; 107 alpha_type operator [] (alpha_type x) const { return x; } 108 }; 109 110 //====================================================gradient_alpha_x_u8 111 struct gradient_alpha_x_u8 112 { 113 typedef int8u alpha_type; 114 alpha_type operator [] (alpha_type x) const { return x; } 115 }; 116 117 //==========================================gradient_alpha_one_munus_x_u8 118 struct gradient_alpha_one_munus_x_u8 119 { 120 typedef int8u alpha_type; 121 alpha_type operator [] (alpha_type x) const { return 255-x; } 122 }; 123 124 } 125 126 #endif 127