Browse Source

Move PngHeader validation to struct

af/merge-core
Jason Nelson 7 years ago
parent
commit
666aaffaab
  1. 33
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 29
      src/ImageSharp/Formats/Png/PngHeader.cs

33
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;
}
/// <summary>
/// Validates the png header.
/// </summary>
/// <exception cref="NotSupportedException">
/// Thrown if the image does pass validation.
/// </exception>
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;
}

29
src/ImageSharp/Formats/Png/PngHeader.cs

@ -80,6 +80,35 @@ namespace SixLabors.ImageSharp.Formats.Png
/// </summary>
public PngInterlaceMode InterlaceMethod { get; }
/// <summary>
/// Validates the png header.
/// </summary>
/// <exception cref="NotSupportedException">
/// Thrown if the image does pass validation.
/// </exception>
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.");
}
}
/// <summary>
/// Writes the header to the given buffer.
/// </summary>

Loading…
Cancel
Save