-
-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1314 from SixLabors/sp/byte-to-tpixel-wrapping
Image.WrapMemory<TPixel> APIs wrapping Memory<byte>
- Loading branch information
Showing
5 changed files
with
322 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
using System; | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace SixLabors.ImageSharp.Memory | ||
{ | ||
/// <summary> | ||
/// A custom <see cref="MemoryManager{T}"/> that can wrap <see cref="Memory{T}"/> of <see cref="byte"/> instances | ||
/// and cast them to be <see cref="Memory{T}"/> for any arbitrary unmanaged <typeparamref name="T"/> value type. | ||
/// </summary> | ||
/// <typeparam name="T">The value type to use when casting the wrapped <see cref="Memory{T}"/> instance.</typeparam> | ||
internal sealed class ByteMemoryManager<T> : MemoryManager<T> | ||
where T : unmanaged | ||
{ | ||
/// <summary> | ||
/// The wrapped <see cref="Memory{T}"/> of <see cref="byte"/> instance. | ||
/// </summary> | ||
private readonly Memory<byte> memory; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ByteMemoryManager{T}"/> class. | ||
/// </summary> | ||
/// <param name="memory">The <see cref="Memory{T}"/> of <see cref="byte"/> instance to wrap.</param> | ||
public ByteMemoryManager(Memory<byte> memory) | ||
{ | ||
this.memory = memory; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Span<T> GetSpan() | ||
{ | ||
return MemoryMarshal.Cast<byte, T>(this.memory.Span); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override MemoryHandle Pin(int elementIndex = 0) | ||
{ | ||
// We need to adjust the offset into the wrapped byte segment, | ||
// as the input index refers to the target-cast memory of T. | ||
// We just have to shift this index by the byte size of T. | ||
return this.memory.Slice(elementIndex * Unsafe.SizeOf<T>()).Pin(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Unpin() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.