Browse Source

fixed a bug in KernelMap caused by overlapping memory areas

af/merge-core
Anton Firszov 7 years ago
parent
commit
eed22c56ca
  1. 11
      src/ImageSharp/Processing/Processors/Transforms/KernelMap.cs
  2. 16
      tests/ImageSharp.Tests/Processing/Processors/Transforms/KernelMapTests.cs
  3. 14
      tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs
  4. 13
      tests/ImageSharp.Tests/TestUtilities/TestUtils.cs

11
src/ImageSharp/Processing/Processors/Transforms/KernelMap.cs

@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.periodicRegionMin = period + radius;
this.periodicRegionMax = destinationSize - radius;
int width = radius * 2;
int width = (radius * 2) + 1;
this.data = memoryAllocator.Allocate2D<float>(width, destinationSize, AllocationOptions.Clean);
this.kernels = new ResizeKernel[destinationSize];
}
@ -141,8 +141,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
/// </summary>
private ResizeKernel CreateKernel(int destIdx, int left, int rightIdx)
{
int flatStartIndex = destIdx * this.data.Width;
int length = rightIdx - left + 1;
if (length > this.data.Width)
{
throw new InvalidOperationException($"Error in KernelMap.CreateKernel({destIdx},{left},{rightIdx}): left > this.data.Width");
}
int flatStartIndex = destIdx * this.data.Width;
Memory<float> bufferSlice = this.data.Memory.Slice(flatStartIndex, length);
return new ResizeKernel(left, bufferSlice);
}

16
tests/ImageSharp.Tests/Processing/Processors/Transforms/KernelMapTests.cs

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using SixLabors.ImageSharp.PixelFormats;
@ -40,25 +41,22 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{ nameof(KnownResamplers.Lanczos3), 9, 12 },
{ nameof(KnownResamplers.Lanczos3), 6, 8 },
{ nameof(KnownResamplers.Lanczos3), 8, 6 },
// TODO: What's wrong here:
{ nameof(KnownResamplers.Lanczos3), 20, 12 },
{nameof(KnownResamplers.Lanczos8), 500, 200 },
{nameof(KnownResamplers.Lanczos8), 100, 10 },
{nameof(KnownResamplers.Lanczos8), 100, 80 },
{nameof(KnownResamplers.Lanczos8), 10, 100 },
{ nameof(KnownResamplers.Lanczos8), 500, 200 },
{ nameof(KnownResamplers.Lanczos8), 100, 10 },
{ nameof(KnownResamplers.Lanczos8), 100, 80 },
{ nameof(KnownResamplers.Lanczos8), 10, 100 },
};
[Theory]
[MemberData(nameof(KernelMapData))]
public void KernelMapContentIsCorrect(string resamplerName, int srcSize, int destSize)
{
var resampler = (IResampler)typeof(KnownResamplers).GetProperty(resamplerName).GetValue(null);
var kernelMap = KernelMap.Calculate(resampler, destSize, srcSize, Configuration.Default.MemoryAllocator);
IResampler resampler = TestUtils.GetResampler(resamplerName);
var referenceMap = ReferenceKernelMap.Calculate(resampler, destSize, srcSize);
var kernelMap = KernelMap.Calculate(resampler, destSize, srcSize, Configuration.Default.MemoryAllocator);
#if DEBUG
this.Output.WriteLine($"Actual KernelMap:\n{PrintKernelMap(kernelMap)}\n");

14
tests/ImageSharp.Tests/Processing/Processors/Transforms/SkewTest.cs

@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{
foreach (string resamplerName in ResamplerNames)
{
IResampler sampler = GetResampler(resamplerName);
IResampler sampler = TestUtils.GetResampler(resamplerName);
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(i => i.Skew(x, y, sampler));
@ -68,17 +68,5 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
}
}
}
private static IResampler GetResampler(string name)
{
PropertyInfo property = typeof(KnownResamplers).GetTypeInfo().GetProperty(name);
if (property is null)
{
throw new Exception($"No resampler named '{name}");
}
return (IResampler)property.GetValue(null);
}
}
}

13
tests/ImageSharp.Tests/TestUtilities/TestUtils.cs

@ -10,6 +10,7 @@ using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives;
@ -284,5 +285,17 @@ namespace SixLabors.ImageSharp.Tests
}
public static string AsInvariantString(this FormattableString formattable) => System.FormattableString.Invariant(formattable);
public static IResampler GetResampler(string name)
{
PropertyInfo property = typeof(KnownResamplers).GetTypeInfo().GetProperty(name);
if (property is null)
{
throw new Exception($"No resampler named '{name}");
}
return (IResampler)property.GetValue(null);
}
}
}
Loading…
Cancel
Save