From 56f00399f5ebff7335fc4278bd07d64ebbdd9011 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 31 Mar 2019 22:55:14 +0200 Subject: [PATCH] make .FromVector4() destructive --- ImageSharp.sln.DotSettings | 1 + src/ImageSharp/Common/Helpers/Guard.cs | 20 +++++++++++++++++++ .../Argb32.PixelOperations.Generated.cs | 4 ++-- .../Bgr24.PixelOperations.Generated.cs | 4 ++-- .../Bgra32.PixelOperations.Generated.cs | 4 ++-- .../Rgb24.PixelOperations.Generated.cs | 4 ++-- .../Generated/_Common.ttinclude | 4 ++-- .../Rgba32.PixelOperations.cs | 4 ++-- .../RgbaVector.PixelOperations.cs | 2 +- .../PixelFormats/PixelOperations{TPixel}.cs | 12 +++++++++-- 10 files changed, 44 insertions(+), 15 deletions(-) diff --git a/ImageSharp.sln.DotSettings b/ImageSharp.sln.DotSettings index 432f4524a..8e7b5dd48 100644 --- a/ImageSharp.sln.DotSettings +++ b/ImageSharp.sln.DotSettings @@ -382,6 +382,7 @@ True True True + True True True True diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index d8cf69a52..e86da78e3 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -257,6 +257,26 @@ namespace SixLabors.ImageSharp } } + /// + /// Verifies that the 'destination' span is not shorter than 'source'. + /// + /// The source element type + /// The destination element type + /// The source span + /// The destination span + /// The name of the argument for 'destination' + [MethodImpl(InliningOptions.ShortMethod)] + public static void DestinationShouldNotBeTooShort( + Span source, + Span destination, + string destinationParamName) + { + if (destination.Length < source.Length) + { + ThrowArgumentException($"Destination span is too short!", destinationParamName); + } + } + /// /// Verifies, that the `source` span has the length of 'minLength', or longer. /// diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Argb32.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Argb32.PixelOperations.Generated.cs index 2ec965dfb..add48cfd8 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Argb32.PixelOperations.Generated.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Argb32.PixelOperations.Generated.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, false); } @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromScaledVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromScaledVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, true); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgr24.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgr24.PixelOperations.Generated.cs index 711a9d1c1..372c71b32 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgr24.PixelOperations.Generated.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgr24.PixelOperations.Generated.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, false); } @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromScaledVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromScaledVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, true); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgra32.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgra32.PixelOperations.Generated.cs index b669dd534..aa4680dcc 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgra32.PixelOperations.Generated.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Bgra32.PixelOperations.Generated.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, false); } @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromScaledVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromScaledVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, true); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Rgb24.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Rgb24.PixelOperations.Generated.cs index dbf3102c4..f87ed88e5 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Rgb24.PixelOperations.Generated.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/Rgb24.PixelOperations.Generated.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, false); } @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.PixelFormats } /// - internal override void FromScaledVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span destPixels) + internal override void FromScaledVector4(Configuration configuration, Span sourceVectors, Span destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, true); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/_Common.ttinclude b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/_Common.ttinclude index f0675cb5b..bfeccfca5 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Generated/_Common.ttinclude +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Generated/_Common.ttinclude @@ -111,7 +111,7 @@ using System.Runtime.InteropServices; { #> /// - internal override void FromVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span<<#=pixelType#>> destPixels) + internal override void FromVector4(Configuration configuration, Span sourceVectors, Span<<#=pixelType#>> destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, false); } @@ -123,7 +123,7 @@ using System.Runtime.InteropServices; } /// - internal override void FromScaledVector4(Configuration configuration, ReadOnlySpan sourceVectors, Span<<#=pixelType#>> destPixels) + internal override void FromScaledVector4(Configuration configuration, Span sourceVectors, Span<<#=pixelType#>> destPixels) { Vector4Converters.RgbaCompatible.FromVector4(configuration, this, sourceVectors, destPixels, true); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.PixelOperations.cs index 004b25cd3..2801b8225 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.PixelOperations.cs @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// internal override void FromVector4( Configuration configuration, - ReadOnlySpan sourceVectors, + Span sourceVectors, Span destPixels) { Guard.DestinationShouldNotBeTooShort(sourceVectors, destPixels, nameof(destPixels)); @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// internal override void FromScaledVector4( Configuration configuration, - ReadOnlySpan sourceVectors, + Span sourceVectors, Span destinationColors) { this.FromVector4(configuration, sourceVectors, destinationColors); diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.PixelOperations.cs b/src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.PixelOperations.cs index bffaf57dd..0cde00736 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/RgbaVector.PixelOperations.cs @@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// internal override void FromScaledVector4( Configuration configuration, - ReadOnlySpan sourceVectors, + Span sourceVectors, Span destinationColors) { Guard.DestinationShouldNotBeTooShort(sourceVectors, destinationColors, nameof(destinationColors)); diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index 115dd7a43..61e338b9c 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -9,6 +9,14 @@ using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.PixelFormats { + internal enum PixelConversionModifiers + { + None = 0, + Scale = 1 << 0, + Premultiply = 1 << 1, + SRgbCompand = 1 << 2, + } + /// /// A stateless class implementing Strategy Pattern for batched pixel-data conversion operations /// for pixel buffers of type . @@ -30,7 +38,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// The to the destination colors. internal virtual void FromVector4( Configuration configuration, - ReadOnlySpan sourceVectors, + Span sourceVectors, Span destPixels) { Guard.NotNull(configuration, nameof(configuration)); @@ -64,7 +72,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// The to the destination colors. internal virtual void FromScaledVector4( Configuration configuration, - ReadOnlySpan sourceVectors, + Span sourceVectors, Span destinationColors) { Guard.NotNull(configuration, nameof(configuration));