Browse Source

Fix ResizeProcessor Compand UnPremultiply bug + tests

af/merge-core
Anton Firszov 8 years ago
parent
commit
86b2bfe100
  1. 3
      src/ImageSharp/Common/Helpers/ImageMaths.cs
  2. 2
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  3. 64
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  4. 2
      tests/Images/External

3
src/ImageSharp/Common/Helpers/ImageMaths.cs

@ -2,9 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives; using SixLabors.Primitives;
@ -33,6 +31,7 @@ namespace SixLabors.ImageSharp
/// <summary> /// <summary>
/// Determine the Least Common Multiple (LCM) of two numbers. /// Determine the Least Common Multiple (LCM) of two numbers.
/// TODO: This method might be useful for building a more compact <see cref="Processing.Processors.Transforms.KernelMap"/>
/// </summary> /// </summary>
public static int LeastCommonMultiple(int a, int b) public static int LeastCommonMultiple(int a, int b)
{ {

2
src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

@ -263,7 +263,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
ResizeKernel window = this.horizontalKernelMap.Kernels[x - startX]; ResizeKernel window = this.horizontalKernelMap.Kernels[x - startX];
Unsafe.Add(ref firstPassBaseRef, x * sourceHeight) = Unsafe.Add(ref firstPassBaseRef, x * sourceHeight) =
window.ConvolveExpand(tempRowSpan, sourceX).UnPremultiply(); window.ConvolveExpand(tempRowSpan, sourceX);
} }
} }
else else

64
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -18,8 +18,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{ {
public static readonly string[] CommonTestImages = { TestImages.Png.CalliphoraPartial }; public static readonly string[] CommonTestImages = { TestImages.Png.CalliphoraPartial };
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.069F); private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.07F);
public static readonly TheoryData<string, IResampler> AllReSamplers = public static readonly TheoryData<string, IResampler> AllReSamplers =
new TheoryData<string, IResampler> new TheoryData<string, IResampler>
{ {
@ -65,20 +65,16 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void Resize_WorksWithAllParallelismLevels<TPixel>(TestImageProvider<TPixel> provider, int maxDegreeOfParallelism) public void Resize_WorksWithAllParallelismLevels<TPixel>(TestImageProvider<TPixel> provider, int maxDegreeOfParallelism)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (maxDegreeOfParallelism >= 0) provider.Configuration.MaxDegreeOfParallelism =
{ maxDegreeOfParallelism > 0 ? maxDegreeOfParallelism : Environment.ProcessorCount;
provider.Configuration.MaxDegreeOfParallelism = maxDegreeOfParallelism;
}
using (Image<TPixel> image = provider.GetImage()) FormattableString details = $"MDP{maxDegreeOfParallelism}";
{
SizeF newSize = image.Size() * 0.5f;
image.Mutate(x => x.Resize((Size)newSize, false));
FormattableString details = $"MDP{maxDegreeOfParallelism}";
image.DebugSave(provider, details); provider.RunValidatingProcessorTest(
//image.CompareToReferenceOutput(ImageComparer.TolerantPercentage(0.005f), provider, details); x => x.Resize(x.GetCurrentSize() / 2),
} details,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
} }
[Theory] [Theory]
@ -100,16 +96,9 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
public void Resize_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void Resize_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) provider.RunValidatingProcessorTest(x => x.Resize(x.GetCurrentSize() / 2), comparer: ValidatorComparer);
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, true));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
} }
[Theory] [Theory]
[WithFileCollection(nameof(CommonTestImages), DefaultPixelType)] [WithFileCollection(nameof(CommonTestImages), DefaultPixelType)]
public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider) public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
@ -130,32 +119,21 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType)] [WithFile(TestImages.Png.Kaboom, DefaultPixelType, false)]
public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider) [WithFile(TestImages.Png.Kaboom, DefaultPixelType, true)]
public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider, bool compand)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) string details = compand ? "Compand" : "";
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2));
image.DebugSave(provider);
image.CompareToReferenceOutput(ValidatorComparer, provider);
}
}
[Theory] provider.RunValidatingProcessorTest(
[WithFile(TestImages.Png.Kaboom, DefaultPixelType)] x => x.Resize(x.GetCurrentSize() / 2, compand),
public void Resize_Compand_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider) details,
where TPixel : struct, IPixel<TPixel> appendPixelTypeToFileName: false,
{ appendSourceFileOrDescription: false);
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, true));
image.DebugSave(provider);
}
} }
[Theory] [Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)] [WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider) public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)

2
tests/Images/External

@ -1 +1 @@
Subproject commit c0627f384c1d3d2f8d914c9578ae31354c35fd2c Subproject commit 03c7fa7582dea75cea0d49514ccb7e1b6dc9e780
Loading…
Cancel
Save