Skip to content

Commit

Permalink
Added HEIF support, GUI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
N00MKRAD committed Oct 2, 2020
1 parent f362121 commit f2314f7
Show file tree
Hide file tree
Showing 37 changed files with 55,329 additions and 84 deletions.
Binary file modified .vs/MagickUtils/v16/.suo
Binary file not shown.
13 changes: 10 additions & 3 deletions FormsHelpers/ConvertTabHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MagickUtils.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -12,10 +13,10 @@ class ConvertTabHelper
{
public static void ConvertFileList (string[] files, ComboBox qualityCombox, ComboBox qualityMaxCombox, IF selectedFormat, CheckBox delSrcCbox)
{
int qMin = int.Parse(qualityCombox.Text.Trim());
int qMin = qualityCombox.GetInt();
int qMax = qMin;
if(!string.IsNullOrWhiteSpace(qualityMaxCombox.Text.Trim()))
qMax = int.Parse(qualityMaxCombox.Text.Trim());
qMax = qualityMaxCombox.GetInt();

foreach(string file in files)
{
Expand Down Expand Up @@ -51,6 +52,9 @@ public static void ConvertFileList (string[] files, ComboBox qualityCombox, Comb

if (selectedFormat == IF.AVIF)
ConvertUtils.ConvertToAvif(file, qMin, delSrcCbox.Checked);

if (selectedFormat == IF.HEIF)
HeifInterface.EncodeImage(file, qMin, delSrcCbox.Checked);
}
}
}
Expand Down Expand Up @@ -91,6 +95,9 @@ public static void ConvertUsingPath (ComboBox qualityCombox, ComboBox qualityMax

if (selectedFormat == IF.AVIF)
ConvertUtils.ConvertDirToAvif(qMin, delSrcCbox.Checked);

if (selectedFormat == IF.HEIF)
ConvertUtils.ConvertDirToHeif(qMin, delSrcCbox.Checked);
}
}
}
18 changes: 15 additions & 3 deletions Interfaces/FlifInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public static string EncodeImage (string path, int q, bool deleteSrc)
flifProcess.WaitForExit();
Program.Print("Done converting " + path);
if(deleteSrc)
File.Delete(path);
DelSource(path, outPath);
return outPath;
}

public static string DecodeImage (string path, bool deleteSrc, bool tempExtension = false)
{
ExtractExe();
Expand All @@ -66,8 +67,8 @@ public static string DecodeImage (string path, bool deleteSrc, bool tempExtensio
flifProcess.Start();
flifProcess.WaitForExit();
Program.Print("Done converting " + path);
if(deleteSrc)
File.Delete(path);
if (deleteSrc)
DelSource(path, outPath);
return outPath;
}

Expand All @@ -78,5 +79,16 @@ public static MagickImage DecodeToMagickImage (string path, bool deleteSrc)
File.Delete(outPath);
return image;
}

static void DelSource(string sourcePath, string newPath)
{
if (Path.GetExtension(sourcePath).ToLower() == Path.GetExtension(newPath).ToLower())
{
Program.Print("-> Not deleting " + Path.GetFileName(sourcePath) + " as it was overwritten");
return;
}
Program.Print("-> Deleting source file: " + Path.GetFileName(sourcePath) + "...");
File.Delete(sourcePath);
}
}
}
69 changes: 69 additions & 0 deletions Interfaces/HeifInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Ionic.Zip;
using MagickUtils.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MagickUtils.Interfaces
{
class HeifInterface
{
static string heifExePath;
static string heifResPath;

public static void Extract(bool overwrite = false)
{
GetPaths();

if (File.Exists(heifExePath) && !overwrite)
return;

File.WriteAllBytes(heifResPath, Resources.heif);
ZipFile zip = ZipFile.Read(heifResPath);
foreach (ZipEntry e in zip)
e.Extract(IOUtils.GetAppDataDir(), ExtractExistingFileAction.OverwriteSilently);

Program.Print("[HeifInterface] Extratced HEIF resources to " + heifExePath);
}

static void GetPaths()
{
heifResPath = Path.Combine(IOUtils.GetAppDataDir(), "heif.zip");
heifExePath = Path.Combine(IOUtils.GetAppDataDir(), "heif", "heif-enc.exe");
}

public static string EncodeImage (string path, int q, bool deleteSrc)
{
Extract();
string outPath = Path.ChangeExtension(path, null) + ".heic";
ProcessStartInfo psi;
string args = " -q " + q + " -o " + outPath.WrapPath(true, true) + path.WrapPath(true, true);
psi = new ProcessStartInfo { FileName = heifExePath, Arguments = args };
psi.WorkingDirectory = Path.GetDirectoryName(heifExePath);
Program.Print("HEIF args:" + args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process heifProcess = new Process { StartInfo = psi };
heifProcess.Start();
heifProcess.WaitForExit();
Program.Print("Done converting " + path);
if (deleteSrc)
DelSource(path, outPath);
return outPath;
}

static void DelSource(string sourcePath, string newPath)
{
if (Path.GetExtension(sourcePath).ToLower() == Path.GetExtension(newPath).ToLower())
{
Program.Print("-> Not deleting " + Path.GetFileName(sourcePath) + " as it was overwritten");
return;
}
Program.Print("-> Deleting source file: " + Path.GetFileName(sourcePath) + "...");
File.Delete(sourcePath);
}
}
}
5 changes: 5 additions & 0 deletions MagickUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
<Reference Include="DdsFileTypePlus, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\DdsFileTypePlusHack.1.0.4\lib\net45\DdsFileTypePlus.dll</HintPath>
</Reference>
<Reference Include="DotNetZip, Version=1.13.8.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>packages\DotNetZip.1.13.8\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="Magick.NET-Q16-AnyCPU, Version=7.22.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>packages\Magick.NET-Q16-AnyCPU.7.22.0\lib\net40\Magick.NET-Q16-AnyCPU.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -122,6 +125,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Interfaces\HeifInterface.cs" />
<Compile Include="MagickUtils\AdjustUtils.cs" />
<Compile Include="MagickUtils\ColorUtils.cs" />
<Compile Include="MagickUtils\GeometryUtils.cs" />
Expand Down Expand Up @@ -200,6 +204,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Resources\gear.bmp" />
<None Include="Resources\heif.zip" />
<None Include="Resources\questmark-72px-bordeer.png" />
<None Include="Resources\questmark.png" />
<None Include="Resources\inpaint-thinlines1-2048px.png" />
Expand Down
32 changes: 18 additions & 14 deletions MagickUtils/ConvertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using ImageMagick.Formats.Dds;
using System.Reflection;
using MagickUtils.Interfaces;

namespace MagickUtils
{
Expand Down Expand Up @@ -196,6 +197,22 @@ public static void ConvertToAvif(string path, int q, bool delSource = false)
PostProcessing(img, path, outPath, delSource);
}

public static async void ConvertDirToHeif(int q, bool delSrc)
{
int counter = 1;
FileInfo[] files = IOUtils.GetFiles();

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

static long bytesPre;

public static void ConvertToJpeg (string path, int q = 95, bool delSource = false)
Expand Down Expand Up @@ -247,19 +264,6 @@ public static void ConvertToPngIM (string path, int q = 50, bool delSource = fal
PostProcessing(img, path, outPath, delSource);
}

public static async Task ConvertToPngMT(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";
PreProcessing(path);
await Task.Delay(1);
img.Write(outPath);
PostProcessing(img, path, outPath, delSource);
}

public static void ConvertToDds (string path, bool delSource = false)
{
MagickImage img = IOUtils.ReadImage(path);
Expand Down Expand Up @@ -328,7 +332,7 @@ static void PostProcessing (MagickImage img, string sourcePath, string outPath,

static void DelSource (string sourcePath, string newPath)
{
if(Path.GetExtension(sourcePath) == Path.GetExtension(newPath))
if(Path.GetExtension(sourcePath).ToLower() == Path.GetExtension(newPath).ToLower())
{
Program.Print("-> Not deleting " + Path.GetFileName(sourcePath) + " as it was overwritten");
return;
Expand Down
Loading

0 comments on commit f2314f7

Please sign in to comment.