| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.4 |
| 3 |
// Copyright (C) 2002-2005 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: [email protected] |
| 12 |
// [email protected] |
| 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_array.h" |
| 24 |
|
| 25 |
namespace agg |
| 26 |
{ |
| 27 |
|
| 28 |
//===========================================================row_accessor |
| 29 |
template<class T> class row_accessor |
| 30 |
{ |
| 31 |
public: |
| 32 |
typedef const_row_info<T> row_data; |
| 33 |
|
| 34 |
//------------------------------------------------------------------- |
| 35 |
row_accessor() : |
| 36 |
m_buf(0), |
| 37 |
m_start(0), |
| 38 |
m_width(0), |
| 39 |
m_height(0), |
| 40 |
m_stride(0) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
//-------------------------------------------------------------------- |
| 45 |
row_accessor(T* buf, unsigned width, unsigned height, int stride) : |
| 46 |
m_buf(0), |
| 47 |
m_start(0), |
| 48 |
m_width(0), |
| 49 |
m_height(0), |
| 50 |
m_stride(0) |
| 51 |
{ |
| 52 |
attach(buf, width, height, stride); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
//-------------------------------------------------------------------- |
| 57 |
void attach(T* buf, unsigned width, unsigned height, int stride) |
| 58 |
{ |
| 59 |
m_buf = m_start = buf; |
| 60 |
m_width = width; |
| 61 |
m_height = height; |
| 62 |
m_stride = stride; |
| 63 |
if(stride < 0) |
| 64 |
{ |
| 65 |
m_start = m_buf - (AGG_INT64)(height - 1) * stride; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
//-------------------------------------------------------------------- |
| 70 |
AGG_INLINE T* buf() { return m_buf; } |
| 71 |
AGG_INLINE const T* buf() const { return m_buf; } |
| 72 |
AGG_INLINE unsigned width() const { return m_width; } |
| 73 |
AGG_INLINE unsigned height() const { return m_height; } |
| 74 |
AGG_INLINE int stride() const { return m_stride; } |
| 75 |
AGG_INLINE unsigned stride_abs() const |
| 76 |
{ |
| 77 |
return (m_stride < 0) ? unsigned(-m_stride) : unsigned(m_stride); |
| 78 |
} |
| 79 |
|
| 80 |
//-------------------------------------------------------------------- |
| 81 |
AGG_INLINE T* row_ptr(int, int y, unsigned) |
| 82 |
{ |
| 83 |
return m_start + y * (AGG_INT64)m_stride; |
| 84 |
} |
| 85 |
AGG_INLINE T* row_ptr(int y) { return m_start + y * (AGG_INT64)m_stride; } |
| 86 |
AGG_INLINE const T* row_ptr(int y) const { return m_start + y * (AGG_INT64)m_stride; } |
| 87 |
AGG_INLINE row_data row (int y) const |
| 88 |
{ |
| 89 |
return row_data(0, m_width-1, row_ptr(y)); |
| 90 |
} |
| 91 |
|
| 92 |
//-------------------------------------------------------------------- |
| 93 |
template<class RenBuf> |
| 94 |
void copy_from(const RenBuf& src) |
| 95 |
{ |
| 96 |
unsigned h = height(); |
| 97 |
if(src.height() < h) h = src.height(); |
| 98 |
|
| 99 |
unsigned l = stride_abs(); |
| 100 |
if(src.stride_abs() < l) l = src.stride_abs(); |
| 101 |
|
| 102 |
l *= sizeof(T); |
| 103 |
|
| 104 |
unsigned y; |
| 105 |
unsigned w = width(); |
| 106 |
for (y = 0; y < h; y++) |
| 107 |
{ |
| 108 |
memcpy(row_ptr(0, y, w), src.row_ptr(y), l); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
//-------------------------------------------------------------------- |
| 113 |
void clear(T value) |
| 114 |
{ |
| 115 |
unsigned y; |
| 116 |
unsigned w = width(); |
| 117 |
unsigned stride = stride_abs(); |
| 118 |
for(y = 0; y < height(); y++) |
| 119 |
{ |
| 120 |
T* p = row_ptr(0, y, w); |
| 121 |
unsigned x; |
| 122 |
for(x = 0; x < stride; x++) |
| 123 |
{ |
| 124 |
*p++ = value; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
private: |
| 130 |
//-------------------------------------------------------------------- |
| 131 |
T* m_buf; // Pointer to renrdering buffer |
| 132 |
T* m_start; // Pointer to first pixel depending on stride |
| 133 |
unsigned m_width; // Width in pixels |
| 134 |
unsigned m_height; // Height in pixels |
| 135 |
int m_stride; // Number of bytes per row. Can be < 0 |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
//==========================================================row_ptr_cache |
| 142 |
template<class T> class row_ptr_cache |
| 143 |
{ |
| 144 |
public: |
| 145 |
typedef const_row_info<T> row_data; |
| 146 |
|
| 147 |
//------------------------------------------------------------------- |
| 148 |
row_ptr_cache() : |
| 149 |
m_buf(0), |
| 150 |
m_rows(), |
| 151 |
m_width(0), |
| 152 |
m_height(0), |
| 153 |
m_stride(0) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
//-------------------------------------------------------------------- |
| 158 |
row_ptr_cache(T* buf, unsigned width, unsigned height, int stride) : |
| 159 |
m_buf(0), |
| 160 |
m_rows(), |
| 161 |
m_width(0), |
| 162 |
m_height(0), |
| 163 |
m_stride(0) |
| 164 |
{ |
| 165 |
attach(buf, width, height, stride); |
| 166 |
} |
| 167 |
|
| 168 |
//-------------------------------------------------------------------- |
| 169 |
void attach(T* buf, unsigned width, unsigned height, int stride) |
| 170 |
{ |
| 171 |
m_buf = buf; |
| 172 |
m_width = width; |
| 173 |
m_height = height; |
| 174 |
m_stride = stride; |
| 175 |
if(height > m_rows.size()) |
| 176 |
{ |
| 177 |
m_rows.resize(height); |
| 178 |
} |
| 179 |
|
| 180 |
T* row_ptr = m_buf; |
| 181 |
|
| 182 |
if(stride < 0) |
| 183 |
{ |
| 184 |
row_ptr = m_buf - (AGG_INT64)(height - 1) * stride; |
| 185 |
} |
| 186 |
|
| 187 |
T** rows = &m_rows[0]; |
| 188 |
|
| 189 |
while(height--) |
| 190 |
{ |
| 191 |
*rows++ = row_ptr; |
| 192 |
row_ptr += stride; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
//-------------------------------------------------------------------- |
| 197 |
AGG_INLINE T* buf() { return m_buf; } |
| 198 |
AGG_INLINE const T* buf() const { return m_buf; } |
| 199 |
AGG_INLINE unsigned width() const { return m_width; } |
| 200 |
AGG_INLINE unsigned height() const { return m_height; } |
| 201 |
AGG_INLINE int stride() const { return m_stride; } |
| 202 |
AGG_INLINE unsigned stride_abs() const |
| 203 |
{ |
| 204 |
return (m_stride < 0) ? unsigned(-m_stride) : unsigned(m_stride); |
| 205 |
} |
| 206 |
|
| 207 |
//-------------------------------------------------------------------- |
| 208 |
AGG_INLINE T* row_ptr(int, int y, unsigned) |
| 209 |
{ |
| 210 |
return m_rows[y]; |
| 211 |
} |
| 212 |
AGG_INLINE T* row_ptr(int y) { return m_rows[y]; } |
| 213 |
AGG_INLINE const T* row_ptr(int y) const { return m_rows[y]; } |
| 214 |
AGG_INLINE row_data row (int y) const |
| 215 |
{ |
| 216 |
return row_data(0, m_width-1, m_rows[y]); |
| 217 |
} |
| 218 |
|
| 219 |
//-------------------------------------------------------------------- |
| 220 |
T const* const* rows() const { return &m_rows[0]; } |
| 221 |
|
| 222 |
//-------------------------------------------------------------------- |
| 223 |
template<class RenBuf> |
| 224 |
void copy_from(const RenBuf& src) |
| 225 |
{ |
| 226 |
unsigned h = height(); |
| 227 |
if(src.height() < h) h = src.height(); |
| 228 |
|
| 229 |
unsigned l = stride_abs(); |
| 230 |
if(src.stride_abs() < l) l = src.stride_abs(); |
| 231 |
|
| 232 |
l *= sizeof(T); |
| 233 |
|
| 234 |
unsigned y; |
| 235 |
unsigned w = width(); |
| 236 |
for (y = 0; y < h; y++) |
| 237 |
{ |
| 238 |
memcpy(row_ptr(0, y, w), src.row_ptr(y), l); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
//-------------------------------------------------------------------- |
| 243 |
void clear(T value) |
| 244 |
{ |
| 245 |
unsigned y; |
| 246 |
unsigned w = width(); |
| 247 |
unsigned stride = stride_abs(); |
| 248 |
for(y = 0; y < height(); y++) |
| 249 |
{ |
| 250 |
T* p = row_ptr(0, y, w); |
| 251 |
unsigned x; |
| 252 |
for(x = 0; x < stride; x++) |
| 253 |
{ |
| 254 |
*p++ = value; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
private: |
| 260 |
//-------------------------------------------------------------------- |
| 261 |
T* m_buf; // Pointer to renrdering buffer |
| 262 |
pod_array<T*> m_rows; // Pointers to each row of the buffer |
| 263 |
unsigned m_width; // Width in pixels |
| 264 |
unsigned m_height; // Height in pixels |
| 265 |
int m_stride; // Number of bytes per row. Can be < 0 |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
//========================================================rendering_buffer |
| 272 |
// |
| 273 |
// The definition of the main type for accessing the rows in the frame |
| 274 |
// buffer. It provides functionality to navigate to the rows in a |
| 275 |
// rectangular matrix, from top to bottom or from bottom to top depending |
| 276 |
// on stride. |
| 277 |
// |
| 278 |
// row_accessor is cheap to create/destroy, but performs one multiplication |
| 279 |
// when calling row_ptr(). |
| 280 |
// |
| 281 |
// row_ptr_cache creates an array of pointers to rows, so, the access |
| 282 |
// via row_ptr() may be faster. But it requires memory allocation |
| 283 |
// when creating. For example, on typical Intel Pentium hardware |
| 284 |
// row_ptr_cache speeds span_image_filter_rgb_nn up to 10% |
| 285 |
// |
| 286 |
// It's used only in short hand typedefs like pixfmt_rgba32 and can be |
| 287 |
// redefined in agg_config.h |
| 288 |
// In real applications you can use both, depending on your needs |
| 289 |
//------------------------------------------------------------------------ |
| 290 |
#ifdef AGG_RENDERING_BUFFER |
| 291 |
typedef AGG_RENDERING_BUFFER rendering_buffer; |
| 292 |
#else |
| 293 |
// typedef row_ptr_cache<int8u> rendering_buffer; |
| 294 |
typedef row_accessor<int8u> rendering_buffer; |
| 295 |
#endif |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
#endif |