diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 2c84c1f5..cccc08d6 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -47,8 +47,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internal EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.ShareX", "samples\ComputeSharp.ShareX\ComputeSharp.ShareX.csproj", "{E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}" -EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 @@ -174,14 +172,6 @@ Global {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.Build.0 = Release|Any CPU {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.ActiveCfg = Release|Any CPU {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.Build.0 = Release|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Debug|x64.ActiveCfg = Debug|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Debug|x64.Build.0 = Debug|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Release|Any CPU.Build.0 = Release|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Release|x64.ActiveCfg = Release|Any CPU - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -197,7 +187,6 @@ Global {8CEFC324-CA6C-4845-9AD9-70B92BD87057} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {BF52326F-E415-49E6-8595-62762216762B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} - {E200B2A8-6BC4-4F50-9D4F-7BAF06F1813A} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj b/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj deleted file mode 100644 index 00562633..00000000 --- a/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - - Exe - net5.0 - true - enable - - - - - - - - - - - - - - - - diff --git a/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs b/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs deleted file mode 100644 index 70dff53d..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs +++ /dev/null @@ -1,120 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/ColorBgra.cs - -using System.Drawing; -using System.Runtime.InteropServices; - -namespace ShareX.HelpersLib -{ - [StructLayout(LayoutKind.Explicit)] - public struct ColorBgra - { - [FieldOffset(0)] - public uint Bgra; - - [FieldOffset(0)] - public byte Blue; - [FieldOffset(1)] - public byte Green; - [FieldOffset(2)] - public byte Red; - [FieldOffset(3)] - public byte Alpha; - - public const byte SizeOf = 4; - - public ColorBgra(uint bgra) : this() - { - Bgra = bgra; - } - - public ColorBgra(byte b, byte g, byte r, byte a = 255) : this() - { - Blue = b; - Green = g; - Red = r; - Alpha = a; - } - - public ColorBgra(Color color) : this(color.B, color.G, color.R, color.A) - { - } - - public static bool operator ==(ColorBgra c1, ColorBgra c2) - { - return c1.Bgra == c2.Bgra; - } - - public static bool operator !=(ColorBgra c1, ColorBgra c2) - { - return c1.Bgra != c2.Bgra; - } - - public override bool Equals(object? obj) - { - return obj is ColorBgra && ((ColorBgra)obj).Bgra == Bgra; - } - - public override int GetHashCode() - { - unchecked - { - return (int)Bgra; - } - } - - public static implicit operator ColorBgra(uint color) - { - return new ColorBgra(color); - } - - public static implicit operator uint(ColorBgra color) - { - return color.Bgra; - } - - public Color ToColor() - { - return Color.FromArgb(Alpha, Red, Green, Blue); - } - - public override string ToString() - { - return string.Format("B: {0}, G: {1}, R: {2}, A: {3}", Blue, Green, Red, Alpha); - } - - public static uint BgraToUInt32(uint b, uint g, uint r, uint a) - { - return b + (g << 8) + (r << 16) + (a << 24); - } - - public static uint BgraToUInt32(byte b, byte g, byte r, byte a) - { - return b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24); - } - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrix.cs b/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrix.cs deleted file mode 100644 index d6cbc103..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrix.cs +++ /dev/null @@ -1,66 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/ConvolutionMatrix.cs - -namespace ShareX.HelpersLib -{ - public class ConvolutionMatrix - { - private readonly double[,] matrix; - - public int Width => matrix.GetLength(1); - public int Height => matrix.GetLength(0); - public byte Offset { get; set; } - - public bool ConsiderAlpha { get; set; } - - public ConvolutionMatrix() : this(3) - { - } - - public ConvolutionMatrix(int size) : this(size, size) - { - } - - public ConvolutionMatrix(int height, int width) - { - matrix = new double[height, width]; - } - - public void SetAll(double value) - { - for (int y = 0; y < Height; y++) - { - for (int x = 0; x < Width; x++) - { - matrix[y, x] = value; - } - } - } - - public ref double this[int y, int x] => ref matrix[y, x]; - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrixManager.cs b/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrixManager.cs deleted file mode 100644 index d74fa361..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrixManager.cs +++ /dev/null @@ -1,148 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/ConvolutionMatrixManager.cs -// Adapted from: https://stackoverflow.com/questions/33569396/correctly-implement-a-2-pass-gaussian-blur -// Filters: http://www.codeproject.com/Articles/2008/Image-Processing-for-Dummies-with-C-and-GDI-Part-2 - -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Threading.Tasks; - -namespace ShareX.HelpersLib -{ - public static class ConvolutionMatrixManager - { - public static Bitmap Apply(this ConvolutionMatrix kernel, Bitmap bmp) - { - Bitmap bmpResult = (Bitmap)bmp.Clone(); - - using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly)) - using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly)) - { - int originX = (kernel.Width - 1) / 2; - int originY = (kernel.Height - 1) / 2; - - Parallel.For(0, source.Height, y => - { - 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++) - { - int fyr = fy - originY; - int offsetY = y + fyr; - - offsetY = offsetY.Clamp(0, source.Height - 1); - - for (int fx = 0; fx < kernel.Width; fx++) - { - int fxr = fx - originX; - int offsetX = x + fxr; - - offsetX = offsetX.Clamp(0, source.Width - 1); - - ColorBgra currentColor = source.GetPixel(offsetX, offsetY); - - 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; - } - } - } - - r += kernel.Offset; - r = r.Clamp(0, 255); - - g += kernel.Offset; - g = g.Clamp(0, 255); - - b += kernel.Offset; - b = b.Clamp(0, 255); - - if (kernel.ConsiderAlpha) - { - a += kernel.Offset; - a = 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 bmpResult; - } - - private static double GaussianFunction(double x, double sigma) - { - double left = 1.0 / (Math.Sqrt(2 * Math.PI) * sigma); - - double exponentNumerator = -x * x; - double exponentDenominator = 2 * Math.Pow(sigma, 2); - double right = Math.Exp(exponentNumerator / exponentDenominator); - - return left * right; - } - - 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; - double midpointY = (height - 1) / 2.0; - - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - sum += cm[y, x] = GaussianFunction(x - midpointX, sigma) * GaussianFunction(y - midpointY, sigma); - } - } - - // Normalise kernel so that the sum of all weights equals 1 - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - cm[y, x] /= sum; - } - } - - return cm; - } - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Cpu/MathHelpers.cs b/samples/ComputeSharp.ShareX/Cpu/MathHelpers.cs deleted file mode 100644 index c7c3f753..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/MathHelpers.cs +++ /dev/null @@ -1,41 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/Helpers/MathHelpers.cs - -using System; - -namespace ShareX.HelpersLib -{ - public static class MathHelpers - { - public static T Clamp(T num, T min, T max) where T : IComparable - { - if (num.CompareTo(min) <= 0) return min; - if (num.CompareTo(max) >= 0) return max; - return num; - } - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Cpu/NumberExtensions.cs b/samples/ComputeSharp.ShareX/Cpu/NumberExtensions.cs deleted file mode 100644 index 11a0cd20..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/NumberExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/Extensions/NumberExtensions.cs - -using System; - -namespace ShareX.HelpersLib -{ - public static class NumberExtensions - { - public static T Clamp(this T num, T min, T max) where T : IComparable - { - return MathHelpers.Clamp(num, min, max); - } - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Cpu/UnsafeBitmap.cs b/samples/ComputeSharp.ShareX/Cpu/UnsafeBitmap.cs deleted file mode 100644 index 64965d39..00000000 --- a/samples/ComputeSharp.ShareX/Cpu/UnsafeBitmap.cs +++ /dev/null @@ -1,192 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2020 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -// Source: https://github.com/ShareX/ShareX/blob/master/ShareX.HelpersLib/UnsafeBitmap.cs - -using System; -using System.Drawing; -using System.Drawing.Imaging; - -namespace ShareX.HelpersLib -{ - public unsafe class UnsafeBitmap : IDisposable - { - public ColorBgra* Pointer { get; private set; } - public bool IsLocked { get; private set; } - public int Width { get; private set; } - public int Height { get; private set; } - - public int PixelCount => Width * Height; - - private Bitmap bitmap; - private BitmapData? bitmapData; - - public UnsafeBitmap(Bitmap bitmap, bool lockBitmap = false, ImageLockMode imageLockMode = ImageLockMode.ReadWrite) - { - this.bitmap = bitmap; - Width = bitmap.Width; - Height = bitmap.Height; - - if (lockBitmap) - { - Lock(imageLockMode); - } - } - - public void Lock(ImageLockMode imageLockMode = ImageLockMode.ReadWrite) - { - if (!IsLocked) - { - IsLocked = true; - bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), imageLockMode, PixelFormat.Format32bppArgb); - Pointer = (ColorBgra*)bitmapData.Scan0.ToPointer(); - } - } - - public void Unlock() - { - if (IsLocked) - { - bitmap.UnlockBits(bitmapData!); - bitmapData = null; - Pointer = null; - IsLocked = false; - } - } - - public static bool operator ==(UnsafeBitmap bmp1, UnsafeBitmap bmp2) - { - return ReferenceEquals(bmp1, bmp2) || bmp1.Equals(bmp2); - } - - public static bool operator !=(UnsafeBitmap bmp1, UnsafeBitmap bmp2) - { - return !(bmp1 == bmp2); - } - - public override bool Equals(object? obj) - { - return obj is UnsafeBitmap && Compare((UnsafeBitmap)obj, this); - } - - public override int GetHashCode() - { - return PixelCount; - } - - public static bool Compare(UnsafeBitmap bmp1, UnsafeBitmap bmp2) - { - int pixelCount = bmp1.PixelCount; - - if (pixelCount != bmp2.PixelCount) - { - return false; - } - - bmp1.Lock(ImageLockMode.ReadOnly); - bmp2.Lock(ImageLockMode.ReadOnly); - - ColorBgra* pointer1 = bmp1.Pointer; - ColorBgra* pointer2 = bmp2.Pointer; - - for (int i = 0; i < pixelCount; i++) - { - if (pointer1->Bgra != pointer2->Bgra) - { - return false; - } - - pointer1++; - pointer2++; - } - - return true; - } - - public bool IsTransparent() - { - int pixelCount = PixelCount; - - ColorBgra* pointer = Pointer; - - for (int i = 0; i < pixelCount; i++) - { - if (pointer->Alpha < 255) - { - return true; - } - - pointer++; - } - - return false; - } - - public ColorBgra GetPixel(int i) - { - return Pointer[i]; - } - - public ColorBgra GetPixel(int x, int y) - { - return Pointer[x + (y * Width)]; - } - - public void SetPixel(int i, ColorBgra color) - { - Pointer[i] = color; - } - - public void SetPixel(int i, uint color) - { - Pointer[i] = color; - } - - public void SetPixel(int x, int y, ColorBgra color) - { - Pointer[x + (y * Width)] = color; - } - - public void SetPixel(int x, int y, uint color) - { - Pointer[x + (y * Width)] = color; - } - - public void ClearPixel(int i) - { - Pointer[i] = 0; - } - - public void ClearPixel(int x, int y) - { - Pointer[x + (y * Width)] = 0; - } - - public void Dispose() - { - Unlock(); - } - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs b/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs deleted file mode 100644 index a02df046..00000000 --- a/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs +++ /dev/null @@ -1,239 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Numerics; -using System.Runtime.CompilerServices; -using Rectangle = System.Drawing.Rectangle; - -namespace ComputeSharp.ShareX -{ - /// - /// Applies Gaussian blur processing to an image. - /// - internal sealed partial class HlslConvolutionManager - { - /// - /// The 1D gaussian blur kernel to use. - /// - private readonly float[] kernel; - - /// - /// Creates a new instance for the specific kernel. - /// - /// The convolution kernel to use. - private HlslConvolutionManager(float[] kernel) - { - this.kernel = kernel; - } - - /// - /// Creates a new instance with the specified parameters. - /// - /// The size of the convolution kernel. - /// The sigma parameter for the gaussian blur. - /// An instance to use to blur images. - public static HlslConvolutionManager Create(int size, float sigma) - { - return new(CreateGaussianBlurKernel(size, sigma)); - } - - /// - /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function. - /// - private static float[] CreateGaussianBlurKernel(int size, float weight) - { - static float Gaussian(float x, float sigma) - { - const float Numerator = 1.0f; - float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; - - float exponentNumerator = -x * x; - float exponentDenominator = 2 * sigma * sigma; - - float left = Numerator / denominator; - float right = MathF.Exp(exponentNumerator / exponentDenominator); - - return left * right; - } - - float[] kernel = new float[size]; - ref float rKernel = ref kernel[0]; - - float sum = 0F; - float midpoint = (size - 1) / 2F; - - for (int i = 0; i < size; i++) - { - float x = i - midpoint; - float gx = Gaussian(x, weight); - sum += gx; - Unsafe.Add(ref rKernel, i) = gx; - } - - for (int i = 0; i < size; i++) - { - Unsafe.Add(ref rKernel, i) /= sum; - } - - return kernel; - } - - /// - /// Applies the current convolution to a give bitmap. - /// - /// The input instance to process. - /// A blurred copy of . - public unsafe Bitmap Apply(Bitmap source) - { - Rectangle bounds = new(0, 0, source.Width, source.Height); - Bitmap result = new(source.Width, source.Height); - BitmapData - sourceData = source.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb), - resultData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - Span - sourceSpan = new((Bgra32*)sourceData.Scan0.ToPointer(), bounds.Width * bounds.Height), - resultSpan = new((Bgra32*)resultData.Scan0.ToPointer(), sourceSpan.Length); - - try - { - using ReadWriteTexture2D leftBuffer = Gpu.Default.AllocateReadWriteTexture2D(sourceSpan, source.Width, source.Height); - using ReadWriteTexture2D rightBuffer = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); - using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(this.kernel); - - ApplyVerticalConvolution(leftBuffer, rightBuffer, kernelBuffer, source.Height, source.Width); - ApplyHorizontalConvolution(rightBuffer, leftBuffer, kernelBuffer, source.Height, source.Width); - - leftBuffer.GetData(resultSpan); - } - finally - { - source.UnlockBits(sourceData); - result.UnlockBits(resultData); - } - - return result; - } - - /// - /// Performs a vertical 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - /// The height of the image to process. - /// The width of the image to process. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyVerticalConvolution( - ReadWriteTexture2D source, - ReadWriteTexture2D target, - ReadOnlyBuffer kernel, - int height, - int width) - { - VerticalConvolutionProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - - /// - /// Kernel for the vertical convolution pass. - /// - [AutoConstructor] - internal readonly partial struct VerticalConvolutionProcessor : IComputeShader - { - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; - - public readonly ReadWriteTexture2D source; - public readonly ReadWriteTexture2D target; - public readonly ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) - { - Vector4 result = Vector4.Zero; - int radiusY = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - - for (int i = 0; i < kernelLength; i++) - { - int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); - Vector4 color = source[offsetX, offsetY]; - - result += kernel[i] * color; - } - - target[ids.XY] = result; - } - } - - /// - /// Performs an horizontal 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - /// The height of the image to process. - /// The width of the image to process. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyHorizontalConvolution( - ReadWriteTexture2D source, - ReadWriteTexture2D target, - ReadOnlyBuffer kernel, - int height, - int width) - { - HorizontalConvolutionProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - - /// - /// Kernel for the horizontal convolution pass. - /// - [AutoConstructor] - internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader - { - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; - - public readonly ReadWriteTexture2D source; - public readonly ReadWriteTexture2D target; - public readonly ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) - { - Vector4 result = Vector4.Zero; - int radiusX = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); - - for (int i = 0; i < kernelLength; i++) - { - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - Vector4 color = source[offsetX, offsetY]; - - result += kernel[i] * color; - } - - target[ids.XY] = result; - } - } - } -} diff --git a/samples/ComputeSharp.ShareX/Program.cs b/samples/ComputeSharp.ShareX/Program.cs deleted file mode 100644 index 90241bc2..00000000 --- a/samples/ComputeSharp.ShareX/Program.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Drawing; -using System.IO; -using System.Reflection; -using ShareX.HelpersLib; - -namespace ComputeSharp.ShareX -{ - class Program - { - static void Main() - { - Console.WriteLine(">> Loading image"); - - string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "city.jpg"); - - using Bitmap sourceBitmap = new(path); - - Console.WriteLine(">> Blurring on CPU"); - - using Bitmap resultBitmapCpu = ConvolutionMatrixManager.GaussianBlur(61, 61, 10).Apply(sourceBitmap); - - string resultPathCpu = Path.Combine( - Path.GetRelativePath(Path.GetDirectoryName(path)!, @"..\..\..\"), - $"{Path.GetFileNameWithoutExtension(path)}_cpu{Path.GetExtension(path)}"); - - Console.WriteLine(">> Saving image"); - - resultBitmapCpu.Save(resultPathCpu); - - Console.WriteLine(">> Blurring on GPU"); - - using Bitmap resultBitmapGpu = HlslConvolutionManager.Create(61, 10).Apply(sourceBitmap); - - string resultPathGpu = Path.Combine( - Path.GetRelativePath(Path.GetDirectoryName(path)!, @"..\..\..\"), - $"{Path.GetFileNameWithoutExtension(path)}_gpu{Path.GetExtension(path)}"); - - Console.WriteLine(">> Saving image"); - - resultBitmapGpu.Save(resultPathGpu); - } - } -}