Browse Source

Move PngHeader parsing logic to struct

pull/717/head
Jason Nelson 7 years ago
parent
commit
389684c4aa
  1. 14
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  2. 20
      src/ImageSharp/Formats/Png/PngHeader.cs

14
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -943,17 +943,9 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <param name="data">The <see cref="T:ReadOnlySpan{byte}"/> containing data.</param>
private void ReadHeaderChunk(PngMetaData pngMetaData, ReadOnlySpan<byte> 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;
}

20
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
{
/// <summary>
@ -74,5 +77,22 @@ namespace SixLabors.ImageSharp.Formats.Png
/// Two values are currently defined: 0 (no interlace) or 1 (Adam7 interlace).
/// </summary>
public PngInterlaceMode InterlaceMethod { get; }
/// <summary>
/// Parses the PngHeader from the given data buffer.
/// </summary>
/// <param name="data">The data to parse.</param>
/// <returns>The parsed PngHeader.</returns>
public static PngHeader Parse(ReadOnlySpan<byte> 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]);
}
}
}

Loading…
Cancel
Save