From 4f9cbcb2a0005fd6742bf4e82324c8ac473d9047 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Sun, 20 Sep 2020 18:30:10 +0200 Subject: [PATCH] Finished model tree, fixed support for certain formats (TGA, DDS), improved alpha support --- Code/Cupscale/MainForm.Designer.cs | 72 +++++------- Code/Cupscale/MainForm.cs | 21 +++- Code/Cupscale/PreviewMerger.cs | 3 +- Code/Cupscale/Program.cs | 2 + Code/Cupscale/Upscale.cs | 19 ++-- Code/Forms/ModelSelectForm.Designer.cs | 37 ++++--- Code/Forms/ModelSelectForm.cs | 22 +++- Code/IO/IOUtils.cs | 1 + Code/IO/Paths.cs | 4 +- Code/ImageUtils/UpscaleProcessing.cs | 146 ++++++++++++++++++++----- Code/ModelData.cs | 15 ++- Code/OS/ESRGAN.cs | 80 ++++++++------ Code/UI/BatchUpscaleUI.cs | 3 +- Code/UI/MainUIHelper.cs | 22 ++-- 14 files changed, 287 insertions(+), 160 deletions(-) diff --git a/Code/Cupscale/MainForm.Designer.cs b/Code/Cupscale/MainForm.Designer.cs index 39e5d7c..cf98dd4 100644 --- a/Code/Cupscale/MainForm.Designer.cs +++ b/Code/Cupscale/MainForm.Designer.cs @@ -57,14 +57,13 @@ private void InitializeComponent() this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.panel5 = new System.Windows.Forms.Panel(); + this.model2TreeBtn = new System.Windows.Forms.Button(); this.model1TreeBtn = new System.Windows.Forms.Button(); this.interpConfigureBtn = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.chainRbtn = new System.Windows.Forms.RadioButton(); this.interpRbtn = new System.Windows.Forms.RadioButton(); this.singleModelRbtn = new System.Windows.Forms.RadioButton(); - this.modelCombox2 = new System.Windows.Forms.ModelCombox(); - this.modelCombox1 = new System.Windows.Forms.ModelCombox(); this.label16 = new System.Windows.Forms.Label(); this.label14 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); @@ -218,14 +217,13 @@ private void InitializeComponent() // panel5 // this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panel5.Controls.Add(this.model2TreeBtn); this.panel5.Controls.Add(this.model1TreeBtn); this.panel5.Controls.Add(this.interpConfigureBtn); this.panel5.Controls.Add(this.label4); this.panel5.Controls.Add(this.chainRbtn); this.panel5.Controls.Add(this.interpRbtn); this.panel5.Controls.Add(this.singleModelRbtn); - this.panel5.Controls.Add(this.modelCombox2); - this.panel5.Controls.Add(this.modelCombox1); this.panel5.Controls.Add(this.label16); this.panel5.Controls.Add(this.label14); this.panel5.Controls.Add(this.label3); @@ -235,6 +233,24 @@ private void InitializeComponent() this.panel5.Size = new System.Drawing.Size(338, 311); this.panel5.TabIndex = 1; // + // model2TreeBtn + // + this.model2TreeBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.model2TreeBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); + this.model2TreeBtn.Enabled = false; + this.model2TreeBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.model2TreeBtn.ForeColor = System.Drawing.Color.White; + this.model2TreeBtn.Location = new System.Drawing.Point(6, 278); + this.model2TreeBtn.Margin = new System.Windows.Forms.Padding(8); + this.model2TreeBtn.Name = "model2TreeBtn"; + this.model2TreeBtn.Size = new System.Drawing.Size(322, 23); + this.model2TreeBtn.TabIndex = 18; + this.model2TreeBtn.Text = "None Selected. Click To Change."; + this.model2TreeBtn.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + this.model2TreeBtn.UseVisualStyleBackColor = false; + this.model2TreeBtn.Click += new System.EventHandler(this.model2TreeBtn_Click); + // // model1TreeBtn // this.model1TreeBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) @@ -242,12 +258,13 @@ private void InitializeComponent() this.model1TreeBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); this.model1TreeBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.model1TreeBtn.ForeColor = System.Drawing.Color.White; - this.model1TreeBtn.Location = new System.Drawing.Point(198, 198); + this.model1TreeBtn.Location = new System.Drawing.Point(6, 228); + this.model1TreeBtn.Margin = new System.Windows.Forms.Padding(8); this.model1TreeBtn.Name = "model1TreeBtn"; - this.model1TreeBtn.Size = new System.Drawing.Size(129, 23); + this.model1TreeBtn.Size = new System.Drawing.Size(322, 23); this.model1TreeBtn.TabIndex = 17; - this.model1TreeBtn.Text = "Tree"; - this.model1TreeBtn.TextAlign = System.Drawing.ContentAlignment.TopCenter; + this.model1TreeBtn.Text = "None Selected. Click To Change."; + this.model1TreeBtn.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.model1TreeBtn.UseVisualStyleBackColor = false; this.model1TreeBtn.Click += new System.EventHandler(this.model1TreeBtn_Click); // @@ -324,37 +341,6 @@ private void InitializeComponent() this.singleModelRbtn.UseVisualStyleBackColor = true; this.singleModelRbtn.CheckedChanged += new System.EventHandler(this.singleModelRbtn_CheckedChanged); // - // modelCombox2 - // - this.modelCombox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.modelCombox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); - this.modelCombox2.Enabled = false; - this.modelCombox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.modelCombox2.ForeColor = System.Drawing.Color.White; - this.modelCombox2.FormattingEnabled = true; - this.modelCombox2.Location = new System.Drawing.Point(8, 280); - this.modelCombox2.Margin = new System.Windows.Forms.Padding(8); - this.modelCombox2.Name = "modelCombox2"; - this.modelCombox2.Size = new System.Drawing.Size(320, 21); - this.modelCombox2.TabIndex = 11; - this.modelCombox2.Text = "Open the dropdown to load available models."; - // - // modelCombox1 - // - this.modelCombox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.modelCombox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); - this.modelCombox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.modelCombox1.ForeColor = System.Drawing.Color.White; - this.modelCombox1.FormattingEnabled = true; - this.modelCombox1.Location = new System.Drawing.Point(8, 229); - this.modelCombox1.Margin = new System.Windows.Forms.Padding(8); - this.modelCombox1.Name = "modelCombox1"; - this.modelCombox1.Size = new System.Drawing.Size(320, 21); - this.modelCombox1.TabIndex = 10; - this.modelCombox1.Text = "Open the dropdown to load available models."; - // // label16 // this.label16.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) @@ -373,7 +359,7 @@ private void InitializeComponent() | System.Windows.Forms.AnchorStyles.Right))); this.label14.AutoSize = true; this.label14.ForeColor = System.Drawing.Color.White; - this.label14.Location = new System.Drawing.Point(6, 208); + this.label14.Location = new System.Drawing.Point(6, 207); this.label14.Name = "label14"; this.label14.Size = new System.Drawing.Size(48, 13); this.label14.TabIndex = 8; @@ -508,7 +494,7 @@ private void InitializeComponent() this.previewGroupbox.Size = new System.Drawing.Size(330, 93); this.previewGroupbox.TabIndex = 5; this.previewGroupbox.TabStop = false; - this.previewGroupbox.Text = "Copy Comparison To Clipboard"; + this.previewGroupbox.Text = "Save Comparison"; // // copyCompToClipboardBtn // @@ -1044,9 +1030,6 @@ private void InitializeComponent() this.ResumeLayout(false); } - - private ModelCombox modelCombox2; - private ModelCombox modelCombox1; private Label label16; private Label label14; private RadioButton singleModelRbtn; @@ -1080,5 +1063,6 @@ private void InitializeComponent() private TableLayoutPanel tableLayoutPanel7; private HTAlt.WinForms.HTButton settingsBtn; private Button model1TreeBtn; + private Button model2TreeBtn; } } diff --git a/Code/Cupscale/MainForm.cs b/Code/Cupscale/MainForm.cs index fc5a2e7..7d7a835 100644 --- a/Code/Cupscale/MainForm.cs +++ b/Code/Cupscale/MainForm.cs @@ -26,7 +26,7 @@ public MainForm() EsrganData.ReloadModelList(); CheckForIllegalCrossThreadCalls = false; InitializeComponent(); - MainUIHelper.Init(previewImg, modelCombox1, modelCombox2, prevOutputFormatCombox, prevOverwriteCombox); + MainUIHelper.Init(previewImg, model1TreeBtn, model2TreeBtn, prevOutputFormatCombox, prevOverwriteCombox); BatchUpscaleUI.Init(batchOutDir, batchFileList); Program.mainForm = this; WindowState = FormWindowState.Maximized; @@ -148,7 +148,7 @@ private void chainRbtn_CheckedChanged(object sender, EventArgs e) public void UpdateModelMode() { - modelCombox2.Enabled = (interpRbtn.Checked || chainRbtn.Checked); + model2TreeBtn.Enabled = (interpRbtn.Checked || chainRbtn.Checked); interpConfigureBtn.Visible = interpRbtn.Checked; if (singleModelRbtn.Checked) MainUIHelper.currentMode = MainUIHelper.Mode.Single; if (interpRbtn.Checked) MainUIHelper.currentMode = MainUIHelper.Mode.Interp; @@ -157,12 +157,14 @@ public void UpdateModelMode() private void interpConfigureBtn_Click(object sender, EventArgs e) { + /* if (modelCombox1.SelectedIndex == -1 || modelCombox2.SelectedIndex == -1) { MessageBox.Show("Please select two models for interpolation.", "Message"); return; } - InterpForm interpForm = new InterpForm(modelCombox1.Text.Trim(), modelCombox2.Text.Trim()); + */ + InterpForm interpForm = new InterpForm(model1TreeBtn.Text.Trim(), model1TreeBtn.Text.Trim()); } private void batchTab_DragEnter(object sender, DragEventArgs e) @@ -199,6 +201,7 @@ async Task DragNDrop (string [] array) DialogForm loadingDialogForm = new DialogForm("Loading " + Path.GetFileName(path) +"..."); await Task.Delay(1); MainUIHelper.ResetCachedImages(); + Logger.Log("3"); if (!MainUIHelper.DroppedImageIsValid(path)) { SetProgress(0f, "Ready."); @@ -207,7 +210,10 @@ async Task DragNDrop (string [] array) return; } File.Copy(path, Paths.tempImgPath, true); - await Upscale.Preprocessing(Paths.imgInPath); + //await Upscale.Preprocessing(Paths.tempImgPath.GetParentDir()); + bool fillAlpha = !bool.Parse(Config.Get("alpha")); + await UpscaleProcessing.ConvertImageTo(path, Paths.tempImgPath, UpscaleProcessing.Format.PngFast, fillAlpha, false, false); + Logger.Log("Done Preprocessing"); previewImg.Image = IOUtils.GetImage(Paths.tempImgPath); Program.lastFilename = path; MainUIHelper.currentScale = 1; @@ -248,7 +254,12 @@ private void copyCompToClipboardBtn_Click(object sender, EventArgs e) private void model1TreeBtn_Click(object sender, EventArgs e) { - new ModelSelectForm(); + ModelSelectForm treeForm = new ModelSelectForm(model1TreeBtn, 1); } + + private void model2TreeBtn_Click(object sender, EventArgs e) + { + ModelSelectForm treeForm = new ModelSelectForm(model2TreeBtn, 2); + } } } diff --git a/Code/Cupscale/PreviewMerger.cs b/Code/Cupscale/PreviewMerger.cs index 3242808..a6a8fb6 100644 --- a/Code/Cupscale/PreviewMerger.cs +++ b/Code/Cupscale/PreviewMerger.cs @@ -36,10 +36,9 @@ public static void Merge() offsetX *= num; offsetY *= num; Logger.Log("Merging " + outputCutoutPath + " onto " + Program.lastFilename + " using offset " + offsetX + "x" + offsetY); - MagickImage sourceImg = new MagickImage(Program.lastFilename); + MagickImage sourceImg = new MagickImage(Paths.tempImgPath); MagickImage cutout = new MagickImage(outputCutoutPath); sourceImg.FilterType = Program.currentFilter; - Logger.Log("Scaling preview-input-scaled.png with filter " + sourceImg.FilterType + " which should match " + Program.currentFilter); sourceImg.Resize(new Percentage(num * 100)); string scaledPrevPath = Path.Combine(Paths.previewOutPath, "preview-input-scaled.png"); sourceImg.Format = MagickFormat.Png; diff --git a/Code/Cupscale/Program.cs b/Code/Cupscale/Program.cs index 7caa642..cb732d9 100644 --- a/Code/Cupscale/Program.cs +++ b/Code/Cupscale/Program.cs @@ -15,6 +15,8 @@ internal static class Program public static string lastFilename; public static string lastDirPath; public static string lastModelName; + public static string currentModel1; + public static string currentModel2; public static FilterType currentFilter = FilterType.Point; public static List
currentTemporaryForms = new List(); // Temp forms that get closed when something gets cancelled diff --git a/Code/Cupscale/Upscale.cs b/Code/Cupscale/Upscale.cs index 3ee7b15..9adf5b9 100644 --- a/Code/Cupscale/Upscale.cs +++ b/Code/Cupscale/Upscale.cs @@ -47,21 +47,21 @@ public static ModelData GetModelData() if (currentMode == Mode.Single) { - string mdl1 = GetMdl(model1); + string mdl1 = Program.currentModel1; if (string.IsNullOrWhiteSpace(mdl1)) return mdl; mdl = new ModelData(mdl1, null, ModelData.ModelMode.Single); } if (currentMode == Mode.Interp) { - string mdl1 = GetMdl(model1); - string mdl2 = GetMdl(model2); + string mdl1 = Program.currentModel1; + string mdl2 = Program.currentModel2; if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2)) return mdl; mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Interp, interpValue); } if (currentMode == Mode.Chain) { - string mdl1 = GetMdl(model1); - string mdl2 = GetMdl(model2); + string mdl1 = Program.currentModel1; + string mdl2 = Program.currentModel2; if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2)) return mdl; mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Chain); } @@ -69,8 +69,9 @@ public static ModelData GetModelData() return mdl; } - public static async Task Preprocessing (string path) + public static async Task Preprocessing (string path, bool appendExt = false) { + Logger.Log("Preprocessing: " + path); bool fillAlpha = !bool.Parse(Config.Get("alpha")); await UpscaleProcessing.ConvertImages(path, UpscaleProcessing.Format.PngFast, fillAlpha); } @@ -102,9 +103,10 @@ public static async Task FilenamePostprocessing () await Program.PutTaskDelay(); } - public static string GetMdl(ComboBox box) + /* + public static string GetMdl(Button btn) { - string mdl = box.Text.Trim(); + string mdl = btn.Text.Trim(); EsrganData.ReloadModelList(); if (!EsrganData.models.Contains(mdl)) { @@ -115,5 +117,6 @@ public static string GetMdl(ComboBox box) } return mdl; } + */ } } diff --git a/Code/Forms/ModelSelectForm.Designer.cs b/Code/Forms/ModelSelectForm.Designer.cs index 13e8506..bfd4d8f 100644 --- a/Code/Forms/ModelSelectForm.Designer.cs +++ b/Code/Forms/ModelSelectForm.Designer.cs @@ -31,8 +31,8 @@ private void InitializeComponent() this.modelTree = new System.Windows.Forms.TreeView(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.panel1 = new System.Windows.Forms.Panel(); - this.confirmBtn = new HTAlt.WinForms.HTButton(); this.cancelBtn = new HTAlt.WinForms.HTButton(); + this.confirmBtn = new HTAlt.WinForms.HTButton(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.SuspendLayout(); @@ -46,6 +46,7 @@ private void InitializeComponent() this.modelTree.Name = "modelTree"; this.modelTree.Size = new System.Drawing.Size(714, 664); this.modelTree.TabIndex = 0; + this.modelTree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.modelTree_AfterSelect); // // tableLayoutPanel1 // @@ -73,23 +74,6 @@ private void InitializeComponent() this.panel1.Size = new System.Drawing.Size(714, 44); this.panel1.TabIndex = 1; // - // confirmBtn - // - this.confirmBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.confirmBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); - this.confirmBtn.FlatAppearance.BorderSize = 0; - this.confirmBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.confirmBtn.ForeColor = System.Drawing.Color.White; - this.confirmBtn.Location = new System.Drawing.Point(186, 6); - this.confirmBtn.Margin = new System.Windows.Forms.Padding(180, 6, 6, 6); - this.confirmBtn.Name = "confirmBtn"; - this.confirmBtn.Size = new System.Drawing.Size(162, 32); - this.confirmBtn.TabIndex = 12; - this.confirmBtn.Text = "Select Model"; - this.confirmBtn.UseVisualStyleBackColor = false; - this.confirmBtn.Click += new System.EventHandler(this.confirmBtn_Click); - // // cancelBtn // this.cancelBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) @@ -107,6 +91,23 @@ private void InitializeComponent() this.cancelBtn.UseVisualStyleBackColor = false; this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click); // + // confirmBtn + // + this.confirmBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.confirmBtn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); + this.confirmBtn.FlatAppearance.BorderSize = 0; + this.confirmBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.confirmBtn.ForeColor = System.Drawing.Color.White; + this.confirmBtn.Location = new System.Drawing.Point(186, 6); + this.confirmBtn.Margin = new System.Windows.Forms.Padding(180, 6, 6, 6); + this.confirmBtn.Name = "confirmBtn"; + this.confirmBtn.Size = new System.Drawing.Size(162, 32); + this.confirmBtn.TabIndex = 12; + this.confirmBtn.Text = "Select Model"; + this.confirmBtn.UseVisualStyleBackColor = false; + this.confirmBtn.Click += new System.EventHandler(this.confirmBtn_Click); + // // ModelSelectForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); diff --git a/Code/Forms/ModelSelectForm.cs b/Code/Forms/ModelSelectForm.cs index 340e6d2..4ecb949 100644 --- a/Code/Forms/ModelSelectForm.cs +++ b/Code/Forms/ModelSelectForm.cs @@ -15,12 +15,17 @@ namespace Cupscale.Forms public partial class ModelSelectForm : Form { public string selectedModel; + public int modelNo; + public Button modelBtn; - public ModelSelectForm() + public ModelSelectForm(Button modelButton, int modelNumber) { InitializeComponent(); Show(); + TopMost = true; CenterToScreen(); + modelBtn = modelButton; + modelNo = modelNumber; } private void ModelSelectForm_Load(object sender, EventArgs e) @@ -28,6 +33,7 @@ private void ModelSelectForm_Load(object sender, EventArgs e) DirectoryInfo modelsDir = new DirectoryInfo(Config.Get("modelPath")); BuildTree(modelsDir, modelTree.Nodes); modelTree.ExpandAll(); + modelTree.SelectedNode = modelTree.Nodes[0]; } private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe) @@ -47,12 +53,24 @@ private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe) private void confirmBtn_Click(object sender, EventArgs e) { - selectedModel = modelTree.SelectedNode.Text; + selectedModel = modelTree.SelectedNode.Name; + modelBtn.Text = Path.GetFileNameWithoutExtension(selectedModel); + Logger.Log("Selected model " + modelBtn.Text + " - Full path: " + selectedModel); + if (modelNo == 1) // idk if this could be less hardcoded? + Program.currentModel1 = selectedModel; + if (modelNo == 2) + Program.currentModel2 = selectedModel; + Close(); } private void cancelBtn_Click(object sender, EventArgs e) { Close(); } + + private void modelTree_AfterSelect(object sender, TreeViewEventArgs e) + { + confirmBtn.Enabled = (Path.GetExtension(modelTree.SelectedNode.Name) == ".pth"); + } } } diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 2c81f40..33e6496 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -121,6 +121,7 @@ private static void CopyWork(DirectoryInfo source, DirectoryInfo target, bool mo public static void DeleteContentsOfDir(string path) { + Logger.Log("Clearing " + path); DirectoryInfo directoryInfo = new DirectoryInfo(path); FileInfo[] files = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in files) diff --git a/Code/IO/Paths.cs b/Code/IO/Paths.cs index 9804008..8c53065 100644 --- a/Code/IO/Paths.cs +++ b/Code/IO/Paths.cs @@ -1,3 +1,4 @@ +using Cupscale.UI; using System.IO; namespace Cupscale.IO @@ -19,12 +20,13 @@ public static void Init() previewOutPath = Path.Combine(IOUtils.GetAppDataDir(), "preview-out"); imgInPath = Path.Combine(IOUtils.GetAppDataDir(), "img-in"); imgOutPath = Path.Combine(IOUtils.GetAppDataDir(), "img-out"); - tempImgPath = Path.Combine(IOUtils.GetAppDataDir(), "temp.png"); + tempImgPath = Path.Combine(IOUtils.GetAppDataDir(), "loaded-img", "temp.png"); progressLogfile = Path.Combine(esrganPath, "prog"); Directory.CreateDirectory(previewPath); Directory.CreateDirectory(previewOutPath); Directory.CreateDirectory(imgInPath); Directory.CreateDirectory(imgOutPath); + Directory.CreateDirectory(tempImgPath.GetParentDir()); } } } diff --git a/Code/ImageUtils/UpscaleProcessing.cs b/Code/ImageUtils/UpscaleProcessing.cs index 3e330d7..9e9915d 100644 --- a/Code/ImageUtils/UpscaleProcessing.cs +++ b/Code/ImageUtils/UpscaleProcessing.cs @@ -98,72 +98,76 @@ public static async Task ConvertImages(string path, Format format, bool removeAl { Logger.Log("Converting " + file.Name + " to " + format.ToString() + ", appendExtension = " + appendExtension); await ConvertImage(file.FullName, format, removeAlpha, appendExtension, delSource); - await Task.Delay(1); + Logger.Log("Done converting this image"); } + Logger.Log("Done converting images"); } public static async Task ConvertImage(string path, Format format, bool fillAlpha, bool appendExtension, bool deleteSource = true) { + Logger.Log("ConvertImage: Loading MagickImage from " + path); MagickImage img = new MagickImage(path); Logger.Log("Converting: " + img.ToString() + " - Target Format: " + format.ToString() + " - DeleteSource: " + deleteSource); - string text = "png"; + string ext = "png"; if (format == Format.PngOpti) { - img.Format = (MagickFormat)171; - img.Quality = (80); + img.Format = MagickFormat.Png; + img.Quality = 70; } if (format == Format.PngFast) { - img.Format = (MagickFormat)171; - img.Quality = (20); + img.Format = MagickFormat.Png; + img.Quality = 20; } if (format == Format.JpegHigh) { - img.Format = (MagickFormat)105; - img.Quality = (95); - text = "jpg"; + img.Format = MagickFormat.Jpeg; + img.Quality = 95; + ext = "jpg"; } if (format == Format.JpegMed) { - img.Format = (MagickFormat)105; - img.Quality = (80); - text = "jpg"; + img.Format = MagickFormat.Jpeg; + img.Quality = 80; + ext = "jpg"; } if (format == Format.WeppyHigh) { - img.Format = (MagickFormat)239; - img.Quality = (92); - text = "webp"; + img.Format = MagickFormat.WebP; + img.Quality = 92; + ext = "webp"; } if (format == Format.WeppyLow) { - img.Format = (MagickFormat)239; - img.Quality = (80); - text = "webp"; + img.Format = MagickFormat.WebP; + img.Quality = 80; + ext = "webp"; } if (format == Format.BMP) { - img.Format = (MagickFormat)17; - text = "bmp"; + img.Format = MagickFormat.Bmp; + ext = "bmp"; } if (format == Format.TGA) { - img.Format = (MagickFormat)218; - text = "tga"; + img.Format = MagickFormat.Tga; + ext = "tga"; } if (format == Format.DDS) { - img.Format = (MagickFormat)43; - text = "dds"; + img.Format = MagickFormat.Dds; + ext = "dds"; } if (fillAlpha) { - img.ColorAlpha(new MagickColor("#" + Config.Get("alphaBgColor"))); + MagickImage colorImg = new MagickImage(new MagickColor("#" + Config.Get("alphaBgColor")), img.Width, img.Height); + colorImg.Composite(img, Gravity.Center, CompositeOperator.Over); + // img.ColorAlpha(new MagickColor("#" + Config.Get("alphaBgColor"))); // Might not work correctly for DDS n stuff? } if (appendExtension) { string extension = Path.GetExtension(path); - string outPath = Path.ChangeExtension(path, null) + extension + "." + text; + string outPath = Path.ChangeExtension(path, null) + extension + "." + ext; Logger.Log("Appending old extension; writing image to " + outPath); img.Write(outPath); if (deleteSource && outPath != path) @@ -174,9 +178,9 @@ public static async Task ConvertImage(string path, Format format, bool fillAlpha } else { - img.Write(Path.ChangeExtension(path, text)); - Logger.Log("Writing image to " + Path.ChangeExtension(path, text)); - if (deleteSource && !(Path.ChangeExtension(path, text) == path)) + img.Write(Path.ChangeExtension(path, ext)); + Logger.Log("Writing image to " + Path.ChangeExtension(path, ext)); + if (deleteSource && !(Path.ChangeExtension(path, ext) == path)) { Logger.Log("Deleting source file: " + path); File.Delete(path); @@ -184,5 +188,89 @@ public static async Task ConvertImage(string path, Format format, bool fillAlpha } await Task.Delay(1); } + + public static async Task ConvertImageTo(string inPath, string outPath, Format format, bool fillAlpha, bool appendExtension, bool deleteSource = true) + { + Logger.Log("ConvertImage: Loading MagickImage from " + inPath); + MagickImage img = new MagickImage(inPath); + Logger.Log("Converting: " + img.ToString() + " - Target Format: " + format.ToString() + " - DeleteSource: " + deleteSource); + string ext = "png"; + if (format == Format.PngOpti) + { + img.Format = MagickFormat.Png; + img.Quality = 70; + } + if (format == Format.PngFast) + { + img.Format = MagickFormat.Png; + img.Quality = 20; + } + if (format == Format.JpegHigh) + { + img.Format = MagickFormat.Jpeg; + img.Quality = 95; + ext = "jpg"; + } + if (format == Format.JpegMed) + { + img.Format = MagickFormat.Jpeg; + img.Quality = 80; + ext = "jpg"; + } + if (format == Format.WeppyHigh) + { + img.Format = MagickFormat.WebP; + img.Quality = 92; + ext = "webp"; + } + if (format == Format.WeppyLow) + { + img.Format = MagickFormat.WebP; + img.Quality = 80; + ext = "webp"; + } + if (format == Format.BMP) + { + img.Format = MagickFormat.Bmp; + ext = "bmp"; + } + if (format == Format.TGA) + { + img.Format = MagickFormat.Tga; + ext = "tga"; + } + if (format == Format.DDS) + { + img.Format = MagickFormat.Dds; + ext = "dds"; + } + if (fillAlpha) + { + img.ColorAlpha(new MagickColor("#" + Config.Get("alphaBgColor"))); + } + if (appendExtension) + { + string extension = Path.GetExtension(inPath); + //string outPath = Path.ChangeExtension(inPath, null) + extension + "." + ext; + Logger.Log("Appending old extension; writing image to " + outPath); + img.Write(outPath); + if (deleteSource && outPath != inPath) + { + Logger.Log("Deleting source file: " + inPath); + File.Delete(inPath); + } + } + else + { + img.Write(Path.ChangeExtension(outPath, ext)); + Logger.Log("Writing image to " + Path.ChangeExtension(outPath, ext)); + if (deleteSource && !(Path.ChangeExtension(outPath, ext) == inPath)) + { + Logger.Log("Deleting source file: " + inPath); + File.Delete(inPath); + } + } + await Task.Delay(1); + } } } diff --git a/Code/ModelData.cs b/Code/ModelData.cs index b8f77ee..308dade 100644 --- a/Code/ModelData.cs +++ b/Code/ModelData.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,16 +9,20 @@ namespace Cupscale { struct ModelData { - public string model1; - public string model2; + public string model1Name; + public string model2Name; + public string model1Path; + public string model2Path; public enum ModelMode { Single, Interp, Chain } public ModelMode mode; public int interp; - public ModelData(string model1Name, string model2Name, ModelMode modelMode, int interpolation = 0) + public ModelData(string model1, string model2, ModelMode modelMode, int interpolation = 0) { - model1 = model1Name; - model2 = model2Name; + model1Name = Path.GetFileNameWithoutExtension(model1); + model2Name = Path.GetFileNameWithoutExtension(model2); + model1Path = model1; + model2Path = model2; mode = modelMode; interp = interpolation; } diff --git a/Code/OS/ESRGAN.cs b/Code/OS/ESRGAN.cs index f8803ee..b02aa78 100644 --- a/Code/OS/ESRGAN.cs +++ b/Code/OS/ESRGAN.cs @@ -1,9 +1,11 @@ using Cupscale.ImageUtils; using Cupscale.IO; using Cupscale.UI; +using System; using System.Diagnostics; using System.Drawing; using System.IO; +using System.Linq.Expressions; using System.Threading.Tasks; using System.Windows.Forms; @@ -18,54 +20,62 @@ public enum PreviewMode { None, Cutout, FullImage } public static async Task UpscaleBasic(string inpath, string outpath, ModelData mdl, string tilesize, bool alpha, PreviewMode mode, bool showTileProgress = true) { - string modelArg = GetModelArg(mdl); - Logger.Log("Model Arg: " + modelArg); - Program.mainForm.SetProgress(5f, "Starting ESRGAN..."); - await Run(inpath, outpath, modelArg, tilesize, alpha, showTileProgress); - File.Delete(Paths.progressLogfile); - if (mode == PreviewMode.Cutout) - { - Program.mainForm.SetProgress(100f, "Merging into preview..."); - await Program.PutTaskDelay(); - PreviewMerger.Merge(); - } - if (mode == PreviewMode.FullImage) - { - Program.mainForm.SetProgress(100f, "Merging into preview..."); - await Program.PutTaskDelay(); - Image outImg = IOUtils.GetImage(Path.Combine(Paths.previewOutPath, "preview.png")); - Image inputImg = IOUtils.GetImage(Program.lastFilename); - MainUIHelper.previewImg.Image = outImg; - MainUIHelper.currentOriginal = inputImg; - MainUIHelper.currentOutput = outImg; - MainUIHelper.currentScale = ImgUtils.GetScale(inputImg, outImg); - MainUIHelper.previewImg.ZoomToFit(); - Program.mainForm.SetProgress(0f, "Done."); + try + { + string modelArg = GetModelArg(mdl); + Logger.Log("Model Arg: " + modelArg); + Program.mainForm.SetProgress(5f, "Starting ESRGAN..."); + await Run(inpath, outpath, modelArg, tilesize, alpha, showTileProgress); + File.Delete(Paths.progressLogfile); + if (mode == PreviewMode.Cutout) + { + Program.mainForm.SetProgress(100f, "Merging into preview..."); + await Program.PutTaskDelay(); + PreviewMerger.Merge(); + } + if (mode == PreviewMode.FullImage) + { + Program.mainForm.SetProgress(100f, "Merging into preview..."); + await Program.PutTaskDelay(); + Image outImg = IOUtils.GetImage(Path.Combine(Paths.previewOutPath, "preview.png")); + Image inputImg = IOUtils.GetImage(Paths.tempImgPath); + MainUIHelper.previewImg.Image = outImg; + MainUIHelper.currentOriginal = inputImg; + MainUIHelper.currentOutput = outImg; + MainUIHelper.currentScale = ImgUtils.GetScale(inputImg, outImg); + MainUIHelper.previewImg.ZoomToFit(); + Program.mainForm.SetProgress(0f, "Done."); + } } + catch(Exception e) + { + MessageBox.Show("An error occured during upscaling: \n\n" + e.Message, "Error"); + } + } - public static string GetModelArg (ModelData modelData) + public static string GetModelArg (ModelData mdl) { - string mdl1 = modelData.model1; - string mdl2 = modelData.model2; - ModelData.ModelMode mdlMode = modelData.mode; + string mdl1 = mdl.model1Path; + string mdl2 = mdl.model2Path; + ModelData.ModelMode mdlMode = mdl.mode; string mdlPath = Config.Get("modelPath").Replace("/", "\\").TrimEnd('\\'); if(mdlMode == ModelData.ModelMode.Single) { - Program.lastModelName = mdl1; - return " --model \"" + mdlPath + "/" + mdl1 + ".pth\""; + Program.lastModelName = mdl.model1Name; + return " --model \"" + mdl1 + "\""; } if (mdlMode == ModelData.ModelMode.Interp) { - int interpLeft = 100 - modelData.interp; - int interpRight = modelData.interp; - Program.lastModelName = mdl1 + ":" + interpLeft + ":" + mdl2 + ":" + interpRight; - return " --model \"" + mdlPath + "/" + mdl1 + ".pth\";" + interpLeft + ";" + "\"" + mdlPath + "/" + mdl2 + ".pth\";" + interpRight; + int interpLeft = 100 - mdl.interp; + int interpRight = mdl.interp; + Program.lastModelName = mdl.model1Name + ":" + interpLeft + ":" + mdl.model2Name + ":" + interpRight; + return " --model \"" + mdl1 + "\";" + interpLeft + ";" + "\"" + mdl2 + "\";" + interpRight; } if (mdlMode == ModelData.ModelMode.Chain) { - Program.lastModelName = mdl1 + ">>" + mdl2; - return " --prefilter \"" + mdlPath + "/" + mdl1 + ".pth\" --model \"" + mdlPath + "/" + mdl2 + ".pth\""; + Program.lastModelName = mdl.model1Name + ">>" + mdl.model2Name; + return " --prefilter \"" + mdl1 + "\" --model \"" + mdl2 + "\""; } return null; } diff --git a/Code/UI/BatchUpscaleUI.cs b/Code/UI/BatchUpscaleUI.cs index 700c96f..6f1d339 100644 --- a/Code/UI/BatchUpscaleUI.cs +++ b/Code/UI/BatchUpscaleUI.cs @@ -35,6 +35,7 @@ public static void LoadDir (string path) static void FillFileList () { + fileList.Clear(); string[] files = Directory.GetFiles(currentInDir, "*", SearchOption.AllDirectories).Where(file => IOUtils.compatibleExtensions.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))).ToArray(); string text = ""; @@ -79,7 +80,7 @@ public static async void GetProgress (string outdir, int target) int count = IOUtils.GetAmountOfFiles(outdir, true); float percentage = (float)count / target; percentage = percentage * 100f; - if (percentage > 100f) + if (percentage >= 100f) break; Program.mainForm.SetProgress((int)Math.Round(percentage), "Upscaled " + count + "/" + target + " images"); } diff --git a/Code/UI/MainUIHelper.cs b/Code/UI/MainUIHelper.cs index c1b7433..e76846a 100644 --- a/Code/UI/MainUIHelper.cs +++ b/Code/UI/MainUIHelper.cs @@ -10,6 +10,7 @@ using Cupscale.Main; using Cupscale.OS; using Cyotek.Windows.Forms; +using HTAlt.WinForms; using ImageMagick; using Paths = Cupscale.IO.Paths; @@ -21,8 +22,8 @@ public enum Mode { Single, Interp, Chain, Advanced } public static Mode currentMode; public static ImageBox previewImg; - public static ComboBox model1; - public static ComboBox model2; + public static Button model1; + public static Button model2; public static int interpValue; @@ -34,11 +35,11 @@ public enum Mode { Single, Interp, Chain, Advanced } public static int currentScale = 1; - public static void Init(ImageBox imgBox, ComboBox model1Box, ComboBox model2Box, ComboBox formatBox, ComboBox overwriteBox) + public static void Init(ImageBox imgBox, Button model1Btn, Button model2Btn, ComboBox formatBox, ComboBox overwriteBox) { previewImg = imgBox; - model1 = model1Box; - model2 = model2Box; + model1 = model1Btn; + model2 = model2Btn; outputFormat = formatBox; overwrite = overwriteBox; } @@ -98,6 +99,7 @@ public static async void UpscalePreview(bool fullImage = false) Program.mainForm.SetBusy(true); Program.mainForm.SetProgress(3f, "Preparing..."); ResetCachedImages(); + IOUtils.DeleteContentsOfDir(Paths.imgInPath); IOUtils.DeleteContentsOfDir(Paths.previewPath); IOUtils.DeleteContentsOfDir(Paths.previewOutPath); ESRGAN.PreviewMode prevMode = ESRGAN.PreviewMode.Cutout; @@ -113,23 +115,23 @@ public static async void UpscalePreview(bool fullImage = false) await Upscale.Preprocessing(Paths.previewPath); if (currentMode == Mode.Single) { - string mdl1 = Upscale.GetMdl(model1); + string mdl1 = Program.currentModel1; if (string.IsNullOrWhiteSpace(mdl1)) return; ModelData mdl = new ModelData(mdl1, null, ModelData.ModelMode.Single); await ESRGAN.UpscaleBasic(Paths.previewPath, Paths.previewOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), prevMode); } if (currentMode == Mode.Interp) { - string mdl1 = Upscale.GetMdl(model1); - string mdl2 = Upscale.GetMdl(model2); + string mdl1 = Program.currentModel1; + string mdl2 = Program.currentModel2; if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2)) return; ModelData mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Interp, interpValue); await ESRGAN.UpscaleBasic(Paths.previewPath, Paths.previewOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), prevMode); } if (currentMode == Mode.Chain) { - string mdl1 = Upscale.GetMdl(model1); - string mdl2 = Upscale.GetMdl(model2); + string mdl1 = Program.currentModel1; + string mdl2 = Program.currentModel2; if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2)) return; ModelData mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Chain); await ESRGAN.UpscaleBasic(Paths.previewPath, Paths.previewOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), prevMode);