Skip to content

Commit

Permalink
Fixed custom format output, fixed preview bug
Browse files Browse the repository at this point in the history
  • Loading branch information
N00MKRAD committed Sep 12, 2020
1 parent f02dc87 commit af5a370
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
30 changes: 19 additions & 11 deletions Code/Cupscale/MainForm.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Cyotek.Windows.Forms.ImageBoxZoomEventArgs>(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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 ()
Expand All @@ -1229,5 +1232,10 @@ private void refreshPrevFullBtn_Click(object sender, EventArgs e)
{
PreviewTabHelper.UpscalePreview(true);
}

private void previewImg_Click(object sender, EventArgs e)
{

}
}
}
9 changes: 5 additions & 4 deletions Code/IO/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
47 changes: 32 additions & 15 deletions Code/UI/PreviewTabHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -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.");
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -137,22 +130,27 @@ 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)
{
Logger.Log("Overwrite mode - removing suffix from filenames");
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)
{
Expand Down Expand Up @@ -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;
}
}
}
2 changes: 0 additions & 2 deletions Code/UI/UIHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit af5a370

Please sign in to comment.