Anti-Grain Geometry Tutorial
agg_rendering_buffer_dynarow.h
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: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // class rendering_buffer_dynarow
17 //
18 //----------------------------------------------------------------------------
19 
20 #ifndef AGG_RENDERING_BUFFER_DYNAROW_INCLUDED
21 #define AGG_RENDERING_BUFFER_DYNAROW_INCLUDED
22 
23 #include <cstring>
24 #include "agg_array.h"
25 
26 namespace agg
27 {
28 
29  //===============================================rendering_buffer_dynarow
30  // Rendering buffer class with dynamic allocation of the rows.
31  // The rows are allocated as needed when requesting for span_ptr().
32  // The class automatically calculates min_x and max_x for each row.
33  // Generally it's more efficient to use this class as a temporary buffer
34  // for rendering a few lines and then to blend it with another buffer.
35  //
37  {
38  public:
39  typedef row_info<int8u> row_data;
40 
41  //-------------------------------------------------------------------
43  {
44  init(0,0,0);
45  }
46 
47  //-------------------------------------------------------------------
49  m_rows(),
50  m_width(0),
51  m_height(0),
52  m_byte_width(0)
53  {
54  }
55 
56  // Allocate and clear the buffer
57  //--------------------------------------------------------------------
58  rendering_buffer_dynarow(unsigned width, unsigned height,
59  unsigned byte_width) :
60  m_rows(height),
61  m_width(width),
62  m_height(height),
63  m_byte_width(byte_width)
64  {
65  std::memset(&m_rows[0], 0, sizeof(row_data) * height);
66  }
67 
68  // Allocate and clear the buffer
69  //--------------------------------------------------------------------
70  void init(unsigned width, unsigned height, unsigned byte_width)
71  {
72  unsigned i;
73  for(i = 0; i < m_height; ++i)
74  {
75  pod_allocator<int8u>::deallocate((int8u*)m_rows[i].ptr, m_byte_width);
76  }
77  if(width && height)
78  {
79  m_width = width;
80  m_height = height;
81  m_byte_width = byte_width;
82  m_rows.resize(height);
83  std::memset(&m_rows[0], 0, sizeof(row_data) * height);
84  }
85  }
86 
87  //--------------------------------------------------------------------
88  unsigned width() const { return m_width; }
89  unsigned height() const { return m_height; }
90  unsigned byte_width() const { return m_byte_width; }
91 
92  // The main function used for rendering. Returns pointer to the
93  // pre-allocated span. Memory for the row is allocated as needed.
94  //--------------------------------------------------------------------
95  int8u* row_ptr(int x, int y, unsigned len)
96  {
97  row_data* r = &m_rows[y];
98  int x2 = x + len - 1;
99  if(r->ptr)
100  {
101  if(x < r->x1) { r->x1 = x; }
102  if(x2 > r->x2) { r->x2 = x2; }
103  }
104  else
105  {
106  int8u* p = pod_allocator<int8u>::allocate(m_byte_width);
107  r->ptr = p;
108  r->x1 = x;
109  r->x2 = x2;
110  std::memset(p, 0, m_byte_width);
111  }
112  return (int8u*)r->ptr;
113  }
114 
115  //--------------------------------------------------------------------
116  const int8u* row_ptr(int y) const { return m_rows[y].ptr; }
117  int8u* row_ptr(int y) { return row_ptr(0, y, m_width); }
118  row_data row (int y) const { return m_rows[y]; }
119 
120  private:
121  //--------------------------------------------------------------------
122  // Prohibit copying
124  const rendering_buffer_dynarow& operator = (const rendering_buffer_dynarow&);
125 
126  private:
127  //--------------------------------------------------------------------
128  pod_array<row_data> m_rows; // Pointers to each row of the buffer
129  unsigned m_width; // Width in pixels
130  unsigned m_height; // Height in pixels
131  unsigned m_byte_width; // Width in bytes
132  };
133 
134 
135 }
136 
137 
138 #endif
Definition: agg_arc.cpp:24