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_path_storage.h"
8 #include "agg_conv_transform.h"
9 #include "agg_bounding_rect.h"
10 #include "agg_renderer_scanline.h"
11 #include "agg_pixfmt_rgb.h"
12 #include "agg_pixfmt_gray.h"
13 #include "agg_alpha_mask_u8.h"
14 #include "agg_scanline_u.h"
15 #include "agg_scanline_p.h"
16 #include "agg_ellipse.h"
17 #include "platform/agg_platform_support.h"
18 
19 enum flip_y_e { flip_y = true };
20 
21 agg::path_storage g_path;
22 agg::rgba8        g_colors[100];
23 unsigned          g_path_idx[100];
24 unsigned          g_npaths = 0;
25 double            g_x1 = 0;
26 double            g_y1 = 0;
27 double            g_x2 = 0;
28 double            g_y2 = 0;
29 double            g_base_dx = 0;
30 double            g_base_dy = 0;
31 double            g_angle = 0;
32 double            g_scale = 1.0;
33 double            g_skew_x = 0;
34 double            g_skew_y = 0;
35 int               g_nclick = 0;
36 
37 
38 unsigned parse_lion(agg::path_storage& ps, agg::rgba8* colors, unsigned* path_idx);
parse_lion()39 void parse_lion()
40 {
41     g_npaths = parse_lion(g_path, g_colors, g_path_idx);
42     agg::pod_array_adaptor<unsigned> path_idx(g_path_idx, 100);
43     agg::bounding_rect(g_path, path_idx, 0, g_npaths, &g_x1, &g_y1, &g_x2, &g_y2);
44     g_base_dx = (g_x2 - g_x1) / 2.0;
45     g_base_dy = (g_y2 - g_y1) / 2.0;
46 }
47 
48 
49 agg::rendering_buffer g_alpha_mask_rbuf;
50 agg::alpha_mask_gray8 g_alpha_mask(g_alpha_mask_rbuf);
51 agg::rasterizer_scanline_aa<> g_rasterizer;
52 
53 
54 class the_application : public agg::platform_support
55 {
56     unsigned char* m_alpha_buf;
57     agg::rendering_buffer m_alpha_rbuf;
58 
59 public:
~the_application()60     virtual ~the_application()
61     {
62         delete [] m_alpha_buf;
63     }
64 
the_application(agg::pix_format_e format,bool flip_y)65     the_application(agg::pix_format_e format, bool flip_y) :
66         agg::platform_support(format, flip_y),
67         m_alpha_buf(0)
68     {
69         parse_lion();
70     }
71 
72 
generate_alpha_mask(int cx,int cy)73     void generate_alpha_mask(int cx, int cy)
74     {
75         delete [] m_alpha_buf;
76         m_alpha_buf = new unsigned char[cx * cy];
77         g_alpha_mask_rbuf.attach(m_alpha_buf, cx, cy, cx);
78 
79         typedef agg::renderer_base<agg::pixfmt_gray8> ren_base;
80         typedef agg::renderer_scanline_aa_solid<ren_base> renderer;
81 
82         agg::pixfmt_gray8 pixf(g_alpha_mask_rbuf);
83         ren_base rb(pixf);
84         renderer r(rb);
85         agg::scanline_p8 sl;
86 
87         rb.clear(agg::gray8(0));
88 
89         agg::ellipse ell;
90 
91         int i;
92         for(i = 0; i < 10; i++)
93         {
94             ell.init(rand() % cx,
95                      rand() % cy,
96                      rand() % 100 + 20,
97                      rand() % 100 + 20,
98                      100);
99 
100             g_rasterizer.add_path(ell);
101             r.color(agg::gray8(rand() & 0xFF, rand() & 0xFF));
102             agg::render_scanlines(g_rasterizer, sl, r);
103         }
104     }
105 
106 
on_resize(int cx,int cy)107     virtual void on_resize(int cx, int cy)
108     {
109         generate_alpha_mask(cx, cy);
110     }
111 
on_draw()112     virtual void on_draw()
113     {
114         int width = rbuf_window().width();
115         int height = rbuf_window().height();
116 
117         typedef agg::scanline_u8_am<agg::alpha_mask_gray8> scanline_type;
118         typedef agg::renderer_base<agg::pixfmt_bgr24> ren_base;
119         typedef agg::renderer_scanline_aa_solid<ren_base> renderer;
120 
121         agg::pixfmt_bgr24 pixf(rbuf_window());
122         ren_base rb(pixf);
123         renderer r(rb);
124 
125         scanline_type sl(g_alpha_mask);
126         rb.clear(agg::rgba8(255, 255, 255));
127 
128         agg::trans_affine mtx;
129         mtx *= agg::trans_affine_translation(-g_base_dx, -g_base_dy);
130         mtx *= agg::trans_affine_scaling(g_scale, g_scale);
131         mtx *= agg::trans_affine_rotation(g_angle + agg::pi);
132         mtx *= agg::trans_affine_skewing(g_skew_x/1000.0, g_skew_y/1000.0);
133         mtx *= agg::trans_affine_translation(width/2, height/2);
134 
135         agg::conv_transform<agg::path_storage, agg::trans_affine> trans(g_path, mtx);
136 
137         agg::render_all_paths(g_rasterizer, sl, r, trans, g_colors, g_path_idx, g_npaths);
138     }
139 
140 
transform(double width,double height,double x,double y)141     void transform(double width, double height, double x, double y)
142     {
143         x -= width / 2;
144         y -= height / 2;
145         g_angle = atan2(y, x);
146         g_scale = sqrt(y * y + x * x) / 100.0;
147     }
148 
149 
on_mouse_button_down(int x,int y,unsigned flags)150     virtual void on_mouse_button_down(int x, int y, unsigned flags)
151     {
152         if(flags & agg::mouse_left)
153         {
154             int width = rbuf_window().width();
155             int height = rbuf_window().height();
156             transform(width, height, x, y);
157             force_redraw();
158         }
159 
160         if(flags & agg::mouse_right)
161         {
162             g_skew_x = x;
163             g_skew_y = y;
164             force_redraw();
165         }
166     }
167 
168 
on_mouse_move(int x,int y,unsigned flags)169     virtual void on_mouse_move(int x, int y, unsigned flags)
170     {
171         on_mouse_button_down(x, y, flags);
172     }
173 
174 
175 };
176 
177 
178 
179 
180 
181 
agg_main(int argc,char * argv[])182 int agg_main(int argc, char* argv[])
183 {
184     the_application app(agg::pix_format_bgr24, flip_y);
185     app.caption("AGG Example. Lion with Alpha-Masking");
186 
187     if(app.init(512, 400, agg::window_resize))
188     {
189         return app.run();
190     }
191     return 1;
192 }
193 
194 
195 
196 
197 
198 
199