From 0ae59fdba44e300963b18cee7285bb7691f17388 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 29 May 2022 14:01:47 +0200 Subject: [PATCH] Avoid bounds checks in GetBit() --- .../Formats/Tiff/Compression/Decompressors/T4BitReader.cs | 4 +++- .../Tiff/Compression/Decompressors/T6TiffCompression.cs | 6 ------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs index 122316f94f..15e2b3c307 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T4BitReader.cs +++ b/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 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; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs index 71d7c4d556..c769d4d685 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs +++ b/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; /// @@ -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); } ///