From 250fc599e16d2258e2272ef51a8e4bfdaa681e0d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 30 Jul 2016 19:37:39 +1000 Subject: [PATCH] Add Lanczos2 resampler Former-commit-id: 7190b01b85ed5933e0f5dd276b3eb718a8d78e30 Former-commit-id: 56df11ca6ffb6d0075115dc3b2c714317a379ab8 Former-commit-id: becd320711a19b21267790300429105e071fc5de --- README.md | 33 ++++-------------- .../Samplers/Resamplers/Lanczos2Resampler.cs | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 src/ImageProcessorCore/Samplers/Resamplers/Lanczos2Resampler.cs diff --git a/README.md b/README.md index 85edc99565..65168b6bde 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # ImageProcessorCore @@ -74,6 +75,7 @@ git clone https://github.com/JimBobSquarePants/ImageProcessor - Resampling algorithms. (Optional gamma correction, resize modes, Performance improvements?) - [x] Box - [x] Bicubic + - [x] Lanczos2 - [x] Lanczos3 - [x] Lanczos5 - [x] Lanczos8 @@ -81,7 +83,6 @@ git clone https://github.com/JimBobSquarePants/ImageProcessor - [x] Nearest Neighbour - [x] Robidoux - [x] Robidoux Sharp - - [x] Robidoux Soft - [x] Spline - [x] Triangle - [x] Welch @@ -100,8 +101,8 @@ git clone https://github.com/JimBobSquarePants/ImageProcessor - [x] Skew by x/y angles and center point. - ColorMatrix operations (Uses Matrix4x4) - [x] BlackWhite - - [x] Greyscale BT709 - - [x] Greyscale BT601 + - [x] Grayscale BT709 + - [x] Grayscale BT601 - [x] Hue - [x] Saturation - [x] Lomograph @@ -160,7 +161,7 @@ With this version the API will change dramatically. Without the constraints of ` Image methods are also fluent which allow chaining much like the `ImageFactory` class in the Framework version. -Here's an example of the code required to resize an image using the default Bicubic resampler then turn the colors into their greyscale equivalent using the BT709 standard matrix. +Here's an example of the code required to resize an image using the default Bicubic resampler then turn the colors into their grayscale equivalent using the BT709 standard matrix. ```csharp using (FileStream stream = File.OpenRead("foo.jpg")) @@ -168,32 +169,12 @@ using (FileStream output = File.OpenWrite("bar.jpg")) { Image image = new Image(stream); image.Resize(image.Width / 2, image.Height / 2) - .Greyscale() + .Grayscale() .Save(output); } ``` -It will also be possible to pass collections of processors as params to manipulate images. For example here I am applying a Gaussian blur with a sigma of 5 to an image, then detecting the edges using a Sobel operator working in greyscale mode. - -```csharp -using (FileStream stream = File.OpenRead("foo.jpg")) -using (FileStream output = File.OpenWrite("bar.jpg")) -{ - Image image = new Image(stream); - List processors = new List() - { - new GuassianBlur(5), - new Sobel { Greyscale = true } - }; - - foreach (IImageProcessor processor in processors){ - - image.Process(processor) - .Save(output); - } -} -``` -Individual processors can be initialised and apply processing against images. This allows nesting which will allow the powerful combination of processing methods: +Individual processors can be initialised and apply processing against images. This allows nesting which brings the potential for powerful combinations of processing methods: ```csharp new Brightness(50).Apply(sourceImage, targetImage, sourceImage.Bounds); diff --git a/src/ImageProcessorCore/Samplers/Resamplers/Lanczos2Resampler.cs b/src/ImageProcessorCore/Samplers/Resamplers/Lanczos2Resampler.cs new file mode 100644 index 0000000000..ced92365b8 --- /dev/null +++ b/src/ImageProcessorCore/Samplers/Resamplers/Lanczos2Resampler.cs @@ -0,0 +1,34 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore +{ + /// + /// The function implements the Lanczos kernel algorithm as described on + /// Wikipedia + /// with a radius of 2 pixels. + /// + public class Lanczos2Resampler : IResampler + { + /// + public float Radius => 2; + + /// + public float GetValue(float x) + { + if (x < 0F) + { + x = -x; + } + + if (x < 2F) + { + return ImageMaths.SinC(x) * ImageMaths.SinC(x / 2F); + } + + return 0F; + } + } +}