Browse Source

do not use Configuration.Default.MemoryManager in AddFrame()

af/merge-core
Anton Firszov 8 years ago
parent
commit
a6fb21a8a0
  1. 10
      src/ImageSharp/Image/ImageFrame.LoadPixelData.cs
  2. 6
      src/ImageSharp/Image/ImageFrameCollection.cs
  3. 15
      src/ImageSharp/Memory/Buffer2D{T}.cs

10
src/ImageSharp/Image/ImageFrame.LoadPixelData.cs

@ -20,30 +20,32 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the given byte array in <typeparamref name="TPixel"/> format.
/// </summary>
/// <param name="memoryManager">The memory manager to use for allocations</param>
/// <param name="data">The byte array containing image data.</param>
/// <param name="width">The width of the final image.</param>
/// <param name="height">The height of the final image.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
public static ImageFrame<TPixel> LoadPixelData<TPixel>(Span<byte> data, int width, int height)
public static ImageFrame<TPixel> LoadPixelData<TPixel>(MemoryManager memoryManager, Span<byte> data, int width, int height)
where TPixel : struct, IPixel<TPixel>
=> LoadPixelData(data.NonPortableCast<byte, TPixel>(), width, height);
=> LoadPixelData(memoryManager, data.NonPortableCast<byte, TPixel>(), width, height);
/// <summary>
/// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data.
/// </summary>
/// <param name="memoryManager">The memory manager to use for allocations</param>
/// <param name="data">The Span containing the image Pixel data.</param>
/// <param name="width">The width of the final image.</param>
/// <param name="height">The height of the final image.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
public static ImageFrame<TPixel> LoadPixelData<TPixel>(Span<TPixel> data, int width, int height)
public static ImageFrame<TPixel> LoadPixelData<TPixel>(MemoryManager memoryManager, Span<TPixel> data, int width, int height)
where TPixel : struct, IPixel<TPixel>
{
int count = width * height;
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
var image = new ImageFrame<TPixel>(Configuration.Default.MemoryManager, width, height);
var image = new ImageFrame<TPixel>(memoryManager, width, height);
SpanHelper.Copy(data, image.GetPixelSpan(), count);
return image;

6
src/ImageSharp/Image/ImageFrameCollection.cs

@ -80,7 +80,11 @@ namespace SixLabors.ImageSharp
/// <inheritdoc/>
public ImageFrame<TPixel> AddFrame(TPixel[] data)
{
var frame = ImageFrame.LoadPixelData(new Span<TPixel>(data), this.RootFrame.Width, this.RootFrame.Height);
var frame = ImageFrame.LoadPixelData(
this.parent.GetMemoryManager(),
new Span<TPixel>(data),
this.RootFrame.Width,
this.RootFrame.Height);
this.frames.Add(frame);
return frame;
}

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

@ -34,8 +34,14 @@ namespace SixLabors.ImageSharp.Memory
/// <inheritdoc />
public int Height { get; private set; }
/// <summary>
/// Gets the span to the whole area.
/// </summary>
public Span<T> Span => this.Buffer.Span;
/// <summary>
/// Gets the backing <see cref="IBuffer{T}"/>
/// </summary>
public IBuffer<T> Buffer { get; private set; }
/// <summary>
@ -56,11 +62,20 @@ namespace SixLabors.ImageSharp.Memory
}
}
/// <summary>
/// Disposes the <see cref="Buffer2D{T}"/> instance
/// </summary>
public void Dispose()
{
this.Buffer?.Dispose();
}
/// <summary>
/// Swap the contents (<see cref="Buffer"/>, <see cref="Width"/>, <see cref="Height"/>) of the two buffers.
/// Useful to transfer the contents of a temporal <see cref="Buffer2D{T}"/> to a persistent <see cref="ImageFrame{TPixel}.PixelBuffer"/>
/// </summary>
/// <param name="a">The first buffer</param>
/// <param name="b">The second buffer</param>
public static void SwapContents(Buffer2D<T> a, Buffer2D<T> b)
{
Size aSize = a.Size();

Loading…
Cancel
Save