Anti-Grain Geometry Tutorial
agg_span_gradient_image.h
1 //----------------------------------------------------------------------------
2 // AGG Contribution Pack - Gradients 1 (AGG CP - Gradients 1)
3 // http://milan.marusinec.sk/aggcp
4 //
5 // For Anti-Grain Geometry - Version 2.4
6 // http://www.antigrain.org
7 //
8 // Contribution Created By:
9 // Milan Marusinec alias Milano
10 // milan@marusinec.sk
11 // Copyright (c) 2007-2008
12 //
13 // Permission to copy, use, modify, sell and distribute this software
14 // is granted provided this copyright notice appears in all copies.
15 // This software is provided "as is" without express or implied
16 // warranty, and with no claim as to its suitability for any purpose.
17 //
18 // [History] -----------------------------------------------------------------
19 //
20 // 03.02.2008-Milano: Ported from Object Pascal code of AggPas
21 //
22 #ifndef AGG_SPAN_GRADIENT_IMAGE_INCLUDED
23 #define AGG_SPAN_GRADIENT_IMAGE_INCLUDED
24 
25 #include <cstring>
26 #include "agg_basics.h"
27 #include "agg_span_gradient.h"
28 #include "agg_color_rgba.h"
29 #include "agg_rendering_buffer.h"
30 #include "agg_pixfmt_rgba.h"
31 
32 namespace agg
33 {
34 
35  //==========================================================one_color_function
36  template<class ColorT> class one_color_function
37  {
38  public:
39  typedef ColorT color_type;
40 
41  color_type m_color;
42 
44  m_color()
45  {
46  }
47 
48  static unsigned size() { return 1; }
49 
50  const color_type& operator [] (unsigned i) const
51  {
52  return m_color;
53  }
54 
55  color_type* operator [] (unsigned i)
56  {
57  return &m_color;
58  }
59  };
60 
61  //==========================================================gradient_image
62  template<class ColorT> class gradient_image
63  {
64  private:
65  //------------ fields
66  typedef ColorT color_type;
68 
69  agg::rgba8* m_buffer;
70 
71  int m_alocdx;
72  int m_alocdy;
73  int m_width;
74  int m_height;
75 
76  color_type* m_color;
77 
78  one_color_function<color_type> m_color_function;
79 
80  public:
81  gradient_image() :
82  m_color_function(),
83  m_buffer(NULL),
84  m_alocdx(0),
85  m_alocdy(0),
86  m_width(0),
87  m_height(0)
88  {
89  m_color = m_color_function[0 ];
90  }
91 
93  {
94  if (m_buffer) { delete [] m_buffer; }
95  }
96 
97  void* image_create(int width, int height )
98  {
99  void* result = NULL;
100 
101  if (width > m_alocdx || height > m_alocdy)
102  {
103  if (m_buffer) { delete [] m_buffer; }
104 
105  m_buffer = NULL;
106  m_buffer = new agg::rgba8[width * height];
107 
108  if (m_buffer)
109  {
110  m_alocdx = width;
111  m_alocdy = height;
112  }
113  else
114  {
115  m_alocdx = 0;
116  m_alocdy = 0;
117  };
118  };
119 
120  if (m_buffer)
121  {
122  m_width = width;
123  m_height = height;
124 
125  for (int rows = 0; rows < height; rows++)
126  {
127  agg::rgba8* row = &m_buffer[rows * m_alocdx ];
128  std::memset(row ,0 ,m_width * 4 );
129  };
130 
131  result = m_buffer;
132  };
133  return result;
134  }
135 
136  void* image_buffer() { return m_buffer; }
137  int image_width() { return m_width; }
138  int image_height() { return m_height; }
139  int image_stride() { return m_alocdx * 4; }
140 
141  int calculate(int x, int y, int d) const
142  {
143  if (m_buffer)
144  {
145  int px = x >> agg::gradient_subpixel_shift;
146  int py = y >> agg::gradient_subpixel_shift;
147 
148  px %= m_width;
149 
150  if (px < 0)
151  {
152  px += m_width;
153  }
154 
155  py %= m_height;
156 
157  if (py < 0 )
158  {
159  py += m_height;
160  }
161 
162  rgba8* pixel = &m_buffer[py * m_alocdx + px ];
163 
164  m_color->r = pixel->r;
165  m_color->g = pixel->g;
166  m_color->b = pixel->b;
167  m_color->a = pixel->a;
168 
169  }
170  else
171  {
172  m_color->r = 0;
173  m_color->g = 0;
174  m_color->b = 0;
175  m_color->a = 0;
176  }
177  return 0;
178  }
179 
180  const one_color_function<color_type>& color_function() const
181  {
182  return m_color_function;
183  }
184 
185  };
186 
187 }
188 
189 #endif
Definition: agg_arc.cpp:24