Skip to content

Commit

Permalink
Added "Keep Folder Structure / Copy All To Root Dir" option for batch…
Browse files Browse the repository at this point in the history
… processing
  • Loading branch information
N00MKRAD committed Sep 27, 2020
1 parent 749ddf2 commit d871b62
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Code/ImageUtils/ImageProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Cupscale
{
internal class ImageProcessing
{
public enum Format { Source, PngOpti, PngFast, PngRaw, Jpeg, Weppy, BMP, TGA, DDS }
public enum Format { Source, Png50, PngFast, PngRaw, Jpeg, Weppy, BMP, TGA, DDS }

public static Upscale.Filter currentFilter = Upscale.Filter.Mitchell;
public static Upscale.ScaleMode currentScaleMode = Upscale.ScaleMode.Percent;
Expand Down Expand Up @@ -59,7 +59,7 @@ public static async Task ConvertImagesToOriginalFormat(bool postprocess, bool se
{
if (GetTrimmedExtension(file2) == "png")
break;
Format format = Format.PngOpti;
Format format = Format.Png50;

if (GetTrimmedExtension(file2) == "jpg" || GetTrimmedExtension(file2) == "jpeg")
format = Format.Jpeg;
Expand Down Expand Up @@ -98,7 +98,7 @@ public static async Task ConvertImageToOriginalFormat(string path, bool postproc
//if (GetTrimmedExtension(file2) == "png")
//break;

Format format = Format.PngFast;
Format format = Format.Png50;

if (GetTrimmedExtension(file2) == "jpg" || GetTrimmedExtension(file2) == "jpeg")
format = Format.Jpeg;
Expand Down Expand Up @@ -151,10 +151,10 @@ public static async Task ConvertImage(string path, Format format, bool fillAlpha
img.Format = MagickFormat.Png;
img.Quality = 0;
}
if (format == Format.PngOpti)
if (format == Format.Png50)
{
img.Format = MagickFormat.Png;
img.Quality = 70;
img.Quality = 50;
}
if (format == Format.PngFast)
{
Expand Down Expand Up @@ -248,7 +248,7 @@ public static async Task PostProcessImage(string path, Format format, bool batch
string ext = "png";
if (format == Format.Source)
ext = Path.GetExtension(path).Replace(".", "");
if (format == Format.PngOpti)
if (format == Format.Png50)
{
img.Format = MagickFormat.Png;
img.Quality = 70;
Expand Down
88 changes: 77 additions & 11 deletions Code/Main/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Code/Main/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private async void MainForm_Load(object sender, EventArgs e)
UIHelpers.InitCombox(postResizeScale, 1);
UIHelpers.InitCombox(postResizeMode, 0);
UIHelpers.FillEnumComboBox(postResizeFilter, typeof(Upscale.Filter), 0);
// Batch Upscale
UIHelpers.InitCombox(batchOutMode, 0);
await CheckInstallation();
}

Expand Down Expand Up @@ -400,5 +402,13 @@ private async void saveMergedPreviewBtn_Click(object sender, EventArgs e)
string finalPath = IOUtils.ReplaceInFilename(outFilename, "[temp]", "");
MessageBox.Show("Saved to " + finalPath + ".", "Message");
}

private void batchOutMode_SelectedIndexChanged(object sender, EventArgs e)
{
if (batchOutMode.SelectedIndex == 0)
PostProcessingQueue.copyMode = PostProcessingQueue.CopyMode.KeepStructure;
if (batchOutMode.SelectedIndex == 1)
PostProcessingQueue.copyMode = PostProcessingQueue.CopyMode.CopyToRoot;
}
}
}
31 changes: 27 additions & 4 deletions Code/Main/PostProcessingQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class PostProcessingQueue

public static bool ncnn;

public enum CopyMode { KeepStructure, CopyToRoot }
public static CopyMode copyMode;

public static void Start (string outpath)
{
currentOutPath = outpath;
Expand Down Expand Up @@ -102,14 +105,34 @@ public static async Task ProcessQueue ()
if(Upscale.overwriteMode == Upscale.Overwrite.Yes)
{
string suffixToRemove = "-" + Program.lastModelName.Replace(":", ".").Replace(">>", "+");
Logger.Log("[Remove Suffix] Copying " + outFilename + " to " + Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")));
File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")), true);
if (copyMode == CopyMode.KeepStructure)
{
string relPath = outFilename.Replace(Paths.imgOutPath, "");
string combinedPath = currentOutPath + relPath;
Logger.Log("combinedPath = " + combinedPath);
Directory.CreateDirectory(combinedPath.GetParentDir());
File.Copy(outFilename, combinedPath.ReplaceInFilename(suffixToRemove, "", true));
}
if (copyMode == CopyMode.CopyToRoot)
{
File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")), true);
}
File.Delete(outFilename);
}
else
{
Logger.Log("[Keep Suffix] Copying " + outFilename + " to " + Path.GetFileName(outFilename));
File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename)), true);
if (copyMode == CopyMode.KeepStructure)
{
string relPath = outFilename.Replace(Paths.imgOutPath, "");
string combinedPath = currentOutPath + relPath;
Logger.Log("combinedPath = " + combinedPath);
Directory.CreateDirectory(combinedPath.GetParentDir());
File.Copy(outFilename, combinedPath, true);
}
if (copyMode == CopyMode.CopyToRoot)
{
File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename)), true);
}
File.Delete(outFilename);
}
BatchUpscaleUI.upscaledImages++;
Expand Down
2 changes: 1 addition & 1 deletion Code/Main/Upscale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static async Task PostprocessingSingle (string path, bool batchProcessing
if (outputFormat.Text == ExportFormats.PNG.ToStringTitleCase())
{
//path = Path.ChangeExtension(path, "png");
await ImageProcessing.PostProcessImage(path, ImageProcessing.Format.PngFast, batchProcessing);
await ImageProcessing.PostProcessImage(path, ImageProcessing.Format.Png50, batchProcessing);
}
if (outputFormat.Text == ExportFormats.SameAsSource.ToStringTitleCase())
await ImageProcessing.ConvertImageToOriginalFormat(path, true, false, batchProcessing);
Expand Down
7 changes: 7 additions & 0 deletions Code/UI/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,12 @@ public static Image Scale (this Image img, float scale, InterpolationMode filter
{
return ImageOperations.Scale(img, scale, filtering);
}

public static string ReplaceInFilename(string path, string find, string replaceWith)
{
string parentDir = path.GetParentDir();
string filename = Path.GetFileName(path);
return Path.Combine(parentDir, filename.Replace(find, replaceWith));
}
}
}

0 comments on commit d871b62

Please sign in to comment.