From c988208b4fbb9a5196385d1b85496c2205fbce52 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 24 Mar 2022 17:28:54 +0100 Subject: [PATCH] Add tests for arithmetic coding --- .../Jpeg/Components/Decoder/JpegBitReader.cs | 3 +-- .../Formats/Jpeg/JpegDecoderCore.cs | 27 +++++++++++++++++-- .../Formats/Jpg/JpegDecoderTests.Baseline.cs | 16 ++++++++++- .../Formats/Jpg/JpegDecoderTests.Images.cs | 4 --- .../Jpg/JpegDecoderTests.Progressive.cs | 12 +++++++++ .../Formats/Jpg/JpegDecoderTests.cs | 4 ++- .../Formats/Jpg/SpectralJpegTests.cs | 1 - tests/ImageSharp.Tests/TestImages.cs | 11 ++++++-- .../Calliphora-arithmetic-grayscale.jpg | 3 +++ .../Calliphora-arithmetic-interleaved.jpg | 3 +++ .../Calliphora-arithmetic-restart.jpg | 3 +++ .../Jpg/baseline/Calliphora_arithmetic.jpg | 3 +++ ...ora-arithmetic-progressive-interleaved.jpg | 3 +++ 13 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-grayscale.jpg create mode 100644 tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-interleaved.jpg create mode 100644 tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-restart.jpg create mode 100644 tests/Images/Input/Jpg/baseline/Calliphora_arithmetic.jpg create mode 100644 tests/Images/Input/Jpg/progressive/Calliphora-arithmetic-progressive-interleaved.jpg diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBitReader.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBitReader.cs index 76bb2b4452..84013319e1 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBitReader.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBitReader.cs @@ -117,8 +117,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder public int Receive(int nbits) { this.CheckBits(); - int bits = Extend(this.GetBits(nbits), nbits); - return bits; + return Extend(this.GetBits(nbits), nbits); } [MethodImpl(InliningOptions.ShortMethod)] diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index c87869ee57..979c3ca7bc 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -105,6 +105,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// private List arithmeticDecodingTables; + /// + /// The restart interval. + /// + private int? resetInterval; + /// /// Initializes a new instance of the class. /// @@ -292,6 +297,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { bool metadataOnly = spectralConverter == null; + this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); + this.Metadata = new ImageMetadata(); // Check for the Start Of Image marker. @@ -324,7 +331,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg case JpegConstants.Markers.SOF0: case JpegConstants.Markers.SOF1: case JpegConstants.Markers.SOF2: - this.scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, ComponentType.Huffman, metadataOnly); break; @@ -333,6 +339,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg case JpegConstants.Markers.SOF13: case JpegConstants.Markers.SOF14: this.scanDecoder = new ArithmeticScanDecoder(stream, spectralConverter, cancellationToken); + if (this.resetInterval.HasValue) + { + this.scanDecoder.ResetInterval = this.resetInterval.Value; + } + this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, ComponentType.Arithmetic, metadataOnly); break; @@ -871,6 +882,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } } + /// + /// Processes a DAC marker, decoding the arithmetic tables. + /// + /// The input stream. + /// The remaining bytes in the segment block. private void ProcessArithmeticTable(BufferedReadStream stream, int remaining) { this.arithmeticDecodingTables ??= new List(4); @@ -1297,7 +1313,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg JpegThrowHelper.ThrowBadMarker(nameof(JpegConstants.Markers.DRI), remaining); } - this.scanDecoder.ResetInterval = this.ReadUint16(stream); + // Save the reset interval, because it can come before or after the SOF marker. + // If the reset interval comes after the SOF marker, the scanDecoder has not been created. + this.resetInterval = this.ReadUint16(stream); + + if (this.scanDecoder != null) + { + this.scanDecoder.ResetInterval = this.resetInterval.Value; + } } /// diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs index 021e3d2726..9aaf2b968f 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Baseline.cs @@ -3,7 +3,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities; - +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using Xunit; // ReSharper disable InconsistentNaming @@ -49,6 +49,20 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg // .Dispose(); } + [Theory] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCoding01, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCoding02, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingGray, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingInterleaved, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingWithRestart, PixelTypes.Rgb24)] + public void DecodeJpeg_WithArithmeticCoding(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(JpegDecoder); + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Tolerant(0.002f), ReferenceDecoder); + } + [Theory] [WithFileCollection(nameof(UnrecoverableTestJpegs), PixelTypes.Rgba32)] public void UnrecoverableImage_Throws_InvalidImageContentException(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 70cbc3af72..8db3f062fd 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -72,10 +72,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestImages.Jpeg.Issues.Fuzz.NullReferenceException823, TestImages.Jpeg.Issues.MalformedUnsupportedComponentCount, - // Arithmetic coding - TestImages.Jpeg.Baseline.ArithmeticCoding, - TestImages.Jpeg.Baseline.ArithmeticCodingProgressive, - // Lossless jpeg TestImages.Jpeg.Baseline.Lossless }; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs index e8533b9bca..30e5da4ef5 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Progressive.cs @@ -4,6 +4,7 @@ using Microsoft.DotNet.RemoteExecutor; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using Xunit; // ReSharper disable InconsistentNaming @@ -29,6 +30,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg appendPixelTypeToFileName: false); } + [Theory] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingProgressive01, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingProgressive02, PixelTypes.Rgb24)] + public void DecodeProgressiveJpeg_WithArithmeticCoding(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(JpegDecoder); + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Tolerant(0.004f), ReferenceDecoder); + } + [Theory] [WithFile(TestImages.Jpeg.Progressive.Progress, PixelTypes.Rgb24)] public void DecodeProgressiveJpeg_WithLimitedAllocatorBufferCapacity(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index faf40ccf7d..497479096c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -13,7 +13,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; - +using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; using Xunit; using Xunit.Abstractions; @@ -24,6 +24,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg [Trait("Format", "Jpg")] public partial class JpegDecoderTests { + private static MagickReferenceDecoder ReferenceDecoder => new(); + public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.Bgr24 | PixelTypes.RgbaVector; private const float BaselineTolerance = 0.001F / 100; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 6480c47d05..c4a448ff8e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -85,7 +85,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg // internal scan decoder which we substitute to assert spectral correctness var debugConverter = new DebugSpectralConverter(); - var scanDecoder = new HuffmanScanDecoder(bufferedStream, debugConverter, cancellationToken: default); // This would parse entire image decoder.ParseStream(bufferedStream, debugConverter, cancellationToken: default); diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 07aff6fc12..060ed3f32e 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -214,14 +214,21 @@ 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/progressive/arithmetic_progressive.jpg"; public const string Lossless = "Jpg/baseline/lossless.jpg"; public const string Winter444_Interleaved = "Jpg/baseline/winter444_interleaved.jpg"; public const string Metadata = "Jpg/baseline/Metadata-test-file.jpg"; public const string ExtendedXmp = "Jpg/baseline/extended-xmp.jpg"; public const string GrayscaleSampling2x2 = "Jpg/baseline/grayscale_sampling22.jpg"; + // Jpeg's with arithmetic coding. + public const string ArithmeticCoding01 = "Jpg/baseline/Calliphora_arithmetic.jpg"; + public const string ArithmeticCoding02 = "Jpg/baseline/arithmetic_coding.jpg"; + public const string ArithmeticCodingProgressive01 = "Jpg/progressive/arithmetic_progressive.jpg"; + public const string ArithmeticCodingProgressive02 = "Jpg/progressive/Calliphora-arithmetic-progressive-interleaved.jpg"; + public const string ArithmeticCodingGray = "Jpg/baseline/Calliphora-arithmetic-grayscale.jpg"; + public const string ArithmeticCodingInterleaved = "Jpg/baseline/Calliphora-arithmetic-interleaved.jpg"; + public const string ArithmeticCodingWithRestart = "Jpg/baseline/Calliphora-arithmetic-restart.jpg"; + public static readonly string[] All = { Cmyk, Ycck, Exif, Floorplan, diff --git a/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-grayscale.jpg b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-grayscale.jpg new file mode 100644 index 0000000000..59256be594 --- /dev/null +++ b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-grayscale.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5800857969f25773606ecb63154a7fee60b831f13932dc845e50276bac11b16 +size 177311 diff --git a/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-interleaved.jpg b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-interleaved.jpg new file mode 100644 index 0000000000..9dc93473da --- /dev/null +++ b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-interleaved.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09bb5d75c3ca9d92d6e6489611f1d9b9815aaec70a16027f4ca17e371aa69e6e +size 234032 diff --git a/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-restart.jpg b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-restart.jpg new file mode 100644 index 0000000000..5ba56de1ab --- /dev/null +++ b/tests/Images/Input/Jpg/baseline/Calliphora-arithmetic-restart.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad547d0de50d1d623a49dc7794838c6d35372ea3fe4bda06365ff8b42daf65bf +size 234225 diff --git a/tests/Images/Input/Jpg/baseline/Calliphora_arithmetic.jpg b/tests/Images/Input/Jpg/baseline/Calliphora_arithmetic.jpg new file mode 100644 index 0000000000..9dc93473da --- /dev/null +++ b/tests/Images/Input/Jpg/baseline/Calliphora_arithmetic.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:09bb5d75c3ca9d92d6e6489611f1d9b9815aaec70a16027f4ca17e371aa69e6e +size 234032 diff --git a/tests/Images/Input/Jpg/progressive/Calliphora-arithmetic-progressive-interleaved.jpg b/tests/Images/Input/Jpg/progressive/Calliphora-arithmetic-progressive-interleaved.jpg new file mode 100644 index 0000000000..91879221c4 --- /dev/null +++ b/tests/Images/Input/Jpg/progressive/Calliphora-arithmetic-progressive-interleaved.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd688e22840892d7fd511782f3ff7043df1a3131fb17b000c6a3ca1d0e069950 +size 228527