diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
index 5ab5764e26..c463ad18e5 100644
--- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
+++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
@@ -80,14 +80,9 @@ namespace ImageSharp.Formats
private YCbCrImage ycbcrImage;
///
- /// The MCU target
+ /// The MCU counter
///
- private int mcuTarget;
-
- ///
- /// The MCUs processed
- ///
- private int mcusProcessed;
+ private int mcuCounter;
///
/// Initializes a new instance of the class.
@@ -197,7 +192,10 @@ namespace ImageSharp.Formats
}
// Process the remaining segments until the End Of Image marker.
- while (this.mcuTarget < 1 || this.mcuTarget != this.mcusProcessed)
+ bool mcuSet = false;
+
+ // we can't currently short circute progressive images so don't try.
+ while (this.IsProgressive || !mcuSet || this.mcuCounter > 0)
{
this.ReadFull(this.Temp, 0, 2);
while (this.Temp[0] != 0xff)
@@ -304,6 +302,9 @@ 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.
+ mcuSet = true;
this.ProcessStartOfScan(remaining);
break;
case JpegConstants.Markers.DRI:
@@ -874,7 +875,6 @@ namespace ImageSharp.Formats
/// The vertical MCU count
private void MakeImage(int mxx, int myy)
{
- this.mcuTarget = mxx * myy;
if (this.grayImage.IsInitialized || this.ycbcrImage != null)
{
return;
@@ -1406,7 +1406,9 @@ namespace ImageSharp.Formats
JpegScanDecoder.Init(&scan, this, remaining);
this.Bits = default(Bits);
this.MakeImage(scan.XNumberOfMCUs, scan.YNumberOfMCUs);
- this.mcusProcessed += scan.ProcessBlocks(this);
+
+ this.mcuCounter = scan.XNumberOfMCUs * scan.YNumberOfMCUs;
+ this.mcuCounter -= scan.ProcessBlocks(this);
}
///
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
index 0697dbb641..5dd0e51aca 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
@@ -28,11 +28,13 @@ namespace ImageSharp.Tests
[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