Browse Source

Avoid bounds checks in GetBit()

pull/2134/head
Brian Popow 4 years ago
parent
commit
0ae59fdba4
  1. 4
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs
  2. 6
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs

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

@ -6,6 +6,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Tiff.Constants;
using SixLabors.ImageSharp.Memory;
@ -815,7 +816,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
Span<byte> dataSpan = this.Data.GetSpan();
int shift = 8 - this.BitsRead - 1;
uint bit = (uint)((dataSpan[(int)this.Position] & (1 << shift)) != 0 ? 1 : 0);
ref byte dataAtPosition = ref Unsafe.Add(ref MemoryMarshal.GetReference(dataSpan), (int)this.Position);
uint bit = (uint)((dataAtPosition & (1 << shift)) != 0 ? 1 : 0);
this.BitsRead++;
return bit;

6
src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs

@ -15,10 +15,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
{
private readonly bool isWhiteZero;
private readonly byte whiteValue;
private readonly byte blackValue;
private readonly int width;
/// <summary>
@ -40,8 +36,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
this.FillOrder = fillOrder;
this.width = width;
this.isWhiteZero = photometricInterpretation == TiffPhotometricInterpretation.WhiteIsZero;
this.whiteValue = (byte)(this.isWhiteZero ? 0 : 1);
this.blackValue = (byte)(this.isWhiteZero ? 1 : 0);
}
/// <summary>

Loading…
Cancel
Save