update dev300-m58
[ooovba.git] / agg / inc / agg_span_gradient_alpha.h
blobd191c7a634a8fc9f5ff78560a6fb68fc2b041f79
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 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 //----------------------------------------------------------------------------
16 #ifndef AGG_SPAN_GRADIENT_ALPHA_INCLUDED
17 #define AGG_SPAN_GRADIENT_ALPHA_INCLUDED
19 #include "agg_span_gradient.h"
21 namespace agg
23 //======================================================span_gradient_alpha
24 template<class ColorT,
25 class Interpolator,
26 class GradientF,
27 class AlphaF>
28 class span_gradient_alpha
30 public:
31 typedef Interpolator interpolator_type;
32 typedef ColorT color_type;
33 typedef typename color_type::value_type alpha_type;
35 enum
37 downscale_shift = interpolator_type::subpixel_shift - gradient_subpixel_shift
41 //--------------------------------------------------------------------
42 span_gradient_alpha() {}
44 //--------------------------------------------------------------------
45 span_gradient_alpha(interpolator_type& inter,
46 const GradientF& gradient_function,
47 const AlphaF& alpha_function,
48 double d1, double d2) :
49 m_interpolator(&inter),
50 m_gradient_function(&gradient_function),
51 m_alpha_function(&alpha_function),
52 m_d1(int(d1 * gradient_subpixel_size)),
53 m_d2(int(d2 * gradient_subpixel_size))
56 //--------------------------------------------------------------------
57 interpolator_type& interpolator() { return *m_interpolator; }
58 const GradientF& gradient_function() const { return *m_gradient_function; }
59 const AlphaF& alpha_function() const { return *m_alpha_function; }
60 double d1() const { return double(m_d1) / gradient_subpixel_size; }
61 double d2() const { return double(m_d2) / gradient_subpixel_size; }
63 //--------------------------------------------------------------------
64 void interpolator(interpolator_type& i) { m_interpolator = &i; }
65 void gradient_function(const GradientF& gf) { m_gradient_function = &gf; }
66 void alpha_function(const AlphaF& af) { m_alpha_function = &af; }
67 void d1(double v) { m_d1 = int(v * gradient_subpixel_size); }
68 void d2(double v) { m_d2 = int(v * gradient_subpixel_size); }
70 //--------------------------------------------------------------------
71 void convert(color_type* span, int x, int y, unsigned len)
73 int dd = m_d2 - m_d1;
74 if(dd < 1) dd = 1;
75 m_interpolator->begin(x+0.5, y+0.5, len);
78 m_interpolator->coordinates(&x, &y);
79 int d = m_gradient_function->calculate(x >> downscale_shift,
80 y >> downscale_shift, dd);
81 d = ((d - m_d1) * (int)m_alpha_function->size()) / dd;
82 if(d < 0) d = 0;
83 if(d >= (int)m_alpha_function->size()) d = m_alpha_function->size() - 1;
84 span->a = (*m_alpha_function)[d];
85 ++span;
86 ++(*m_interpolator);
88 while(--len);
91 private:
92 interpolator_type* m_interpolator;
93 const GradientF* m_gradient_function;
94 const AlphaF* m_alpha_function;
95 int m_d1;
96 int m_d2;
100 //=======================================================gradient_alpha_x
101 template<class ColorT> struct gradient_alpha_x
103 typedef typename ColorT::value_type alpha_type;
104 alpha_type operator [] (alpha_type x) const { return x; }
107 //====================================================gradient_alpha_x_u8
108 struct gradient_alpha_x_u8
110 typedef int8u alpha_type;
111 alpha_type operator [] (alpha_type x) const { return x; }
114 //==========================================gradient_alpha_one_munus_x_u8
115 struct gradient_alpha_one_munus_x_u8
117 typedef int8u alpha_type;
118 alpha_type operator [] (alpha_type x) const { return 255-x; }
123 #endif