From 8fe8948fa134b2810d8720c955ff26524a9c0136 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 8 Nov 2022 19:12:11 +0100 Subject: [PATCH] Review suggestions --- src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 15234d8ac5..76b486416b 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Six Labors Split License. using System.Buffers; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Constants; using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; @@ -507,7 +508,7 @@ internal class TiffDecoderCore : IImageDecoderInternals try { - int bytesPerTileRow = ((tileWidth * bitsPerPixel) + 7) / 8; + int bytesPerTileRow = RoundUpToMultipleOfEight(tileWidth * bitsPerPixel); int uncompressedTilesSize = bytesPerTileRow * tileLength; for (int i = 0; i < tilesBuffers.Length; i++) { @@ -611,8 +612,8 @@ internal class TiffDecoderCore : IImageDecoderInternals int height = pixels.Height; int bitsPerPixel = this.BitsPerPixel; - int bytesPerRow = ((width * bitsPerPixel) + 7) / 8; - int bytesPerTileRow = ((tileWidth * bitsPerPixel) + 7) / 8; + int bytesPerRow = RoundUpToMultipleOfEight(width * bitsPerPixel); + int bytesPerTileRow = RoundUpToMultipleOfEight(tileWidth * bitsPerPixel); int uncompressedTilesSize = bytesPerTileRow * tileLength; using IMemoryOwner tileBuffer = this.memoryAllocator.Allocate(uncompressedTilesSize, AllocationOptions.Clean); using IMemoryOwner uncompressedPixelBuffer = this.memoryAllocator.Allocate(tilesDown * tileLength * bytesPerRow, AllocationOptions.Clean); @@ -643,7 +644,7 @@ internal class TiffDecoderCore : IImageDecoderInternals int tileBufferOffset = 0; uncompressedPixelBufferOffset += bytesPerTileRow * tileX; - int bytesToCopy = isLastHorizontalTile ? ((bitsPerPixel * remainingPixelsInRow) + 7) / 8 : bytesPerTileRow; + int bytesToCopy = isLastHorizontalTile ? RoundUpToMultipleOfEight(bitsPerPixel * remainingPixelsInRow) : bytesPerTileRow; for (int y = 0; y < tileLength; y++) { Span uncompressedPixelRow = uncompressedPixelBufferSpan.Slice(uncompressedPixelBufferOffset, bytesToCopy); @@ -799,4 +800,7 @@ internal class TiffDecoderCore : IImageDecoderInternals return (int)height.Value; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int RoundUpToMultipleOfEight(int value) => (int)(((uint)value + 7) / 8); }