Skip to content

Commit 9ae8be4

Browse files
committed
Add more tests
1 parent addec72 commit 9ae8be4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Buffers.Binary;
55
using SixLabors.ImageSharp.Formats.Png;
6+
using SixLabors.ImageSharp.Formats.Png.Chunks;
67
using SixLabors.ImageSharp.PixelFormats;
78

89
// ReSharper disable InconsistentNaming
@@ -59,6 +60,38 @@ public void EndChunk_IsLast()
5960
}
6061
}
6162

63+
[Theory]
64+
[WithFile(TestImages.Png.DefaultNotAnimated, PixelTypes.Rgba32)]
65+
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
66+
public void AcTL_CorrectlyWritten<TPixel>(TestImageProvider<TPixel> provider)
67+
where TPixel : unmanaged, IPixel<TPixel>
68+
{
69+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
70+
PngMetadata metadata = image.Metadata.GetPngMetadata();
71+
int correctFrameCount = image.Frames.Count - (metadata.DefaultImageAnimated ? 0 : 1);
72+
using MemoryStream memStream = new();
73+
image.Save(memStream, PngEncoder);
74+
memStream.Position = 0;
75+
Span<byte> bytesSpan = memStream.ToArray().AsSpan(8); // Skip header.
76+
bool foundAcTl = false;
77+
while (bytesSpan.Length > 0 && !foundAcTl)
78+
{
79+
int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan[..4]);
80+
PngChunkType type = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4));
81+
if (type == PngChunkType.AnimationControl)
82+
{
83+
AnimationControl control = AnimationControl.Parse(bytesSpan[8..]);
84+
foundAcTl = true;
85+
Assert.True(control.NumberFrames == correctFrameCount);
86+
Assert.True(control.NumberPlays == metadata.RepeatCount);
87+
}
88+
89+
bytesSpan = bytesSpan[(4 + 4 + length + 4)..];
90+
}
91+
92+
Assert.True(foundAcTl);
93+
}
94+
6295
[Theory]
6396
[InlineData(PngChunkType.Gamma)]
6497
[InlineData(PngChunkType.Chroma)]

tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ public void Decode_ReadsExifData<TPixel>(TestImageProvider<TPixel> provider)
146146
VerifyExifDataIsPresent(exif);
147147
}
148148

149+
[Theory]
150+
[WithFile(TestImages.Png.DefaultNotAnimated, PixelTypes.Rgba32)]
151+
public void Decode_IdentifiesDefaultFrameNotAnimated<TPixel>(TestImageProvider<TPixel> provider)
152+
where TPixel : unmanaged, IPixel<TPixel>
153+
{
154+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
155+
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
156+
Assert.False(meta.DefaultImageAnimated);
157+
}
158+
159+
[Theory]
160+
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
161+
public void Decode_IdentifiesDefaultFrameAnimated<TPixel>(TestImageProvider<TPixel> provider)
162+
where TPixel : unmanaged, IPixel<TPixel>
163+
{
164+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
165+
PngMetadata meta = image.Metadata.GetFormatMetadata(PngFormat.Instance);
166+
Assert.True(meta.DefaultImageAnimated);
167+
}
168+
149169
[Theory]
150170
[WithFile(TestImages.Png.PngWithMetadata, PixelTypes.Rgba32)]
151171
public void Decode_IgnoresExifData_WhenIgnoreMetadataIsTrue<TPixel>(TestImageProvider<TPixel> provider)

0 commit comments

Comments
 (0)