Skip to content

Commit

Permalink
Build 6 stuff
Browse files Browse the repository at this point in the history
- Added batch processing
- Finished Alpha support including previewing
- Added support for ALL image formats that Magick.NET supports*
- UI design improvements
- Fixed various bugs regarding output formats
- Merged preview now uses the active filtering mode

*For previewing. For upscaling, the following formats are currently supported: PNG, JPEG, WEBP, BMP, TGA, DDS.
  • Loading branch information
N00MKRAD committed Sep 19, 2020
1 parent 550cfc2 commit 446f475
Show file tree
Hide file tree
Showing 18 changed files with 915 additions and 371 deletions.
2 changes: 1 addition & 1 deletion Code/Cupscale.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<ApplicationIcon>CupscaleLogo1.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BetterTabControl" Version="1.0.19295.1523" />
<PackageReference Include="Costura.Fody" Version="4.1.0" />
<PackageReference Include="CyotekImageBox" Version="1.2.1" />
<PackageReference Include="CyotekTabList" Version="2.0.0" />
Expand All @@ -22,6 +21,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="HTAlt.WinForms" Version="0.1.5.1" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="7.21.1" />
<PackageReference Include="TabControl" Version="2.1.2" />
</ItemGroup>
Expand Down
511 changes: 365 additions & 146 deletions Code/Cupscale/MainForm.Designer.cs

Large diffs are not rendered by default.

152 changes: 111 additions & 41 deletions Code/Cupscale/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using Cupscale.Properties;
using System.Drawing.Drawing2D;
using Cupscale.Forms;
using Cupscale.Main;
using System.Threading.Tasks;
using System.IO;
using Cupscale.IO;

namespace Cupscale
{
Expand All @@ -22,7 +26,8 @@ public MainForm()
EsrganData.ReloadModelList();
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
PreviewTabHelper.Init(previewImg, modelCombox1, modelCombox2, prevOutputFormatCombox, prevOverwriteCombox);
MainUIHelper.Init(previewImg, modelCombox1, modelCombox2, prevOutputFormatCombox, prevOverwriteCombox);
BatchUpscaleUI.Init(batchOutDir, batchFileList);
Program.mainForm = this;
WindowState = FormWindowState.Maximized;
}
Expand All @@ -34,21 +39,29 @@ private void MainForm_Load(object sender, EventArgs e)
UIHelpers.InitCombox(prevClipboardTypeCombox, 0);
}

public void SetPreviewProgress(float prog, string statusText = "")
public void SetProgress(float prog, string statusText = "")
{
prevProgbar.Value = (int)Math.Round(prog);
if (!string.IsNullOrWhiteSpace(statusText))
int percent = (int)Math.Round(prog);
if (percent < 0) percent = 0;
if (percent > 100) percent = 100;
htProgBar.Visible = false;
htProgBar.Value = percent;
htProgBar.Visible = true;
if (!string.IsNullOrWhiteSpace(statusText))
statusLabel.Text = statusText;
}

private void refreshModelsBtn_Click(object sender, EventArgs e)
{
EsrganData.ReloadModelList();
public void SetBusy (bool state)
{
Program.busy = state;
upscaleBtn.Enabled = !state;
refreshPreviewCutoutBtn.Enabled = !state;
refreshPreviewFullBtn.Enabled = !state;
}

private void refreshPrevBtn_Click(object sender, EventArgs e)
private void refreshModelsBtn_Click(object sender, EventArgs e)
{
PreviewTabHelper.UpscalePreview();
EsrganData.ReloadModelList();
}

private void prevToggleFilterBtn_Click(object sender, EventArgs e)
Expand All @@ -70,26 +83,15 @@ private void prevToggleFilterBtn_Click(object sender, EventArgs e)
private void previewImg_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}

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;
previewImg.ZoomToFit();
DragNDrop(array);
}

private void previewImg_MouseDown(object sender, MouseEventArgs e)
Expand All @@ -102,11 +104,6 @@ private void previewImg_MouseUp(object sender, MouseEventArgs e)
PreviewMerger.ShowOutput();
}

private void upscalePrevBtn_Click(object sender, EventArgs e)
{
PreviewTabHelper.UpscaleImage();
}

protected override void Dispose(bool disposing)
{
if (disposing && components != null)
Expand All @@ -116,26 +113,17 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private void copyComparisonClipboardBtn_Click(object sender, EventArgs e)
{
if(prevClipboardTypeCombox.SelectedIndex == 0) ClipboardPreview.CopyToClipboardSideBySide();
if(prevClipboardTypeCombox.SelectedIndex == 1) ClipboardPreview.CopyToClipboardSlider();
}

private void previewImg_Zoomed(object sender, ImageBoxZoomEventArgs e)
{
if (previewImg.Image == null)
return;
UpdatePreviewInfo();
if (previewImg.Zoom < 25) previewImg.Zoom = 25;
}

void UpdatePreviewInfo ()
{
PreviewTabHelper.UpdatePreviewLabels(prevZoomLabel, prevSizeLabel, prevCutoutLabel);
}

private void refreshPrevFullBtn_Click(object sender, EventArgs e)
{
PreviewTabHelper.UpscalePreview(true);
MainUIHelper.UpdatePreviewLabels(prevZoomLabel, prevSizeLabel, prevCutoutLabel);
}

private void settingsBtn_Click(object sender, EventArgs e)
Expand All @@ -162,9 +150,9 @@ public void UpdateModelMode()
{
modelCombox2.Enabled = (interpRbtn.Checked || chainRbtn.Checked);
interpConfigureBtn.Visible = interpRbtn.Checked;
if (singleModelRbtn.Checked) PreviewTabHelper.currentMode = PreviewTabHelper.Mode.Single;
if (interpRbtn.Checked) PreviewTabHelper.currentMode = PreviewTabHelper.Mode.Interp;
if (chainRbtn.Checked) PreviewTabHelper.currentMode = PreviewTabHelper.Mode.Chain;
if (singleModelRbtn.Checked) MainUIHelper.currentMode = MainUIHelper.Mode.Single;
if (interpRbtn.Checked) MainUIHelper.currentMode = MainUIHelper.Mode.Interp;
if (chainRbtn.Checked) MainUIHelper.currentMode = MainUIHelper.Mode.Chain;
}

private void interpConfigureBtn_Click(object sender, EventArgs e)
Expand All @@ -176,5 +164,87 @@ private void interpConfigureBtn_Click(object sender, EventArgs e)
}
InterpForm interpForm = new InterpForm(modelCombox1.Text.Trim(), modelCombox2.Text.Trim());
}

private void batchTab_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void batchTab_DragDrop(object sender, DragEventArgs e)
{
string[] array = e.Data.GetData(DataFormats.FileDrop) as string[];
DragNDrop(array);
}

async Task DragNDrop (string [] array)
{
string path = array[0];
if (IOUtils.IsPathDirectory(path))
{
htTabControl.SelectedIndex = 1;
int compatFilesAmount = IOUtils.GetAmountOfCompatibleFiles(path, true);
batchDirLabel.Text = "Loaded \"" + path + "\" - Found " + compatFilesAmount + " compatible files.";
BatchUpscaleUI.LoadDir(path);
BringToFront();
upscaleBtn.Text = "Upscale " + compatFilesAmount + " Images";
return;
}
upscaleBtn.Text = "Upscale And Save";
htTabControl.SelectedIndex = 0;
previewImg.Text = "";
SetProgress(0f, "Loading image...");
DialogForm loadingDialogForm = new DialogForm("Loading image...");
await Task.Delay(1);
MainUIHelper.ResetCachedImages();
if (!MainUIHelper.DroppedImageIsValid(array[0]))
{
SetProgress(0f, "Ready.");
await Task.Delay(1);
Program.CloseTempForms();
return;
}
string imgTempPath = Path.Combine(Paths.imgInPath, "temp.png");
File.Copy(array[0], imgTempPath, true);
await Upscale.Preprocessing(Paths.imgInPath);
previewImg.Image = IOUtils.GetImage(imgTempPath);
Program.lastFilename = array[0];
MainUIHelper.currentScale = 1;
previewImg.ZoomToFit();
loadingDialogForm.Close();
SetProgress(0f, "Ready.");
}

private void upscaleBtn_Click(object sender, EventArgs e)
{
if (Program.busy)
return;
if(htTabControl.SelectedIndex == 0)
{
MainUIHelper.UpscaleImage();
}
if (htTabControl.SelectedIndex == 1)
{
BatchUpscaleUI.Run();
}
}

private void refreshPreviewFullBtn_Click(object sender, EventArgs e)
{
MainUIHelper.UpscalePreview(true);
}

private void refreshPreviewCutoutBtn_Click(object sender, EventArgs e)
{
MainUIHelper.UpscalePreview();
}

private void copyCompToClipboardBtn_Click(object sender, EventArgs e)
{
if (prevClipboardTypeCombox.SelectedIndex == 0) ClipboardPreview.CopyToClipboardSideBySide();
if (prevClipboardTypeCombox.SelectedIndex == 1) ClipboardPreview.CopyToClipboardSlider();
}
}
}
35 changes: 18 additions & 17 deletions Code/Cupscale/PreviewMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class PreviewMerger

public static void Merge()
{
Program.mainForm.SetPreviewProgress(100f);
Program.mainForm.SetProgress(100f);
inputCutoutPath = Path.Combine(Paths.previewPath, "preview.png");
outputCutoutPath = Path.Combine(Paths.previewOutPath, "preview.png");
if (offsetX < 0f)
Expand All @@ -39,20 +39,21 @@ public static void Merge()
MagickImage sourceImg = new MagickImage(Program.lastFilename);
MagickImage cutout = new MagickImage(outputCutoutPath);
sourceImg.FilterType = Program.currentFilter;
sourceImg.Scale(new Percentage(num * 100));
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.Jpg;
sourceImg.Quality = 98;
sourceImg.Format = MagickFormat.Png;
sourceImg.Quality = 0; // Save preview as uncompressed PNG for max speed
sourceImg.Write(scaledPrevPath);
sourceImg.Composite(cutout, (Gravity)1, new PointD(offsetX, offsetY));
string text2 = Path.Combine(Paths.previewOutPath, "preview-merged.png");
sourceImg.Write(text2);
Image image = IOUtils.GetImage(text2);
PreviewTabHelper.currentOriginal = IOUtils.GetImage(scaledPrevPath);
PreviewTabHelper.currentOutput = image;
PreviewTabHelper.currentScale = ImgUtils.GetScale(IOUtils.GetImage(inputCutoutPath), IOUtils.GetImage(outputCutoutPath));
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, image);
Program.mainForm.SetPreviewProgress(0f, "Done.");
sourceImg.Composite(cutout, (Gravity)1, new PointD(offsetX, offsetY), CompositeOperator.Replace);
string mergedPreviewPath = Path.Combine(Paths.previewOutPath, "preview-merged.png");
sourceImg.Write(mergedPreviewPath);
Image image = IOUtils.GetImage(mergedPreviewPath);
MainUIHelper.currentOriginal = IOUtils.GetImage(scaledPrevPath);
MainUIHelper.currentOutput = image;
MainUIHelper.currentScale = ImgUtils.GetScale(IOUtils.GetImage(inputCutoutPath), IOUtils.GetImage(outputCutoutPath));
UIHelpers.ReplaceImageAtSameScale(MainUIHelper.previewImg, image);
Program.mainForm.SetProgress(0f, "Done.");
}

private static int GetScale()
Expand All @@ -66,19 +67,19 @@ private static int GetScale()

public static void ShowOutput()
{
if (PreviewTabHelper.currentOutput != null)
if (MainUIHelper.currentOutput != null)
{
showingOriginal = false;
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, PreviewTabHelper.currentOutput);
UIHelpers.ReplaceImageAtSameScale(MainUIHelper.previewImg, MainUIHelper.currentOutput);
}
}

public static void ShowOriginal()
{
if (PreviewTabHelper.currentOriginal != null)
if (MainUIHelper.currentOriginal != null)
{
showingOriginal = true;
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, PreviewTabHelper.currentOriginal);
UIHelpers.ReplaceImageAtSameScale(MainUIHelper.previewImg, MainUIHelper.currentOriginal);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion Code/Cupscale/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cupscale.Forms;
Expand All @@ -12,9 +13,14 @@ internal static class Program
{
public static MainForm mainForm;
public static string lastFilename;
public static string lastDirPath;
public static string lastModelName;
public static FilterType currentFilter = FilterType.Point;

public static List<Form> currentTemporaryForms = new List<Form>(); // Temp forms that get closed when something gets cancelled

public static bool busy;

[STAThread]
private static void Main()
{
Expand All @@ -30,7 +36,11 @@ private static void Main()
Application.Run(new MainForm());
}


public static void CloseTempForms ()
{
foreach (Form form in currentTemporaryForms)
form.Close();
}

public static async Task PutTaskDelay()
{
Expand Down
Loading

0 comments on commit 446f475

Please sign in to comment.