diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
index 3edd6f70e2..2dd547c916 100644
--- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
+++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
@@ -187,7 +187,10 @@ namespace ImageSharp.Formats
}
// Process the remaining segments until the End Of Image marker.
- while (true)
+ bool processBytes = true;
+
+ // we can't currently short circute progressive images so don't try.
+ while (processBytes)
{
this.ReadFull(this.Temp, 0, 2);
while (this.Temp[0] != 0xff)
@@ -294,7 +297,15 @@ namespace ImageSharp.Formats
return;
}
+ // when this is a progressive image this gets called a number of times
+ // need to know how many times this should be called in total.
this.ProcessStartOfScan(remaining);
+ if (!this.IsProgressive)
+ {
+ // if this is not a progressive image we can stop processing bytes as we now have the image data.
+ processBytes = false;
+ }
+
break;
case JpegConstants.Markers.DRI:
if (configOnly)
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
new file mode 100644
index 0000000000..5dd0e51aca
--- /dev/null
+++ b/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
@@ -0,0 +1,40 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using ImageSharp.Formats;
+using Xunit;
+using Xunit.Abstractions;
+// ReSharper disable InconsistentNaming
+
+namespace ImageSharp.Tests
+{
+ using System.Numerics;
+
+ using ImageSharp.Formats.Jpg;
+ using ImageSharp.Processing;
+
+ public class BadEOFJpegTests : MeasureFixture
+ {
+ public BadEOFJpegTests(ITestOutputHelper output)
+ : base(output)
+ {
+ }
+
+ [Theory]
+ [WithFile(TestImages.Jpeg.BadEOF, PixelTypes.Color)]
+ [WithFile(TestImages.Jpeg.Progress, PixelTypes.Color)]
+ public void LoadImage(TestImageProvider provider)
+ where TColor : struct, IPackedPixel, IEquatable
+ {
+ var image = provider.GetImage();
+ Assert.NotNull(image);
+ provider.Utility.SaveTestOutputFile(image, "bmp");
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs
index 89b3c0f0d1..9e2d21a9bd 100644
--- a/tests/ImageSharp.Tests/TestImages.cs
+++ b/tests/ImageSharp.Tests/TestImages.cs
@@ -51,6 +51,7 @@ namespace ImageSharp.Tests
public const string Festzug = "Jpg/Festzug.jpg";
public const string Hiyamugi = "Jpg/Hiyamugi.jpg";
+ public const string BadEOF = "Jpg/badeof.jpg";
public const string Snake = "Jpg/Snake.jpg";
public const string Lake = "Jpg/Lake.jpg";
diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Jpg/badeof.jpg b/tests/ImageSharp.Tests/TestImages/Formats/Jpg/badeof.jpg
new file mode 100644
index 0000000000..1ba3418dee
Binary files /dev/null and b/tests/ImageSharp.Tests/TestImages/Formats/Jpg/badeof.jpg differ