Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_renderer_primitives.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_RENDERER_PRIMITIVES_INCLUDED
00026 #define AGG_RENDERER_PRIMITIVES_INCLUDED
00027 
00028 #include "agg_basics.h"
00029 #include "agg_renderer_base.h"
00030 #include "agg_dda_line.h"
00031 #include "agg_ellipse_bresenham.h"
00032 
00033 namespace agg
00034 {
00035     //-----------------------------------------------------renderer_primitives
00036     template<class BaseRenderer> class renderer_primitives
00037     {
00038     public:
00039         typedef BaseRenderer base_ren_type;
00040         typedef typename base_ren_type::color_type color_type;
00041 
00042         //--------------------------------------------------------------------
00043         explicit renderer_primitives(base_ren_type& ren) :
00044             m_ren(&ren),
00045             m_fill_color(),
00046             m_line_color(),
00047             m_curr_x(0),
00048             m_curr_y(0)
00049         {}
00050         void attach(base_ren_type& ren) { m_ren = &ren; }
00051 
00052         //--------------------------------------------------------------------
00053         static int coord(double c) 
00054         { 
00055             return iround(c * line_bresenham_interpolator::subpixel_scale); 
00056         }
00057 
00058         //--------------------------------------------------------------------
00059         void fill_color(const color_type& c) { m_fill_color = c; }
00060         void line_color(const color_type& c) { m_line_color = c; }
00061         const color_type& fill_color() const { return m_fill_color; }
00062         const color_type& line_color() const { return m_line_color; }
00063 
00064         //--------------------------------------------------------------------
00065         void rectangle(int x1, int y1, int x2, int y2)
00066         {
00067             m_ren->blend_hline(x1,   y1,   x2-1, m_line_color, cover_full);
00068             m_ren->blend_vline(x2,   y1,   y2-1, m_line_color, cover_full);
00069             m_ren->blend_hline(x1+1, y2,   x2,   m_line_color, cover_full);
00070             m_ren->blend_vline(x1,   y1+1, y2,   m_line_color, cover_full);
00071         }
00072 
00073         //--------------------------------------------------------------------
00074         void solid_rectangle(int x1, int y1, int x2, int y2)
00075         {
00076             m_ren->blend_bar(x1, y1, x2, y2, m_fill_color, cover_full);
00077         }
00078 
00079         //--------------------------------------------------------------------
00080         void outlined_rectangle(int x1, int y1, int x2, int y2) 
00081         {
00082             rectangle(x1, y1, x2, y2);
00083             m_ren->blend_bar(x1+1, y1+1, x2-1, y2-1, m_fill_color, cover_full);
00084         }
00085 
00086         //--------------------------------------------------------------------
00087         void ellipse(int x, int y, int rx, int ry)
00088         {
00089             ellipse_bresenham_interpolator ei(rx, ry);
00090             int dx = 0;
00091             int dy = -ry;
00092             do
00093             {
00094                 dx += ei.dx();
00095                 dy += ei.dy();
00096                 m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
00097                 m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
00098                 m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
00099                 m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
00100                 ++ei;
00101             }
00102             while(dy < 0);
00103         }
00104 
00105         //--------------------------------------------------------------------
00106         void solid_ellipse(int x, int y, int rx, int ry)
00107         {
00108             ellipse_bresenham_interpolator ei(rx, ry);
00109             int dx = 0;
00110             int dy = -ry;
00111             int dy0 = dy;
00112             int dx0 = dx;
00113 
00114             do
00115             {
00116                 dx += ei.dx();
00117                 dy += ei.dy();
00118 
00119                 if(dy != dy0)
00120                 {
00121                     m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
00122                     m_ren->blend_hline(x-dx0, y-dy0, x+dx0, m_fill_color, cover_full);
00123                 }
00124                 dx0 = dx;
00125                 dy0 = dy;
00126                 ++ei;
00127             }
00128             while(dy < 0);
00129             m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
00130         }
00131 
00132         //--------------------------------------------------------------------
00133         void outlined_ellipse(int x, int y, int rx, int ry)
00134         {
00135             ellipse_bresenham_interpolator ei(rx, ry);
00136             int dx = 0;
00137             int dy = -ry;
00138 
00139             do
00140             {
00141                 dx += ei.dx();
00142                 dy += ei.dy();
00143 
00144                 m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
00145                 m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
00146                 m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
00147                 m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
00148 
00149                 if(ei.dy() && dx)
00150                 {
00151                    m_ren->blend_hline(x-dx+1, y+dy, x+dx-1, m_fill_color, cover_full);
00152                    m_ren->blend_hline(x-dx+1, y-dy, x+dx-1, m_fill_color, cover_full);
00153                 }
00154                 ++ei;
00155             }
00156             while(dy < 0);
00157         }
00158 
00159         //--------------------------------------------------------------------
00160         void line(int x1, int y1, int x2, int y2, bool last=false)
00161         {
00162             line_bresenham_interpolator li(x1, y1, x2, y2);
00163 
00164             unsigned len = li.len();
00165             if(len == 0)
00166             {
00167                 if(last)
00168                 {
00169                     m_ren->blend_pixel(li.line_lr(x1), li.line_lr(y1), m_line_color, cover_full);
00170                 }
00171                 return;
00172             }
00173 
00174             if(last) ++len;
00175 
00176             if(li.is_ver())
00177             {
00178                 do
00179                 {
00180                     m_ren->blend_pixel(li.x2(), li.y1(), m_line_color, cover_full);
00181                     li.vstep();
00182                 }
00183                 while(--len);
00184             }
00185             else
00186             {
00187                 do
00188                 {
00189                     m_ren->blend_pixel(li.x1(), li.y2(), m_line_color, cover_full);
00190                     li.hstep();
00191                 }
00192                 while(--len);
00193             }
00194         }
00195 
00196         //--------------------------------------------------------------------
00197         void move_to(int x, int y)
00198         {
00199             m_curr_x = x;
00200             m_curr_y = y;
00201         }
00202 
00203         //--------------------------------------------------------------------
00204         void line_to(int x, int y, bool last=false)
00205         {
00206             line(m_curr_x, m_curr_y, x, y, last);
00207             m_curr_x = x;
00208             m_curr_y = y;
00209         }
00210 
00211         //--------------------------------------------------------------------
00212         const base_ren_type& ren() const { return *m_ren; }        
00213         base_ren_type& ren() { return *m_ren; }        
00214 
00215         //--------------------------------------------------------------------
00216         const rendering_buffer& rbuf() const { return m_ren->rbuf(); }
00217         rendering_buffer& rbuf() { return m_ren->rbuf(); }
00218 
00219     private:
00220         base_ren_type* m_ren;
00221         color_type m_fill_color;
00222         color_type m_line_color;
00223         int m_curr_x;
00224         int m_curr_y;
00225     };
00226 
00227 }
00228 
00229 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines