1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include "agg_basics.h"
5 #include "agg_rendering_buffer.h"
6 #include "agg_rasterizer_scanline_aa.h"
7 #include "agg_rasterizer_outline.h"
8 #include "agg_path_storage.h"
9 #include "agg_conv_stroke.h"
10 #include "agg_conv_transform.h"
11 #include "agg_bounding_rect.h"
12 #include "agg_scanline_u.h"
13 #include "agg_scanline_p.h"
14 #include "agg_pixfmt_rgb.h"
15 #include "agg_renderer_base.h"
16 #include "agg_renderer_outline_aa.h"
17 #include "agg_rasterizer_outline_aa.h"
18 #include "agg_renderer_scanline.h"
19 #include "agg_span_allocator.h"
20 #include "agg_ellipse.h"
21 #include "platform/agg_platform_support.h"
22 
23 enum flip_y_e { flip_y = true };
24 
25 agg::path_storage g_path;
26 agg::rgba8        g_colors[100];
27 unsigned          g_path_idx[100];
28 unsigned          g_npaths = 0;
29 double            g_x1 = 0;
30 double            g_y1 = 0;
31 double            g_x2 = 0;
32 double            g_y2 = 0;
33 double            g_base_dx = 0;
34 double            g_base_dy = 0;
35 double            g_angle = 0;
36 double            g_scale = 1.0;
37 double            g_skew_x = 0;
38 double            g_skew_y = 0;
39 int               g_nclick = 0;
40 
41 
42 
43 
44 unsigned parse_lion(agg::path_storage& ps, agg::rgba8* colors, unsigned* path_idx);
parse_lion()45 void parse_lion()
46 {
47     g_npaths = parse_lion(g_path, g_colors, g_path_idx);
48     agg::pod_array_adaptor<unsigned> path_idx(g_path_idx, 100);
49     agg::bounding_rect(g_path, path_idx, 0, g_npaths, &g_x1, &g_y1, &g_x2, &g_y2);
50     g_base_dx = (g_x2 - g_x1) / 2.0;
51     g_base_dy = (g_y2 - g_y1) / 2.0;
52 }
53 
54 
55 
56 
57 
58 
59 namespace agg
60 {
61     template<class Order> class span_simple_blur_rgb24
62     {
63     public:
64         //--------------------------------------------------------------------
65         typedef rgba8 color_type;
66 
67         //--------------------------------------------------------------------
span_simple_blur_rgb24()68         span_simple_blur_rgb24() : m_source_image(0) {}
69 
70         //--------------------------------------------------------------------
span_simple_blur_rgb24(const rendering_buffer & src)71         span_simple_blur_rgb24(const rendering_buffer& src) :
72             m_source_image(&src) {}
73 
74         //--------------------------------------------------------------------
source_image(const rendering_buffer & src)75         void source_image(const rendering_buffer& src) { m_source_image = &src; }
source_image() const76         const rendering_buffer& source_image() const { return *m_source_image; }
77 
78         //--------------------------------------------------------------------
prepare()79         void prepare() {}
80 
81         //--------------------------------------------------------------------
generate(color_type * span,int x,int y,int len)82         void generate(color_type* span, int x, int y, int len)
83         {
84             if(y < 1 || y >= int(m_source_image->height() - 1))
85             {
86                 do
87                 {
88                     *span++ = rgba8(0,0,0,0);
89                 }
90                 while(--len);
91                 return;
92             }
93 
94             do
95             {
96                 int color[4];
97                 color[0] = color[1] = color[2] = color[3] = 0;
98                 if(x > 0 && x < int(m_source_image->width()-1))
99                 {
100                     int i = 3;
101                     do
102                     {
103                         const int8u* ptr = m_source_image->row_ptr(y - i + 2) + (x - 1) * 3;
104 
105                         color[0] += *ptr++;
106                         color[1] += *ptr++;
107                         color[2] += *ptr++;
108                         color[3] += 255;
109 
110                         color[0] += *ptr++;
111                         color[1] += *ptr++;
112                         color[2] += *ptr++;
113                         color[3] += 255;
114 
115                         color[0] += *ptr++;
116                         color[1] += *ptr++;
117                         color[2] += *ptr++;
118                         color[3] += 255;
119                     }
120                     while(--i);
121                     color[0] /= 9;
122                     color[1] /= 9;
123                     color[2] /= 9;
124                     color[3] /= 9;
125                 }
126                 *span++ = rgba8(color[Order::R], color[Order::G], color[Order::B], color[3]);
127                 ++x;
128             }
129             while(--len);
130         }
131 
132     private:
133         const rendering_buffer* m_source_image;
134     };
135 
136 }
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 class the_application : public agg::platform_support
150 {
151     double m_cx;
152     double m_cy;
153 
154 public:
~the_application()155     virtual ~the_application()
156     {
157     }
158 
the_application(agg::pix_format_e format,bool flip_y)159     the_application(agg::pix_format_e format, bool flip_y) :
160         agg::platform_support(format, flip_y),
161         m_cx(100),
162         m_cy(102)
163     {
164         parse_lion();
165     }
166 
on_resize(int cx,int cy)167     virtual void on_resize(int cx, int cy)
168     {
169     }
170 
on_draw()171     virtual void on_draw()
172     {
173         typedef agg::pixfmt_bgr24 pixfmt;
174         typedef agg::renderer_base<pixfmt> renderer_base;
175         typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_solid;
176 
177         pixfmt pixf(rbuf_window());
178         renderer_base rb(pixf);
179         renderer_solid rs(rb);
180 
181         rb.clear(agg::rgba(1, 1, 1));
182 
183         agg::trans_affine mtx;
184         agg::conv_transform<agg::path_storage, agg::trans_affine> trans(g_path, mtx);
185 
186         mtx *= agg::trans_affine_translation(-g_base_dx, -g_base_dy);
187         mtx *= agg::trans_affine_scaling(g_scale, g_scale);
188         mtx *= agg::trans_affine_rotation(g_angle + agg::pi);
189         mtx *= agg::trans_affine_skewing(g_skew_x/1000.0, g_skew_y/1000.0);
190         mtx *= agg::trans_affine_translation(initial_width()/4, initial_height()/2);
191         mtx *= trans_affine_resizing();
192 
193         agg::rasterizer_scanline_aa<> ras2;
194         agg::scanline_p8 sl;
195         agg::scanline_u8 sl2;
196 
197         agg::render_all_paths(ras2, sl, rs, trans, g_colors, g_path_idx, g_npaths);
198 
199         mtx *= ~trans_affine_resizing();
200         mtx *= agg::trans_affine_translation(initial_width()/2, 0);
201         mtx *= trans_affine_resizing();
202 
203         agg::line_profile_aa profile;
204         profile.width(1.0);
205         agg::renderer_outline_aa<renderer_base> rp(rb, profile);
206         agg::rasterizer_outline_aa<agg::renderer_outline_aa<renderer_base> > ras(rp);
207         ras.round_cap(true);
208 
209         ras.render_all_paths(trans, g_colors, g_path_idx, g_npaths);
210 
211         agg::ellipse ell(m_cx, m_cy, 100.0, 100.0, 100);
212         agg::conv_stroke<agg::ellipse> ell_stroke1(ell);
213         ell_stroke1.width(6.0);
214         agg::conv_stroke<agg::conv_stroke<agg::ellipse> > ell_stroke2(ell_stroke1);
215 
216         ell_stroke2.width(2.0);
217         rs.color(agg::rgba(0,0.2,0));
218         ras2.add_path(ell_stroke2);
219         agg::render_scanlines(ras2, sl, rs);
220 
221         typedef agg::span_simple_blur_rgb24<agg::order_bgr> span_blur_gen;
222         typedef agg::span_allocator<span_blur_gen::color_type> span_blur_alloc;
223 
224         span_blur_alloc sa;
225         span_blur_gen sg;
226 
227         sg.source_image(rbuf_img(0));
228         ras2.add_path(ell);
229 
230         copy_window_to_img(0);
231         agg::render_scanlines_aa(ras2, sl2, rb, sa, sg);
232 
233         // More blur if desired :-)
234         //copy_window_to_img(0);
235         //agg::render_scanlines(ras2, sl2, rblur);
236         //copy_window_to_img(0);
237         //agg::render_scanlines(ras2, sl2, rblur);
238         //copy_window_to_img(0);
239         //agg::render_scanlines(ras2, sl2, rblur);
240     }
241 
242 
on_mouse_button_down(int x,int y,unsigned flags)243     virtual void on_mouse_button_down(int x, int y, unsigned flags)
244     {
245         if(flags & agg::mouse_left)
246         {
247             m_cx = x;
248             m_cy = y;
249             force_redraw();
250         }
251     }
252 
253 
on_mouse_move(int x,int y,unsigned flags)254     virtual void on_mouse_move(int x, int y, unsigned flags)
255     {
256         on_mouse_button_down(x, y, flags);
257     }
258 
259 };
260 
261 
262 
263 
264 
265 
agg_main(int argc,char * argv[])266 int agg_main(int argc, char* argv[])
267 {
268     the_application app(agg::pix_format_bgr24, flip_y);
269     app.caption("AGG Example. Lion with Alpha-Masking");
270 
271     if(app.init(512, 400, agg::window_resize))
272     {
273         return app.run();
274     }
275     return 1;
276 }
277 
278 
279 
280 
281 
282 
283