Skip to content

Commit

Permalink
add new tool: Lossless compression #1518
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Jan 6, 2024
1 parent ae5809a commit 4dca945
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Source/Components/ImageGlass.Base/BHelper/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static string FormatSize(long size)
var mod = 1024d;
var sized = size * 1d;

var units = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };
var units = new string[] { "bytes", "KB", "MB", "GB", "TB", "PB" };
int i;
for (i = 0; sized > mod; i++)
{
Expand Down
6 changes: 6 additions & 0 deletions Source/Components/ImageGlass.Base/Language/IgLang.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ public void InitDefaultLanguage()
_ = TryAdd("FrmMain.MnuFrameNav", "Frame navigation"); // v7.5
_ = TryAdd("FrmMain.MnuCropTool", "Crop image"); // v7.6
_ = TryAdd("FrmMain.MnuGetMoreTools", "Get more tools…"); // v9.0

_ = TryAdd("FrmMain.MnuLosslessCompression", "Lossless compression"); // v9.0
_ = TryAdd("FrmMain.MnuLosslessCompression._Description", "This tool uses Magick.NET library for lossless compression, optimizing file size. Overwrites only if the compressed file is smaller than the original.\r\n\r\nDo you want to proceed?"); // v9.0
_ = TryAdd("FrmMain.MnuLosslessCompression._Compressing", "Performing lossless compression…"); // v9.0
_ = TryAdd("FrmMain.MnuLosslessCompression._Done", "Done lossless compression.\r\nThe new file size is {0}, saved {1}."); // v9.0

#endregion

_ = TryAdd("FrmMain.MnuSettings", "Settings"); // v3.0
Expand Down
38 changes: 37 additions & 1 deletion Source/Components/ImageGlass.Base/Photoing/Codecs/PhotoCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public static async Task SaveAsync(string srcFileName, string destFilePath, Code
var metadata = LoadMetadata(srcFileName, readOptions);
if (!metadata.SupportsWriting)
{
throw new FileFormatException("Unsupported image format.");
throw new FileFormatException("IGE_001: Unsupported image format.");
}

var settings = ParseSettings(readOptions, true, srcFileName);
Expand Down Expand Up @@ -673,6 +673,42 @@ public static void InitMagickNET()
ResourceLimits.LimitMemory(new Percentage(75));
}


/// <summary>
/// Checks if the supplied file name is supported for lossless compression using Magick.NET.
/// </summary>
public static bool IsLosslessCompressSupported(string? filePath)
{
var opt = new ImageOptimizer()
{
OptimalCompression = true,
};

return opt.IsSupported(filePath);
}

/// <summary>
/// Performs lossless compression on the specified file using Magick.NET.
/// If the new file size is not smaller, the file won't be overwritten.
/// </summary>
/// <returns>True when the image could be compressed otherwise false.</returns>
/// <exception cref="NotSupportedException"></exception>
public static bool LosslessCompress(string? filePath)
{
if (string.IsNullOrWhiteSpace(filePath)) return false;

var fi = new FileInfo(filePath);
var opt = new ImageOptimizer()
{
OptimalCompression = true,
};

// check if the format is supported
if (!opt.IsSupported(fi)) throw new NotSupportedException("IGE_002: Unsupported image format.");

return opt.LosslessCompress(fi);
}

#endregion // Public functions


Expand Down
1 change: 1 addition & 0 deletions Source/Components/ImageGlass.Base/Types/IgCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static class IgCommands
public static string REMOVE_DEFAULT_PHOTO_VIEWER => "remove-default-viewer";
public static string START_SLIDESHOW => "start-slideshow";
public static string EXPORT_FRAMES => "export-frames";
public static string LOSSLESS_COMPRESS => "lossless-compress";


public static string QUICK_SETUP => "quick-setup";
Expand Down
63 changes: 36 additions & 27 deletions Source/ImageGlass/FrmMain.Designer.cs

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

5 changes: 5 additions & 0 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,11 @@ private void MnuFrameNav_Click(object sender, EventArgs e)
IG_ToggleFrameNavTool();
}

private void MnuLosslessCompression_Click(object sender, EventArgs e)
{
IG_LosslessCompression();
}

private void MnuGetMoreTools_Click(object sender, EventArgs e)
{
_ = BHelper.OpenUrlAsync("https://imageglass.org/tools", "from_get_more_tools");
Expand Down
3 changes: 3 additions & 0 deletions Source/ImageGlass/FrmMain.resx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@
<metadata name="MnuSubMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>637, 21</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>46</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAQAEBAAAAEAIABoBAAARgAAABgYAAABACAAiAkAAK4EAAAgIAAAAQAgAKgQAAA2DgAAMDAAAAEA
Expand Down
5 changes: 4 additions & 1 deletion Source/ImageGlass/FrmMain/FrmMain.Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public partial class FrmMain
// MnuTools
{ nameof(MnuColorPicker), [new(Keys.K)] },
{ nameof(MnuCropTool), [new(Keys.C)] },
{ nameof(MnuFrameNav), [new(Keys.P)] },
{ nameof(MnuFrameNav), [new(Keys.P)] },
{ nameof(MnuLosslessCompression), [new(Keys.Alt | Keys.C)] },
{ Const.IGTOOL_EXIFTOOL, [new(Keys.X)] },

// MnuHelp
Expand Down Expand Up @@ -747,6 +748,7 @@ public void LoadLanguage()
MnuColorPicker.Text = lang[$"{Name}.{nameof(MnuColorPicker)}"];
MnuFrameNav.Text = lang[$"{Name}.{nameof(MnuFrameNav)}"];
MnuCropTool.Text = lang[$"{Name}.{nameof(MnuCropTool)}"];
MnuLosslessCompression.Text = lang[$"{Name}.{nameof(MnuLosslessCompression)}"];
MnuGetMoreTools.Text = lang[$"{Name}.{nameof(MnuGetMoreTools)}"];

foreach (var item in MnuTools.DropDownItems)
Expand Down Expand Up @@ -1046,6 +1048,7 @@ public void LoadExternalTools()
nameof(MnuColorPicker),
nameof(MnuCropTool),
nameof(MnuFrameNav),
nameof(MnuLosslessCompression),
nameof(MnuExternalToolsSeparator),
nameof(MnuGetMoreTools),
};
Expand Down
Loading

0 comments on commit 4dca945

Please sign in to comment.