Browse Source

stop processing jpeg file once image loaded.

pull/85/head
Scott Williams 9 years ago
parent
commit
ea0126f3c8
  1. 5
      src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs
  2. 15
      src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
  3. 38
      tests/ImageSharp.Tests/Formats/Jpg/BadEofJpegTests.cs
  4. 1
      tests/ImageSharp.Tests/TestImages.cs
  5. BIN
      tests/ImageSharp.Tests/TestImages/Formats/Jpg/badeof.jpg

5
src/ImageSharp.Formats.Jpeg/Components/Decoder/JpegScanDecoder.cs

@ -118,7 +118,8 @@ namespace ImageSharp.Formats.Jpg
/// Reads the blocks from the <see cref="JpegDecoderCore"/>-s stream, and processes them into the corresponding <see cref="JpegPixelArea"/> instances. /// Reads the blocks from the <see cref="JpegDecoderCore"/>-s stream, and processes them into the corresponding <see cref="JpegPixelArea"/> instances.
/// </summary> /// </summary>
/// <param name="decoder">The <see cref="JpegDecoderCore"/> instance</param> /// <param name="decoder">The <see cref="JpegDecoderCore"/> instance</param>
public void ProcessBlocks(JpegDecoderCore decoder) /// <returns>MCUs processed</returns>
public int ProcessBlocks(JpegDecoderCore decoder)
{ {
int blockCount = 0; int blockCount = 0;
int mcu = 0; int mcu = 0;
@ -229,6 +230,8 @@ namespace ImageSharp.Formats.Jpg
// for mx // for mx
} }
return mcu;
} }
private void ResetDc() private void ResetDc()

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

@ -79,6 +79,16 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
private YCbCrImage ycbcrImage; private YCbCrImage ycbcrImage;
/// <summary>
/// The MCU target
/// </summary>
private int mcuTarget;
/// <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.
/// </summary> /// </summary>
@ -187,7 +197,7 @@ namespace ImageSharp.Formats
} }
// Process the remaining segments until the End Of Image marker. // Process the remaining segments until the End Of Image marker.
while (true) while (this.mcuTarget < 1 || this.mcuTarget != this.mcusProcessed)
{ {
this.ReadFull(this.Temp, 0, 2); this.ReadFull(this.Temp, 0, 2);
while (this.Temp[0] != 0xff) while (this.Temp[0] != 0xff)
@ -864,6 +874,7 @@ 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;
@ -1395,7 +1406,7 @@ 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);
scan.ProcessBlocks(this); this.mcusProcessed += scan.ProcessBlocks(this);
} }
/// <summary> /// <summary>

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

@ -0,0 +1,38 @@
// <copyright file="JpegTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
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)]
public void LoadImage<TColor>(TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
var image = provider.GetImage();
Assert.NotNull(image);
}
}
}

1
tests/ImageSharp.Tests/TestImages.cs

@ -51,6 +51,7 @@ namespace ImageSharp.Tests
public const string Festzug = "Jpg/Festzug.jpg"; public const string Festzug = "Jpg/Festzug.jpg";
public const string Hiyamugi = "Jpg/Hiyamugi.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 Snake = "Jpg/Snake.jpg";
public const string Lake = "Jpg/Lake.jpg"; public const string Lake = "Jpg/Lake.jpg";

BIN
tests/ImageSharp.Tests/TestImages/Formats/Jpg/badeof.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Loading…
Cancel
Save