Ejemplo n.º 1
0
        private void ExtractSegment(Bitmap image, int x, int y, bool reverse, ref int len, ref int prevCol, List <ColorSegment> rv, int minPower, int maxPower, int res, RasterConverter.ImageProcessor.Direction dir)
        {
            //System.Diagnostics.Debug.WriteLine(String.Format("X:{0} Y:{1}", x, y));

            len++;
            int col = GetColor(image, x, y, minPower, maxPower);

            if (prevCol == -1)
            {
                prevCol = col;
            }

            if (prevCol != col)
            {
                if (dir == RasterConverter.ImageProcessor.Direction.Horizontal)
                {
                    rv.Add(new XSegment(prevCol, len, res, reverse));
                }
                else if (dir == RasterConverter.ImageProcessor.Direction.Vertical)
                {
                    rv.Add(new YSegment(prevCol, len, res, reverse));
                }
                else if (dir == RasterConverter.ImageProcessor.Direction.Diagonal)
                {
                    rv.Add(new DSegment(prevCol, len, res, reverse));
                }

                len = 0;
            }

            prevCol = col;
        }
Ejemplo n.º 2
0
        private List <ColorSegment> GetSegments(Bitmap bmp, RasterConverter.ImageProcessor.Direction dir, int minPower, int maxPower, int res)
        {
            List <ColorSegment> rv = new List <ColorSegment>();

            if (dir == RasterConverter.ImageProcessor.Direction.Horizontal || dir == RasterConverter.ImageProcessor.Direction.Vertical)
            {
                bool h = (dir == RasterConverter.ImageProcessor.Direction.Horizontal);                 //horizontal/vertical

                for (int i = 0; i < (h ? bmp.Height : bmp.Width); i++)
                {
                    bool d       = IsEven(i);               //direct/reverse
                    int  prevCol = -1;
                    int  len     = -1;

                    for (int j = d ? 0 : (h ? bmp.Width - 1 : bmp.Height - 1); d ? (j < (h ? bmp.Width : bmp.Height)) : (j >= 0); j = (d ? j + 1 : j - 1))
                    {
                        ExtractSegment(bmp, h?j:i, h?i:j, !d, ref len, ref prevCol, rv, minPower, maxPower, res, dir);                          //extract different segments
                    }
                    if (h)
                    {
                        rv.Add(new XSegment(prevCol, len + 1, res, !d));                         //close last segment
                    }
                    else
                    {
                        rv.Add(new YSegment(prevCol, len + 1, res, !d));                         //close last segment
                    }
                    if (i < (h ? bmp.Height - 1 : bmp.Width - 1))
                    {
                        if (h)
                        {
                            rv.Add(new VSeparator(res));                             //new line
                        }
                        else
                        {
                            rv.Add(new HSeparator(res));                             //new line
                        }
                    }
                }
            }
            else if (dir == RasterConverter.ImageProcessor.Direction.Diagonal)
            {
                //based on: http://stackoverflow.com/questions/1779199/traverse-matrix-in-diagonal-strips
                //based on: http://stackoverflow.com/questions/2112832/traverse-rectangular-matrix-in-diagonal-strips

                /*
                 *
                 +------------+
                 |  -         |
                 |  -  -      |
                 +-------+    |
                 |  -  - |  - |
                 +-------+----+
                 |
                 */


                //the algorithm runs along the matrix for diagonal lines (slice index)
                //z1 and z2 contains the number of missing elements in the lower right and upper left
                //the length of the segment can be determined as "slice - z1 - z2"
                //my modified version of algorithm reverses travel direction each slice

                rv.Add(new VSeparator(res));                 //new line

                int w = bmp.Width;
                int h = bmp.Height;
                for (int slice = 0; slice < w + h - 1; ++slice)
                {
                    bool d = IsEven(slice);                     //direct/reverse

                    int prevCol = -1;
                    int len     = -1;

                    int z1 = slice < h ? 0 : slice - h + 1;
                    int z2 = slice < w ? 0 : slice - w + 1;

                    for (int j = (d ? z1 : slice - z2); d?j <= slice - z2 : j >= z1; j = (d ? j + 1 : j - 1))
                    {
                        ExtractSegment(bmp, j, slice - j, !d, ref len, ref prevCol, rv, minPower, maxPower, res, dir); //extract different segments
                    }
                    rv.Add(new DSegment(prevCol, len + 1, res, !d));                                                   //close last segment

                    //System.Diagnostics.Debug.WriteLine(String.Format("sl:{0} z1:{1} z2:{2}", slice, z1, z2));

                    if (slice < Math.Min(w, h) - 1)                   //first part of the image
                    {
                        if (d)
                        {
                            rv.Add(new HSeparator(res));                             //new line
                        }
                        else
                        {
                            rv.Add(new VSeparator(res));                             //new line
                        }
                    }
                    else if (slice >= Math.Max(w, h) - 1)                   //third part of image
                    {
                        if (d)
                        {
                            rv.Add(new VSeparator(res));                             //new line
                        }
                        else
                        {
                            rv.Add(new HSeparator(res));                             //new line
                        }
                    }
                    else                     //central part of the image
                    {
                        if (w > h)
                        {
                            rv.Add(new HSeparator(res));                             //new line
                        }
                        else
                        {
                            rv.Add(new VSeparator(res));                             //new line
                        }
                    }
                }
            }

            return(rv);
        }
Ejemplo n.º 3
0
        public void LoadImageL2L(Bitmap bmp, string filename, int res, int oX, int oY, int markSpeed, int travelSpeed, int minPower, int maxPower, string lOn, string lOff, RasterConverter.ImageProcessor.Direction dir)
        {
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

            long start = Tools.HiResTimer.TotalMilliseconds;

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

            //absolute
            list.Add(new GrblCommand("G90"));
            //use travel speed
            list.Add(new GrblCommand(String.Format("F{0}", travelSpeed)));
            //move fast to offset
            list.Add(new GrblCommand(String.Format("G0 X{0} Y{1}", formatnumber(oX), formatnumber(oY))));
            //laser on and power to zero
            list.Add(new GrblCommand(String.Format("{0} S0", lOn)));
            //set speed to markspeed
            list.Add(new GrblCommand(String.Format("G1 F{0}", markSpeed)));
            //relative
            list.Add(new GrblCommand("G91"));

            ImageLine2Line(bmp, res, markSpeed, travelSpeed, minPower, maxPower, lOn, lOff, dir);

            //laser off
            list.Add(new GrblCommand(lOff));
            //absolute
            list.Add(new GrblCommand("G90"));
            //move fast to origin
            list.Add(new GrblCommand("G0 X0 Y0"));

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

            if (OnFileLoaded != null)
            {
                OnFileLoaded(elapsed, filename);
            }
        }
Ejemplo n.º 4
0
        private void ImageLine2Line(Bitmap bmp, int res, int markSpeed, int travelSpeed, int minPower, int maxPower, string lOn, string lOff, RasterConverter.ImageProcessor.Direction dir)
        {
            bool fast = false;
            List <ColorSegment> segments = GetSegments(bmp, dir, minPower, maxPower, res);
            List <GrblCommand>  temp     = new List <GrblCommand>();

            foreach (ColorSegment seg in segments)
            {
                bool changespeed = (fast != seg.Fast);        //se veloce != dafareveloce

                if (seg.IsSeparator && !fast)                 //fast = previous segment contains S0 color
                {
                    temp.Add(new GrblCommand("S0"));
                }

                fast = seg.Fast;

                if (changespeed)
                {
                    temp.Add(new GrblCommand(String.Format("{0} F{1} {2}", fast ? "G0" : "G1", fast ? travelSpeed : markSpeed, seg.ToString())));
                }
                else
                {
                    temp.Add(new GrblCommand(seg.ToString()));
                }

                //if (seg.IsSeparator)
                //	list.Add(new GrblCommand(lOn));
            }

            temp = OptimizeLine2Line(temp, travelSpeed);
            list.AddRange(temp);
        }
Ejemplo n.º 5
0
        public void LoadImagePotrace(Bitmap bmp, string filename, int res, int oX, int oY, int borderSpeed, int markSpeed, int travelSpeed, int minPower, int maxPower, string lOn, string lOff, bool UseSpotRemoval, int SpotRemoval, bool UseSmoothing, decimal Smoothing, bool UseOptimize, decimal Optimize, RasterConverter.ImageProcessor.Direction fdir, int fres)
        {
            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 (fdir != 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(res / fres, 1) + 1) / 2.0f);
                        using (Bitmap resampled = RasterConverter.ImageTransform.ResizeImage(ptb, new Size(bmp.Width * fres / res, bmp.Height * fres / res), true, InterpolationMode.HighQualityBicubic))
                        {
                            //absolute
                            list.Add(new GrblCommand("G90"));
                            //use travel speed
                            list.Add(new GrblCommand(String.Format("F{0}", travelSpeed)));
                            //move fast to offset
                            list.Add(new GrblCommand(String.Format("G0 X{0} Y{1}", formatnumber(oX), formatnumber(oY))));
                            //laser on and power to zero
                            list.Add(new GrblCommand(String.Format("{0} S0", lOn)));
                            //set speed to markspeed
                            list.Add(new GrblCommand(String.Format("G1 F{0}", markSpeed)));
                            //relative
                            list.Add(new GrblCommand("G91"));

                            //trace all line
                            ImageLine2Line(resampled, fres, markSpeed, travelSpeed, minPower, maxPower, lOn, lOff, fdir);

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

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

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

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


            //laser off
            list.Add(new GrblCommand(String.Format("{0}", 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);
            }
        }