@@ -114,6 +114,11 @@ internal sealed class PngDecoderCore : IImageDecoderInternals
114114 /// </summary>
115115 private PngChunk ? nextChunk ;
116116
117+ /// <summary>
118+ /// A value indicating whether the image data has been read.
119+ /// </summary>
120+ private bool hasImageData ;
121+
117122 /// <summary>
118123 /// Initializes a new instance of the <see cref="PngDecoderCore"/> class.
119124 /// </summary>
@@ -565,7 +570,11 @@ private void ReadScanlines<TPixel>(PngChunk chunk, ImageFrame<TPixel> image, Png
565570 where TPixel : unmanaged, IPixel < TPixel >
566571 {
567572 using var deframeStream = new ZlibInflateStream ( this . currentStream , this . ReadNextDataChunk ) ;
568- deframeStream . AllocateNewBytes ( chunk . Length , true ) ;
573+ if ( ! deframeStream . AllocateNewBytes ( chunk . Length , ! this . hasImageData ) )
574+ {
575+ return ;
576+ }
577+
569578 DeflateStream dataStream = deframeStream . CompressedStream ;
570579
571580 if ( this . header . InterlaceMethod == PngInterlaceMode . Adam7 )
@@ -596,7 +605,7 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
596605 int bytesRead = compressedStream . Read ( scanlineSpan , this . currentRowBytesRead , this . bytesPerScanline - this . currentRowBytesRead ) ;
597606 if ( bytesRead <= 0 )
598607 {
599- return ;
608+ goto EXIT ;
600609 }
601610
602611 this . currentRowBytesRead += bytesRead ;
@@ -635,6 +644,9 @@ private void DecodePixelData<TPixel>(DeflateStream compressedStream, ImageFrame<
635644 this . SwapScanlineBuffers ( ) ;
636645 this . currentRow ++ ;
637646 }
647+
648+ EXIT :
649+ this . hasImageData = true ;
638650 }
639651
640652 /// <summary>
@@ -672,7 +684,7 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
672684 int bytesRead = compressedStream . Read ( this . scanline . GetSpan ( ) , this . currentRowBytesRead , bytesPerInterlaceScanline - this . currentRowBytesRead ) ;
673685 if ( bytesRead <= 0 )
674686 {
675- return ;
687+ goto EXIT ;
676688 }
677689
678690 this . currentRowBytesRead += bytesRead ;
@@ -729,6 +741,9 @@ private void DecodeInterlacedPixelData<TPixel>(DeflateStream compressedStream, I
729741 pass = 0 ;
730742 break ;
731743 }
744+
745+ EXIT :
746+ this . hasImageData = true ;
732747 }
733748 }
734749
0 commit comments