Browse Source

Add UnitTest

pull/2511/head
Poker 3 years ago
parent
commit
01caebd34a
No known key found for this signature in database GPG Key ID: 720AFAD63099D9CB
  1. 38
      src/ImageSharp/Formats/Png/PngChunkType.cs
  2. 7
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  3. 15
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  4. 13
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs
  5. 14
      tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs
  6. 12
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  7. 4
      tests/Images/Input/Png/apng.png

38
src/ImageSharp/Formats/Png/PngChunkType.cs

@ -9,22 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Png;
internal enum PngChunkType : uint
{
/// <summary>
/// </summary>
/// <remarks>acTL (Single)</remarks>
AnimationControl = 0x6163544cU,
/// <summary>
/// </summary>
/// <remarks>fcTL (Multiple)</remarks>
FrameControl = 0x6663544cU,
/// <summary>
/// </summary>
/// <remarks>fdAT (Multiple)</remarks>
FrameData = 0x66644154U,
/// <summary>
/// The IDAT chunk contains the actual image data. The image can contains more
/// This chunk contains the actual image data. The image can contains more
/// than one chunk of this type. All chunks together are the whole image.
/// </summary>
/// <remarks>IDAT (Multiple)</remarks>
@ -155,6 +140,27 @@ internal enum PngChunkType : uint
/// <remarks>cHRM (Single)</remarks>
Chroma = 0x6348524d,
/// <summary>
/// This chunk is an ancillary chunk as defined in the PNG Specification.
/// It must appear before the first IDAT chunk within a valid PNG stream.
/// </summary>
/// <remarks>acTL (Single, APNG)</remarks>
AnimationControl = 0x6163544cU,
/// <summary>
/// This chunk is an ancillary chunk as defined in the PNG Specification.
/// It must appear before the IDAT or fdAT chunks of the frame to which it applies.
/// </summary>
/// <remarks>fcTL (Multiple, APNG)</remarks>
FrameControl = 0x6663544cU,
/// <summary>
/// This chunk has the same purpose as an IDAT chunk.
/// It has the same structure as an IDAT chunk, except preceded by a sequence number.
/// </summary>
/// <remarks>fdAT (Multiple, APNG)</remarks>
FrameData = 0x66644154U,
/// <summary>
/// Malformed chunk named CgBI produced by apple, which is not conform to the specification.
/// Related issue is here https://github.com/SixLabors/ImageSharp/issues/410

7
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -168,7 +168,8 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
this.WriteXmpChunk(stream, metadata);
this.WriteTextChunks(stream, pngMetadata);
if (this.encoder.IsSimplePng is not true && targetImage.Frames.Count > 1)
if ((this.encoder.IsSimplePng is null && targetImage.Frames.Count > 1)
|| this.encoder.IsSimplePng is false)
{
this.WriteAnimationControlChunk(stream, targetImage.Frames.Count, pngMetadata.NumberPlays);
@ -642,7 +643,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
ref Rgba32 rgbaPaletteRef = ref MemoryMarshal.GetReference(rgbaPaletteSpan);
// Loop, assign, and extract alpha values from the palette.
for (int i = 0; i < paletteLength; i++)
for (int i = 0; i < paletteLength; ++i)
{
Rgba32 rgba = Unsafe.Add(ref rgbaPaletteRef, (uint)i);
byte alpha = rgba.A;
@ -674,7 +675,7 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
/// <param name="meta">The image metadata.</param>
private void WritePhysicalChunk(Stream stream, ImageMetadata meta)
{
if ((this.chunkFilter & PngChunkFilter.ExcludePhysicalChunk) == PngChunkFilter.ExcludePhysicalChunk)
if (this.chunkFilter.HasFlag(PngChunkFilter.ExcludePhysicalChunk))
{
return;
}

15
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -111,20 +111,7 @@ public partial class PngDecoderTests
public void Decode_APng<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
image.SaveAsPng("C:\\WorkSpace\\1.png");
image.DebugSave(provider);
image.CompareToOriginal(provider, ImageComparer.Exact);
// TODO test
}
[Theory]
[WithFile("C:\\WorkSpace\\Fuck.png", PixelTypes.Rgba32)]
public void Decode_APng2<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
image.SaveAsPng("C:\\WorkSpace\\1.png");
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance); // MagickReferenceDecoder cannot decode APNGs
}
[Theory]

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

@ -439,6 +439,19 @@ public partial class PngEncoderTests
});
}
[Theory]
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
public void Encode_APng<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
using MemoryStream memStream = new();
image.Save(memStream, PngEncoder);
memStream.Position = 0;
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
ImageComparer.Exact.VerifySimilarity(output, image);
}
[Theory]
[MemberData(nameof(PngTrnsFiles))]
public void Encode_PreserveTrns(string imagePath, PngBitDepth pngBitDepth, PngColorType pngColorType)

14
tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

@ -208,7 +208,7 @@ public class ImagingTestCaseUtility
bool appendPixelTypeToFileName = true)
where TPixel : unmanaged, IPixel<TPixel>
{
encoder = encoder ?? TestEnvironment.GetReferenceEncoder($"foo.{extension}");
encoder ??= TestEnvironment.GetReferenceEncoder($"foo.{extension}");
string[] files = this.GetTestOutputFileNamesMultiFrame(
image.Frames.Count,
@ -218,14 +218,10 @@ public class ImagingTestCaseUtility
for (int i = 0; i < image.Frames.Count; i++)
{
using (Image<TPixel> frameImage = image.Frames.CloneFrame(i))
{
string filePath = files[i];
using (FileStream stream = File.OpenWrite(filePath))
{
frameImage.Save(stream, encoder);
}
}
using Image<TPixel> frameImage = image.Frames.CloneFrame(i);
string filePath = files[i];
using FileStream stream = File.OpenWrite(filePath);
frameImage.Save(stream, encoder);
}
return files;

12
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -534,10 +534,8 @@ public static class TestImageExtensions
referenceDecoder ??= TestEnvironment.GetReferenceDecoder(path);
using MemoryStream stream = new(testFile.Bytes);
using (Image<TPixel> original = referenceDecoder.Decode<TPixel>(referenceDecoderOptions ?? DecoderOptions.Default, stream))
{
comparer.VerifySimilarity(original, image);
}
using Image<TPixel> original = referenceDecoder.Decode<TPixel>(referenceDecoderOptions ?? DecoderOptions.Default, stream);
comparer.VerifySimilarity(original, image);
return image;
}
@ -560,10 +558,8 @@ public static class TestImageExtensions
referenceDecoder ??= TestEnvironment.GetReferenceDecoder(path);
using MemoryStream stream = new(testFile.Bytes);
using (Image<TPixel> original = referenceDecoder.Decode<TPixel>(DecoderOptions.Default, stream))
{
comparer.VerifySimilarity(original, image);
}
using Image<TPixel> original = referenceDecoder.Decode<TPixel>(DecoderOptions.Default, stream);
comparer.VerifySimilarity(original, image);
return image;
}

4
tests/Images/Input/Png/apng.png

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f6b0e5a904e269a9108b32c0f5cc98cda4240a60db421f560f45d2e36ead32a9
size 212
oid sha256:7c15e4670da1826d1cc25555bd6cbe287ecc70327cd029a7613334a39a283021
size 2508

Loading…
Cancel
Save