Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_rendering_buffer_dynarow.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_RENDERING_BUFFER_DYNAROW_INCLUDED
00026 #define AGG_RENDERING_BUFFER_DYNAROW_INCLUDED
00027 
00028 #include "agg_array.h"
00029 
00030 namespace agg
00031 {
00032 
00033     //===============================================rendering_buffer_dynarow
00034     // Rendering buffer class with dynamic allocation of the rows.
00035     // The rows are allocated as needed when requesting for span_ptr().
00036     // The class automatically calculates min_x and max_x for each row.
00037     // Generally it's more efficient to use this class as a temporary buffer
00038     // for rendering a few lines and then to blend it with another buffer.
00039     //
00040     class rendering_buffer_dynarow
00041     {
00042     public:
00043         typedef row_info<int8u> row_data;
00044 
00045         //-------------------------------------------------------------------
00046         ~rendering_buffer_dynarow()
00047         {
00048             init(0,0,0);
00049         }
00050 
00051         //-------------------------------------------------------------------
00052         rendering_buffer_dynarow() :
00053             m_rows(),
00054             m_width(0),
00055             m_height(0),
00056             m_byte_width(0)
00057         {
00058         }
00059 
00060         // Allocate and clear the buffer
00061         //--------------------------------------------------------------------
00062         rendering_buffer_dynarow(unsigned width, unsigned height, 
00063                                  unsigned byte_width) :
00064             m_rows(height),
00065             m_width(width),
00066             m_height(height),
00067             m_byte_width(byte_width)
00068         {
00069             memset(&m_rows[0], 0, sizeof(row_data) * height);
00070         }
00071 
00072         // Allocate and clear the buffer
00073         //--------------------------------------------------------------------
00074         void init(unsigned width, unsigned height, unsigned byte_width)
00075         {
00076             unsigned i;
00077             for(i = 0; i < m_height; ++i) 
00078             {
00079                 pod_allocator<int8u>::deallocate((int8u*)m_rows[i].ptr, m_byte_width);
00080             }
00081             if(width && height)
00082             {
00083                 m_width  = width;
00084                 m_height = height;
00085                 m_byte_width = byte_width;
00086                 m_rows.resize(height);
00087                 memset(&m_rows[0], 0, sizeof(row_data) * height);
00088             }
00089         }
00090 
00091         //--------------------------------------------------------------------
00092         unsigned width()      const { return m_width;  }
00093         unsigned height()     const { return m_height; }
00094         unsigned byte_width() const { return m_byte_width; }
00095 
00096         // The main function used for rendering. Returns pointer to the 
00097         // pre-allocated span. Memory for the row is allocated as needed.
00098         //--------------------------------------------------------------------
00099         int8u* row_ptr(int x, int y, unsigned len)
00100         {
00101             row_data* r = &m_rows[y];
00102             int x2 = x + len - 1;
00103             if(r->ptr)
00104             {
00105                 if(x  < r->x1) { r->x1 = x;  }
00106                 if(x2 > r->x2) { r->x2 = x2; }
00107             }
00108             else
00109             {
00110                 int8u* p = pod_allocator<int8u>::allocate(m_byte_width);
00111                 r->ptr = p;
00112                 r->x1  = x;
00113                 r->x2  = x2;
00114                 memset(p, 0, m_byte_width);
00115             }
00116             return (int8u*)r->ptr;
00117         }
00118 
00119         //--------------------------------------------------------------------
00120         const int8u* row_ptr(int y) const { return m_rows[y].ptr; }
00121               int8u* row_ptr(int y)       { return row_ptr(0, y, m_width); }
00122         row_data     row    (int y) const { return m_rows[y]; }
00123 
00124     private:
00125         //--------------------------------------------------------------------
00126         // Prohibit copying
00127         rendering_buffer_dynarow(const rendering_buffer_dynarow&);
00128         const rendering_buffer_dynarow& operator = (const rendering_buffer_dynarow&);
00129 
00130     private:
00131         //--------------------------------------------------------------------
00132         pod_array<row_data> m_rows;       // Pointers to each row of the buffer
00133         unsigned            m_width;      // Width in pixels
00134         unsigned            m_height;     // Height in pixels
00135         unsigned            m_byte_width; // Width in bytes
00136     };
00137 
00138 
00139 }
00140 
00141 
00142 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines