00001 #include <stdlib.h> 00002 #include <ctype.h> 00003 #include <stdio.h> 00004 #include "agg_basics.h" 00005 #include "agg_rendering_buffer.h" 00006 #include "agg_rasterizer_scanline_aa.h" 00007 #include "agg_path_storage.h" 00008 #include "agg_conv_transform.h" 00009 #include "agg_bounding_rect.h" 00010 #include "agg_renderer_scanline.h" 00011 #include "agg_pixfmt_rgb.h" 00012 #include "agg_pixfmt_gray.h" 00013 #include "agg_alpha_mask_u8.h" 00014 #include "agg_scanline_u.h" 00015 #include "agg_scanline_p.h" 00016 #include "agg_ellipse.h" 00017 #include "platform/agg_platform_support.h" 00018 00019 enum flip_y_e { flip_y = true }; 00020 00021 agg::path_storage g_path; 00022 agg::rgba8 g_colors[100]; 00023 unsigned g_path_idx[100]; 00024 unsigned g_npaths = 0; 00025 double g_x1 = 0; 00026 double g_y1 = 0; 00027 double g_x2 = 0; 00028 double g_y2 = 0; 00029 double g_base_dx = 0; 00030 double g_base_dy = 0; 00031 double g_angle = 0; 00032 double g_scale = 1.0; 00033 double g_skew_x = 0; 00034 double g_skew_y = 0; 00035 int g_nclick = 0; 00036 00037 00038 unsigned parse_lion(agg::path_storage& ps, agg::rgba8* colors, unsigned* path_idx); 00039 void parse_lion() 00040 { 00041 g_npaths = parse_lion(g_path, g_colors, g_path_idx); 00042 agg::pod_array_adaptor<unsigned> path_idx(g_path_idx, 100); 00043 agg::bounding_rect(g_path, path_idx, 0, g_npaths, &g_x1, &g_y1, &g_x2, &g_y2); 00044 g_base_dx = (g_x2 - g_x1) / 2.0; 00045 g_base_dy = (g_y2 - g_y1) / 2.0; 00046 } 00047 00048 00049 agg::rendering_buffer g_alpha_mask_rbuf; 00050 agg::alpha_mask_gray8 g_alpha_mask(g_alpha_mask_rbuf); 00051 agg::rasterizer_scanline_aa<> g_rasterizer; 00052 00053 00054 class the_application : public agg::platform_support 00055 { 00056 unsigned char* m_alpha_buf; 00057 agg::rendering_buffer m_alpha_rbuf; 00058 00059 public: 00060 virtual ~the_application() 00061 { 00062 delete [] m_alpha_buf; 00063 } 00064 00065 the_application(agg::pix_format_e format, bool flip_y) : 00066 agg::platform_support(format, flip_y), 00067 m_alpha_buf(0) 00068 { 00069 parse_lion(); 00070 } 00071 00072 00073 void generate_alpha_mask(int cx, int cy) 00074 { 00075 delete [] m_alpha_buf; 00076 m_alpha_buf = new unsigned char[cx * cy]; 00077 g_alpha_mask_rbuf.attach(m_alpha_buf, cx, cy, cx); 00078 00079 typedef agg::renderer_base<agg::pixfmt_gray8> ren_base; 00080 typedef agg::renderer_scanline_aa_solid<ren_base> renderer; 00081 00082 agg::pixfmt_gray8 pixf(g_alpha_mask_rbuf); 00083 ren_base rb(pixf); 00084 renderer r(rb); 00085 agg::scanline_p8 sl; 00086 00087 rb.clear(agg::gray8(0)); 00088 00089 agg::ellipse ell; 00090 00091 int i; 00092 for(i = 0; i < 10; i++) 00093 { 00094 ell.init(rand() % cx, 00095 rand() % cy, 00096 rand() % 100 + 20, 00097 rand() % 100 + 20, 00098 100); 00099 00100 g_rasterizer.add_path(ell); 00101 r.color(agg::gray8(rand() & 0xFF, rand() & 0xFF)); 00102 agg::render_scanlines(g_rasterizer, sl, r); 00103 } 00104 } 00105 00106 00107 virtual void on_resize(int cx, int cy) 00108 { 00109 generate_alpha_mask(cx, cy); 00110 } 00111 00112 virtual void on_draw() 00113 { 00114 int width = rbuf_window().width(); 00115 int height = rbuf_window().height(); 00116 00117 typedef agg::scanline_u8_am<agg::alpha_mask_gray8> scanline_type; 00118 typedef agg::renderer_base<agg::pixfmt_bgr24> ren_base; 00119 typedef agg::renderer_scanline_aa_solid<ren_base> renderer; 00120 00121 agg::pixfmt_bgr24 pixf(rbuf_window()); 00122 ren_base rb(pixf); 00123 renderer r(rb); 00124 00125 scanline_type sl(g_alpha_mask); 00126 rb.clear(agg::rgba8(255, 255, 255)); 00127 00128 agg::trans_affine mtx; 00129 mtx *= agg::trans_affine_translation(-g_base_dx, -g_base_dy); 00130 mtx *= agg::trans_affine_scaling(g_scale, g_scale); 00131 mtx *= agg::trans_affine_rotation(g_angle + agg::pi); 00132 mtx *= agg::trans_affine_skewing(g_skew_x/1000.0, g_skew_y/1000.0); 00133 mtx *= agg::trans_affine_translation(width/2, height/2); 00134 00135 agg::conv_transform<agg::path_storage, agg::trans_affine> trans(g_path, mtx); 00136 00137 agg::render_all_paths(g_rasterizer, sl, r, trans, g_colors, g_path_idx, g_npaths); 00138 } 00139 00140 00141 void transform(double width, double height, double x, double y) 00142 { 00143 x -= width / 2; 00144 y -= height / 2; 00145 g_angle = atan2(y, x); 00146 g_scale = sqrt(y * y + x * x) / 100.0; 00147 } 00148 00149 00150 virtual void on_mouse_button_down(int x, int y, unsigned flags) 00151 { 00152 if(flags & agg::mouse_left) 00153 { 00154 int width = rbuf_window().width(); 00155 int height = rbuf_window().height(); 00156 transform(width, height, x, y); 00157 force_redraw(); 00158 } 00159 00160 if(flags & agg::mouse_right) 00161 { 00162 g_skew_x = x; 00163 g_skew_y = y; 00164 force_redraw(); 00165 } 00166 } 00167 00168 00169 virtual void on_mouse_move(int x, int y, unsigned flags) 00170 { 00171 on_mouse_button_down(x, y, flags); 00172 } 00173 00174 00175 }; 00176 00177 00178 00179 00180 00181 00182 int agg_main(int argc, char* argv[]) 00183 { 00184 the_application app(agg::pix_format_bgr24, flip_y); 00185 app.caption("AGG Example. Lion with Alpha-Masking"); 00186 00187 if(app.init(512, 400, agg::window_resize)) 00188 { 00189 return app.run(); 00190 } 00191 return 1; 00192 } 00193 00194 00195 00196 00197 00198