mirror of https://github.com/SixLabors/ImageSharp
Browse Source
refactor encoders to remove IImageFormat and split the logic across Encoders and Decoders. remove IXXXOptions and moved setting onto encoders/decoders directly. add out param on Image.Load APIs to output mime type of loaded image.pull/254/head
86 changed files with 1094 additions and 2433 deletions
@ -0,0 +1,25 @@ |
|||
// <copyright file="BmpConstants.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Defines constants relating to BMPs
|
|||
/// </summary>
|
|||
internal static class BmpConstants |
|||
{ |
|||
/// <summary>
|
|||
/// The list of mimetypes that equate to a bmp
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> MimeTypes = new[] { "image/bmp", "image/x-windows-bmp" }; |
|||
|
|||
/// <summary>
|
|||
/// The list of mimetypes that equate to a bmp
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> FileExtensions = new[] { "bm", "bmp", "dip" }; |
|||
} |
|||
} |
|||
@ -1,45 +0,0 @@ |
|||
// <copyright file="BmpEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="BmpEncoder"/>.
|
|||
/// </summary>
|
|||
public sealed class BmpEncoderOptions : EncoderOptions, IBmpEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="BmpEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public BmpEncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="BmpEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
private BmpEncoderOptions(IEncoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the number of bits per pixel.
|
|||
/// </summary>
|
|||
public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24; |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IBmpEncoderOptions"/> instance with a cast
|
|||
/// or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
/// <returns>The options for the <see cref="BmpEncoder"/>.</returns>
|
|||
internal static IBmpEncoderOptions Create(IEncoderOptions options) |
|||
{ |
|||
return options as IBmpEncoderOptions ?? new BmpEncoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
// <copyright file="BmpFormat.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the means to encode and decode bitmap images.
|
|||
/// </summary>
|
|||
public class BmpFormat : IImageFormat |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public string MimeType => "image/bmp"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string Extension => "bmp"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> SupportedExtensions => new string[] { "bmp", "dip" }; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageDecoder Decoder => new BmpDecoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageEncoder Encoder => new BmpEncoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 2; |
|||
|
|||
/// <inheritdoc/>
|
|||
public bool IsSupportedFileFormat(byte[] header) |
|||
{ |
|||
return header.Length >= this.HeaderSize && |
|||
header[0] == 0x42 && // B
|
|||
header[1] == 0x4D; // M
|
|||
} |
|||
} |
|||
} |
|||
@ -1,18 +0,0 @@ |
|||
// <copyright file="IBmpEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="BmpEncoder"/>.
|
|||
/// </summary>
|
|||
public interface IBmpEncoderOptions : IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the number of bits per pixel.
|
|||
/// </summary>
|
|||
BmpBitsPerPixel BitsPerPixel { get; } |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// <copyright file="DecoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the shared decoder options.
|
|||
/// </summary>
|
|||
public class DecoderOptions : IDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DecoderOptions"/> class.
|
|||
/// </summary>
|
|||
public DecoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DecoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The decoder options</param>
|
|||
protected DecoderOptions(IDecoderOptions options) |
|||
{ |
|||
if (options != null) |
|||
{ |
|||
this.IgnoreMetadata = options.IgnoreMetadata; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
|
|||
/// </summary>
|
|||
public bool IgnoreMetadata { get; set; } = false; |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// <copyright file="EncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the shared encoder options.
|
|||
/// </summary>
|
|||
public class EncoderOptions : IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="EncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public EncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="EncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The encoder options</param>
|
|||
protected EncoderOptions(IEncoderOptions options) |
|||
{ |
|||
if (options != null) |
|||
{ |
|||
this.IgnoreMetadata = options.IgnoreMetadata; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being encoded.
|
|||
/// </summary>
|
|||
public bool IgnoreMetadata { get; set; } = false; |
|||
} |
|||
} |
|||
@ -1,47 +0,0 @@ |
|||
// <copyright file="GifDecoderOptions.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.Text; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="GifDecoder"/>.
|
|||
/// </summary>
|
|||
public sealed class GifDecoderOptions : DecoderOptions, IGifDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GifDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
public GifDecoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GifDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the decoder.</param>
|
|||
private GifDecoderOptions(IDecoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the encoding that should be used when reading comments.
|
|||
/// </summary>
|
|||
public Encoding TextEncoding { get; set; } = GifConstants.DefaultEncoding; |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IGifDecoderOptions"/> instance with a cast
|
|||
/// or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the decoder.</param>
|
|||
/// <returns>The options for the <see cref="GifDecoder"/>.</returns>
|
|||
internal static IGifDecoderOptions Create(IDecoderOptions options) |
|||
{ |
|||
return options as IGifDecoderOptions ?? new GifDecoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,65 +0,0 @@ |
|||
// <copyright file="GifEncoderOptions.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.Text; |
|||
|
|||
using Quantizers; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="GifEncoder"/>.
|
|||
/// </summary>
|
|||
public sealed class GifEncoderOptions : EncoderOptions, IGifEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GifEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public GifEncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="GifEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
private GifEncoderOptions(IEncoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the encoding that should be used when writing comments.
|
|||
/// </summary>
|
|||
public Encoding TextEncoding { get; set; } = GifConstants.DefaultEncoding; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the quality of output for images.
|
|||
/// </summary>
|
|||
/// <remarks>For gifs the value ranges from 1 to 256.</remarks>
|
|||
public int Quality { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the transparency threshold.
|
|||
/// </summary>
|
|||
public byte Threshold { get; set; } = 128; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the quantizer for reducing the color count.
|
|||
/// </summary>
|
|||
public IQuantizer Quantizer { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IGifEncoderOptions"/> instance with a
|
|||
/// cast or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
/// <returns>The options for the <see cref="GifEncoder"/>.</returns>
|
|||
internal static IGifEncoderOptions Create(IEncoderOptions options) |
|||
{ |
|||
return options as IGifEncoderOptions ?? new GifEncoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,45 +0,0 @@ |
|||
// <copyright file="GifFormat.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the means to encode and decode gif images.
|
|||
/// </summary>
|
|||
public class GifFormat : IImageFormat |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public string Extension => "gif"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string MimeType => "image/gif"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> SupportedExtensions => new string[] { "gif" }; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageDecoder Decoder => new GifDecoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageEncoder Encoder => new GifEncoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 6; |
|||
|
|||
/// <inheritdoc/>
|
|||
public bool IsSupportedFileFormat(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
|
|||
} |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
// <copyright file="IGifDecoderOptions.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.Text; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="GifDecoder"/>.
|
|||
/// </summary>
|
|||
public interface IGifDecoderOptions : IDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the encoding that should be used when reading comments.
|
|||
/// </summary>
|
|||
Encoding TextEncoding { get; } |
|||
} |
|||
} |
|||
@ -1,38 +0,0 @@ |
|||
// <copyright file="IGifEncoderOptions.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.Text; |
|||
|
|||
using Quantizers; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="GifEncoder"/>.
|
|||
/// </summary>
|
|||
public interface IGifEncoderOptions : IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the encoding that should be used when writing comments.
|
|||
/// </summary>
|
|||
Encoding TextEncoding { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the quality of output for images.
|
|||
/// </summary>
|
|||
/// <remarks>For gifs the value ranges from 1 to 256.</remarks>
|
|||
int Quality { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the transparency threshold.
|
|||
/// </summary>
|
|||
byte Threshold { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the quantizer for reducing the color count.
|
|||
/// </summary>
|
|||
IQuantizer Quantizer { get; } |
|||
} |
|||
} |
|||
@ -1,18 +0,0 @@ |
|||
// <copyright file="IDecoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the shared decoder options.
|
|||
/// </summary>
|
|||
public interface IDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
|
|||
/// </summary>
|
|||
bool IgnoreMetadata { get; } |
|||
} |
|||
} |
|||
@ -1,18 +0,0 @@ |
|||
// <copyright file="IEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the shared encoder options.
|
|||
/// </summary>
|
|||
public interface IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets a value indicating whether the metadata should be ignored when the image is being encoded.
|
|||
/// </summary>
|
|||
bool IgnoreMetadata { get; } |
|||
} |
|||
} |
|||
@ -1,60 +0,0 @@ |
|||
// <copyright file="IImageFormat.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates a supported image format, providing means to encode and decode an image.
|
|||
/// Individual formats implements in this interface must be registered in the <see cref="Configuration"/>
|
|||
/// </summary>
|
|||
public interface IImageFormat |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the standard identifier used on the Internet to indicate the type of data that a file contains.
|
|||
/// </summary>
|
|||
string MimeType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the default file extension for this format.
|
|||
/// </summary>
|
|||
string Extension { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the supported file extensions for this format.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The supported file extension.
|
|||
/// </returns>
|
|||
IEnumerable<string> SupportedExtensions { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the image encoder for encoding an image from a stream.
|
|||
/// </summary>
|
|||
IImageEncoder Encoder { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the image decoder for decoding an image from a stream.
|
|||
/// </summary>
|
|||
IImageDecoder Decoder { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the size of the header for this image type.
|
|||
/// </summary>
|
|||
/// <value>The size of the header.</value>
|
|||
int HeaderSize { get; } |
|||
|
|||
/// <summary>
|
|||
/// Returns a value indicating whether the <see cref="IImageDecoder"/> supports the specified
|
|||
/// file header.
|
|||
/// </summary>
|
|||
/// <param name="header">The <see cref="T:byte[]"/> containing the file header.</param>
|
|||
/// <returns>
|
|||
/// True if the decoder supports the file header; otherwise, false.
|
|||
/// </returns>
|
|||
bool IsSupportedFileFormat(byte[] header); |
|||
} |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
// <copyright file="IJpegEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="JpegEncoder"/>.
|
|||
/// </summary>
|
|||
public interface IJpegEncoderOptions : IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the quality, that will be used to encode the image. Quality
|
|||
/// index must be between 0 and 100 (compression from max to min).
|
|||
/// </summary>
|
|||
/// <value>The quality of the jpg image from 0 to 100.</value>
|
|||
int Quality { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the subsample ration, that will be used to encode the image.
|
|||
/// </summary>
|
|||
/// <value>The subsample ratio of the jpg image.</value>
|
|||
JpegSubsample? Subsample { get; } |
|||
} |
|||
} |
|||
@ -1,56 +0,0 @@ |
|||
// <copyright file="JpegEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="JpegEncoder"/>.
|
|||
/// </summary>
|
|||
public sealed class JpegEncoderOptions : EncoderOptions, IJpegEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="JpegEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public JpegEncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="JpegEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
private JpegEncoderOptions(IEncoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the quality, that will be used to encode the image. Quality
|
|||
/// index must be between 0 and 100 (compression from max to min).
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// If the quality is less than or equal to 90, the subsampling ratio will switch to <see cref="JpegSubsample.Ratio420"/>
|
|||
/// </remarks>
|
|||
/// <value>The quality of the jpg image from 0 to 100.</value>
|
|||
public int Quality { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the subsample ration, that will be used to encode the image.
|
|||
/// </summary>
|
|||
/// <value>The subsample ratio of the jpg image.</value>
|
|||
public JpegSubsample? Subsample { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IJpegEncoderOptions"/> instance with a
|
|||
/// cast or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
/// <returns>The options for the <see cref="JpegEncoder"/>.</returns>
|
|||
internal static IJpegEncoderOptions Create(IEncoderOptions options) |
|||
{ |
|||
return options as IJpegEncoderOptions ?? new JpegEncoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,89 +0,0 @@ |
|||
// <copyright file="JpegFormat.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the means to encode and decode jpeg images.
|
|||
/// </summary>
|
|||
public class JpegFormat : IImageFormat |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public string MimeType => "image/jpeg"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string Extension => "jpg"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> SupportedExtensions => new string[] { "jpg", "jpeg", "jfif" }; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageDecoder Decoder => new JpegDecoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageEncoder Encoder => new JpegEncoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 11; |
|||
|
|||
/// <inheritdoc/>
|
|||
public bool IsSupportedFileFormat(byte[] header) |
|||
{ |
|||
return header.Length >= this.HeaderSize && |
|||
(IsJfif(header) || IsExif(header) || 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 static bool IsJfif(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 static bool IsExif(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 static bool IsJpeg(byte[] header) |
|||
{ |
|||
bool isJpg = |
|||
header[0] == 0xFF && // 255
|
|||
header[1] == 0xD8; // 216
|
|||
|
|||
return isJpg; |
|||
} |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
// <copyright file="IPngDecoderOptions.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.Text; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="PngDecoder"/>.
|
|||
/// </summary>
|
|||
public interface IPngDecoderOptions : IDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the encoding that should be used when reading text chunks.
|
|||
/// </summary>
|
|||
Encoding TextEncoding { get; } |
|||
} |
|||
} |
|||
@ -1,54 +0,0 @@ |
|||
// <copyright file="IPngEncoderOptions.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 Quantizers; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="PngEncoder"/>.
|
|||
/// </summary>
|
|||
public interface IPngEncoderOptions : IEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the quality of output for images.
|
|||
/// </summary>
|
|||
int Quality { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the png color type
|
|||
/// </summary>
|
|||
PngColorType PngColorType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the compression level 1-9.
|
|||
/// </summary>
|
|||
int CompressionLevel { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the gamma value, that will be written
|
|||
/// the the stream, when the <see cref="WriteGamma"/> property
|
|||
/// is set to true.
|
|||
/// </summary>
|
|||
/// <value>The gamma value of the image.</value>
|
|||
float Gamma { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets quantizer for reducing the color count.
|
|||
/// </summary>
|
|||
IQuantizer Quantizer { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the transparency threshold.
|
|||
/// </summary>
|
|||
byte Threshold { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether this instance should write
|
|||
/// gamma information to the stream.
|
|||
/// </summary>
|
|||
bool WriteGamma { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
// <copyright file="PngConstants.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.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
/// <summary>
|
|||
/// Defines png constants defined in the specification.
|
|||
/// </summary>
|
|||
internal static class PngConstants |
|||
{ |
|||
/// <summary>
|
|||
/// The default encoding for text metadata
|
|||
/// </summary>
|
|||
public static readonly Encoding DefaultEncoding = Encoding.GetEncoding("ASCII"); |
|||
|
|||
/// <summary>
|
|||
/// The list of mimetypes that equate to a jpeg
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> MimeTypes = new[] { "image/png" }; |
|||
|
|||
/// <summary>
|
|||
/// The list of mimetypes that equate to a jpeg
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> FileExtensions = new[] { "png" }; |
|||
} |
|||
} |
|||
@ -1,49 +0,0 @@ |
|||
// <copyright file="PngDecoderOptions.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.Text; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="PngDecoder"/>.
|
|||
/// </summary>
|
|||
public sealed class PngDecoderOptions : DecoderOptions, IPngDecoderOptions |
|||
{ |
|||
private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("ASCII"); |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
public PngDecoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngDecoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the decoder.</param>
|
|||
private PngDecoderOptions(IDecoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the encoding that should be used when reading text chunks.
|
|||
/// </summary>
|
|||
public Encoding TextEncoding { get; set; } = DefaultEncoding; |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IPngDecoderOptions"/> instance with a cast
|
|||
/// or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the decoder.</param>
|
|||
/// <returns>The options for the <see cref="PngDecoder"/>.</returns>
|
|||
internal static IPngDecoderOptions Create(IDecoderOptions options) |
|||
{ |
|||
return options as IPngDecoderOptions ?? new PngDecoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,82 +0,0 @@ |
|||
// <copyright file="PngEncoderOptions.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 Quantizers; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="PngEncoder"/>.
|
|||
/// </summary>
|
|||
public sealed class PngEncoderOptions : EncoderOptions, IPngEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public PngEncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PngEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
private PngEncoderOptions(IEncoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the quality of output for images.
|
|||
/// </summary>
|
|||
public int Quality { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the png color type
|
|||
/// </summary>
|
|||
public PngColorType PngColorType { get; set; } = PngColorType.RgbWithAlpha; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the compression level 1-9.
|
|||
/// <remarks>Defaults to 6.</remarks>
|
|||
/// </summary>
|
|||
public int CompressionLevel { get; set; } = 6; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the gamma value, that will be written
|
|||
/// the the stream, when the <see cref="WriteGamma"/> property
|
|||
/// is set to true. The default value is 2.2F.
|
|||
/// </summary>
|
|||
/// <value>The gamma value of the image.</value>
|
|||
public float Gamma { get; set; } = 2.2F; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets quantizer for reducing the color count.
|
|||
/// </summary>
|
|||
public IQuantizer Quantizer { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the transparency threshold.
|
|||
/// </summary>
|
|||
public byte Threshold { get; set; } = 255; |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets a value indicating whether this instance should write
|
|||
/// gamma information to the stream. The default value is false.
|
|||
/// </summary>
|
|||
public bool WriteGamma { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="IPngEncoderOptions"/> instance with a
|
|||
/// cast or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
/// <returns>The options for the <see cref="PngEncoder"/>.</returns>
|
|||
internal static IPngEncoderOptions Create(IEncoderOptions options) |
|||
{ |
|||
return options as IPngEncoderOptions ?? new PngEncoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -1,47 +0,0 @@ |
|||
// <copyright file="PngFormat.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.Collections.Generic; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the means to encode and decode png images.
|
|||
/// </summary>
|
|||
public class PngFormat : IImageFormat |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public string MimeType => "image/png"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string Extension => "png"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> SupportedExtensions => new string[] { "png" }; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageDecoder Decoder => new PngDecoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageEncoder Encoder => new PngEncoder(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 8; |
|||
|
|||
/// <inheritdoc/>
|
|||
public bool IsSupportedFileFormat(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