Browse Source

Refactor BitReaderBase

pull/2364/head
Stefan Nikolei 3 years ago
parent
commit
ea2c4fa5f8
  1. 18
      src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs
  2. 8
      src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs
  3. 5
      src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Buffers; using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Webp.BitReader; namespace SixLabors.ImageSharp.Formats.Webp.BitReader;
@ -14,10 +13,14 @@ internal abstract class BitReaderBase : IDisposable
{ {
private bool isDisposed; private bool isDisposed;
protected BitReaderBase(IMemoryOwner<byte> data) => this.Data = data;
protected BitReaderBase(Stream inputStream, int imageDataSize, MemoryAllocator memoryAllocator) => this.Data = ReadImageDataFromStream(inputStream, imageDataSize, memoryAllocator);
/// <summary> /// <summary>
/// Gets or sets the raw encoded image data. /// Gets or sets the raw encoded image data.
/// </summary> /// </summary>
public IMemoryOwner<byte>? Data { get; set; } public IMemoryOwner<byte> Data { get; set; }
/// <summary> /// <summary>
/// Copies the raw encoded image data from the stream into a byte array. /// Copies the raw encoded image data from the stream into a byte array.
@ -25,12 +28,13 @@ internal abstract class BitReaderBase : IDisposable
/// <param name="input">The input stream.</param> /// <param name="input">The input stream.</param>
/// <param name="bytesToRead">Number of bytes to read as indicated from the chunk size.</param> /// <param name="bytesToRead">Number of bytes to read as indicated from the chunk size.</param>
/// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param> /// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param>
[MemberNotNull(nameof(Data))] protected static IMemoryOwner<byte> ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator)
protected void ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator)
{ {
this.Data = memoryAllocator.Allocate<byte>(bytesToRead); IMemoryOwner<byte> data = memoryAllocator.Allocate<byte>(bytesToRead);
Span<byte> dataSpan = this.Data.Memory.Span; Span<byte> dataSpan = data.Memory.Span;
input.Read(dataSpan[..bytesToRead], 0, bytesToRead); input.Read(dataSpan[..bytesToRead], 0, bytesToRead);
return data;
} }
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
@ -42,7 +46,7 @@ internal abstract class BitReaderBase : IDisposable
if (disposing) if (disposing)
{ {
this.Data?.Dispose(); this.Data.Dispose();
} }
this.isDisposed = true; this.isDisposed = true;

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

@ -57,12 +57,12 @@ internal class Vp8BitReader : BitReaderBase
/// <param name="partitionLength">The partition length.</param> /// <param name="partitionLength">The partition length.</param>
/// <param name="startPos">Start index in the data array. Defaults to 0.</param> /// <param name="startPos">Start index in the data array. Defaults to 0.</param>
public Vp8BitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator, uint partitionLength, int startPos = 0) public Vp8BitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator, uint partitionLength, int startPos = 0)
: base(inputStream, (int)imageDataSize, memoryAllocator)
{ {
Guard.MustBeLessThan(imageDataSize, int.MaxValue, nameof(imageDataSize)); Guard.MustBeLessThan(imageDataSize, int.MaxValue, nameof(imageDataSize));
this.ImageDataSize = imageDataSize; this.ImageDataSize = imageDataSize;
this.PartitionLength = partitionLength; this.PartitionLength = partitionLength;
this.ReadImageDataFromStream(inputStream, (int)imageDataSize, memoryAllocator);
this.InitBitreader(partitionLength, startPos); this.InitBitreader(partitionLength, startPos);
} }
@ -73,8 +73,8 @@ internal class Vp8BitReader : BitReaderBase
/// <param name="partitionLength">The partition length.</param> /// <param name="partitionLength">The partition length.</param>
/// <param name="startPos">Start index in the data array. Defaults to 0.</param> /// <param name="startPos">Start index in the data array. Defaults to 0.</param>
public Vp8BitReader(IMemoryOwner<byte> imageData, uint partitionLength, int startPos = 0) public Vp8BitReader(IMemoryOwner<byte> imageData, uint partitionLength, int startPos = 0)
: base(imageData)
{ {
this.Data = imageData;
this.ImageDataSize = (uint)imageData.Memory.Length; this.ImageDataSize = (uint)imageData.Memory.Length;
this.PartitionLength = partitionLength; this.PartitionLength = partitionLength;
this.InitBitreader(partitionLength, startPos); this.InitBitreader(partitionLength, startPos);
@ -186,7 +186,7 @@ internal class Vp8BitReader : BitReaderBase
{ {
if (this.pos < this.bufferMax) if (this.pos < this.bufferMax)
{ {
ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data!.Memory.Span.Slice((int)this.pos, 8)); ulong inBits = BinaryPrimitives.ReadUInt64LittleEndian(this.Data.Memory.Span.Slice((int)this.pos, 8));
this.pos += BitsCount >> 3; this.pos += BitsCount >> 3;
ulong bits = ByteSwap64(inBits); ulong bits = ByteSwap64(inBits);
bits >>= 64 - BitsCount; bits >>= 64 - BitsCount;
@ -205,7 +205,7 @@ internal class Vp8BitReader : BitReaderBase
if (this.pos < this.bufferEnd) if (this.pos < this.bufferEnd)
{ {
this.bits += 8; this.bits += 8;
this.value = this.Data!.Memory.Span[(int)this.pos++] | (this.value << 8); this.value = this.Data.Memory.Span[(int)this.pos++] | (this.value << 8);
} }
else if (!this.eof) else if (!this.eof)
{ {

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

@ -63,8 +63,8 @@ internal class Vp8LBitReader : BitReaderBase
/// </summary> /// </summary>
/// <param name="data">Lossless compressed image data.</param> /// <param name="data">Lossless compressed image data.</param>
public Vp8LBitReader(IMemoryOwner<byte> data) public Vp8LBitReader(IMemoryOwner<byte> data)
: base(data)
{ {
this.Data = data;
this.len = data.Memory.Length; this.len = data.Memory.Length;
this.value = 0; this.value = 0;
this.bitPos = 0; this.bitPos = 0;
@ -88,11 +88,10 @@ internal class Vp8LBitReader : BitReaderBase
/// <param name="imageDataSize">The raw image data size in bytes.</param> /// <param name="imageDataSize">The raw image data size in bytes.</param>
/// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param> /// <param name="memoryAllocator">Used for allocating memory during reading data from the stream.</param>
public Vp8LBitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator) public Vp8LBitReader(Stream inputStream, uint imageDataSize, MemoryAllocator memoryAllocator)
: base(inputStream, (int)imageDataSize, memoryAllocator)
{ {
long length = imageDataSize; long length = imageDataSize;
this.ReadImageDataFromStream(inputStream, (int)imageDataSize, memoryAllocator);
this.len = length; this.len = length;
this.value = 0; this.value = 0;
this.bitPos = 0; this.bitPos = 0;

Loading…
Cancel
Save