From b560918106aa378b633ec0746e7dc1814ce9ac88 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Wed, 8 Feb 2023 18:10:11 +0100 Subject: [PATCH] do not use null!. --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 65ab8dfc15..f04ba25ff3 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -29,12 +29,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// /// The metadata. /// - private ImageMetadata metadata = null!; + private ImageMetadata? metadata; /// /// The tga specific metadata. /// - private TgaMetadata tgaMetadata = null!; + private TgaMetadata? tgaMetadata; /// /// The file header containing general information about the image. @@ -533,6 +533,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { TPixel color = default; bool invertX = InvertX(origin); + + Guard.NotNull(this.tgaMetadata); + if (this.tgaMetadata.AlphaChannelBits == 8 && !invertX) { using IMemoryOwner row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 4, 0); @@ -591,6 +594,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals where TPixel : unmanaged, IPixel { TPixel color = default; + + Guard.NotNull(this.tgaMetadata); + byte alphaBits = this.tgaMetadata.AlphaChannelBits; using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean)) { @@ -722,6 +728,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgra pixel"); } + Guard.NotNull(this.tgaMetadata); + byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : this.scratchBuffer[3]; color.FromBgra32(new Bgra32(this.scratchBuffer[2], this.scratchBuffer[1], this.scratchBuffer[0], alpha)); pixelRow[x] = color;