From 1e4b61db5247ce10df7cf016e9e49129b4d70b2e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 14 Apr 2018 23:28:34 +1000 Subject: [PATCH] Ensure pixel is assigned and add encoded comparison and --- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 2 +- .../Formats/Gif/GifEncoderTests.cs | 20 +++++++++- .../TestUtilities/TestImageExtensions.cs | 37 +++++++++++++++++-- tests/Images/External | 2 +- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 4a6cb0596..cb865e95d 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -342,7 +342,7 @@ namespace SixLabors.ImageSharp.Formats.Gif { ref TPixel entry = ref Unsafe.Add(ref paletteRef, i); entry.ToRgb24(ref rgb); - Unsafe.Add(ref rgb24Ref, i); + Unsafe.Add(ref rgb24Ref, i) = rgb; } writer.Write(colorTable.Array, 0, colorTableLength); diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index 1d0087de3..1e0cd948b 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -2,11 +2,11 @@ // Licensed under the Apache License, Version 2.0. using System.IO; -using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Quantization; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using Xunit; // ReSharper disable InconsistentNaming @@ -15,6 +15,7 @@ namespace SixLabors.ImageSharp.Tests public class GifEncoderTests { private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; + private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.001F); [Theory] [WithTestPatternImages(100, 100, TestPixelTypes)] @@ -23,7 +24,22 @@ namespace SixLabors.ImageSharp.Tests { using (Image image = provider.GetImage()) { - provider.Utility.SaveTestOutputFile(image, "gif", new GifEncoder()); + var encoder = new GifEncoder() + { + // Use the palette quantizer without dithering to ensure results + // are consistant + Quantizer = new PaletteQuantizer(false) + }; + + // Always save as we need to compare the encoded output. + provider.Utility.SaveTestOutputFile(image, "gif", encoder); + } + + // Compare encoded result + string path = provider.Utility.GetTestOutputFileName("gif", null, true); + using (var encoded = Image.Load(path)) + { + encoded.CompareToReferenceOutput(ValidatorComparer, provider, null, "gif"); } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index dbae4f85d..7616f89ea 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -87,6 +87,37 @@ namespace SixLabors.ImageSharp.Tests return image; } + /// + /// Saves the image only when not running in the CI server. + /// + /// The pixel format + /// The image + /// The image provider + /// The image encoder + /// Details to be concatenated to the test output file, describing the parameters of the test. + /// A boolean indicating whether to append the pixel type to the output file name. + public static Image DebugSave( + this Image image, + ITestImageProvider provider, + IImageEncoder encoder, + object testOutputDetails = null, + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + if (TestEnvironment.RunsOnCI) + { + return image; + } + + // We are running locally then we want to save it out + provider.Utility.SaveTestOutputFile( + image, + encoder: encoder, + testOutputDetails: testOutputDetails, + appendPixelTypeToFileName: appendPixelTypeToFileName); + return image; + } + public static Image DebugSaveMultiFrame( this Image image, ITestImageProvider provider, @@ -168,7 +199,7 @@ namespace SixLabors.ImageSharp.Tests provider, testOutputDetails, extension, - appendPixelTypeToFileName)) + appendPixelTypeToFileName)) { comparer.VerifySimilarity(referenceImage, image); } @@ -272,7 +303,7 @@ namespace SixLabors.ImageSharp.Tests } Image firstTemp = temporaryFrameImages[0]; - + var result = new Image(firstTemp.Width, firstTemp.Height); foreach (Image fi in temporaryFrameImages) @@ -345,7 +376,7 @@ namespace SixLabors.ImageSharp.Tests { return CompareToOriginal(image, provider, ImageComparer.Tolerant()); } - + public static Image CompareToOriginal( this Image image, ITestImageProvider provider, diff --git a/tests/Images/External b/tests/Images/External index 5a66c9c6d..01af5f369 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 5a66c9c6da02bf27345f90adc05d415c0d0450ea +Subproject commit 01af5f36912ec7080cae3187a48905d1e54f6ea7