Skip to content

Commit 6cda7b0

Browse files
Fix scanline lengths
1 parent c306c56 commit 6cda7b0

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/ImageSharp/Formats/Gif/GifEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static GifMetadata GetGifMetadata<TPixel>(Image<TPixel> image)
176176
{
177177
if (image.Metadata.TryGetGifMetadata(out GifMetadata? gif))
178178
{
179-
return gif;
179+
return (GifMetadata)gif.DeepClone();
180180
}
181181

182182
if (image.Metadata.TryGetPngMetadata(out PngMetadata? png))

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,12 @@ private void DecodePixelData<TPixel>(
764764
{
765765
cancellationToken.ThrowIfCancellationRequested();
766766
int bytesPerFrameScanline = this.CalculateScanlineLength((int)frameControl.Width) + 1;
767-
Span<byte> scanlineSpan = this.scanline.GetSpan()[..bytesPerFrameScanline];
767+
Span<byte> scanSpan = this.scanline.GetSpan()[..bytesPerFrameScanline];
768+
Span<byte> prevSpan = this.scanline.GetSpan()[..bytesPerFrameScanline];
769+
768770
while (currentRowBytesRead < bytesPerFrameScanline)
769771
{
770-
int bytesRead = compressedStream.Read(scanlineSpan, currentRowBytesRead, bytesPerFrameScanline - currentRowBytesRead);
772+
int bytesRead = compressedStream.Read(scanSpan, currentRowBytesRead, bytesPerFrameScanline - currentRowBytesRead);
771773
if (bytesRead <= 0)
772774
{
773775
return;
@@ -778,33 +780,33 @@ private void DecodePixelData<TPixel>(
778780

779781
currentRowBytesRead = 0;
780782

781-
switch ((FilterType)scanlineSpan[0])
783+
switch ((FilterType)scanSpan[0])
782784
{
783785
case FilterType.None:
784786
break;
785787

786788
case FilterType.Sub:
787-
SubFilter.Decode(scanlineSpan, this.bytesPerPixel);
789+
SubFilter.Decode(scanSpan, this.bytesPerPixel);
788790
break;
789791

790792
case FilterType.Up:
791-
UpFilter.Decode(scanlineSpan, this.previousScanline.GetSpan());
793+
UpFilter.Decode(scanSpan, prevSpan);
792794
break;
793795

794796
case FilterType.Average:
795-
AverageFilter.Decode(scanlineSpan, this.previousScanline.GetSpan(), this.bytesPerPixel);
797+
AverageFilter.Decode(scanSpan, prevSpan, this.bytesPerPixel);
796798
break;
797799

798800
case FilterType.Paeth:
799-
PaethFilter.Decode(scanlineSpan, this.previousScanline.GetSpan(), this.bytesPerPixel);
801+
PaethFilter.Decode(scanSpan, prevSpan, this.bytesPerPixel);
800802
break;
801803

802804
default:
803805
PngThrowHelper.ThrowUnknownFilter();
804806
break;
805807
}
806808

807-
this.ProcessDefilteredScanline(frameControl, currentRow, scanlineSpan, imageFrame, pngMetadata, blendRowBuffer);
809+
this.ProcessDefilteredScanline(frameControl, currentRow, scanSpan, imageFrame, pngMetadata, blendRowBuffer);
808810
this.SwapScanlineBuffers();
809811
currentRow++;
810812
}

0 commit comments

Comments
 (0)