Browse Source

prevent progressive images breaking.

top progressive images from shorting the loop as they break if you do.
af/merge-core
Scott Williams 10 years ago
parent
commit
c329941f97
  1. 22
      src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
  2. 2
      tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs

22
src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs

@ -80,14 +80,9 @@ namespace ImageSharp.Formats
private YCbCrImage ycbcrImage; private YCbCrImage ycbcrImage;
/// <summary> /// <summary>
/// The MCU target /// The MCU counter
/// </summary> /// </summary>
private int mcuTarget; private int mcuCounter;
/// <summary>
/// The MCUs processed
/// </summary>
private int mcusProcessed;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JpegDecoderCore" /> class. /// Initializes a new instance of the <see cref="JpegDecoderCore" /> class.
@ -197,7 +192,10 @@ namespace ImageSharp.Formats
} }
// Process the remaining segments until the End Of Image marker. // 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); this.ReadFull(this.Temp, 0, 2);
while (this.Temp[0] != 0xff) while (this.Temp[0] != 0xff)
@ -304,6 +302,9 @@ namespace ImageSharp.Formats
return; 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); this.ProcessStartOfScan(remaining);
break; break;
case JpegConstants.Markers.DRI: case JpegConstants.Markers.DRI:
@ -874,7 +875,6 @@ namespace ImageSharp.Formats
/// <param name="myy">The vertical MCU count</param> /// <param name="myy">The vertical MCU count</param>
private void MakeImage(int mxx, int myy) private void MakeImage(int mxx, int myy)
{ {
this.mcuTarget = mxx * myy;
if (this.grayImage.IsInitialized || this.ycbcrImage != null) if (this.grayImage.IsInitialized || this.ycbcrImage != null)
{ {
return; return;
@ -1406,7 +1406,9 @@ namespace ImageSharp.Formats
JpegScanDecoder.Init(&scan, this, remaining); JpegScanDecoder.Init(&scan, this, remaining);
this.Bits = default(Bits); this.Bits = default(Bits);
this.MakeImage(scan.XNumberOfMCUs, scan.YNumberOfMCUs); this.MakeImage(scan.XNumberOfMCUs, scan.YNumberOfMCUs);
this.mcusProcessed += scan.ProcessBlocks(this);
this.mcuCounter = scan.XNumberOfMCUs * scan.YNumberOfMCUs;
this.mcuCounter -= scan.ProcessBlocks(this);
} }
/// <summary> /// <summary>

2
tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs

@ -28,11 +28,13 @@ namespace ImageSharp.Tests
[Theory] [Theory]
[WithFile(TestImages.Jpeg.BadEOF, PixelTypes.Color)] [WithFile(TestImages.Jpeg.BadEOF, PixelTypes.Color)]
[WithFile(TestImages.Jpeg.Progress, PixelTypes.Color)]
public void LoadImage<TColor>(TestImageProvider<TColor> provider) public void LoadImage<TColor>(TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
var image = provider.GetImage(); var image = provider.GetImage();
Assert.NotNull(image); Assert.NotNull(image);
provider.Utility.SaveTestOutputFile(image, "bmp");
} }
} }
} }
Loading…
Cancel
Save