Skip to content

Commit 01caebd

Browse files
committed
Add UnitTest
1 parent 7b6c32d commit 01caebd

File tree

7 files changed

+51
-52
lines changed

7 files changed

+51
-52
lines changed

src/ImageSharp/Formats/Png/PngChunkType.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Png;
99
internal enum PngChunkType : uint
1010
{
1111
/// <summary>
12-
/// </summary>
13-
/// <remarks>acTL (Single)</remarks>
14-
AnimationControl = 0x6163544cU,
15-
16-
/// <summary>
17-
/// </summary>
18-
/// <remarks>fcTL (Multiple)</remarks>
19-
FrameControl = 0x6663544cU,
20-
21-
/// <summary>
22-
/// </summary>
23-
/// <remarks>fdAT (Multiple)</remarks>
24-
FrameData = 0x66644154U,
25-
26-
/// <summary>
27-
/// The IDAT chunk contains the actual image data. The image can contains more
12+
/// This chunk contains the actual image data. The image can contains more
2813
/// than one chunk of this type. All chunks together are the whole image.
2914
/// </summary>
3015
/// <remarks>IDAT (Multiple)</remarks>
@@ -155,6 +140,27 @@ internal enum PngChunkType : uint
155140
/// <remarks>cHRM (Single)</remarks>
156141
Chroma = 0x6348524d,
157142

143+
/// <summary>
144+
/// This chunk is an ancillary chunk as defined in the PNG Specification.
145+
/// It must appear before the first IDAT chunk within a valid PNG stream.
146+
/// </summary>
147+
/// <remarks>acTL (Single, APNG)</remarks>
148+
AnimationControl = 0x6163544cU,
149+
150+
/// <summary>
151+
/// This chunk is an ancillary chunk as defined in the PNG Specification.
152+
/// It must appear before the IDAT or fdAT chunks of the frame to which it applies.
153+
/// </summary>
154+
/// <remarks>fcTL (Multiple, APNG)</remarks>
155+
FrameControl = 0x6663544cU,
156+
157+
/// <summary>
158+
/// This chunk has the same purpose as an IDAT chunk.
159+
/// It has the same structure as an IDAT chunk, except preceded by a sequence number.
160+
/// </summary>
161+
/// <remarks>fdAT (Multiple, APNG)</remarks>
162+
FrameData = 0x66644154U,
163+
158164
/// <summary>
159165
/// Malformed chunk named CgBI produced by apple, which is not conform to the specification.
160166
/// Related issue is here https://github.com/SixLabors/ImageSharp/issues/410

src/ImageSharp/Formats/Png/PngEncoderCore.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
168168
this.WriteXmpChunk(stream, metadata);
169169
this.WriteTextChunks(stream, pngMetadata);
170170

171-
if (this.encoder.IsSimplePng is not true && targetImage.Frames.Count > 1)
171+
if ((this.encoder.IsSimplePng is null && targetImage.Frames.Count > 1)
172+
|| this.encoder.IsSimplePng is false)
172173
{
173174
this.WriteAnimationControlChunk(stream, targetImage.Frames.Count, pngMetadata.NumberPlays);
174175

@@ -642,7 +643,7 @@ private void WritePaletteChunk<TPixel>(Stream stream, IndexedImageFrame<TPixel>?
642643
ref Rgba32 rgbaPaletteRef = ref MemoryMarshal.GetReference(rgbaPaletteSpan);
643644

644645
// Loop, assign, and extract alpha values from the palette.
645-
for (int i = 0; i < paletteLength; i++)
646+
for (int i = 0; i < paletteLength; ++i)
646647
{
647648
Rgba32 rgba = Unsafe.Add(ref rgbaPaletteRef, (uint)i);
648649
byte alpha = rgba.A;
@@ -674,7 +675,7 @@ private void WritePaletteChunk<TPixel>(Stream stream, IndexedImageFrame<TPixel>?
674675
/// <param name="meta">The image metadata.</param>
675676
private void WritePhysicalChunk(Stream stream, ImageMetadata meta)
676677
{
677-
if ((this.chunkFilter & PngChunkFilter.ExcludePhysicalChunk) == PngChunkFilter.ExcludePhysicalChunk)
678+
if (this.chunkFilter.HasFlag(PngChunkFilter.ExcludePhysicalChunk))
678679
{
679680
return;
680681
}

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,7 @@ public void Decode<TPixel>(TestImageProvider<TPixel> provider)
111111
public void Decode_APng<TPixel>(TestImageProvider<TPixel> provider)
112112
where TPixel : unmanaged, IPixel<TPixel>
113113
{
114-
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
115-
image.SaveAsPng("C:\\WorkSpace\\1.png");
116-
image.DebugSave(provider);
117-
image.CompareToOriginal(provider, ImageComparer.Exact);
118-
// TODO test
119-
}
120-
121-
[Theory]
122-
[WithFile("C:\\WorkSpace\\Fuck.png", PixelTypes.Rgba32)]
123-
public void Decode_APng2<TPixel>(TestImageProvider<TPixel> provider)
124-
where TPixel : unmanaged, IPixel<TPixel>
125-
{
126-
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
127-
image.SaveAsPng("C:\\WorkSpace\\1.png");
114+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance); // MagickReferenceDecoder cannot decode APNGs
128115
}
129116

130117
[Theory]

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,19 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
439439
});
440440
}
441441

442+
[Theory]
443+
[WithFile(TestImages.Png.APng, PixelTypes.Rgba32)]
444+
public void Encode_APng<TPixel>(TestImageProvider<TPixel> provider)
445+
where TPixel : unmanaged, IPixel<TPixel>
446+
{
447+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
448+
using MemoryStream memStream = new();
449+
image.Save(memStream, PngEncoder);
450+
memStream.Position = 0;
451+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
452+
ImageComparer.Exact.VerifySimilarity(output, image);
453+
}
454+
442455
[Theory]
443456
[MemberData(nameof(PngTrnsFiles))]
444457
public void Encode_PreserveTrns(string imagePath, PngBitDepth pngBitDepth, PngColorType pngColorType)

tests/ImageSharp.Tests/TestUtilities/ImagingTestCaseUtility.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public string[] SaveTestOutputFileMultiFrame<TPixel>(
208208
bool appendPixelTypeToFileName = true)
209209
where TPixel : unmanaged, IPixel<TPixel>
210210
{
211-
encoder = encoder ?? TestEnvironment.GetReferenceEncoder($"foo.{extension}");
211+
encoder ??= TestEnvironment.GetReferenceEncoder($"foo.{extension}");
212212

213213
string[] files = this.GetTestOutputFileNamesMultiFrame(
214214
image.Frames.Count,
@@ -218,14 +218,10 @@ public string[] SaveTestOutputFileMultiFrame<TPixel>(
218218

219219
for (int i = 0; i < image.Frames.Count; i++)
220220
{
221-
using (Image<TPixel> frameImage = image.Frames.CloneFrame(i))
222-
{
223-
string filePath = files[i];
224-
using (FileStream stream = File.OpenWrite(filePath))
225-
{
226-
frameImage.Save(stream, encoder);
227-
}
228-
}
221+
using Image<TPixel> frameImage = image.Frames.CloneFrame(i);
222+
string filePath = files[i];
223+
using FileStream stream = File.OpenWrite(filePath);
224+
frameImage.Save(stream, encoder);
229225
}
230226

231227
return files;

tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,8 @@ public static Image<TPixel> CompareToOriginal<TPixel>(
534534
referenceDecoder ??= TestEnvironment.GetReferenceDecoder(path);
535535

536536
using MemoryStream stream = new(testFile.Bytes);
537-
using (Image<TPixel> original = referenceDecoder.Decode<TPixel>(referenceDecoderOptions ?? DecoderOptions.Default, stream))
538-
{
539-
comparer.VerifySimilarity(original, image);
540-
}
537+
using Image<TPixel> original = referenceDecoder.Decode<TPixel>(referenceDecoderOptions ?? DecoderOptions.Default, stream);
538+
comparer.VerifySimilarity(original, image);
541539

542540
return image;
543541
}
@@ -560,10 +558,8 @@ public static Image<TPixel> CompareToOriginalMultiFrame<TPixel>(
560558
referenceDecoder ??= TestEnvironment.GetReferenceDecoder(path);
561559

562560
using MemoryStream stream = new(testFile.Bytes);
563-
using (Image<TPixel> original = referenceDecoder.Decode<TPixel>(DecoderOptions.Default, stream))
564-
{
565-
comparer.VerifySimilarity(original, image);
566-
}
561+
using Image<TPixel> original = referenceDecoder.Decode<TPixel>(DecoderOptions.Default, stream);
562+
comparer.VerifySimilarity(original, image);
567563

568564
return image;
569565
}

tests/Images/Input/Png/apng.png

Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)