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;