| 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 |
// [email protected] |
| 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 |
// 02.02.2008-Milano: Ported from Object Pascal code of AggPas |
| 21 |
// |
| 22 |
#ifndef AGG_SPAN_GRADIENT_CONTOUR_INCLUDED |
| 23 |
#define AGG_SPAN_GRADIENT_CONTOUR_INCLUDED |
| 24 |
|
| 25 |
#include "agg_basics.h" |
| 26 |
#include "agg_trans_affine.h" |
| 27 |
#include "agg_path_storage.h" |
| 28 |
#include "agg_pixfmt_gray.h" |
| 29 |
#include "agg_conv_transform.h" |
| 30 |
#include "agg_conv_curve.h" |
| 31 |
#include "agg_bounding_rect.h" |
| 32 |
#include "agg_renderer_base.h" |
| 33 |
#include "agg_renderer_primitives.h" |
| 34 |
#include "agg_rasterizer_outline.h" |
| 35 |
#include "agg_span_gradient.h" |
| 36 |
|
| 37 |
#define infinity 1E20 |
| 38 |
|
| 39 |
namespace agg |
| 40 |
{ |
| 41 |
|
| 42 |
//==========================================================gradient_contour |
| 43 |
class gradient_contour |
| 44 |
{ |
| 45 |
private: |
| 46 |
int8u* m_buffer; |
| 47 |
int m_width; |
| 48 |
int m_height; |
| 49 |
int m_frame; |
| 50 |
|
| 51 |
double m_d1; |
| 52 |
double m_d2; |
| 53 |
|
| 54 |
public: |
| 55 |
gradient_contour() : |
| 56 |
m_buffer(NULL), |
| 57 |
m_width(0), |
| 58 |
m_height(0), |
| 59 |
m_frame(10), |
| 60 |
m_d1(0), |
| 61 |
m_d2(100) |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
gradient_contour(double d1, double d2) : |
| 66 |
m_buffer(NULL), |
| 67 |
m_width(0), |
| 68 |
m_height(0), |
| 69 |
m_frame(10), |
| 70 |
m_d1(d1), |
| 71 |
m_d2(d2) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
~gradient_contour() |
| 76 |
{ |
| 77 |
if (m_buffer) |
| 78 |
{ |
| 79 |
delete [] m_buffer; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
int8u* contour_create(path_storage* ps ); |
| 84 |
|
| 85 |
int contour_width() { return m_width; } |
| 86 |
int contour_height() { return m_height; } |
| 87 |
|
| 88 |
void d1(double d ) { m_d1 = d; } |
| 89 |
void d2(double d ) { m_d2 = d; } |
| 90 |
|
| 91 |
void frame(int f ) { m_frame = f; } |
| 92 |
int frame() { return m_frame; } |
| 93 |
|
| 94 |
int calculate(int x, int y, int d) const |
| 95 |
{ |
| 96 |
if (m_buffer) |
| 97 |
{ |
| 98 |
int px = x >> agg::gradient_subpixel_shift; |
| 99 |
int py = y >> agg::gradient_subpixel_shift; |
| 100 |
|
| 101 |
px %= m_width; |
| 102 |
|
| 103 |
if (px < 0) |
| 104 |
{ |
| 105 |
px += m_width; |
| 106 |
} |
| 107 |
|
| 108 |
py %= m_height; |
| 109 |
|
| 110 |
if (py < 0 ) |
| 111 |
{ |
| 112 |
py += m_height; |
| 113 |
} |
| 114 |
|
| 115 |
return iround(m_buffer[py * m_width + px ] * (m_d2 / 256 ) + m_d1 ) << gradient_subpixel_shift; |
| 116 |
|
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
return 0; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
}; |
| 125 |
|
| 126 |
static AGG_INLINE int square(int x ) { return x * x; } |
| 127 |
|
| 128 |
// DT algorithm by: Pedro Felzenszwalb |
| 129 |
void dt(float* spanf, float* spang, float* spanr, int* spann ,int length ) |
| 130 |
{ |
| 131 |
int k = 0; |
| 132 |
float s; |
| 133 |
|
| 134 |
spann[0 ] = 0; |
| 135 |
spang[0 ] = float(-infinity ); |
| 136 |
spang[1 ] = float(+infinity ); |
| 137 |
|
| 138 |
for (int q = 1; q <= length - 1; q++) |
| 139 |
{ |
| 140 |
s = ((spanf[q ] + square(q ) ) - (spanf[spann[k ] ] + square(spann[k ] ) ) ) / (2 * q - 2 * spann[k ] ); |
| 141 |
|
| 142 |
while (s <= spang[k ]) |
| 143 |
{ |
| 144 |
k--; |
| 145 |
s = ((spanf[q ] + square(q ) ) - (spanf[spann[k ] ] + square(spann[k ] ) ) ) / (2 * q - 2 * spann[k ] ); |
| 146 |
} |
| 147 |
|
| 148 |
k++; |
| 149 |
spann[k ] = q; |
| 150 |
spang[k ] = s; |
| 151 |
spang[k + 1 ] = float(+infinity); |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
k = 0; |
| 156 |
|
| 157 |
for (int q = 0; q <= length - 1; q++) |
| 158 |
{ |
| 159 |
while (spang[k + 1 ] < q ) |
| 160 |
{ |
| 161 |
k++; |
| 162 |
} |
| 163 |
|
| 164 |
spanr[q ] = square(q - spann[k ] ) + spanf[spann[k ] ]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// DT algorithm by: Pedro Felzenszwalb |
| 169 |
int8u* gradient_contour::contour_create(path_storage* ps ) |
| 170 |
{ |
| 171 |
int8u* result = NULL; |
| 172 |
|
| 173 |
if (ps) |
| 174 |
{ |
| 175 |
// I. Render Black And White NonAA Stroke of the Path |
| 176 |
// Path Bounding Box + Some Frame Space Around [configurable] |
| 177 |
agg::conv_curve<agg::path_storage> conv(*ps); |
| 178 |
|
| 179 |
double x1, y1, x2, y2; |
| 180 |
|
| 181 |
if (agg::bounding_rect_single(conv ,0 ,&x1 ,&y1 ,&x2 ,&y2 )) |
| 182 |
{ |
| 183 |
// Create BW Rendering Surface |
| 184 |
int width = int(ceil(x2 - x1 ) ) + m_frame * 2 + 1; |
| 185 |
int height = int(ceil(y2 - y1 ) ) + m_frame * 2 + 1; |
| 186 |
|
| 187 |
int8u* buffer = new int8u[width * height]; |
| 188 |
|
| 189 |
if (buffer) |
| 190 |
{ |
| 191 |
memset(buffer ,255 ,width * height ); |
| 192 |
|
| 193 |
// Setup VG Engine & Render |
| 194 |
agg::rendering_buffer rb; |
| 195 |
rb.attach(buffer ,width ,height ,width ); |
| 196 |
|
| 197 |
agg::pixfmt_gray8 pf(rb); |
| 198 |
agg::renderer_base<agg::pixfmt_gray8> renb(pf ); |
| 199 |
|
| 200 |
agg::renderer_primitives<agg::renderer_base<agg::pixfmt_gray8> > prim(renb ); |
| 201 |
agg::rasterizer_outline<renderer_primitives<agg::renderer_base<agg::pixfmt_gray8> > > ras(prim ); |
| 202 |
|
| 203 |
agg::trans_affine mtx; |
| 204 |
mtx *= agg::trans_affine_translation(-x1 + m_frame, -y1 + m_frame ); |
| 205 |
|
| 206 |
agg::conv_transform<agg::conv_curve<agg::path_storage> > trans(conv ,mtx ); |
| 207 |
|
| 208 |
prim.line_color(agg::rgba8(0 ,0 ,0 ,255 ) ); |
| 209 |
ras.add_path(trans ); |
| 210 |
|
| 211 |
// II. Distance Transform |
| 212 |
// Create Float Buffer + 0 vs. infinity (1e20) assignment |
| 213 |
float* image = new float[width * height]; |
| 214 |
|
| 215 |
if (image) |
| 216 |
{ |
| 217 |
for (int y = 0, l = 0; y < height; y++ ) |
| 218 |
{ |
| 219 |
for (int x = 0; x < width; x++, l++ ) |
| 220 |
{ |
| 221 |
if (buffer[l ] == 0) |
| 222 |
{ |
| 223 |
image[l ] = 0.0; |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
image[l ] = float(infinity ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
// DT of 2d |
| 234 |
// SubBuff<float> max width,height |
| 235 |
int length = width; |
| 236 |
|
| 237 |
if (height > length) |
| 238 |
{ |
| 239 |
length = height; |
| 240 |
} |
| 241 |
|
| 242 |
float* spanf = new float[length]; |
| 243 |
float* spang = new float[length + 1]; |
| 244 |
float* spanr = new float[length]; |
| 245 |
int* spann = new int[length]; |
| 246 |
|
| 247 |
if ((spanf) && (spang) && (spanr) && (spann)) |
| 248 |
{ |
| 249 |
// Transform along columns |
| 250 |
for (int x = 0; x < width; x++ ) |
| 251 |
{ |
| 252 |
for (int y = 0; y < height; y++ ) |
| 253 |
{ |
| 254 |
spanf[y] = image[y * width + x]; |
| 255 |
} |
| 256 |
|
| 257 |
// DT of 1d |
| 258 |
dt(spanf ,spang ,spanr ,spann ,height ); |
| 259 |
|
| 260 |
for (int y = 0; y < height; y++ ) |
| 261 |
{ |
| 262 |
image[y * width + x] = spanr[y]; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Transform along rows |
| 267 |
for (int y = 0; y < height; y++ ) |
| 268 |
{ |
| 269 |
for (int x = 0; x < width; x++ ) |
| 270 |
{ |
| 271 |
spanf[x] = image[y * width + x]; |
| 272 |
} |
| 273 |
|
| 274 |
// DT of 1d |
| 275 |
dt(spanf ,spang ,spanr ,spann ,width ); |
| 276 |
|
| 277 |
for (int x = 0; x < width; x++ ) |
| 278 |
{ |
| 279 |
image[y * width + x] = spanr[x]; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Take Square Roots, Min & Max |
| 284 |
float min = sqrt(image[0] ); |
| 285 |
float max = min; |
| 286 |
|
| 287 |
for (int y = 0, l = 0; y < height; y++ ) |
| 288 |
{ |
| 289 |
for (int x = 0; x < width; x++, l++ ) |
| 290 |
{ |
| 291 |
image[l] = sqrt(image[l]); |
| 292 |
|
| 293 |
if (min > image[l]) |
| 294 |
{ |
| 295 |
min = image[l]; |
| 296 |
} |
| 297 |
|
| 298 |
if (max < image[l]) |
| 299 |
{ |
| 300 |
max = image[l]; |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// III. Convert To Grayscale |
| 307 |
if (min == max) |
| 308 |
{ |
| 309 |
memset(buffer ,0 ,width * height ); |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
float scale = 255 / (max - min ); |
| 314 |
|
| 315 |
for (int y = 0, l = 0; y < height; y++ ) |
| 316 |
{ |
| 317 |
for (int x = 0; x < width; x++ ,l++ ) |
| 318 |
{ |
| 319 |
buffer[l] = int8u(int((image[l] - min ) * scale )); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
// OK |
| 325 |
if (m_buffer) |
| 326 |
{ |
| 327 |
delete [] m_buffer; |
| 328 |
} |
| 329 |
|
| 330 |
m_buffer = buffer; |
| 331 |
m_width = width; |
| 332 |
m_height = height; |
| 333 |
|
| 334 |
buffer = NULL; |
| 335 |
result = m_buffer; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
if (spanf) { delete [] spanf; } |
| 340 |
if (spang) { delete [] spang; } |
| 341 |
if (spanr) { delete [] spanr; } |
| 342 |
if (spann) { delete [] spann; } |
| 343 |
|
| 344 |
delete [] image; |
| 345 |
|
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (buffer) |
| 350 |
{ |
| 351 |
delete [] buffer; |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
return result; |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
#endif |