1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: ..\;..\ctrl;..\svg;..\util;..\platform\win;expat-wrap
//
program
alpha_gradient ;
uses
agg_basics ,
agg_platform_support ,
agg_color ,
agg_pixfmt ,
agg_pixfmt_rgb ,
agg_ctrl ,
agg_spline_ctrl ,
agg_renderer_base ,
agg_renderer_scanline ,
agg_rasterizer_scanline_aa ,
agg_scanline ,
agg_scanline_u ,
agg_render_scanlines ,
agg_array ,
agg_span_gradient ,
agg_span_gradient_alpha ,
agg_span_interpolator_linear ,
agg_span_converter ,
agg_span_allocator ,
agg_ellipse ,
agg_vcgen_stroke ,
agg_trans_affine ,
agg_math ;
{$I agg_mode.inc }
const
flip_y = true;
type
the_application = object(platform_support )
m_x ,
m_y : array[0..2 ] of double;
m_dx ,
m_dy : double;
m_idx : int;
m_alpha : spline_ctrl;
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
destructor Destruct;
procedure fill_color_array(array_ : pod_auto_array_ptr; begin_ ,middle_ ,end_ : aggclr_ptr );
procedure on_draw; virtual;
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
procedure on_mouse_button_up (x ,y : int; flags : unsigned ); virtual;
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
end;
{ CONSTRUCT }
constructor the_application.Construct;
begin
inherited Construct(format_ ,flip_y_ );
m_alpha.Construct(2 ,2 ,200 ,30 ,6 ,not flip_y_ );
m_idx:=-1;
m_x[0 ]:=257;
m_y[0 ]:=60;
m_x[1 ]:=369;
m_y[1 ]:=170;
m_x[2 ]:=143;
m_y[2 ]:=310;
m_alpha.point_(0 ,0.0 ,0.0);
m_alpha.point_(1 ,1.0 / 5.0 ,1.0 - 4.0 / 5.0 );
m_alpha.point_(2 ,2.0 / 5.0 ,1.0 - 3.0 / 5.0 );
m_alpha.point_(3 ,3.0 / 5.0 ,1.0 - 2.0 / 5.0 );
m_alpha.point_(4 ,4.0 / 5.0 ,1.0 - 1.0 / 5.0 );
m_alpha.point_(5 ,1.0 ,1.0 );
m_alpha.update_spline;
add_ctrl(@m_alpha );
end;
{ DESTRUCT }
destructor the_application.Destruct;
begin
inherited Destruct;
m_alpha.Destruct;
end;
{ FILL_COLOR_ARRAY }
procedure the_application.fill_color_array;
var
i : unsigned;
begin
i:=0;
while i < 128 do
begin
aggclr_ptr(array_.array_operator(i ) )^:=
begin_.gradient(middle_ ,i / 128.0 );
inc(i );
end;
while i < 256 do
begin
aggclr_ptr(array_.array_operator(i ) )^:=
middle_.gradient(end_ ,(i - 128 ) / 128.0 );
inc(i );
end;
end;
{ ON_DRAW }
procedure the_application.on_draw;
type
// Gradient shape function (linear, radial, custom, etc)
gradient_func_type = gradient_circle;
// Alpha gradient shape function (linear, radial, custom, etc)
gradient_alpha_func_type = gradient_xy;
// Span interpolator. This object is used in all span generators
// that operate with transformations during iterating of the spans,
// for example, image transformers use the interpolator too.
interpolator_type = span_interpolator_linear;
// Span allocator is an object that allocates memory for
// the array of colors that will be used to render the
// color spans. One object can be shared between different
// span generators.
span_allocator_type = span_allocator;
// Gradient colors array adaptor
gradient_colors_type = pod_auto_array;
// Finally, the gradient span generator working with the color_type
// color type.
span_gradient_type = span_gradient;
// Gradient alpha array adaptor
gradient_alpha_type = pod_auto_array;
// The alpha gradient span converter working with the color_type
// color type.
span_gradient_alpha_type = span_gradient_alpha;
// Span converter type
span_conv_type = span_converter;
// The gradient (plus span converter) renderer type
renderer_gradient_type = renderer_scanline_aa;
var
pf : pixel_formats;
rgba ,
rgbb ,
rgbc : aggclr;
ren_base : renderer_base;
ren_solid : renderer_scanline_aa_solid;
sl : scanline_u8;
ras : rasterizer_scanline_aa;
ell : ellipse;
i ,w ,h : unsigned;
parallelogram : array[0..5 ] of double;
tas : trans_affine_scaling;
tar : trans_affine_rotation;
tat : trans_affine_translation;
stroke : vcgen_stroke;
// The gradient objects declarations
gradient_func : gradient_func_type; // The gradient function
alpha_func : gradient_alpha_func_type; // The gradient function
gradient_mtx , // Gradient affine transformer
alpha_mtx : trans_affine; // Alpha affine transformer
span_interpolator , // Span gradient interpolator
span_interpolator_alpha : interpolator_type; // Span alpha interpolator
span_allocator : span_allocator_type; // Span Allocator
color_array : gradient_colors_type; // The gradient colors
span_gradient : span_gradient_type;
alpha_array : gradient_alpha_type;
span_gradient_alpha : span_gradient_alpha_type;
span_conv : span_conv_type;
ren_gradient : renderer_gradient_type;
begin
// Initialize structures
pixfmt_bgr24(pf ,rbuf_window );
ren_base.Construct (@pf );
ren_solid.Construct(@ren_base );
rgba.ConstrDbl(1 ,1 ,1 );
ren_base.clear(@rgba );
sl.Construct;
ras.Construct;
// Draw some background
ell.Construct;
RandSeed:=1234;
w:=trunc(_width );
h:=trunc(_height );
for i:=0 to 99 do
begin
ell.init(
Random($7fff ) mod w ,Random($7fff ) mod h ,
Random($7fff ) mod 60 + 5 ,Random($7fff ) mod 60 + 5 ,50 );
rgba.ConstrDbl(
Random($7fff ) / $7fff ,
Random($7fff ) / $7fff ,
Random($7fff ) / $7fff ,
Random($7fff ) / $7fff / 2.0 );
ren_solid.color_(@rgba );
ras.add_path (@ell);
render_scanlines(@ras ,@sl ,@ren_solid );
end;
parallelogram[0 ]:=m_x[0 ];
parallelogram[1 ]:=m_y[0 ];
parallelogram[2 ]:=m_x[1 ];
parallelogram[3 ]:=m_y[1 ];
parallelogram[4 ]:=m_x[2 ];
parallelogram[5 ]:=m_y[2 ];
// The gradient objects initializations
gradient_func.Construct;
alpha_func.Construct;
gradient_mtx.Construct;
alpha_mtx.Construct;
span_interpolator.Construct (@gradient_mtx );
span_interpolator_alpha.Construct(@alpha_mtx );
span_allocator.Construct;
color_array.Construct(256 ,sizeof(aggclr ) );
// Initialize the gradient span itself.
// The last two arguments are so called "d1" and "d2"
// defining two distances in pixels, where the gradient starts
// and where it ends. The actual meaning of "d1" and "d2" depands
// on the gradient function.
span_gradient.Construct(
@span_allocator ,
@span_interpolator ,
@gradient_func ,
@color_array ,0 ,150 );
alpha_array.Construct(256 ,sizeof(int8u ) );
span_gradient_alpha.Construct(
@span_interpolator_alpha ,
@alpha_func ,
@alpha_array ,0 ,100 );
// Span converter initialization
span_conv.Construct(@span_gradient ,@span_gradient_alpha );
// The gradient renderer
ren_gradient.Construct(@ren_base ,@span_conv );
// Finally we can draw a circle
tas.Construct(0.75 ,1.2 );
tar.Construct(pi / 3.0 );
tat.Construct(_width / 2 ,_height / 2 );
gradient_mtx.multiply(@tas );
gradient_mtx.multiply(@tar );
gradient_mtx.multiply(@tat );
gradient_mtx.invert;
alpha_mtx.parl_to_rect(@parallelogram ,-100 ,-100 ,100 ,100 );
rgba.ConstrDbl(0 ,0.19 ,0.19 );
rgbb.ConstrDbl(0.7 ,0.7 ,0.19 );
rgbc.ConstrDbl(0.31 ,0 ,0 );
fill_color_array(@color_array ,@rgba ,@rgbb ,@rgbc );
// Fill Alpha array
for i:=0 to 255 do
int8u_ptr(alpha_array.array_operator(i ) )^:=
int8u(trunc(m_alpha._value(i / 255.0 ) * base_mask ) );
ell.init(_width / 2 ,_height / 2 ,150 ,150 ,100 );
ras.add_path (@ell);
render_scanlines(@ras ,@sl ,@ren_gradient );
// Draw the control points and the parallelogram
rgba.ConstrDbl (0 ,0.4 ,0.4 ,0.31 );
ren_solid.color_(@rgba );
ell.init (m_x[0 ] ,m_y[0 ] ,5 ,5 ,20 );
ras.add_path (@ell);
render_scanlines(@ras ,@sl ,@ren_solid );
ell.init (m_x[1 ] ,m_y[1 ] ,5 ,5 ,20 );
ras.add_path (@ell );
render_scanlines(@ras ,@sl ,@ren_solid );
ell.init (m_x[2 ] ,m_y[2 ] ,5 ,5 ,20 );
ras.add_path (@ell );
render_scanlines(@ras ,@sl ,@ren_solid );
stroke.Construct;
stroke.add_vertex(m_x[0 ] ,m_y[0 ] ,path_cmd_move_to );
stroke.add_vertex(m_x[1 ] ,m_y[1 ] ,path_cmd_line_to );
stroke.add_vertex(m_x[2 ] ,m_y[2 ] ,path_cmd_line_to );
stroke.add_vertex(m_x[0 ] + m_x[2 ] - m_x[1 ] ,m_y[0 ] + m_y[2 ] - m_y[1 ] ,path_cmd_line_to );
stroke.add_vertex(0 ,0 ,path_cmd_end_poly or path_flags_close );
rgba.ConstrDbl (0 ,0 ,0 );
ren_solid.color_(@rgba );
ras.add_path (@stroke);
render_scanlines(@ras ,@sl ,@ren_solid );
// Render the controls
render_ctrl(@ras ,@sl ,@ren_solid ,@m_alpha );
// Free AGG resources
sl.Destruct;
ras.Destruct;
span_allocator.Destruct;
color_array.Destruct;
alpha_array.Destruct;
stroke.Destruct;
end;
{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
var
dx ,dy : double;
begin
if flags and mouse_left <> 0 then
begin
if m_idx = 3 then
begin
dx:=x - m_dx;
dy:=y - m_dy;
m_x[1 ]:=m_x[1 ] - (m_x[0 ] - dx );
m_y[1 ]:=m_y[1 ] - (m_y[0 ] - dy );
m_x[2 ]:=m_x[2 ] - (m_x[0 ] - dx );
m_y[2 ]:=m_y[2 ] - (m_y[0 ] - dy );
m_x[0 ]:=dx;
m_y[0 ]:=dy;
force_redraw;
exit;
end;
if m_idx >= 0 then
begin
m_x[m_idx ]:=x - m_dx;
m_y[m_idx ]:=y - m_dy;
force_redraw;
end;
end
else
on_mouse_button_up(x ,y ,flags );
end;
{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
var
i : unsigned;
begin
if flags and mouse_left <> 0 then
begin
i:=0;
while i < 3 do
begin
if Sqrt((x - m_x[i ] ) * (x - m_x[i ] ) + (y - m_y[i ] ) * (y - m_y[i ] ) ) < 10.0 then
begin
m_dx :=x - m_x[i ];
m_dy :=y - m_y[i ];
m_idx:=i;
break;
end;
inc(i );
end;
if i = 3 then
if point_in_triangle(
m_x[0 ] ,m_y[0 ] ,
m_x[1 ] ,m_y[1 ] ,
m_x[2 ] ,m_y[2 ] ,x ,y ) then
begin
m_dx :=x - m_x[0 ];
m_dy :=y - m_y[0 ];
m_idx:=3;
end;
end;
end;
{ ON_MOUSE_BUTTON_UP }
procedure the_application.on_mouse_button_up;
begin
m_idx:=-1;
end;
{ ON_KEY }
procedure the_application.on_key;
var
dx ,dy : double;
begin
dx:=0;
dy:=0;
case key of
key_left : dx:=-0.1;
key_right : dx:= 0.1;
key_up : dy:= 0.1;
key_down : dy:=-0.1;
end;
m_x[0 ]:=m_x[0 ] + dx;
m_y[0 ]:=m_y[0 ] + dy;
m_x[1 ]:=m_x[1 ] + dx;
m_y[1 ]:=m_y[1 ] + dy;
force_redraw;
if key = key_f1 then
message_(
'The demo shows how to combine any span generator with alpha-channel gradient.'#13#13 +
'How to play with:'#13#13 +
'Use the mouse to move the parallelogram around.'#13 +
'Use the arrow keys to move the parallelogram very precisely.' +
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
end;
VAR
app : the_application;
BEGIN
app.Construct(pix_format_bgr24 ,flip_y );
app.caption_ ('AGG Example. Alpha channel gradient (F1-Help)' );
if app.init(400 ,320 ,window_resize ) then
app.run;
app.Destruct;
END.
|