Browse Source

Throw NotSupportedException for arithmetic coding and lossless jpeg's

pull/1742/head
Brian Popow 5 years ago
parent
commit
59a84498d5
  1. 45
      src/ImageSharp/Formats/Jpeg/JpegConstants.cs
  2. 18
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  3. 7
      src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs
  4. 15
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  5. 3
      tests/ImageSharp.Tests/TestImages.cs
  6. 3
      tests/Images/Input/Jpg/baseline/arithmetic_coding.jpg
  7. 3
      tests/Images/Input/Jpg/baseline/arithmetic_progressive.jpg
  8. 3
      tests/Images/Input/Jpg/baseline/lossless.jpg

45
src/ImageSharp/Formats/Jpeg/JpegConstants.cs

@ -133,6 +133,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// </summary>
public const byte APP15 = 0xEF;
/// <summary>
/// Define arithmetic coding conditioning marker.
/// </summary>
public const byte DAC = 0xCC;
/// <summary>
/// The text comment marker
/// </summary>
@ -173,6 +178,46 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// </summary>
public const byte SOF2 = 0xC2;
/// <summary>
/// Start of Frame marker, non differential lossless, Huffman coding.
/// </summary>
public const byte SOF3 = 0xC3;
/// <summary>
/// Start of Frame marker, differential lossless, Huffman coding.
/// </summary>
public const byte SOF7 = 0xC7;
/// <summary>
/// Start of Frame marker, non-differential, arithmetic coding, Extended sequential DCT.
/// </summary>
public const byte SOF9 = 0xC9;
/// <summary>
/// Start of Frame marker, non-differential, arithmetic coding, Progressive DCT.
/// </summary>
public const byte SOF10 = 0xCA;
/// <summary>
/// Start of Frame marker, non-differential, arithmetic coding, Lossless (sequential).
/// </summary>
public const byte SOF11 = 0xCB;
/// <summary>
/// Start of Frame marker, differential, arithmetic coding, Differential sequential DCT.
/// </summary>
public const byte SOF13 = 0xCD;
/// <summary>
/// Start of Frame marker, differential, arithmetic coding, Differential progressive DCT.
/// </summary>
public const byte SOF14 = 0xCE;
/// <summary>
/// Start of Frame marker, differential, arithmetic coding, Differential lossless (sequential).
/// </summary>
public const byte SOF15 = 0xCF;
/// <summary>
/// Define Huffman Table(s)
/// <remarks>

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

@ -247,6 +247,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, metadataOnly);
break;
case JpegConstants.Markers.SOF3:
case JpegConstants.Markers.SOF7:
JpegThrowHelper.ThrowNotSupportedException("Decoding lossless jpeg files is not supported.");
break;
case JpegConstants.Markers.SOF9:
case JpegConstants.Markers.SOF10:
case JpegConstants.Markers.SOF11:
case JpegConstants.Markers.SOF13:
case JpegConstants.Markers.SOF14:
case JpegConstants.Markers.SOF15:
JpegThrowHelper.ThrowNotSupportedException("Decoding jpeg files with arithmetic coding is not supported.");
break;
case JpegConstants.Markers.SOS:
if (!metadataOnly)
{
@ -326,6 +340,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
case JpegConstants.Markers.COM:
stream.Skip(remaining);
break;
case JpegConstants.Markers.DAC:
JpegThrowHelper.ThrowNotSupportedException("Decoding jpeg files with arithmetic coding is not supported.");
break;
}
}

7
src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

@ -8,6 +8,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{
internal static class JpegThrowHelper
{
/// <summary>
/// Cold path optimization for throwing <see cref="NotSupportedException"/>'s.
/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);
/// <summary>
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
/// </summary>

15
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -74,7 +74,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
byte[] bytes = TestFile.Create(TestImages.Jpeg.Progressive.Progress).Bytes;
using var ms = new MemoryStream(bytes);
using var bufferedStream = new BufferedReadStream(Configuration.Default, ms);
var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
using var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
using Image<Rgba32> image = decoder.Decode<Rgba32>(bufferedStream, cancellationToken: default);
// I don't know why these numbers are different. All I know is that the decoder works
@ -174,6 +174,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
await Assert.ThrowsAsync<TaskCanceledException>(async () => await Image.IdentifyAsync(config, "someFakeFile", cts.Token));
}
[Theory]
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCoding, PixelTypes.Rgba32)]
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingProgressive, PixelTypes.Rgba32)]
[WithFile(TestImages.Jpeg.Baseline.Lossless, PixelTypes.Rgba32)]
public void ThrowsNotSupported_WithUnsupportedJpegs<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
Assert.Throws<NotSupportedException>(() =>
{
using Image<TPixel> image = provider.GetImage(JpegDecoder);
});
}
// https://github.com/SixLabors/ImageSharp/pull/1732
[Theory]
[WithFile(TestImages.Jpeg.Issues.WrongColorSpace, PixelTypes.Rgba32)]

3
tests/ImageSharp.Tests/TestImages.cs

@ -200,6 +200,9 @@ namespace SixLabors.ImageSharp.Tests
public const string App13WithEmptyIptc = "Jpg/baseline/iptc-psAPP13-wIPTCempty.jpg";
public const string HistogramEqImage = "Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg";
public const string ForestBridgeDifferentComponentsQuality = "Jpg/baseline/forest_bridge.jpg";
public const string ArithmeticCoding = "Jpg/baseline/arithmetic_coding.jpg";
public const string ArithmeticCodingProgressive = "Jpg/baseline/arithmetic_progressive.jpg";
public const string Lossless = "Jpg/baseline/lossless.jpg";
public static readonly string[] All =
{

3
tests/Images/Input/Jpg/baseline/arithmetic_coding.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fec7012d9ae52a12c4617fd7526e506feee812fc67e923a76b2ca88c95f7a538
size 3111

3
tests/Images/Input/Jpg/baseline/arithmetic_progressive.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3af237c172248d39c7b82c879de3d162e4dafaf36dc298add210740843edd33f
size 3129

3
tests/Images/Input/Jpg/baseline/lossless.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8937e245885e1f280e1843ad48a4349ec1a3f71f86c954229cd44160aeeaaac4
size 209584
Loading…
Cancel
Save