Updated ImageSharp to nightly version
このコミットが含まれているのは:
1 親
028693053f
コミット
717fd71d8c
5ファイル変更
+57
-76
検索条件に一致するファイルはありません
@@ -16,7 +16,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-unstable1043" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
@@ -59,9 +58,10 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
public float Gamma { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) where TPixel : struct, IPixel<TPixel>
|
||||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
where TPixel : unmanaged, IPixel<TPixel>
|
||||
{
|
||||
return new HlslBokehBlurProcessor<TPixel>(this, source, sourceRectangle);
|
||||
return new HlslBokehBlurProcessor<TPixel>(this, configuration, source, sourceRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Advanced;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
@@ -20,7 +19,8 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
/// </summary>
|
||||
/// <typeparam name="TPixel">The pixel format</typeparam>
|
||||
/// <remarks>This processor is based on the code from Mike Pound, see <a href="https://github.com/mikepound/convolve">github.com/mikepound/convolve</a></remarks>
|
||||
public sealed class HlslBokehBlurProcessor<TPixel> : IImageProcessor<TPixel> where TPixel : struct, IPixel<TPixel>
|
||||
public sealed class HlslBokehBlurProcessor<TPixel> : ImageProcessor<TPixel>
|
||||
where TPixel : unmanaged, IPixel<TPixel>
|
||||
{
|
||||
/// <summary>
|
||||
/// The kernel radius
|
||||
@@ -62,24 +62,20 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
/// </summary>
|
||||
private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Vector2[][] Kernels)> Cache = new ConcurrentDictionary<(int, int), (Vector4[], float, Vector2[][])>();
|
||||
|
||||
/// <summary>
|
||||
/// The source <see cref="Image{TPixel}"/> instance to modify
|
||||
/// </summary>
|
||||
private readonly Image<TPixel> Source;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslBokehBlurProcessor{TPixel}"/> class
|
||||
/// </summary>
|
||||
/// <param name="definition">The <see cref="HlslBokehBlurProcessor"/> defining the processor parameters</param>
|
||||
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library</param>
|
||||
/// <param name="source">The source <see cref="Image{TPixel}"/> instance to modify</param>
|
||||
/// <param name="sourceRectangle">The source <see cref="Rectangle"/> that indicates the area to edit</param>
|
||||
public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
: base(configuration, source, sourceRectangle)
|
||||
{
|
||||
Radius = definition.Radius;
|
||||
KernelSize = Radius * 2 + 1;
|
||||
ComponentsCount = definition.Components;
|
||||
Gamma = definition.Gamma;
|
||||
Source = source;
|
||||
|
||||
// Reuse the initialized values from the cache, if possible
|
||||
var parameters = (Radius, ComponentsCount);
|
||||
@@ -259,12 +255,12 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Apply()
|
||||
protected override void OnFrameApply(ImageFrame<TPixel> source)
|
||||
{
|
||||
// Preliminary gamma highlight pass
|
||||
using IMemoryOwner<Vector4> source4 = GetExposedVector4Buffer();
|
||||
using IMemoryOwner<Vector4> source4 = GetExposedVector4Buffer(source);
|
||||
|
||||
using ReadOnlyBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span);
|
||||
using ReadOnlyBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span);
|
||||
using ReadWriteBuffer<Vector4> processingBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Size);
|
||||
using ReadWriteBuffer<Vector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Size * 2);
|
||||
using ReadOnlyBuffer<Vector2> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer<Vector2>(KernelSize);
|
||||
@@ -281,9 +277,11 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W);
|
||||
}
|
||||
|
||||
// Apply the inverse gamma exposure pass, and write the final pixel data
|
||||
// Apply the inverse gamma exposure pass
|
||||
processingBuffer.GetData(source4.Memory.Span);
|
||||
ApplyInverseGammaExposure(source4);
|
||||
|
||||
// Write the final pixel data
|
||||
ApplyInverseGammaExposure(source4, source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -429,16 +427,17 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
/// <summary>
|
||||
/// Applies the gamma correction/highlight to the input pixel buffer and returns an <see cref="IMemoryOwner{T}"/> instance with <see cref="Vector4"/> values.
|
||||
/// </summary>
|
||||
/// <param name="source">The source image</param>
|
||||
[Pure]
|
||||
private IMemoryOwner<Vector4> GetExposedVector4Buffer()
|
||||
private IMemoryOwner<Vector4> GetExposedVector4Buffer(ImageFrame<TPixel> source)
|
||||
{
|
||||
IMemoryOwner<Vector4> source4 = Source.GetConfiguration().MemoryAllocator.Allocate<Vector4>(Source.Width * Source.Height);
|
||||
IMemoryOwner<Vector4> source4 = Configuration.MemoryAllocator.Allocate<Vector4>(source.Width * source.Height);
|
||||
float exp = Gamma;
|
||||
int width = Source.Width;
|
||||
|
||||
Parallel.For(0, Source.Height, y =>
|
||||
Parallel.For(0, source.Height, y =>
|
||||
{
|
||||
ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference();
|
||||
ref TPixel rPixel = ref source.GetPixelRowSpan(y).GetPinnableReference();
|
||||
ref Vector4 r4 = ref source4.Memory.Span.Slice(y * width).GetPinnableReference();
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
@@ -458,17 +457,18 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
/// Applies the inverse gamma exposure pass to compute the final pixel values for a target image
|
||||
/// </summary>
|
||||
/// <param name="sourceValues">The source <see cref="Vector4"/> buffer to read from</param>
|
||||
private void ApplyInverseGammaExposure(IMemoryOwner<Vector4> sourceValues)
|
||||
/// <param name="source">The source image</param>
|
||||
private void ApplyInverseGammaExposure(IMemoryOwner<Vector4> sourceValues, ImageFrame<TPixel> source)
|
||||
{
|
||||
int width = Source.Width;
|
||||
int width = source.Width;
|
||||
float expGamma = 1 / Gamma;
|
||||
|
||||
Parallel.For(0, Source.Height, y =>
|
||||
Parallel.For(0, source.Height, y =>
|
||||
{
|
||||
Vector4 low = Vector4.Zero;
|
||||
var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
|
||||
|
||||
ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference();
|
||||
ref TPixel rPixel = ref source.GetPixelRowSpan(y).GetPinnableReference();
|
||||
ref Vector4 r4 = ref sourceValues.Memory.Span.Slice(y * width).GetPinnableReference();
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
@@ -483,8 +483,5 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
@@ -48,9 +47,10 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
public int Radius { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Image<TPixel> source, Rectangle sourceRectangle) where TPixel : struct, IPixel<TPixel>
|
||||
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
where TPixel : unmanaged, IPixel<TPixel>
|
||||
{
|
||||
return new HlslGaussianBlurProcessor<TPixel>(this, source, sourceRectangle);
|
||||
return new HlslGaussianBlurProcessor<TPixel>(this, configuration, source, sourceRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
-44
@@ -1,13 +1,12 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Advanced;
|
||||
using SixLabors.ImageSharp.Memory;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing.Processors;
|
||||
using SixLabors.Primitives;
|
||||
|
||||
namespace ComputeSharp.BokehBlur.Processors
|
||||
{
|
||||
@@ -15,29 +14,26 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
/// Applies Gaussian blur processing to an image.
|
||||
/// </summary>
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
||||
internal class HlslGaussianBlurProcessor<TPixel> : IImageProcessor<TPixel> where TPixel : struct, IPixel<TPixel>
|
||||
internal class HlslGaussianBlurProcessor<TPixel> : ImageProcessor<TPixel>
|
||||
where TPixel : unmanaged, IPixel<TPixel>
|
||||
{
|
||||
/// <summary>
|
||||
/// The 1D kernel to apply
|
||||
/// </summary>
|
||||
private readonly float[] Kernel;
|
||||
|
||||
/// <summary>
|
||||
/// The source <see cref="Image{TPixel}"/> instance to modify
|
||||
/// </summary>
|
||||
private readonly Image<TPixel> Source;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HlslGaussianBlurProcessor"/> class
|
||||
/// </summary>
|
||||
/// <param name="definition">The <see cref="HlslGaussianBlurProcessor"/> defining the processor parameters</param>
|
||||
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library</param>
|
||||
/// <param name="source">The source <see cref="Image{TPixel}"/> for the current processor instance</param>
|
||||
/// <param name="sourceRectangle">The source area to process for the current processor instance</param>
|
||||
public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
|
||||
: base(configuration, source, sourceRectangle)
|
||||
{
|
||||
int kernelSize = definition.Radius * 2 + 1;
|
||||
Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma);
|
||||
Source = source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,20 +85,31 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Apply()
|
||||
protected override void OnFrameApply(ImageFrame<TPixel> source)
|
||||
{
|
||||
using Image<RgbaVector> source = Source.CloneAs<RgbaVector>();
|
||||
Span<Vector4> sourceSpan = MemoryMarshal.Cast<RgbaVector, Vector4>(source.GetPixelSpan());
|
||||
IMemoryGroup<TPixel> group = source.GetPixelMemoryGroup();
|
||||
|
||||
using ReadWriteBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan);
|
||||
using ReadWriteBuffer<Vector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Size);
|
||||
using ReadOnlyBuffer<float> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel);
|
||||
for (int i = group.Count - 1; i >= 0; i--)
|
||||
{
|
||||
Span<TPixel> spanTPixel = group[i].Span;
|
||||
|
||||
ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer);
|
||||
ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer);
|
||||
using IMemoryOwner<Vector4> group4 = Configuration.MemoryAllocator.Allocate<Vector4>(spanTPixel.Length);
|
||||
|
||||
sourceBuffer.GetData(sourceSpan);
|
||||
WriteResultData(source);
|
||||
Span<Vector4> spanVector4 = group4.Memory.Span;
|
||||
|
||||
PixelOperations<TPixel>.Instance.ToVector4(Configuration, spanTPixel, spanVector4);
|
||||
|
||||
using ReadWriteBuffer<Vector4> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(spanVector4);
|
||||
using ReadWriteBuffer<Vector4> firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer<Vector4>(sourceBuffer.Size);
|
||||
using ReadOnlyBuffer<float> kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel);
|
||||
|
||||
ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer);
|
||||
ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer);
|
||||
|
||||
sourceBuffer.GetData(spanVector4);
|
||||
|
||||
PixelOperations<TPixel>.Instance.FromVector4Destructive(Configuration, spanVector4, spanTPixel);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -230,28 +237,5 @@ namespace ComputeSharp.BokehBlur.Processors
|
||||
target[offsetXY] = result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the final data to the source image
|
||||
/// </summary>
|
||||
/// <param name="image">The source <see cref="Image{TPixel}"/> to read from</param>
|
||||
private void WriteResultData(Image<RgbaVector> image)
|
||||
{
|
||||
int width = Source.Width;
|
||||
|
||||
Parallel.For(0, Source.Height, y =>
|
||||
{
|
||||
ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference();
|
||||
ref Vector4 r4 = ref MemoryMarshal.Cast<RgbaVector, Vector4>(image.GetPixelRowSpan(y)).GetPinnableReference();
|
||||
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Unsafe.Add(ref rPixel, x).FromVector4(Unsafe.Add(ref r4, x));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする