Example #1
0
        private void OnExecute()
        {
            ParametersModel parameters = new ParametersModel();

            // parse and validate arguments
            parameters.Mode            = (ShapeType)Mode;
            parameters.Alpha           = (byte)(Alpha ?? 0x80);
            parameters.Repeat          = Repeat ?? 0;
            parameters.Nprimitives     = Nprimitives ?? 10;
            parameters.NthFrame        = NthFrame ?? 1;
            parameters.CanvasResize    = InputResize ?? 256;
            parameters.RenderSize      = OutputSize ?? 1024;
            parameters.WorkersCount    = Workers ?? 0;
            parameters.ShapeProbeCount = ShapeProbeCount ?? 1000;
            parameters.ShapeAge        = ShapeAge ?? 100;

            // determine worker count
            if (parameters.WorkersCount < 1)
            {
                parameters.WorkersCount = Environment.ProcessorCount;
            }

            // read input image
            Logger.WriteLine(1, "reading {0}", InputFile);
            Image <Rgba32> inputImage = File.LoadImage(InputFile);

            // determine background color
            if (String.IsNullOrEmpty(Background))
            {
                parameters.Background = Core.Core.AverageImageColor(inputImage);
            }
            else
            {
                parameters.Background = Rgba32.FromHex(Background);
            }

            // run algorithm
            RendererModel model = new RendererModel(inputImage, parameters);

            model.RunRenderer();

            // write output image(s)
            foreach (var outFile in OutputFiles.Split(' '))
            {
                var  ext        = System.IO.Path.GetExtension(outFile).ToLower();
                bool percent    = outFile.Contains("{0");
                bool saveFrames = percent && !ext.Equals(".gif");

                Logger.WriteLine(1, "writing {0}", outFile);
                switch (ext)
                {
                case ".png":
                case ".jpg":
                case ".jpeg":
                    File.SaveFrames(outFile, model.GetFrames(saveFrames, parameters.NthFrame));
                    break;

                case ".svg":
                    File.SaveSVG(outFile, model.GetSVG(saveFrames, parameters.NthFrame));
                    break;

                case ".gif":
                    File.SaveGIF(outFile, model.GetFrames(0.001), 0, 0);
                    break;

                default:
                    throw new Exception("unrecognized file extension: " + ext);
                }
            }

            System.Console.WriteLine("End");
        }