From 6ff3aed4fb8381eb6d2fd40a000e2882430585fb Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 23 Jun 2018 12:22:24 +0100 Subject: [PATCH] remove `SavePixelData()` apis in favour of `GetPixelSpan()` --- src/ImageSharp/ImageExtensions.cs | 93 ------------------- .../ImageSharp.Tests/Image/ImageSaveTests.cs | 50 ---------- .../Transforms/AffineTransformTests.cs | 5 +- 3 files changed, 2 insertions(+), 146 deletions(-) diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index d8cda2f8f..9a46400fd 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -110,72 +110,6 @@ namespace SixLabors.ImageSharp source.Save(stream, encoder); } - /// - /// Returns the a copy of the image pixels as a byte array in row-major order. - /// - /// The pixel format. - /// The source image - /// A copy of the pixel data as bytes from this frame. - /// Thrown if the stream is null. - public static byte[] SavePixelData(this ImageFrame source) - where TPixel : struct, IPixel - => MemoryMarshal.AsBytes(source.GetPixelSpan()).ToArray(); - - /// - /// Writes the raw image pixels to the given byte array in row-major order. - /// - /// The pixel format. - /// The source image. - /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this ImageFrame source, byte[] buffer) - where TPixel : struct, IPixel - => SavePixelData(source, MemoryMarshal.Cast(buffer.AsSpan())); - - /// - /// Writes the raw image pixels to the given TPixel array in row-major order. - /// - /// The pixel format. - /// The source image - /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this ImageFrame source, TPixel[] buffer) - where TPixel : struct, IPixel - => SavePixelData(source, buffer.AsSpan()); - - /// - /// Returns a copy of the raw image pixels as a byte array in row-major order. - /// - /// The pixel format. - /// The source image. - /// A copy of the pixel data from the first frame as bytes. - /// Thrown if the stream is null. - public static byte[] SavePixelData(this Image source) - where TPixel : struct, IPixel - => source.Frames.RootFrame.SavePixelData(); - - /// - /// Writes the raw image pixels to the given byte array in row-major order. - /// - /// The pixel format. - /// The source image. - /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this Image source, byte[] buffer) - where TPixel : struct, IPixel - => source.Frames.RootFrame.SavePixelData(buffer); - - /// - /// Writes the raw image pixels to the given TPixel array in row-major order. - /// - /// The pixel format. - /// The source image - /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this Image source, TPixel[] buffer) - where TPixel : struct, IPixel - => source.Frames.RootFrame.SavePixelData(buffer); - /// /// Returns a Base64 encoded string from the given image. /// @@ -194,32 +128,5 @@ namespace SixLabors.ImageSharp return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(stream.ToArray())}"; } } - - /// - /// Writes the raw image bytes to the given byte span. - /// - /// The pixel format. - /// The source image - /// The span to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this Image source, Span buffer) - where TPixel : struct, IPixel - => source.Frames.RootFrame.SavePixelData(MemoryMarshal.Cast(buffer)); - - /// - /// Writes the raw image pixels to the given TPixel span. - /// - /// The pixel format. - /// The source image - /// The span to save the raw pixel data to. - /// Thrown if the stream is null. - public static void SavePixelData(this ImageFrame source, Span buffer) - where TPixel : struct, IPixel - { - Span sourceBuffer = source.GetPixelSpan(); - Guard.MustBeGreaterThanOrEqualTo(buffer.Length, sourceBuffer.Length, nameof(buffer)); - - sourceBuffer.CopyTo(buffer); - } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs index 857ecb1d0..3f4cb8afa 100644 --- a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs @@ -48,56 +48,6 @@ namespace SixLabors.ImageSharp.Tests this.Image = new Image(config, 1, 1); } - [Theory] - [WithTestPatternImages(13, 19, PixelTypes.Rgba32 | PixelTypes.Bgr24)] - public void SavePixelData_ToPixelStructArray(TestImageProvider provider) - where TPixel : struct, IPixel - { - using (Image image = provider.GetImage()) - { - var buffer = new TPixel[image.Width * image.Height]; - image.SavePixelData(buffer); - - image.ComparePixelBufferTo(buffer); - - // TODO: We need a separate test-case somewhere ensuring that image pixels are stored in row-major order! - } - } - - [Theory] - [WithTestPatternImages(19, 13, PixelTypes.Rgba32 | PixelTypes.Bgr24)] - public void SavePixelData_ToByteArray(TestImageProvider provider) - where TPixel : struct, IPixel - { - using (Image image = provider.GetImage()) - { - byte[] buffer = new byte[image.Width * image.Height * Unsafe.SizeOf()]; - - image.SavePixelData(buffer); - - image.ComparePixelBufferTo(MemoryMarshal.Cast(buffer.AsSpan())); - } - } - - [Fact] - public void SavePixelData_Rgba32_WhenBufferIsTooSmall_Throws() - { - using (var img = new Image(2, 2)) - { - img[0, 0] = Rgba32.White; - img[1, 0] = Rgba32.Black; - - img[0, 1] = Rgba32.Red; - img[1, 1] = Rgba32.Blue; - byte[] buffer = new byte[2 * 2]; // width * height * bytes per pixel - - Assert.Throws(() => - { - img.SavePixelData(buffer); - }); - } - } - [Fact] public void SavePath() { diff --git a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs index 3232c848e..1b06b0d6c 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/AffineTransformTests.cs @@ -1,7 +1,7 @@ using System; using System.Numerics; using System.Reflection; - +using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Transforms; @@ -241,8 +241,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Transforms private static void VerifyAllPixelsAreWhiteOrTransparent(Image image) where TPixel : struct, IPixel { - var data = new TPixel[image.Width * image.Height]; - image.Frames.RootFrame.SavePixelData(data); + Span data = image.Frames.RootFrame.GetPixelSpan(); var rgba = default(Rgba32); var white = new Rgb24(255, 255, 255); foreach (TPixel pixel in data)