Exemple #1
0
        public MainMod()
        {
            // initialize default settings
            ApiSettings.Initialize();

            Post("/api/inpaint", async x =>
            {
                try
                {
                    Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Incomming request from " + this.Request.UserHostAddress);
                    if (this.Request.Files.Count() < 2)
                    {
                        Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + $"] Error, {this.Request.Files.Count()} files found");
                        return("Err");
                    }

                    Bitmap BitmapImg = null, BitmapMask = null;
                    var donors       = new List <ZsImage>();
                    foreach (var file in this.Request.Files)
                    {
                        Bitmap tempBitmap;
                        byte[] ByteImg = new byte[file.Value.Length];
                        file.Value.Read(ByteImg, 0, (int)file.Value.Length);
                        using (MemoryStream ms = new MemoryStream(ByteImg))
                            tempBitmap = new Bitmap(ms);

                        if (BitmapImg == null)
                        {
                            BitmapImg = tempBitmap;
                        }
                        else if (BitmapMask == null)
                        {
                            BitmapMask = tempBitmap;
                        }
                        else
                        {
                            donors.Add(tempBitmap.ToArgbImage());
                        }
                    }

                    var imageArgb  = ConvertToArgbImage(BitmapImg);
                    var markupArgb = ConvertToArgbImage(BitmapMask);

                    var inpainter = new Inpainter();

                    // Convert body request to settings
                    InpaintSettings userSettings = new InpaintSettings();
                    try
                    {
                        userSettings = JsonConvert.DeserializeObject <InpaintSettings>(Request.Body.AsString());
                    }
                    catch { }

                    // Now merge then, giving priority to the default
                    if (userSettings.PatchSize > ApiSettings._Instance.InpaintSettings.PatchSize)
                    {
                        userSettings.PatchSize = ApiSettings._Instance.InpaintSettings.PatchSize;
                    }

                    if (userSettings.MaxInpaintIterations > ApiSettings._Instance.InpaintSettings.MaxInpaintIterations)
                    {
                        userSettings.MaxInpaintIterations = ApiSettings._Instance.InpaintSettings.MaxInpaintIterations;
                    }


                    Image finalResult = null;

                    inpainter.IterationFinished += (sender, eventArgs) =>
                    {
                        Bitmap iterationResult = eventArgs.InpaintedLabImage
                                                 .FromLabToRgb()
                                                 .FromRgbToBitmap();
                        finalResult = iterationResult;
                        Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] call on inpainter.IterationFinished (Level " + eventArgs.LevelIndex + ", Iteration " + eventArgs.InpaintIteration + ")"); //Debugging
                    };

                    await Task.Factory.StartNew(() => inpainter.Inpaint(imageArgb, markupArgb, userSettings, donors));

                    Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Processing finished");
#if DEBUG
                    finalResult.Save(@"..\..\TESTAPP.PNG"); //Debugging
#endif

                    MemoryStream stream = new MemoryStream();
                    finalResult.Save(stream, ImageFormat.Png);
                    return(Convert.ToBase64String(stream.ToArray())); //this does the job ¯\_(ツ)_/¯
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(null);
                }
            });

            Get("/api/settings", async x =>
            {
                return(Response.AsJson <ApiSettings>(ApiSettings._Instance));
            });

            Get(@"/", _ =>
            {
                return(Response.AsFile("TestWebsite/index.html", "text/html"));
            });

            Get("/ping", _ => "Ping is successful");
        }
Exemple #2
0
        // TODO: add Seamcarving Settings to the ApiSettings object ;D

        public static void Initialize()
        {
            _Instance = new ApiSettings();
        }