Skip to content

Commit 9f3f908

Browse files
Merge branch 'sn/nullable/format_tga' of https://github.com/stefannikolei/ImageSharp into sn/nullable/format_tga
2 parents c8fd4a3 + b560918 commit 9f3f908

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
2929
/// <summary>
3030
/// The metadata.
3131
/// </summary>
32-
private ImageMetadata metadata = null!;
32+
private ImageMetadata? metadata;
3333

3434
/// <summary>
3535
/// The tga specific metadata.
3636
/// </summary>
37-
private TgaMetadata tgaMetadata = null!;
37+
private TgaMetadata? tgaMetadata;
3838

3939
/// <summary>
4040
/// The file header containing general information about the image.
@@ -533,6 +533,9 @@ private void ReadBgra32<TPixel>(BufferedReadStream stream, int width, int height
533533
{
534534
TPixel color = default;
535535
bool invertX = InvertX(origin);
536+
537+
Guard.NotNull(this.tgaMetadata);
538+
536539
if (this.tgaMetadata.AlphaChannelBits == 8 && !invertX)
537540
{
538541
using IMemoryOwner<byte> row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 4, 0);
@@ -591,6 +594,9 @@ private void ReadRle<TPixel>(BufferedReadStream stream, int width, int height, B
591594
where TPixel : unmanaged, IPixel<TPixel>
592595
{
593596
TPixel color = default;
597+
598+
Guard.NotNull(this.tgaMetadata);
599+
594600
byte alphaBits = this.tgaMetadata.AlphaChannelBits;
595601
using (IMemoryOwner<byte> buffer = this.memoryAllocator.Allocate<byte>(width * height * bytesPerPixel, AllocationOptions.Clean))
596602
{
@@ -722,6 +728,8 @@ private void ReadBgra32Pixel<TPixel>(BufferedReadStream stream, int x, TPixel co
722728
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgra pixel");
723729
}
724730

731+
Guard.NotNull(this.tgaMetadata);
732+
725733
byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : this.scratchBuffer[3];
726734
color.FromBgra32(new Bgra32(this.scratchBuffer[2], this.scratchBuffer[1], this.scratchBuffer[0], alpha));
727735
pixelRow[x] = color;

0 commit comments

Comments
 (0)