예제 #1
0
        public void CsPotrace_PotraceTrace_LoadBitmap_Points()
        {
            using var bmp = getSquare();
            Potrace po     = new Potrace();
            var     result = po.PotraceTrace(bmp);

            var points = result[0].SelectMany(p => new dPoint[] { p.A, p.B }).ToArray();

            Assert.Equal(8, points.Length);

            Assert.Equal(2, points[0].X);
            Assert.Equal(5, points[0].Y);
            Assert.Equal(5, points[1].X);
            Assert.Equal(8, points[1].Y);
            Assert.Equal(5, points[2].X);
            Assert.Equal(8, points[2].Y);
            Assert.Equal(8, points[3].X);
            Assert.Equal(5, points[3].Y);
            Assert.Equal(8, points[4].X);
            Assert.Equal(5, points[4].Y);
            Assert.Equal(5, points[5].X);
            Assert.Equal(2, points[5].Y);
            Assert.Equal(5, points[6].X);
            Assert.Equal(2, points[6].Y);
            Assert.Equal(2, points[7].X);
            Assert.Equal(5, points[7].Y);
        }
예제 #2
0
        public void CsPotrace_PotraceTrace_LoadBitmap_CurveCount()
        {
            using var bmp = getSquare();
            Potrace po     = new Potrace();
            var     result = po.PotraceTrace(bmp);

            Assert.Single(result); // External List
            Assert.Equal(4, result[0].Count);
        }
예제 #3
0
        public void CsPotrace_PotraceTrace_LoadBitmap_CurveCountCircle()
        {
            using var bmp = getCircle();
            Potrace po     = new Potrace();
            var     result = po.PotraceTrace(bmp);

            var points = result[0].SelectMany(p => new dPoint[] { p.A, p.B }).ToArray();

            Assert.Equal(8, points.Length);
        }
예제 #4
0
        private void PreviewVector(Bitmap bmp)
        {
            Potrace.turdsize        = (int)(UseSpotRemoval ? SpotRemoval : 2);
            Potrace.alphamax        = UseSmoothing ? (double)Smoothing : 0.0;
            Potrace.opttolerance    = UseOptimize ? (double)Optimize : 0.2;
            Potrace.curveoptimizing = UseOptimize;             //optimize the path p, replacing sequences of Bezier segments by a single segment when possible.

            if (MustExitTH)
            {
                return;
            }

            List <List <CsPotrace.Curve> > plist = Potrace.PotraceTrace(bmp);

            if (MustExitTH)
            {
                return;
            }

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.White);                 //remove original image

                using (Brush fill = new SolidBrush(Color.FromArgb(FillingDirection != Direction.None ? 255 : 30, Color.Black)))
                    Potrace.Export2GDIPlus(plist, g, fill, null, 1);                     //trace filling

                if (MustExitTH)
                {
                    return;
                }

                PreviewLineByLine(bmp);                 //process filling with line by line preview

                if (MustExitTH)
                {
                    return;
                }

                Potrace.Export2GDIPlus(plist, g, null, Pens.Red, 0);                 //trace borders

                if (MustExitTH)
                {
                    return;
                }
            }
        }
예제 #5
0
        public void LoadImagePotrace(Bitmap bmp, string filename, bool UseSpotRemoval, int SpotRemoval, bool UseSmoothing, decimal Smoothing, bool UseOptimize, decimal Optimize, L2LConf c)
        {
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

            long start = Tools.HiResTimer.TotalMilliseconds;

            mTotalTravelOff   = 0;
            mTotalTravelOn    = 0;
            mEstimatedTimeOff = TimeSpan.Zero;
            mEstimatedTimeOn  = TimeSpan.Zero;
            list.Clear();
            mRange.ResetRange();

            Potrace.turdsize        = (int)(UseSpotRemoval ? SpotRemoval : 2);
            Potrace.alphamax        = UseSmoothing ? (double)Smoothing : 0.0;
            Potrace.opttolerance    = UseOptimize ? (double)Optimize : 0.2;
            Potrace.curveoptimizing = UseOptimize;             //optimize the path p, replacing sequences of Bezier segments by a single segment when possible.

            List <List <CsPotrace.Curve> > plist = Potrace.PotraceTrace(bmp);

            if (c.dir != RasterConverter.ImageProcessor.Direction.None)
            {
                using (Bitmap ptb = new Bitmap(bmp.Width, bmp.Height))
                {
                    using (Graphics g = Graphics.FromImage(ptb))
                    {
                        //Potrace.Export2GDIPlus(plist, g, Brushes.Black, null, (Math.Max(c.res/c.fres, 1) + 1) / 2.0f);
                        Potrace.Export2GDIPlus(plist, g, Brushes.Black, null, Math.Max(1, c.res / c.fres));
                        using (Bitmap resampled = RasterConverter.ImageTransform.ResizeImage(ptb, new Size((int)(bmp.Width * c.fres / c.res), (int)(bmp.Height * c.fres / c.res)), true, InterpolationMode.HighQualityBicubic))
                        {
                            //absolute
                            list.Add(new GrblCommand("G90"));
                            //use travel speed
                            list.Add(new GrblCommand(String.Format("F{0}", c.travelSpeed)));
                            //move fast to offset
                            list.Add(new GrblCommand(String.Format("G0 X{0} Y{1}", formatnumber(c.oX), formatnumber(c.oY))));
                            if (c.pwm)
                            {
                                list.Add(new GrblCommand(String.Format("{0} S0", c.lOn)));                                 //laser on and power to zero
                            }
                            else
                            {
                                list.Add(new GrblCommand(String.Format("{0} S255", c.lOff)));                                 //laser off and power to max power
                            }
                            //set speed to markspeed
                            list.Add(new GrblCommand(String.Format("G1 F{0}", c.markSpeed)));
                            //relative
                            list.Add(new GrblCommand("G91"));


                            c.vectorfilling = true;
                            ImageLine2Line(resampled, c);

                            //laser off
                            list.Add(new GrblCommand(c.lOff));
                        }
                    }
                }
            }

            //absolute
            list.Add(new GrblCommand("G90"));
            //use travel speed
            list.Add(new GrblCommand(String.Format("F{0}", c.travelSpeed)));
            //move fast to offset
            list.Add(new GrblCommand(String.Format("G0 X{0} Y{1}", formatnumber(c.oX), formatnumber(c.oY))));
            //laser off and power to maxPower
            list.Add(new GrblCommand(String.Format("{0} S{1}", c.lOff, c.maxPower)));
            //set speed to borderspeed
            list.Add(new GrblCommand(String.Format("G1 F{0}", c.borderSpeed)));

            //trace borders
            List <string> gc = Potrace.Export2GCode(plist, c.oX, c.oY, c.res, c.lOn, c.lOff, bmp.Size);

            foreach (string code in gc)
            {
                list.Add(new GrblCommand(code));
            }


            //laser off
            list.Add(new GrblCommand(String.Format("{0}", c.lOff)));

            //move fast to origin
            list.Add(new GrblCommand("G0 X0 Y0"));

            Analyze();
            long elapsed = Tools.HiResTimer.TotalMilliseconds - start;

            if (OnFileLoaded != null)
            {
                OnFileLoaded(elapsed, filename);
            }
        }
예제 #6
0
        public void LoadImagePotrace(Bitmap bmp, string filename, bool UseSpotRemoval, int SpotRemoval, bool UseSmoothing, decimal Smoothing, bool UseOptimize, decimal Optimize, bool useOptimizeFast, L2LConf c, bool append)
        {
            RiseOnFileLoading(filename);

            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
            DateTime start = DateTime.Now;

            if (!append)
            {
                list.Clear();
            }

            Potrace potrace = new Potrace();

            //list.Add(new GrblCommand("G90")); //absolute (Moved to custom Header)

            mRange.ResetRange();

            potrace.turdsize        = (int)(UseSpotRemoval ? SpotRemoval : 2);
            potrace.alphamax        = UseSmoothing ? (double)Smoothing : 0.0;
            potrace.opttolerance    = UseOptimize ? (double)Optimize : 0.2;
            potrace.curveoptimizing = UseOptimize;             //optimize the path p, replacing sequences of Bezier segments by a single segment when possible.

            List <List <Curve> > plist = potrace.PotraceTrace(bmp);

            if (c.dir != RasterConverter.Direction.None)
            {
                using (Bitmap ptb = new Bitmap(bmp.Width, bmp.Height))
                {
                    using (Graphics g = Graphics.FromImage(ptb))
                    {
                        //Potrace.Export2GDIPlus(plist, g, Brushes.Black, null, (Math.Max(c.res/c.fres, 1) + 1) / 2.0f);
                        Potrace.Export2GDIPlus(plist, g, Brushes.Black, null, Math.Max(1, c.res / c.fres));
                        using (Bitmap resampled = RasterConverter.ImageTransform.ResizeImage(ptb, new Size((int)(bmp.Width * c.fres / c.res), (int)(bmp.Height * c.fres / c.res)), true, InterpolationMode.HighQualityBicubic))
                        {
                            if (c.pwm)
                            {
                                list.Add(new GrblCommand(String.Format("{0} S0", c.lOn)));                                 //laser on and power to zero
                            }
                            else
                            {
                                list.Add(new GrblCommand(String.Format("{0} S255", c.lOff)));                                 //laser off and power to max power
                            }
                            //set speed to markspeed
                            // For marlin, need to specify G1 each time :
                            // list.Add(new GrblCommand(String.Format("G1 F{0}", c.markSpeed)));
                            list.Add(new GrblCommand(String.Format("F{0}", c.markSpeed)));

                            c.vectorfilling = true;
                            ImageLine2Line(resampled, c);

                            //laser off
                            list.Add(new GrblCommand(c.lOff));
                        }
                    }
                }
            }

            //Optimize fast movement
            if (useOptimizeFast)
            {
                plist = OptimizePaths(plist);
            }

            //laser off and power to maxPower
            list.Add(new GrblCommand(String.Format("{0} S{1}", c.lOff, c.maxPower)));
            //set speed to borderspeed
            // For marlin, need to specify G1 each time :
            //list.Add(new GrblCommand(String.Format("G1 F{0}", c.borderSpeed)));
            list.Add(new GrblCommand(String.Format("F{0}", c.borderSpeed)));

            //trace borders
            List <string> gc = Potrace.Export2GCode(plist, c.oX, c.oY, c.res, c.lOn, c.lOff, bmp.Size);

            foreach (string code in gc)
            {
                list.Add(new GrblCommand(code));
            }

            //laser off (superflua??)
            //list.Add(new GrblCommand(String.Format("{0}", c.lOff)));

            Analyze();
            long elapsed = (long)(DateTime.Now - start).TotalMilliseconds;

            RiseOnFileLoaded(filename, elapsed);
        }