Browse Source

CloningImageProcessor uses SwapOrCopyPixelsBuffersFrom()

af/merge-core
Anton Firszov 8 years ago
parent
commit
dfd59dd25e
  1. 19
      src/ImageSharp/ImageFrame{TPixel}.cs
  2. 4
      src/ImageSharp/Image{TPixel}.cs
  3. 2
      src/ImageSharp/Processing/Processors/CloningImageProcessor.cs
  4. 2
      tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs
  5. 23
      tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs
  6. 22
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  7. 47
      tests/ImageSharp.Tests/TestUtilities/TestUtils.cs
  8. 2
      tests/Images/External

19
src/ImageSharp/ImageFrame{TPixel}.cs

@ -245,30 +245,15 @@ namespace SixLabors.ImageSharp
this.GetPixelSpan().CopyTo(target.GetSpan()); this.GetPixelSpan().CopyTo(target.GetSpan());
} }
/// <summary>
/// Switches the buffers used by the image and the PixelAccessor meaning that the Image will "own" the buffer from the PixelAccessor and the PixelAccessor will now own the Images buffer.
/// </summary>
/// <param name="pixelSource">The pixel source.</param>
internal void SwapPixelsBuffers(PixelAccessor<TPixel> pixelSource)
{
Guard.NotNull(pixelSource, nameof(pixelSource));
// Push my memory into the accessor (which in turn unpins the old buffer ready for the images use)
Buffer2D<TPixel> newPixels = pixelSource.SwapBufferOwnership(this.PixelBuffer);
this.PixelBuffer = newPixels;
}
/// <summary> /// <summary>
/// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer. /// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer.
/// </summary> /// </summary>
/// <param name="pixelSource">The pixel source.</param> /// <param name="pixelSource">The pixel source.</param>
internal void SwapPixelsBuffers(ImageFrame<TPixel> pixelSource) internal void SwapOrCopyPixelsBufferFrom(ImageFrame<TPixel> pixelSource)
{ {
Guard.NotNull(pixelSource, nameof(pixelSource)); Guard.NotNull(pixelSource, nameof(pixelSource));
Buffer2D<TPixel> temp = this.PixelBuffer; Buffer2D<TPixel>.SwapOrCopyContent(this.PixelBuffer, pixelSource.PixelBuffer);
this.PixelBuffer = pixelSource.PixelBuffer;
pixelSource.PixelBuffer = temp;
} }
/// <summary> /// <summary>

4
src/ImageSharp/Image{TPixel}.cs

@ -233,13 +233,13 @@ namespace SixLabors.ImageSharp
/// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer. /// Switches the buffers used by the image and the pixelSource meaning that the Image will "own" the buffer from the pixelSource and the pixelSource will now own the Images buffer.
/// </summary> /// </summary>
/// <param name="pixelSource">The pixel source.</param> /// <param name="pixelSource">The pixel source.</param>
internal void SwapPixelsBuffers(Image<TPixel> pixelSource) internal void SwapOrCopyPixelsBuffersFrom(Image<TPixel> pixelSource)
{ {
Guard.NotNull(pixelSource, nameof(pixelSource)); Guard.NotNull(pixelSource, nameof(pixelSource));
for (int i = 0; i < this.frames.Count; i++) for (int i = 0; i < this.frames.Count; i++)
{ {
this.frames[i].SwapPixelsBuffers(pixelSource.frames[i]); this.frames[i].SwapOrCopyPixelsBufferFrom(pixelSource.frames[i]);
} }
} }
} }

2
src/ImageSharp/Processing/Processors/CloningImageProcessor.cs

@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. The processor changed the number of frames."); throw new ImageProcessingException($"An error occurred when processing the image using {this.GetType().Name}. The processor changed the number of frames.");
} }
source.SwapPixelsBuffers(cloned); source.SwapOrCopyPixelsBuffersFrom(cloned);
} }
} }

2
tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs

@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
var bounds = new Rectangle(10, 10, size.Width / 2, size.Height / 2); var bounds = new Rectangle(10, 10, size.Width / 2, size.Height / 2);
ctx.DetectEdges(bounds); ctx.DetectEdges(bounds);
}, },
testName: nameof(this.DetectEdges_InBox)); useReferenceOutputFrom: nameof(this.DetectEdges_InBox));
} }
[Theory] [Theory]

23
tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs

@ -11,10 +11,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
{ {
using SixLabors.ImageSharp.Processing.Transforms; using SixLabors.ImageSharp.Processing.Transforms;
public class FlipTests : FileTestBase public class FlipTests
{ {
public static readonly string[] FlipFiles = { TestImages.Bmp.F };
public static readonly TheoryData<FlipMode> FlipValues public static readonly TheoryData<FlipMode> FlipValues
= new TheoryData<FlipMode> = new TheoryData<FlipMode>
{ {
@ -24,21 +22,28 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
}; };
[Theory] [Theory]
[WithTestPatternImages(nameof(FlipValues), 53, 37, DefaultPixelType)] [WithTestPatternImages(nameof(FlipValues), 53, 37, PixelTypes.Rgba32)]
[WithTestPatternImages(nameof(FlipValues), 17, 32, DefaultPixelType)] [WithTestPatternImages(nameof(FlipValues), 17, 32, PixelTypes.Rgba32)]
public void Flip<TPixel>(TestImageProvider<TPixel> provider, FlipMode flipMode) public void Flip<TPixel>(TestImageProvider<TPixel> provider, FlipMode flipMode)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
provider.RunValidatingProcessorTest(ctx => ctx.Flip(flipMode), testOutputDetails: flipMode); provider.RunValidatingProcessorTest(
ctx => ctx.Flip(flipMode),
testOutputDetails: flipMode,
appendPixelTypeToFileName: false);
} }
[Theory] [Theory]
[WithTestPatternImages(nameof(FlipValues), 53, 37, DefaultPixelType)] [WithTestPatternImages(nameof(FlipValues), 53, 37, PixelTypes.Rgba32)]
[WithTestPatternImages(nameof(FlipValues), 17, 32, DefaultPixelType)] [WithTestPatternImages(nameof(FlipValues), 17, 32, PixelTypes.Rgba32)]
public void Flip_WorksOnWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider, FlipMode flipMode) public void Flip_WorksOnWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider, FlipMode flipMode)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
provider.RunValidatingProcessorTestOnWrappedMemoryImage(ctx => ctx.Flip(flipMode), testOutputDetails: flipMode); provider.RunValidatingProcessorTestOnWrappedMemoryImage(
ctx => ctx.Flip(flipMode),
testOutputDetails: flipMode,
useReferenceOutputFrom: nameof(this.Flip),
appendPixelTypeToFileName: false);
} }
} }
} }

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

@ -84,6 +84,28 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
} }
} }
[Theory]
[WithFileCollection(nameof(CommonTestImages), DefaultPixelType)]
public void Resize_ThrowsForWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image0 = provider.GetImage())
{
var mmg = TestMemoryManager<TPixel>.CreateAsCopyOfPixelData(image0);
using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height))
{
Assert.ThrowsAny<Exception>(
() =>
{
image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true));
});
}
}
}
[Theory] [Theory]
[WithFile(TestImages.Png.Kaboom, DefaultPixelType)] [WithFile(TestImages.Png.Kaboom, DefaultPixelType)]
public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider) public void Resize_DoesNotBleedAlphaPixels<TPixel>(TestImageProvider<TPixel> provider)

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

@ -163,11 +163,15 @@ namespace SixLabors.ImageSharp.Tests
/// <param name="process">The image processing method to test. (As a delegate)</param> /// <param name="process">The image processing method to test. (As a delegate)</param>
/// <param name="testOutputDetails">The value to append to the test output.</param> /// <param name="testOutputDetails">The value to append to the test output.</param>
/// <param name="comparer">The custom image comparer to use</param> /// <param name="comparer">The custom image comparer to use</param>
/// <param name="appendPixelTypeToFileName"></param>
/// <param name="appendSourceFileOrDescription"></param>
internal static void RunValidatingProcessorTest<TPixel>( internal static void RunValidatingProcessorTest<TPixel>(
this TestImageProvider<TPixel> provider, this TestImageProvider<TPixel> provider,
Action<IImageProcessingContext<TPixel>> process, Action<IImageProcessingContext<TPixel>> process,
object testOutputDetails = null, object testOutputDetails = null,
ImageComparer comparer = null) ImageComparer comparer = null,
bool appendPixelTypeToFileName = true,
bool appendSourceFileOrDescription = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (comparer == null) if (comparer == null)
@ -178,12 +182,22 @@ namespace SixLabors.ImageSharp.Tests
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.Mutate(process); image.Mutate(process);
image.DebugSave(provider, testOutputDetails);
image.DebugSave(
provider,
testOutputDetails,
appendPixelTypeToFileName: appendPixelTypeToFileName,
appendSourceFileOrDescription: appendSourceFileOrDescription);
// TODO: Investigate the cause of pixel inaccuracies under Linux // TODO: Investigate the cause of pixel inaccuracies under Linux
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
image.CompareToReferenceOutput(comparer, provider, testOutputDetails); image.CompareToReferenceOutput(
comparer,
provider,
testOutputDetails,
appendPixelTypeToFileName: appendPixelTypeToFileName,
appendSourceFileOrDescription: appendSourceFileOrDescription);
} }
} }
} }
@ -193,7 +207,9 @@ namespace SixLabors.ImageSharp.Tests
Action<IImageProcessingContext<TPixel>> process, Action<IImageProcessingContext<TPixel>> process,
object testOutputDetails = null, object testOutputDetails = null,
ImageComparer comparer = null, ImageComparer comparer = null,
string testName = null) string useReferenceOutputFrom = null,
bool appendPixelTypeToFileName = true,
bool appendSourceFileOrDescription = true)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
if (comparer == null) if (comparer == null)
@ -201,11 +217,6 @@ namespace SixLabors.ImageSharp.Tests
comparer = ImageComparer.TolerantPercentage(0.001f); comparer = ImageComparer.TolerantPercentage(0.001f);
} }
if (testName != null)
{
provider.Utility.TestName = testName;
}
using (Image<TPixel> image0 = provider.GetImage()) using (Image<TPixel> image0 = provider.GetImage())
{ {
var mmg = TestMemoryManager<TPixel>.CreateAsCopyOfPixelData(image0.GetPixelSpan()); var mmg = TestMemoryManager<TPixel>.CreateAsCopyOfPixelData(image0.GetPixelSpan());
@ -213,12 +224,26 @@ namespace SixLabors.ImageSharp.Tests
using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height)) using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height))
{ {
image1.Mutate(process); image1.Mutate(process);
image1.DebugSave(provider, testOutputDetails); image1.DebugSave(
provider,
testOutputDetails,
appendPixelTypeToFileName: appendPixelTypeToFileName,
appendSourceFileOrDescription: appendSourceFileOrDescription);
// TODO: Investigate the cause of pixel inaccuracies under Linux // TODO: Investigate the cause of pixel inaccuracies under Linux
if (TestEnvironment.IsWindows) if (TestEnvironment.IsWindows)
{ {
image1.CompareToReferenceOutput(comparer, provider, testOutputDetails); if (useReferenceOutputFrom != null)
{
provider.Utility.TestName = useReferenceOutputFrom;
}
image1.CompareToReferenceOutput(
comparer,
provider,
testOutputDetails,
appendPixelTypeToFileName: appendPixelTypeToFileName,
appendSourceFileOrDescription: appendSourceFileOrDescription);
} }
} }
} }

2
tests/Images/External

@ -1 +1 @@
Subproject commit b1f057df33b7bfa6cabe714cf7090ac6017ea5d8 Subproject commit 802ffbae9af22d986226bc1c20d7d96aaf25d6b9
Loading…
Cancel
Save