Anti-Grain Geometry Tutorial
agg_span_gradient.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 #ifndef AGG_SPAN_GRADIENT_INCLUDED
17 #define AGG_SPAN_GRADIENT_INCLUDED
18 
19 #include <cmath>
20 #include <cstdlib>
21 #include "agg_basics.h"
22 #include "agg_math.h"
23 #include "agg_array.h"
24 
25 
26 namespace agg
27 {
28 
29  enum gradient_subpixel_scale_e
30  {
31  gradient_subpixel_shift = 4, //-----gradient_subpixel_shift
32  gradient_subpixel_scale = 1 << gradient_subpixel_shift, //-----gradient_subpixel_scale
33  gradient_subpixel_mask = gradient_subpixel_scale - 1 //-----gradient_subpixel_mask
34  };
35 
36 
37 
38  //==========================================================span_gradient
39  template<class ColorT,
40  class Interpolator,
41  class GradientF,
42  class ColorF>
44  {
45  public:
46  typedef Interpolator interpolator_type;
47  typedef ColorT color_type;
48 
49  enum downscale_shift_e
50  {
51  downscale_shift = interpolator_type::subpixel_shift -
52  gradient_subpixel_shift
53  };
54 
55  //--------------------------------------------------------------------
56  span_gradient() {}
57 
58  //--------------------------------------------------------------------
59  span_gradient(interpolator_type& inter,
60  GradientF& gradient_function,
61  ColorF& color_function,
62  double d1, double d2) :
63  m_interpolator(&inter),
64  m_gradient_function(&gradient_function),
65  m_color_function(&color_function),
66  m_d1(iround(d1 * gradient_subpixel_scale)),
67  m_d2(iround(d2 * gradient_subpixel_scale))
68  {}
69 
70  //--------------------------------------------------------------------
71  interpolator_type& interpolator() { return *m_interpolator; }
72  const GradientF& gradient_function() const { return *m_gradient_function; }
73  const ColorF& color_function() const { return *m_color_function; }
74  double d1() const { return double(m_d1) / gradient_subpixel_scale; }
75  double d2() const { return double(m_d2) / gradient_subpixel_scale; }
76 
77  //--------------------------------------------------------------------
78  void interpolator(interpolator_type& i) { m_interpolator = &i; }
79  void gradient_function(GradientF& gf) { m_gradient_function = &gf; }
80  void color_function(ColorF& cf) { m_color_function = &cf; }
81  void d1(double v) { m_d1 = iround(v * gradient_subpixel_scale); }
82  void d2(double v) { m_d2 = iround(v * gradient_subpixel_scale); }
83 
84  //--------------------------------------------------------------------
85  void prepare() {}
86 
87  //--------------------------------------------------------------------
88  void generate(color_type* span, int x, int y, unsigned len)
89  {
90  int dd = m_d2 - m_d1;
91  if(dd < 1) dd = 1;
92  m_interpolator->begin(x+0.5, y+0.5, len);
93  do
94  {
95  m_interpolator->coordinates(&x, &y);
96  int d = m_gradient_function->calculate(x >> downscale_shift,
97  y >> downscale_shift, m_d2);
98  d = ((d - m_d1) * (int)m_color_function->size()) / dd;
99  if(d < 0) d = 0;
100  if(d >= (int)m_color_function->size()) d = m_color_function->size() - 1;
101  *span++ = (*m_color_function)[d];
102  ++(*m_interpolator);
103  }
104  while(--len);
105  }
106 
107  private:
108  interpolator_type* m_interpolator;
109  GradientF* m_gradient_function;
110  ColorF* m_color_function;
111  int m_d1;
112  int m_d2;
113  };
114 
115 
116 
117 
118  //=====================================================gradient_linear_color
119  template<class ColorT>
121  {
122  typedef ColorT color_type;
123 
125  gradient_linear_color(const color_type& c1, const color_type& c2,
126  unsigned size = 256) :
127  m_c1(c1), m_c2(c2), m_size(size)
128  // VFALCO 4/28/09
129  ,m_mult(1/(double(size)-1))
130  // VFALCO
131  {}
132 
133  unsigned size() const { return m_size; }
134  color_type operator [] (unsigned v) const
135  {
136  // VFALCO 4/28/09
137  //return m_c1.gradient(m_c2, double(v) / double(m_size - 1));
138  return m_c1.gradient(m_c2, double(v) * m_mult );
139  // VFALCO
140  }
141 
142  void colors(const color_type& c1, const color_type& c2, unsigned size = 256)
143  {
144  m_c1 = c1;
145  m_c2 = c2;
146  m_size = size;
147  // VFALCO 4/28/09
148  m_mult=1/(double(size)-1);
149  // VFALCO
150  }
151 
152  color_type m_c1;
153  color_type m_c2;
154  unsigned m_size;
155  // VFALCO 4/28/09
156  double m_mult;
157  // VFALCO
158  };
159 
160 
161 
162 
163 
164 
165  //==========================================================gradient_circle
167  {
168  // Actually the same as radial. Just for compatibility
169  public:
170  static AGG_INLINE int calculate(int x, int y, int)
171  {
172  return int(fast_sqrt(x*x + y*y));
173  }
174  };
175 
176 
177  //==========================================================gradient_radial
179  {
180  public:
181  static AGG_INLINE int calculate(int x, int y, int)
182  {
183  return int(fast_sqrt(x*x + y*y));
184  }
185  };
186 
187  //========================================================gradient_radial_d
189  {
190  public:
191  static AGG_INLINE int calculate(int x, int y, int)
192  {
193  return uround(std::sqrt(double(x)*double(x) + double(y)*double(y)));
194  }
195  };
196 
197  //====================================================gradient_radial_focus
199  {
200  public:
201  //---------------------------------------------------------------------
203  m_r(100 * gradient_subpixel_scale),
204  m_fx(0),
205  m_fy(0)
206  {
207  update_values();
208  }
209 
210  //---------------------------------------------------------------------
211  gradient_radial_focus(double r, double fx, double fy) :
212  m_r (iround(r * gradient_subpixel_scale)),
213  m_fx(iround(fx * gradient_subpixel_scale)),
214  m_fy(iround(fy * gradient_subpixel_scale))
215  {
216  update_values();
217  }
218 
219  //---------------------------------------------------------------------
220  void init(double r, double fx, double fy)
221  {
222  m_r = iround(r * gradient_subpixel_scale);
223  m_fx = iround(fx * gradient_subpixel_scale);
224  m_fy = iround(fy * gradient_subpixel_scale);
225  update_values();
226  }
227 
228  //---------------------------------------------------------------------
229  double radius() const { return double(m_r) / gradient_subpixel_scale; }
230  double focus_x() const { return double(m_fx) / gradient_subpixel_scale; }
231  double focus_y() const { return double(m_fy) / gradient_subpixel_scale; }
232 
233  //---------------------------------------------------------------------
234  int calculate(int x, int y, int) const
235  {
236  double dx = x - m_fx;
237  double dy = y - m_fy;
238  double d2 = dx * m_fy - dy * m_fx;
239  double d3 = m_r2 * (dx * dx + dy * dy) - d2 * d2;
240  return iround((dx * m_fx + dy * m_fy + std::sqrt(std::fabs(d3))) * m_mul);
241  }
242 
243  private:
244  //---------------------------------------------------------------------
245  void update_values()
246  {
247  // Calculate the invariant values. In case the focal center
248  // lies exactly on the gradient circle the divisor degenerates
249  // into zero. In this case we just move the focal center by
250  // one subpixel unit possibly in the direction to the origin (0,0)
251  // and calculate the values again.
252  //-------------------------
253  m_r2 = double(m_r) * double(m_r);
254  m_fx2 = double(m_fx) * double(m_fx);
255  m_fy2 = double(m_fy) * double(m_fy);
256  double d = (m_r2 - (m_fx2 + m_fy2));
257  if(d == 0)
258  {
259  if(m_fx) { if(m_fx < 0) ++m_fx; else --m_fx; }
260  if(m_fy) { if(m_fy < 0) ++m_fy; else --m_fy; }
261  m_fx2 = double(m_fx) * double(m_fx);
262  m_fy2 = double(m_fy) * double(m_fy);
263  d = (m_r2 - (m_fx2 + m_fy2));
264  }
265  m_mul = m_r / d;
266  }
267 
268  int m_r;
269  int m_fx;
270  int m_fy;
271  double m_r2;
272  double m_fx2;
273  double m_fy2;
274  double m_mul;
275  };
276 
277 
278  //==============================================================gradient_x
280  {
281  public:
282  static int calculate(int x, int, int) { return x; }
283  };
284 
285 
286  //==============================================================gradient_y
288  {
289  public:
290  static int calculate(int, int y, int) { return y; }
291  };
292 
293  //========================================================gradient_diamond
295  {
296  public:
297  static AGG_INLINE int calculate(int x, int y, int)
298  {
299  int ax = std::abs(x);
300  int ay = std::abs(y);
301  return ax > ay ? ax : ay;
302  }
303  };
304 
305  //=============================================================gradient_xy
307  {
308  public:
309  static AGG_INLINE int calculate(int x, int y, int d)
310  {
311  return std::abs(x) * std::abs(y) / d;
312  }
313  };
314 
315  //========================================================gradient_sqrt_xy
317  {
318  public:
319  static AGG_INLINE int calculate(int x, int y, int)
320  {
321  return fast_sqrt(std::abs(x) * std::abs(y));
322  }
323  };
324 
325  //==========================================================gradient_conic
327  {
328  public:
329  static AGG_INLINE int calculate(int x, int y, int d)
330  {
331  return uround(std::fabs(std::atan2(double(y), double(x))) * double(d) / pi);
332  }
333  };
334 
335  //=================================================gradient_repeat_adaptor
336  template<class GradientF> class gradient_repeat_adaptor
337  {
338  public:
339  gradient_repeat_adaptor(const GradientF& gradient) :
340  m_gradient(&gradient) {}
341 
342  AGG_INLINE int calculate(int x, int y, int d) const
343  {
344  int ret = m_gradient->calculate(x, y, d) % d;
345  if(ret < 0) ret += d;
346  return ret;
347  }
348 
349  private:
350  const GradientF* m_gradient;
351  };
352 
353  //================================================gradient_reflect_adaptor
354  template<class GradientF> class gradient_reflect_adaptor
355  {
356  public:
357  gradient_reflect_adaptor(const GradientF& gradient) :
358  m_gradient(&gradient) {}
359 
360  AGG_INLINE int calculate(int x, int y, int d) const
361  {
362  int d2 = d << 1;
363  int ret = m_gradient->calculate(x, y, d) % d2;
364  if(ret < 0) ret += d2;
365  if(ret >= d) ret = d2 - ret;
366  return ret;
367  }
368 
369  private:
370  const GradientF* m_gradient;
371  };
372 
373 
374 }
375 
376 #endif
Definition: agg_arc.cpp:24