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