Browse Source

Fix relative offsetting in Pin() methods

js/color-alpha-handling
Sergio Pedri 6 years ago
parent
commit
77a6d08a46
  1. 6
      src/ImageSharp/Memory/ByteMemoryManager{T}.cs
  2. 10
      tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

6
src/ImageSharp/Memory/ByteMemoryManager{T}.cs

@ -2,6 +2,7 @@
// 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
@ -42,7 +43,10 @@ namespace SixLabors.ImageSharp.Memory
/// <inheritdoc/>
public override MemoryHandle Pin(int elementIndex = 0)
{
return this.memory.Slice(elementIndex).Pin();
// 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/>

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

@ -105,7 +105,15 @@ namespace SixLabors.ImageSharp.Tests
/// <inheritdoc/>
public override MemoryHandle Pin(int elementIndex = 0)
{
return this.memory.Slice(elementIndex).Pin();
int byteOffset = elementIndex * Unsafe.SizeOf<TTo>();
int shiftedOffset = Math.DivRem(byteOffset, Unsafe.SizeOf<TFrom>(), out int remainder);
if (remainder != 0)
{
ThrowHelper.ThrowArgumentException("The input index doesn't result in an aligned item access", nameof(elementIndex));
}
return this.memory.Slice(shiftedOffset).Pin();
}
/// <inheritdoc/>

Loading…
Cancel
Save