diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index a24b69160..112dc7262 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -198,7 +198,6 @@ namespace SixLabors.ImageSharp.Formats.Png
{
case PngChunkType.Header:
this.ReadHeaderChunk(pngMetaData, chunk.Data.Array);
- this.ValidateHeader();
break;
case PngChunkType.Physical:
this.ReadPhysicalChunk(metaData, chunk.Data.GetSpan());
@@ -287,7 +286,6 @@ namespace SixLabors.ImageSharp.Formats.Png
{
case PngChunkType.Header:
this.ReadHeaderChunk(pngMetaData, chunk.Data.Array);
- this.ValidateHeader();
break;
case PngChunkType.Physical:
this.ReadPhysicalChunk(metaData, chunk.Data.GetSpan());
@@ -886,37 +884,10 @@ namespace SixLabors.ImageSharp.Formats.Png
{
this.header = PngHeader.Parse(data);
+ this.header.Validate();
+
pngMetaData.BitDepth = (PngBitDepth)this.header.BitDepth;
pngMetaData.ColorType = this.header.ColorType;
- }
-
- ///
- /// Validates the png header.
- ///
- ///
- /// Thrown if the image does pass validation.
- ///
- private void ValidateHeader()
- {
- if (!PngConstants.ColorTypes.ContainsKey(this.header.ColorType))
- {
- throw new NotSupportedException("Color type is not supported or not valid.");
- }
-
- if (!PngConstants.ColorTypes[this.header.ColorType].Contains(this.header.BitDepth))
- {
- throw new NotSupportedException("Bit depth is not supported or not valid.");
- }
-
- if (this.header.FilterMethod != 0)
- {
- throw new NotSupportedException("The png specification only defines 0 as filter method.");
- }
-
- if (this.header.InterlaceMethod != PngInterlaceMode.None && this.header.InterlaceMethod != PngInterlaceMode.Adam7)
- {
- throw new NotSupportedException("The png specification only defines 'None' and 'Adam7' as interlaced methods.");
- }
this.pngColorType = this.header.ColorType;
}
diff --git a/src/ImageSharp/Formats/Png/PngHeader.cs b/src/ImageSharp/Formats/Png/PngHeader.cs
index ec22f1bb4..0523502b0 100644
--- a/src/ImageSharp/Formats/Png/PngHeader.cs
+++ b/src/ImageSharp/Formats/Png/PngHeader.cs
@@ -80,6 +80,35 @@ namespace SixLabors.ImageSharp.Formats.Png
///
public PngInterlaceMode InterlaceMethod { get; }
+ ///
+ /// Validates the png header.
+ ///
+ ///
+ /// Thrown if the image does pass validation.
+ ///
+ public void Validate()
+ {
+ if (!PngConstants.ColorTypes.ContainsKey(this.ColorType))
+ {
+ throw new NotSupportedException("Color type is not supported or not valid.");
+ }
+
+ if (PngConstants.ColorTypes[this.ColorType].AsSpan().IndexOf(this.BitDepth) == -1)
+ {
+ throw new NotSupportedException("Bit depth is not supported or not valid.");
+ }
+
+ if (this.FilterMethod != 0)
+ {
+ throw new NotSupportedException("The png specification only defines 0 as filter method.");
+ }
+
+ if (this.InterlaceMethod != PngInterlaceMode.None && this.InterlaceMethod != PngInterlaceMode.Adam7)
+ {
+ throw new NotSupportedException("The png specification only defines 'None' and 'Adam7' as interlaced methods.");
+ }
+ }
+
///
/// Writes the header to the given buffer.
///