Browse Source

Block8x8 Load and CopyTo simplified

Similar to a534328dc4
pull/2418/head
Günther Foidl 3 years ago
parent
commit
f348d704ef
  1. 27
      src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs
  2. 3
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs

27
src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs

@ -63,9 +63,10 @@ internal partial struct Block8x8
public static Block8x8 Load(Span<short> data)
{
Unsafe.SkipInit(out Block8x8 result);
result.LoadFrom(data);
return result;
DebugGuard.MustBeGreaterThanOrEqualTo(data.Length, Size, "data is too small");
ref byte src = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(data));
return Unsafe.ReadUnaligned<Block8x8>(ref src);
}
/// <summary>
@ -93,9 +94,10 @@ internal partial struct Block8x8
/// </summary>
public void CopyTo(Span<short> destination)
{
ref byte selfRef = ref Unsafe.As<Block8x8, byte>(ref this);
ref byte destRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<short, byte>(destination));
Unsafe.CopyBlockUnaligned(ref destRef, ref selfRef, Size * sizeof(short));
DebugGuard.MustBeGreaterThanOrEqualTo(destination.Length, Size, "destination is too small");
ref byte destRef = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(destination));
Unsafe.WriteUnaligned(ref destRef, this);
}
/// <summary>
@ -124,19 +126,6 @@ internal partial struct Block8x8
}
}
/// <summary>
/// Load raw 16bit integers from source.
/// </summary>
/// <param name="source">Source</param>
[MethodImpl(InliningOptions.ShortMethod)]
public void LoadFrom(Span<short> source)
{
ref byte sourceRef = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(source));
ref byte destRef = ref Unsafe.As<Block8x8, byte>(ref this);
Unsafe.CopyBlockUnaligned(ref destRef, ref sourceRef, Size * sizeof(short));
}
/// <summary>
/// Cast and copy <see cref="Size"/> <see cref="int"/>-s from the beginning of 'source' span.
/// </summary>

3
tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs

@ -269,8 +269,7 @@ public class Block8x8Tests : JpegFixture
short[] expected = Create8x8ShortData();
ReferenceImplementations.Transpose8x8(expected);
var block8x8 = default(Block8x8);
block8x8.LoadFrom(Create8x8ShortData());
Block8x8 block8x8 = Block8x8.Load(Create8x8ShortData());
block8x8.TransposeInplace();

Loading…
Cancel
Save