diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 64cf602b0..4fb09f0a9 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -245,30 +245,15 @@ namespace SixLabors.ImageSharp this.GetPixelSpan().CopyTo(target.GetSpan()); } - /// - /// 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. - /// - /// The pixel source. - internal void SwapPixelsBuffers(PixelAccessor 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 newPixels = pixelSource.SwapBufferOwnership(this.PixelBuffer); - this.PixelBuffer = newPixels; - } - /// /// 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. /// /// The pixel source. - internal void SwapPixelsBuffers(ImageFrame pixelSource) + internal void SwapOrCopyPixelsBufferFrom(ImageFrame pixelSource) { Guard.NotNull(pixelSource, nameof(pixelSource)); - Buffer2D temp = this.PixelBuffer; - this.PixelBuffer = pixelSource.PixelBuffer; - pixelSource.PixelBuffer = temp; + Buffer2D.SwapOrCopyContent(this.PixelBuffer, pixelSource.PixelBuffer); } /// diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 2d84d4552..ad754bc75 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/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. /// /// The pixel source. - internal void SwapPixelsBuffers(Image pixelSource) + internal void SwapOrCopyPixelsBuffersFrom(Image pixelSource) { Guard.NotNull(pixelSource, nameof(pixelSource)); for (int i = 0; i < this.frames.Count; i++) { - this.frames[i].SwapPixelsBuffers(pixelSource.frames[i]); + this.frames[i].SwapOrCopyPixelsBufferFrom(pixelSource.frames[i]); } } } diff --git a/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs b/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs index ec342dd9f..8150d5921 100644 --- a/src/ImageSharp/Processing/Processors/CloningImageProcessor.cs +++ b/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."); } - source.SwapPixelsBuffers(cloned); + source.SwapOrCopyPixelsBuffersFrom(cloned); } } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs index 87a057549..6894f9b9b 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs +++ b/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); ctx.DetectEdges(bounds); }, - testName: nameof(this.DetectEdges_InBox)); + useReferenceOutputFrom: nameof(this.DetectEdges_InBox)); } [Theory] diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs index dd475675b..0ac8a9459 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/FlipTests.cs @@ -11,10 +11,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.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 FlipValues = new TheoryData { @@ -24,21 +22,28 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms }; [Theory] - [WithTestPatternImages(nameof(FlipValues), 53, 37, DefaultPixelType)] - [WithTestPatternImages(nameof(FlipValues), 17, 32, DefaultPixelType)] + [WithTestPatternImages(nameof(FlipValues), 53, 37, PixelTypes.Rgba32)] + [WithTestPatternImages(nameof(FlipValues), 17, 32, PixelTypes.Rgba32)] public void Flip(TestImageProvider provider, FlipMode flipMode) where TPixel : struct, IPixel { - provider.RunValidatingProcessorTest(ctx => ctx.Flip(flipMode), testOutputDetails: flipMode); + provider.RunValidatingProcessorTest( + ctx => ctx.Flip(flipMode), + testOutputDetails: flipMode, + appendPixelTypeToFileName: false); } [Theory] - [WithTestPatternImages(nameof(FlipValues), 53, 37, DefaultPixelType)] - [WithTestPatternImages(nameof(FlipValues), 17, 32, DefaultPixelType)] + [WithTestPatternImages(nameof(FlipValues), 53, 37, PixelTypes.Rgba32)] + [WithTestPatternImages(nameof(FlipValues), 17, 32, PixelTypes.Rgba32)] public void Flip_WorksOnWrappedMemoryImage(TestImageProvider provider, FlipMode flipMode) where TPixel : struct, IPixel { - provider.RunValidatingProcessorTestOnWrappedMemoryImage(ctx => ctx.Flip(flipMode), testOutputDetails: flipMode); + provider.RunValidatingProcessorTestOnWrappedMemoryImage( + ctx => ctx.Flip(flipMode), + testOutputDetails: flipMode, + useReferenceOutputFrom: nameof(this.Flip), + appendPixelTypeToFileName: false); } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index 84da154db..3fc22264d 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/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(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image0 = provider.GetImage()) + { + var mmg = TestMemoryManager.CreateAsCopyOfPixelData(image0); + + using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height)) + { + Assert.ThrowsAny( + () => + { + image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); + }); + } + } + } + + [Theory] [WithFile(TestImages.Png.Kaboom, DefaultPixelType)] public void Resize_DoesNotBleedAlphaPixels(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs index 1c44c9aae..ba7f6ad31 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs @@ -163,11 +163,15 @@ namespace SixLabors.ImageSharp.Tests /// The image processing method to test. (As a delegate) /// The value to append to the test output. /// The custom image comparer to use + /// + /// internal static void RunValidatingProcessorTest( this TestImageProvider provider, Action> process, object testOutputDetails = null, - ImageComparer comparer = null) + ImageComparer comparer = null, + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { if (comparer == null) @@ -178,12 +182,22 @@ namespace SixLabors.ImageSharp.Tests using (Image image = provider.GetImage()) { image.Mutate(process); - image.DebugSave(provider, testOutputDetails); + + image.DebugSave( + provider, + testOutputDetails, + appendPixelTypeToFileName: appendPixelTypeToFileName, + appendSourceFileOrDescription: appendSourceFileOrDescription); // TODO: Investigate the cause of pixel inaccuracies under Linux 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> process, object testOutputDetails = null, ImageComparer comparer = null, - string testName = null) + string useReferenceOutputFrom = null, + bool appendPixelTypeToFileName = true, + bool appendSourceFileOrDescription = true) where TPixel : struct, IPixel { if (comparer == null) @@ -201,11 +217,6 @@ namespace SixLabors.ImageSharp.Tests comparer = ImageComparer.TolerantPercentage(0.001f); } - if (testName != null) - { - provider.Utility.TestName = testName; - } - using (Image image0 = provider.GetImage()) { var mmg = TestMemoryManager.CreateAsCopyOfPixelData(image0.GetPixelSpan()); @@ -213,12 +224,26 @@ namespace SixLabors.ImageSharp.Tests using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height)) { image1.Mutate(process); - image1.DebugSave(provider, testOutputDetails); + image1.DebugSave( + provider, + testOutputDetails, + appendPixelTypeToFileName: appendPixelTypeToFileName, + appendSourceFileOrDescription: appendSourceFileOrDescription); // TODO: Investigate the cause of pixel inaccuracies under Linux if (TestEnvironment.IsWindows) { - image1.CompareToReferenceOutput(comparer, provider, testOutputDetails); + if (useReferenceOutputFrom != null) + { + provider.Utility.TestName = useReferenceOutputFrom; + } + + image1.CompareToReferenceOutput( + comparer, + provider, + testOutputDetails, + appendPixelTypeToFileName: appendPixelTypeToFileName, + appendSourceFileOrDescription: appendSourceFileOrDescription); } } } diff --git a/tests/Images/External b/tests/Images/External index b1f057df3..802ffbae9 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit b1f057df33b7bfa6cabe714cf7090ac6017ea5d8 +Subproject commit 802ffbae9af22d986226bc1c20d7d96aaf25d6b9