diff --git a/GmodAddonCompressor/Bases/ImageEditBase.cs b/GmodAddonCompressor/Bases/ImageEditBase.cs index 0753cd2..b9f1202 100644 --- a/GmodAddonCompressor/Bases/ImageEditBase.cs +++ b/GmodAddonCompressor/Bases/ImageEditBase.cs @@ -273,6 +273,9 @@ private async Task SaveMagickImage(string imageSourcePath, string imageSavePath) { try { + if (ImageContext.RemoveRedundantAlpha) + await RemoveRedundantAlpha(image); + var size = new MagickGeometry(resizeWidth, resizeHeight); size.IgnoreAspectRatio = isSingleColor ? true : !ImageContext.KeepImageAspectRatio; @@ -333,5 +336,52 @@ private async Task SaveMagickImage(string imageSourcePath, string imageSavePath) File.Copy(additionalCompressionFilePath, imageSavePath); } } + + /* + * The Following checks if an alpha is redundant by the alpha's deviation + * A Value of 0 or NaN indicates a solid, unused alpha + * Alpha is then disabled: drastically reducing filesize + */ + private async Task RemoveRedundantAlpha(MagickImage image) + { + _logger.LogInformation($"Attempting to remove redundant alphas.."); + try + { + if (!image.HasAlpha) + return; + + double dev = GetStandardDeviation(image, PixelChannel.Alpha); + if (dev == 0 || Double.IsNaN(dev)) + { + image.Alpha(AlphaOption.Off); + } + } + catch (Exception ex) + { + if(ex==null) + _logger.LogError("Something went wrong removing redundant alphas"); + else + _logger.LogError(ex.ToString()); + } + + } + + private double GetStandardDeviation(MagickImage image, PixelChannel channel) + { + // Get the statistics of the alpha channel + var statistics = image.Statistics(); + + var alphaStatistics = statistics.GetChannel(PixelChannel.Alpha); + + // The StandardDeviation property gives you the standard deviation + if (alphaStatistics == null) + return -1; + else + return alphaStatistics.StandardDeviation; + } } + + } + + diff --git a/GmodAddonCompressor/DataContexts/ImageContext.cs b/GmodAddonCompressor/DataContexts/ImageContext.cs index cfe5f1c..1a56b07 100644 --- a/GmodAddonCompressor/DataContexts/ImageContext.cs +++ b/GmodAddonCompressor/DataContexts/ImageContext.cs @@ -10,5 +10,6 @@ internal class ImageContext internal static bool ReduceExactlyToLimits; internal static bool KeepImageAspectRatio; internal static bool ImageMagickVTFCompress; + internal static bool RemoveRedundantAlpha; } } diff --git a/GmodAddonCompressor/DataContexts/MainWindowContext.cs b/GmodAddonCompressor/DataContexts/MainWindowContext.cs index 583c9b5..5e7905c 100644 --- a/GmodAddonCompressor/DataContexts/MainWindowContext.cs +++ b/GmodAddonCompressor/DataContexts/MainWindowContext.cs @@ -24,6 +24,7 @@ internal class MainWindowContext : INotifyPropertyChanged private bool _reduceExactlyToResolution = true; private bool _keepImageAspectRatio = true; private bool _imageMagickVTFCompress = false; + private bool _removeRedundantAlpha = false; private uint _imageSkipWidth = 0; private uint _imageSkipHeight = 0; private int _wavRate = 22050; @@ -173,6 +174,16 @@ public bool ImageMagickVTFCompress } } + public bool RemoveRedundantAlpha + { + get { return _removeRedundantAlpha; } + set + { + _removeRedundantAlpha = value; + OnPropertyChanged(); + } + } + public bool KeepImageAspectRatio { get { return _keepImageAspectRatio; } diff --git a/GmodAddonCompressor/GmodAddonCompressor.csproj b/GmodAddonCompressor/GmodAddonCompressor.csproj index 7327fe2..ce07ab9 100644 --- a/GmodAddonCompressor/GmodAddonCompressor.csproj +++ b/GmodAddonCompressor/GmodAddonCompressor.csproj @@ -13,7 +13,7 @@ - + diff --git a/GmodAddonCompressor/MainWindow.xaml b/GmodAddonCompressor/MainWindow.xaml index b2193a0..00a1b2d 100644 --- a/GmodAddonCompressor/MainWindow.xaml +++ b/GmodAddonCompressor/MainWindow.xaml @@ -31,16 +31,16 @@ - + - - + + - + @@ -88,7 +88,7 @@ - + @@ -140,8 +140,12 @@ - - + + + + + + + Content="ImageMagick VTF compress (Demo)" Checked="CheckBox_Checked_1"/> + + + - + - + Style="{StaticResource DarkCheckBox}" IsChecked="{Binding ChangeOriginalCodeToMinimalistic}" Content="Change original code"/> @@ -303,7 +307,7 @@ diff --git a/GmodAddonCompressor/MainWindow.xaml.cs b/GmodAddonCompressor/MainWindow.xaml.cs index 1d8a09b..f3fc6e2 100644 --- a/GmodAddonCompressor/MainWindow.xaml.cs +++ b/GmodAddonCompressor/MainWindow.xaml.cs @@ -10,7 +10,7 @@ namespace GmodAddonCompressor public partial class MainWindow : Window { private MainWindowContext _context = new MainWindowContext(); - private const string _version = "v2.0.4"; + private const string _version = "v2.0.5"; public MainWindow() { @@ -66,6 +66,7 @@ private async Task StartCompressProcess(string addonDirectoryPath) ImageContext.ReduceExactlyToLimits = _context.ReduceExactlyToLimits; ImageContext.KeepImageAspectRatio = _context.KeepImageAspectRatio; ImageContext.ImageMagickVTFCompress = _context.ImageMagickVTFCompress; + ImageContext.RemoveRedundantAlpha = _context.RemoveRedundantAlpha; LuaContext.ChangeOriginalCodeToMinimalistic = _context.ChangeOriginalCodeToMinimalistic; var compressSystem = new CompressAddonSystem(addonDirectoryPath); @@ -113,5 +114,15 @@ private void CheckBox_DisableDebugConsole(object sender, RoutedEventArgs e) { ConsoleHelper.FreeConsole(); } + + private void CheckBox_Checked(object sender, RoutedEventArgs e) + { + + } + + private void CheckBox_Checked_1(object sender, RoutedEventArgs e) + { + + } } } diff --git a/GmodAddonCompressor/Objects/VTFEdit.cs b/GmodAddonCompressor/Objects/VTFEdit.cs index 2a07f80..5d60663 100644 --- a/GmodAddonCompressor/Objects/VTFEdit.cs +++ b/GmodAddonCompressor/Objects/VTFEdit.cs @@ -8,9 +8,12 @@ using ImageMagick; using Microsoft.Extensions.Logging; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; +using System.Linq; +using System.Threading.Channels; using System.Threading.Tasks; namespace GmodAddonCompressor.Objects @@ -245,6 +248,7 @@ private async Task VtfToPng(VtfFileModel vtfInfo, string vtfFilePath, string vtf await VtfToImage(vtfInfo, "png", vtfFilePath, vtfDirectory); } + private async Task OptImageToVtf(VtfFileModel vtfInfo, string imageFilePath, string? pngDirectory = null) { if (string.IsNullOrEmpty(pngDirectory))