Skip to content

Commit

Permalink
Added Expand functionality to Crop tab, more error handling, added ro…
Browse files Browse the repository at this point in the history
…tate/flip utils
  • Loading branch information
N00MKRAD committed Aug 16, 2020
1 parent 11d4a5a commit 9cef490
Show file tree
Hide file tree
Showing 19 changed files with 695 additions and 125 deletions.
Binary file modified .vs/MagickUtils/v16/.suo
Binary file not shown.
14 changes: 14 additions & 0 deletions FormsHelpers/CropTabHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public static void CropAbsolute (TextBox wBox, TextBox hBox, ComboBox gravBox)
CropUtils.CropAbsoluteDir(wBox.GetInt(), hBox.GetInt(), GetGravity(gravBox.SelectedIndex));
}

public static void CropDivisible (ComboBox divisibleByBox, ComboBox gravBox, CheckBox expandBox)
{
CropUtils.CropDivisibleDir(divisibleByBox.GetInt(), GetGravity(gravBox.SelectedIndex), expandBox.Checked);
}

public static void CropPadding (TextBox pixMinBox, TextBox pixMaxBox, ComboBox modeBox)
{
int pixMin = pixMinBox.GetInt();
int pixMax = pixMin;
if(!string.IsNullOrWhiteSpace(pixMaxBox.Text))
pixMax = pixMaxBox.GetInt();
CropUtils.CropPaddingDir(pixMin, pixMax, modeBox.SelectedIndex == 1);
}

public static Gravity GetGravity (int selectedIndex)
{
switch(selectedIndex)
Expand Down
1 change: 1 addition & 0 deletions MagickUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="MagickUtils\AdjustUtils.cs" />
<Compile Include="MagickUtils\GeometryUtils.cs" />
<Compile Include="MagickUtils\TilingUtils.cs" />
<Compile Include="Utils\Config.cs" />
<Compile Include="FormsHelpers\ConvertTabHelper.cs" />
Expand Down
8 changes: 7 additions & 1 deletion MagickUtils/ConvertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static async void ConvertDirToFlif (int q, bool delSrc)
public static void ConvertToJpeg (string path, int q = 95, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Jpeg;
img.Quality = q;
string outPath = Path.ChangeExtension(path, null) + ".jpg";
Expand All @@ -155,6 +156,7 @@ public static void ConvertToJpeg (string path, int q = 95, bool delSource = fals
public static void ConvertToJpegRandomQuality (string path, int qMin, int qMax, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Jpeg;
Random rand = new Random();
img.Quality = rand.Next(qMin, qMax + 1);
Expand All @@ -167,6 +169,7 @@ public static void ConvertToJpegRandomQuality (string path, int qMin, int qMax,
public static void ConvertToPng (string path, int pngCompressLvl = 0, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Png;
img.Quality = pngCompressLvl;
string outPath = Path.ChangeExtension(path, null) + ".png";
Expand All @@ -178,6 +181,7 @@ public static void ConvertToPng (string path, int pngCompressLvl = 0, bool delSo
public static void ConvertToDds (string path, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Dds;
var defines = new DdsWriteDefines { Compression = DdsCompression.Dxt1 };
img.Settings.SetDefines(defines);
Expand All @@ -190,6 +194,7 @@ public static void ConvertToDds (string path, bool delSource = false)
public static void ConvertToTga (string path, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Tga;
string outPath = Path.ChangeExtension(path, null) + ".tga";
PreProcessing(path);
Expand All @@ -199,7 +204,7 @@ public static void ConvertToTga (string path, bool delSource = false)

public static void ConvertToWebp (string path, int q, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
MagickImage img = IOUtils.ReadImage(path);if(img == null) return;
img.Format = MagickFormat.WebP;
string outPath = Path.ChangeExtension(path, null) + ".webp";
img.Quality = q;
Expand All @@ -211,6 +216,7 @@ public static void ConvertToWebp (string path, int q, bool delSource = false)
public static void ConvertToJpeg2000 (string path, int q, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
if(img == null) return;
img.Format = MagickFormat.Jp2;
string outPath = Path.ChangeExtension(path, null) + ".jp2";
img.Quality = q;
Expand Down
108 changes: 80 additions & 28 deletions MagickUtils/CropUtils.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using ImageMagick;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MagickUtils
{
class CropUtils
{
static long bytesPre = 0;

public static async void CropDivisibleDir (int divisibleBy)
public static async void CropDivisibleDir (int divisibleBy, Gravity grav, bool expand)
{
int counter = 1;
FileInfo[] Files = IOUtils.GetFiles();
Expand All @@ -22,29 +24,42 @@ public static async void CropDivisibleDir (int divisibleBy)
{
Program.ShowProgress("Cropping Image ", counter, Files.Length);
counter++;
CropDivisible(file.FullName, divisibleBy);
CropDivisible(file.FullName, divisibleBy, grav, expand);
if (counter % 3 == 0) await Program.PutTaskDelay();
}
Program.PostProcessing();
}

public static void CropDivisible (string path, int divisibleBy)
public static void CropDivisible (string path, int divisibleBy, Gravity grav, bool expand)
{
MagickImage img = IOUtils.ReadImage(path);

int divisbleWidth = img.Width;
while (divisbleWidth % divisibleBy != 0) divisbleWidth--;

int divisibleHeight = img.Height;
while (divisibleHeight % divisibleBy != 0) divisibleHeight--;

if(!expand) // Crop
{
while(divisbleWidth % divisibleBy != 0) divisbleWidth--;
while(divisibleHeight % divisibleBy != 0) divisibleHeight--;
}
else // Expand
{
while(divisbleWidth % divisibleBy != 0) divisbleWidth++;
while(divisibleHeight % divisibleBy != 0) divisibleHeight++;
img.BackgroundColor = new MagickColor("#" + Config.backgroundColor);
}

if(divisbleWidth == img.Width && divisibleHeight == img.Height)
{
Program.Print("-> Skipping " + Path.GetFileName(path) + " as its resolution is already divisible by " + divisibleBy);
}
else
{
img.Crop(divisbleWidth, divisibleHeight, Gravity.Center);
Program.Print("-> Divisible resolution: " + divisbleWidth + "x" + divisibleHeight);
if(!expand) // Crop
img.Crop(divisbleWidth, divisibleHeight, grav);
else // Expand
img.Extent(divisbleWidth, divisibleHeight, grav);
img.RePage();
img.Write(path);
}
Expand All @@ -54,11 +69,11 @@ public static async void CropRelativeDir (int minSize, int maxSize, SizeMode siz
{
int counter = 1;
FileInfo[] Files = IOUtils.GetFiles();
Program.Print("Cropping " + Files.Length + " images...");
Program.Print("Resizing " + Files.Length + " images...");
Program.PreProcessing();
foreach(FileInfo file in Files)
{
Program.ShowProgress("Cropping Image ", counter, Files.Length);
Program.ShowProgress("Resizing Image ", counter, Files.Length);
counter++;
CropRelative(file.FullName, minSize, maxSize, sizeMode, grav);
if(counter % 3 == 0) await Program.PutTaskDelay();
Expand All @@ -71,38 +86,77 @@ public static void CropRelative (string path, int minSize, int maxSize, SizeMode
{
MagickImage img = IOUtils.ReadImage(path);
PreProcessing(path);
string fname = Path.GetFileName(path);
Program.Print("-> " + fname + " (" + img.Width + "x" + img.Height + ")");

Random rand = new Random();
int targetSize = rand.Next(minSize, maxSize + 1);
MagickGeometry geom = null;

bool heightLonger = img.Height > img.Width;
bool widthLonger = img.Width > img.Height;
bool square = (img.Height == img.Width);

if(square || sizeMode == SizeMode.Height || (sizeMode == SizeMode.Longer && heightLonger) || (sizeMode == SizeMode.Shorter && widthLonger))
{
//if(onlyDownscale && (img.Height <= targetSize)) return;
Program.Print(" -> Cropping to " + targetSize + "px height...");
Program.Print("-> Resizing to " + targetSize + "px height...");
int w = (int)Math.Round(img.Width * ((targetSize / (float)img.Height)));
MagickGeometry geom = new MagickGeometry(w + "x" + targetSize);
img.Crop(geom, grav);
geom = new MagickGeometry(w + "x" + targetSize);
}
if(sizeMode == SizeMode.Width || (sizeMode == SizeMode.Longer && widthLonger) || (sizeMode == SizeMode.Shorter && heightLonger))
{
//if(onlyDownscale && (img.Width <= targetSize) return;
Program.Print(" -> Cropping to " + targetSize + "px width...");
Program.Print("-> Resizing to " + targetSize + "px width...");
int h = (int)Math.Round(img.Height * ((targetSize / (float)img.Width)));
MagickGeometry geom = new MagickGeometry(targetSize + "x" + h);
img.Crop(geom, grav);
geom = new MagickGeometry(targetSize + "x" + h);
}
if(sizeMode == SizeMode.Percentage)
{
Program.Print(" -> Cropping to " + targetSize + "% with filter...");
int w = (int)Math.Round(img.Width * targetSize / 100f);
int h = (int)Math.Round(img.Height * targetSize / 100f);
img.Crop(h, w, grav);
Program.Print("-> Resizing to " + targetSize + "% (" + w + "x" + h + ")...");
geom = new MagickGeometry(w + "x" + h);
}
img.Extent(geom, grav);
img.Write(path);
PostProcessing(img, path);
}

public static async void CropPaddingDir (int pixMin, int pixMax, bool cut)
{
int counter = 1;
FileInfo[] Files = IOUtils.GetFiles();
Program.Print("Resizing " + Files.Length + " images...");
Program.PreProcessing();
foreach(FileInfo file in Files)
{
Program.ShowProgress("Resizing Image ", counter, Files.Length);
counter++;
CropPadding(file.FullName, pixMin, pixMax, cut);
if(counter % 3 == 0) await Program.PutTaskDelay();
}
Program.PostProcessing();
}

public static void CropPadding (string path, int pixMin, int pixMax, bool cut)
{
MagickImage img = IOUtils.ReadImage(path);
PreProcessing(path);

Random rand = new Random();
int pix = rand.Next(pixMin, pixMax + 1);
int w = img.Width;
int h = img.Height;
if(!cut)
{
w = img.Width + pix;
h = img.Height + pix;
}
else
{
w = img.Width - pix;
h = img.Height - pix;
}
MagickGeometry geom = new MagickGeometry(w + "x" + h + "!");
img.Extent(geom, Gravity.Center);

img.Write(path);
PostProcessing(img, path);
}
Expand All @@ -111,11 +165,11 @@ public static async void CropAbsoluteDir (int newWidth, int newHeight, Gravity g
{
int counter = 1;
FileInfo[] Files = IOUtils.GetFiles();
Program.Print("Cropping " + Files.Length + " images...");
Program.Print("Resizing " + Files.Length + " images...");
Program.PreProcessing();
foreach(FileInfo file in Files)
{
Program.ShowProgress("Cropping Image ", counter, Files.Length);
Program.ShowProgress("Resizing Image ", counter, Files.Length);
counter++;
CropAbsolute(file.FullName, newWidth, newHeight, grav);
if(counter % 3 == 0) await Program.PutTaskDelay();
Expand All @@ -127,10 +181,8 @@ public static void CropAbsolute (string path, int newWidth, int newHeight, Gravi
{
MagickImage img = IOUtils.ReadImage(path);
PreProcessing(path);
string fname = Path.GetFileName(path);
Program.Print("-> " + fname + " (" + img.Width + "x" + img.Height + ")");

img.Crop(newWidth, newHeight, grav);
img.Extent(newWidth, newHeight, grav);

img.Write(path);
PostProcessing(img, path);
Expand All @@ -149,12 +201,12 @@ static void PostProcessing (MagickImage img, string outPath)
Program.sw.Stop();
img.Dispose();
//long bytesPost = new FileInfo(outPath).Length;
//Program.Print(" -> Done. Size pre: " + Format.Filesize(bytesPre) + " - Size post: " + Format.Filesize(bytesPost) + " - Ratio: " + Format.Ratio(bytesPre, bytesPost));
//Program.Print("-> Done. Size pre: " + Format.Filesize(bytesPre) + " - Size post: " + Format.Filesize(bytesPost) + " - Ratio: " + Format.Ratio(bytesPre, bytesPost));
}

static void DelSource (string path)
{
Program.Print(" -> Deleting source file: " + Path.GetFileName(path) + "...\n");
Program.Print("-> Deleting source file: " + Path.GetFileName(path) + "...\n");
File.Delete(path);
}
}
Expand Down
Loading

0 comments on commit 9cef490

Please sign in to comment.