Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

compositing2.cpp

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "agg_rendering_buffer.h"
00003 #include "agg_rasterizer_scanline_aa.h"
00004 #include "agg_scanline_u.h"
00005 #include "agg_renderer_scanline.h"
00006 #include "agg_rounded_rect.h"
00007 #include "agg_pixfmt_rgba.h"
00008 #include "agg_span_allocator.h"
00009 #include "agg_span_gradient.h"
00010 #include "agg_gsv_text.h"
00011 #include "agg_span_interpolator_linear.h"
00012 #include "platform/agg_platform_support.h"
00013 #include "ctrl/agg_slider_ctrl.h"
00014 #include "ctrl/agg_rbox_ctrl.h"
00015 
00016 
00017 enum flip_y_e { flip_y = true };
00018 
00019 typedef agg::rgba8 color;
00020 typedef agg::order_bgra order;
00021 typedef agg::pixel32_type pixel_type;
00022 #define pix_format agg::pix_format_bgra32
00023 
00024 
00025 typedef agg::blender_rgba<color, order> prim_blender_type; 
00026 typedef agg::pixfmt_alpha_blend_rgba<prim_blender_type, agg::rendering_buffer, pixel_type> prim_pixfmt_type;
00027 typedef agg::renderer_base<prim_pixfmt_type> prim_ren_base_type;
00028 
00029 
00030 void force_comp_op_link()
00031 {
00032     // For unknown reason Digital Mars C++ doesn't want to link these 
00033     // functions if they are not specified explicitly. 
00034     agg::int8u p[4] = {0};
00035     agg::comp_op_rgba_contrast   <color, order>::blend_pix(p,0,0,0,0,0);
00036     agg::comp_op_rgba_darken     <color, order>::blend_pix(p,0,0,0,0,0);
00037     agg::comp_op_rgba_lighten    <color, order>::blend_pix(p,0,0,0,0,0);
00038     agg::comp_op_rgba_color_dodge<color, order>::blend_pix(p,0,0,0,0,0);
00039     agg::comp_op_rgba_color_burn <color, order>::blend_pix(p,0,0,0,0,0);
00040     agg::comp_op_rgba_hard_light <color, order>::blend_pix(p,0,0,0,0,0);
00041     agg::comp_op_rgba_soft_light <color, order>::blend_pix(p,0,0,0,0,0);
00042     agg::comp_op_rgba_difference <color, order>::blend_pix(p,0,0,0,0,0);
00043     agg::comp_op_rgba_exclusion  <color, order>::blend_pix(p,0,0,0,0,0);
00044     agg::comp_op_rgba_src_atop   <color, order>::blend_pix(p,0,0,0,0,0);
00045     agg::comp_op_rgba_dst_atop   <color, order>::blend_pix(p,0,0,0,0,0);
00046     agg::comp_op_rgba_xor        <color, order>::blend_pix(p,0,0,0,0,0);
00047     agg::comp_op_rgba_plus       <color, order>::blend_pix(p,0,0,0,0,0);
00048     agg::comp_op_rgba_minus      <color, order>::blend_pix(p,0,0,0,0,0);
00049     agg::comp_op_rgba_multiply   <color, order>::blend_pix(p,0,0,0,0,0);
00050     agg::comp_op_rgba_screen     <color, order>::blend_pix(p,0,0,0,0,0);
00051     agg::comp_op_rgba_overlay    <color, order>::blend_pix(p,0,0,0,0,0);
00052     agg::comp_op_rgba_src        <color, order>::blend_pix(p,0,0,0,0,0);
00053     agg::comp_op_rgba_dst        <color, order>::blend_pix(p,0,0,0,0,0);
00054     agg::comp_op_rgba_src_over   <color, order>::blend_pix(p,0,0,0,0,0);
00055     agg::comp_op_rgba_dst_over   <color, order>::blend_pix(p,0,0,0,0,0);
00056     agg::comp_op_rgba_src_in     <color, order>::blend_pix(p,0,0,0,0,0);
00057     agg::comp_op_rgba_dst_in     <color, order>::blend_pix(p,0,0,0,0,0);
00058     agg::comp_op_rgba_src_out    <color, order>::blend_pix(p,0,0,0,0,0);
00059     agg::comp_op_rgba_dst_out    <color, order>::blend_pix(p,0,0,0,0,0);
00060     agg::comp_op_rgba_clear      <color, order>::blend_pix(p,0,0,0,0,0);
00061 }
00062 
00063 
00064 template<class Container, class ColorT> 
00065 void generate_color_ramp(Container& c, 
00066                          ColorT c1, ColorT c2, ColorT c3, ColorT c4)
00067 {
00068     unsigned i;
00069     for(i = 0; i < 85; i++)
00070     {
00071         c[i] = c1.gradient(c2, i/85.0);
00072     }
00073     for(; i < 170; i++)
00074     {
00075         c[i] = c2.gradient(c3, (i - 85)/85.0);
00076     }
00077     for(; i < 256; i++)
00078     {
00079         c[i] = c3.gradient(c4, (i - 170)/85.0);
00080     }
00081 }
00082 
00083 
00084 class the_application : public agg::platform_support
00085 {
00086     agg::slider_ctrl<color>    m_alpha_dst;
00087     agg::slider_ctrl<color>    m_alpha_src;
00088     agg::rbox_ctrl<agg::rgba8> m_comp_op;
00089 
00090     agg::pod_auto_array<color, 256> m_ramp1;
00091     agg::pod_auto_array<color, 256> m_ramp2;
00092 
00093     agg::rasterizer_scanline_aa<> m_ras;
00094     agg::scanline_u8 m_sl;
00095 
00096 public:
00097     the_application(agg::pix_format_e format, bool flip_y) :
00098         agg::platform_support(format, flip_y),
00099         m_alpha_dst(5, 5,    400, 11,    !flip_y),
00100         m_alpha_src(5, 5+15, 400, 11+15, !flip_y),
00101         m_comp_op(420, 5.0, 420+170.0, 395.0, !flip_y)
00102     {
00103         m_alpha_dst.label("Dst Alpha=%.2f");
00104         m_alpha_dst.value(1.0);
00105         add_ctrl(m_alpha_dst);
00106 
00107         m_alpha_src.label("Src Alpha=%.2f");
00108         m_alpha_src.value(1.0);
00109         add_ctrl(m_alpha_src);
00110 
00111         m_comp_op.text_size(7);
00112         m_comp_op.add_item("clear");
00113         m_comp_op.add_item("src");
00114         m_comp_op.add_item("dst");
00115         m_comp_op.add_item("src-over");
00116         m_comp_op.add_item("dst-over");
00117         m_comp_op.add_item("src-in");
00118         m_comp_op.add_item("dst-in");
00119         m_comp_op.add_item("src-out");
00120         m_comp_op.add_item("dst-out");
00121         m_comp_op.add_item("src-atop");
00122         m_comp_op.add_item("dst-atop");
00123         m_comp_op.add_item("xor");
00124         m_comp_op.add_item("plus");
00125         m_comp_op.add_item("minus");
00126         m_comp_op.add_item("multiply");
00127         m_comp_op.add_item("screen");
00128         m_comp_op.add_item("overlay");
00129         m_comp_op.add_item("darken");
00130         m_comp_op.add_item("lighten");
00131         m_comp_op.add_item("color-dodge");
00132         m_comp_op.add_item("color-burn");
00133         m_comp_op.add_item("hard-light");
00134         m_comp_op.add_item("soft-light");
00135         m_comp_op.add_item("difference");
00136         m_comp_op.add_item("exclusion");
00137         m_comp_op.add_item("contrast");
00138         m_comp_op.cur_item(3);
00139         add_ctrl(m_comp_op);
00140     }
00141 
00142 
00143     template<class RenBase, class ColorRamp> 
00144     void radial_shape(RenBase& rbase, const ColorRamp& colors,
00145                       double x1, double y1, double x2, double y2)
00146     {
00147         typedef RenBase renderer_base_type;
00148         typedef agg::gradient_radial gradient_func_type;
00149         typedef ColorRamp color_func_type;
00150         typedef agg::span_interpolator_linear<> interpolator_type;
00151         typedef agg::span_allocator<color> span_allocator_type;
00152         typedef agg::span_gradient<color, 
00153                                    interpolator_type, 
00154                                    gradient_func_type, 
00155                                    color_func_type> span_gradient_type;
00156 
00157         gradient_func_type  gradient_func;                   // The gradient function
00158         agg::trans_affine   gradient_mtx;
00159         interpolator_type   span_interpolator(gradient_mtx); // Span interpolator
00160         span_allocator_type span_allocator;                  // Span Allocator
00161         span_gradient_type  span_gradient(span_interpolator, 
00162                                           gradient_func, 
00163                                           colors, 
00164                                           0, 100);
00165 
00166         double cx = (x1 + x2) / 2.0;
00167         double cy = (y1 + y2) / 2.0;
00168         double r  = 0.5 * (((x2 - x1) < (y2 - y1)) ? (x2 - x1) : (y2 - y1));
00169 
00170         gradient_mtx *= agg::trans_affine_scaling(r / 100.0);
00171         gradient_mtx *= agg::trans_affine_translation(cx, cy);
00172         gradient_mtx *= trans_affine_resizing();
00173         gradient_mtx.invert();
00174 
00175         agg::ellipse ell(cx, cy, r, r, 100);
00176         agg::conv_transform<agg::ellipse> trans(ell, trans_affine_resizing());
00177         m_ras.add_path(trans);
00178 
00179         agg::render_scanlines_aa(m_ras, m_sl, rbase, span_allocator, span_gradient);
00180     }
00181 
00182 
00183     template<class RenBase> void render_scene(RenBase& rb)
00184     {
00185         typedef agg::comp_op_adaptor_rgba<color, order> blender_type;
00186         typedef agg::pixfmt_custom_blend_rgba<blender_type, agg::rendering_buffer> pixfmt_type;
00187         typedef agg::renderer_base<pixfmt_type> renderer_type;
00188 
00189         pixfmt_type pixf(rbuf_window());
00190         renderer_type ren(pixf);
00191 
00192 
00193         pixf.comp_op(agg::comp_op_difference);
00194         radial_shape(ren, m_ramp1, 50, 50, 50+320, 50+320);
00195 
00196         pixf.comp_op(m_comp_op.cur_item());
00197         double cx = 50;
00198         double cy = 50;
00199         radial_shape(ren, m_ramp2, cx+120-70, cy+120-70, cx+120+70, cy+120+70);
00200         radial_shape(ren, m_ramp2, cx+200-70, cy+120-70, cx+200+70, cy+120+70);
00201         radial_shape(ren, m_ramp2, cx+120-70, cy+200-70, cx+120+70, cy+200+70); 
00202         radial_shape(ren, m_ramp2, cx+200-70, cy+200-70, cx+200+70, cy+200+70);
00203     }
00204 
00205 
00206     virtual void on_draw()
00207     {
00208         prim_pixfmt_type pixf(rbuf_window());
00209         prim_ren_base_type rb(pixf);
00210         rb.clear(agg::rgba8(255, 255, 255));
00211 
00212         generate_color_ramp(m_ramp1, 
00213                             agg::rgba(0, 0, 0, m_alpha_dst.value()),
00214                             agg::rgba(0, 0, 1, m_alpha_dst.value()),
00215                             agg::rgba(0, 1, 0, m_alpha_dst.value()),
00216                             agg::rgba(1, 0, 0, 0));
00217 
00218         generate_color_ramp(m_ramp2, 
00219                             agg::rgba(0, 0, 0, m_alpha_src.value()),
00220                             agg::rgba(0, 0, 1, m_alpha_src.value()),
00221                             agg::rgba(0, 1, 0, m_alpha_src.value()),
00222                             agg::rgba(1, 0, 0, 0));
00223 
00224         render_scene(rb);
00225         agg::renderer_scanline_aa_solid<prim_ren_base_type> ren(rb);
00226 
00227         agg::render_ctrl_rs(m_ras, m_sl, ren, m_alpha_dst);
00228         agg::render_ctrl_rs(m_ras, m_sl, ren, m_alpha_src);
00229         agg::render_ctrl_rs(m_ras, m_sl, ren, m_comp_op);
00230     }
00231 
00232 };
00233 
00234 
00235 int agg_main(int argc, char* argv[])
00236 {
00237     force_comp_op_link();
00238     the_application app(pix_format, flip_y);
00239     app.caption("AGG Example. Compositing Modes");
00240 
00241     if(app.init(600, 400, agg::window_resize|agg::window_keep_aspect_ratio))
00242     {
00243         return app.run();
00244     }
00245     return 1;
00246 }
00247 
00248 

© sourcejam.com 2005-2008