Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_span_gradient_alpha.h
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 // Anti-Grain Geometry (AGG) - Version 2.5
00003 // A high quality rendering engine for C++
00004 // Copyright (C) 2002-2006 Maxim Shemanarev
00005 // Contact: mcseem@antigrain.com
00006 //          mcseemagg@yahoo.com
00007 //          http://antigrain.com
00008 // 
00009 // AGG is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 // 
00014 // AGG is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU General Public License
00020 // along with AGG; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00022 // MA 02110-1301, USA.
00023 //----------------------------------------------------------------------------
00024 
00025 #ifndef AGG_SPAN_GRADIENT_ALPHA_INCLUDED
00026 #define AGG_SPAN_GRADIENT_ALPHA_INCLUDED
00027 
00028 #include "agg_span_gradient.h"
00029 
00030 namespace agg
00031 {
00032     //======================================================span_gradient_alpha
00033     template<class ColorT, 
00034              class Interpolator,
00035              class GradientF, 
00036              class AlphaF>
00037     class span_gradient_alpha
00038     {
00039     public:
00040         typedef Interpolator interpolator_type;
00041         typedef ColorT color_type;
00042         typedef typename color_type::value_type alpha_type;
00043 
00044         enum downscale_shift_e
00045         {
00046             downscale_shift = interpolator_type::subpixel_shift - gradient_subpixel_shift
00047         };
00048 
00049 
00050         //--------------------------------------------------------------------
00051         span_gradient_alpha() {}
00052 
00053         //--------------------------------------------------------------------
00054         span_gradient_alpha(interpolator_type& inter,
00055                             const GradientF& gradient_function,
00056                             const AlphaF& alpha_function,
00057                             double d1, double d2) : 
00058             m_interpolator(&inter),
00059             m_gradient_function(&gradient_function),
00060             m_alpha_function(&alpha_function),
00061             m_d1(iround(d1 * gradient_subpixel_scale)),
00062             m_d2(iround(d2 * gradient_subpixel_scale))
00063         {}
00064 
00065         //--------------------------------------------------------------------
00066         interpolator_type& interpolator() { return *m_interpolator; }
00067         const GradientF& gradient_function() const { return *m_gradient_function; }
00068         const AlphaF& alpha_function() const { return *m_alpha_function; }
00069         double d1() const { return double(m_d1) / gradient_subpixel_scale; }
00070         double d2() const { return double(m_d2) / gradient_subpixel_scale; }
00071 
00072         //--------------------------------------------------------------------
00073         void interpolator(interpolator_type& i) { m_interpolator = &i; }
00074         void gradient_function(const GradientF& gf) { m_gradient_function = &gf; }
00075         void alpha_function(const AlphaF& af) { m_alpha_function = &af; }
00076         void d1(double v) { m_d1 = iround(v * gradient_subpixel_scale); }
00077         void d2(double v) { m_d2 = iround(v * gradient_subpixel_scale); }
00078 
00079         //--------------------------------------------------------------------
00080         void prepare() {}
00081 
00082         //--------------------------------------------------------------------
00083         void generate(color_type* span, int x, int y, unsigned len)
00084         {   
00085             int dd = m_d2 - m_d1;
00086             if(dd < 1) dd = 1;
00087             m_interpolator->begin(x+0.5, y+0.5, len);
00088             do
00089             {
00090                 m_interpolator->coordinates(&x, &y);
00091                 int d = m_gradient_function->calculate(x >> downscale_shift, 
00092                                                        y >> downscale_shift, m_d2);
00093                 d = ((d - m_d1) * (int)m_alpha_function->size()) / dd;
00094                 if(d < 0) d = 0;
00095                 if(d >= (int)m_alpha_function->size()) d = m_alpha_function->size() - 1;
00096                 span->a = (*m_alpha_function)[d];
00097                 ++span;
00098                 ++(*m_interpolator);
00099             }
00100             while(--len);
00101         }
00102 
00103     private:
00104         interpolator_type* m_interpolator;
00105         const GradientF*   m_gradient_function;
00106         const AlphaF*      m_alpha_function;
00107         int                m_d1;
00108         int                m_d2;
00109     };
00110 
00111 
00112     //=======================================================gradient_alpha_x
00113     template<class ColorT> struct gradient_alpha_x
00114     {
00115         typedef typename ColorT::value_type alpha_type;
00116         alpha_type operator [] (alpha_type x) const { return x; }
00117     };
00118 
00119     //====================================================gradient_alpha_x_u8
00120     struct gradient_alpha_x_u8
00121     {
00122         typedef int8u alpha_type;
00123         alpha_type operator [] (alpha_type x) const { return x; }
00124     };
00125 
00126     //==========================================gradient_alpha_one_munus_x_u8
00127     struct gradient_alpha_one_munus_x_u8
00128     {
00129         typedef int8u alpha_type;
00130         alpha_type operator [] (alpha_type x) const { return 255-x; }
00131     };
00132 
00133 }
00134 
00135 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines