Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
N00MKRAD committed Jul 20, 2020
1 parent 4700953 commit 7819cbd
Show file tree
Hide file tree
Showing 75 changed files with 170,734 additions and 0 deletions.
Binary file added .vs/MagickUtils/v16/.suo
Binary file not shown.
45 changes: 45 additions & 0 deletions AdjustUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;

namespace MagickUtils
{
class AdjustUtils
{
public static void AutoGamma (string path)
{
MagickImage img = new MagickImage(path);
string fname = Path.ChangeExtension(path, null);
Print("-> " + fname + "\n");
img.AutoGamma();
img.Write(path);
}

public static void AutoLevel (string path)
{
MagickImage img = new MagickImage(path);
string fname = Path.ChangeExtension(path, null);
Print("-> " + fname + "\n");
img.AutoLevel();
img.Write(path);
}

public static void AutoThreshold (string path)
{
MagickImage img = new MagickImage(path);
string fname = Path.ChangeExtension(path, null);
Print("-> " + fname + "\n");
img.AutoThreshold(AutoThresholdMethod.Kapur);
img.Write(path);
}

static void Print (string s)
{
Console.WriteLine(s);
}
}
}
48 changes: 48 additions & 0 deletions AdjustUtilsUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MagickUtils
{
class AdjustUtilsUI
{
public static void AutoLevel (string path, string ext, bool recursive)
{
/*
string mode = "1";
Console.Write("[Default: 1] Select mode:\n1 Auto-Gamma\n2 Auto-Level\n3 Auto-Threshold\n");
string modeInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(modeInput.Trim())) mode = modeInput;
string recursive = "n";
Console.Write("[Default: n] Recursive (include all subfolders)? (y/n): ");
string recursiveInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(recursiveInput.Trim())) recursive = recursiveInput;
FileInfo[] files = null;
DirectoryInfo d = new DirectoryInfo(Program.currentDir);
if(Program.IsTrue(recursive))
files = d.GetFiles("*." + ext, SearchOption.AllDirectories);
else
files = d.GetFiles("*." + ext, SearchOption.TopDirectoryOnly);
*/


FileInfo[] files = Program.GetFiles(path, ext, recursive);

int counter = 1;
foreach(FileInfo file in files)
{
//Program.Print("Adjusting Image " + counter + "/" + files.Length);
Program.ShowProgress("Adjusting image ", counter, files.Length);
AdjustUtils.AutoLevel(file.FullName);
counter++;
}
}
}
}
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
19 changes: 19 additions & 0 deletions ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MagickUtils
{
class ConsoleUtils
{
public static void ClearCurrentConsoleLine ()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
}
}
91 changes: 91 additions & 0 deletions ConvertUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
using System.IO;
using ImageMagick.Formats.Dds;

namespace MagickUtils
{
class ConvertUtils
{
static long bytesPre;

public static void ConvertToJpeg (string path, int q = 95, bool delSource = false)
{
MagickImage img = new MagickImage(path) { Format = MagickFormat.Jpeg };
img.Quality = q;
string outPath = Path.ChangeExtension(path, null) + ".jpg";
PreProcessing(path);
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

public static void ConvertToJpegRandomQuality (string path, int qMin, int qMax, bool delSource = false)
{
MagickImage img = new MagickImage(path) { Format = MagickFormat.Jpeg };
Random rand = new Random();
img.Quality = rand.Next(qMin, qMax + 1);
string outPath = Path.ChangeExtension(path, null) + ".jpg";
PreProcessing(path, " [JPEG Quality: " + img.Quality + "]");
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

public static void ConvertToPng (string path, int pngCompressLvl = 0, bool delSource = false)
{
MagickImage img = new MagickImage(path) { Format = MagickFormat.Png };
img.Quality = pngCompressLvl;
string outPath = Path.ChangeExtension(path, null) + ".png";
PreProcessing(path);
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

public static void ConvertToDds (string path, bool delSource = false)
{
MagickImage img = new MagickImage(path) { Format = MagickFormat.Dds };
var defines = new DdsWriteDefines { Compression = DdsCompression.Dxt1 };
img.Settings.SetDefines(defines);
string outPath = Path.ChangeExtension(path, null) + ".dds";
PreProcessing(path);
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

public static void ConvertToTga (string path, bool delSource = false)
{
MagickImage img = new MagickImage(path) { Format = MagickFormat.Tga };
string outPath = Path.ChangeExtension(path, null) + ".tga";
PreProcessing(path);
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

static void PreProcessing (string path, string infoSuffix = null)
{
bytesPre = 0;
bytesPre = new FileInfo(path).Length;
Program.Print("-> Processing " + Path.GetFileName(path) + " " + infoSuffix);
Program.sw.Start();
}

static void PostProcessing (MagickImage img, string sourcePath, string outPath, bool delSource)
{
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));
if(delSource)
DelSource(sourcePath);
}

static void DelSource (string path)
{
Program.Print(" -> Deleting source file: " + Path.GetFileName(path) + "...\n");
File.Delete(path);
}
}
}
158 changes: 158 additions & 0 deletions ConvertUtilsUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.IO;
using System.Diagnostics;

namespace MagickUtils
{
class ConvertUtilsUI
{
public static void ConvertChooser (string ext)
{
Console.Write("\nAvailable commands:\n");
Console.Write("1 Convert to JPEG\n2 Convert to PNG\n3 Convert to DDS\n4 Convert to TGA");
Console.Write("\n\n");

Console.Write("Enter your command number: ");
string cmd = Console.ReadLine();

/*
if(int.Parse(cmd) == 1)
ConvertDirToJpeg(ext);
if(int.Parse(cmd) == 2)
ConvertDirToPng(ext);
if(int.Parse(cmd) == 3)
ConvertDirToDds(ext);
if(int.Parse(cmd) == 4)
ConvertDirToTga(ext);
*/
}

public static void ConvertDirToJpegCmd (string ext)
{
string qMin = "95";
Console.Write("[Default: 95] Minimum JPEG Quality: ");
string qMinInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(qMinInput.Trim())) qMin = qMinInput;

string qMax = qMin;
Console.Write("[Leave blank to use fixed quality] Maximum JPEG Quality: ");
string qMaxInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(qMaxInput.Trim())) qMax = qMaxInput;

string recursive = "n";
Console.Write("[Default: n] Recursive (include all subfolders)? (y/n): ");
string recursiveInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(recursiveInput.Trim())) recursive = recursiveInput;

string delSrc = "n";
Console.Write("[Default: n] Delete source file afterwards? (y/n): ");
string delSrcInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(delSrcInput.Trim())) delSrc = delSrcInput;

ConvertDirToJpeg(Program.currentDir, ext, int.Parse(qMin), int.Parse(qMax), Program.IsTrue(recursive), Program.IsTrue(delSrc));
}

public static void ConvertDirToJpeg (string path, string ext, int qMin, int qMax, bool recursive, bool delSrc)
{
int counter = 1;
FileInfo[] files = Program.GetFiles(path, ext, recursive);

Program.PreProcessing();
foreach(FileInfo file in files)
{
Program.ShowProgress("Converting Image ", counter, files.Length);
ConvertUtils.ConvertToJpegRandomQuality(file.FullName, qMin, qMax, delSrc);
counter++;
Program.mainForm.Show();
}
Program.PostProcessing();
}

public static void ConvertDirToPng (string path, string ext, int q, bool recursive, bool delSrc)
{
/*
string pngCompressionLvl = "0";
Console.Write("[Default: 0] Set PNG compression (0-100): ");
string newPngCompressionLvl = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(newPngCompressionLvl.Trim())) pngCompressionLvl = newPngCompressionLvl;
string recursive = "n";
Console.Write("[Default: n] Recursive (include all subfolders)? (y/n): ");
string recursiveInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(recursiveInput.Trim())) recursive = recursiveInput;
string delSrc = "n";
Console.Write("[Default: n] Delete source file afterwards? (y/n): ");
string delSrcInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(delSrcInput.Trim())) delSrc = delSrcInput;
*/

int counter = 1;
FileInfo[] files = Program.GetFiles(path, ext, recursive);

Program.PreProcessing();
foreach(FileInfo file in files)
{
Program.ShowProgress("Converting Image ", counter, files.Length);
ConvertUtils.ConvertToPng(file.FullName, q, delSrc);
counter++;
}
Program.PostProcessing();
}


public static void ConvertDirToDds (string path, string ext, bool recursive, bool delSrc)
{
/*
string recursive = "n";
Console.Write("[Default: n] Recursive (include all subfolders)? (y/n): ");
string recursiveInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(recursiveInput.Trim())) recursive = recursiveInput;
string delSrc = "n";
Console.Write("[Default: n] Delete source file afterwards? (y/n): ");
string delSrcInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(delSrcInput.Trim())) delSrc = delSrcInput;
*/

int counter = 1;
FileInfo[] files = Program.GetFiles(path, ext, recursive);

Program.PreProcessing();
foreach(FileInfo file in files)
{
Program.ShowProgress("Converting Image ", counter, files.Length);
counter++;
ConvertUtils.ConvertToDds(file.FullName, delSrc);
}
Program.PostProcessing();
}

public static void ConvertDirToTga (string path, string ext, bool recursive, bool delSrc)
{
/*
string recursive = "n";
Console.Write("[Default: n] Recursive (include all subfolders)? (y/n): ");
string recursiveInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(recursiveInput.Trim())) recursive = recursiveInput;
string delSrc = "n";
Console.Write("[Default: n] Delete source file afterwards? (y/n): ");
string delSrcInput = Console.ReadLine();
if(!string.IsNullOrWhiteSpace(delSrcInput.Trim())) delSrc = delSrcInput;
*/

int counter = 1;
FileInfo[] files = Program.GetFiles(path, ext, recursive);

Program.PreProcessing();
foreach(FileInfo file in files)
{
Program.ShowProgress("Converting Image ", counter, files.Length);
counter++;
ConvertUtils.ConvertToTga(file.FullName, delSrc);
}
Program.PostProcessing();
}
}
}
20 changes: 20 additions & 0 deletions EffectUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
using System.IO;

namespace MagickUtils
{
class EffectUtils
{


static void Print (string s)
{
Console.WriteLine(s);
}
}
}
Loading

0 comments on commit 7819cbd

Please sign in to comment.