mirror of https://github.com/SixLabors/ImageSharp
113 changed files with 1966 additions and 2256 deletions
@ -0,0 +1,12 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>netcoreapp1.1</TargetFramework> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\ImageSharp\ImageSharp.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,20 @@ |
|||||
|
using System; |
||||
|
using ImageSharp; |
||||
|
using ImageSharp.Formats; |
||||
|
|
||||
|
namespace ChangeDefaultEncoderOptions |
||||
|
{ |
||||
|
class Program |
||||
|
{ |
||||
|
static void Main(string[] args) |
||||
|
{ |
||||
|
// lets switch out the default encoder for jpeg to one
|
||||
|
// that saves at 90 quality and ignores the matadata
|
||||
|
Configuration.Default.SetEncoder(ImageFormats.Jpeg, new ImageSharp.Formats.JpegEncoder() |
||||
|
{ |
||||
|
Quality = 90, |
||||
|
IgnoreMetadata = true |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
// <copyright file="BmpConfigurationModule.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>
|
||||
|
/// Registers the image encoders, decoders and mime type detectors for the bmp format.
|
||||
|
/// </summary>
|
||||
|
public sealed class BmpConfigurationModule : IConfigurationModule |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public void Configure(Configuration config) |
||||
|
{ |
||||
|
config.SetEncoder(ImageFormats.Bitmap, new BmpEncoder()); |
||||
|
config.SetDecoder(ImageFormats.Bitmap, new BmpDecoder()); |
||||
|
config.AddImageFormatDetector(new BmpImageFormatDetector()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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 file extensions 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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,37 @@ |
|||||
|
// <copyright file="BmpImageFormatDetector.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; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Detects bmp file headers
|
||||
|
/// </summary>
|
||||
|
public sealed class BmpImageFormatDetector : IImageFormatDetector |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public int HeaderSize => 2; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
if (this.IsSupportedFileFormat(header)) |
||||
|
{ |
||||
|
return ImageFormats.Bitmap; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
return header.Length >= this.HeaderSize && |
||||
|
header[0] == 0x42 && // B
|
||||
|
header[1] == 0x4D; // M
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
// <copyright file="BmpDecoder.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 ImageSharp.PixelFormats; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Image decoder options for decoding Windows bitmap streams.
|
||||
|
/// </summary>
|
||||
|
internal interface IBmpDecoderOptions |
||||
|
{ |
||||
|
// added this for consistancy so we can add stuff as required, no options currently availible
|
||||
|
} |
||||
|
} |
||||
@ -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; |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,22 @@ |
|||||
|
// <copyright file="GifConfigurationModule.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>
|
||||
|
/// Registers the image encoders, decoders and mime type detectors for the gif format.
|
||||
|
/// </summary>
|
||||
|
public sealed class GifConfigurationModule : IConfigurationModule |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public void Configure(Configuration config) |
||||
|
{ |
||||
|
config.SetEncoder(ImageFormats.Gif, new GifEncoder()); |
||||
|
config.SetDecoder(ImageFormats.Gif, new GifDecoder()); |
||||
|
|
||||
|
config.AddImageFormatDetector(new GifImageFormatDetector()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,41 @@ |
|||||
|
// <copyright file="GifImageFormatDetector.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; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Detects gif file headers
|
||||
|
/// </summary>
|
||||
|
public sealed class GifImageFormatDetector : IImageFormatDetector |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public int HeaderSize => 6; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
if (this.IsSupportedFileFormat(header)) |
||||
|
{ |
||||
|
return ImageFormats.Gif; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
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,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; } |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,30 @@ |
|||||
|
// <copyright file="IImageFormatDetector.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 IImageFormatDetector |
||||
|
{ |
||||
|
/// <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>
|
||||
|
IImageFormat DetectFormat(ReadOnlySpan<byte> header); |
||||
|
} |
||||
|
} |
||||
@ -1,14 +1,20 @@ |
|||||
// <copyright file="IDecoderOptions.cs" company="James Jackson-South">
|
// <copyright file="JpegDecoder.cs" company="James Jackson-South">
|
||||
// Copyright (c) James Jackson-South and contributors.
|
// Copyright (c) James Jackson-South and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
// </copyright>
|
// </copyright>
|
||||
|
|
||||
namespace ImageSharp |
namespace ImageSharp.Formats |
||||
{ |
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
|
||||
|
using ImageSharp.PixelFormats; |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Encapsulates the shared decoder options.
|
/// Image decoder for generating an image out of a jpg stream.
|
||||
/// </summary>
|
/// </summary>
|
||||
public interface IDecoderOptions |
internal interface IJpegDecoderOptions |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
|
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
|
||||
@ -0,0 +1,22 @@ |
|||||
|
// <copyright file="JpegConfigurationModule.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>
|
||||
|
/// Registers the image encoders, decoders and mime type detectors for the jpeg format.
|
||||
|
/// </summary>
|
||||
|
public sealed class JpegConfigurationModule : IConfigurationModule |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public void Configure(Configuration config) |
||||
|
{ |
||||
|
config.SetEncoder(ImageFormats.Jpeg, new JpegEncoder()); |
||||
|
config.SetDecoder(ImageFormats.Jpeg, new JpegDecoder()); |
||||
|
|
||||
|
config.AddImageFormatDetector(new JpegImageFormatDetector()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,87 @@ |
|||||
|
// <copyright file="JpegImageFormatDetector.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; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Detects Jpeg file headers
|
||||
|
/// </summary>
|
||||
|
public sealed class JpegImageFormatDetector : IImageFormatDetector |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public int HeaderSize => 11; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
if (this.IsSupportedFileFormat(header)) |
||||
|
{ |
||||
|
return ImageFormats.Jpeg; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private bool IsSupportedFileFormat(ReadOnlySpan<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(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
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(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
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(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
bool isJpg = |
||||
|
header[0] == 0xFF && // 255
|
||||
|
header[1] == 0xD8; // 216
|
||||
|
|
||||
|
return isJpg; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
// <copyright file="PngConfigurationModule.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>
|
||||
|
/// Registers the image encoders, decoders and mime type detectors for the png format.
|
||||
|
/// </summary>
|
||||
|
public sealed class PngConfigurationModule : IConfigurationModule |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public void Configure(Configuration host) |
||||
|
{ |
||||
|
host.SetEncoder(ImageFormats.Png, new PngEncoder()); |
||||
|
host.SetDecoder(ImageFormats.Png, new PngDecoder()); |
||||
|
host.AddImageFormatDetector(new PngImageFormatDetector()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// <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 png.
|
||||
|
/// </summary>
|
||||
|
public static readonly IEnumerable<string> MimeTypes = new[] { "image/png" }; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The list of file extensions that equate to a png.
|
||||
|
/// </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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,43 @@ |
|||||
|
// <copyright file="PngImageFormatDetector.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; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Detects png file headers
|
||||
|
/// </summary>
|
||||
|
public sealed class PngImageFormatDetector : IImageFormatDetector |
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public int HeaderSize => 8; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
if (this.IsSupportedFileFormat(header)) |
||||
|
{ |
||||
|
return ImageFormats.Png; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header) |
||||
|
{ |
||||
|
// TODO: This should be in constants
|
||||
|
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
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
// <copyright file="IConfigurationModule.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Represents an interface that can register image encoders, decoders and image format detectors.
|
||||
|
/// </summary>
|
||||
|
public interface IConfigurationModule |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Called when loaded into a configuration object so the module can register items into the configuration.
|
||||
|
/// </summary>
|
||||
|
/// <param name="configuration">The configuration that will retain the encoders, decodes and mime type detectors.</param>
|
||||
|
void Configure(Configuration configuration); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// <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 |
||||
|
{ |
||||
|
using ImageSharp.Formats; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The static collection of all the default image formats
|
||||
|
/// </summary>
|
||||
|
public static class ImageFormats |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The format details for the jpegs.
|
||||
|
/// </summary>
|
||||
|
public static readonly IImageFormat Jpeg = new JpegFormat(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The format details for the pngs.
|
||||
|
/// </summary>
|
||||
|
public static readonly IImageFormat Png = new PngFormat(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The format details for the gifs.
|
||||
|
/// </summary>
|
||||
|
public static readonly IImageFormat Gif = new GifFormat(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The format details for the bitmaps.
|
||||
|
/// </summary>
|
||||
|
public static readonly IImageFormat Bitmap = new BmpFormat(); |
||||
|
} |
||||
|
} |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue