Browse Source

Avoid more bounds checks

pull/2134/head
Brian Popow 4 years ago
parent
commit
07d2e79c38
  1. 13
      src/ImageSharp/Formats/Tiff/Compression/BitWriterUtils.cs
  2. 4
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs

13
src/ImageSharp/Formats/Tiff/Compression/BitWriterUtils.cs

@ -3,6 +3,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.Formats.Tiff.Compression namespace SixLabors.ImageSharp.Formats.Tiff.Compression
{ {
@ -46,9 +47,17 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public static void WriteBit(Span<byte> buffer, int bufferPos, int bitPos) => buffer[bufferPos] |= (byte)(1 << (7 - bitPos)); public static void WriteBit(Span<byte> buffer, int bufferPos, int bitPos)
{
ref byte b = ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), bufferPos);
b |= (byte)(1 << (7 - bitPos));
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public static void WriteZeroBit(Span<byte> buffer, int bufferPos, int bitPos) => buffer[bufferPos] = (byte)(buffer[bufferPos] & ~(1 << (7 - bitPos))); public static void WriteZeroBit(Span<byte> buffer, int bufferPos, int bitPos)
{
ref byte b = ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), bufferPos);
b = (byte)(b & ~(1 << (7 - bitPos)));
}
} }
} }

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

@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.Constants;
using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -75,7 +77,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors
int bufferPos = (int)(bitsWritten / 8); int bufferPos = (int)(bitsWritten / 8);
for (int i = 0; i < scanLine.Length; i++) for (int i = 0; i < scanLine.Length; i++)
{ {
if (scanLine[i] == white) if (Unsafe.Add(ref MemoryMarshal.GetReference(scanLine), i) == white)
{ {
BitWriterUtils.WriteZeroBit(buffer, bufferPos, bitPos); BitWriterUtils.WriteZeroBit(buffer, bufferPos, bitPos);
} }

Loading…
Cancel
Save