From 89348379adf06c661294d00cd9989b4410af7299 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 15:42:48 -0700 Subject: [PATCH 01/21] Expose LoadPixelData overloads accepting Span --- src/ImageSharp/Image.LoadPixelData.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index 0179e62ac..5f85d61ba 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - private static Image LoadPixelData(Span data, int width, int height) + public static Image LoadPixelData(Span data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - private static Image LoadPixelData(Span data, int width, int height) + public static Image LoadPixelData(Span data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - private static Image LoadPixelData(Configuration config, Span data, int width, int height) + public static Image LoadPixelData(Configuration config, Span data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(config, MemoryMarshal.Cast(data), width, height); @@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - private static Image LoadPixelData(Configuration config, Span data, int width, int height) + public static Image LoadPixelData(Configuration config, Span data, int width, int height) where TPixel : struct, IPixel { int count = width * height; From 052dbdc6a9bda000ddb881d6c4b45d951a5747ba Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:21:45 -0700 Subject: [PATCH 02/21] Cleanup ByteExtensions --- .../Common/Extensions/ByteExtensions.cs | 35 ++----------------- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 12 ++++--- 3 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index b5b868dea..ec53063e6 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/src/ImageSharp/Common/Extensions/ByteExtensions.cs @@ -12,44 +12,15 @@ namespace SixLabors.ImageSharp /// internal static class ByteExtensions { - /// - /// Returns a reference to the given position of the array unsafe casted to . - /// - /// The byte array. - /// The offset in bytes. - /// The reference at the given offset. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 GetRgb24(this byte[] bytes, int offset) - { - DebugGuard.MustBeLessThan(offset + 2, bytes.Length, nameof(offset)); - - return ref Unsafe.As(ref bytes[offset]); - } - /// /// Returns a reference to the given position of the span unsafe casted to . /// /// The byte span. - /// The offset in bytes. - /// The reference at the given offset. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 GetRgb24(this Span bytes, int offset) - { - DebugGuard.MustBeLessThan(offset + 2, bytes.Length, nameof(offset)); - - return ref Unsafe.As(ref bytes[offset]); - } - - /// - /// Returns a reference to the given position of the buffer pointed by `baseRef` unsafe casted to . - /// - /// A reference to the beginning of the buffer - /// The offset in bytes. /// The reference at the given offset. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 GetRgb24(ref byte baseRef, int offset) + public static ref Rgb24 AsRgb24(this Span bytes) { - return ref Unsafe.As(ref Unsafe.Add(ref baseRef, offset)); - } + return ref Unsafe.As(ref bytes[0]); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 1900d0df0..6c03bd2b1 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -461,7 +461,7 @@ namespace SixLabors.ImageSharp.Formats.Gif int indexOffset = index * 3; ref TPixel pixel = ref Unsafe.Add(ref rowRef, x); - rgba.Rgb = colorTable.GetRgb24(indexOffset); + rgba.Rgb = colorTable.Slice(indexOffset).AsRgb24(); pixel.PackFromRgba32(rgba); } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 8fefcb480..20a650e1f 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -853,7 +853,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : struct, IPixel { ReadOnlySpan newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth); - byte[] pal = this.palette; + Span pal = this.palette; var color = default(TPixel); var rgba = default(Rgba32); @@ -868,7 +868,7 @@ namespace SixLabors.ImageSharp.Formats.Png int pixelOffset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = pal.GetRgb24(pixelOffset); + rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); color.PackFromRgba32(rgba); row[x] = color; @@ -883,7 +883,7 @@ namespace SixLabors.ImageSharp.Formats.Png int index = newScanline[x]; int pixelOffset = index * 3; - rgba.Rgb = pal.GetRgb24(pixelOffset); + rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); color.PackFromRgba32(rgba); row[x] = color; @@ -946,9 +946,11 @@ namespace SixLabors.ImageSharp.Formats.Png ReadOnlySpan newScanline = ToArrayByBitsLength(scanlineBuffer, this.bytesPerScanline, this.header.BitDepth); var rgba = default(Rgba32); + Span pal = this.palette; if (this.paletteAlpha != null && this.paletteAlpha.Length > 0) { + // If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha // channel and we should try to read it. for (int x = pixelOffset, o = 0; x < this.header.Width; x += increment, o++) @@ -957,7 +959,7 @@ namespace SixLabors.ImageSharp.Formats.Png int offset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = this.palette.GetRgb24(offset); + rgba.Rgb = pal.Slice(offset).AsRgb24(); color.PackFromRgba32(rgba); rowSpan[x] = color; @@ -972,7 +974,7 @@ namespace SixLabors.ImageSharp.Formats.Png int index = newScanline[o]; int offset = index * 3; - rgba.Rgb = this.palette.GetRgb24(offset); + rgba.Rgb = pal.Slice(offset).AsRgb24(); color.PackFromRgba32(rgba); rowSpan[x] = color; From d590008880190333fdd09c248e1f0fdaf6266924 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:22:28 -0700 Subject: [PATCH 03/21] Remove trailing whitespace --- src/ImageSharp/Common/Extensions/ByteExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs index ec53063e6..ee41096cf 100644 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ b/src/ImageSharp/Common/Extensions/ByteExtensions.cs @@ -21,6 +21,6 @@ namespace SixLabors.ImageSharp public static ref Rgb24 AsRgb24(this Span bytes) { return ref Unsafe.As(ref bytes[0]); - } + } } } \ No newline at end of file From 049988669bfbf80562567547916edeb955ee6a2e Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:37:10 -0700 Subject: [PATCH 04/21] Simplify png decoder --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 20a650e1f..b89c655c8 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -853,7 +853,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : struct, IPixel { ReadOnlySpan newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth); - Span pal = this.palette; + ReadOnlySpan pal = MemoryMarshal.Cast(this.palette); var color = default(TPixel); var rgba = default(Rgba32); @@ -865,10 +865,9 @@ namespace SixLabors.ImageSharp.Formats.Png for (int x = 0; x < this.header.Width; x++) { int index = newScanline[x]; - int pixelOffset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); + rgba.Rgb = pal[index]; color.PackFromRgba32(rgba); row[x] = color; @@ -881,9 +880,8 @@ namespace SixLabors.ImageSharp.Formats.Png for (int x = 0; x < this.header.Width; x++) { int index = newScanline[x]; - int pixelOffset = index * 3; - rgba.Rgb = pal.Slice(pixelOffset).AsRgb24(); + rgba.Rgb = pal[index]; color.PackFromRgba32(rgba); row[x] = color; @@ -946,7 +944,7 @@ namespace SixLabors.ImageSharp.Formats.Png ReadOnlySpan newScanline = ToArrayByBitsLength(scanlineBuffer, this.bytesPerScanline, this.header.BitDepth); var rgba = default(Rgba32); - Span pal = this.palette; + Span pal = MemoryMarshal.Cast(this.palette); if (this.paletteAlpha != null && this.paletteAlpha.Length > 0) { @@ -956,10 +954,9 @@ namespace SixLabors.ImageSharp.Formats.Png for (int x = pixelOffset, o = 0; x < this.header.Width; x += increment, o++) { int index = newScanline[o]; - int offset = index * 3; rgba.A = this.paletteAlpha.Length > index ? this.paletteAlpha[index] : (byte)255; - rgba.Rgb = pal.Slice(offset).AsRgb24(); + rgba.Rgb = pal[index]; color.PackFromRgba32(rgba); rowSpan[x] = color; @@ -972,10 +969,8 @@ namespace SixLabors.ImageSharp.Formats.Png for (int x = pixelOffset, o = 0; x < this.header.Width; x += increment, o++) { int index = newScanline[o]; - int offset = index * 3; - - rgba.Rgb = pal.Slice(offset).AsRgb24(); + rgba.Rgb = pal[index]; color.PackFromRgba32(rgba); rowSpan[x] = color; } From ddda1005e9cb50bee32b8a72d06eb6cbef370c0e Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:44:27 -0700 Subject: [PATCH 05/21] Simplify GifDecoder --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 24 +++++++------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 6c03bd2b1..4fbd4baf5 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -39,11 +39,6 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private IManagedByteBuffer globalColorTable; - /// - /// The global color table length - /// - private int globalColorTableLength; - /// /// The area to restore. /// @@ -333,8 +328,8 @@ namespace SixLabors.ImageSharp.Formats.Gif indices = this.configuration.MemoryManager.AllocateManagedByteBuffer(imageDescriptor.Width * imageDescriptor.Height, true); this.ReadFrameIndices(imageDescriptor, indices.Span); - IManagedByteBuffer colorTable = localColorTable ?? this.globalColorTable; - this.ReadFrameColors(ref image, ref previousFrame, indices.Span, colorTable.Span, imageDescriptor); + ReadOnlySpan colorTable = MemoryMarshal.Cast((localColorTable ?? this.globalColorTable).Span); + this.ReadFrameColors(ref image, ref previousFrame, indices.Span, colorTable, imageDescriptor); // Skip any remaining blocks this.Skip(0); @@ -370,7 +365,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The indexed pixels. /// The color table containing the available colors. /// The - private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Span indices, Span colorTable, in GifImageDescriptor descriptor) + private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Span indices, ReadOnlySpan colorTable, in GifImageDescriptor descriptor) where TPixel : struct, IPixel { ref byte indicesRef = ref MemoryMarshal.GetReference(indices); @@ -458,11 +453,8 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.graphicsControlExtension.TransparencyFlag == false || this.graphicsControlExtension.TransparencyIndex != index) { - int indexOffset = index * 3; - ref TPixel pixel = ref Unsafe.Add(ref rowRef, x); - rgba.Rgb = colorTable.Slice(indexOffset).AsRgb24(); - + rgba.Rgb = colorTable[index]; pixel.PackFromRgba32(rgba); } @@ -534,12 +526,12 @@ namespace SixLabors.ImageSharp.Formats.Gif if (this.logicalScreenDescriptor.GlobalColorTableFlag) { - this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; + int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; - this.globalColorTable = this.MemoryManager.AllocateManagedByteBuffer(this.globalColorTableLength, true); + this.globalColorTable = this.MemoryManager.AllocateManagedByteBuffer(globalColorTableLength, true); - // Read the global color table from the stream - stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); + // Read the global color table data from the stream + stream.Read(this.globalColorTable.Array, 0, globalColorTableLength); } } } From b6ad0930c58d3eb075b82ea6a0857a7f87fcc836 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:44:34 -0700 Subject: [PATCH 06/21] Remove ByteExtensions --- .../Common/Extensions/ByteExtensions.cs | 26 ------------------- 1 file changed, 26 deletions(-) delete mode 100644 src/ImageSharp/Common/Extensions/ByteExtensions.cs diff --git a/src/ImageSharp/Common/Extensions/ByteExtensions.cs b/src/ImageSharp/Common/Extensions/ByteExtensions.cs deleted file mode 100644 index ee41096cf..000000000 --- a/src/ImageSharp/Common/Extensions/ByteExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.Runtime.CompilerServices; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp -{ - /// - /// Extension methods for the struct buffers. - /// - internal static class ByteExtensions - { - /// - /// Returns a reference to the given position of the span unsafe casted to . - /// - /// The byte span. - /// The reference at the given offset. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Rgb24 AsRgb24(this Span bytes) - { - return ref Unsafe.As(ref bytes[0]); - } - } -} \ No newline at end of file From 2e5110fdf23aa23420a43fc7ff1bfee057ec5d8b Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:44:45 -0700 Subject: [PATCH 07/21] Simplfiy ToRgbaHex --- src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs index 184928d0e..f5ebdb3fd 100644 --- a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs @@ -76,7 +76,10 @@ namespace SixLabors.ImageSharp.PixelFormats /// private static string ToRgbaHex(string hex) { - hex = hex.StartsWith("#") ? hex.Substring(1) : hex; + if (hex[0] == '#') + { + hex = hex.Substring(1); + } if (hex.Length == 8) { From c8ca5d16a6c541ca05a26b42e645f9c92656a801 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 9 May 2018 16:56:08 -0700 Subject: [PATCH 08/21] =?UTF-8?q?=F0=9F=91=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index b89c655c8..cc98b8450 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -948,7 +948,6 @@ namespace SixLabors.ImageSharp.Formats.Png if (this.paletteAlpha != null && this.paletteAlpha.Length > 0) { - // If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha // channel and we should try to read it. for (int x = pixelOffset, o = 0; x < this.header.Width; x += increment, o++) From 82ac8a720abac930e0985119bcf30ea7fa361d60 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 07:59:51 -0700 Subject: [PATCH 09/21] Rename Gaurd.NotNullOrEmpty to NotNullOrWhiteSpace to match behavior & cleanup messages. Also remove the unused message arguments. --- src/ImageSharp/Common/Helpers/Guard.cs | 67 ++++++++----------- src/ImageSharp/MetaData/ImageProperty.cs | 2 +- .../PixelFormats/ColorBuilder{TPixel}.cs | 2 +- 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 9f0a46f80..603cf566f 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -15,16 +15,15 @@ namespace SixLabors.ImageSharp internal static class Guard { /// - /// Verifies, that the method parameter with specified object value is not null - /// and throws an exception if it is found to be so. + /// Ensures that the value is not null. /// - /// The target object, which cannot be null. + /// The target object, which cannot be null. /// The name of the parameter that is to be checked. /// The error message, if any to add to the exception. - /// is null - public static void NotNull(object target, string parameterName, string message = "") + /// is null + public static void NotNull(object value, string parameterName, string message = "") { - if (target == null) + if (value == null) { if (!string.IsNullOrWhiteSpace(message)) { @@ -36,57 +35,49 @@ namespace SixLabors.ImageSharp } /// - /// Verifies, that the string method parameter with specified object value and message - /// is not null, not empty and does not contain only blanks and throws an exception - /// if the object is null. + /// Ensures that the target value is not null, empty, or whitespace. /// - /// The target string, which should be checked against being null or empty. + /// The target string, which should be checked against being null or empty. /// Name of the parameter. - /// The error message, if any to add to the exception. - /// is null. - /// is empty or contains only blanks. - public static void NotNullOrEmpty(string target, string parameterName, string message = "") + /// is null. + /// is empty or contains only blanks. + public static void NotNullOrWhiteSpace(string value, string parameterName) { - NotNull(target, parameterName, message); - - if (string.IsNullOrWhiteSpace(target)) + if (value == null) { - if (!string.IsNullOrWhiteSpace(message)) - { - throw new ArgumentException(message, parameterName); - } + throw new ArgumentNullException(parameterName); + } - throw new ArgumentException("Value cannot be null or empty and cannot contain only blanks.", parameterName); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Must not be empty or whitespace.", parameterName); } } /// - /// Verifies, that the enumeration is not null and not empty. + /// Ensures that the enumeration is not null or empty. /// - /// The type of objects in the - /// The target enumeration, which should be checked against being null or empty. + /// The type of objects in the + /// The target enumeration, which should be checked against being null or empty. /// Name of the parameter. /// The error message, if any to add to the exception. - /// is null. - /// is empty. - public static void NotNullOrEmpty(IEnumerable target, string parameterName, string message = "") + /// is null. + /// is empty. + public static void NotNullOrEmpty(IEnumerable value, string parameterName) { - NotNull(target, parameterName, message); - - if (!target.Any()) + if (value == null) { - if (!string.IsNullOrWhiteSpace(message)) - { - throw new ArgumentException(message, parameterName); - } + throw new ArgumentNullException(parameterName); + } - throw new ArgumentException("Value cannot be empty.", parameterName); + if (!value.Any()) + { + throw new ArgumentException("Must not be empty.", parameterName); } } /// - /// Verifies that the specified value is less than a maximum value - /// and throws an exception if it is not. + /// Ensures that the specified value is less than a maximum value. /// /// The target value, which should be validated. /// The maximum value. diff --git a/src/ImageSharp/MetaData/ImageProperty.cs b/src/ImageSharp/MetaData/ImageProperty.cs index c67c1f3cf..3e0cccd42 100644 --- a/src/ImageSharp/MetaData/ImageProperty.cs +++ b/src/ImageSharp/MetaData/ImageProperty.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.MetaData /// The value of the property. public ImageProperty(string name, string value) { - Guard.NotNullOrEmpty(name, nameof(name)); + Guard.NotNullOrWhiteSpace(name, nameof(name)); this.Name = name; this.Value = value; diff --git a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs index f5ebdb3fd..1e7645aeb 100644 --- a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Returns a that represents the color defined by the provided RGBA heax string. public static TPixel FromHex(string hex) { - Guard.NotNullOrEmpty(hex, nameof(hex)); + Guard.NotNullOrWhiteSpace(hex, nameof(hex)); hex = ToRgbaHex(hex); uint packedValue; From f47c4bcbcffbfc4de612ccd9640ca7b38e6e8d98 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 08:09:10 -0700 Subject: [PATCH 10/21] Don't use Linq to check if IEnumerable is empty. --- src/ImageSharp/ImageFrameCollection.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs index 0318a7068..cacad34a4 100644 --- a/src/ImageSharp/ImageFrameCollection.cs +++ b/src/ImageSharp/ImageFrameCollection.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp internal ImageFrameCollection(Image parent, IEnumerable> frames) { Guard.NotNull(parent, nameof(parent)); - Guard.NotNullOrEmpty(frames, nameof(frames)); + Guard.NotNull(frames, nameof(frames)); this.parent = parent; @@ -42,6 +42,12 @@ namespace SixLabors.ImageSharp this.ValidateFrame(f); this.frames.Add(f); } + + // Ensure at least 1 frame was added to the frames collection + if (this.frames.Count == 0) + { + throw new ArgumentException("Must not be empty.", nameof(frames)); + } } /// From 54c26c476aa16b57ffb8a13a0d99f04a39cd7925 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 08:12:32 -0700 Subject: [PATCH 11/21] React to Gaurd changes --- src/ImageSharp/Common/Helpers/Guard.cs | 26 +++++--------------- src/ImageSharp/ImageExtensions.cs | 2 +- tests/ImageSharp.Tests/Helpers/GuardTests.cs | 12 ++++----- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 603cf566f..9258beb36 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; namespace SixLabors.ImageSharp { @@ -19,17 +18,11 @@ namespace SixLabors.ImageSharp /// /// The target object, which cannot be null. /// The name of the parameter that is to be checked. - /// The error message, if any to add to the exception. /// is null - public static void NotNull(object value, string parameterName, string message = "") + public static void NotNull(object value, string parameterName) { if (value == null) { - if (!string.IsNullOrWhiteSpace(message)) - { - throw new ArgumentNullException(parameterName, message); - } - throw new ArgumentNullException(parameterName); } } @@ -60,17 +53,16 @@ namespace SixLabors.ImageSharp /// The type of objects in the /// The target enumeration, which should be checked against being null or empty. /// Name of the parameter. - /// The error message, if any to add to the exception. /// is null. /// is empty. - public static void NotNullOrEmpty(IEnumerable value, string parameterName) + public static void NotNullOrEmpty(ICollection value, string parameterName) { if (value == null) { throw new ArgumentNullException(parameterName); } - if (!value.Any()) + if (value.Count == 0) { throw new ArgumentException("Must not be empty.", parameterName); } @@ -182,15 +174,9 @@ namespace SixLabors.ImageSharp /// Verifies, that the method parameter with specified target value is true /// and throws an exception if it is found to be so. /// - /// - /// The target value, which cannot be false. - /// - /// - /// The name of the parameter that is to be checked. - /// - /// - /// The error message, if any to add to the exception. - /// + /// The target value, which cannot be false. + /// The name of the parameter that is to be checked. + /// The error message, if any to add to the exception. /// /// is false /// diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 2cdb71fc0..75d947469 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp public static void Save(this Image source, string filePath) where TPixel : struct, IPixel { - Guard.NotNullOrEmpty(filePath, nameof(filePath)); + Guard.NotNullOrWhiteSpace(filePath, nameof(filePath)); string ext = Path.GetExtension(filePath).Trim('.'); IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); diff --git a/tests/ImageSharp.Tests/Helpers/GuardTests.cs b/tests/ImageSharp.Tests/Helpers/GuardTests.cs index 83075dc83..42913e02d 100644 --- a/tests/ImageSharp.Tests/Helpers/GuardTests.cs +++ b/tests/ImageSharp.Tests/Helpers/GuardTests.cs @@ -35,27 +35,27 @@ namespace SixLabors.ImageSharp.Tests.Helpers /// [Fact] [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed. Suppression is OK here.")] - public void NotEmptyThrowsWhenEmpty() + public void NotEmptyOrWhiteSpaceThrowsWhenEmpty() { - Assert.Throws(() => Guard.NotNullOrEmpty("", string.Empty)); + Assert.Throws(() => Guard.NotNullOrWhiteSpace("", string.Empty)); } /// /// Tests that the method throws when the argument is whitespace. /// [Fact] - public void NotEmptyThrowsWhenWhitespace() + public void NotEmptyOrWhiteSpaceThrowsOnWhitespace() { - Assert.Throws(() => Guard.NotNullOrEmpty(" ", string.Empty)); + Assert.Throws(() => Guard.NotNullOrWhiteSpace(" ", string.Empty)); } /// /// Tests that the method throws when the argument name is null. /// [Fact] - public void NotEmptyThrowsWhenParameterNameNull() + public void NotEmptyOrWhiteSpaceThrowsWhenParameterNameNull() { - Assert.Throws(() => Guard.NotNullOrEmpty(null, null)); + Assert.Throws(() => Guard.NotNullOrWhiteSpace(null, null)); } /// From 6a3688d0981eb75b956d1c11fe61a2c14b2dcf88 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 08:12:59 -0700 Subject: [PATCH 12/21] Verify ColorBuilder throws on null and empty --- .../PixelFormats/ColorBuilderTests.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs diff --git a/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs b/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs new file mode 100644 index 000000000..6c139c2c0 --- /dev/null +++ b/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs @@ -0,0 +1,31 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Colors +{ + public class ColorBuilderTests + { + [Fact] + public void ParseHexLeadingPoundIsOptional() + { + Assert.Equal(new Rgb24(0, 128, 128), ColorBuilder.FromHex("#008080")); + Assert.Equal(new Rgb24(0, 128, 128), ColorBuilder.FromHex("008080")); + } + + [Fact] + public void ParseHexThrowsOnEmpty() + { + Assert.Throws(() => ColorBuilder.FromHex("")); + } + + [Fact] + public void ParseHexThrowsOnNull() + { + Assert.Throws(() => ColorBuilder.FromHex(null)); + } + } +} From a14ca40dfbf7397a770221459d37906f4c563595 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 08:21:33 -0700 Subject: [PATCH 13/21] Optimize ColorBuilder --- .../PixelFormats/ColorBuilder{TPixel}.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs index 1e7645aeb..c2c0277f9 100644 --- a/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/ColorBuilder{TPixel}.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.Globalization; namespace SixLabors.ImageSharp.PixelFormats @@ -26,18 +27,14 @@ namespace SixLabors.ImageSharp.PixelFormats Guard.NotNullOrWhiteSpace(hex, nameof(hex)); hex = ToRgbaHex(hex); - uint packedValue; - if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out packedValue)) + + if (hex == null || !uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint packedValue)) { throw new ArgumentException("Hexadecimal string is not in the correct format.", nameof(hex)); } TPixel result = default; - var rgba = new Rgba32( - (byte)(packedValue >> 24), - (byte)(packedValue >> 16), - (byte)(packedValue >> 8), - (byte)(packedValue >> 0)); + var rgba = new Rgba32(BinaryPrimitives.ReverseEndianness(packedValue)); result.PackFromRgba32(rgba); return result; @@ -96,12 +93,12 @@ namespace SixLabors.ImageSharp.PixelFormats return null; } - string red = char.ToString(hex[0]); - string green = char.ToString(hex[1]); - string blue = char.ToString(hex[2]); - string alpha = hex.Length == 3 ? "F" : char.ToString(hex[3]); + char r = hex[0]; + char g = hex[1]; + char b = hex[2]; + char a = hex.Length == 3 ? 'F' : hex[3]; - return string.Concat(red, red, green, green, blue, blue, alpha, alpha); + return new string(new[] { r, r, g, g, b, b, a, a }); } } } \ No newline at end of file From 5d02aebdc8c0c16d625ff0690ad058ebfb66a7a5 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 08:24:56 -0700 Subject: [PATCH 14/21] Improve ColorBuilder test coverage --- tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs b/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs index 6c139c2c0..e56cac279 100644 --- a/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/ColorBuilderTests.cs @@ -3,12 +3,21 @@ using System; using SixLabors.ImageSharp.PixelFormats; + using Xunit; namespace SixLabors.ImageSharp.Tests.Colors { public class ColorBuilderTests { + [Fact] + public void ParseShortHex() + { + Assert.Equal(new Rgb24(255, 255, 255), ColorBuilder.FromHex("#fff")); + Assert.Equal(new Rgb24(255, 255, 255), ColorBuilder.FromHex("fff")); + Assert.Equal(new Rgba32(0, 0, 0, 255), ColorBuilder.FromHex("000f")); + } + [Fact] public void ParseHexLeadingPoundIsOptional() { From 00a0451e211343730f74de83fb8efbfbef7810ec Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 09:08:00 -0700 Subject: [PATCH 15/21] Allow leading period in FindFormatByFileExtension --- src/ImageSharp/Formats/ImageFormatManager.cs | 7 +++++++ src/ImageSharp/ImageExtensions.cs | 2 +- .../TestUtilities/TestEnvironment.Formats.cs | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/ImageFormatManager.cs b/src/ImageSharp/Formats/ImageFormatManager.cs index 67ba11147..4e33a0445 100644 --- a/src/ImageSharp/Formats/ImageFormatManager.cs +++ b/src/ImageSharp/Formats/ImageFormatManager.cs @@ -85,6 +85,13 @@ namespace SixLabors.ImageSharp.Formats /// The if found otherwise null public IImageFormat FindFormatByFileExtension(string extension) { + Guard.NotNullOrWhiteSpace(extension, nameof(extension)); + + if (extension[0] == '.') + { + extension = extension.Substring(1); + } + return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)); } diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 75d947469..9f9a7e57a 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp { Guard.NotNullOrWhiteSpace(filePath, nameof(filePath)); - string ext = Path.GetExtension(filePath).Trim('.'); + string ext = Path.GetExtension(filePath); IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); if (format == null) { diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index fa9497a8f..6bebf3887 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -32,8 +32,8 @@ namespace SixLabors.ImageSharp.Tests internal static IImageFormat GetImageFormat(string filePath) { - string extension = Path.GetExtension(filePath).ToLower(); - if (extension[0] == '.') extension = extension.Substring(1); + string extension = Path.GetExtension(filePath); + IImageFormat format = Configuration.ImageFormatsManager.FindFormatByFileExtension(extension); return format; } From b220a1b46764b800d57ec46bf6d4aa71fe98cb83 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 13:47:50 -0700 Subject: [PATCH 16/21] Use ReadOnlySpans in LoadPixelData --- src/ImageSharp/Image.LoadPixelData.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index 5f85d61ba..307660b5a 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(Span data, int width, int height) + public static Image LoadPixelData(ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp /// A new . public static Image LoadPixelData(Configuration config, byte[] data, int width, int height) where TPixel : struct, IPixel - => LoadPixelData(config, MemoryMarshal.Cast(data.AsSpan()), width, height); + => LoadPixelData(config, MemoryMarshal.Cast(new ReadOnlySpan(data)), width, height); /// /// Create a new instance of the class from the given byte array in format. @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(Configuration config, Span data, int width, int height) + public static Image LoadPixelData(Configuration config, ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(config, MemoryMarshal.Cast(data), width, height); @@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp public static Image LoadPixelData(Configuration config, TPixel[] data, int width, int height) where TPixel : struct, IPixel { - return LoadPixelData(config, data.AsSpan(), width, height); + return LoadPixelData(config, new ReadOnlySpan(data), width, height); } /// @@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(Configuration config, Span data, int width, int height) + public static Image LoadPixelData(Configuration config, ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel { int count = width * height; From 86d68315af8ef540de9b0c9454c17488c7599db8 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 10 May 2018 13:48:16 -0700 Subject: [PATCH 17/21] Allow pixel data to be saved directly to a span --- src/ImageSharp/ImageExtensions.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 9f9a7e57a..192287ba5 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -4,13 +4,11 @@ using System; using System.Collections.Generic; using System.IO; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; -using SixLabors.Primitives; namespace SixLabors.ImageSharp { @@ -198,24 +196,24 @@ namespace SixLabors.ImageSharp } /// - /// Saves the raw image to the given bytes. + /// Saves the raw image to the given byte buffer. /// /// The Pixel format. /// The source image /// The buffer to save the raw pixel data to. /// Thrown if the stream is null. - internal static void SavePixelData(this Image source, Span buffer) + public static void SavePixelData(this Image source, Span buffer) where TPixel : struct, IPixel => source.Frames.RootFrame.SavePixelData(MemoryMarshal.Cast(buffer)); /// - /// Saves the raw image to the given bytes. + /// Saves the raw image to the given byte buffer. /// /// The Pixel format. /// The source image /// The buffer to save the raw pixel data to. /// Thrown if the stream is null. - internal static void SavePixelData(this ImageFrame source, Span buffer) + public static void SavePixelData(this ImageFrame source, Span buffer) where TPixel : struct, IPixel { Span sourceBuffer = source.GetPixelSpan(); @@ -224,4 +222,4 @@ namespace SixLabors.ImageSharp sourceBuffer.CopyTo(buffer); } } -} +} \ No newline at end of file From 14d77efc5c41d1305471598103b9c1eb499ac617 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 11 May 2018 10:31:17 -0700 Subject: [PATCH 18/21] Update Span ->ReadOnlySpan --- src/ImageSharp/Image.LoadPixelData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index 307660b5a..282f98086 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static Image LoadPixelData(Span data, int width, int height) + public static Image LoadPixelData(ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(Configuration.Default, data, width, height); From c161a7ee0b8b91e85701fcdfbefa51e1f2c30b67 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 11 May 2018 10:31:47 -0700 Subject: [PATCH 19/21] Tidy up ImageExtension docs --- src/ImageSharp/ImageExtensions.cs | 78 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 192287ba5..d8cda2f8f 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -19,10 +19,10 @@ namespace SixLabors.ImageSharp { #if !NETSTANDARD1_1 /// - /// Saves the image to the given stream using the currently loaded image format. + /// Writes the image to the given stream using the currently loaded image format. /// - /// The Pixel format. - /// The source image + /// The pixel format. + /// The source image. /// The file path to save the image to. /// Thrown if the stream is null. public static void Save(this Image source, string filePath) @@ -62,13 +62,13 @@ namespace SixLabors.ImageSharp } /// - /// Saves the image to the given stream using the currently loaded image format. + /// Writes the image to the given stream using the currently loaded image format. /// - /// The Pixel format. - /// The source image + /// The pixel format. + /// The source image. /// The file path to save the image to. /// The encoder to save the image with. - /// Thrown if the encoder is null. + /// Thrown if the encoder is null. public static void Save(this Image source, string filePath, IImageEncoder encoder) where TPixel : struct, IPixel { @@ -81,13 +81,13 @@ namespace SixLabors.ImageSharp #endif /// - /// Saves the image to the given stream using the currently loaded image format. + /// Writes the image to the given stream using the currently loaded image format. /// /// The Pixel format. - /// The source image + /// The source image. /// The stream to save the image to. - /// The format to save the image to. - /// Thrown if the stream is null. + /// The format to save the image in. + /// Thrown if the stream is null. public static void Save(this Image source, Stream stream, IImageFormat format) where TPixel : struct, IPixel { @@ -111,67 +111,67 @@ namespace SixLabors.ImageSharp } /// - /// Saves the raw image pixels to a byte array in row-major order. + /// Returns the a copy of the image pixels as a byte array in row-major order. /// - /// The Pixel format. + /// The pixel format. /// The source image /// A copy of the pixel data as bytes from this frame. - /// Thrown if the stream is null. + /// Thrown if the stream is null. public static byte[] SavePixelData(this ImageFrame source) where TPixel : struct, IPixel => MemoryMarshal.AsBytes(source.GetPixelSpan()).ToArray(); /// - /// Saves the raw image pixels to the given byte array in row-major order. + /// Writes the raw image pixels to the given byte array in row-major order. /// - /// The Pixel format. - /// The source image + /// The pixel format. + /// The source image. /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. + /// 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())); /// - /// Saves the raw image pixels to the given TPixel array in row-major order. + /// Writes the raw image pixels to the given TPixel array in row-major order. /// - /// The Pixel format. + /// The pixel format. /// The source image /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. + /// Thrown if the stream is null. public static void SavePixelData(this ImageFrame source, TPixel[] buffer) where TPixel : struct, IPixel => SavePixelData(source, buffer.AsSpan()); /// - /// Saves the raw image pixels to a byte array in row-major order. + /// Returns a copy of the raw image pixels as a byte array in row-major order. /// - /// The Pixel format. - /// The source image + /// The pixel format. + /// The source image. /// A copy of the pixel data from the first frame as bytes. - /// Thrown if the stream is null. + /// Thrown if the stream is null. public static byte[] SavePixelData(this Image source) where TPixel : struct, IPixel => source.Frames.RootFrame.SavePixelData(); /// - /// Saves the raw image pixels to the given byte array in row-major order. + /// Writes the raw image pixels to the given byte array in row-major order. /// - /// The Pixel format. - /// The source image + /// The pixel format. + /// The source image. /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. + /// Thrown if the stream is null. public static void SavePixelData(this Image source, byte[] buffer) where TPixel : struct, IPixel => source.Frames.RootFrame.SavePixelData(buffer); /// - /// Saves the raw image pixels to the given TPixel array in row-major order. + /// Writes the raw image pixels to the given TPixel array in row-major order. /// - /// The Pixel format. + /// The pixel format. /// The source image /// The buffer to save the raw pixel data to. - /// Thrown if the stream is null. + /// Thrown if the stream is null. public static void SavePixelData(this Image source, TPixel[] buffer) where TPixel : struct, IPixel => source.Frames.RootFrame.SavePixelData(buffer); @@ -180,7 +180,7 @@ namespace SixLabors.ImageSharp /// Returns a Base64 encoded string from the given image. /// /// - /// The Pixel format. + /// The pixel format. /// The source image /// The format. /// The @@ -196,22 +196,22 @@ namespace SixLabors.ImageSharp } /// - /// Saves the raw image to the given byte buffer. + /// Writes the raw image bytes to the given byte span. /// - /// The Pixel format. + /// The pixel format. /// The source image - /// The buffer to save the raw pixel data to. + /// 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)); /// - /// Saves the raw image to the given byte buffer. + /// Writes the raw image pixels to the given TPixel span. /// - /// The Pixel format. + /// The pixel format. /// The source image - /// The buffer to save the raw pixel data to. + /// 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 From 88098d741e075a0157e4d9ed5cbbae9f6a5653ec Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 11 May 2018 10:32:42 -0700 Subject: [PATCH 20/21] Update three more spans to ReadOnly --- src/ImageSharp/Formats/Png/Filters/NoneFilter.cs | 2 +- src/ImageSharp/ImageFrame.LoadPixelData.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index 0164ceafa..14af8ca6a 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Filters /// The scanline to encode /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(Span scanline, Span result) + public static void Encode(ReadOnlySpan scanline, Span result) { // Insert a byte before the data. result[0] = 0; diff --git a/src/ImageSharp/ImageFrame.LoadPixelData.cs b/src/ImageSharp/ImageFrame.LoadPixelData.cs index 1306c2836..4639a104b 100644 --- a/src/ImageSharp/ImageFrame.LoadPixelData.cs +++ b/src/ImageSharp/ImageFrame.LoadPixelData.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static ImageFrame LoadPixelData(MemoryManager memoryManager, Span data, int width, int height) + public static ImageFrame LoadPixelData(MemoryManager memoryManager, ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel => LoadPixelData(memoryManager, MemoryMarshal.Cast(data), width, height); @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp /// The height of the final image. /// The pixel format. /// A new . - public static ImageFrame LoadPixelData(MemoryManager memoryManager, Span data, int width, int height) + public static ImageFrame LoadPixelData(MemoryManager memoryManager, ReadOnlySpan data, int width, int height) where TPixel : struct, IPixel { int count = width * height; From 609e7663a0a042ae7e90850deae76085c89910c4 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 11 May 2018 10:37:29 -0700 Subject: [PATCH 21/21] React to LoadPixelData change --- src/ImageSharp/ImageFrameCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs index cacad34a4..be15a6527 100644 --- a/src/ImageSharp/ImageFrameCollection.cs +++ b/src/ImageSharp/ImageFrameCollection.cs @@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp var frame = ImageFrame.LoadPixelData( this.parent.GetMemoryManager(), - new Span(source), + new ReadOnlySpan(source), this.RootFrame.Width, this.RootFrame.Height); this.frames.Add(frame);