diff --git a/Code/Cupscale/MainForm.cs b/Code/Cupscale/MainForm.cs index c1926be..b7a985b 100644 --- a/Code/Cupscale/MainForm.cs +++ b/Code/Cupscale/MainForm.cs @@ -1,7 +1,6 @@ using System; using System.ComponentModel; using System.Drawing; -using System.Drawing; using System.Windows.Forms; using Cupscale.UI; using Cyotek.Windows.Forms; @@ -208,6 +207,8 @@ private void previewImg_DragDrop(object sender, DragEventArgs e) string[] array = e.Data.GetData(DataFormats.FileDrop) as string[]; previewImg.Text = ""; PreviewTabHelper.ResetCachedImages(); + if (!PreviewTabHelper.DroppedImageIsValid(array[0])) + return; previewImg.Image = IOUtils.GetImage(array[0]); Program.lastFilename = array[0]; PreviewTabHelper.currentScale = 1; @@ -260,6 +261,7 @@ private void InitializeComponent() this.panel3 = new System.Windows.Forms.Panel(); this.refreshPrevFullBtn = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.prevCutoutLabel = new System.Windows.Forms.Label(); this.prevSizeLabel = new System.Windows.Forms.Label(); this.prevZoomLabel = new System.Windows.Forms.Label(); this.previewGroupbox = new System.Windows.Forms.GroupBox(); @@ -306,7 +308,6 @@ private void InitializeComponent() this.label4 = new System.Windows.Forms.Label(); this.confSaveBtn = new System.Windows.Forms.Button(); this.alphaBgColorDialog = new System.Windows.Forms.ColorDialog(); - this.prevCutoutLabel = new System.Windows.Forms.Label(); this.mainTabControl.SuspendLayout(); this.upscaleTab.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout(); @@ -565,6 +566,15 @@ private void InitializeComponent() this.groupBox1.TabStop = false; this.groupBox1.Text = "Preview Info"; // + // prevCutoutLabel + // + this.prevCutoutLabel.AutoSize = true; + this.prevCutoutLabel.Location = new System.Drawing.Point(6, 48); + this.prevCutoutLabel.Name = "prevCutoutLabel"; + this.prevCutoutLabel.Size = new System.Drawing.Size(38, 13); + this.prevCutoutLabel.TabIndex = 8; + this.prevCutoutLabel.Text = "Cutout"; + // // prevSizeLabel // this.prevSizeLabel.AutoSize = true; @@ -721,6 +731,7 @@ private void InitializeComponent() this.previewImg.TabStop = false; this.previewImg.Text = "Drag And Drop An Image Into This Area"; this.previewImg.Zoomed += new System.EventHandler(this.previewImg_Zoomed); + this.previewImg.Click += new System.EventHandler(this.previewImg_Click); this.previewImg.DragDrop += new System.Windows.Forms.DragEventHandler(this.previewImg_DragDrop); this.previewImg.DragEnter += new System.Windows.Forms.DragEventHandler(this.previewImg_DragEnter); this.previewImg.MouseDown += new System.Windows.Forms.MouseEventHandler(this.previewImg_MouseDown); @@ -1143,15 +1154,6 @@ private void InitializeComponent() this.confSaveBtn.UseVisualStyleBackColor = false; this.confSaveBtn.Click += new System.EventHandler(this.confSaveEsrganBtn_Click); // - // prevCutoutLabel - // - this.prevCutoutLabel.AutoSize = true; - this.prevCutoutLabel.Location = new System.Drawing.Point(6, 48); - this.prevCutoutLabel.Name = "prevCutoutLabel"; - this.prevCutoutLabel.Size = new System.Drawing.Size(38, 13); - this.prevCutoutLabel.TabIndex = 8; - this.prevCutoutLabel.Text = "Cutout"; - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -1210,6 +1212,7 @@ private void copyComparisonClipboardBtn_Click(object sender, EventArgs e) private void previewImg_Zoomed(object sender, ImageBoxZoomEventArgs e) { UpdatePreviewInfo(); + if (previewImg.Zoom < 25) previewImg.Zoom = 25; } void UpdatePreviewInfo () @@ -1229,5 +1232,10 @@ private void refreshPrevFullBtn_Click(object sender, EventArgs e) { PreviewTabHelper.UpscalePreview(true); } + + private void previewImg_Click(object sender, EventArgs e) + { + + } } } diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 6463055..d114c76 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -83,11 +83,12 @@ public static bool IsFileValid(string path) return true; } - public static void Copy(string sourceDirectoryName, string targetDirectoryName, bool move = false) + public static void Copy(string sourceDir, string targetDir, bool move = false) { - Directory.CreateDirectory(targetDirectoryName); - DirectoryInfo source = new DirectoryInfo(sourceDirectoryName); - DirectoryInfo target = new DirectoryInfo(targetDirectoryName); + Logger.Log("Copying directory \"" + sourceDir + "\" to \"" + targetDir + "\" (Move: " + move + ")"); + Directory.CreateDirectory(targetDir); + DirectoryInfo source = new DirectoryInfo(sourceDir); + DirectoryInfo target = new DirectoryInfo(targetDir); CopyWork(source, target, move); } diff --git a/Code/UI/PreviewTabHelper.cs b/Code/UI/PreviewTabHelper.cs index 343cbfd..e77600a 100644 --- a/Code/UI/PreviewTabHelper.cs +++ b/Code/UI/PreviewTabHelper.cs @@ -4,6 +4,7 @@ using System.IO; using System.Numerics; using System.Security.AccessControl; +using System.Threading.Tasks; using System.Windows.Forms; using Cupscale.IO; using Cupscale.OS; @@ -37,21 +38,13 @@ public static void Init(ImageBox imgBox, ComboBox basicModelBox, ComboBox format public static void UpdateMode(int tabIndex) { if (tabIndex == 0) - { currentMode = Mode.Basic; - } if (tabIndex == 1) - { currentMode = Mode.Interp; - } if (tabIndex == 2) - { currentMode = Mode.Chain; - } if (tabIndex == 3) - { currentMode = Mode.Advanced; - } } public static async void UpscaleImage() @@ -63,7 +56,7 @@ public static async void UpscaleImage() Program.mainForm.SetPreviewProgress(3f, "Preprocessing..."); if (!CopyImage()) // Try to copy/move image to input folder, return if failed { - Cancel("I/O Error"); + Cancel("I/O Error"); return; } UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.PngFast, !Config.GetBool("alpha"), true, true); @@ -74,9 +67,9 @@ public static async void UpscaleImage() return; } await ESRGAN.UpscaleBasic(Paths.imgInPath, Paths.imgOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), ESRGAN.PreviewMode.None); - Postprocessing(); - AddModelSuffix(Paths.imgOutPath); - CopyImagesToOriginalLocation(); + await Postprocessing(); + await AddModelSuffix(Paths.imgOutPath); + await CopyImagesToOriginalLocation(); Program.mainForm.SetPreviewProgress(0, "Done."); } } @@ -109,7 +102,7 @@ static bool CopyImage () return true; } - static async void Postprocessing() + static async Task Postprocessing() { Program.mainForm.SetPreviewProgress(100f, "Postprocessing..."); await Program.PutTaskDelay(); @@ -128,7 +121,7 @@ static async void Postprocessing() UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.WeppyLow); } - static void AddModelSuffix(string path) + static async Task AddModelSuffix(string path) { DirectoryInfo d = new DirectoryInfo(path); FileInfo[] files = d.GetFiles("*", SearchOption.AllDirectories); @@ -137,10 +130,11 @@ static void AddModelSuffix(string path) string pathNoExt = Path.ChangeExtension(file.FullName, null); string ext = Path.GetExtension(file.FullName); File.Move(file.FullName, pathNoExt + "-" + Program.lastModelName + ext); + await Task.Delay(1); } } - static void CopyImagesToOriginalLocation() + static async Task CopyImagesToOriginalLocation() { if (overwrite.SelectedIndex == 1) { @@ -148,11 +142,15 @@ static void CopyImagesToOriginalLocation() IOUtils.ReplaceInFilenamesDir(Paths.imgOutPath, "-" + Program.lastModelName, ""); } IOUtils.Copy(Paths.imgOutPath, Path.GetDirectoryName(Program.lastFilename)); + await Task.Delay(1); } public static async void UpscalePreview(bool fullImage = false) { Program.mainForm.SetPreviewProgress(3f, "Preparing..."); + ResetCachedImages(); + IOUtils.DeleteContentsOfDir(Paths.previewPath); + IOUtils.DeleteContentsOfDir(Paths.previewOutPath); ESRGAN.PreviewMode prevMode = ESRGAN.PreviewMode.Cutout; if (fullImage) { @@ -253,5 +251,24 @@ public static void UpdatePreviewLabels(Label zoom, Label size, Label cutout) size.Text = "Size: " + previewImg.Image.Width + "x" + previewImg.Image.Height + " (Original: " + previewImg.Image.Width / currScale + "x" + previewImg.Image.Height / currScale + ")"; cutout.Text = "Cutout: " + cutoutW + "x" + cutoutH + " (Original: " + cutoutW / currScale + "x" + cutoutH / currScale + ")";// + "% - Unscaled Size: " + previewImg.Image.Size * currScale + "%"; } + + public static bool DroppedImageIsValid (string path) + { + try + { + Image img =IOUtils.GetImage(path); + if(img.Width > 4096 || img.Height > 4096) + { + MessageBox.Show("Image is too big for the preview!\nPlease use images with less than 4096 pixels on either side.", "Error"); + return false; + } + } + catch (Exception e) + { + MessageBox.Show("Failed to open image:\n\n" + e.Message, "Error"); + return false; + } + return true; + } } } diff --git a/Code/UI/UIHelpers.cs b/Code/UI/UIHelpers.cs index bcb10ca..c178d6d 100644 --- a/Code/UI/UIHelpers.cs +++ b/Code/UI/UIHelpers.cs @@ -20,10 +20,8 @@ public static void FillModelComboBox(ComboBox box, bool resetIndex = false) { EsrganData.ReloadModelList(); box.Items.Clear(); - Console.WriteLine("Clearing " + box.ToString() + ".Items"); foreach (string model in EsrganData.models) { - Console.WriteLine("Adding " + model + " to " + box.ToString() + ".Items"); box.Items.Add(model); } if (resetIndex || string.IsNullOrEmpty(box.Text))