From 9cf343bfef68d12e64877ac1f683005cd3f5bec8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 16 Feb 2020 22:08:47 +1100 Subject: [PATCH] Add ditherscale to palette API --- .../Extensions/Dithering/DitherExtensions.cs | 81 ++++++++++++++++++- .../Dithering/PaletteDitherProcessor.cs | 29 ++++++- 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs b/src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs index ebd2ea613..abdfb969c 100644 --- a/src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Dithering/DitherExtensions.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Processing public static class DitherExtensions { /// - /// 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 . /// /// The image this method extends. /// The to allow chaining of operations. @@ -26,9 +26,24 @@ namespace SixLabors.ImageSharp.Processing /// The image this method extends. /// The ordered ditherer. /// The to allow chaining of operations. - public static IImageProcessingContext Dither(this IImageProcessingContext source, IDither dither) => + public static IImageProcessingContext Dither( + this IImageProcessingContext source, + IDither dither) => source.ApplyProcessor(new PaletteDitherProcessor(dither)); + /// + /// Dithers the image reducing it to a web-safe palette using ordered dithering. + /// + /// The image this method extends. + /// The ordered ditherer. + /// The dithering scale used to adjust the amount of dither. + /// The to allow chaining of operations. + public static IImageProcessingContext Dither( + this IImageProcessingContext source, + IDither dither, + float ditherScale) => + source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale)); + /// /// Dithers the image reducing it to the given palette using ordered dithering. /// @@ -42,6 +57,32 @@ namespace SixLabors.ImageSharp.Processing ReadOnlyMemory palette) => source.ApplyProcessor(new PaletteDitherProcessor(dither, palette)); + /// + /// Dithers the image reducing it to the given palette using ordered dithering. + /// + /// The image this method extends. + /// The ordered ditherer. + /// The dithering scale used to adjust the amount of dither. + /// The palette to select substitute colors from. + /// The to allow chaining of operations. + public static IImageProcessingContext Dither( + this IImageProcessingContext source, + IDither dither, + float ditherScale, + ReadOnlyMemory palette) => + source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale, palette)); + + /// + /// Dithers the image reducing it to a web-safe palette using . + /// + /// The image this method extends. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The to allow chaining of operations. + public static IImageProcessingContext Dither(this IImageProcessingContext source, Rectangle rectangle) => + Dither(source, KnownDitherings.BayerDither4x4, rectangle); + /// /// Dithers the image reducing it to a web-safe palette using ordered dithering. /// @@ -57,6 +98,23 @@ namespace SixLabors.ImageSharp.Processing Rectangle rectangle) => source.ApplyProcessor(new PaletteDitherProcessor(dither), rectangle); + /// + /// Dithers the image reducing it to a web-safe palette using ordered dithering. + /// + /// The image this method extends. + /// The ordered ditherer. + /// The dithering scale used to adjust the amount of dither. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The to allow chaining of operations. + public static IImageProcessingContext Dither( + this IImageProcessingContext source, + IDither dither, + float ditherScale, + Rectangle rectangle) => + source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale), rectangle); + /// /// Dithers the image reducing it to the given palette using ordered dithering. /// @@ -73,5 +131,24 @@ namespace SixLabors.ImageSharp.Processing ReadOnlyMemory palette, Rectangle rectangle) => source.ApplyProcessor(new PaletteDitherProcessor(dither, palette), rectangle); + + /// + /// Dithers the image reducing it to the given palette using ordered dithering. + /// + /// The image this method extends. + /// The ordered ditherer. + /// The dithering scale used to adjust the amount of dither. + /// The palette to select substitute colors from. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The to allow chaining of operations. + public static IImageProcessingContext Dither( + this IImageProcessingContext source, + IDither dither, + float ditherScale, + ReadOnlyMemory palette, + Rectangle rectangle) => + source.ApplyProcessor(new PaletteDitherProcessor(dither, ditherScale, palette), rectangle); } } diff --git a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor.cs b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor.cs index 40949bb28..6217535c5 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor.cs +++ b/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 /// /// The ordered ditherer. public PaletteDitherProcessor(IDither dither) - : this(dither, Color.WebSafePalette) + : this(dither, QuantizerConstants.MaxDitherScale) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The ordered ditherer. + /// The dithering scale used to adjust the amount of dither. + public PaletteDitherProcessor(IDither dither, float ditherScale) + : this(dither, ditherScale, Color.WebSafePalette) { } @@ -26,8 +37,22 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering /// The dithering algorithm. /// The palette to select substitute colors from. public PaletteDitherProcessor(IDither dither, ReadOnlyMemory palette) + : this(dither, QuantizerConstants.MaxDitherScale, palette) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The dithering algorithm. + /// The dithering scale used to adjust the amount of dither. + /// The palette to select substitute colors from. + public PaletteDitherProcessor(IDither dither, float ditherScale, ReadOnlyMemory 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; }