Skip to content

Commit f348d70

Browse files
committed
Block8x8 Load and CopyTo simplified
Similar to a534328
1 parent 528469f commit f348d70

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public short this[int idx]
6363

6464
public static Block8x8 Load(Span<short> data)
6565
{
66-
Unsafe.SkipInit(out Block8x8 result);
67-
result.LoadFrom(data);
68-
return result;
66+
DebugGuard.MustBeGreaterThanOrEqualTo(data.Length, Size, "data is too small");
67+
68+
ref byte src = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(data));
69+
return Unsafe.ReadUnaligned<Block8x8>(ref src);
6970
}
7071

7172
/// <summary>
@@ -93,9 +94,10 @@ public short[] ToArray()
9394
/// </summary>
9495
public void CopyTo(Span<short> destination)
9596
{
96-
ref byte selfRef = ref Unsafe.As<Block8x8, byte>(ref this);
97-
ref byte destRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast<short, byte>(destination));
98-
Unsafe.CopyBlockUnaligned(ref destRef, ref selfRef, Size * sizeof(short));
97+
DebugGuard.MustBeGreaterThanOrEqualTo(destination.Length, Size, "destination is too small");
98+
99+
ref byte destRef = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(destination));
100+
Unsafe.WriteUnaligned(ref destRef, this);
99101
}
100102

101103
/// <summary>
@@ -124,19 +126,6 @@ public void LoadFrom(ReadOnlySpan<byte> source)
124126
}
125127
}
126128

127-
/// <summary>
128-
/// Load raw 16bit integers from source.
129-
/// </summary>
130-
/// <param name="source">Source</param>
131-
[MethodImpl(InliningOptions.ShortMethod)]
132-
public void LoadFrom(Span<short> source)
133-
{
134-
ref byte sourceRef = ref Unsafe.As<short, byte>(ref MemoryMarshal.GetReference(source));
135-
ref byte destRef = ref Unsafe.As<Block8x8, byte>(ref this);
136-
137-
Unsafe.CopyBlockUnaligned(ref destRef, ref sourceRef, Size * sizeof(short));
138-
}
139-
140129
/// <summary>
141130
/// Cast and copy <see cref="Size"/> <see cref="int"/>-s from the beginning of 'source' span.
142131
/// </summary>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ static void RunTest()
269269
short[] expected = Create8x8ShortData();
270270
ReferenceImplementations.Transpose8x8(expected);
271271

272-
var block8x8 = default(Block8x8);
273-
block8x8.LoadFrom(Create8x8ShortData());
272+
Block8x8 block8x8 = Block8x8.Load(Create8x8ShortData());
274273

275274
block8x8.TransposeInplace();
276275

0 commit comments

Comments
 (0)