1 //---------------------------------------------------------------------------- 2 // Anti-Grain Geometry - Version 2.2 3 // Copyright (C) 2002-2004 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 //---------------------------------------------------------------------------- 15 // 16 // class rendering_buffer 17 // 18 //---------------------------------------------------------------------------- 19 20 #ifndef AGG_RENDERING_BUFFER_INCLUDED 21 #define AGG_RENDERING_BUFFER_INCLUDED 22 23 #include "agg_basics.h" 24 25 namespace agg 26 { 27 28 //==========================================================row_ptr_cache 29 template<class T> class row_ptr_cache 30 { 31 public: 32 //------------------------------------------------------------------- 33 ~row_ptr_cache() 34 { 35 delete [] m_rows; 36 } 37 38 //------------------------------------------------------------------- 39 row_ptr_cache() : 40 m_buf(0), 41 m_rows(0), 42 m_width(0), 43 m_height(0), 44 m_stride(0), 45 m_max_height(0) 46 { 47 } 48 49 //-------------------------------------------------------------------- 50 row_ptr_cache(T* buf, unsigned width, unsigned height, int stride) : 51 m_buf(0), 52 m_rows(0), 53 m_width(0), 54 m_height(0), 55 m_stride(0), 56 m_max_height(0) 57 { 58 attach(buf, width, height, stride); 59 } 60 61 //-------------------------------------------------------------------- 62 void attach(T* buf, unsigned width, unsigned height, int stride) 63 { 64 m_buf = buf; 65 m_width = width; 66 m_height = height; 67 m_stride = stride; 68 if(height > m_max_height) 69 { 70 delete [] m_rows; 71 m_rows = new T* [m_max_height = height]; 72 } 73 74 T* row_ptr = m_buf; 75 76 if(stride < 0) 77 { 78 row_ptr = m_buf - int(height - 1) * stride; 79 } 80 81 T** rows = m_rows; 82 83 while(height--) 84 { 85 *rows++ = row_ptr; 86 row_ptr += stride; 87 } 88 } 89 90 //-------------------------------------------------------------------- 91 const T* buf() const { return m_buf; } 92 unsigned width() const { return m_width; } 93 unsigned height() const { return m_height; } 94 int stride() const { return m_stride; } 95 unsigned stride_abs() const 96 { 97 return (m_stride < 0) ? 98 unsigned(-m_stride) : 99 unsigned(m_stride); 100 } 101 102 //-------------------------------------------------------------------- 103 T* row(unsigned y) { return m_rows[y]; } 104 const T* row(unsigned y) const { return m_rows[y]; } 105 T const* const* rows() const { return m_rows; } 106 107 //-------------------------------------------------------------------- 108 void copy_from(const row_ptr_cache<T>& mtx) 109 { 110 unsigned h = height(); 111 if(mtx.height() < h) h = mtx.height(); 112 113 unsigned l = stride_abs(); 114 if(mtx.stride_abs() < l) l = mtx.stride_abs(); 115 116 l *= sizeof(T); 117 118 unsigned y; 119 for (y = 0; y < h; y++) 120 { 121 memcpy(row(y), mtx.row(y), l); 122 } 123 } 124 125 private: 126 //-------------------------------------------------------------------- 127 // Prohibit copying 128 row_ptr_cache(const row_ptr_cache<T>&); 129 const row_ptr_cache<T>& operator = (const row_ptr_cache<T>&); 130 131 private: 132 //-------------------------------------------------------------------- 133 T* m_buf; // Pointer to renrdering buffer 134 T** m_rows; // Pointers to each row of the buffer 135 unsigned m_width; // Width in pixels 136 unsigned m_height; // Height in pixels 137 int m_stride; // Number of bytes per row. Can be < 0 138 unsigned m_max_height; // The maximal height (currently allocated) 139 }; 140 141 142 143 //========================================================rendering_buffer 144 typedef row_ptr_cache<int8u> rendering_buffer; 145 146 } 147 148 149 #endif 150