Browse Source

PngEncoderTests using reference images

af/merge-core
Anton Firszov 9 years ago
parent
commit
a470f61e07
  1. 7
      tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs
  2. 117
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs
  3. 2
      tests/Images/External

7
tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs

@ -7,15 +7,16 @@ using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using Xunit; using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests namespace SixLabors.ImageSharp.Tests
{ {
public class GifEncoderTests public class GifEncoderTests
{ {
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;
[Theory] [Theory]
[WithTestPatternImages(100, 100, PixelTypes)] [WithTestPatternImages(100, 100, TestPixelTypes)]
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider) public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
@ -78,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests
} }
[Fact] [Fact]
public void Encode_CommentIsToLong_CommentIsTrimmed() public void Encode_WhenCommentIsTooLong_CommentIsTrimmed()
{ {
using (Image<Rgba32> input = new Image<Rgba32>(1, 1)) using (Image<Rgba32> input = new Image<Rgba32>(1, 1))
{ {

117
tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

@ -10,36 +10,127 @@ using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using Xunit; using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests namespace SixLabors.ImageSharp.Tests
{ {
using SixLabors.ImageSharp.Quantizers;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
public class PngEncoderTests : FileTestBase public class PngEncoderTests : FileTestBase
{ {
private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; /// <summary>
/// All types except Palette
/// </summary>
public static readonly TheoryData<PngColorType> PngColorTypes = new TheoryData<PngColorType>()
{
PngColorType.RgbWithAlpha,
PngColorType.Rgb,
PngColorType.Grayscale,
PngColorType.GrayscaleWithAlpha,
};
/// <summary>
/// All types except Palette
/// </summary>
public static readonly TheoryData<int> CompressionLevels = new TheoryData<int>()
{
1, 2, 3, 4, 5, 6, 7, 8, 9
};
public static readonly TheoryData<int> PaletteSizes = new TheoryData<int>()
{
30, 55, 100, 201, 255
};
[Theory] [Theory]
[WithTestPatternImages(100, 100, PixelTypes, PngColorType.RgbWithAlpha)] [WithTestPatternImages(nameof(PngColorTypes), 48, 24, PixelTypes.Rgba32)]
[WithTestPatternImages(100, 100, PixelTypes, PngColorType.Rgb)] [WithTestPatternImages(nameof(PngColorTypes), 47, 8, PixelTypes.Rgba32)]
[WithTestPatternImages(100, 100, PixelTypes, PngColorType.Palette)] [WithTestPatternImages(nameof(PngColorTypes), 49, 7, PixelTypes.Rgba32)]
[WithTestPatternImages(100, 100, PixelTypes, PngColorType.Grayscale)] [WithSolidFilledImages(nameof(PngColorTypes), 1, 1, 255, 100, 50, 255, PixelTypes.Rgba32)]
[WithTestPatternImages(100, 100, PixelTypes, PngColorType.GrayscaleWithAlpha)] [WithTestPatternImages(nameof(PngColorTypes), 7, 5, PixelTypes.Rgba32)]
public void EncodeGeneratedPatterns<TPixel>(TestImageProvider<TPixel> provider, PngColorType pngColorType) public void WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> provider, PngColorType pngColorType)
where TPixel : struct, IPixel<TPixel>
{
TestPngEncoderCore(provider, pngColorType, appendPngColorType: true);
}
[Theory]
[WithTestPatternImages(nameof(PngColorTypes), 24, 24, PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.Rgb24)]
public void IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider, PngColorType pngColorType)
where TPixel : struct, IPixel<TPixel>
{
TestPngEncoderCore(provider, pngColorType, appendPixelType: true);
}
[Theory]
[WithTestPatternImages(nameof(CompressionLevels), 24, 24, PixelTypes.Rgba32)]
public void WorksWithAllCompressionLevels<TPixel>(TestImageProvider<TPixel> provider, int compressionLevel)
where TPixel : struct, IPixel<TPixel>
{
TestPngEncoderCore(provider, PngColorType.RgbWithAlpha, compressionLevel, appendCompressionLevel: true);
}
[Theory]
[WithTestPatternImages(nameof(PaletteSizes), 72, 72, PixelTypes.Rgba32)]
public void PaletteColorType_WuQuantizer<TPixel>(TestImageProvider<TPixel> provider, int paletteSize)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var encoder = new PngEncoder
{
PngColorType = PngColorType.Palette,
PaletteSize = paletteSize,
Quantizer = new WuQuantizer<TPixel>()
};
image.VerifyEncoder(provider, "png", $"PaletteSize-{paletteSize}", encoder, appendPixelTypeToFileName: false);
}
}
private static bool HasAlpha(PngColorType pngColorType) =>
pngColorType == PngColorType.GrayscaleWithAlpha || pngColorType == PngColorType.RgbWithAlpha;
private static void TestPngEncoderCore<TPixel>(
TestImageProvider<TPixel> provider,
PngColorType pngColorType,
int compressionLevel = 6,
bool appendPngColorType = false,
bool appendPixelType = false,
bool appendCompressionLevel = false)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
var options = new PngEncoder() if (!HasAlpha(pngColorType))
{ {
PngColorType = pngColorType image.Mutate(c => c.Opacity(1));
}; }
provider.Utility.TestName += "_" + pngColorType;
var encoder = new PngEncoder { PngColorType = pngColorType, CompressionLevel = compressionLevel};
provider.Utility.SaveTestOutputFile(image, "png", options); string pngColorTypeInfo = appendPixelType ? pngColorType.ToString() : "";
string compressionLevelInfo = appendCompressionLevel ? $"_C{compressionLevel}" : "";
string debugInfo = $"{pngColorTypeInfo}{compressionLevelInfo}";
string referenceInfo = $"{pngColorTypeInfo}";
// Does DebugSave & load reference CompareToReferenceInput():
string path = ((ITestImageProvider)provider).Utility.SaveTestOutputFile(image, "png", encoder, debugInfo, appendPixelType);
IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path);
string referenceOutputFile = ((ITestImageProvider)provider).Utility.GetReferenceOutputFileName("png", referenceInfo, appendPixelType);
using (var encodedImage = Image.Load<TPixel>(referenceOutputFile, referenceDecoder))
{
ImageComparer comparer = null ?? ImageComparer.Exact;
comparer.CompareImagesOrFrames(image, encodedImage);
}
} }
} }
[Theory] [Theory]
[WithBlankImages(1, 1, PixelTypes.All)] [WithBlankImages(1, 1, PixelTypes.Rgba32)]
public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider) public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {

2
tests/Images/External

@ -1 +1 @@
Subproject commit 65db1d045e74e7702ec33b44f88f97b4bf86f1ea Subproject commit 06809c1f2462332731f2a88bd866d5222f533aa5
Loading…
Cancel
Save