virtual void on_init() { g_x1 = 0.0; g_y1 = 0.0; g_x2 = rbuf_img(0).width(); g_y2 = rbuf_img(0).height(); double x1 = g_x1;// * 100.0; double y1 = g_y1;// * 100.0; double x2 = g_x2;// * 100.0; double y2 = g_y2;// * 100.0; double dx = width() / 2.0 - (x2 - x1) / 2.0; double dy = height() / 2.0 - (y2 - y1) / 2.0; m_quad.xn(0) = floor(x1 + dx); m_quad.yn(0) = floor(y1 + dy);// - 150; m_quad.xn(1) = floor(x2 + dx); m_quad.yn(1) = floor(y1 + dy);// - 110; m_quad.xn(2) = floor(x2 + dx); m_quad.yn(2) = floor(y2 + dy);// - 300; m_quad.xn(3) = floor(x1 + dx); m_quad.yn(3) = floor(y2 + dy);// - 200; pixfmt pixf(rbuf_img(0)); pixf.apply_gamma_dir(m_gamma_lut); }
virtual void on_idle() { int i; for(i = 0; i < 6; i++) { move_point(m_poly1.xn(i), m_poly1.yn(i), m_dx1[i], m_dy1[i]); move_point(m_poly2.xn(i), m_poly2.yn(i), m_dx2[i], m_dy2[i]); normalize_point(i); } force_redraw(); }
virtual void on_idle() { int i; for(i = 0; i < 6; i++) { move_point(m_poly.xn(i), m_poly.yn(i), m_dx[i], m_dy[i]); } force_redraw(); }
the_application(agg::pix_format_e format, bool flip_y) : agg::platform_support(format, flip_y), m_quad(4, 5.0), m_trans_type(420, 5.0, 420+130.0, 55.0, !flip_y) { parse_lion(); m_quad.xn(0) = g_x1; m_quad.yn(0) = g_y1; m_quad.xn(1) = g_x2; m_quad.yn(1) = g_y1; m_quad.xn(2) = g_x2; m_quad.yn(2) = g_y2; m_quad.xn(3) = g_x1; m_quad.yn(3) = g_y2; m_trans_type.add_item("Bilinear"); m_trans_type.add_item("Perspective"); m_trans_type.cur_item(0); add_ctrl(m_trans_type); }
virtual void on_init() { g_x1 = -150; g_y1 = -150; g_x2 = 150; g_y2 = 150; double trans_x1 = -200; double trans_y1 = -200; double trans_x2 = 200; double trans_y2 = 200; double dx = width() / 2.0 - (trans_x2 + trans_x1) / 2.0; double dy = height() / 2.0 - (trans_y2 + trans_y1) / 2.0; m_quad.xn(0) = floor(trans_x1 + dx); m_quad.yn(0) = floor(trans_y1 + dy); m_quad.xn(1) = floor(trans_x2 + dx); m_quad.yn(1) = floor(trans_y1 + dy); m_quad.xn(2) = floor(trans_x2 + dx); m_quad.yn(2) = floor(trans_y2 + dy); m_quad.xn(3) = floor(trans_x1 + dx); m_quad.yn(3) = floor(trans_y2 + dy); }
void normalize_point(unsigned i) { double d = agg::calc_distance(m_poly1.xn(i), m_poly1.yn(i), m_poly2.xn(i), m_poly2.yn(i)); // 28.8 is 20 * sqrt(2) if(d > 28.28) { m_poly2.xn(i) = m_poly1.xn(i) + (m_poly2.xn(i) - m_poly1.xn(i)) * 28.28 / d; m_poly2.yn(i) = m_poly1.yn(i) + (m_poly2.yn(i) - m_poly1.yn(i)) * 28.28 / d; } }
virtual void on_init() { double dx = width() / 2.0 - (m_quad.xn(1) - m_quad.xn(0)) / 2.0; double dy = height() / 2.0 - (m_quad.yn(2) - m_quad.yn(0)) / 2.0; m_quad.xn(0) += dx; m_quad.yn(0) += dy; m_quad.xn(1) += dx; m_quad.yn(1) += dy; m_quad.xn(2) += dx; m_quad.yn(2) += dy; m_quad.xn(3) += dx; m_quad.yn(3) += dy; }
virtual void on_init() { m_quad1.xn(0) = 50; m_quad1.yn(0) = 200 - 20; m_quad1.xn(1) = width() / 2 - 25; m_quad1.yn(1) = 200; m_quad1.xn(2) = width() / 2 - 25; m_quad1.yn(2) = height() - 50 - 20; m_quad1.xn(3) = 50; m_quad1.yn(3) = height() - 50; m_quad2.xn(0) = width() / 2 + 25; m_quad2.yn(0) = 200 - 20; m_quad2.xn(1) = width() - 50; m_quad2.yn(1) = 200; m_quad2.xn(2) = width() - 50; m_quad2.yn(2) = height() - 50 - 20; m_quad2.xn(3) = width() / 2 + 25; m_quad2.yn(3) = height() - 50; }
virtual void on_init() { m_poly.xn(0) = 50; m_poly.yn(0) = 50; m_poly.xn(1) = 150 + 20; m_poly.yn(1) = 150 - 20; m_poly.xn(2) = 250 - 20; m_poly.yn(2) = 250 + 20; m_poly.xn(3) = 350 + 20; m_poly.yn(3) = 350 - 20; m_poly.xn(4) = 450 - 20; m_poly.yn(4) = 450 + 20; m_poly.xn(5) = 550; m_poly.yn(5) = 550; }
virtual void on_key(int x, int y, unsigned key, unsigned flags) { if(key == ' ') { double cx = (m_quad.xn(0) + m_quad.xn(1) + m_quad.xn(2) + m_quad.xn(3)) / 4; double cy = (m_quad.yn(0) + m_quad.yn(1) + m_quad.yn(2) + m_quad.yn(3)) / 4; agg::trans_affine tr = agg::trans_affine_translation(-cx, -cy); tr *= agg::trans_affine_rotation(agg::pi / 2.0); tr *= agg::trans_affine_translation(cx, cy); tr.transform(&m_quad.xn(0), &m_quad.yn(0)); tr.transform(&m_quad.xn(1), &m_quad.yn(1)); tr.transform(&m_quad.xn(2), &m_quad.yn(2)); tr.transform(&m_quad.xn(3), &m_quad.yn(3)); force_redraw(); } }
virtual void on_init() { m_poly1.xn(0) = 10 + 50; m_poly1.yn(0) = -10 + 50; m_poly1.xn(1) = 10 + 150 + 20; m_poly1.yn(1) = -10 + 150 - 20; m_poly1.xn(2) = 10 + 250 - 20; m_poly1.yn(2) = -10 + 250 + 20; m_poly1.xn(3) = 10 + 350 + 20; m_poly1.yn(3) = -10 + 350 - 20; m_poly1.xn(4) = 10 + 450 - 20; m_poly1.yn(4) = -10 + 450 + 20; m_poly1.xn(5) = 10 + 550; m_poly1.yn(5) = -10 + 550; m_poly2.xn(0) = -10 + 50; m_poly2.yn(0) = 10 + 50; m_poly2.xn(1) = -10 + 150 + 20; m_poly2.yn(1) = 10 + 150 - 20; m_poly2.xn(2) = -10 + 250 - 20; m_poly2.yn(2) = 10 + 250 + 20; m_poly2.xn(3) = -10 + 350 + 20; m_poly2.yn(3) = 10 + 350 - 20; m_poly2.xn(4) = -10 + 450 - 20; m_poly2.yn(4) = 10 + 450 + 20; m_poly2.xn(5) = -10 + 550; m_poly2.yn(5) = 10 + 550; }
virtual void on_draw() { pixfmt pixf(rbuf_window()); pixfmt_pre pixf_pre(rbuf_window()); renderer_base rb(pixf); renderer_base_pre rb_pre(pixf_pre); if(!m_test_flag) { rb.clear(agg::rgba(1, 1, 1)); } if(m_trans_type.cur_item() == 0) { // For the affine parallelogram transformations we // calculate the 4-th (implicit) point of the parallelogram m_quad.xn(3) = m_quad.xn(0) + (m_quad.xn(2) - m_quad.xn(1)); m_quad.yn(3) = m_quad.yn(0) + (m_quad.yn(2) - m_quad.yn(1)); } if(!m_test_flag) { //-------------------------- // Render the "quad" tool and controls g_rasterizer.add_path(m_quad); agg::render_scanlines_aa_solid(g_rasterizer, g_scanline, rb, agg::rgba(0, 0.3, 0.5, 0.6)); //-------------------------- agg::render_ctrl(g_rasterizer, g_scanline, rb, m_trans_type); } // Prepare the polygon to rasterize. Here we need to fill // the destination (transformed) polygon. g_rasterizer.clip_box(0, 0, width(), height()); g_rasterizer.reset(); g_rasterizer.move_to_d(m_quad.xn(0), m_quad.yn(0)); g_rasterizer.line_to_d(m_quad.xn(1), m_quad.yn(1)); g_rasterizer.line_to_d(m_quad.xn(2), m_quad.yn(2)); g_rasterizer.line_to_d(m_quad.xn(3), m_quad.yn(3)); typedef agg::span_allocator<color_type> span_alloc_type; span_alloc_type sa; agg::image_filter<agg::image_filter_hanning> filter; typedef agg::wrap_mode_reflect_auto_pow2 remainder_type; typedef agg::image_accessor_wrap<pixfmt, remainder_type, remainder_type> img_source_type; pixfmt img_pixf(rbuf_img(0)); img_source_type img_src(img_pixf); enum subdiv_shift_e { subdiv_shift = 2 }; switch(m_trans_type.cur_item()) { case 0: { // Note that we consruct an affine matrix that transforms // a parallelogram to a rectangle, i.e., it's inverted. // It's actually the same as: // tr(g_x1, g_y1, g_x2, g_y2, m_triangle.polygon()); // tr.invert(); agg::trans_affine tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); // Also note that we can use the linear interpolator instead of // arbitrary span_interpolator_trans. It works much faster, // but the transformations must be linear and parellel. typedef agg::span_interpolator_linear<agg::trans_affine> interpolator_type; interpolator_type interpolator(tr); typedef span_image_filter_2x2<img_source_type, interpolator_type> span_gen_type; span_gen_type sg(img_src, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); break; } case 1: { agg::trans_bilinear tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); if(tr.is_valid()) { typedef agg::span_interpolator_linear<agg::trans_bilinear> interpolator_type; interpolator_type interpolator(tr); typedef span_image_filter_2x2<img_source_type, interpolator_type> span_gen_type; span_gen_type sg(img_src, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } case 2: { agg::trans_perspective tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); if(tr.is_valid()) { typedef agg::span_interpolator_linear_subdiv<agg::trans_perspective, 8> interpolator_type; interpolator_type interpolator(tr); typedef span_image_filter_2x2<img_source_type, interpolator_type> span_gen_type; span_gen_type sg(img_src, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } } }
virtual void on_draw() { if(m_gamma.value() != m_old_gamma) { m_gamma_lut.gamma(m_gamma.value()); load_img(0, "spheres"); pixfmt pixf(rbuf_img(0)); pixf.apply_gamma_dir(m_gamma_lut); m_old_gamma = m_gamma.value(); } pixfmt pixf(rbuf_window()); pixfmt_pre pixf_pre(rbuf_window()); renderer_base rb(pixf); renderer_base_pre rb_pre(pixf_pre); renderer_solid r(rb); rb.clear(agg::rgba(1, 1, 1)); if(m_trans_type.cur_item() < 2) { // For the affine parallelogram transformations we // calculate the 4-th (implicit) point of the parallelogram m_quad.xn(3) = m_quad.xn(0) + (m_quad.xn(2) - m_quad.xn(1)); m_quad.yn(3) = m_quad.yn(0) + (m_quad.yn(2) - m_quad.yn(1)); } //-------------------------- // Render the "quad" tool and controls g_rasterizer.add_path(m_quad); r.color(agg::rgba(0, 0.3, 0.5, 0.1)); agg::render_scanlines(g_rasterizer, g_scanline, r); // Prepare the polygon to rasterize. Here we need to fill // the destination (transformed) polygon. g_rasterizer.clip_box(0, 0, width(), height()); g_rasterizer.reset(); int b = 0; g_rasterizer.move_to_d(m_quad.xn(0)-b, m_quad.yn(0)-b); g_rasterizer.line_to_d(m_quad.xn(1)+b, m_quad.yn(1)-b); g_rasterizer.line_to_d(m_quad.xn(2)+b, m_quad.yn(2)+b); g_rasterizer.line_to_d(m_quad.xn(3)-b, m_quad.yn(3)+b); typedef agg::span_allocator<color_type> span_alloc_type; span_alloc_type sa; agg::image_filter_bilinear filter_kernel; agg::image_filter_lut filter(filter_kernel, true); pixfmt pixf_img(rbuf_img(0)); typedef agg::image_accessor_clone<pixfmt> source_type; source_type source(pixf_img); start_timer(); switch(m_trans_type.cur_item()) { case 0: { agg::trans_affine tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); typedef agg::span_interpolator_linear<agg::trans_affine> interpolator_type; interpolator_type interpolator(tr); typedef image_filter_2x2_type<source_type, interpolator_type> span_gen_type; span_gen_type sg(source, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); break; } case 1: { agg::trans_affine tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); typedef agg::span_interpolator_linear<agg::trans_affine> interpolator_type; typedef image_resample_affine_type<source_type> span_gen_type; interpolator_type interpolator(tr); span_gen_type sg(source, interpolator, filter); sg.blur(m_blur.value()); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); break; } case 2: { agg::trans_perspective tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); if(tr.is_valid()) { typedef agg::span_interpolator_linear_subdiv<agg::trans_perspective> interpolator_type; interpolator_type interpolator(tr); typedef image_filter_2x2_type<source_type, interpolator_type> span_gen_type; span_gen_type sg(source, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } case 3: { agg::trans_perspective tr(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); if(tr.is_valid()) { typedef agg::span_interpolator_trans<agg::trans_perspective> interpolator_type; interpolator_type interpolator(tr); typedef image_filter_2x2_type<source_type, interpolator_type> span_gen_type; span_gen_type sg(source, interpolator, filter); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } case 4: { typedef agg::span_interpolator_persp_lerp<> interpolator_type; typedef agg::span_subdiv_adaptor<interpolator_type> subdiv_adaptor_type; interpolator_type interpolator(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); subdiv_adaptor_type subdiv_adaptor(interpolator); if(interpolator.is_valid()) { typedef image_resample_type<source_type, subdiv_adaptor_type> span_gen_type; span_gen_type sg(source, subdiv_adaptor, filter); sg.blur(m_blur.value()); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } case 5: { typedef agg::span_interpolator_persp_exact<> interpolator_type; typedef agg::span_subdiv_adaptor<interpolator_type> subdiv_adaptor_type; interpolator_type interpolator(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); subdiv_adaptor_type subdiv_adaptor(interpolator); if(interpolator.is_valid()) { typedef image_resample_type<source_type, subdiv_adaptor_type> span_gen_type; span_gen_type sg(source, subdiv_adaptor, filter); sg.blur(m_blur.value()); agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg); } break; } } double tm = elapsed_time(); pixf.apply_gamma_inv(m_gamma_lut); char buf[64]; agg::gsv_text t; t.size(10.0); agg::conv_stroke<agg::gsv_text> pt(t); pt.width(1.5); sprintf(buf, "%3.2f ms", tm); t.start_point(10.0, 70.0); t.text(buf); g_rasterizer.add_path(pt); r.color(agg::rgba(0,0,0)); agg::render_scanlines(g_rasterizer, g_scanline, r); //-------------------------- agg::render_ctrl(g_rasterizer, g_scanline, rb, m_trans_type); agg::render_ctrl(g_rasterizer, g_scanline, rb, m_gamma); agg::render_ctrl(g_rasterizer, g_scanline, rb, m_blur); }