diff --git a/src/ImageSharp/Formats/WebP/Readme.md b/src/ImageSharp/Formats/WebP/Readme.md new file mode 100644 index 000000000..ba4ece71c --- /dev/null +++ b/src/ImageSharp/Formats/WebP/Readme.md @@ -0,0 +1,4 @@ += WebP Format +Reference implementation, specification and stuff like that: +https://developers.google.com/speed/webp + diff --git a/src/ImageSharp/Formats/WebP/WebPConstants.cs b/src/ImageSharp/Formats/WebP/WebPConstants.cs new file mode 100644 index 000000000..794fa16cc --- /dev/null +++ b/src/ImageSharp/Formats/WebP/WebPConstants.cs @@ -0,0 +1,20 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Collections.Generic; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + internal static class WebPConstants + { + /// + /// The list of file extensions that equate to WebP. + /// + public static readonly IEnumerable FileExtensions = new[] { "webp" }; + + /// + /// The list of mimetypes that equate to a jpeg. + /// + public static readonly IEnumerable MimeTypes = new[] { "image/webp", }; + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPDecoder.cs b/src/ImageSharp/Formats/WebP/WebPDecoder.cs new file mode 100644 index 000000000..100715328 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/WebPDecoder.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + public sealed class WebPDecoder : IImageDecoder + { + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPFormat.cs b/src/ImageSharp/Formats/WebP/WebPFormat.cs new file mode 100644 index 000000000..48a595258 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/WebPFormat.cs @@ -0,0 +1,33 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Collections.Generic; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + /// + /// Registers the image encoders, decoders and mime type detectors for the WebP format + /// + public sealed class WebPFormat : IImageFormat + { + /// + /// Gets the current instance. + /// + public static WebPFormat Instance { get; } = new WebPFormat(); + + /// + public string Name => "WebP"; + + /// + public string DefaultMimeType => "image/webp"; + + /// + public IEnumerable MimeTypes => WebPConstants.MimeTypes; + + /// + public IEnumerable FileExtensions => WebPConstants.FileExtensions; + + /// + public WebPMetadata CreateDefaultFormatMetadata() => new WebPMetadata(); + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPImageFormatDetector.cs b/src/ImageSharp/Formats/WebP/WebPImageFormatDetector.cs new file mode 100644 index 000000000..208229c4d --- /dev/null +++ b/src/ImageSharp/Formats/WebP/WebPImageFormatDetector.cs @@ -0,0 +1,42 @@ +using System; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + /// + /// Detects WebP file headers + /// + public sealed class WebPImageFormatDetector : IImageFormatDetector + { + /// + public int HeaderSize => 12; + + /// + public IImageFormat DetectFormat(ReadOnlySpan header) + { + return this.IsSupportedFileFormat(header) ? WebPFormat.Instance : null; + } + + private bool IsSupportedFileFormat(ReadOnlySpan header) + { + return header.Length >= this.HeaderSize && + this.IsRiffContainer(header) && + this.IsWebPFile(header); + } + + private bool IsRiffContainer(ReadOnlySpan header) + { + return header[0] == 0x52 && // R + header[1] == 0x49 && // I + header[2] == 0x46 && // F + header[3] == 0x46; // F + } + + private bool IsWebPFile(ReadOnlySpan header) + { + return header[8] == 0x57 && // W + header[9] == 0x45 && // E + header[10] == 0x42 && // B + header[11] == 0x50; // P + } + } +} diff --git a/src/ImageSharp/Formats/WebP/WebPMetadata.cs b/src/ImageSharp/Formats/WebP/WebPMetadata.cs new file mode 100644 index 000000000..95f50fe27 --- /dev/null +++ b/src/ImageSharp/Formats/WebP/WebPMetadata.cs @@ -0,0 +1,16 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; + +namespace SixLabors.ImageSharp.Formats.WebP +{ + /// + /// Provides WebP specific metadata information for the image. + /// + public class WebPMetadata : IDeepCloneable + { + /// + public IDeepCloneable DeepClone() => throw new NotImplementedException(); + } +}