From 6bac83838baf0f577da3920944582185a65610c4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 23 Feb 2020 17:22:48 +1100 Subject: [PATCH] Simplify Resamplers --- src/ImageSharp/Common/Helpers/ImageMaths.cs | 34 ---- src/ImageSharp/Processing/KnownResamplers.cs | 22 +-- .../Resamplers/CatmullRomResampler.cs | 77 --------- .../Transforms/Resamplers/CubicResampler.cs | 153 ++++++++++++++++++ .../Transforms/Resamplers/HermiteResampler.cs | 76 --------- .../Resamplers/Lanczos2Resampler.cs | 83 ---------- .../Resamplers/Lanczos3Resampler.cs | 83 ---------- .../Resamplers/Lanczos8Resampler.cs | 83 ---------- ...nczos5Resampler.cs => LanczosResampler.cs} | 38 ++++- .../Resamplers/MitchellNetravaliResampler.cs | 74 --------- .../Resamplers/RobidouxResampler.cs | 75 --------- .../Resamplers/RobidouxSharpResampler.cs | 75 --------- .../Transforms/Resamplers/SplineResampler.cs | 75 --------- 13 files changed, 196 insertions(+), 752 deletions(-) delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/CatmullRomResampler.cs create mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/CubicResampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/HermiteResampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos2Resampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos3Resampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos8Resampler.cs rename src/ImageSharp/Processing/Processors/Transforms/Resamplers/{Lanczos5Resampler.cs => LanczosResampler.cs} (66%) delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/MitchellNetravaliResampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxResampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxSharpResampler.cs delete mode 100644 src/ImageSharp/Processing/Processors/Transforms/Resamplers/SplineResampler.cs diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index e7b14be420..a0ce30212f 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -242,40 +242,6 @@ namespace SixLabors.ImageSharp return 1F; } - /// - /// Returns the result of a B-C filter against the given value. - /// - /// - /// The value to process. - /// The B-Spline curve variable. - /// The Cardinal curve variable. - /// - /// The . - /// - [MethodImpl(InliningOptions.ShortMethod)] - public static float GetBcValue(float x, float b, float c) - { - if (x < 0F) - { - x = -x; - } - - float temp = x * x; - if (x < 1F) - { - x = ((12 - (9 * b) - (6 * c)) * (x * temp)) + ((-18 + (12 * b) + (6 * c)) * temp) + (6 - (2 * b)); - return x / 6F; - } - - if (x < 2F) - { - x = ((-b - (6 * c)) * (x * temp)) + (((6 * b) + (30 * c)) * temp) + (((-12 * b) - (48 * c)) * x) + ((8 * b) + (24 * c)); - return x / 6F; - } - - return 0F; - } - /// /// Gets the bounding from the given points. /// diff --git a/src/ImageSharp/Processing/KnownResamplers.cs b/src/ImageSharp/Processing/KnownResamplers.cs index 6c73513c87..348c084071 100644 --- a/src/ImageSharp/Processing/KnownResamplers.cs +++ b/src/ImageSharp/Processing/KnownResamplers.cs @@ -24,43 +24,43 @@ namespace SixLabors.ImageSharp.Processing /// /// Gets the Catmull-Rom sampler, a well known standard Cubic Filter often used as a interpolation function /// - public static IResampler CatmullRom { get; } = default(CatmullRomResampler); + public static IResampler CatmullRom { get; } = CubicResampler.CatmullRom; /// /// Gets the Hermite sampler. A type of smoothed triangular interpolation filter that rounds off strong edges while /// preserving flat 'color levels' in the original image. /// - public static IResampler Hermite { get; } = default(HermiteResampler); + public static IResampler Hermite { get; } = CubicResampler.Hermite; /// /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 2 pixels. /// This algorithm provides sharpened results when compared to others when downsampling. /// - public static IResampler Lanczos2 { get; } = default(Lanczos2Resampler); + public static IResampler Lanczos2 { get; } = LanczosResampler.Lanczos2; /// /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 3 pixels /// This algorithm provides sharpened results when compared to others when downsampling. /// - public static IResampler Lanczos3 { get; } = default(Lanczos3Resampler); + public static IResampler Lanczos3 { get; } = LanczosResampler.Lanczos3; /// /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 5 pixels /// This algorithm provides sharpened results when compared to others when downsampling. /// - public static IResampler Lanczos5 { get; } = default(Lanczos5Resampler); + public static IResampler Lanczos5 { get; } = LanczosResampler.Lanczos5; /// /// Gets the Lanczos kernel sampler that implements smooth interpolation with a radius of 8 pixels /// This algorithm provides sharpened results when compared to others when downsampling. /// - public static IResampler Lanczos8 { get; } = default(Lanczos8Resampler); + public static IResampler Lanczos8 { get; } = LanczosResampler.Lanczos8; /// /// Gets the Mitchell-Netravali sampler. This seperable cubic algorithm yields a very good equilibrium between /// detail preservation (sharpness) and smoothness. /// - public static IResampler MitchellNetravali { get; } = default(MitchellNetravaliResampler); + public static IResampler MitchellNetravali { get; } = CubicResampler.MitchellNetravali; /// /// Gets the Nearest-Neighbour sampler that implements the nearest neighbor algorithm. This uses a very fast, unscaled filter @@ -72,17 +72,17 @@ namespace SixLabors.ImageSharp.Processing /// Gets the Robidoux sampler. This algorithm developed by Nicolas Robidoux providing a very good equilibrium between /// detail preservation (sharpness) and smoothness comparable to . /// - public static IResampler Robidoux { get; } = default(RobidouxResampler); + public static IResampler Robidoux { get; } = CubicResampler.Robidoux; /// /// Gets the Robidoux Sharp sampler. A sharpened form of the sampler /// - public static IResampler RobidouxSharp { get; } = default(RobidouxSharpResampler); + public static IResampler RobidouxSharp { get; } = CubicResampler.RobidouxSharp; /// - /// Gets the Spline sampler. A seperable cubic algorithm similar to but yielding smoother results. + /// Gets the Spline sampler. A separable cubic algorithm similar to but yielding smoother results. /// - public static IResampler Spline { get; } = default(SplineResampler); + public static IResampler Spline { get; } = CubicResampler.Spline; /// /// Gets the Triangle sampler, otherwise known as Bilinear. This interpolation algorithm can be used where perfect image transformation diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CatmullRomResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CatmullRomResampler.cs deleted file mode 100644 index f62b7d989c..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CatmullRomResampler.cs +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The Catmull-Rom filter is a well known standard Cubic Filter often used as a interpolation function. - /// This filter produces a reasonably sharp edge, but without a the pronounced gradient change on large - /// scale image enlargements that a 'Lagrange' filter can produce. - /// - /// - public readonly struct CatmullRomResampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - const float B = 0; - const float C = 0.5F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CubicResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CubicResampler.cs new file mode 100644 index 0000000000..a8f3f0b63a --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/CubicResampler.cs @@ -0,0 +1,153 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Numerics; +using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors.Transforms +{ + /// + /// Cubic filters contain a collection of different filters of varying B-Spline and + /// Cardinal values. With these two values you can generate any smoothly fitting + /// (continuious first derivative) piece-wise cubic filter. + /// + /// + /// + public readonly struct CubicResampler : IResampler + { + private readonly float bspline; + private readonly float cardinal; + + /// + /// The Catmull-Rom filter is a well known standard Cubic Filter often used as a interpolation function. + /// This filter produces a reasonably sharp edge, but without a the pronounced gradient change on large + /// scale image enlargements that a 'Lagrange' filter can produce. + /// + public static CubicResampler CatmullRom = new CubicResampler(2, 0, .5F); + + /// + /// The Hermite filter is type of smoothed triangular interpolation Filter, + /// This filter rounds off strong edges while preserving flat 'color levels' in the original image. + /// + public static CubicResampler Hermite = new CubicResampler(2, 0, 0); + + /// + /// The function implements the Mitchell-Netravali algorithm as described on + /// Wikipedia + /// + public static CubicResampler MitchellNetravali = new CubicResampler(2, .3333333F, .3333333F); + + /// + /// The function implements the Robidoux algorithm. + /// + /// + public static CubicResampler Robidoux = new CubicResampler(2, .37821575509399867F, .31089212245300067F); + + /// + /// The function implements the Robidoux Sharp algorithm. + /// + /// + public static CubicResampler RobidouxSharp = new CubicResampler(2, .2620145123990142F, .3689927438004929F); + + /// + /// The function implements the spline algorithm. + /// + /// + /// + /// The function implements the Robidoux Sharp algorithm. + /// + /// + public static CubicResampler Spline = new CubicResampler(2, 1, 0); + + /// + /// Initializes a new instance of the struct. + /// + /// The sampling radius. + /// The B-Spline value. + /// The Cardinal cubic value. + public CubicResampler(float radius, float bspline, float cardinal) + { + this.Radius = radius; + this.bspline = bspline; + this.cardinal = cardinal; + } + + /// + public float Radius { get; } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public float GetValue(float x) + { + float b = this.bspline; + float c = this.cardinal; + + if (x < 0F) + { + x = -x; + } + + float temp = x * x; + if (x < 1F) + { + x = ((12 - (9 * b) - (6 * c)) * (x * temp)) + ((-18 + (12 * b) + (6 * c)) * temp) + (6 - (2 * b)); + return x / 6F; + } + + if (x < 2F) + { + x = ((-b - (6 * c)) * (x * temp)) + (((6 * b) + (30 * c)) * temp) + (((-12 * b) - (48 * c)) * x) + ((8 * b) + (24 * c)); + return x / 6F; + } + + return 0F; + } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public void ApplyResizeTransform( + Configuration configuration, + Image source, + Image destination, + Rectangle sourceRectangle, + Rectangle destinationRectangle, + bool compand) + where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( + configuration, + in this, + source, + destination, + sourceRectangle, + destinationRectangle, + compand); + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public void ApplyAffineTransform( + Configuration configuration, + ImageFrame source, + ImageFrame destination, + Matrix3x2 matrix) + where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( + configuration, + in this, + source, + destination, + matrix); + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public void ApplyProjectiveTransform( + Configuration configuration, + ImageFrame source, + ImageFrame destination, + Matrix4x4 matrix) + where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( + configuration, + in this, + source, + destination, + matrix); + } +} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/HermiteResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/HermiteResampler.cs deleted file mode 100644 index 883d4b684a..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/HermiteResampler.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The Hermite filter is type of smoothed triangular interpolation Filter, - /// This filter rounds off strong edges while preserving flat 'color levels' in the original image. - /// - /// - public readonly struct HermiteResampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - const float B = 0F; - const float C = 0F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos2Resampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos2Resampler.cs deleted file mode 100644 index 93174a23a1..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos2Resampler.cs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the Lanczos kernel algorithm as described on - /// Wikipedia - /// with a radius of 2 pixels. - /// - public readonly struct Lanczos2Resampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - if (x < 0F) - { - x = -x; - } - - if (x < 2F) - { - return ImageMaths.SinC(x) * ImageMaths.SinC(x / 2F); - } - - return 0F; - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos3Resampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos3Resampler.cs deleted file mode 100644 index 6c008be630..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos3Resampler.cs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the Lanczos kernel algorithm as described on - /// Wikipedia - /// with a radius of 3 pixels. - /// - public readonly struct Lanczos3Resampler : IResampler - { - /// - public float Radius => 3; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - if (x < 0F) - { - x = -x; - } - - if (x < 3F) - { - return ImageMaths.SinC(x) * ImageMaths.SinC(x / 3F); - } - - return 0F; - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos8Resampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos8Resampler.cs deleted file mode 100644 index d24c7a1f08..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos8Resampler.cs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the Lanczos kernel algorithm as described on - /// Wikipedia - /// with a radius of 8 pixels. - /// - public readonly struct Lanczos8Resampler : IResampler - { - /// - public float Radius => 8; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - if (x < 0F) - { - x = -x; - } - - if (x < 8F) - { - return ImageMaths.SinC(x) * ImageMaths.SinC(x / 8F); - } - - return 0F; - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos5Resampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/LanczosResampler.cs similarity index 66% rename from src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos5Resampler.cs rename to src/ImageSharp/Processing/Processors/Transforms/Resamplers/LanczosResampler.cs index 56da01fb27..4ed2d541c2 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/Lanczos5Resampler.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/LanczosResampler.cs @@ -9,13 +9,38 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms { /// /// The function implements the Lanczos kernel algorithm as described on - /// Wikipedia - /// with a radius of 5 pixels. + /// Wikipedia. /// - public readonly struct Lanczos5Resampler : IResampler + public readonly struct LanczosResampler : IResampler { + /// + /// Implements the Lanczos kernel algorithm with a radius of 2. + /// + public static LanczosResampler Lanczos2 = new LanczosResampler(2); + + /// + /// Implements the Lanczos kernel algorithm with a radius of 3. + /// + public static LanczosResampler Lanczos3 = new LanczosResampler(3); + + /// + /// Implements the Lanczos kernel algorithm with a radius of 5. + /// + public static LanczosResampler Lanczos5 = new LanczosResampler(5); + + /// + /// Implements the Lanczos kernel algorithm with a radius of 8. + /// + public static LanczosResampler Lanczos8 = new LanczosResampler(8); + + /// + /// Initializes a new instance of the struct. + /// + /// The sampling radius. + public LanczosResampler(float radius) => this.Radius = radius; + /// - public float Radius => 5; + public float Radius { get; } /// [MethodImpl(InliningOptions.ShortMethod)] @@ -26,9 +51,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms x = -x; } - if (x < 5F) + float radius = this.Radius; + if (x < radius) { - return ImageMaths.SinC(x) * ImageMaths.SinC(x / 5F); + return ImageMaths.SinC(x) * ImageMaths.SinC(x / radius); } return 0F; diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/MitchellNetravaliResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/MitchellNetravaliResampler.cs deleted file mode 100644 index e67e50ab0b..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/MitchellNetravaliResampler.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the mitchell algorithm as described on - /// Wikipedia - /// - public readonly struct MitchellNetravaliResampler : IResampler - { - /// - public float Radius => 2; - - /// - public float GetValue(float x) - { - const float B = 0.3333333F; - const float C = 0.3333333F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxResampler.cs deleted file mode 100644 index 1b4d84e5a0..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxResampler.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the Robidoux algorithm. - /// - /// - public readonly struct RobidouxResampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - const float B = 0.37821575509399867F; - const float C = 0.31089212245300067F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxSharpResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxSharpResampler.cs deleted file mode 100644 index 52991a6493..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/RobidouxSharpResampler.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the Robidoux Sharp algorithm. - /// - /// - public readonly struct RobidouxSharpResampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - const float B = 0.2620145123990142F; - const float C = 0.3689927438004929F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/SplineResampler.cs b/src/ImageSharp/Processing/Processors/Transforms/Resamplers/SplineResampler.cs deleted file mode 100644 index a21ed495bc..0000000000 --- a/src/ImageSharp/Processing/Processors/Transforms/Resamplers/SplineResampler.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Transforms -{ - /// - /// The function implements the spline algorithm. - /// - /// - public readonly struct SplineResampler : IResampler - { - /// - public float Radius => 2; - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public float GetValue(float x) - { - const float B = 1F; - const float C = 0F; - - return ImageMaths.GetBcValue(x, B, C); - } - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyResizeTransform( - Configuration configuration, - Image source, - Image destination, - Rectangle sourceRectangle, - Rectangle destinationRectangle, - bool compand) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyResizeTransform( - configuration, - in this, - source, - destination, - sourceRectangle, - destinationRectangle, - compand); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyAffineTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix3x2 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyAffineTransform( - configuration, - in this, - source, - destination, - matrix); - - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void ApplyProjectiveTransform( - Configuration configuration, - ImageFrame source, - ImageFrame destination, - Matrix4x4 matrix) - where TPixel : struct, IPixel => ResamplerExtensions.ApplyProjectiveTransform( - configuration, - in this, - source, - destination, - matrix); - } -}