Browse Source

Rename methods to use Dangerous prefix.

pull/1674/head
James Jackson-South 5 years ago
parent
commit
0791b576bd
  1. 4
      src/ImageSharp/Memory/Buffer2DExtensions.cs
  2. 12
      src/ImageSharp/Memory/Buffer2D{T}.cs
  3. 2
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs
  4. 6
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs
  5. 2
      tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs
  6. 2
      tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs
  7. 8
      tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs
  8. 14
      tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
  9. 2
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

4
src/ImageSharp/Memory/Buffer2DExtensions.cs

@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Memory
/// Copy <paramref name="columnCount"/> columns of <paramref name="buffer"/> inplace, /// Copy <paramref name="columnCount"/> columns of <paramref name="buffer"/> inplace,
/// from positions starting at <paramref name="sourceIndex"/> to positions at <paramref name="destIndex"/>. /// from positions starting at <paramref name="sourceIndex"/> to positions at <paramref name="destIndex"/>.
/// </summary> /// </summary>
internal static unsafe void CopyColumns<T>( internal static unsafe void DangerousCopyColumns<T>(
this Buffer2D<T> buffer, this Buffer2D<T> buffer,
int sourceIndex, int sourceIndex,
int destIndex, int destIndex,
@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.Memory
int dOffset = destIndex * elementSize; int dOffset = destIndex * elementSize;
long count = columnCount * elementSize; long count = columnCount * elementSize;
Span<byte> span = MemoryMarshal.AsBytes(buffer.GetSingleMemory().Span); Span<byte> span = MemoryMarshal.AsBytes(buffer.DangerousGetSingleMemory().Span);
fixed (byte* ptr = span) fixed (byte* ptr = span)
{ {

12
src/ImageSharp/Memory/Buffer2D{T}.cs

@ -168,10 +168,10 @@ namespace SixLabors.ImageSharp.Memory
/// Thrown when the backing group is discontiguous. /// Thrown when the backing group is discontiguous.
/// </exception> /// </exception>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Span<T> GetSingleSpan() internal Span<T> DangerousGetSingleSpan()
{ {
// TODO: If we need a public version of this method, we need to cache the non-fast Memory<T> of this.MemoryGroup // TODO: If we need a public version of this method, we need to cache the non-fast Memory<T> of this.MemoryGroup
return this.cachedMemory.Length != 0 ? this.cachedMemory.Span : this.GetSingleSpanSlow(); return this.cachedMemory.Length != 0 ? this.cachedMemory.Span : this.DangerousGetSingleSpanSlow();
} }
/// <summary> /// <summary>
@ -183,10 +183,10 @@ namespace SixLabors.ImageSharp.Memory
/// Thrown when the backing group is discontiguous. /// Thrown when the backing group is discontiguous.
/// </exception> /// </exception>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Memory<T> GetSingleMemory() internal Memory<T> DangerousGetSingleMemory()
{ {
// TODO: If we need a public version of this method, we need to cache the non-fast Memory<T> of this.MemoryGroup // TODO: If we need a public version of this method, we need to cache the non-fast Memory<T> of this.MemoryGroup
return this.cachedMemory.Length != 0 ? this.cachedMemory : this.GetSingleMemorySlow(); return this.cachedMemory.Length != 0 ? this.cachedMemory : this.DangerousGetSingleMemorySlow();
} }
/// <summary> /// <summary>
@ -203,10 +203,10 @@ namespace SixLabors.ImageSharp.Memory
private Memory<T> GetRowMemorySlow(int y) => this.FastMemoryGroup.GetBoundedSlice(y * (long)this.Width, this.Width); private Memory<T> GetRowMemorySlow(int y) => this.FastMemoryGroup.GetBoundedSlice(y * (long)this.Width, this.Width);
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
private Memory<T> GetSingleMemorySlow() => this.FastMemoryGroup.Single(); private Memory<T> DangerousGetSingleMemorySlow() => this.FastMemoryGroup.Single();
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
private Span<T> GetSingleSpanSlow() => this.FastMemoryGroup.Single().Span; private Span<T> DangerousGetSingleSpanSlow() => this.FastMemoryGroup.Single().Span;
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
private ref T GetElementSlow(int x, int y) private ref T GetElementSlow(int x, int y)

2
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs

@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.DestinationLength = destinationLength; this.DestinationLength = destinationLength;
this.MaxDiameter = (radius * 2) + 1; this.MaxDiameter = (radius * 2) + 1;
this.data = memoryAllocator.Allocate2D<float>(this.MaxDiameter, bufferHeight, AllocationOptions.Clean); this.data = memoryAllocator.Allocate2D<float>(this.MaxDiameter, bufferHeight, AllocationOptions.Clean);
this.pinHandle = this.data.GetSingleMemory().Pin(); this.pinHandle = this.data.DangerousGetSingleMemory().Pin();
this.kernels = new ResizeKernel[destinationLength]; this.kernels = new ResizeKernel[destinationLength];
this.tempValues = new double[this.MaxDiameter]; this.tempValues = new double[this.MaxDiameter];
} }

6
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs

@ -115,7 +115,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
Span<Vector4> tempColSpan = this.tempColumnBuffer.GetSpan(); Span<Vector4> tempColSpan = this.tempColumnBuffer.GetSpan();
// When creating transposedFirstPassBuffer, we made sure it's contiguous: // When creating transposedFirstPassBuffer, we made sure it's contiguous:
Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.GetSingleSpan(); Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan();
for (int y = rowInterval.Min; y < rowInterval.Max; y++) for (int y = rowInterval.Min; y < rowInterval.Max; y++)
{ {
@ -153,7 +153,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
// Copy previous bottom band to the new top: // Copy previous bottom band to the new top:
// (rows <--> columns, because the buffer is transposed) // (rows <--> columns, because the buffer is transposed)
this.transposedFirstPassBuffer.CopyColumns( this.transposedFirstPassBuffer.DangerousCopyColumns(
this.workerHeight - this.windowBandHeight, this.workerHeight - this.windowBandHeight,
0, 0,
this.windowBandHeight); this.windowBandHeight);
@ -167,7 +167,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
private void CalculateFirstPassValues(RowInterval calculationInterval) private void CalculateFirstPassValues(RowInterval calculationInterval)
{ {
Span<Vector4> tempRowSpan = this.tempRowBuffer.GetSpan(); Span<Vector4> tempRowSpan = this.tempRowBuffer.GetSpan();
Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.GetSingleSpan(); Span<Vector4> transposedFirstPassBufferSpan = this.transposedFirstPassBuffer.DangerousGetSingleSpan();
for (int y = calculationInterval.Min; y < calculationInterval.Max; y++) for (int y = calculationInterval.Min; y < calculationInterval.Max; y++)
{ {

2
tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs

@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
this.Output.WriteLine($"Component{i}: {diff}"); this.Output.WriteLine($"Component{i}: {diff}");
averageDifference += diff.average; averageDifference += diff.average;
totalDifference += diff.total; totalDifference += diff.total;
tolerance += libJpegComponent.SpectralBlocks.GetSingleSpan().Length; tolerance += libJpegComponent.SpectralBlocks.DangerousGetSingleSpan().Length;
} }
averageDifference /= componentCount; averageDifference /= componentCount;

2
tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs

@ -361,7 +361,7 @@ namespace SixLabors.ImageSharp.Tests.Helpers
in operation); in operation);
// Assert: // Assert:
TestImageExtensions.CompareBuffers(expected.GetSingleSpan(), actual.GetSingleSpan()); TestImageExtensions.CompareBuffers(expected.DangerousGetSingleSpan(), actual.DangerousGetSingleSpan());
} }
} }

8
tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

@ -160,7 +160,7 @@ namespace SixLabors.ImageSharp.Tests
using (var image = Image.WrapMemory(memory, bmp.Width, bmp.Height)) using (var image = Image.WrapMemory(memory, bmp.Width, bmp.Height))
{ {
Assert.Equal(memory, image.GetRootFramePixelBuffer().GetSingleMemory()); Assert.Equal(memory, image.GetRootFramePixelBuffer().DangerousGetSingleMemory());
Assert.True(image.TryGetSinglePixelSpan(out Span<Bgra32> imageSpan)); Assert.True(image.TryGetSinglePixelSpan(out Span<Bgra32> imageSpan));
imageSpan.Fill(bg); imageSpan.Fill(bg);
for (var i = 10; i < 20; i++) for (var i = 10; i < 20; i++)
@ -196,7 +196,7 @@ namespace SixLabors.ImageSharp.Tests
using (var image = Image.WrapMemory(memoryManager, bmp.Width, bmp.Height)) using (var image = Image.WrapMemory(memoryManager, bmp.Width, bmp.Height))
{ {
Assert.Equal(memoryManager.Memory, image.GetRootFramePixelBuffer().GetSingleMemory()); Assert.Equal(memoryManager.Memory, image.GetRootFramePixelBuffer().DangerousGetSingleMemory());
Assert.True(image.TryGetSinglePixelSpan(out Span<Bgra32> imageSpan)); Assert.True(image.TryGetSinglePixelSpan(out Span<Bgra32> imageSpan));
imageSpan.Fill(bg); imageSpan.Fill(bg);
for (var i = 10; i < 20; i++) for (var i = 10; i < 20; i++)
@ -255,7 +255,7 @@ namespace SixLabors.ImageSharp.Tests
using (var image = Image.WrapMemory<Bgra32>(byteMemory, bmp.Width, bmp.Height)) using (var image = Image.WrapMemory<Bgra32>(byteMemory, bmp.Width, bmp.Height))
{ {
Span<Bgra32> pixelSpan = pixelMemory.Span; Span<Bgra32> pixelSpan = pixelMemory.Span;
Span<Bgra32> imageSpan = image.GetRootFramePixelBuffer().GetSingleMemory().Span; Span<Bgra32> imageSpan = image.GetRootFramePixelBuffer().DangerousGetSingleMemory().Span;
// We can't compare the two Memory<T> instances directly as they wrap different memory managers. // We can't compare the two Memory<T> instances directly as they wrap different memory managers.
// To check that the underlying data matches, we can just manually check their lenth, and the // To check that the underlying data matches, we can just manually check their lenth, and the
@ -327,7 +327,7 @@ namespace SixLabors.ImageSharp.Tests
using (var image = Image.WrapMemory<Bgra32>(p, bmp.Width, bmp.Height)) using (var image = Image.WrapMemory<Bgra32>(p, bmp.Width, bmp.Height))
{ {
Span<Bgra32> pixelSpan = pixelMemory.Span; Span<Bgra32> pixelSpan = pixelMemory.Span;
Span<Bgra32> imageSpan = image.GetRootFramePixelBuffer().GetSingleMemory().Span; Span<Bgra32> imageSpan = image.GetRootFramePixelBuffer().DangerousGetSingleMemory().Span;
Assert.Equal(pixelSpan.Length, imageSpan.Length); Assert.Equal(pixelSpan.Length, imageSpan.Length);
Assert.True(Unsafe.AreSame(ref pixelSpan.GetPinnableReference(), ref imageSpan.GetPinnableReference())); Assert.True(Unsafe.AreSame(ref pixelSpan.GetPinnableReference(), ref imageSpan.GetPinnableReference()));

14
tests/ImageSharp.Tests/Memory/Buffer2DTests.cs

@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
Assert.Equal(width, buffer.Width); Assert.Equal(width, buffer.Width);
Assert.Equal(height, buffer.Height); Assert.Equal(height, buffer.Height);
Assert.Equal(0, buffer.FastMemoryGroup.TotalLength); Assert.Equal(0, buffer.FastMemoryGroup.TotalLength);
Assert.Equal(0, buffer.GetSingleSpan().Length); Assert.Equal(0, buffer.DangerousGetSingleSpan().Length);
} }
} }
@ -87,7 +87,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
{ {
using (Buffer2D<int> buffer = this.MemoryAllocator.Allocate2D<int>(42, 42, AllocationOptions.Clean)) using (Buffer2D<int> buffer = this.MemoryAllocator.Allocate2D<int>(42, 42, AllocationOptions.Clean))
{ {
Span<int> span = buffer.GetSingleSpan(); Span<int> span = buffer.DangerousGetSingleSpan();
for (int j = 0; j < span.Length; j++) for (int j = 0; j < span.Length; j++)
{ {
Assert.Equal(0, span[j]); Assert.Equal(0, span[j]);
@ -249,9 +249,9 @@ namespace SixLabors.ImageSharp.Tests.Memory
var rnd = new Random(123); var rnd = new Random(123);
using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(width, height)) using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(width, height))
{ {
rnd.RandomFill(b.GetSingleSpan(), 0, 1); rnd.RandomFill(b.DangerousGetSingleSpan(), 0, 1);
b.CopyColumns(startIndex, destIndex, columnCount); b.DangerousCopyColumns(startIndex, destIndex, columnCount);
for (int y = 0; y < b.Height; y++) for (int y = 0; y < b.Height; y++)
{ {
@ -271,10 +271,10 @@ namespace SixLabors.ImageSharp.Tests.Memory
var rnd = new Random(123); var rnd = new Random(123);
using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(100, 100)) using (Buffer2D<float> b = this.MemoryAllocator.Allocate2D<float>(100, 100))
{ {
rnd.RandomFill(b.GetSingleSpan(), 0, 1); rnd.RandomFill(b.DangerousGetSingleSpan(), 0, 1);
b.CopyColumns(0, 50, 22); b.DangerousCopyColumns(0, 50, 22);
b.CopyColumns(0, 50, 22); b.DangerousCopyColumns(0, 50, 22);
for (int y = 0; y < b.Height; y++) for (int y = 0; y < b.Height; y++)
{ {

2
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -672,7 +672,7 @@ namespace SixLabors.ImageSharp.Tests
var image = new Image<Rgba32>(buffer.Width, buffer.Height); var image = new Image<Rgba32>(buffer.Width, buffer.Height);
Assert.True(image.Frames.RootFrame.TryGetSinglePixelSpan(out Span<Rgba32> pixels)); Assert.True(image.Frames.RootFrame.TryGetSinglePixelSpan(out Span<Rgba32> pixels));
Span<float> bufferSpan = buffer.GetSingleSpan(); Span<float> bufferSpan = buffer.DangerousGetSingleSpan();
for (int i = 0; i < bufferSpan.Length; i++) for (int i = 0; i < bufferSpan.Length; i++)
{ {

Loading…
Cancel
Save