From 432c0d7262e66b71dd1e49a241c8ca4d1a2979dd Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 29 May 2022 18:05:39 +0200 Subject: [PATCH] Avoid bounds checks in BlackIsZero and WhiteIsZero --- .../BlackIsZero1TiffColor{TPixel}.cs | 9 +++++++-- .../WhiteIsZero1TiffColor{TPixel}.cs | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs index 6cb9c61761..2d8e21d6cf 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -23,19 +25,22 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation colorBlack.FromRgba32(Color.Black); colorWhite.FromRgba32(Color.White); + ref byte dataRef = ref MemoryMarshal.GetReference(data); for (int y = top; y < top + height; y++) { Span pixelRowSpan = pixels.DangerousGetRowSpan(y); + ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan); for (int x = left; x < left + width; x += 8) { - byte b = data[offset++]; + byte b = Unsafe.Add(ref dataRef, offset++); int maxShift = Math.Min(left + width - x, 8); for (int shift = 0; shift < maxShift; shift++) { int bit = (b >> (7 - shift)) & 1; - pixelRowSpan[x + shift] = bit == 0 ? colorBlack : colorWhite; + ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift); + pixel = bit == 0 ? colorBlack : colorWhite; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs index 41152ac727..ad7d6c08a9 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor{TPixel}.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -22,19 +24,22 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation colorBlack.FromRgba32(Color.Black); colorWhite.FromRgba32(Color.White); + ref byte dataRef = ref MemoryMarshal.GetReference(data); for (int y = top; y < top + height; y++) { Span pixelRowSpan = pixels.DangerousGetRowSpan(y); + ref TPixel pixelRowRef = ref MemoryMarshal.GetReference(pixelRowSpan); for (int x = left; x < left + width; x += 8) { - byte b = data[offset++]; + byte b = Unsafe.Add(ref dataRef, offset++); int maxShift = Math.Min(left + width - x, 8); for (int shift = 0; shift < maxShift; shift++) { int bit = (b >> (7 - shift)) & 1; - pixelRowSpan[x + shift] = bit == 0 ? colorWhite : colorBlack; + ref TPixel pixel = ref Unsafe.Add(ref pixelRowRef, x + shift); + pixel = bit == 0 ? colorWhite : colorBlack; } } }