diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index eedc96fb3..df6cd69e7 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -11,13 +11,11 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { using System; - using System.Collections.Generic; using System.IO; using System.Linq; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Jpeg; - using SixLabors.ImageSharp.Formats.Jpeg.Common; using SixLabors.ImageSharp.Formats.Jpeg.GolangPort; using SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort; using SixLabors.ImageSharp.PixelFormats; @@ -66,26 +64,6 @@ namespace SixLabors.ImageSharp.Tests private static IImageDecoder PdfJsJpegDecoder => new JpegDecoder(); - private static void VerifyJpegComponent(IJpegComponent component, int expectedBlocksX, int expectedBlocksY) - { - Assert.Equal(component.WidthInBlocks, expectedBlocksX); - Assert.Equal(component.HeightInBlocks, expectedBlocksY); - } - - private static void Verify3ComponentJpeg( - IEnumerable components, - int xBc0, int yBc0, - int xBc1, int yBc1, - int xBc2, int yBc2) - { - IJpegComponent[] c = components.ToArray(); - Assert.Equal(3, components.Count()); - - VerifyJpegComponent(c[0], xBc0, yBc0); - VerifyJpegComponent(c[1], xBc1, yBc1); - VerifyJpegComponent(c[2], xBc2, yBc2); - } - [Fact] public void ParseStream_BasicPropertiesAreCorrect1_Old() { @@ -94,8 +72,8 @@ namespace SixLabors.ImageSharp.Tests { var decoder = new OldJpegDecoderCore(Configuration.Default, new JpegDecoder()); decoder.ParseStream(ms); - - Verify3ComponentJpeg(decoder.Components, 43, 61, 22, 31, 22, 31); + + VerifyJpeg.Components3(decoder.Components, 43, 61, 22, 31, 22, 31); } } @@ -108,7 +86,7 @@ namespace SixLabors.ImageSharp.Tests var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); decoder.ParseStream(ms); - Verify3ComponentJpeg(decoder.Frame.Components, 43, 61, 22, 31, 22, 31); + VerifyJpeg.Components3(decoder.Frame.Components, 43, 61, 22, 31, 22, 31); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/LibJpegTools.cs b/tests/ImageSharp.Tests/Formats/Jpg/LibJpegTools.cs index 628e5966c..a9385b0e2 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/LibJpegTools.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/LibJpegTools.cs @@ -137,11 +137,11 @@ namespace SixLabors.ImageSharp.Tests return null; } - Image result = new Image(c0.BlockCountX * 8, c0.BlockCountY * 8); + Image result = new Image(c0.WidthInBlocks * 8, c0.HeightInBlocks * 8); - for (int by = 0; by < c0.BlockCountY; by++) + for (int by = 0; by < c0.HeightInBlocks; by++) { - for (int bx = 0; bx < c0.BlockCountX; bx++) + for (int bx = 0; bx < c0.WidthInBlocks; bx++) { this.WriteToImage(bx, by, result); } @@ -227,23 +227,23 @@ namespace SixLabors.ImageSharp.Tests } } - public class ComponentData : IEquatable + public class ComponentData : IEquatable, IJpegComponent { - public ComponentData(int blockCountY, int blockCountX, int index) + public ComponentData(int heightInBlocks, int widthInBlocks, int index) { - this.BlockCountY = blockCountY; - this.BlockCountX = blockCountX; + this.HeightInBlocks = heightInBlocks; + this.WidthInBlocks = widthInBlocks; this.Index = index; - this.Blocks = new Buffer2D(this.BlockCountX, this.BlockCountY); + this.Blocks = new Buffer2D(this.WidthInBlocks, this.HeightInBlocks); } - public Size Size => new Size(this.BlockCountX, this.BlockCountY); + public Size Size => new Size(this.WidthInBlocks, this.HeightInBlocks); public int Index { get; } - public int BlockCountY { get; } + public int HeightInBlocks { get; } - public int BlockCountX { get; } + public int WidthInBlocks { get; } public Buffer2D Blocks { get; private set; } @@ -290,9 +290,9 @@ namespace SixLabors.ImageSharp.Tests index ); - for (int y = 0; y < result.BlockCountY; y++) + for (int y = 0; y < result.HeightInBlocks; y++) { - for (int x = 0; x < result.BlockCountX; x++) + for (int x = 0; x < result.WidthInBlocks; x++) { short[] data = c.GetBlockBuffer(y, x).ToArray(); result.MakeBlock(data, y, x); @@ -310,9 +310,9 @@ namespace SixLabors.ImageSharp.Tests c.Index ); - for (int y = 0; y < result.BlockCountY; y++) + for (int y = 0; y < result.HeightInBlocks; y++) { - for (int x = 0; x < result.BlockCountX; x++) + for (int x = 0; x < result.WidthInBlocks; x++) { short[] data = c.GetBlockReference(x, y).ToArray(); result.MakeBlock(data, y, x); @@ -324,11 +324,11 @@ namespace SixLabors.ImageSharp.Tests public Image CreateGrayScaleImage() { - Image result = new Image(this.BlockCountX * 8, this.BlockCountY * 8); + Image result = new Image(this.WidthInBlocks * 8, this.HeightInBlocks * 8); - for (int by = 0; by < this.BlockCountY; by++) + for (int by = 0; by < this.HeightInBlocks; by++) { - for (int bx = 0; bx < this.BlockCountX; bx++) + for (int bx = 0; bx < this.WidthInBlocks; bx++) { this.WriteToImage(bx, by, result); } @@ -370,15 +370,15 @@ namespace SixLabors.ImageSharp.Tests { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; - bool ok = this.Index == other.Index && this.BlockCountY == other.BlockCountY - && this.BlockCountX == other.BlockCountX; + bool ok = this.Index == other.Index && this.HeightInBlocks == other.HeightInBlocks + && this.WidthInBlocks == other.WidthInBlocks; //&& this.MinVal == other.MinVal //&& this.MaxVal == other.MaxVal; if (!ok) return false; - for (int y = 0; y < this.BlockCountY; y++) + for (int y = 0; y < this.HeightInBlocks; y++) { - for (int x = 0; x < this.BlockCountX; x++) + for (int x = 0; x < this.WidthInBlocks; x++) { Block8x8 a = this.Blocks[x, y]; Block8x8 b = other.Blocks[x, y]; @@ -401,8 +401,8 @@ namespace SixLabors.ImageSharp.Tests unchecked { var hashCode = this.Index; - hashCode = (hashCode * 397) ^ this.BlockCountY; - hashCode = (hashCode * 397) ^ this.BlockCountX; + hashCode = (hashCode * 397) ^ this.HeightInBlocks; + hashCode = (hashCode * 397) ^ this.WidthInBlocks; hashCode = (hashCode * 397) ^ this.MinVal.GetHashCode(); hashCode = (hashCode * 397) ^ this.MaxVal.GetHashCode(); return hashCode; @@ -479,33 +479,35 @@ namespace SixLabors.ImageSharp.Tests try { RunDumpJpegCoeffsTool(testFile.FullPath, coeffFileFullPath); - byte[] spectralBytes = File.ReadAllBytes(coeffFileFullPath); - File.Delete(coeffFileFullPath); - - using (var ms = new MemoryStream(testFile.Bytes)) + + using (var dumpStream = new FileStream(coeffFileFullPath, FileMode.Open)) + using (var rdr = new BinaryReader(dumpStream)) { - OldJpegDecoderCore decoder = new OldJpegDecoderCore(Configuration.Default, new JpegDecoder()); - decoder.ParseStream(ms); + int componentCount = rdr.ReadInt16(); + ComponentData[] result = new ComponentData[componentCount]; - Span dump = new Span(spectralBytes).NonPortableCast(); - int counter = 0; + for (int i = 0; i < componentCount; i++) + { + int widthInBlocks = rdr.ReadInt16(); + int heightInBlocks = rdr.ReadInt16(); + ComponentData resultComponent = new ComponentData(heightInBlocks, widthInBlocks, i); + result[i] = resultComponent; + } - OldComponent[] components = decoder.Components; - ComponentData[] result = new ComponentData[components.Length]; + byte[] buffer = new byte[64*sizeof(short)]; - for (int i = 0; i < components.Length; i++) + for (int i = 0; i < result.Length; i++) { - OldComponent c = components[i]; - ComponentData resultComponent = new ComponentData(c.HeightInBlocks, c.WidthInBlocks, i); - result[i] = resultComponent; + ComponentData c = result[i]; for (int y = 0; y < c.HeightInBlocks; y++) { for (int x = 0; x < c.WidthInBlocks; x++) { - short[] block = dump.Slice(counter, 64).ToArray(); - resultComponent.MakeBlock(block, y, x); - counter += 64; + rdr.Read(buffer, 0, buffer.Length); + + short[] block = buffer.AsSpan().NonPortableCast().ToArray(); + c.MakeBlock(block, y, x); } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs index 49495d4e1..1212a6a64 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs @@ -59,14 +59,24 @@ namespace SixLabors.ImageSharp.Tests } [Theory] - [InlineData(TestImages.Jpeg.Baseline.Calliphora)] - [InlineData(TestImages.Jpeg.Progressive.Progress)] - public void ExtractSpectralData(string testImage) + [WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgba32)] + [WithFile(TestImages.Jpeg.Progressive.Progress, PixelTypes.Rgba32)] + public void ExtractSpectralData(TestImageProvider provider) + where TPixel : struct, IPixel { + string testImage = provider.SourceFileOrDescription; LibJpegTools.SpectralData data = LibJpegTools.ExtractSpectralData(testImage); Assert.True(data.ComponentCount == 3); Assert.True(data.Components.Length == 3); + + this.SaveSpectralImage(provider, data); + + // I knew this one well: + if (testImage == TestImages.Jpeg.Progressive.Progress) + { + VerifyJpeg.Components3(data.Components, 43, 61, 22, 31, 22, 31); + } } [Theory] diff --git a/tests/ImageSharp.Tests/Formats/Jpg/VerifyJpeg.cs b/tests/ImageSharp.Tests/Formats/Jpg/VerifyJpeg.cs new file mode 100644 index 000000000..c571cd472 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Jpg/VerifyJpeg.cs @@ -0,0 +1,32 @@ +namespace SixLabors.ImageSharp.Tests +{ + using System.Collections.Generic; + using System.Linq; + + using SixLabors.ImageSharp.Formats.Jpeg.Common; + + using Xunit; + + internal static class VerifyJpeg + { + internal static void ComponentSize(IJpegComponent component, int expectedBlocksX, int expectedBlocksY) + { + Assert.Equal(component.WidthInBlocks, expectedBlocksX); + Assert.Equal(component.HeightInBlocks, expectedBlocksY); + } + + internal static void Components3( + IEnumerable components, + int xBc0, int yBc0, + int xBc1, int yBc1, + int xBc2, int yBc2) + { + IJpegComponent[] c = components.ToArray(); + Assert.Equal(3, components.Count()); + + ComponentSize(c[0], xBc0, yBc0); + ComponentSize(c[1], xBc1, yBc1); + ComponentSize(c[2], xBc2, yBc2); + } + } +} \ No newline at end of file diff --git a/tests/Images/External b/tests/Images/External index fd428515b..860116ca7 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit fd428515bb3b125621c3b9518dfd07c6d919d3bf +Subproject commit 860116ca736c8eba875c8393b97793e80a71d634