forked from Spacefish/NeoSolve.ImageSharp.AVIF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVIFImageFormatDetector.cs
More file actions
34 lines (28 loc) · 970 Bytes
/
AVIFImageFormatDetector.cs
File metadata and controls
34 lines (28 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using SixLabors.ImageSharp.Formats;
using System;
using System.Diagnostics.CodeAnalysis;
namespace NeoSolve.ImageSharp.AVIF;
public class AVIFImageFormatDetector : IImageFormatDetector
{
public int HeaderSize => 12;
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat format)
{
bool isAVIF = header.Length <= HeaderSize && IsAvif(header);
format = isAVIF ? AVIFFormat.Instance : null;
return isAVIF;
}
private static bool IsAvif(ReadOnlySpan<byte> header)
{
// Check for the 'ftyp' box type at byte offset 4.
if (header[4] != 'f' || header[5] != 't' || header[6] != 'y' || header[7] != 'p')
{
return false;
}
// Check for the 'avif' brand at byte offset 8.
if (header[8] == 'a' && header[9] == 'v' && header[10] == 'i' && header[11] == 'f')
{
return true;
}
return false;
}
}