diff --git a/src/ImageSharp/Formats/Tiff/Compression/T4BitReader.cs b/src/ImageSharp/Formats/Tiff/Compression/T4BitReader.cs index 3a4f8bd51..7558382de 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/T4BitReader.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/T4BitReader.cs @@ -6,7 +6,7 @@ using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; - +using SixLabors.ImageSharp.Formats.Experimental.Tiff.Utils; using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression @@ -230,7 +230,7 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression public T4BitReader(Stream input, int bytesToRead, MemoryAllocator allocator, bool isModifiedHuffman = false) { this.Data = allocator.Allocate(bytesToRead); - this.ReadImageDataFromStream(input, bytesToRead, allocator); + this.ReadImageDataFromStream(input, bytesToRead); this.isModifiedHuffmanRle = isModifiedHuffman; this.dataLength = bytesToRead; @@ -253,46 +253,22 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression /// /// Gets a value indicating whether there is more data to read left. /// - public bool HasMoreData - { - get - { - return this.position < (ulong)this.dataLength - 1; - } - } + public bool HasMoreData => this.position < (ulong)this.dataLength - 1; /// /// Gets a value indicating whether the current run is a white pixel run, otherwise its a black pixel run. /// - public bool IsWhiteRun - { - get - { - return this.isWhiteRun; - } - } + public bool IsWhiteRun => this.isWhiteRun; /// /// Gets the number of pixels in the current run. /// - public uint RunLength - { - get - { - return this.runLength; - } - } + public uint RunLength => this.runLength; /// /// Gets a value indicating whether the end of a pixel row has been reached. /// - public bool IsEndOfScanLine - { - get - { - return this.curValueBitsRead == 12 && this.value == 1; - } - } + public bool IsEndOfScanLine => this.curValueBitsRead == 12 && this.value == 1; /// /// Read the next run of pixels. @@ -834,25 +810,10 @@ namespace SixLabors.ImageSharp.Formats.Experimental.Tiff.Compression } } - private void ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator allocator) + private void ReadImageDataFromStream(Stream input, int bytesToRead) { - int bufferLength = 4096; - IMemoryOwner buffer = allocator.Allocate(bufferLength); - Span bufferSpan = buffer.GetSpan(); Span dataSpan = this.Data.GetSpan(); - - int read; - while (bytesToRead > 0 && (read = input.Read(bufferSpan, 0, Math.Min(bufferLength, bytesToRead))) > 0) - { - buffer.Slice(0, read).CopyTo(dataSpan); - bytesToRead -= read; - dataSpan = dataSpan.Slice(read); - } - - if (bytesToRead > 0) - { - TiffThrowHelper.ThrowImageFormatException("tiff image file has insufficient data"); - } + input.ReadFull(dataSpan, bytesToRead); } } }