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 struct row_data 34 { 35 int x1, x2; 36 const int8u* ptr; 37 row_data() {} 38 row_data(int x1_, int x2_, const int8u* ptr_) : 39 x1(x1_), x2(x2_), ptr(ptr_) {} 40 }; 41 42 //------------------------------------------------------------------- 43 ~row_ptr_cache() 44 { 45 delete [] m_rows; 46 } 47 48 //------------------------------------------------------------------- 49 row_ptr_cache() : 50 m_buf(0), 51 m_rows(0), 52 m_width(0), 53 m_height(0), 54 m_stride(0), 55 m_max_height(0) 56 { 57 } 58 59 //-------------------------------------------------------------------- 60 row_ptr_cache(T* buf, unsigned width, unsigned height, int stride) : 61 m_buf(0), 62 m_rows(0), 63 m_width(0), 64 m_height(0), 65 m_stride(0), 66 m_max_height(0) 67 { 68 attach(buf, width, height, stride); 69 } 70 71 //-------------------------------------------------------------------- 72 void attach(T* buf, unsigned width, unsigned height, int stride) 73 { 74 m_buf = buf; 75 m_width = width; 76 m_height = height; 77 m_stride = stride; 78 if(height > m_max_height) 79 { 80 delete [] m_rows; 81 m_rows = new T* [m_max_height = height]; 82 } 83 84 T* row_ptr = m_buf; 85 86 if(stride < 0) 87 { 88 row_ptr = m_buf - int(height - 1) * stride; 89 } 90 91 T** rows = m_rows; 92 93 while(height--) 94 { 95 *rows++ = row_ptr; 96 row_ptr += stride; 97 } 98 } 99 100 //-------------------------------------------------------------------- 101 const T* buf() const { return m_buf; } 102 unsigned width() const { return m_width; } 103 unsigned height() const { return m_height; } 104 int stride() const { return m_stride; } 105 unsigned stride_abs() const 106 { 107 return (m_stride < 0) ? 108 unsigned(-m_stride) : 109 unsigned(m_stride); 110 } 111 112 //-------------------------------------------------------------------- 113 T* row(unsigned y) { return m_rows[y]; } 114 const T* row(unsigned y) const { return m_rows[y]; } 115 T const* const* rows() const { return m_rows; } 116 117 //-------------------------------------------------------------------- 118 void copy_from(const row_ptr_cache<T>& mtx) 119 { 120 unsigned h = height(); 121 if(mtx.height() < h) h = mtx.height(); 122 123 unsigned l = stride_abs(); 124 if(mtx.stride_abs() < l) l = mtx.stride_abs(); 125 126 l *= sizeof(T); 127 128 unsigned y; 129 for (y = 0; y < h; y++) 130 { 131 memcpy(row(y), mtx.row(y), l); 132 } 133 } 134 135 //-------------------------------------------------------------------- 136 void clear(T value) 137 { 138 unsigned y; 139 for(y = 0; y < height(); y++) 140 { 141 T* p = row(y); 142 unsigned x; 143 for(x = 0; x < stride_abs(); x++) 144 { 145 *p++ = value; 146 } 147 } 148 } 149 150 151 private: 152 //-------------------------------------------------------------------- 153 // Prohibit copying 154 row_ptr_cache(const row_ptr_cache<T>&); 155 const row_ptr_cache<T>& operator = (const row_ptr_cache<T>&); 156 157 private: 158 //-------------------------------------------------------------------- 159 T* m_buf; // Pointer to renrdering buffer 160 T** m_rows; // Pointers to each row of the buffer 161 unsigned m_width; // Width in pixels 162 unsigned m_height; // Height in pixels 163 int m_stride; // Number of bytes per row. Can be < 0 164 unsigned m_max_height; // The maximal height (currently allocated) 165 }; 166 167 168 169 //========================================================rendering_buffer 170 typedef row_ptr_cache<int8u> rendering_buffer; 171 172 } 173 174 175 #endif 176