Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_span_image_filter.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_IMAGE_FILTER_INCLUDED
00026 #define AGG_SPAN_IMAGE_FILTER_INCLUDED
00027 
00028 #include "agg_basics.h"
00029 #include "agg_image_filters.h"
00030 #include "agg_span_interpolator_linear.h"
00031 
00032 namespace agg
00033 {
00034 
00035     //-------------------------------------------------------span_image_filter
00036     template<class Source, class Interpolator> class span_image_filter
00037     {
00038     public:
00039         typedef Source source_type;
00040         typedef Interpolator interpolator_type;
00041 
00042         //--------------------------------------------------------------------
00043         span_image_filter() {}
00044         span_image_filter(source_type& src, 
00045                           interpolator_type& interpolator,
00046                           const image_filter_lut* filter) : 
00047             m_src(&src),
00048             m_interpolator(&interpolator),
00049             m_filter(filter),
00050             m_dx_dbl(0.5),
00051             m_dy_dbl(0.5),
00052             m_dx_int(image_subpixel_scale / 2),
00053             m_dy_int(image_subpixel_scale / 2)
00054         {}
00055         void attach(source_type& v) { m_src = &v; }
00056 
00057         //--------------------------------------------------------------------
00058                source_type& source()            { return *m_src; }
00059         const  source_type& source()      const { return *m_src; }
00060         const  image_filter_lut& filter() const { return *m_filter; }
00061         int    filter_dx_int()            const { return m_dx_int; }
00062         int    filter_dy_int()            const { return m_dy_int; }
00063         double filter_dx_dbl()            const { return m_dx_dbl; }
00064         double filter_dy_dbl()            const { return m_dy_dbl; }
00065 
00066         //--------------------------------------------------------------------
00067         void interpolator(interpolator_type& v)  { m_interpolator = &v; }
00068         void filter(const image_filter_lut& v)   { m_filter = &v; }
00069         void filter_offset(double dx, double dy)
00070         {
00071             m_dx_dbl = dx;
00072             m_dy_dbl = dy;
00073             m_dx_int = iround(dx * image_subpixel_scale);
00074             m_dy_int = iround(dy * image_subpixel_scale);
00075         }
00076         void filter_offset(double d) { filter_offset(d, d); }
00077 
00078         //--------------------------------------------------------------------
00079         interpolator_type& interpolator() { return *m_interpolator; }
00080 
00081         //--------------------------------------------------------------------
00082         void prepare() {}
00083 
00084         //--------------------------------------------------------------------
00085     private:
00086         source_type*            m_src;
00087         interpolator_type*      m_interpolator;
00088         const image_filter_lut* m_filter;
00089         double   m_dx_dbl;
00090         double   m_dy_dbl;
00091         unsigned m_dx_int;
00092         unsigned m_dy_int;
00093     };
00094 
00095 
00096 
00097 
00098     //==============================================span_image_resample_affine
00099     template<class Source> 
00100     class span_image_resample_affine : 
00101     public span_image_filter<Source, span_interpolator_linear<trans_affine> >
00102     {
00103     public:
00104         typedef Source source_type;
00105         typedef span_interpolator_linear<trans_affine> interpolator_type;
00106         typedef span_image_filter<source_type, interpolator_type> base_type;
00107 
00108         //--------------------------------------------------------------------
00109         span_image_resample_affine() : 
00110             m_scale_limit(200.0),
00111             m_blur_x(1.0),
00112             m_blur_y(1.0)
00113         {}
00114 
00115         //--------------------------------------------------------------------
00116         span_image_resample_affine(source_type& src, 
00117                                    interpolator_type& inter,
00118                                    const image_filter_lut& filter) :
00119             base_type(src, inter, &filter),
00120             m_scale_limit(200.0),
00121             m_blur_x(1.0),
00122             m_blur_y(1.0)
00123         {}
00124 
00125 
00126         //--------------------------------------------------------------------
00127         int  scale_limit() const { return uround(m_scale_limit); }
00128         void scale_limit(int v)  { m_scale_limit = v; }
00129 
00130         //--------------------------------------------------------------------
00131         double blur_x() const { return m_blur_x; }
00132         double blur_y() const { return m_blur_y; }
00133         void blur_x(double v) { m_blur_x = v; }
00134         void blur_y(double v) { m_blur_y = v; }
00135         void blur(double v) { m_blur_x = m_blur_y = v; }
00136 
00137         //--------------------------------------------------------------------
00138         void prepare() 
00139         {
00140             double scale_x;
00141             double scale_y;
00142 
00143             base_type::interpolator().transformer().scaling_abs(&scale_x, &scale_y);
00144 
00145             if(scale_x * scale_y > m_scale_limit)
00146             {
00147                 scale_x = scale_x * m_scale_limit / (scale_x * scale_y);
00148                 scale_y = scale_y * m_scale_limit / (scale_x * scale_y);
00149             }
00150 
00151             if(scale_x < 1) scale_x = 1;
00152             if(scale_y < 1) scale_y = 1;
00153 
00154             if(scale_x > m_scale_limit) scale_x = m_scale_limit;
00155             if(scale_y > m_scale_limit) scale_y = m_scale_limit;
00156 
00157             scale_x *= m_blur_x;
00158             scale_y *= m_blur_y;
00159 
00160             if(scale_x < 1) scale_x = 1;
00161             if(scale_y < 1) scale_y = 1;
00162 
00163             m_rx     = uround(    scale_x * double(image_subpixel_scale));
00164             m_rx_inv = uround(1.0/scale_x * double(image_subpixel_scale));
00165 
00166             m_ry     = uround(    scale_y * double(image_subpixel_scale));
00167             m_ry_inv = uround(1.0/scale_y * double(image_subpixel_scale));
00168         }
00169 
00170     protected:
00171         int m_rx;
00172         int m_ry;
00173         int m_rx_inv;
00174         int m_ry_inv;
00175 
00176     private:
00177         double m_scale_limit;
00178         double m_blur_x;
00179         double m_blur_y;
00180     };
00181 
00182 
00183 
00184     //=====================================================span_image_resample
00185     template<class Source, class Interpolator> 
00186     class span_image_resample : 
00187     public span_image_filter<Source, Interpolator>
00188     {
00189     public:
00190         typedef Source source_type;
00191         typedef Interpolator interpolator_type;
00192         typedef span_image_filter<source_type, interpolator_type> base_type;
00193 
00194         //--------------------------------------------------------------------
00195         span_image_resample() : 
00196             m_scale_limit(20),
00197             m_blur_x(image_subpixel_scale),
00198             m_blur_y(image_subpixel_scale)
00199         {}
00200 
00201         //--------------------------------------------------------------------
00202         span_image_resample(source_type& src, 
00203                             interpolator_type& inter,
00204                             const image_filter_lut& filter) :
00205             base_type(src, inter, &filter),
00206             m_scale_limit(20),
00207             m_blur_x(image_subpixel_scale),
00208             m_blur_y(image_subpixel_scale)
00209         {}
00210 
00211         //--------------------------------------------------------------------
00212         int  scale_limit() const { return m_scale_limit; }
00213         void scale_limit(int v)  { m_scale_limit = v; }
00214 
00215         //--------------------------------------------------------------------
00216         double blur_x() const { return double(m_blur_x) / double(image_subpixel_scale); }
00217         double blur_y() const { return double(m_blur_y) / double(image_subpixel_scale); }
00218         void blur_x(double v) { m_blur_x = uround(v * double(image_subpixel_scale)); }
00219         void blur_y(double v) { m_blur_y = uround(v * double(image_subpixel_scale)); }
00220         void blur(double v)   { m_blur_x = 
00221                                 m_blur_y = uround(v * double(image_subpixel_scale)); }
00222 
00223     protected:
00224         AGG_INLINE void adjust_scale(int* rx, int* ry)
00225         {
00226             if(*rx < image_subpixel_scale) *rx = image_subpixel_scale;
00227             if(*ry < image_subpixel_scale) *ry = image_subpixel_scale;
00228             if(*rx > image_subpixel_scale * m_scale_limit) 
00229             {
00230                 *rx = image_subpixel_scale * m_scale_limit;
00231             }
00232             if(*ry > image_subpixel_scale * m_scale_limit) 
00233             {
00234                 *ry = image_subpixel_scale * m_scale_limit;
00235             }
00236             *rx = (*rx * m_blur_x) >> image_subpixel_shift;
00237             *ry = (*ry * m_blur_y) >> image_subpixel_shift;
00238             if(*rx < image_subpixel_scale) *rx = image_subpixel_scale;
00239             if(*ry < image_subpixel_scale) *ry = image_subpixel_scale;
00240         }
00241 
00242         int m_scale_limit;
00243         int m_blur_x;
00244         int m_blur_y;
00245     };
00246 
00247 
00248 
00249 
00250 }
00251 
00252 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines