Browse Source

Access data span only for new byte, not for every bit

pull/2134/head
Brian Popow 5 years ago
parent
commit
a26bfe0123
  1. 3
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/ModifiedHuffmanBitReader.cs
  2. 36
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs

3
src/ImageSharp/Formats/Tiff/Compression/Decompressors/ModifiedHuffmanBitReader.cs

@ -57,8 +57,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
if (remainder != 0) if (remainder != 0)
{ {
// Skip padding bits, move to next byte. // Skip padding bits, move to next byte.
this.Position++; this.AdvancePosition();
this.ResetBitsRead();
} }
} }

36
src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs

@ -229,12 +229,20 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
this.RunLength = 0; this.RunLength = 0;
this.eolPadding = eolPadding; this.eolPadding = eolPadding;
Span<byte> dataSpan = this.Data.GetSpan();
this.DataAtPosition = dataSpan[(int)this.Position];
if (this.eolPadding) if (this.eolPadding)
{ {
this.maxCodeLength = 24; this.maxCodeLength = 24;
} }
} }
/// <summary>
/// Gets or sets the byte at the given position.
/// </summary>
private byte DataAtPosition { get; set; }
/// <summary> /// <summary>
/// Gets the current value. /// Gets the current value.
/// </summary> /// </summary>
@ -453,6 +461,23 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
return v; return v;
} }
/// <summary>
/// Advances the position by one byte.
/// </summary>
/// <returns>True, if data could be advanced by one byte.</returns>
protected bool AdvancePosition()
{
this.LoadNewByte();
if (this.Position < (ulong)this.DataLength)
{
Span<byte> dataSpan = this.Data.GetSpan();
this.DataAtPosition = Unsafe.Add(ref MemoryMarshal.GetReference(dataSpan), (int)this.Position);
return true;
}
return false;
}
private uint WhiteTerminatingCodeRunLength() private uint WhiteTerminatingCodeRunLength()
{ {
switch (this.CurValueBitsRead) switch (this.CurValueBitsRead)
@ -811,13 +836,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
{ {
if (this.BitsRead >= 8) if (this.BitsRead >= 8)
{ {
this.LoadNewByte(); this.AdvancePosition();
} }
Span<byte> dataSpan = this.Data.GetSpan();
int shift = 8 - this.BitsRead - 1; int shift = 8 - this.BitsRead - 1;
ref byte dataAtPosition = ref Unsafe.Add(ref MemoryMarshal.GetReference(dataSpan), (int)this.Position); uint bit = (uint)((this.DataAtPosition & (1 << shift)) != 0 ? 1 : 0);
uint bit = (uint)((dataAtPosition & (1 << shift)) != 0 ? 1 : 0);
this.BitsRead++; this.BitsRead++;
return bit; return bit;
@ -827,11 +850,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
{ {
this.Position++; this.Position++;
this.ResetBitsRead(); this.ResetBitsRead();
if (this.Position >= (ulong)this.DataLength)
{
TiffThrowHelper.ThrowImageFormatException("tiff image has invalid ccitt compressed data");
}
} }
private void ReadImageDataFromStream(Stream input, int bytesToRead) private void ReadImageDataFromStream(Stream input, int bytesToRead)

Loading…
Cancel
Save