Browse Source

Add ditherscale to palette API

af/octree-no-pixelmap
James Jackson-South 6 years ago
parent
commit
9cf343bfef
  1. 81
      src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs
  2. 29
      src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor.cs

81
src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing
public static class DitherExtensions
{
/// <summary>
/// Dithers the image reducing it to a web-safe palette using Bayer4x4 ordered dithering.
/// Dithers the image reducing it to a web-safe palette using <see cref="KnownDitherings.BayerDither4x4"/>.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
@ -26,9 +26,24 @@ namespace SixLabors.ImageSharp.Processing
/// <param name="source">The image this method extends.</param>
/// <param name="dither">The ordered ditherer.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(this IImageProcessingContext source, IDither dither) =>
public static IImageProcessingContext Dither(
this IImageProcessingContext source,
IDither dither) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither));
/// <summary>
/// Dithers the image reducing it to a web-safe palette using ordered dithering.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="dither">The ordered ditherer.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(
this IImageProcessingContext source,
IDither dither,
float ditherScale) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale));
/// <summary>
/// Dithers the image reducing it to the given palette using ordered dithering.
/// </summary>
@ -42,6 +57,32 @@ namespace SixLabors.ImageSharp.Processing
ReadOnlyMemory<Color> palette) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, palette));
/// <summary>
/// Dithers the image reducing it to the given palette using ordered dithering.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="dither">The ordered ditherer.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
/// <param name="palette">The palette to select substitute colors from.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(
this IImageProcessingContext source,
IDither dither,
float ditherScale,
ReadOnlyMemory<Color> palette) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale, palette));
/// <summary>
/// Dithers the image reducing it to a web-safe palette using <see cref="KnownDitherings.BayerDither4x4"/>.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(this IImageProcessingContext source, Rectangle rectangle) =>
Dither(source, KnownDitherings.BayerDither4x4, rectangle);
/// <summary>
/// Dithers the image reducing it to a web-safe palette using ordered dithering.
/// </summary>
@ -57,6 +98,23 @@ namespace SixLabors.ImageSharp.Processing
Rectangle rectangle) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither), rectangle);
/// <summary>
/// Dithers the image reducing it to a web-safe palette using ordered dithering.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="dither">The ordered ditherer.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(
this IImageProcessingContext source,
IDither dither,
float ditherScale,
Rectangle rectangle) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale), rectangle);
/// <summary>
/// Dithers the image reducing it to the given palette using ordered dithering.
/// </summary>
@ -73,5 +131,24 @@ namespace SixLabors.ImageSharp.Processing
ReadOnlyMemory<Color> palette,
Rectangle rectangle) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, palette), rectangle);
/// <summary>
/// Dithers the image reducing it to the given palette using ordered dithering.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="dither">The ordered ditherer.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
/// <param name="palette">The palette to select substitute colors from.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Dither(
this IImageProcessingContext source,
IDither dither,
float ditherScale,
ReadOnlyMemory<Color> palette,
Rectangle rectangle) =>
source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale, palette), rectangle);
}
}

29
src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor.cs

@ -3,6 +3,7 @@
using System;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Processing.Processors.Dithering
{
@ -16,7 +17,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
/// </summary>
/// <param name="dither">The ordered ditherer.</param>
public PaletteDitherProcessor(IDither dither)
: this(dither, Color.WebSafePalette)
: this(dither, QuantizerConstants.MaxDitherScale)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PaletteDitherProcessor"/> class.
/// </summary>
/// <param name="dither">The ordered ditherer.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
public PaletteDitherProcessor(IDither dither, float ditherScale)
: this(dither, ditherScale, Color.WebSafePalette)
{
}
@ -26,8 +37,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
/// <param name="dither">The dithering algorithm.</param>
/// <param name="palette">The palette to select substitute colors from.</param>
public PaletteDitherProcessor(IDither dither, ReadOnlyMemory<Color> palette)
: this(dither, QuantizerConstants.MaxDitherScale, palette)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PaletteDitherProcessor"/> class.
/// </summary>
/// <param name="dither">The dithering algorithm.</param>
/// <param name="ditherScale">The dithering scale used to adjust the amount of dither.</param>
/// <param name="palette">The palette to select substitute colors from.</param>
public PaletteDitherProcessor(IDither dither, float ditherScale, ReadOnlyMemory<Color> palette)
{
this.Dither = dither ?? throw new ArgumentNullException(nameof(dither));
Guard.MustBeGreaterThan(palette.Length, 0, nameof(palette));
Guard.NotNull(dither, nameof(dither));
this.Dither = dither;
this.DitherScale = ditherScale.Clamp(QuantizerConstants.MinDitherScale, QuantizerConstants.MaxDitherScale);
this.Palette = palette;
}

Loading…
Cancel
Save