using System; using SixLabors.ImageSharp.Processing; namespace Spectre.Console { /// /// Contains extension methods for . /// public static class CanvasImageExtensions { /// /// Sets the maximum width of the rendered image. /// /// The canvas image. /// The maximum width. /// The same instance so that multiple calls can be chained. public static CanvasImage MaxWidth(this CanvasImage image, int? maxWidth) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.MaxWidth = maxWidth; return image; } /// /// Disables the maximum width of the rendered image. /// /// The canvas image. /// The same instance so that multiple calls can be chained. public static CanvasImage NoMaxWidth(this CanvasImage image) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.MaxWidth = null; return image; } /// /// Sets the pixel width. /// /// The canvas image. /// The pixel width. /// The same instance so that multiple calls can be chained. public static CanvasImage PixelWidth(this CanvasImage image, int width) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.PixelWidth = width; return image; } /// /// Mutates the underlying image. /// /// The canvas image. /// The action that mutates the underlying image. /// The same instance so that multiple calls can be chained. public static CanvasImage Mutate(this CanvasImage image, Action action) { if (image is null) { throw new ArgumentNullException(nameof(image)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } image.Image.Mutate(action); return image; } /// /// Uses a bicubic sampler that implements the bicubic kernel algorithm W(x). /// /// The canvas image. /// The same instance so that multiple calls can be chained. public static CanvasImage BicubicResampler(this CanvasImage image) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.Resampler = KnownResamplers.Bicubic; return image; } /// /// Uses a bilinear sampler. This interpolation algorithm /// can be used where perfect image transformation with pixel matching is impossible, /// so that one can calculate and assign appropriate intensity values to pixels. /// /// The canvas image. /// The same instance so that multiple calls can be chained. public static CanvasImage BilinearResampler(this CanvasImage image) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.Resampler = KnownResamplers.Triangle; return image; } /// /// Uses a Nearest-Neighbour sampler that implements the nearest neighbor algorithm. /// This uses a very fast, unscaled filter which will select the closest pixel to /// the new pixels position. /// /// The canvas image. /// The same instance so that multiple calls can be chained. public static CanvasImage NearestNeighborResampler(this CanvasImage image) { if (image is null) { throw new ArgumentNullException(nameof(image)); } image.Resampler = KnownResamplers.NearestNeighbor; return image; } } }