mirror of https://github.com/SixLabors/ImageSharp
8 changed files with 218 additions and 5 deletions
@ -0,0 +1,59 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.MetaData; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
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,32 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
|
|||
namespace SixLabors.ImageSharp.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 Span<T> GetSpan() |
|||
{ |
|||
return this.Memory.Span; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue