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

gamma_correction.cpp

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "agg_trans_affine.h"
00003 #include "agg_conv_stroke.h"
00004 #include "agg_rasterizer_scanline_aa.h"
00005 #include "agg_rendering_buffer.h"
00006 #include "agg_scanline_u.h"
00007 #include "agg_renderer_scanline.h"
00008 #include "agg_gamma_lut.h"
00009 #include "ctrl/agg_slider_ctrl.h"
00010 #include "platform/agg_platform_support.h"
00011 
00012 #define AGG_BGR24 
00013 //#define AGG_RGB24
00014 //#define AGG_BGRA32 
00015 //#define AGG_RGBA32 
00016 //#define AGG_ARGB32 
00017 //#define AGG_ABGR32
00018 //#define AGG_RGB565
00019 //#define AGG_RGB555
00020 #include "pixel_formats.h"
00021 
00022 enum flip_y_e { flip_y = true };
00023 
00024 class the_application : public agg::platform_support
00025 {
00026     agg::slider_ctrl<agg::rgba8> m_thickness;
00027     agg::slider_ctrl<agg::rgba8> m_gamma;
00028     agg::slider_ctrl<agg::rgba8> m_contrast;
00029     double m_rx;
00030     double m_ry;
00031 
00032 public:
00033     the_application(agg::pix_format_e format, bool flip_y) :
00034         agg::platform_support(format, flip_y),
00035         m_thickness(5, 5,    400-5, 11,    !flip_y),
00036         m_gamma    (5, 5+15, 400-5, 11+15, !flip_y),
00037         m_contrast (5, 5+30, 400-5, 11+30, !flip_y)
00038     {
00039         add_ctrl(m_thickness);
00040         add_ctrl(m_gamma);
00041         add_ctrl(m_contrast);
00042 
00043         m_thickness.label("Thickness=%3.2f");
00044         m_gamma.label("Gamma=%3.2f");
00045         m_contrast.label("Contrast");
00046 
00047         m_thickness.range(0.0, 3.0);
00048         m_gamma.range(0.5, 3.0);
00049         m_contrast.range(0.0, 1.0);
00050 
00051         m_thickness.value(1.0);
00052         m_gamma.value(1.0);
00053         m_contrast.value(1.0);
00054     }
00055 
00056     virtual void on_init()
00057     {
00058         m_rx = width() / 3.0;
00059         m_ry = height() / 3.0;
00060     }
00061 
00062     virtual void on_draw()
00063     {
00064         typedef agg::gamma_lut<agg::int8u, agg::int8u, 8, 8> gamma_type;
00065         typedef pixfmt_gamma<gamma_type> pixfmt_type;
00066         typedef agg::renderer_base<pixfmt_type> ren_base;
00067 
00068         double g = m_gamma.value();
00069         gamma_type gamma(g);
00070         pixfmt_type pixf(rbuf_window(), gamma);
00071         ren_base renb(pixf);
00072         renb.clear(agg::rgba(1, 1, 1));
00073 
00074 
00075         double dark = 1.0 - m_contrast.value();
00076         double light = m_contrast.value();
00077 
00078         renb.copy_bar(0,0,int(width())/2, int(height()),                agg::rgba(dark,dark,dark));
00079         renb.copy_bar(int(width())/2+1,0, int(width()), int(height()),  agg::rgba(light,light,light));
00080         renb.copy_bar(0,int(height())/2+1, int(width()), int(height()), agg::rgba(1.0,dark,dark));
00081 
00082         agg::rasterizer_scanline_aa<> ras;
00083         agg::scanline_u8 sl;
00084         agg::path_storage path;
00085 
00086 
00087         unsigned i;
00088         double x = (width() - 256.0) / 2.0;
00089         double y = 50.0;
00090         path.remove_all();
00091         agg::gamma_power gp(g);
00092         for(i = 0; i < 256; i++)
00093         {
00094             double v = double(i) / 255.0;
00095             double gval = gp(v);
00096             double dy = gval * 255.0;
00097             if(i == 0) path.move_to(x + i, y + dy);
00098             else       path.line_to(x + i, y + dy);
00099         }
00100         agg::conv_stroke<agg::path_storage> gpoly(path);
00101         gpoly.width(2.0);
00102         ras.reset();
00103         ras.add_path(gpoly);
00104         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(80,127,80));
00105 
00106         agg::ellipse ell(width() / 2, height() / 2, m_rx, m_ry, 150);
00107         agg::conv_stroke<agg::ellipse> poly(ell);
00108         poly.width(m_thickness.value());
00109         ras.reset();
00110         ras.add_path(poly);
00111         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(255,0,0));
00112 
00113         ell.init(width() / 2, height() / 2, m_rx-5.0, m_ry-5.0, 150);
00114         ras.reset();
00115         ras.add_path(poly);
00116         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0,255,0));
00117 
00118         ell.init(width() / 2, height() / 2, m_rx-10.0, m_ry-10.0, 150);
00119         ras.reset();
00120         ras.add_path(poly);
00121         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0,0,255));
00122 
00123         ell.init(width() / 2, height() / 2, m_rx-15.0, m_ry-15.0, 150);
00124         ras.reset();
00125         ras.add_path(poly);
00126         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0,0,0));
00127 
00128         ell.init(width() / 2, height() / 2, m_rx-20.0, m_ry-20.0, 150);
00129         ras.reset();
00130         ras.add_path(poly);
00131         agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(255,255,255));
00132 
00133         agg::render_ctrl(ras, sl, renb, m_thickness);
00134         agg::render_ctrl(ras, sl, renb, m_gamma);
00135         agg::render_ctrl(ras, sl, renb, m_contrast);
00136     }
00137 
00138 
00139     virtual void on_mouse_button_down(int x, int y, unsigned flags)
00140     {
00141         if(flags & agg::mouse_left)
00142         {
00143             m_rx = fabs(width()/2 - x);
00144             m_ry = fabs(height()/2 - y);
00145             force_redraw();
00146         }
00147     }
00148 
00149 
00150     virtual void on_mouse_move(int x, int y, unsigned flags)
00151     {
00152         on_mouse_button_down(x, y, flags);
00153     }
00154 
00155 };
00156 
00157 
00158 int agg_main(int argc, char* argv[])
00159 {
00160     the_application app(pix_format, flip_y);
00161     app.caption("AGG Example. Thin red ellipse");
00162 
00163     if(app.init(400, 320, 0))
00164     {
00165         return app.run();
00166     }
00167     return 1;
00168 }
00169 
00170 

© sourcejam.com 2005-2008