Example #1
0
        private void PlaybackForm_Load(object sender, EventArgs e)
        {
            ImageViewer   viewer = new ImageViewer();
            Anime4KScaler scaler = new Anime4KScaler(Anime4KAlgorithmVersion.v10RC2);

            Application.Idle += (s, ea) => {
                if (capture != null)
                {
                    Mat frame = capture.QueryFrame();

                    Image <Rgba32> scaled = scaler.Scale(ToImageSharpImage <Rgba32>(frame.ToBitmap()), sHeight / iHeight);
                    viewer.Image = ToBitmap(scaled).ToImage <Bgr, byte>();
                }
            };
            viewer.ShowDialog();

            //timer1.Start();
        }
Example #2
0
        /// <summary>
        /// Anime4K Cli main function
        /// </summary>
        /// <param name="args">console arguments</param>
        /// <returns>the return code</returns>
        static ErrorCode MainA4K(string[] args)
        {
            //error when no args are given
            if (args.Length <= 0)
            {
                return(ErrorCode.NO_ARGUMENTS);
            }

            //prepare variables (+ default values) that should be parsed from command line
            string         inputFile;
            string         outputFile;
            float          scaleFactor      = 2f;
            Size           targetSize       = Size.Empty;
            float          strengthColor    = -1f;
            float          strengthGradient = -1f;
            int            passes           = 1;
            bool           debug            = false;
            Anime4KVersion version          = Anime4KVersion.v09;

            //parse the command line arguments
            Dictionary <string, string> cArgs = ParseArgs(args, new char[] { '-', '/' });

            //check for help arg
            if (cArgs.TryGetValue("help", "?", out string _))
            {
                PrintHelp("HELP_ARG_SUPPLIED");
            }

            #region Parse Argument List

            //get input path
            if (!cArgs.TryGetValue("input", "i", out inputFile) ||
                string.IsNullOrWhiteSpace(inputFile) || !File.Exists(inputFile))
            {
                return(ErrorCode.CANNOT_FIND_INPUT_FILE);
            }


            //get output path
            if (!cArgs.TryGetValue("output", "o", out outputFile))
            {
                //default to inputFile + "_anime4k.png"
                outputFile = Path.Combine(Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile) + "_anime4k.png");
            }

            //get scale
            if (cArgs.TryGetValue("scale", "s", out string scaleStr) &&
                !float.TryParse(scaleStr, out scaleFactor))
            {
                Console.WriteLine($"Failed to parse scale factor value from input string \"{scaleStr}\"! Defaulting to {scaleFactor}");
            }

            //get resolution
            if (cArgs.TryGetValue("resolution", "r", out string resStr) &&
                !TryParseSize(resStr, out targetSize))
            {
                Console.WriteLine($"Failed to parse target resolution value from input string \"{resStr}\"!");
            }

            //get color strength
            if (cArgs.TryGetValue("strenghtC", "sc", out string strenghtColStr) &&
                !float.TryParse(strenghtColStr, out strengthColor))
            {
                Console.WriteLine($"Failed to parse color push strength value from input string \"{strenghtColStr}\"!");
            }

            //get gradient strength
            if (cArgs.TryGetValue("strenghtG", "sg", out string strenghtGradStr) &&
                !float.TryParse(strenghtGradStr, out strengthGradient))
            {
                Console.WriteLine($"Failed to parse gradient push strength value from input string \"{strenghtGradStr}\"!");
            }

            //get a4k laps count
            if (cArgs.TryGetValue("passes", "p", out string passStr) &&
                !int.TryParse(passStr, out passes))
            {
                Console.WriteLine($"Failed to parse anime4k pass count value from input string \"{passStr}\"!");
            }

            //get debug flag
            if (cArgs.TryGetValue("debug", "d", out string dbStr))
            {
                //default to true if no value given
                if (string.IsNullOrWhiteSpace(dbStr))
                {
                    dbStr = "true";
                }

                if (!bool.TryParse(dbStr, out debug))
                {
                    Console.WriteLine($"Failed to parse debug flag from input string \"{dbStr}\"!");
                }
            }

            //get version
            if (cArgs.TryGetValue("version", "v", out string verStr) &&
                !Enum.TryParse(verStr, true, out version))
            {
                Console.WriteLine($"Failed to parse anime4k version from input string \"{verStr}\"!");
            }

            #endregion

            //get mode flags
            bool hasTargetSize     = targetSize != Size.Empty;
            bool hasStrengthValues = strengthColor >= 0 && strengthGradient >= 0;

            //dump out input parameters
            Console.WriteLine($@"
Input Parameters DUMP:
--Files-----------------------------------------
Input File Path:    ""{inputFile}""
Output File Path:   ""{outputFile}""
Save Sub- Phases:   {(debug ? "YES" : "NO")}
--Resolution--------------------------------------
Scale Factor:       {scaleFactor}
Target Resolution:  {targetSize.Width} x {targetSize.Height}
Use Resolution:     {(hasTargetSize ? "YES" : "NO")}
--Anime4K-Config----------------------------------
Anime4K Version:        {version}
Anime4K Passes:         {passes}
Color Push Strength:    {(strengthColor == -1 ? "AUTO" : strengthColor.ToString())}
Gradient Push Strength: {(strengthGradient == -1 ? "AUTO" : strengthGradient.ToString())}
Use User Strengths:     {(hasStrengthValues ? "YES" : "NO")}
");

            //load input file image
            Console.WriteLine("Load Source Image...");
            Image <Rgba32> inputImg = Image.Load <Rgba32>(inputFile);

            #region Create Scaler
            //create scaler
            Anime4KScaler scaler;
            switch (version)
            {
            case Anime4KVersion.v10RC2:
            {
                //Create anime4k scaler version 1.0 RC2
                scaler = new Anime4KScaler(new Anime4K010RC2());
                break;
            }

            case Anime4KVersion.v09:
            default:
            {
                //Create anime4k scaler version 0.9 (default)
                scaler = new Anime4KScaler(new Anime4K09());
                break;
            }
            }
            #endregion

            #region Run Anime4K
            //apply scaling according to mode flags
            Console.WriteLine("Run Anime4K based on mode flags...");
            Image <Rgba32> outputImg;
            Stopwatch      sw = new Stopwatch();
            sw.Start();
            if (hasTargetSize)
            {
                if (hasStrengthValues)
                {
                    //target size + strength
                    outputImg = scaler.Scale(inputImg, targetSize.Width, targetSize.Height, passes, strengthColor, strengthGradient, debug);
                }
                else
                {
                    //target size no strength
                    outputImg = scaler.Scale(inputImg, targetSize.Width, targetSize.Height, passes, debug);
                }
            }
            else
            {
                if (hasStrengthValues)
                {
                    //scale factor + strength
                    outputImg = scaler.Scale(inputImg, scaleFactor, passes, strengthColor, strengthGradient, debug);
                }
                else
                {
                    //scale factor no strength
                    outputImg = scaler.Scale(inputImg, scaleFactor, passes, debug);
                }
            }
            sw.Stop();
            #endregion

            //save output image
            Console.WriteLine($"Anime4K Finished in {sw.ElapsedMilliseconds} ms ({sw.Elapsed.TotalSeconds.ToString("0.##")} s)");
            Console.WriteLine("Saving output image...");
            outputImg.Save(outputFile);

            //exit without error
            Console.WriteLine("Finished!");
            return(ErrorCode.NO_ERROR);
        }