mirror of https://github.com/SixLabors/ImageSharp
26 changed files with 571 additions and 484 deletions
@ -1,56 +0,0 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a box color cube.
|
|||
/// </summary>
|
|||
internal struct Box |
|||
{ |
|||
/// <summary>
|
|||
/// Gets or sets the min red value, exclusive.
|
|||
/// </summary>
|
|||
public int R0 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the max red value, inclusive.
|
|||
/// </summary>
|
|||
public int R1 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the min green value, exclusive.
|
|||
/// </summary>
|
|||
public int G0 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the max green value, inclusive.
|
|||
/// </summary>
|
|||
public int G1 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the min blue value, exclusive.
|
|||
/// </summary>
|
|||
public int B0 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the max blue value, inclusive.
|
|||
/// </summary>
|
|||
public int B1 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the min alpha value, exclusive.
|
|||
/// </summary>
|
|||
public int A0 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the max alpha value, inclusive.
|
|||
/// </summary>
|
|||
public int A1 { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the volume.
|
|||
/// </summary>
|
|||
public int Volume { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods to allow the execution of the quantization process on an image frame.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
public interface IFrameQuantizer<TPixel> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <summary>
|
|||
/// Gets a value indicating whether to apply dithering to the output image.
|
|||
/// </summary>
|
|||
bool Dither { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the dithering algorithm to apply to the output image.
|
|||
/// </summary>
|
|||
IErrorDiffuser DitherType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Quantize an image frame and return the resulting output pixels.
|
|||
/// </summary>
|
|||
/// <param name="image">The image to quantize.</param>
|
|||
/// <returns>
|
|||
/// A <see cref="QuantizedFrame{TPixel}"/> representing a quantized version of the image pixels.
|
|||
/// </returns>
|
|||
QuantizedFrame<TPixel> QuantizeFrame(ImageFrame<TPixel> image); |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
using SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods for allowing quantization of images pixels with configurable dithering.
|
|||
/// </summary>
|
|||
public interface IQuantizer |
|||
{ |
|||
/// <summary>
|
|||
/// Gets a value indicating whether to apply dithering to the output image.
|
|||
/// </summary>
|
|||
bool Dither { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the dithering algorithm to apply to the output image.
|
|||
/// </summary>
|
|||
IErrorDiffuser DitherType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Creates the generic frame quantizer
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
/// <returns>The <see cref="IFrameQuantizer{TPixel}"/></returns>
|
|||
IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() |
|||
where TPixel : struct, IPixel<TPixel>; |
|||
} |
|||
} |
|||
@ -1,42 +0,0 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Provides methods for for allowing quantization of images pixels with configurable dithering.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
public interface IQuantizer<TPixel> : IQuantizer |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <summary>
|
|||
/// Quantize an image and return the resulting output pixels.
|
|||
/// </summary>
|
|||
/// <param name="image">The image to quantize.</param>
|
|||
/// <param name="maxColors">The maximum number of colors to return.</param>
|
|||
/// <returns>
|
|||
/// A <see cref="T:QuantizedImage"/> representing a quantized version of the image pixels.
|
|||
/// </returns>
|
|||
QuantizedFrame<TPixel> Quantize(ImageFrame<TPixel> image, int maxColors); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Provides methods for allowing quantization of images pixels with configurable dithering.
|
|||
/// </summary>
|
|||
public interface IQuantizer |
|||
{ |
|||
/// <summary>
|
|||
/// Gets or sets a value indicating whether to apply dithering to the output image.
|
|||
/// </summary>
|
|||
bool Dither { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the dithering algorithm to apply to the output image.
|
|||
/// </summary>
|
|||
IErrorDiffuser DitherType { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
using SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Allows the quantization of images pixels using Octrees.
|
|||
/// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx"/>
|
|||
/// </summary>
|
|||
public class OctreeQuantizer : IQuantizer |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
|
|||
/// </summary>
|
|||
public OctreeQuantizer() |
|||
: this(255) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
public OctreeQuantizer(bool dither) |
|||
: this(dither, DiffuseMode.FloydSteinberg, 255) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
|
|||
public OctreeQuantizer(int maxColors) |
|||
: this(true, DiffuseMode.FloydSteinberg, maxColors) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
|
|||
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
|
|||
public OctreeQuantizer(bool dither, IErrorDiffuser ditherType, int maxColors) |
|||
{ |
|||
Guard.NotNull(ditherType, nameof(ditherType)); |
|||
Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors)); |
|||
|
|||
this.Dither = dither; |
|||
this.DitherType = ditherType; |
|||
this.MaxColors = maxColors; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public bool Dither { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IErrorDiffuser DitherType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the maximum number of colors to hold in the color palette.
|
|||
/// </summary>
|
|||
public int MaxColors { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new OctreeFrameQuantizer<TPixel>(this); |
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
using SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Allows the quantization of images pixels using web safe colors defined in the CSS Color Module Level 4.
|
|||
/// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx"/>
|
|||
/// Override this class to provide your own palette.
|
|||
/// </summary>
|
|||
public class PaletteQuantizer : IQuantizer |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
|
|||
/// </summary>
|
|||
public PaletteQuantizer() |
|||
: this(true, DiffuseMode.FloydSteinberg) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
public PaletteQuantizer(bool dither) |
|||
: this(dither, DiffuseMode.FloydSteinberg) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
|
|||
public PaletteQuantizer(bool dither, IErrorDiffuser ditherType) |
|||
{ |
|||
Guard.NotNull(ditherType, nameof(ditherType)); |
|||
|
|||
this.Dither = dither; |
|||
this.DitherType = ditherType; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public bool Dither { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IErrorDiffuser DitherType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the palette to use to quantize the image.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
/// <returns>The <see cref="T:TPixel[]"/></returns>
|
|||
public virtual TPixel[] GetPalette<TPixel>() |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> NamedColors<TPixel>.WebSafePalette; |
|||
|
|||
/// <inheritdoc />
|
|||
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new PaletteFrameQuantizer<TPixel>(this); |
|||
} |
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing.Dithering; |
|||
using SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion; |
|||
using SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers; |
|||
|
|||
namespace SixLabors.ImageSharp.Processing.Quantization |
|||
{ |
|||
/// <summary>
|
|||
/// Allows the quantization of images pixels using Xiaolin Wu's Color Quantizer.
|
|||
/// <see href="http://www.ece.mcmaster.ca/~xwu/cq.c"/>
|
|||
/// </summary>
|
|||
public class WuQuantizer : IQuantizer |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
|
|||
/// </summary>
|
|||
public WuQuantizer() |
|||
: this(255) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
public WuQuantizer(bool dither) |
|||
: this(dither, DiffuseMode.FloydSteinberg, 255) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
|
|||
public WuQuantizer(int maxColors) |
|||
: this(true, DiffuseMode.FloydSteinberg, maxColors) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
|
|||
/// </summary>
|
|||
/// <param name="dither">Whether to apply dithering to the output image</param>
|
|||
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
|
|||
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
|
|||
public WuQuantizer(bool dither, IErrorDiffuser ditherType, int maxColors) |
|||
{ |
|||
Guard.NotNull(ditherType, nameof(ditherType)); |
|||
Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors)); |
|||
|
|||
this.Dither = dither; |
|||
this.DitherType = ditherType; |
|||
this.MaxColors = maxColors; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public bool Dither { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IErrorDiffuser DitherType { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the maximum number of colors to hold in the color palette.
|
|||
/// </summary>
|
|||
public int MaxColors { get; } |
|||
|
|||
/// <inheritdoc />
|
|||
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() |
|||
where TPixel : struct, IPixel<TPixel> |
|||
=> new WuFrameQuantizer<TPixel>(this); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue