mirror of https://github.com/SixLabors/ImageSharp
310 changed files with 4625 additions and 6889 deletions
@ -0,0 +1,12 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>netcoreapp1.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\ImageSharp.Drawing\ImageSharp.Drawing.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,71 @@ |
|||
|
|||
|
|||
namespace AvatarWithRoundedCorner |
|||
{ |
|||
using System; |
|||
using System.Numerics; |
|||
using ImageSharp; |
|||
using SixLabors.Primitives; |
|||
using SixLabors.Shapes; |
|||
|
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
System.IO.Directory.CreateDirectory("output"); |
|||
|
|||
GenerateAvatar("fb.jpg", "output/fb.png", new Size(200, 200), 20); |
|||
GenerateAvatar("fb.jpg", "output/fb-round.png", new Size(200, 200), 100); |
|||
GenerateAvatar("fb.jpg", "output/fb-rounder.png", new Size(200, 200), 150); |
|||
} |
|||
|
|||
private static void GenerateAvatar(string source, string destination, Size size, float cornerRadius) |
|||
{ |
|||
using (var image = Image.Load(source)) |
|||
{ |
|||
image.Resize(new ImageSharp.Processing.ResizeOptions |
|||
{ |
|||
Size = size, |
|||
Mode = ImageSharp.Processing.ResizeMode.Crop |
|||
}); |
|||
|
|||
ApplyRoundedCourners(image, cornerRadius); |
|||
image.Save(destination); |
|||
} |
|||
} |
|||
|
|||
public static void ApplyRoundedCourners(Image<Rgba32> img, float cornerRadius) |
|||
{ |
|||
var corners = BuildCorners(img.Width, img.Height, cornerRadius); |
|||
// now we have our corners time to draw them
|
|||
img.Fill(Rgba32.Transparent, corners, new GraphicsOptions(true) |
|||
{ |
|||
BlenderMode = ImageSharp.PixelFormats.PixelBlenderMode.Src // enforces that any part of this shape that has color is punched out of the background
|
|||
}); |
|||
} |
|||
|
|||
public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius) |
|||
{ |
|||
// first create a square
|
|||
var rect = new SixLabors.Shapes.RectangularePolygon(-0.5f, -0.5f, cornerRadius, cornerRadius); |
|||
|
|||
// then cut out of the square a circle so we are left with a corner
|
|||
var cornerToptLeft = rect.Clip(new SixLabors.Shapes.EllipsePolygon(cornerRadius-0.5f, cornerRadius - 0.5f, cornerRadius)); |
|||
|
|||
// corner is now a corner shape positions top left
|
|||
//lets make 3 more positioned correctly, we cando that by translating the orgional artound the center of the image
|
|||
var center = new Vector2(imageWidth / 2, imageHeight / 2); |
|||
var angle = Math.PI / 2f; |
|||
|
|||
float rightPos = imageWidth - cornerToptLeft.Bounds.Width +1; |
|||
float bottomPos = imageHeight - cornerToptLeft.Bounds.Height + 1; |
|||
|
|||
// move it across the widthof the image - the width of the shape
|
|||
var cornerTopRight = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0); |
|||
var cornerBottomLeft = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos); |
|||
var cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos); |
|||
|
|||
return new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
version https://git-lfs.github.com/spec/v1 |
|||
oid sha256:93bb4d6281dc1e845db57e836e0dca30b7a4062e81044efb27ad4d8b1a33130c |
|||
size 15787 |
|||
@ -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,80 @@ |
|||
// <copyright file="FillPaths.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
using System; |
|||
using Drawing; |
|||
using Drawing.Brushes; |
|||
using ImageSharp.PixelFormats; |
|||
using SixLabors.Shapes; |
|||
|
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image{TPixel}"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Flood fills the image in the shape of the provided polygon with the specified brush..
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The type of the color.</typeparam>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="brush">The brush.</param>
|
|||
/// <param name="path">The shape.</param>
|
|||
/// <param name="options">The graphics options.</param>
|
|||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
|||
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, Action<PathBuilder> path, GraphicsOptions options) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
var pb = new PathBuilder(); |
|||
path(pb); |
|||
|
|||
return source.Fill(brush, pb.Build(), options); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flood fills the image in the shape of the provided polygon with the specified brush.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The type of the color.</typeparam>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="brush">The brush.</param>
|
|||
/// <param name="path">The path.</param>
|
|||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
|||
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, IBrush<TPixel> brush, Action<PathBuilder> path) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
return source.Fill(brush, path, GraphicsOptions.Default); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flood fills the image in the shape of the provided polygon with the specified brush..
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The type of the color.</typeparam>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="color">The color.</param>
|
|||
/// <param name="path">The path.</param>
|
|||
/// <param name="options">The options.</param>
|
|||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
|||
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color, Action<PathBuilder> path, GraphicsOptions options) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
return source.Fill(new SolidBrush<TPixel>(color), path, options); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Flood fills the image in the shape of the provided polygon with the specified brush..
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The type of the color.</typeparam>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <param name="color">The color.</param>
|
|||
/// <param name="path">The path.</param>
|
|||
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
|
|||
public static Image<TPixel> Fill<TPixel>(this Image<TPixel> source, TPixel color, Action<PathBuilder> path) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
return source.Fill(new SolidBrush<TPixel>(color), path); |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
// <copyright file="RectangleExtensions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Drawing |
|||
{ |
|||
using System; |
|||
|
|||
/// <summary>
|
|||
/// Extension methods for helping to bridge Shaper2D and ImageSharp primitives.
|
|||
/// </summary>
|
|||
internal static class RectangleExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Converts a Shaper2D <see cref="SixLabors.Shapes.Rectangle"/> to an ImageSharp <see cref="Rectangle"/> by creating a <see cref="Rectangle"/> the entirely surrounds the source.
|
|||
/// </summary>
|
|||
/// <param name="source">The image this method extends.</param>
|
|||
/// <returns>A <see cref="Rectangle"/> representation of this <see cref="SixLabors.Shapes.Rectangle"/></returns>
|
|||
public static Rectangle Convert(this SixLabors.Shapes.Rectangle source) |
|||
{ |
|||
int left = (int)MathF.Floor(source.Left); |
|||
int right = (int)MathF.Ceiling(source.Right); |
|||
int top = (int)MathF.Floor(source.Top); |
|||
int bottom = (int)MathF.Ceiling(source.Bottom); |
|||
return new Rectangle(left, top, right - left, bottom - top); |
|||
} |
|||
} |
|||
} |
|||
@ -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.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
namespace ImageSharp.Formats |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
|
|||
using ImageSharp.PixelFormats; |
|||
|
|||
/// <summary>
|
|||
/// Encapsulates the shared decoder options.
|
|||
/// Image decoder for generating an image out of a jpg stream.
|
|||
/// </summary>
|
|||
public interface IDecoderOptions |
|||
internal interface IJpegDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// 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); |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue