diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 066d9426..2c84c1f5 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -45,7 +45,9 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Sample.Shared" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals.SourceGenerators", "tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj", "{BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" +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 @@ -172,6 +174,14 @@ 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 @@ -187,6 +197,7 @@ 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 new file mode 100644 index 00000000..55f80df8 --- /dev/null +++ b/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj @@ -0,0 +1,14 @@ + + + + Exe + net5.0 + true + enable + + + + + + + diff --git a/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs b/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs new file mode 100644 index 00000000..70dff53d --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/ColorBgra.cs @@ -0,0 +1,120 @@ +#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 new file mode 100644 index 00000000..d6cbc103 --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrix.cs @@ -0,0 +1,66 @@ +#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 new file mode 100644 index 00000000..d74fa361 --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/ConvolutionMatrixManager.cs @@ -0,0 +1,148 @@ +#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 new file mode 100644 index 00000000..c7c3f753 --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/MathHelpers.cs @@ -0,0 +1,41 @@ +#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 new file mode 100644 index 00000000..11a0cd20 --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/NumberExtensions.cs @@ -0,0 +1,39 @@ +#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 new file mode 100644 index 00000000..64965d39 --- /dev/null +++ b/samples/ComputeSharp.ShareX/Cpu/UnsafeBitmap.cs @@ -0,0 +1,192 @@ +#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/Program.cs b/samples/ComputeSharp.ShareX/Program.cs new file mode 100644 index 00000000..5eca73eb --- /dev/null +++ b/samples/ComputeSharp.ShareX/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ComputeSharp.ShareX +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +}