Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions GmodAddonCompressor/Bases/ImageEditBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}


}


1 change: 1 addition & 0 deletions GmodAddonCompressor/DataContexts/ImageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ internal class ImageContext
internal static bool ReduceExactlyToLimits;
internal static bool KeepImageAspectRatio;
internal static bool ImageMagickVTFCompress;
internal static bool RemoveRedundantAlpha;
}
}
11 changes: 11 additions & 0 deletions GmodAddonCompressor/DataContexts/MainWindowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,6 +174,16 @@ public bool ImageMagickVTFCompress
}
}

public bool RemoveRedundantAlpha
{
get { return _removeRedundantAlpha; }
set
{
_removeRedundantAlpha = value;
OnPropertyChanged();
}
}

public bool KeepImageAspectRatio
{
get { return _keepImageAspectRatio; }
Expand Down
2 changes: 1 addition & 1 deletion GmodAddonCompressor/GmodAddonCompressor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="12.0.0" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.5.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
Expand Down
40 changes: 22 additions & 18 deletions GmodAddonCompressor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="34.04"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="260"></ColumnDefinition>
<ColumnDefinition Width="180"></ColumnDefinition>
<ColumnDefinition Width="293"></ColumnDefinition>
<ColumnDefinition Width="147"></ColumnDefinition>
</Grid.ColumnDefinitions>

<Grid Grid.Column="0" Grid.Row="0" Margin="5, 10, 5, 10">
<Grid Grid.Column="0" Grid.Row="0" Margin="5,10,5,10">
<StackPanel Orientation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -88,7 +88,7 @@
</StackPanel>
</Grid>

<Grid Grid.Column="1" Grid.Row="0" Margin="10, 10, 5, 10">
<Grid Grid.Column="1" Grid.Row="0" Margin="10,10,5,10">
<StackPanel Orientation="Vertical">

<StackPanel Orientation="Vertical">
Expand Down Expand Up @@ -140,8 +140,12 @@
</StackPanel>
</Grid>

<Grid Grid.Column="2" Grid.Row="0" Margin="5, 10, 5, 10">
<StackPanel Orientation="Vertical">
<Grid Grid.Column="2" Grid.Row="0" Margin="5,10,5,10">
<Grid.RowDefinitions>
<RowDefinition Height="245*"/>
<RowDefinition Height="54*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" Margin="0,0,0,-21" Grid.RowSpan="2">
<StackPanel Orientation="Horizontal">
<CheckBox
Style="{StaticResource DarkCheckBox}"
Expand Down Expand Up @@ -261,25 +265,25 @@
Margin="5"
IsChecked="{Binding ImageMagickVTFCompress}"
Style="{StaticResource DarkCheckBox}"
Content="ImageMagick VTF compress (Demo)"/>
Content="ImageMagick VTF compress (Demo)" Checked="CheckBox_Checked_1"/>

<CheckBox
Margin="5"
IsChecked="{Binding RemoveRedundantAlpha}"
Style="{StaticResource DarkCheckBox}"
Content="Remove Redundant Alpha (ImageMagic needed)&#xD;&#xA;" Width="284" Checked="CheckBox_Checked"/>

</StackPanel>
</Grid>

<Grid Grid.Column="3" Grid.Row="0" Margin="5, 10, 5, 10">
<Grid Grid.Column="3" Grid.Row="0" Margin="5,10,5,10">
<StackPanel Orientation="Vertical">
<CheckBox
Style="{StaticResource DarkCheckBox}"
Margin="20, 5, 0, 5"
Content="LUA"
HorizontalAlignment="Left"
IsChecked="{Binding CompressLUA}"/>

<CheckBox
Style="{StaticResource DarkCheckBox}"
Margin="20, 5, 0, 5"
Content="Change original code"
HorizontalAlignment="Left"
IsChecked="{Binding ChangeOriginalCodeToMinimalistic}"/>
Style="{StaticResource DarkCheckBox}" IsChecked="{Binding ChangeOriginalCodeToMinimalistic}" Content="Change original code"/>
</StackPanel>
</Grid>

Expand All @@ -303,7 +307,7 @@

<TextBlock
Style="{StaticResource DarkTextBlock}"
Text="Author: Shark_vil"
Text="Author: Shark_vil, zeThijs"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</StackPanel>
Expand Down
13 changes: 12 additions & 1 deletion GmodAddonCompressor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{

}
}
}
4 changes: 4 additions & 0 deletions GmodAddonCompressor/Objects/VTFEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down