Browse Source

Optimize png format detection

af/merge-core
Jason Nelson 8 years ago
parent
commit
733f76763a
  1. 5
      src/ImageSharp/Formats/Png/PngConstants.cs
  2. 12
      src/ImageSharp/Formats/Png/PngImageFormatDetector.cs

5
src/ImageSharp/Formats/Png/PngConstants.cs

@ -36,5 +36,10 @@ namespace SixLabors.ImageSharp.Formats.Png
0x1A, // EOF
0x0A // LF
};
/// <summary>
/// The header bytes as a big endian coded ulong.
/// </summary>
public const ulong HeaderValue = 0x89504E470D0A1A0AUL;
}
}

12
src/ImageSharp/Formats/Png/PngImageFormatDetector.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers.Binary;
namespace SixLabors.ImageSharp.Formats.Png
{
@ -26,16 +27,7 @@ namespace SixLabors.ImageSharp.Formats.Png
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
{
// TODO: This should be in constants
return header.Length >= this.HeaderSize &&
header[0] == 0x89 &&
header[1] == 0x50 && // P
header[2] == 0x4E && // N
header[3] == 0x47 && // G
header[4] == 0x0D && // CR
header[5] == 0x0A && // LF
header[6] == 0x1A && // EOF
header[7] == 0x0A; // LF
return header.Length >= this.HeaderSize && BinaryPrimitives.ReadUInt64BigEndian(header) == PngConstants.HeaderValue;
}
}
}
Loading…
Cancel
Save