Exemple #1
0
        public override void OnDraw(Graphics2D graphics2D)
        {
            ImageBuffer widgetsSubImage = ImageBuffer.NewSubImageReference(graphics2D.DestImage, graphics2D.GetClippingRect());

            int width = (int)widgetsSubImage.Width;
            int height = (int)widgetsSubImage.Height;

            if (numMasksSlider.Value != sliderValue)
            {
                generate_alpha_mask(width, height);
                sliderValue = numMasksSlider.Value;
            }

            rasterizer.SetVectorClipBox(0, 0, width, height);

            unsafe
            {
                alphaMaskImageBuffer.AttachBuffer(alphaByteArray, 0, width, height, width, 8, 1);

                MatterHackers.Agg.Image.AlphaMaskAdaptor imageAlphaMaskAdaptor = new MatterHackers.Agg.Image.AlphaMaskAdaptor(widgetsSubImage, alphaMask);
                ImageClippingProxy alphaMaskClippingProxy = new ImageClippingProxy(imageAlphaMaskAdaptor);
                ImageClippingProxy clippingProxy = new ImageClippingProxy(widgetsSubImage);

                Affine transform = Affine.NewIdentity();
                transform *= Affine.NewTranslation(-lionShape.Center.x, -lionShape.Center.y);
                transform *= Affine.NewScaling(lionScale, lionScale);
                transform *= Affine.NewRotation(angle + Math.PI);
                transform *= Affine.NewSkewing(skewX / 1000.0, skewY / 1000.0);
                transform *= Affine.NewTranslation(Width / 2, Height / 2);

                clippingProxy.clear(new RGBA_Floats(1, 1, 1));

                ScanlineRenderer scanlineRenderer = new ScanlineRenderer();
                // draw a background to show how the mask is working better
                int RectWidth = 30;
                for (int i = 0; i < 40; i++)
                {
                    for (int j = 0; j < 40; j++)
                    {
                        if ((i + j) % 2 != 0)
                        {
                            VertexSource.RoundedRect rect = new VertexSource.RoundedRect(i * RectWidth, j * RectWidth, (i + 1) * RectWidth, (j + 1) * RectWidth, 0);
                            rect.normalize_radius();

                            // Drawing as an outline
                            rasterizer.add_path(rect);
                            scanlineRenderer.render_scanlines_aa_solid(clippingProxy, rasterizer, scanlineCache, new RGBA_Bytes(.9, .9, .9));
                        }
                    }
                }

                //int x, y;

                // Render the lion
                VertexSourceApplyTransform trans = new VertexSourceApplyTransform(lionShape.Path, transform);
                scanlineRenderer.RenderSolidAllPaths(alphaMaskClippingProxy, rasterizer, scanlineCache, trans, lionShape.Colors, lionShape.PathIndex, lionShape.NumPaths);

                /*
                // Render random Bresenham lines and markers
                agg::renderer_markers<amask_ren_type> m(r);
                for(i = 0; i < 50; i++)
                {
                    m.line_color(agg::rgba8(randGenerator.Next() & 0x7F, 
                                            randGenerator.Next() & 0x7F, 
                                            randGenerator.Next() & 0x7F, 
                                            (randGenerator.Next() & 0x7F) + 0x7F)); 
                    m.fill_color(agg::rgba8(randGenerator.Next() & 0x7F, 
                                            randGenerator.Next() & 0x7F, 
                                            randGenerator.Next() & 0x7F, 
                                            (randGenerator.Next() & 0x7F) + 0x7F));

                    m.line(m.coord(randGenerator.Next() % width), m.coord(randGenerator.Next() % height), 
                           m.coord(randGenerator.Next() % width), m.coord(randGenerator.Next() % height));

                    m.marker(randGenerator.Next() % width, randGenerator.Next() % height, randGenerator.Next() % 10 + 5,
                             agg::marker_e(randGenerator.Next() % agg::end_of_markers));
                }


                // Render random anti-aliased lines
                double w = 5.0;
                agg::line_profile_aa profile;
                profile.width(w);

                typedef agg::renderer_outline_aa<amask_ren_type> renderer_type;
                renderer_type ren(r, profile);

                typedef agg::rasterizer_outline_aa<renderer_type> rasterizer_type;
                rasterizer_type ras(ren);
                ras.round_cap(true);

                for(i = 0; i < 50; i++)
                {
                    ren.Color = agg::rgba8(randGenerator.Next() & 0x7F, 
                                         randGenerator.Next() & 0x7F, 
                                         randGenerator.Next() & 0x7F, 
                                         //255));
                                         (randGenerator.Next() & 0x7F) + 0x7F); 
                    ras.move_to_d(randGenerator.Next() % width, randGenerator.Next() % height);
                    ras.line_to_d(randGenerator.Next() % width, randGenerator.Next() % height);
                    ras.render(false);
                }


                // Render random circles with gradient
                typedef agg::gradient_linear_color<color_type> grad_color;
                typedef agg::gradient_circle grad_func;
                typedef agg::span_interpolator_linear<> interpolator_type;
                typedef agg::span_gradient<color_type, 
                                          interpolator_type, 
                                          grad_func, 
                                          grad_color> span_grad_type;

                agg::trans_affine grm;
                grad_func grf;
                grad_color grc(agg::rgba8(0,0,0), agg::rgba8(0,0,0));
                agg::ellipse ell;
                agg::span_allocator<color_type> sa;
                interpolator_type inter(grm);
                span_grad_type sg(inter, grf, grc, 0, 10);
                agg::renderer_scanline_aa<amask_ren_type, 
                                          agg::span_allocator<color_type>,
                                          span_grad_type> rg(r, sa, sg);
                for(i = 0; i < 50; i++)
                {
                    x = randGenerator.Next() % width;
                    y = randGenerator.Next() % height;
                    double r = randGenerator.Next() % 10 + 5;
                    grm.reset();
                    grm *= agg::trans_affine_scaling(r / 10.0);
                    grm *= agg::trans_affine_translation(x, y);
                    grm.invert();
                    grc.colors(agg::rgba8(255, 255, 255, 0),
                               agg::rgba8(randGenerator.Next() & 0x7F, 
                                          randGenerator.Next() & 0x7F, 
                                          randGenerator.Next() & 0x7F, 
                                          255));
                    sg.color_function(grc);
                    ell.init(x, y, r, r, 32);
                    g_rasterizer.add_path(ell);
                    agg::render_scanlines(g_rasterizer, g_scanline, rg);
                }
                 */

                //m_num_cb.Render(g_rasterizer, g_scanline, clippingProxy);
            }
            alphaMaskImageBuffer.DettachBuffer();
            base.OnDraw(graphics2D);
        }
Exemple #2
0
        public override void OnDraw(Graphics2D graphics2D)
        {
            ImageBuffer widgetsSubImage = ImageBuffer.NewSubImageReference(graphics2D.DestImage, graphics2D.GetClippingRect());

            int width  = (int)widgetsSubImage.Width;
            int height = (int)widgetsSubImage.Height;

            if (numMasksSlider.Value != sliderValue)
            {
                generate_alpha_mask(width, height);
                sliderValue = numMasksSlider.Value;
            }

            rasterizer.SetVectorClipBox(0, 0, width, height);

            unsafe
            {
                alphaMaskImageBuffer.AttachBuffer(alphaByteArray, 0, width, height, width, 8, 1);

                MatterHackers.Agg.Image.AlphaMaskAdaptor imageAlphaMaskAdaptor = new MatterHackers.Agg.Image.AlphaMaskAdaptor(widgetsSubImage, alphaMask);
                ImageClippingProxy alphaMaskClippingProxy = new ImageClippingProxy(imageAlphaMaskAdaptor);
                ImageClippingProxy clippingProxy          = new ImageClippingProxy(widgetsSubImage);

                Affine transform = Affine.NewIdentity();
                transform *= Affine.NewTranslation(-lionShape.Center.X, -lionShape.Center.Y);
                transform *= Affine.NewScaling(lionScale, lionScale);
                transform *= Affine.NewRotation(angle + Math.PI);
                transform *= Affine.NewSkewing(skewX / 1000.0, skewY / 1000.0);
                transform *= Affine.NewTranslation(Width / 2, Height / 2);

                clippingProxy.clear(new ColorF(1, 1, 1));

                ScanlineRenderer scanlineRenderer = new ScanlineRenderer();
                // draw a background to show how the mask is working better
                int RectWidth = 30;
                for (int i = 0; i < 40; i++)
                {
                    for (int j = 0; j < 40; j++)
                    {
                        if ((i + j) % 2 != 0)
                        {
                            VertexSource.RoundedRect rect = new VertexSource.RoundedRect(i * RectWidth, j * RectWidth, (i + 1) * RectWidth, (j + 1) * RectWidth, 0);
                            rect.normalize_radius();

                            // Drawing as an outline
                            rasterizer.add_path(rect);
                            scanlineRenderer.RenderSolid(clippingProxy, rasterizer, scanlineCache, new Color(.9, .9, .9));
                        }
                    }
                }

                //int x, y;

                // Render the lion
                VertexSourceApplyTransform trans = new VertexSourceApplyTransform(lionShape.Path, transform);
                scanlineRenderer.RenderSolidAllPaths(alphaMaskClippingProxy, rasterizer, scanlineCache, trans, lionShape.Colors, lionShape.PathIndex, lionShape.NumPaths);

                /*
                 * // Render random Bresenham lines and markers
                 * agg::renderer_markers<amask_ren_type> m(r);
                 * for(i = 0; i < 50; i++)
                 * {
                 *      m.line_color(agg::rgba8(randGenerator.Next() & 0x7F,
                 *                                                      randGenerator.Next() & 0x7F,
                 *                                                      randGenerator.Next() & 0x7F,
                 *                                                      (randGenerator.Next() & 0x7F) + 0x7F));
                 *      m.fill_color(agg::rgba8(randGenerator.Next() & 0x7F,
                 *                                                      randGenerator.Next() & 0x7F,
                 *                                                      randGenerator.Next() & 0x7F,
                 *                                                      (randGenerator.Next() & 0x7F) + 0x7F));
                 *
                 *      m.line(m.coord(randGenerator.Next() % width), m.coord(randGenerator.Next() % height),
                 *                 m.coord(randGenerator.Next() % width), m.coord(randGenerator.Next() % height));
                 *
                 *      m.marker(randGenerator.Next() % width, randGenerator.Next() % height, randGenerator.Next() % 10 + 5,
                 *                       agg::marker_e(randGenerator.Next() % agg::end_of_markers));
                 * }
                 *
                 * // Render random anti-aliased lines
                 * double w = 5.0;
                 * agg::line_profile_aa profile;
                 * profile.width(w);
                 *
                 * typedef agg::renderer_outline_aa<amask_ren_type> renderer_type;
                 * renderer_type ren(r, profile);
                 *
                 * typedef agg::rasterizer_outline_aa<renderer_type> rasterizer_type;
                 * rasterizer_type ras(ren);
                 * ras.round_cap(true);
                 *
                 * for(i = 0; i < 50; i++)
                 * {
                 *      ren.Color = agg::rgba8(randGenerator.Next() & 0x7F,
                 *                                               randGenerator.Next() & 0x7F,
                 *                                               randGenerator.Next() & 0x7F,
                 *                                               //255));
                 *                                               (randGenerator.Next() & 0x7F) + 0x7F);
                 *      ras.move_to_d(randGenerator.Next() % width, randGenerator.Next() % height);
                 *      ras.line_to_d(randGenerator.Next() % width, randGenerator.Next() % height);
                 *      ras.render(false);
                 * }
                 *
                 * // Render random circles with gradient
                 * typedef agg::gradient_linear_color<color_type> grad_color;
                 * typedef agg::gradient_circle grad_func;
                 * typedef agg::span_interpolator_linear<> interpolator_type;
                 * typedef agg::span_gradient<color_type,
                 *                                                interpolator_type,
                 *                                                grad_func,
                 *                                                grad_color> span_grad_type;
                 *
                 * agg::trans_affine grm;
                 * grad_func grf;
                 * grad_color grc(agg::rgba8(0,0,0), agg::rgba8(0,0,0));
                 * agg::ellipse ell;
                 * agg::span_allocator<color_type> sa;
                 * interpolator_type inter(grm);
                 * span_grad_type sg(inter, grf, grc, 0, 10);
                 * agg::renderer_scanline_aa<amask_ren_type,
                 *                                                agg::span_allocator<color_type>,
                 *                                                span_grad_type> rg(r, sa, sg);
                 * for(i = 0; i < 50; i++)
                 * {
                 *      x = randGenerator.Next() % width;
                 *      y = randGenerator.Next() % height;
                 *      double r = randGenerator.Next() % 10 + 5;
                 *      grm.reset();
                 *      grm *= agg::trans_affine_scaling(r / 10.0);
                 *      grm *= agg::trans_affine_translation(x, y);
                 *      grm.invert();
                 *      grc.colors(agg::rgba8(255, 255, 255, 0),
                 *                         agg::rgba8(randGenerator.Next() & 0x7F,
                 *                                                randGenerator.Next() & 0x7F,
                 *                                                randGenerator.Next() & 0x7F,
                 *                                                255));
                 *      sg.color_function(grc);
                 *      ell.init(x, y, r, r, 32);
                 *      g_rasterizer.add_path(ell);
                 *      agg::render_scanlines(g_rasterizer, g_scanline, rg);
                 * }
                 */

                //m_num_cb.Render(g_rasterizer, g_scanline, clippingProxy);
            }
            alphaMaskImageBuffer.DettachBuffer();
            base.OnDraw(graphics2D);
        }
Exemple #3
0
        private void _InternalRender(IVertexSource vertexSource, RGBA_Bytes color)
        {
            if (_clipBuffer != null) {
                // DEBUG_saveImageBuffer(_clipBuffer);
                // DEBUG_saveImageBuffer(this.imb);

                IAlphaMask alphaMask = new AlphaMaskByteClipped (_clipBuffer, 1, 0);
                AlphaMaskAdaptor imageAlphaMaskAdaptor = new AlphaMaskAdaptor (aggGc.DestImage, alphaMask);
                ImageClippingProxy alphaMaskClippingProxy = new ImageClippingProxy (imageAlphaMaskAdaptor);

                var scanlineRenderer = new ScanlineRenderer ();
                var rasterizer = new ScanlineRasterizer ();
                var scanlineCache = new ScanlineCachePacked8();

                VertexSourceApplyTransform trans = new VertexSourceApplyTransform(vertexSource, aggGc.GetTransform());
                rasterizer.add_path(trans);

                scanlineRenderer.render_scanlines_aa_solid(alphaMaskClippingProxy,rasterizer,scanlineCache,color);
                aggGc.DestImage.MarkImageChanged();
            } else {
                aggGc.Render (vertexSource, color);
            }
        }