mirror of https://github.com/SixLabors/ImageSharp
31 changed files with 884 additions and 312 deletions
@ -0,0 +1,42 @@ |
|||
// <copyright file="PngImageFormatProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects gif file headers
|
|||
/// </summary>
|
|||
public class BmpImageFormatProvider : IImageFormatProvider |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(IImageFormatHost host) |
|||
{ |
|||
var encoder = new BmpEncoder(); |
|||
foreach (string mimeType in BmpConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeEncoder(mimeType, encoder); |
|||
} |
|||
|
|||
foreach (string mimeType in BmpConstants.FileExtensions) |
|||
{ |
|||
host.SetFileExtensionEncoder(mimeType, encoder); |
|||
} |
|||
|
|||
var decoder = new BmpDecoder(); |
|||
foreach (string mimeType in BmpConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeDecoder(mimeType, decoder); |
|||
} |
|||
|
|||
host.AddMimeTypeDetector(new BmpMimeTypeDetector()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
// <copyright file="PngMimeTypeDetector.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects bmp file headers
|
|||
/// </summary>
|
|||
internal class BmpMimeTypeDetector : IMimeTypeDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 2; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string DetectMimeType(Span<byte> header) |
|||
{ |
|||
if (this.IsSupportedFileFormat(header)) |
|||
{ |
|||
return "image/bmp"; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private bool IsSupportedFileFormat(Span<byte> header) |
|||
{ |
|||
return header.Length >= this.HeaderSize && |
|||
header[0] == 0x42 && // B
|
|||
header[1] == 0x4D; // M
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
// <copyright file="PngImageFormatProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects gif file headers
|
|||
/// </summary>
|
|||
public class GifImageFormatProvider : IImageFormatProvider |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(IImageFormatHost host) |
|||
{ |
|||
var encoder = new GifEncoder(); |
|||
foreach (string mimeType in GifConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeEncoder(mimeType, encoder); |
|||
} |
|||
|
|||
foreach (string mimeType in GifConstants.FileExtensions) |
|||
{ |
|||
host.SetFileExtensionEncoder(mimeType, encoder); |
|||
} |
|||
|
|||
var decoder = new GifDecoder(); |
|||
foreach (string mimeType in GifConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeDecoder(mimeType, decoder); |
|||
} |
|||
|
|||
host.AddMimeTypeDetector(new GifMimeTypeDetector()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// <copyright file="PngMimeTypeDetector.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects gif file headers
|
|||
/// </summary>
|
|||
public class GifMimeTypeDetector : IMimeTypeDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 6; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string DetectMimeType(Span<byte> header) |
|||
{ |
|||
if (this.IsSupportedFileFormat(header)) |
|||
{ |
|||
return "image/gif"; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private bool IsSupportedFileFormat(Span<byte> header) |
|||
{ |
|||
return header.Length >= this.HeaderSize && |
|||
header[0] == 0x47 && // G
|
|||
header[1] == 0x49 && // I
|
|||
header[2] == 0x46 && // F
|
|||
header[3] == 0x38 && // 8
|
|||
(header[4] == 0x39 || header[4] == 0x37) && // 9 or 7
|
|||
header[5] == 0x61; // a
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
// <copyright file="IImageFormatProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
/// <summary>
|
|||
/// Represents an abstract class that can register image encoders, decoders and mime type detectors
|
|||
/// </summary>
|
|||
public interface IImageFormatProvider |
|||
{ |
|||
/// <summary>
|
|||
/// Called when loaded so the provider and register its encoders, decodes and mime type detectors into an IImageFormatHost.
|
|||
/// </summary>
|
|||
/// <param name="host">The host that will retain the encoders, decodes and mime type detectors.</param>
|
|||
void Configure(IImageFormatHost host); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Represents an abstract class that can have encoders decoders and mimetype detecotrs loaded into.
|
|||
/// </summary>
|
|||
public interface IImageFormatHost |
|||
{ |
|||
/// <summary>
|
|||
/// Sets a specific image encoder as the encoder for a specific mimetype
|
|||
/// </summary>
|
|||
/// <param name="mimeType">the target mimetype</param>
|
|||
/// <param name="encoder">the encoder to use</param>
|
|||
void SetMimeTypeEncoder(string mimeType, IImageEncoder encoder); // could/should this be an Action<IImageEncoder>???
|
|||
|
|||
/// <summary>
|
|||
/// Sets a specific image encoder as the encoder for a specific mimetype
|
|||
/// </summary>
|
|||
/// <param name="extension">the target mimetype</param>
|
|||
/// <param name="encoder">the encoder to use</param>
|
|||
void SetFileExtensionEncoder(string extension, IImageEncoder encoder); |
|||
|
|||
/// <summary>
|
|||
/// Sets a specific image decoder as the decoder for a specific mimetype
|
|||
/// </summary>
|
|||
/// <param name="mimeType">the target mimetype</param>
|
|||
/// <param name="decoder">the decoder to use</param>
|
|||
void SetMimeTypeDecoder(string mimeType, IImageDecoder decoder); |
|||
|
|||
/// <summary>
|
|||
/// Adds a new detector for detecting in mime types
|
|||
/// </summary>
|
|||
/// <param name="detector">The detector</param>
|
|||
void AddMimeTypeDetector(IMimeTypeDetector detector); |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
// <copyright file="IMimeTypeDetector.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
/// <summary>
|
|||
/// Used for detecting mime types from a file header
|
|||
/// </summary>
|
|||
public interface IMimeTypeDetector |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the size of the header for this image type.
|
|||
/// </summary>
|
|||
/// <value>The size of the header.</value>
|
|||
int HeaderSize { get; } |
|||
|
|||
/// <summary>
|
|||
/// Detect mimetype
|
|||
/// </summary>
|
|||
/// <param name="header">The <see cref="T:byte[]"/> containing the file header.</param>
|
|||
/// <returns>returns the mime type of detected othersie returns null</returns>
|
|||
string DetectMimeType(Span<byte> header); |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
// <copyright file="PngImageFormatProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects png file headers
|
|||
/// </summary>
|
|||
public class JpegImageFormatProvider : IImageFormatProvider |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(IImageFormatHost host) |
|||
{ |
|||
var pngEncoder = new JpegEncoder(); |
|||
foreach (string mimeType in JpegConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeEncoder(mimeType, pngEncoder); |
|||
} |
|||
|
|||
foreach (string mimeType in JpegConstants.FileExtensions) |
|||
{ |
|||
host.SetFileExtensionEncoder(mimeType, pngEncoder); |
|||
} |
|||
|
|||
var pngDecoder = new JpegDecoder(); |
|||
foreach (string mimeType in JpegConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeDecoder(mimeType, pngDecoder); |
|||
} |
|||
|
|||
host.AddMimeTypeDetector(new JpegMimeTypeDetector()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,88 @@ |
|||
// <copyright file="PngMimeTypeDetector.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects Jpeg file headers
|
|||
/// </summary>
|
|||
public class JpegMimeTypeDetector : IMimeTypeDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 11; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string DetectMimeType(Span<byte> header) |
|||
{ |
|||
if (this.IsSupportedFileFormat(header)) |
|||
{ |
|||
return "image/jpeg"; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private bool IsSupportedFileFormat(Span<byte> header) |
|||
{ |
|||
return header.Length >= this.HeaderSize && |
|||
(this.IsJfif(header) || this.IsExif(header) || this.IsJpeg(header)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a value indicating whether the given bytes identify Jfif data.
|
|||
/// </summary>
|
|||
/// <param name="header">The bytes representing the file header.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool IsJfif(Span<byte> header) |
|||
{ |
|||
bool isJfif = |
|||
header[6] == 0x4A && // J
|
|||
header[7] == 0x46 && // F
|
|||
header[8] == 0x49 && // I
|
|||
header[9] == 0x46 && // F
|
|||
header[10] == 0x00; |
|||
|
|||
return isJfif; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a value indicating whether the given bytes identify EXIF data.
|
|||
/// </summary>
|
|||
/// <param name="header">The bytes representing the file header.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool IsExif(Span<byte> header) |
|||
{ |
|||
bool isExif = |
|||
header[6] == 0x45 && // E
|
|||
header[7] == 0x78 && // X
|
|||
header[8] == 0x69 && // I
|
|||
header[9] == 0x66 && // F
|
|||
header[10] == 0x00; |
|||
|
|||
return isExif; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a value indicating whether the given bytes identify Jpeg data.
|
|||
/// This is a last chance resort for jpegs that contain ICC information.
|
|||
/// </summary>
|
|||
/// <param name="header">The bytes representing the file header.</param>
|
|||
/// <returns>The <see cref="bool"/></returns>
|
|||
private bool IsJpeg(Span<byte> header) |
|||
{ |
|||
bool isJpg = |
|||
header[0] == 0xFF && // 255
|
|||
header[1] == 0xD8; // 216
|
|||
|
|||
return isJpg; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
// <copyright file="PngImageFormatProvider.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects png file headers
|
|||
/// </summary>
|
|||
public class PngImageFormatProvider : IImageFormatProvider |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(IImageFormatHost host) |
|||
{ |
|||
var pngEncoder = new PngEncoder(); |
|||
foreach (string mimeType in PngConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeEncoder(mimeType, pngEncoder); |
|||
} |
|||
|
|||
foreach (string mimeType in PngConstants.FileExtensions) |
|||
{ |
|||
host.SetFileExtensionEncoder(mimeType, pngEncoder); |
|||
} |
|||
|
|||
var pngDecoder = new PngDecoder(); |
|||
foreach (string mimeType in PngConstants.MimeTypes) |
|||
{ |
|||
host.SetMimeTypeDecoder(mimeType, pngDecoder); |
|||
} |
|||
|
|||
host.AddMimeTypeDetector(new PngMimeTypeDetector()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// <copyright file="PngMimeTypeDetector.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Detects png file headers
|
|||
/// </summary>
|
|||
public class PngMimeTypeDetector : IMimeTypeDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 8; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string DetectMimeType(Span<byte> header) |
|||
{ |
|||
if (this.IsSupportedFileFormat(header)) |
|||
{ |
|||
return "image/png"; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private bool IsSupportedFileFormat(Span<byte> header) |
|||
{ |
|||
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
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue