Skip to content

Commit ea2c4fa

Browse files
committed
Refactor BitReaderBase
1 parent 1f9e6df commit ea2c4fa

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Buffers;
5-
using System.Diagnostics.CodeAnalysis;
65
using SixLabors.ImageSharp.Memory;
76

87
namespace SixLabors.ImageSharp.Formats.Webp.BitReader;
@@ -14,23 +13,28 @@ internal abstract class BitReaderBase : IDisposable
1413
{
1514
private bool isDisposed;
1615

16+
protected BitReaderBase(IMemoryOwner<byte> data) => this.Data = data;
17+
18+
protected BitReaderBase(Stream inputStream, int imageDataSize, MemoryAllocator memoryAllocator) => this.Data = ReadImageDataFromStream(inputStream, imageDataSize, memoryAllocator);
19+
1720
/// <summary>
1821
/// Gets or sets the raw encoded image data.
1922
/// </summary>
20-
public IMemoryOwner<byte>? Data { get; set; }
23+
public IMemoryOwner<byte> Data { get; set; }
2124

2225
/// <summary>
2326
/// Copies the raw encoded image data from the stream into a byte array.
2427
/// </summary>
2528
/// <param name="input">The input stream.</param>
2629
/// <param name="bytesToRead">Number of bytes to read as indicated from the chunk size.</param>
2730
/// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param>
28-
[MemberNotNull(nameof(Data))]
29-
protected void ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator)
31+
protected static IMemoryOwner<byte> ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator)
3032
{
31-
this.Data = memoryAllocator.Allocate<byte>(bytesToRead);
32-
Span<byte> dataSpan = this.Data.Memory.Span;
33+
IMemoryOwner<byte> data = memoryAllocator.Allocate<byte>(bytesToRead);
34+
Span<byte> dataSpan = data.Memory.Span;
3335
input.Read(dataSpan[..bytesToRead], 0, bytesToRead);
36+
37+
return data;
3438
}
3539

3640
protected virtual void Dispose(bool disposing)
@@ -42,7 +46,7 @@ protected virtual void Dispose(bool disposing)
4246

4347
if (disposing)
4448
{
45-
this.Data?.Dispose();
49+
this.Data.Dispose();
4650
}
4751

4852
this.isDisposed = true;

src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ internal class Vp8BitReader : BitReaderBase
5757
/// <param name="partitionLength">The partition length.</param>
5858
/// <param name="startPos">Start index in the data array. Defaults to 0.</param>
5959
public Vp8BitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator, uint partitionLength, int startPos = 0)
60+
: base(inputStream, (int)imageDataSize, memoryAllocator)
6061
{
6162
Guard.MustBeLessThan(imageDataSize, int.MaxValue, nameof(imageDataSize));
6263

6364
this.ImageDataSize = imageDataSize;
6465
this.PartitionLength = partitionLength;
65-
this.ReadImageDataFromStream(inputStream, (int)imageDataSize, memoryAllocator);
6666
this.InitBitreader(partitionLength, startPos);
6767
}
6868

@@ -73,8 +73,8 @@ public Vp8BitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memo
7373
/// <param name="partitionLength">The partition length.</param>
7474
/// <param name="startPos">Start index in the data array. Defaults to 0.</param>
7575
public Vp8BitReader(IMemoryOwner<byte> imageData, uint partitionLength, int startPos = 0)
76+
: base(imageData)
7677
{
77-
this.Data = imageData;
7878
this.ImageDataSize = (uint)imageData.Memory.Length;
7979
this.PartitionLength = partitionLength;
8080
this.InitBitreader(partitionLength, startPos);
@@ -186,7 +186,7 @@ private void LoadNewBytes()
186186
{
187187
if (this.pos < this.bufferMax)
188188
{
189-
ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data!.Memory.Span.Slice((int)this.pos, 8));
189+
ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data.Memory.Span.Slice((int)this.pos, 8));
190190
this.pos += BitsCount >> 3;
191191
ulong bits = ByteSwap64(inBits);
192192
bits >>= 64 - BitsCount;
@@ -205,7 +205,7 @@ private void LoadFinalBytes()
205205
if (this.pos < this.bufferEnd)
206206
{
207207
this.bits += 8;
208-
this.value = this.Data!.Memory.Span[(int)this.pos++] | (this.value << 8);
208+
this.value = this.Data.Memory.Span[(int)this.pos++] | (this.value << 8);
209209
}
210210
else if (!this.eof)
211211
{

src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ internal class Vp8LBitReader : BitReaderBase
6363
/// </summary>
6464
/// <param name="data">Lossless compressed image data.</param>
6565
public Vp8LBitReader(IMemoryOwner<byte> data)
66+
: base(data)
6667
{
67-
this.Data = data;
6868
this.len = data.Memory.Length;
6969
this.value = 0;
7070
this.bitPos = 0;
@@ -88,11 +88,10 @@ public Vp8LBitReader(IMemoryOwner<byte> data)
8888
/// <param name="imageDataSize">The raw image data size in bytes.</param>
8989
/// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param>
9090
public Vp8LBitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator)
91+
: base(inputStream, (int)imageDataSize, memoryAllocator)
9192
{
9293
long length = imageDataSize;
9394

94-
this.ReadImageDataFromStream(inputStream, (int)imageDataSize, memoryAllocator);
95-
9695
this.len = length;
9796
this.value = 0;
9897
this.bitPos = 0;

0 commit comments

Comments
 (0)