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; } } }