From 7b2923d7efe82188f35fd86bb61fbd450160c547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 26 Mar 2023 13:44:25 +0200 Subject: [PATCH] Use constant to specify the size of the buffer It's only one value for the fixed buffer size and the creation of the span -- so less error prone once the value needs to be updated. --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 5 +++-- .../Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs | 1 + src/ImageSharp/Formats/Png/PngEncoderCore.cs | 5 +++-- src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs | 5 +++-- src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs | 5 +++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index efde4e9aa8..55ad2c4585 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -765,8 +765,9 @@ internal sealed class GifDecoderCore : IImageDecoderInternals private unsafe struct ScratchBuffer { - private fixed byte scratch[16]; + private const int Size = 16; + private fixed byte scratch[Size]; - public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], 16); + public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], Size); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs index 5ecf779615..02a346ff07 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs @@ -53,6 +53,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder private ArithmeticDecodingTable[] acDecodingTables; + // Don't make this a ReadOnlySpan, as the values need to get updated. private readonly byte[] fixedBin = { 113, 0, 0, 0 }; private readonly CancellationToken cancellationToken; diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index a2edbc4c3f..fb1d33277a 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -1412,8 +1412,9 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable private unsafe struct ScratchBuffer { - private fixed byte scratch[16]; + private const int Size = 16; + private fixed byte scratch[Size]; - public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], 16); + public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], Size); } } diff --git a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs index 8baf2cc156..ab78d18604 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs @@ -260,8 +260,9 @@ internal abstract class BitWriterBase private unsafe struct ScratchBuffer { - private fixed byte scratch[4]; + private const int Size = 4; + private fixed byte scratch[Size]; - public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], 4); + public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], Size); } } diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index e3c2797bf3..1f7c7586eb 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -1834,8 +1834,9 @@ internal class Vp8LEncoder : IDisposable /// private unsafe struct ScratchBuffer { - private fixed int scratch[256]; + private const int Size = 256; + private fixed int scratch[Size]; - public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], 256); + public Span Span => MemoryMarshal.CreateSpan(ref this.scratch[0], Size); } }