00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include "agg_ellipse.h"
00005 #include "agg_trans_affine.h"
00006 #include "agg_conv_transform.h"
00007
00008 #include "agg_rendering_buffer.h"
00009 #include "agg_pixfmt_rgb.h"
00010 #include "agg_span_allocator.h"
00011 #include "agg_span_image_filter_rgb.h"
00012 #include "agg_image_accessors.h"
00013 #include "agg_span_interpolator_linear.h"
00014 #include "agg_span_converter.h"
00015 #include "agg_scanline_u.h"
00016 #include "agg_renderer_scanline.h"
00017 #include "agg_rasterizer_scanline_aa.h"
00018
00019 #include "ctrl/agg_spline_ctrl.h"
00020 #include "platform/agg_platform_support.h"
00021
00022 enum flip_y_e { flip_y = true };
00023
00024
00025 namespace agg
00026 {
00027
00028
00029 class span_conv_brightness_alpha_rgb8
00030 {
00031 public:
00032 typedef rgba8 color_type;
00033 typedef int8u alpha_type;
00034
00035 enum array_size_e
00036 {
00037 array_size = 256 * 3
00038 };
00039
00040 span_conv_brightness_alpha_rgb8(const alpha_type* alpha_array) :
00041 m_alpha_array(alpha_array)
00042 {
00043 }
00044
00045 void prepare() {}
00046 void generate(color_type* span, int x, int y, unsigned len) const
00047 {
00048 do
00049 {
00050 span->a = m_alpha_array[span->r + span->g + span->b];
00051 ++span;
00052 }
00053 while(--len);
00054 }
00055
00056 private:
00057 const alpha_type* m_alpha_array;
00058 };
00059
00060 }
00061
00062
00063
00064
00065 class the_application : public agg::platform_support
00066 {
00067 agg::spline_ctrl<agg::rgba8> m_alpha;
00068 double m_x[50];
00069 double m_y[50];
00070 double m_rx[50];
00071 double m_ry[50];
00072 agg::rgba8 m_colors[50];
00073
00074 public:
00075 the_application(agg::pix_format_e format, bool flip_y) :
00076 agg::platform_support(format, flip_y),
00077 m_alpha(2, 2, 200, 30, 6, !flip_y)
00078 {
00079 m_alpha.value(0, 1.0);
00080 m_alpha.value(1, 1.0);
00081 m_alpha.value(2, 1.0);
00082 m_alpha.value(3, 0.5);
00083 m_alpha.value(4, 0.5);
00084 m_alpha.value(5, 1.0);
00085 m_alpha.update_spline();
00086 add_ctrl(m_alpha);
00087 }
00088
00089 virtual ~the_application()
00090 {
00091 }
00092
00093
00094 virtual void on_init()
00095 {
00096 unsigned i;
00097 for(i = 0; i < 50; i++)
00098 {
00099 m_x[i] = rand() % int(width());
00100 m_y[i] = rand() % int(height());
00101 m_rx[i] = rand() % 60 + 10;
00102 m_ry[i] = rand() % 60 + 10;
00103 m_colors[i] = agg::rgba8(rand() & 0xFF,
00104 rand() & 0xFF,
00105 rand() & 0xFF,
00106 rand() & 0xFF);
00107 }
00108 }
00109
00110
00111
00112 virtual void on_draw()
00113 {
00114 typedef agg::pixfmt_bgr24 pixfmt;
00115 typedef agg::renderer_base<pixfmt> renderer_base;
00116
00117 pixfmt pixf(rbuf_window());
00118 renderer_base rb(pixf);
00119
00120 rb.clear(agg::rgba(1.0, 1.0, 1.0));
00121
00122 agg::trans_affine src_mtx;
00123 src_mtx *= agg::trans_affine_translation(-initial_width()/2, -initial_height()/2);
00124 src_mtx *= agg::trans_affine_rotation(10.0 * agg::pi / 180.0);
00125 src_mtx *= agg::trans_affine_translation(initial_width()/2, initial_height()/2);
00126 src_mtx *= trans_affine_resizing();
00127
00128 agg::trans_affine img_mtx = src_mtx;
00129 img_mtx.invert();
00130
00131 typedef agg::span_allocator<agg::rgba8> span_alloc;
00132
00133 unsigned i;
00134
00135 unsigned char brightness_alpha_array[agg::span_conv_brightness_alpha_rgb8::array_size];
00136 for(i = 0; i < agg::span_conv_brightness_alpha_rgb8::array_size; i++)
00137 {
00138 brightness_alpha_array[i] =
00139 agg::int8u(m_alpha.value(double(i) /
00140 double(agg::span_conv_brightness_alpha_rgb8::array_size)) * 255.0);
00141 }
00142 agg::span_conv_brightness_alpha_rgb8 color_alpha(brightness_alpha_array);
00143
00144
00145
00146 typedef agg::image_accessor_clip<pixfmt> img_source_type;
00147 typedef agg::span_interpolator_linear<> interpolator_type;
00148 typedef agg::span_image_filter_rgb_bilinear<img_source_type,
00149 interpolator_type> span_gen;
00150 typedef agg::span_converter<span_gen,
00151 agg::span_conv_brightness_alpha_rgb8> span_conv;
00152
00153
00154 span_alloc sa;
00155 interpolator_type interpolator(img_mtx);
00156 pixfmt img_pixf(rbuf_img(0));
00157 img_source_type img_src(img_pixf, agg::rgba(0,0,0,0));
00158 span_gen sg(img_src, interpolator);
00159 span_conv sc(sg, color_alpha);
00160 agg::ellipse ell;
00161 agg::rasterizer_scanline_aa<> ras;
00162 agg::scanline_u8 sl;
00163
00164 for(i = 0; i < 50; i++)
00165 {
00166 ell.init(m_x[i], m_y[i], m_rx[i], m_ry[i], 50);
00167 ras.add_path(ell);
00168 agg::render_scanlines_aa_solid(ras, sl, rb, m_colors[i]);
00169 }
00170
00171
00172 ell.init(initial_width() / 2.0,
00173 initial_height() / 2.0,
00174 initial_width() / 1.9,
00175 initial_height() / 1.9, 200);
00176
00177
00178 agg::conv_transform<agg::ellipse> tr(ell, src_mtx);
00179
00180
00181 ras.add_path(tr);
00182 agg::render_scanlines_aa(ras, sl, rb, sa, sc);
00183
00184 agg::render_ctrl(ras, sl, rb, m_alpha);
00185 }
00186
00187 virtual void on_key(int x, int y, unsigned key, unsigned flags)
00188 {
00189 if(key == ' ')
00190 {
00191 FILE* fd = fopen(full_file_name("alpha"), "w");
00192
00193 int i;
00194 for(i = 0; i < agg::span_conv_brightness_alpha_rgb8::array_size; i++)
00195 {
00196 int alpha =
00197 agg::int8u(m_alpha.value(double(i) /
00198 double(agg::span_conv_brightness_alpha_rgb8::array_size)) * 255.0);
00199 if(i % 32 == 0) fprintf(fd, "\n");
00200 fprintf(fd, "%3d, ", alpha);
00201 }
00202
00203 fclose(fd);
00204 }
00205 }
00206
00207
00208
00209
00210 };
00211
00212
00213
00214
00215
00216 int agg_main(int argc, char* argv[])
00217 {
00218 the_application app(agg::pix_format_bgr24, flip_y);
00219 app.caption("Image Affine Transformations with Alpha-function");
00220
00221 const char* img_name = "spheres";
00222 if(argc >= 2) img_name = argv[1];
00223 if(!app.load_img(0, img_name))
00224 {
00225 char buf[256];
00226 if(strcmp(img_name, "spheres") == 0)
00227 {
00228 sprintf(buf, "File not found: %s%s. Download http://www.antigrain.com/%s%s\n"
00229 "or copy it from another directory if available.",
00230 img_name, app.img_ext(), img_name, app.img_ext());
00231 }
00232 else
00233 {
00234 sprintf(buf, "File not found: %s%s", img_name, app.img_ext());
00235 }
00236 app.message(buf);
00237 return 1;
00238 }
00239
00240 if(app.init(app.rbuf_img(0).width(), app.rbuf_img(0).height(), agg::window_resize))
00241 {
00242 return app.run();
00243 }
00244 return 0;
00245 }
00246
00247