public override void OnDraw(Graphics2D graphics2D) { ImageBuffer widgetsSubImage = ImageBuffer.NewSubImageReference(graphics2D.DestImage, graphics2D.GetClippingRect()); if (!didInit) { didInit = true; OnInitialize(); } if (m_gamma.Value != m_old_gamma) { m_gamma_lut.SetGamma(m_gamma.Value); ImageIO.LoadImageData("spheres.bmp", m_SourceImage); //m_SourceImage.apply_gamma_dir(m_gamma_lut); m_old_gamma = m_gamma.Value; } ImageBuffer pixf = new ImageBuffer(); switch (widgetsSubImage.BitDepth) { case 24: pixf.Attach(widgetsSubImage, new BlenderBGR()); break; case 32: pixf.Attach(widgetsSubImage, new BlenderBGRA()); break; default: throw new NotImplementedException(); } ImageClippingProxy clippingProxy = new ImageClippingProxy(pixf); clippingProxy.clear(new ColorF(1, 1, 1)); if (m_trans_type.SelectedIndex < 2) { // For the affine parallelogram transformations we // calculate the 4-th (implicit) point of the parallelogram m_quad.SetXN(3, m_quad.GetXN(0) + (m_quad.GetXN(2) - m_quad.GetXN(1))); m_quad.SetYN(3, m_quad.GetYN(0) + (m_quad.GetYN(2) - m_quad.GetYN(1))); } ScanlineRenderer scanlineRenderer = new ScanlineRenderer(); // draw a background to show how the alpha is working int RectWidth = 70; int xoffset = 50; int yoffset = 50; for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if ((i + j) % 2 != 0) { VertexSource.RoundedRect rect = new VertexSource.RoundedRect(i * RectWidth + xoffset, j * RectWidth + yoffset, (i + 1) * RectWidth + xoffset, (j + 1) * RectWidth + yoffset, 2); rect.normalize_radius(); g_rasterizer.add_path(rect); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(.2, .2, .2)); } } } //-------------------------- // Render the "quad" tool and controls g_rasterizer.add_path(m_quad); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0, 0.3, 0.5, 0.1)); // Prepare the polygon to rasterize. Here we need to fill // the destination (transformed) polygon. g_rasterizer.SetVectorClipBox(0, 0, Width, Height); g_rasterizer.reset(); int b = 0; g_rasterizer.move_to_d(m_quad.GetXN(0) - b, m_quad.GetYN(0) - b); g_rasterizer.line_to_d(m_quad.GetXN(1) + b, m_quad.GetYN(1) - b); g_rasterizer.line_to_d(m_quad.GetXN(2) + b, m_quad.GetYN(2) + b); g_rasterizer.line_to_d(m_quad.GetXN(3) - b, m_quad.GetYN(3) + b); //typedef agg::span_allocator<color_type> span_alloc_type; span_allocator sa = new span_allocator(); image_filter_bilinear filter_kernel = new image_filter_bilinear(); ImageFilterLookUpTable filter = new ImageFilterLookUpTable(filter_kernel, true); ImageBufferAccessorClamp source = new ImageBufferAccessorClamp(m_SourceImage); stopwatch.Restart(); switch (m_trans_type.SelectedIndex) { 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; span_interpolator_persp_lerp interpolator = new span_interpolator_persp_lerp(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2); span_subdiv_adaptor subdiv_adaptor = new span_subdiv_adaptor(interpolator); span_image_resample sg = null; if (interpolator.is_valid()) { switch (source.SourceImage.BitDepth) { case 24: sg = new span_image_resample_rgb(source, subdiv_adaptor, filter); break; case 32: sg = new span_image_resample_rgba(source, subdiv_adaptor, filter); break; } sg.blur(m_blur.Value); scanlineRenderer.GenerateAndRender(g_rasterizer, g_scanline, clippingProxy, 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 = stopwatch.ElapsedMilliseconds; //pixf.apply_gamma_inv(m_gamma_lut); gsv_text t = new gsv_text(); t.SetFontSize(10.0); Stroke pt = new Stroke(t); pt.Width = 1.5; string buf = string.Format("{0:F2} ms", tm); t.start_point(10.0, 70.0); t.text(buf); g_rasterizer.add_path(pt); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0, 0, 0)); //-------------------------- //m_trans_type.Render(g_rasterizer, g_scanline, clippingProxy); //m_gamma.Render(g_rasterizer, g_scanline, clippingProxy); //m_blur.Render(g_rasterizer, g_scanline, clippingProxy); base.OnDraw(graphics2D); }
public override void OnDraw(Graphics2D graphics2D) { ImageBuffer widgetsSubImage = ImageBuffer.NewSubImageReference(graphics2D.DestImage, graphics2D.GetClippingRect()); IImageByte backBuffer = widgetsSubImage; if (!didInit) { didInit = true; OnInitialize(); } ImageBuffer image; if (backBuffer.BitDepth == 32) { image = new ImageBuffer(); image.Attach(backBuffer, new BlenderBGRA()); } else { if (backBuffer.BitDepth != 24) { throw new System.NotSupportedException(); } image = new ImageBuffer(); image.Attach(backBuffer, new BlenderBGR()); } ImageClippingProxy clippingProxy = new ImageClippingProxy(image); clippingProxy.clear(new ColorF(1, 1, 1)); g_rasterizer.SetVectorClipBox(0, 0, Width, Height); ScanlineRenderer scanlineRenderer = new ScanlineRenderer(); if (transformationTypeRadioButton.SelectedIndex == 0) { Bilinear tr = new Bilinear(lionShape.Bounds.Left, lionShape.Bounds.Bottom, lionShape.Bounds.Right, lionShape.Bounds.Top, quadPolygonControl.polygon()); if (tr.is_valid()) { //-------------------------- // Render transformed lion // foreach (var shape in lionShape.Shapes) { g_rasterizer.add_path(new VertexSourceApplyTransform(shape.VertexStorage, tr)); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, shape.Color); } //-------------------------- //-------------------------- // Render transformed ellipse // VertexSource.Ellipse ell = new MatterHackers.Agg.VertexSource.Ellipse((lionShape.Bounds.Left + lionShape.Bounds.Right) * 0.5, (lionShape.Bounds.Bottom + lionShape.Bounds.Top) * 0.5, (lionShape.Bounds.Right - lionShape.Bounds.Left) * 0.5, (lionShape.Bounds.Top - lionShape.Bounds.Bottom) * 0.5, 200); Stroke ell_stroke = new Stroke(ell); ell_stroke.width(3.0); VertexSourceApplyTransform trans_ell = new VertexSourceApplyTransform(ell, tr); VertexSourceApplyTransform trans_ell_stroke = new VertexSourceApplyTransform(ell_stroke, tr); g_rasterizer.add_path(trans_ell); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0.5, 0.3, 0.0, 0.3)); g_rasterizer.add_path(trans_ell_stroke); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0.0, 0.3, 0.2, 1.0)); } } else { Perspective tr = new Perspective(lionShape.Bounds.Left, lionShape.Bounds.Bottom, lionShape.Bounds.Right, lionShape.Bounds.Top, quadPolygonControl.polygon()); if (tr.is_valid()) { // Render transformed lion foreach (var shape in lionShape.Shapes) { VertexSourceApplyTransform trans = new VertexSourceApplyTransform(shape.VertexStorage, tr); g_rasterizer.reset(); g_rasterizer.add_path(trans); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, shape.Color); } // Render transformed ellipse VertexSource.Ellipse FilledEllipse = new MatterHackers.Agg.VertexSource.Ellipse((lionShape.Bounds.Left + lionShape.Bounds.Right) * 0.5, (lionShape.Bounds.Bottom + lionShape.Bounds.Top) * 0.5, (lionShape.Bounds.Right - lionShape.Bounds.Left) * 0.5, (lionShape.Bounds.Top - lionShape.Bounds.Bottom) * 0.5, 200); Stroke EllipseOutline = new Stroke(FilledEllipse); EllipseOutline.width(3.0); VertexSourceApplyTransform TransformedFilledEllipse = new VertexSourceApplyTransform(FilledEllipse, tr); VertexSourceApplyTransform TransformedEllipesOutline = new VertexSourceApplyTransform(EllipseOutline, tr); g_rasterizer.add_path(TransformedFilledEllipse); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0.5, 0.3, 0.0, 0.3)); g_rasterizer.add_path(TransformedEllipesOutline); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0.0, 0.3, 0.2, 1.0)); } } //-------------------------- // Render the "quad" tool and controls g_rasterizer.add_path(quadPolygonControl); scanlineRenderer.RenderSolid(clippingProxy, g_rasterizer, g_scanline, new Color(0, 0.3, 0.5, 0.6)); //m_trans_type.Render(g_rasterizer, g_scanline, clippingProxy); base.OnDraw(graphics2D); }