From 3ee4126e57f36bd07eb5c4c7b54f3f6d877ea5b5 Mon Sep 17 00:00:00 2001 From: zeThijs Date: Sat, 6 Jan 2024 02:53:41 +0100 Subject: [PATCH 1/2] Add option to remove redundant, unused, alpha channels --- .vscode/launch.json | 26 ++++++++++ .vscode/tasks.json | 41 +++++++++++++++ GmodAddonCompressor/Bases/ImageEditBase.cs | 50 +++++++++++++++++++ .../DataContexts/ImageContext.cs | 1 + .../DataContexts/MainWindowContext.cs | 11 ++++ .../GmodAddonCompressor.csproj | 2 +- GmodAddonCompressor/MainWindow.xaml | 40 ++++++++------- GmodAddonCompressor/MainWindow.xaml.cs | 13 ++++- GmodAddonCompressor/Objects/VTFEdit.cs | 4 ++ 9 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cd7d00c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/GmodAddonCompressor/bin/Debug/net6.0-windows/GmodAddonCompressor.dll", + "args": [], + "cwd": "${workspaceFolder}/GmodAddonCompressor", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4c3387e --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file 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)) From 58dcf5e6d25558ad1e9a3acb19a160b0fc476633 Mon Sep 17 00:00:00 2001 From: zeThijs Date: Sat, 6 Jan 2024 02:59:51 +0100 Subject: [PATCH 2/2] remove vscode dir --- .vscode/launch.json | 26 -------------------------- .vscode/tasks.json | 41 ----------------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index cd7d00c..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/GmodAddonCompressor/bin/Debug/net6.0-windows/GmodAddonCompressor.dll", - "args": [], - "cwd": "${workspaceFolder}/GmodAddonCompressor", - // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 4c3387e..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/GmodAddonCompressor/GmodAddonCompressor.csproj" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file