From e1a2a9b2c676da18e63bbc0f26c37bfb30a79fd1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 20 Oct 2015 17:53:13 +1100 Subject: [PATCH] Add new test image for rewrite Former-commit-id: 9ddf97eb0300834d945db627e4c500e67fbf1fb7 Former-commit-id: c9a893530a085724596af1054492d125dc0355ea Former-commit-id: 3266f3ce095fd3f865d0e540e6096f794de70c4f --- src/ImageProcessor/ImageProcessor.csproj | 1 + .../Samplers/Resamplers/Lanczos8Resampler.cs | 33 +++++++++++++++++++ src/ImageProcessor/Samplers/Resize.cs | 8 ++--- .../Processors/ProcessorTestBase.cs | 3 +- .../Processors/Samplers/SamplerTests.cs | 3 +- .../TestImages/Formats/Gif/rings.gif | 3 ++ 6 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/ImageProcessor/Samplers/Resamplers/Lanczos8Resampler.cs create mode 100644 tests/ImageProcessor.Tests/TestImages/Formats/Gif/rings.gif diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index bc3b47826..2cdf7b750 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -181,6 +181,7 @@ + diff --git a/src/ImageProcessor/Samplers/Resamplers/Lanczos8Resampler.cs b/src/ImageProcessor/Samplers/Resamplers/Lanczos8Resampler.cs new file mode 100644 index 000000000..ac9065db4 --- /dev/null +++ b/src/ImageProcessor/Samplers/Resamplers/Lanczos8Resampler.cs @@ -0,0 +1,33 @@ +// +// Copyright © James South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessor.Samplers +{ + /// + /// The function implements the Lanczos kernel algorithm as described on + /// Wikipedia + /// + public class Lanczos8Resampler : IResampler + { + /// + public double Radius => 8; + + /// + public double GetValue(double x) + { + if (x < 0) + { + x = -x; + } + + if (x < 8) + { + return ImageMaths.SinC(x) * ImageMaths.SinC(x / 8f); + } + + return 0; + } + } +} diff --git a/src/ImageProcessor/Samplers/Resize.cs b/src/ImageProcessor/Samplers/Resize.cs index 811294eb5..ce146186b 100644 --- a/src/ImageProcessor/Samplers/Resize.cs +++ b/src/ImageProcessor/Samplers/Resize.cs @@ -48,7 +48,7 @@ namespace ImageProcessor.Samplers int startX = targetRectangle.X; int endX = targetRectangle.Right; int right = (int)(this.Sampler.Radius + .5); - int left = -right; + int left = (-right) + 1; // Scaling factors double widthFactor = sourceWidth / (double)targetRectangle.Width; @@ -86,7 +86,7 @@ namespace ImageProcessor.Samplers for (int yy = left; yy <= right; yy++) { // Get Y cooefficient - double kernel1 = this.Sampler.GetValue(dy - yy); + double kernel1 = this.Sampler.GetValue(yy - dy); if (Math.Abs(kernel1) < Epsilon) { @@ -126,7 +126,7 @@ namespace ImageProcessor.Samplers } Bgra sourceColor = source[originX2, originY2]; - sourceColor = PixelOperations.ToLinear(sourceColor); + // sourceColor = PixelOperations.ToLinear(sourceColor); r += kernel2 * sourceColor.R; g += kernel2 * sourceColor.G; @@ -136,7 +136,7 @@ namespace ImageProcessor.Samplers } Bgra destinationColor = new Bgra(b.ToByte(), g.ToByte(), r.ToByte(), a.ToByte()); - destinationColor = PixelOperations.ToSrgb(destinationColor); + // destinationColor = PixelOperations.ToSrgb(destinationColor); target[x, y] = destinationColor; } } diff --git a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs index 252425edf..1804557fa 100644 --- a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs +++ b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs @@ -19,11 +19,12 @@ namespace ImageProcessor.Tests /// public static readonly List Files = new List { - //"../../TestImages/Formats/Jpg/Backdrop.jpg", + "../../TestImages/Formats/Jpg/Backdrop.jpg", "../../TestImages/Formats/Jpg/Calliphora.jpg", //"../../TestImages/Formats/Bmp/Car.bmp", //"../../TestImages/Formats/Png/cmyk.png", //"../../TestImages/Formats/Gif/leaf.gif" + "../../TestImages/Formats/Gif/rings.gif" // { "../../TestImages/Formats/Gif/ani.gif" }, // { "../../TestImages/Formats/Gif/ani2.gif" }, diff --git a/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs b/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs index 0cb6117b3..9b15f8013 100644 --- a/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs +++ b/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs @@ -17,6 +17,7 @@ namespace ImageProcessor.Tests { "Bilinear", new TriangleResampler() }, { "NearestNeighbour", new BoxResampler() }, { "Lanczos3", new Lanczos3Resampler() }, + { "Lanczos8", new Lanczos8Resampler() }, { "MitchellNetravali", new MitchellNetravaliResampler() }, { "Hermite", new HermiteResampler() }, { "Spline", new SplineResampler() }, @@ -44,7 +45,7 @@ namespace ImageProcessor.Tests string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); using (FileStream output = File.OpenWrite($"Resized/{filename}")) { - image.Resize(200, 298, sampler).Save(output); + image.Resize(100, 100, sampler).Save(output); } Trace.WriteLine($"{name}: {watch.ElapsedMilliseconds}ms"); diff --git a/tests/ImageProcessor.Tests/TestImages/Formats/Gif/rings.gif b/tests/ImageProcessor.Tests/TestImages/Formats/Gif/rings.gif new file mode 100644 index 000000000..acd5d6339 --- /dev/null +++ b/tests/ImageProcessor.Tests/TestImages/Formats/Gif/rings.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:716448da88152225767c024aac498f5b7562b6e8391907cefc9d03dba40050fd +size 53435