mirror of https://github.com/SixLabors/ImageSharp
136 changed files with 2163 additions and 1371 deletions
@ -0,0 +1,59 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using SixLabors.ImageSharp.MetaData; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.Memory; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <content>
|
|||
/// Adds static methods allowing wrapping an existing memory area as an image.
|
|||
/// </content>
|
|||
public static partial class Image |
|||
{ |
|||
// TODO: This is a WIP API, should be public when finished.
|
|||
|
|||
/// <summary>
|
|||
/// Wraps an existing contigous memory area of 'width'x'height' pixels,
|
|||
/// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel type</typeparam>
|
|||
/// <param name="config">The <see cref="Configuration"/></param>
|
|||
/// <param name="pixelMemory">The pixel memory</param>
|
|||
/// <param name="width">The width of the memory image</param>
|
|||
/// <param name="height">The height of the memory image</param>
|
|||
/// <param name="metaData">The <see cref="ImageMetaData"/></param>
|
|||
/// <returns>An <see cref="Image{TPixel}"/> instance</returns>
|
|||
internal static Image<TPixel> WrapMemory<TPixel>( |
|||
Configuration config, |
|||
Memory<TPixel> pixelMemory, |
|||
int width, |
|||
int height, |
|||
ImageMetaData metaData) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
var buffer = new ConsumedBuffer<TPixel>(pixelMemory); |
|||
return new Image<TPixel>(config, buffer, width, height, metaData); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Wraps an existing contigous memory area of 'width'x'height' pixels,
|
|||
/// allowing to view/manipulate it as an ImageSharp <see cref="Image{TPixel}"/> instance.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel type</typeparam>
|
|||
/// <param name="pixelMemory">The pixel memory</param>
|
|||
/// <param name="width">The width of the memory image</param>
|
|||
/// <param name="height">The height of the memory image</param>
|
|||
/// <returns>An <see cref="Image{TPixel}"/> instance</returns>
|
|||
internal static Image<TPixel> WrapMemory<TPixel>( |
|||
Memory<TPixel> pixelMemory, |
|||
int width, |
|||
int height) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
return WrapMemory(Configuration.Default, pixelMemory, width, height, new ImageMetaData()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
|
|||
namespace SixLabors.Memory |
|||
{ |
|||
/// <summary>
|
|||
/// A buffer implementation that consumes an existing <see cref="Memory{T}"/> instance.
|
|||
/// The ownership of the memory remains external.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The value type</typeparam>
|
|||
internal sealed class ConsumedBuffer<T> : IBuffer<T> |
|||
where T : struct |
|||
{ |
|||
public ConsumedBuffer(Memory<T> memory) |
|||
{ |
|||
this.Memory = memory; |
|||
} |
|||
|
|||
public Memory<T> Memory { get; } |
|||
|
|||
public bool IsMemoryOwner => false; |
|||
|
|||
public Span<T> GetSpan() |
|||
{ |
|||
return this.Memory.Span; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System.Buffers; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
namespace SixLabors.Memory |
|||
{ |
|||
/// <summary>
|
|||
/// Provides a base class for <see cref="IBuffer{T}"/> implementations by implementing pinning logic for <see cref="MemoryManager{T}"/> adaption.
|
|||
/// </summary>
|
|||
internal abstract class ManagedBufferBase<T> : System.Buffers.MemoryManager<T>, IBuffer<T> |
|||
where T : struct |
|||
{ |
|||
private GCHandle pinHandle; |
|||
|
|||
public bool IsMemoryOwner => true; |
|||
|
|||
/// <summary>
|
|||
/// Gets the object that should be pinned.
|
|||
/// </summary>
|
|||
protected abstract object GetPinnableObject(); |
|||
|
|||
public override unsafe MemoryHandle Pin(int elementIndex = 0) |
|||
{ |
|||
if (!this.pinHandle.IsAllocated) |
|||
{ |
|||
this.pinHandle = GCHandle.Alloc(this.GetPinnableObject(), GCHandleType.Pinned); |
|||
} |
|||
|
|||
void* ptr = (void*)this.pinHandle.AddrOfPinnedObject(); |
|||
return new MemoryHandle(ptr, this.pinHandle); |
|||
} |
|||
|
|||
public override void Unpin() |
|||
{ |
|||
if (this.pinHandle.IsAllocated) |
|||
{ |
|||
this.pinHandle.Free(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,9 +1,9 @@ |
|||
namespace SixLabors.ImageSharp.Memory |
|||
namespace SixLabors.Memory |
|||
{ |
|||
/// <summary>
|
|||
/// Implements <see cref="MemoryManager"/> by newing up arrays by the GC on every allocation requests.
|
|||
/// Implements <see cref="MemoryAllocator"/> by newing up arrays by the GC on every allocation requests.
|
|||
/// </summary>
|
|||
public class SimpleGcMemoryManager : MemoryManager |
|||
public class SimpleGcMemoryAllocator : MemoryAllocator |
|||
{ |
|||
/// <inheritdoc />
|
|||
internal override IBuffer<T> Allocate<T>(int length, bool clear) |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue