diff --git a/ShareX.HelpersLib/ConvolutionMatrix.cs b/ShareX.HelpersLib/ConvolutionMatrix.cs index e7573a927..5dcaf472d 100644 --- a/ShareX.HelpersLib/ConvolutionMatrix.cs +++ b/ShareX.HelpersLib/ConvolutionMatrix.cs @@ -33,6 +33,8 @@ public class ConvolutionMatrix public int Height => matrix.GetLength(0); public byte Offset { get; set; } + public bool ConsiderAlpha { get; set; } + public ConvolutionMatrix() : this(3) { } diff --git a/ShareX.HelpersLib/ConvolutionMatrixManager.cs b/ShareX.HelpersLib/ConvolutionMatrixManager.cs index c825b3ffb..79f9d13c0 100644 --- a/ShareX.HelpersLib/ConvolutionMatrixManager.cs +++ b/ShareX.HelpersLib/ConvolutionMatrixManager.cs @@ -29,6 +29,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; +using System.Threading.Tasks; namespace ShareX.HelpersLib { @@ -44,13 +45,14 @@ public static Image Apply(this ConvolutionMatrix kernel, Image img) int originX = (kernel.Width - 1) / 2; int originY = (kernel.Height - 1) / 2; - for (int y = 0; y < source.Height; y++) + Parallel.For(0, source.Height, y => { - for (int x = 0; x < source.Width; x++) + Parallel.For(0, source.Width, x => { double r = 0.0; double g = 0.0; double b = 0.0; + double a = 0.0; // Apply each matrix multiplier to the color components for each pixel. for (int fy = 0; fy < kernel.Height; fy++) @@ -72,6 +74,10 @@ public static Image Apply(this ConvolutionMatrix kernel, Image img) r += kernel[fy, fx] * currentColor.Red; g += kernel[fy, fx] * currentColor.Green; b += kernel[fy, fx] * currentColor.Blue; + if (kernel.ConsiderAlpha) + { + a += kernel[fy, fx] * currentColor.Alpha; + } } } @@ -84,9 +90,26 @@ public static Image Apply(this ConvolutionMatrix kernel, Image img) b += kernel.Offset; b.Clamp(0, 255); - dest.SetPixel(x, y, new ColorBgra((byte)b, (byte)g, (byte)r, source.GetPixel(x, y).Alpha)); - } - } + if (kernel.ConsiderAlpha) + { + a += kernel.Offset; + a.Clamp(0, 255); + } + + dest.SetPixel( + x, + y, + new ColorBgra( + (byte)b, + (byte)g, + (byte)r, + kernel.ConsiderAlpha + ? (byte)a + : source.GetPixel(x, y).Alpha + ) + ); + }); + }); } return result; @@ -115,6 +138,7 @@ private static double GaussianFunction(double x, double sigma) public static ConvolutionMatrix GaussianBlur(int height, int width, double sigma) { ConvolutionMatrix cm = new ConvolutionMatrix(height, width); + cm.ConsiderAlpha = true; double sum = 0.0; double midpointX = (width - 1) / 2.0; diff --git a/ShareX.ImageEffectsLib/Filters/GaussianBlur.cs b/ShareX.ImageEffectsLib/Filters/GaussianBlur.cs index 6119e96ee..1819c0425 100644 --- a/ShareX.ImageEffectsLib/Filters/GaussianBlur.cs +++ b/ShareX.ImageEffectsLib/Filters/GaussianBlur.cs @@ -67,7 +67,11 @@ public override Image Apply(Image img) { ConvolutionMatrix kernelHoriz = ConvolutionMatrixManager.GaussianBlur(1, size, sigma); - ConvolutionMatrix kernelVert = new ConvolutionMatrix(size, 1); + ConvolutionMatrix kernelVert = new ConvolutionMatrix(size, 1) + { + ConsiderAlpha = kernelHoriz.ConsiderAlpha + }; + for (int i = 0; i < size; i++) { kernelVert[i, 0] = kernelHoriz[0, i];