Removed ShareX sample
このコミットが含まれているのは:
1 親
d5992f0039
コミット
f6a7e3f7f3
10ファイル変更
-926
検索条件に一致するファイルはありません
@@ -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}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\ComputeSharp.ImageProcessing\city.jpg" Link="city.jpg" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false"
|
||||
PrivateAssets="contentfiles;build" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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];
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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>(T num, T min, T max) where T : IComparable<T>
|
||||
{
|
||||
if (num.CompareTo(min) <= 0) return min;
|
||||
if (num.CompareTo(max) >= 0) return max;
|
||||
return num;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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<T>(this T num, T min, T max) where T : IComparable<T>
|
||||
{
|
||||
return MathHelpers.Clamp(num, min, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies Gaussian blur processing to an image.
|
||||
/// </summary>
|
||||
internal sealed partial class HlslConvolutionManager
|
||||
{
|
||||
/// <summary>
|
||||
/// The 1D gaussian blur kernel to use.
|
||||
/// </summary>
|
||||
private readonly float[] kernel;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslConvolutionManager"/> instance for the specific kernel.
|
||||
/// </summary>
|
||||
/// <param name="kernel">The convolution kernel to use.</param>
|
||||
private HlslConvolutionManager(float[] kernel)
|
||||
{
|
||||
this.kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HlslConvolutionManager"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="size">The size of the convolution kernel.</param>
|
||||
/// <param name="sigma">The sigma parameter for the gaussian blur.</param>
|
||||
/// <returns>An <see cref="HlslConvolutionManager"/> instance to use to blur images.</returns>
|
||||
public static HlslConvolutionManager Create(int size, float sigma)
|
||||
{
|
||||
return new(CreateGaussianBlurKernel(size, sigma));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the current convolution to a give bitmap.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <see cref="Bitmap"/> instance to process.</param>
|
||||
/// <returns>A blurred copy of <paramref name="source"/>.</returns>
|
||||
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<Bgra32>
|
||||
sourceSpan = new((Bgra32*)sourceData.Scan0.ToPointer(), bounds.Width * bounds.Height),
|
||||
resultSpan = new((Bgra32*)resultData.Scan0.ToPointer(), sourceSpan.Length);
|
||||
|
||||
try
|
||||
{
|
||||
using ReadWriteTexture2D<Bgra32, Vector4> leftBuffer = Gpu.Default.AllocateReadWriteTexture2D<Bgra32, Vector4>(sourceSpan, source.Width, source.Height);
|
||||
using ReadWriteTexture2D<Vector4> rightBuffer = Gpu.Default.AllocateReadWriteTexture2D<Vector4>(source.Width, source.Height);
|
||||
using ReadOnlyBuffer<float> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a vertical 1D complex convolution with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="source">The source <see cref="ReadWriteTexture2D{T,TPixel}"/> to read data from.</param>
|
||||
/// <param name="target">The target <see cref="ReadWriteTexture2D{T}"/> to write the results to.</param>
|
||||
/// <param name="kernel">The <see cref="ReadOnlyBuffer{T}"/> with the values for the current complex kernel.</param>
|
||||
/// <param name="height">The height of the image to process.</param>
|
||||
/// <param name="width">The width of the image to process.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void ApplyVerticalConvolution(
|
||||
ReadWriteTexture2D<Bgra32, Vector4> source,
|
||||
ReadWriteTexture2D<Vector4> target,
|
||||
ReadOnlyBuffer<float> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Kernel for the vertical convolution pass.
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct VerticalConvolutionProcessor : IComputeShader
|
||||
{
|
||||
public readonly int maxY;
|
||||
public readonly int maxX;
|
||||
public readonly int kernelLength;
|
||||
|
||||
public readonly ReadWriteTexture2D<Bgra32, Vector4> source;
|
||||
public readonly ReadWriteTexture2D<Vector4> target;
|
||||
public readonly ReadOnlyBuffer<float> kernel;
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs an horizontal 1D complex convolution with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="source">The source <see cref="ReadWriteTexture2D{T}"/> to read data from.</param>
|
||||
/// <param name="target">The target <see cref="ReadWriteTexture2D{T,TPixel}"/> to write the results to.</param>
|
||||
/// <param name="kernel">The <see cref="ReadOnlyBuffer{T}"/> with the values for the current complex kernel.</param>
|
||||
/// <param name="height">The height of the image to process.</param>
|
||||
/// <param name="width">The width of the image to process.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void ApplyHorizontalConvolution(
|
||||
ReadWriteTexture2D<Vector4> source,
|
||||
ReadWriteTexture2D<Bgra32, Vector4> target,
|
||||
ReadOnlyBuffer<float> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Kernel for the horizontal convolution pass.
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader
|
||||
{
|
||||
public readonly int maxY;
|
||||
public readonly int maxX;
|
||||
public readonly int kernelLength;
|
||||
|
||||
public readonly ReadWriteTexture2D<Vector4> source;
|
||||
public readonly ReadWriteTexture2D<Bgra32, Vector4> target;
|
||||
public readonly ReadOnlyBuffer<float> kernel;
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする