// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Webp {
///
/// Detects Webp file headers.
///
public sealed class WebpImageFormatDetector : IImageFormatDetector
{
///
public int HeaderSize => 12;
///
public bool TryDetectFormat(ReadOnlySpan header, [NotNullWhen(true)] out IImageFormat? format)
{
format = this.IsSupportedFileFormat(header) ? WebpFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan header)
=> header.Length >= this.HeaderSize && IsRiffContainer(header) && IsWebpFile(header);
///
/// Checks, if the header starts with a valid RIFF FourCC.
///
/// The header bytes.
/// True, if its a valid RIFF FourCC.
private static bool IsRiffContainer(ReadOnlySpan header)
=> header[..4].SequenceEqual(WebpConstants.RiffFourCc);
///
/// Checks if 'WEBP' is present in the header.
///
/// The header bytes.
/// True, if its a webp file.
private static bool IsWebpFile(ReadOnlySpan header)
=> header.Slice(8, 4).SequenceEqual(WebpConstants.WebpHeader);
}
}