diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index 1bfba68ed..d4b29f49d 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -943,17 +943,9 @@ namespace SixLabors.ImageSharp.Formats.Png
/// The containing data.
private void ReadHeaderChunk(PngMetaData pngMetaData, ReadOnlySpan data)
{
- byte bitDepth = data[8];
- this.header = new PngHeader(
- width: BinaryPrimitives.ReadInt32BigEndian(data.Slice(0, 4)),
- height: BinaryPrimitives.ReadInt32BigEndian(data.Slice(4, 4)),
- bitDepth: bitDepth,
- colorType: (PngColorType)data[9],
- compressionMethod: data[10],
- filterMethod: data[11],
- interlaceMethod: (PngInterlaceMode)data[12]);
-
- pngMetaData.BitDepth = (PngBitDepth)bitDepth;
+ this.header = PngHeader.Parse(data);
+
+ pngMetaData.BitDepth = (PngBitDepth)this.header.BitDepth;
pngMetaData.ColorType = this.header.ColorType;
}
diff --git a/src/ImageSharp/Formats/Png/PngHeader.cs b/src/ImageSharp/Formats/Png/PngHeader.cs
index df85642be..389ba80ae 100644
--- a/src/ImageSharp/Formats/Png/PngHeader.cs
+++ b/src/ImageSharp/Formats/Png/PngHeader.cs
@@ -1,6 +1,9 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
+using System;
+using System.Buffers.Binary;
+
namespace SixLabors.ImageSharp.Formats.Png
{
///
@@ -74,5 +77,22 @@ namespace SixLabors.ImageSharp.Formats.Png
/// Two values are currently defined: 0 (no interlace) or 1 (Adam7 interlace).
///
public PngInterlaceMode InterlaceMethod { get; }
+
+ ///
+ /// Parses the PngHeader from the given data buffer.
+ ///
+ /// The data to parse.
+ /// The parsed PngHeader.
+ public static PngHeader Parse(ReadOnlySpan data)
+ {
+ return new PngHeader(
+ width: BinaryPrimitives.ReadInt32BigEndian(data.Slice(0, 4)),
+ height: BinaryPrimitives.ReadInt32BigEndian(data.Slice(4, 4)),
+ bitDepth: data[8],
+ colorType: (PngColorType)data[9],
+ compressionMethod: data[10],
+ filterMethod: data[11],
+ interlaceMethod: (PngInterlaceMode)data[12]);
+ }
}
}