Skip to content

Commit f02dc87

Browse files
author
N00MKRAD
committed
Fixes, better preview info (zoom/size/cutout)
1 parent 2b1de78 commit f02dc87

File tree

7 files changed

+332
-250
lines changed

7 files changed

+332
-250
lines changed

Code/Cupscale/MainForm.cs

Lines changed: 229 additions & 211 deletions
Large diffs are not rendered by default.

Code/Cupscale/PreviewMerger.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Drawing;
33
using System.IO;
4+
using Cupscale.ImageUtils;
45
using Cupscale.IO;
56
using Cupscale.UI;
67
using ImageMagick;
@@ -10,19 +11,14 @@ namespace Cupscale
1011
{
1112
internal class PreviewMerger
1213
{
13-
public static Image currentOriginal;
14-
public static Image currentOutput;
15-
1614
public static float offsetX;
17-
1815
public static float offsetY;
19-
2016
public static string inputCutoutPath;
21-
2217
public static string outputCutoutPath;
23-
2418
public static int scale;
2519

20+
public static bool showingOriginal = false;
21+
2622
public static void Merge()
2723
{
2824
Program.mainForm.SetPreviewProgress(100f);
@@ -44,16 +40,17 @@ public static void Merge()
4440
MagickImage val2 = new MagickImage(outputCutoutPath);
4541
val.FilterType = (FilterType)1;
4642
val.Scale(new Percentage(num * 100));
47-
string text = Path.Combine(Paths.previewOutPath, "preview-input-scaled.png");
43+
string scaledPrevPath = Path.Combine(Paths.previewOutPath, "preview-input-scaled.png");
4844
val.Format = (MagickFormat)171;
4945
val.Quality = (10);
50-
val.Write(text);
46+
val.Write(scaledPrevPath);
5147
val.Composite((IMagickImage<ushort>)(object)val2, (Gravity)1, new PointD((double)offsetX, (double)offsetY));
5248
string text2 = Path.Combine(Paths.previewOutPath, "preview-merged.png");
5349
val.Write(text2);
5450
Image image = IOUtils.GetImage(text2);
55-
currentOriginal = IOUtils.GetImage(text);
56-
currentOutput = image;
51+
PreviewTabHelper.currentOriginal = IOUtils.GetImage(scaledPrevPath);
52+
PreviewTabHelper.currentOutput = image;
53+
PreviewTabHelper.currentScale = ImgUtils.GetScale(IOUtils.GetImage(inputCutoutPath), IOUtils.GetImage(outputCutoutPath));
5754
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, image);
5855
Program.mainForm.SetPreviewProgress(0f, "Done.");
5956
}
@@ -67,25 +64,21 @@ private static int GetScale()
6764
return result;
6865
}
6966

70-
public static void ResetCachedImages()
71-
{
72-
currentOriginal = null;
73-
currentOutput = null;
74-
}
75-
7667
public static void ShowOutput()
7768
{
78-
if (currentOutput != null)
69+
if (PreviewTabHelper.currentOutput != null)
7970
{
80-
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, currentOutput);
71+
showingOriginal = false;
72+
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, PreviewTabHelper.currentOutput);
8173
}
8274
}
8375

8476
public static void ShowOriginal()
8577
{
86-
if (currentOriginal != null)
78+
if (PreviewTabHelper.currentOriginal != null)
8779
{
88-
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, currentOriginal);
80+
showingOriginal = true;
81+
UIHelpers.ReplaceImageAtSameScale(PreviewTabHelper.previewImg, PreviewTabHelper.currentOriginal);
8982
}
9083
}
9184
}

Code/ImageUtils/ImgUtils.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Cupscale.ImageUtils
9+
{
10+
class ImgUtils
11+
{
12+
public static int GetScale (Image imgFrom, Image imgTo)
13+
{
14+
return (int)Math.Round(GetScaleFloat(imgFrom, imgTo));
15+
}
16+
17+
public static float GetScaleFloat(Image imgFrom, Image imgTo)
18+
{
19+
return (float)imgTo.Width / (float)imgFrom.Width;
20+
}
21+
}
22+
}

Code/shellUpscaler/ImageProcessing.cs renamed to Code/ImageUtils/UpscaleProcessing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace shellUpscaler
1010
{
11-
internal class ImageProcessing
11+
internal class UpscaleProcessing
1212
{
1313
public enum Format
1414
{

Code/OS/ESRGAN.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using Cupscale.ImageUtils;
12
using Cupscale.IO;
3+
using Cupscale.UI;
24
using System.Diagnostics;
5+
using System.Drawing;
36
using System.IO;
47
using System.Threading.Tasks;
58
using System.Windows.Forms;
@@ -10,19 +13,33 @@ internal class ESRGAN
1013
{
1114
private static Process currentProcess;
1215

13-
public static async Task UpscaleBasic(string inpath, string outpath, string model, string tilesize, bool alpha, bool isPreview = false)
16+
public enum PreviewMode { None, Cutout, FullImage }
17+
public static async Task UpscaleBasic(string inpath, string outpath, string model, string tilesize, bool alpha, PreviewMode mode)
1418
{
1519
string formattedModelPath = Config.Get("modelPath").Replace("/", "\\").TrimEnd('\\');
1620
string modelArg = "\"" + formattedModelPath + "/" + model + ".pth\"";
1721
Program.mainForm.SetPreviewProgress(5f, "Starting ESRGAN...");
1822
await Run(inpath, outpath, modelArg, tilesize, alpha);
1923
File.Delete(Paths.progressLogfile);
20-
if (isPreview)
24+
if (mode == PreviewMode.Cutout)
2125
{
2226
Program.mainForm.SetPreviewProgress(100f, "Merging into preview...");
2327
await Program.PutTaskDelay();
2428
PreviewMerger.Merge();
2529
}
30+
if (mode == PreviewMode.FullImage)
31+
{
32+
Program.mainForm.SetPreviewProgress(100f, "Merging into preview...");
33+
await Program.PutTaskDelay();
34+
Image outImg = IOUtils.GetImage(Path.Combine(Paths.previewOutPath, "preview.png"));
35+
Image inputImg = IOUtils.GetImage(Program.lastFilename);
36+
PreviewTabHelper.previewImg.Image = outImg;
37+
PreviewTabHelper.currentOriginal = inputImg;
38+
PreviewTabHelper.currentOutput = outImg;
39+
PreviewTabHelper.currentScale = ImgUtils.GetScale(inputImg, outImg);
40+
PreviewTabHelper.previewImg.ZoomToFit();
41+
Program.mainForm.SetPreviewProgress(0f, "Done.");
42+
}
2643
}
2744

2845
public static async Task Run(string inpath, string outpath, string modelArg, string tilesize, bool alpha)

Code/UI/PreviewTabHelper.cs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Drawing;
33
using System.Drawing.Drawing2D;
44
using System.IO;
5+
using System.Numerics;
56
using System.Security.AccessControl;
67
using System.Windows.Forms;
78
using Cupscale.IO;
@@ -20,6 +21,11 @@ public enum Mode { Basic, Interp, Chain, Advanced }
2021
private static ComboBox outputFormat;
2122
private static ComboBox overwrite;
2223

24+
public static Image currentOriginal;
25+
public static Image currentOutput;
26+
27+
public static int currentScale = 1;
28+
2329
public static void Init(ImageBox imgBox, ComboBox basicModelBox, ComboBox formatBox, ComboBox overwriteBox)
2430
{
2531
previewImg = imgBox;
@@ -60,14 +66,14 @@ public static async void UpscaleImage()
6066
Cancel("I/O Error");
6167
return;
6268
}
63-
ImageProcessing.ConvertImages(ImageProcessing.Format.PngFast, !Config.GetBool("alpha"), true, true);
69+
UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.PngFast, !Config.GetBool("alpha"), true, true);
6470
string mdl = GetMdl();
6571
if (string.IsNullOrWhiteSpace(mdl))
6672
{
6773
Cancel("Model Not Found");
6874
return;
6975
}
70-
await ESRGAN.UpscaleBasic(Paths.imgInPath, Paths.imgOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")));
76+
await ESRGAN.UpscaleBasic(Paths.imgInPath, Paths.imgOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), ESRGAN.PreviewMode.None);
7177
Postprocessing();
7278
AddModelSuffix(Paths.imgOutPath);
7379
CopyImagesToOriginalLocation();
@@ -109,17 +115,17 @@ static async void Postprocessing()
109115
await Program.PutTaskDelay();
110116
Logger.Log("Postprocessing - outputFormat.SelectedIndex = " + outputFormat.SelectedIndex);
111117
if (outputFormat.SelectedIndex == 0)
112-
ImageProcessing.ChangeOutputExtensions("png");
118+
UpscaleProcessing.ChangeOutputExtensions("png");
113119
if (outputFormat.SelectedIndex == 1)
114-
ImageProcessing.ConvertImagesToOriginalFormat();
120+
UpscaleProcessing.ConvertImagesToOriginalFormat();
115121
if (outputFormat.SelectedIndex == 2)
116-
ImageProcessing.ConvertImages(ImageProcessing.Format.JpegHigh);
122+
UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.JpegHigh);
117123
if (outputFormat.SelectedIndex == 3)
118-
ImageProcessing.ConvertImages(ImageProcessing.Format.JpegMed);
124+
UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.JpegMed);
119125
if (outputFormat.SelectedIndex == 4)
120-
ImageProcessing.ConvertImages(ImageProcessing.Format.WeppyHigh);
126+
UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.WeppyHigh);
121127
if (outputFormat.SelectedIndex == 5)
122-
ImageProcessing.ConvertImages(ImageProcessing.Format.WeppyLow);
128+
UpscaleProcessing.ConvertImages(UpscaleProcessing.Format.WeppyLow);
123129
}
124130

125131
static void AddModelSuffix(string path)
@@ -147,9 +153,11 @@ static void CopyImagesToOriginalLocation()
147153
public static async void UpscalePreview(bool fullImage = false)
148154
{
149155
Program.mainForm.SetPreviewProgress(3f, "Preparing...");
150-
if (fullImage)
156+
ESRGAN.PreviewMode prevMode = ESRGAN.PreviewMode.Cutout;
157+
if (fullImage)
151158
{
152-
if(!IOUtils.TryCopy(Program.lastFilename, Path.Combine(Paths.previewPath, "preview.png"), true)) return;
159+
prevMode = ESRGAN.PreviewMode.FullImage;
160+
if (!IOUtils.TryCopy(Program.lastFilename, Path.Combine(Paths.previewPath, "preview.png"), true)) return;
153161
}
154162
else
155163
{
@@ -160,7 +168,7 @@ public static async void UpscalePreview(bool fullImage = false)
160168
string mdl = GetMdl();
161169
if (string.IsNullOrWhiteSpace(mdl)) return;
162170
Logger.Log(Paths.previewPath + " - " + Paths.previewOutPath + " - " + mdl + " - " + Config.Get("tilesize") + " - " + bool.Parse(Config.Get("alpha")));
163-
await ESRGAN.UpscaleBasic(Paths.previewPath, Paths.previewOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), isPreview: true);
171+
await ESRGAN.UpscaleBasic(Paths.previewPath, Paths.previewOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), prevMode);
164172
}
165173
}
166174

@@ -194,18 +202,17 @@ public static Bitmap GetCurrentRegion() // thx ieu
194202
double zoomFactor = previewImg.ZoomFactor;
195203
int num3 = (int)Math.Round(SystemInformation.VerticalScrollBarWidth / zoomFactor);
196204
int num4 = (int)Math.Round(SystemInformation.HorizontalScrollBarHeight / zoomFactor);
197-
Size size = previewImg.Image.Size;
198205
int num5 = (int)Math.Round(sourceImageRegion.Width * zoomFactor);
199206
int num6 = (int)Math.Round(sourceImageRegion.Height * zoomFactor);
200-
Size size2 = previewImg.GetInsideViewPort().Size;
207+
Size size = previewImg.GetInsideViewPort().Size;
201208
Logger.Log("Saving current region to bitmap. Offset: " + previewImg.AutoScrollPosition.X + "x" + previewImg.AutoScrollPosition.Y);
202209
PreviewMerger.offsetX = (float)previewImg.AutoScrollPosition.X / (float)previewImg.ZoomFactor;
203210
PreviewMerger.offsetY = (float)previewImg.AutoScrollPosition.Y / (float)previewImg.ZoomFactor;
204-
if (num5 <= size2.Width)
211+
if (num5 <= size.Width)
205212
{
206213
num3 = 0;
207214
}
208-
if (num6 <= size2.Height)
215+
if (num6 <= size.Height)
209216
{
210217
num4 = 0;
211218
}
@@ -222,5 +229,29 @@ public static Bitmap GetCurrentRegion() // thx ieu
222229
}
223230
return bitmap;
224231
}
232+
233+
public static SizeF GetCutoutSize ()
234+
{
235+
SizeF cutoutSize = previewImg.GetSourceImageRegion().Size;
236+
cutoutSize.Width = (int)Math.Round(cutoutSize.Width);
237+
cutoutSize.Height = (int)Math.Round(cutoutSize.Height);
238+
return cutoutSize;
239+
}
240+
241+
public static void ResetCachedImages()
242+
{
243+
currentOriginal = null;
244+
currentOutput = null;
245+
}
246+
247+
public static void UpdatePreviewLabels(Label zoom, Label size, Label cutout)
248+
{
249+
int currScale = currentScale;
250+
int cutoutW = (int)GetCutoutSize().Width;
251+
int cutoutH = (int)GetCutoutSize().Height;
252+
zoom.Text = "Zoom: " + previewImg.Zoom + "% (Original: " + previewImg.Zoom * currScale + "%)";
253+
size.Text = "Size: " + previewImg.Image.Width + "x" + previewImg.Image.Height + " (Original: " + previewImg.Image.Width / currScale + "x" + previewImg.Image.Height / currScale + ")";
254+
cutout.Text = "Cutout: " + cutoutW + "x" + cutoutH + " (Original: " + cutoutW / currScale + "x" + cutoutH / currScale + ")";// + "% - Unscaled Size: " + previewImg.Image.Size * currScale + "%";
255+
}
225256
}
226257
}

Code/UI/UIHelpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static void FillModelComboBox(ComboBox box, bool resetIndex = false)
3434

3535
public static void ReplaceImageAtSameScale(ImageBox imgBox, Image newImg)
3636
{
37+
Logger.Log("Replacing image on " + imgBox.Name + " with new image (" + newImg.Width + "x" + newImg.Height + ")");
3738
float num = (float)imgBox.Image.Width / (float)newImg.Width;
3839
float num2 = (float)imgBox.AutoScrollPosition.X / num;
3940
float num3 = (float)imgBox.AutoScrollPosition.Y / num;

0 commit comments

Comments
 (0)