diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index a450aebf43..435c629bc6 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -19,6 +19,31 @@ jobs: isARM: - ${{ contains(github.event.pull_request.labels.*.name, 'arch:arm32') || contains(github.event.pull_request.labels.*.name, 'arch:arm64') }} options: + - os: ubuntu-latest + framework: net9.0 + sdk: 9.0.x + sdk-preview: true + runtime: -x64 + codecov: false + - os: macos-13 # macos-latest runs on arm64 runners where libgdiplus is unavailable + framework: net9.0 + sdk: 9.0.x + sdk-preview: true + runtime: -x64 + codecov: false + - os: windows-latest + framework: net9.0 + sdk: 9.0.x + sdk-preview: true + runtime: -x64 + codecov: false + - os: buildjet-4vcpu-ubuntu-2204-arm + framework: net9.0 + sdk: 9.0.x + sdk-preview: true + runtime: -x64 + codecov: false + - os: ubuntu-latest framework: net8.0 sdk: 8.0.x @@ -100,7 +125,7 @@ jobs: uses: actions/setup-dotnet@v4 with: dotnet-version: | - 8.0.x + 9.0.x - name: DotNet Build if: ${{ matrix.options.sdk-preview != true }} diff --git a/src/ImageSharp.ruleset b/src/ImageSharp.ruleset index b609890200..dee0393cd7 100644 --- a/src/ImageSharp.ruleset +++ b/src/ImageSharp.ruleset @@ -1,4 +1,7 @@  + + + \ No newline at end of file diff --git a/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs b/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs new file mode 100644 index 0000000000..f753e7282b --- /dev/null +++ b/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs @@ -0,0 +1,15 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Acts as a base encoder for all formats that are aware of and can handle alpha transparency. +/// +public abstract class AlphaAwareImageEncoder : ImageEncoder +{ + /// + /// Gets or initializes the mode that determines how transparent pixels are handled during encoding. + /// + public TransparentColorMode TransparentColorMode { get; init; } +} diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 7c92d3e463..321a559b1e 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -91,6 +91,11 @@ internal sealed class BmpEncoderCore /// private readonly IPixelSamplingStrategy pixelSamplingStrategy; + /// + /// The transparent color mode. + /// + private readonly TransparentColorMode transparentColorMode; + /// private readonly bool processedAlphaMask; @@ -113,6 +118,7 @@ internal sealed class BmpEncoderCore // TODO: Use a palette quantizer if supplied. this.quantizer = encoder.Quantizer ?? KnownQuantizers.Octree; this.pixelSamplingStrategy = encoder.PixelSamplingStrategy; + this.transparentColorMode = encoder.TransparentColorMode; this.infoHeaderType = encoder.SupportTransparency ? BmpInfoHeaderType.WinVersion4 : BmpInfoHeaderType.WinVersion3; this.processedAlphaMask = encoder.ProcessedAlphaMask; this.skipFileHeader = encoder.SkipFileHeader; @@ -181,14 +187,14 @@ internal sealed class BmpEncoderCore Span buffer = stackalloc byte[infoHeaderSize]; - // for ico/cur encoder. + // For ico/cur encoder. if (!this.skipFileHeader) { WriteBitmapFileHeader(stream, infoHeaderSize, colorPaletteSize, iccProfileSize, infoHeader, buffer); } this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize); - this.WriteImage(configuration, stream, image); + this.WriteImage(configuration, stream, image, cancellationToken); WriteColorProfile(stream, iccProfileData, buffer, basePosition); stream.Flush(); @@ -345,44 +351,65 @@ internal sealed class BmpEncoderCore /// /// The containing pixel data. /// - private void WriteImage(Configuration configuration, Stream stream, Image image) + /// The token to monitor for cancellation requests. + private void WriteImage( + Configuration configuration, + Stream stream, + Image image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - Buffer2D pixels = image.Frames.RootFrame.PixelBuffer; - switch (this.bitsPerPixel) + ImageFrame? clonedFrame = null; + try { - case BmpBitsPerPixel.Bit32: - this.Write32BitPixelData(configuration, stream, pixels); - break; + if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + { + clonedFrame = image.Frames.RootFrame.Clone(); + EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + } - case BmpBitsPerPixel.Bit24: - this.Write24BitPixelData(configuration, stream, pixels); - break; + ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; + Buffer2D pixels = encodingFrame.PixelBuffer; - case BmpBitsPerPixel.Bit16: - this.Write16BitPixelData(configuration, stream, pixels); - break; + switch (this.bitsPerPixel) + { + case BmpBitsPerPixel.Bit32: + this.Write32BitPixelData(configuration, stream, pixels, cancellationToken); + break; - case BmpBitsPerPixel.Bit8: - this.Write8BitPixelData(configuration, stream, image); - break; + case BmpBitsPerPixel.Bit24: + this.Write24BitPixelData(configuration, stream, pixels, cancellationToken); + break; - case BmpBitsPerPixel.Bit4: - this.Write4BitPixelData(configuration, stream, image); - break; + case BmpBitsPerPixel.Bit16: + this.Write16BitPixelData(configuration, stream, pixels, cancellationToken); + break; - case BmpBitsPerPixel.Bit2: - this.Write2BitPixelData(configuration, stream, image); - break; + case BmpBitsPerPixel.Bit8: + this.Write8BitPixelData(configuration, stream, encodingFrame, cancellationToken); + break; - case BmpBitsPerPixel.Bit1: - this.Write1BitPixelData(configuration, stream, image); - break; - } + case BmpBitsPerPixel.Bit4: + this.Write4BitPixelData(configuration, stream, encodingFrame, cancellationToken); + break; + + case BmpBitsPerPixel.Bit2: + this.Write2BitPixelData(configuration, stream, encodingFrame, cancellationToken); + break; - if (this.processedAlphaMask) + case BmpBitsPerPixel.Bit1: + this.Write1BitPixelData(configuration, stream, encodingFrame, cancellationToken); + break; + } + + if (this.processedAlphaMask) + { + ProcessedAlphaMask(stream, encodingFrame); + } + } + finally { - ProcessedAlphaMask(stream, image); + clonedFrame?.Dispose(); } } @@ -396,7 +423,12 @@ internal sealed class BmpEncoderCore /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write32BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) + /// The token to monitor for cancellation requests. + private void Write32BitPixelData( + Configuration configuration, + Stream stream, + Buffer2D pixels, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -404,6 +436,8 @@ internal sealed class BmpEncoderCore for (int y = pixels.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra32Bytes( configuration, @@ -421,7 +455,12 @@ internal sealed class BmpEncoderCore /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write24BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) + /// The token to monitor for cancellation requests. + private void Write24BitPixelData( + Configuration configuration, + Stream stream, + Buffer2D pixels, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -431,6 +470,8 @@ internal sealed class BmpEncoderCore for (int y = pixels.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgr24Bytes( configuration, @@ -448,7 +489,12 @@ internal sealed class BmpEncoderCore /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write16BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) + /// The token to monitor for cancellation requests. + private void Write16BitPixelData( + Configuration configuration, + Stream stream, + Buffer2D pixels, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -458,6 +504,8 @@ internal sealed class BmpEncoderCore for (int y = pixels.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra5551Bytes( @@ -476,21 +524,32 @@ internal sealed class BmpEncoderCore /// The type of the pixel. /// The global configuration. /// The to write to. - /// The containing pixel data. - private void Write8BitPixelData(Configuration configuration, Stream stream, Image image) + /// The containing pixel data. + /// The token to monitor for cancellation requests. + private void Write8BitPixelData( + Configuration configuration, + Stream stream, + ImageFrame encodingFrame, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - bool isL8 = typeof(TPixel) == typeof(L8); + PixelTypeInfo info = TPixel.GetPixelTypeInfo(); + bool is8BitLuminance = + info.BitsPerPixel == 8 + && info.ColorType == PixelColorType.Luminance + && info.AlphaRepresentation == PixelAlphaRepresentation.None + && info.ComponentInfo!.Value.ComponentCount == 1; + using IMemoryOwner colorPaletteBuffer = this.memoryAllocator.Allocate(ColorPaletteSize8Bit, AllocationOptions.Clean); Span colorPalette = colorPaletteBuffer.GetSpan(); - if (isL8) + if (is8BitLuminance) { - this.Write8BitPixelData(stream, image, colorPalette); + this.Write8BitLuminancePixelData(stream, encodingFrame, colorPalette, cancellationToken); } else { - this.Write8BitColor(configuration, stream, image, colorPalette); + this.Write8BitColor(configuration, stream, encodingFrame, colorPalette, cancellationToken); } } @@ -500,21 +559,29 @@ internal sealed class BmpEncoderCore /// The type of the pixel. /// The global configuration. /// The to write to. - /// The containing pixel data. + /// The containing pixel data. /// A byte span of size 1024 for the color palette. - private void Write8BitColor(Configuration configuration, Stream stream, Image image, Span colorPalette) + /// The token to monitor for cancellation requests. + private void Write8BitColor( + Configuration configuration, + Stream stream, + ImageFrame encodingFrame, + Span colorPalette, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); - using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); + using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(encodingFrame, encodingFrame.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); - for (int y = image.Height - 1; y >= 0; y--) + for (int y = encodingFrame.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + ReadOnlySpan pixelSpan = quantized.DangerousGetRowSpan(y); stream.Write(pixelSpan); @@ -530,9 +597,14 @@ internal sealed class BmpEncoderCore /// /// The type of the pixel. /// The to write to. - /// The containing pixel data. + /// The containing pixel data. /// A byte span of size 1024 for the color palette. - private void Write8BitPixelData(Stream stream, Image image, Span colorPalette) + /// The token to monitor for cancellation requests. + private void Write8BitLuminancePixelData( + Stream stream, + ImageFrame encodingFrame, + Span colorPalette, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { // Create a color palette with 256 different gray values. @@ -549,9 +621,11 @@ internal sealed class BmpEncoderCore } stream.Write(colorPalette); - Buffer2D imageBuffer = image.GetRootFramePixelBuffer(); - for (int y = image.Height - 1; y >= 0; y--) + Buffer2D imageBuffer = encodingFrame.PixelBuffer; + for (int y = encodingFrame.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + ReadOnlySpan inputPixelRow = imageBuffer.DangerousGetRowSpan(y); ReadOnlySpan outputPixelRow = MemoryMarshal.AsBytes(inputPixelRow); stream.Write(outputPixelRow); @@ -569,8 +643,13 @@ internal sealed class BmpEncoderCore /// The type of the pixel. /// The global configuration. /// The to write to. - /// The containing pixel data. - private void Write4BitPixelData(Configuration configuration, Stream stream, Image image) + /// The containing pixel data. + /// The token to monitor for cancellation requests. + private void Write4BitPixelData( + Configuration configuration, + Stream stream, + ImageFrame encodingFrame, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -580,9 +659,9 @@ internal sealed class BmpEncoderCore DitherScale = this.quantizer.Options.DitherScale }); - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); + frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); - using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(encodingFrame, encodingFrame.Bounds); using IMemoryOwner colorPaletteBuffer = this.memoryAllocator.Allocate(ColorPaletteSize4Bit, AllocationOptions.Clean); Span colorPalette = colorPaletteBuffer.GetSpan(); @@ -591,8 +670,10 @@ internal sealed class BmpEncoderCore ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; - for (int y = image.Height - 1; y >= 0; y--) + for (int y = encodingFrame.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + pixelRowSpan = quantized.DangerousGetRowSpan(y); int endIdx = pixelRowSpan.Length % 2 == 0 ? pixelRowSpan.Length : pixelRowSpan.Length - 1; @@ -619,8 +700,13 @@ internal sealed class BmpEncoderCore /// The type of the pixel. /// The global configuration. /// The to write to. - /// The containing pixel data. - private void Write2BitPixelData(Configuration configuration, Stream stream, Image image) + /// The containing pixel data. + /// The token to monitor for cancellation requests. + private void Write2BitPixelData( + Configuration configuration, + Stream stream, + ImageFrame encodingFrame, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -630,9 +716,9 @@ internal sealed class BmpEncoderCore DitherScale = this.quantizer.Options.DitherScale }); - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); + frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); - using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(encodingFrame, encodingFrame.Bounds); using IMemoryOwner colorPaletteBuffer = this.memoryAllocator.Allocate(ColorPaletteSize2Bit, AllocationOptions.Clean); Span colorPalette = colorPaletteBuffer.GetSpan(); @@ -641,8 +727,10 @@ internal sealed class BmpEncoderCore ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; - for (int y = image.Height - 1; y >= 0; y--) + for (int y = encodingFrame.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + pixelRowSpan = quantized.DangerousGetRowSpan(y); int endIdx = pixelRowSpan.Length % 4 == 0 ? pixelRowSpan.Length : pixelRowSpan.Length - 4; @@ -678,8 +766,13 @@ internal sealed class BmpEncoderCore /// The type of the pixel. /// The global configuration. /// The to write to. - /// The containing pixel data. - private void Write1BitPixelData(Configuration configuration, Stream stream, Image image) + /// The containing pixel data. + /// The token to monitor for cancellation requests. + private void Write1BitPixelData( + Configuration configuration, + Stream stream, + ImageFrame encodingFrame, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -689,9 +782,9 @@ internal sealed class BmpEncoderCore DitherScale = this.quantizer.Options.DitherScale }); - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); + frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); - using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(encodingFrame, encodingFrame.Bounds); using IMemoryOwner colorPaletteBuffer = this.memoryAllocator.Allocate(ColorPaletteSize1Bit, AllocationOptions.Clean); Span colorPalette = colorPaletteBuffer.GetSpan(); @@ -700,8 +793,10 @@ internal sealed class BmpEncoderCore ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; - for (int y = image.Height - 1; y >= 0; y--) + for (int y = encodingFrame.Height - 1; y >= 0; y--) { + cancellationToken.ThrowIfCancellationRequested(); + quantizedPixelRow = quantized.DangerousGetRowSpan(y); int endIdx = quantizedPixelRow.Length % 8 == 0 ? quantizedPixelRow.Length : quantizedPixelRow.Length - 8; @@ -766,10 +861,10 @@ internal sealed class BmpEncoderCore stream.WriteByte(indices); } - private static void ProcessedAlphaMask(Stream stream, Image image) + private static void ProcessedAlphaMask(Stream stream, ImageFrame encodingFrame) where TPixel : unmanaged, IPixel { - int arrayWidth = image.Width / 8; + int arrayWidth = encodingFrame.Width / 8; int padding = arrayWidth % 4; if (padding is not 0) { @@ -777,10 +872,10 @@ internal sealed class BmpEncoderCore } Span mask = stackalloc byte[arrayWidth]; - for (int y = image.Height - 1; y >= 0; y--) + for (int y = encodingFrame.Height - 1; y >= 0; y--) { mask.Clear(); - Span row = image.GetRootFramePixelBuffer().DangerousGetRowSpan(y); + Span row = encodingFrame.PixelBuffer.DangerousGetRowSpan(y); for (int i = 0; i < arrayWidth; i++) { diff --git a/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs b/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs index 4e9a432b16..01b7fbce08 100644 --- a/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs +++ b/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs @@ -48,13 +48,13 @@ public class CurFrameMetadata : IFormatFrameMetadata /// Gets or sets the encoding width.
/// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. /// - public byte EncodingWidth { get; set; } + public byte? EncodingWidth { get; set; } /// /// Gets or sets the encoding height.
/// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. ///
- public byte EncodingHeight { get; set; } + public byte? EncodingHeight { get; set; } /// /// Gets or sets the number of bits per pixel.
@@ -80,20 +80,6 @@ public class CurFrameMetadata : IFormatFrameMetadata }; } - byte encodingWidth = metadata.EncodingWidth switch - { - > 255 => 0, - <= 255 and >= 1 => (byte)metadata.EncodingWidth, - _ => 0 - }; - - byte encodingHeight = metadata.EncodingHeight switch - { - > 255 => 0, - <= 255 and >= 1 => (byte)metadata.EncodingHeight, - _ => 0 - }; - int bpp = metadata.PixelTypeInfo.Value.BitsPerPixel; BmpBitsPerPixel bbpp = bpp switch { @@ -116,8 +102,8 @@ public class CurFrameMetadata : IFormatFrameMetadata { BmpBitsPerPixel = bbpp, Compression = compression, - EncodingWidth = encodingWidth, - EncodingHeight = encodingHeight, + EncodingWidth = ClampEncodingDimension(metadata.EncodingWidth), + EncodingHeight = ClampEncodingDimension(metadata.EncodingHeight), ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null }; } @@ -138,8 +124,8 @@ public class CurFrameMetadata : IFormatFrameMetadata { float ratioX = destination.Width / (float)source.Width; float ratioY = destination.Height / (float)source.Height; - this.EncodingWidth = Scale(this.EncodingWidth, destination.Width, ratioX); - this.EncodingHeight = Scale(this.EncodingHeight, destination.Height, ratioY); + this.EncodingWidth = ScaleEncodingDimension(this.EncodingWidth, destination.Width, ratioX); + this.EncodingHeight = ScaleEncodingDimension(this.EncodingHeight, destination.Height, ratioY); } /// @@ -156,7 +142,7 @@ public class CurFrameMetadata : IFormatFrameMetadata this.HotspotY = entry.BitCount; } - internal IconDirEntry ToIconDirEntry() + internal IconDirEntry ToIconDirEntry(Size size) { byte colorCount = this.Compression == IconFrameCompression.Png || this.BmpBitsPerPixel > BmpBitsPerPixel.Bit8 ? (byte)0 @@ -164,8 +150,8 @@ public class CurFrameMetadata : IFormatFrameMetadata return new() { - Width = this.EncodingWidth, - Height = this.EncodingHeight, + Width = ClampEncodingDimension(this.EncodingWidth ?? size.Width), + Height = ClampEncodingDimension(this.EncodingHeight ?? size.Height), Planes = this.HotspotX, BitCount = this.HotspotY, ColorCount = colorCount @@ -233,13 +219,22 @@ public class CurFrameMetadata : IFormatFrameMetadata }; } - private static byte Scale(byte? value, int destination, float ratio) + private static byte ScaleEncodingDimension(byte? value, int destination, float ratio) { if (value is null) { - return (byte)Math.Clamp(destination, 0, 255); + return ClampEncodingDimension(destination); } - return Math.Min((byte)MathF.Ceiling(value.Value * ratio), (byte)Math.Clamp(destination, 0, 255)); + return ClampEncodingDimension(MathF.Ceiling(value.Value * ratio)); } + + private static byte ClampEncodingDimension(float? dimension) + => dimension switch + { + // Encoding dimensions can be between 0-256 where 0 means 256 or greater. + > 255 => 0, + <= 255 and >= 1 => (byte)dimension, + _ => 0 + }; } diff --git a/src/ImageSharp/Formats/EncodingUtilities.cs b/src/ImageSharp/Formats/EncodingUtilities.cs new file mode 100644 index 0000000000..a979fdf6fa --- /dev/null +++ b/src/ImageSharp/Formats/EncodingUtilities.cs @@ -0,0 +1,97 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Buffers; +using System.Numerics; +using System.Runtime.Intrinsics; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Provides utilities for encoding images. +/// +internal static class EncodingUtilities +{ + public static bool ShouldClearTransparentPixels(TransparentColorMode mode) + where TPixel : unmanaged, IPixel + => mode == TransparentColorMode.Clear && + TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Unassociated; + + /// + /// Convert transparent pixels, to pixels represented by , which can yield + /// to better compression in some cases. + /// + /// The type of the pixel. + /// The cloned where the transparent pixels will be changed. + /// The color to replace transparent pixels with. + public static void ClearTransparentPixels(ImageFrame clone, Color color) + where TPixel : unmanaged, IPixel + { + Buffer2DRegion buffer = clone.PixelBuffer.GetRegion(); + ClearTransparentPixels(clone.Configuration, ref buffer, color); + } + + /// + /// Convert transparent pixels, to pixels represented by , which can yield + /// to better compression in some cases. + /// + /// The type of the pixel. + /// The configuration. + /// The cloned where the transparent pixels will be changed. + /// The color to replace transparent pixels with. + public static void ClearTransparentPixels( + Configuration configuration, + ref Buffer2DRegion clone, + Color color) + where TPixel : unmanaged, IPixel + { + using IMemoryOwner vectors = configuration.MemoryAllocator.Allocate(clone.Width); + Span vectorsSpan = vectors.GetSpan(); + Vector4 replacement = color.ToScaledVector4(); + for (int y = 0; y < clone.Height; y++) + { + Span span = clone.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, span, vectorsSpan, PixelConversionModifiers.Scale); + ClearTransparentPixelRow(vectorsSpan, replacement); + PixelOperations.Instance.FromVector4Destructive(configuration, vectorsSpan, span, PixelConversionModifiers.Scale); + } + } + + private static void ClearTransparentPixelRow( + Span vectorsSpan, + Vector4 replacement) + { + if (Vector128.IsHardwareAccelerated) + { + Vector128 replacement128 = replacement.AsVector128(); + + for (int i = 0; i < vectorsSpan.Length; i++) + { + ref Vector4 v = ref vectorsSpan[i]; + Vector128 v128 = v.AsVector128(); + + // Do `vector == 0` + Vector128 mask = Vector128.Equals(v128, Vector128.Zero); + + // Replicate the result for W to all elements (is AllBitsSet if the W was 0 and Zero otherwise) + mask = Vector128.Shuffle(mask, Vector128.Create(3, 3, 3, 3)); + + // Use the mask to select the replacement vector + // (replacement & mask) | (v128 & ~mask) + v = Vector128.ConditionalSelect(mask, replacement128, v128).AsVector4(); + } + } + else + { + for (int i = 0; i < vectorsSpan.Length; i++) + { + if (vectorsSpan[i].W == 0F) + { + vectorsSpan[i] = replacement; + } + } + } + } +} diff --git a/src/ImageSharp/Formats/FormatConnectingMetadata.cs b/src/ImageSharp/Formats/FormatConnectingMetadata.cs index baf0a35457..9cfe40f385 100644 --- a/src/ImageSharp/Formats/FormatConnectingMetadata.cs +++ b/src/ImageSharp/Formats/FormatConnectingMetadata.cs @@ -45,7 +45,7 @@ public class FormatConnectingMetadata /// Gets the default background color of the canvas when animating. /// This color may be used to fill the unused space on the canvas around the frames, /// as well as the transparent pixels of the first frame. - /// The background color is also used when the disposal mode is . + /// The background color is also used when a frame disposal mode is . ///
/// /// Defaults to . diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index a99b5862dd..3d6990478d 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -665,7 +665,7 @@ internal sealed class GifDecoderCore : ImageDecoderCore return; } - Rectangle interest = Rectangle.Intersect(frame.Bounds(), this.restoreArea.Value); + Rectangle interest = Rectangle.Intersect(frame.Bounds, this.restoreArea.Value); Buffer2DRegion pixelRegion = frame.PixelBuffer.GetRegion(interest); pixelRegion.Clear(); diff --git a/src/ImageSharp/Formats/Gif/GifEncoder.cs b/src/ImageSharp/Formats/Gif/GifEncoder.cs index 6cb8f9d8ce..37b585c618 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoder.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoder.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Gif; /// /// Image encoder for writing image data to a stream in gif format. /// -public sealed class GifEncoder : QuantizingImageEncoder +public sealed class GifEncoder : QuantizingAnimatedImageEncoder { /// /// Gets the color table mode: Global or local. diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 2e05ef782f..797e825dc4 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -54,6 +54,21 @@ internal sealed class GifEncoderCore /// private readonly IPixelSamplingStrategy pixelSamplingStrategy; + /// + /// The default background color of the canvas when animating. + /// This color may be used to fill the unused space on the canvas around the frames, + /// as well as the transparent pixels of the first frame. + /// The background color is also used when a frame disposal mode is . + /// + private readonly Color? backgroundColor; + + /// + /// The number of times any animation is repeated. + /// + private readonly ushort? repeatCount; + + private readonly TransparentColorMode transparentColorMode; + /// /// Initializes a new instance of the class. /// @@ -68,6 +83,9 @@ internal sealed class GifEncoderCore this.hasQuantizer = encoder.Quantizer is not null; this.colorTableMode = encoder.ColorTableMode; this.pixelSamplingStrategy = encoder.PixelSamplingStrategy; + this.backgroundColor = encoder.BackgroundColor; + this.repeatCount = encoder.RepeatCount; + this.transparentColorMode = encoder.TransparentColorMode; } /// @@ -116,18 +134,40 @@ internal sealed class GifEncoderCore } } + // Quantize the first frame. Checking to see whether we can clear the transparent pixels + // to allow for a smaller color palette and encoded result. using (IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration)) { + ImageFrame? clonedFrame = null; + Configuration configuration = this.configuration; + TransparentColorMode mode = this.transparentColorMode; + IPixelSamplingStrategy strategy = this.pixelSamplingStrategy; + if (EncodingUtilities.ShouldClearTransparentPixels(mode)) + { + clonedFrame = image.Frames.RootFrame.Clone(); + + GifFrameMetadata frameMeta = clonedFrame.Metadata.GetGifMetadata(); + Color background = frameMeta.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; + + EncodingUtilities.ClearTransparentPixels(clonedFrame, background); + } + + ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; + if (useGlobalTable) { - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); - quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + frameQuantizer.BuildPalette(configuration, mode, strategy, image); + quantized = frameQuantizer.QuantizeFrame(encodingFrame, image.Bounds); } else { - frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image.Frames.RootFrame); - quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); + frameQuantizer.BuildPalette(configuration, mode, strategy, encodingFrame); + quantized = frameQuantizer.QuantizeFrame(encodingFrame, image.Bounds); } + + clonedFrame?.Dispose(); } // Write the header. @@ -141,9 +181,12 @@ internal sealed class GifEncoderCore frameMetadata.TransparencyIndex = ClampIndex(derivedTransparencyIndex); } - byte backgroundIndex = derivedTransparencyIndex >= 0 - ? frameMetadata.TransparencyIndex - : gifMetadata.BackgroundColorIndex; + if (!TryGetBackgroundIndex(quantized, this.backgroundColor, out byte backgroundIndex)) + { + backgroundIndex = derivedTransparencyIndex >= 0 + ? frameMetadata.TransparencyIndex + : gifMetadata.BackgroundColorIndex; + } // Get the number of bits. int bitDepth = ColorNumerics.GetBitsNeededForColorDepth(quantized.Palette.Length); @@ -161,19 +204,32 @@ internal sealed class GifEncoderCore // Write application extensions. XmpProfile? xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile; - this.WriteApplicationExtensions(stream, image.Frames.Count, gifMetadata.RepeatCount, xmpProfile); + this.WriteApplicationExtensions(stream, image.Frames.Count, this.repeatCount ?? gifMetadata.RepeatCount, xmpProfile); } - this.EncodeFirstFrame(stream, frameMetadata, quantized); - - // Capture the global palette for reuse on subsequent frames and cleanup the quantized frame. - TPixel[] globalPalette = image.Frames.Count == 1 ? [] : quantized.Palette.ToArray(); + // If the token is cancelled during encoding of frames we must ensure the + // quantized frame is disposed. + try + { + this.EncodeFirstFrame(stream, frameMetadata, quantized, cancellationToken); - this.EncodeAdditionalFrames(stream, image, globalPalette, derivedTransparencyIndex, frameMetadata.DisposalMode); + // Capture the global palette for reuse on subsequent frames and cleanup the quantized frame. + TPixel[] globalPalette = image.Frames.Count == 1 ? [] : quantized.Palette.ToArray(); - stream.WriteByte(GifConstants.EndIntroducer); + this.EncodeAdditionalFrames( + stream, + image, + globalPalette, + derivedTransparencyIndex, + frameMetadata.DisposalMode, + cancellationToken); + } + finally + { + stream.WriteByte(GifConstants.EndIntroducer); - quantized?.Dispose(); + quantized?.Dispose(); + } } private static GifFrameMetadata GetGifFrameMetadata(ImageFrame frame, int transparencyIndex) @@ -194,7 +250,8 @@ internal sealed class GifEncoderCore Image image, ReadOnlyMemory globalPalette, int globalTransparencyIndex, - FrameDisposalMode previousDisposalMode) + FrameDisposalMode previousDisposalMode, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { if (image.Frames.Count == 1) @@ -211,51 +268,61 @@ internal sealed class GifEncoderCore // This frame is reused to store de-duplicated pixel buffers. using ImageFrame encodingFrame = new(previousFrame.Configuration, previousFrame.Size); - for (int i = 1; i < image.Frames.Count; i++) + try { - // Gather the metadata for this frame. - ImageFrame currentFrame = image.Frames[i]; - ImageFrame? nextFrame = i < image.Frames.Count - 1 ? image.Frames[i + 1] : null; - GifFrameMetadata gifMetadata = GetGifFrameMetadata(currentFrame, globalTransparencyIndex); - bool useLocal = this.colorTableMode == FrameColorTableMode.Local || (gifMetadata.ColorTableMode == FrameColorTableMode.Local); - - if (!useLocal && !hasPaletteQuantizer && i > 0) + for (int i = 1; i < image.Frames.Count; i++) { - // The palette quantizer can reuse the same global pixel map across multiple frames since the palette is unchanging. - // This allows a reduction of memory usage across multi-frame gifs using a global palette - // and also allows use to reuse the cache from previous runs. - int transparencyIndex = gifMetadata.HasTransparency ? gifMetadata.TransparencyIndex : -1; - paletteQuantizer = new(this.configuration, this.quantizer!.Options, globalPalette, transparencyIndex); - hasPaletteQuantizer = true; - } + cancellationToken.ThrowIfCancellationRequested(); - this.EncodeAdditionalFrame( - stream, - previousFrame, - currentFrame, - nextFrame, - encodingFrame, - useLocal, - gifMetadata, - paletteQuantizer, - previousDisposalMode); + // Gather the metadata for this frame. + ImageFrame currentFrame = image.Frames[i]; + ImageFrame? nextFrame = i < image.Frames.Count - 1 ? image.Frames[i + 1] : null; + GifFrameMetadata gifMetadata = GetGifFrameMetadata(currentFrame, globalTransparencyIndex); + bool useLocal = this.colorTableMode == FrameColorTableMode.Local || (gifMetadata.ColorTableMode == FrameColorTableMode.Local); - previousFrame = currentFrame; - previousDisposalMode = gifMetadata.DisposalMode; - } + if (!useLocal && !hasPaletteQuantizer && i > 0) + { + // The palette quantizer can reuse the same global pixel map across multiple frames since the palette is unchanging. + // This allows a reduction of memory usage across multi-frame gifs using a global palette + // and also allows use to reuse the cache from previous runs. + int transparencyIndex = gifMetadata.HasTransparency ? gifMetadata.TransparencyIndex : -1; + paletteQuantizer = new(this.configuration, this.quantizer!.Options, globalPalette, transparencyIndex); + hasPaletteQuantizer = true; + } - if (hasPaletteQuantizer) + this.EncodeAdditionalFrame( + stream, + previousFrame, + currentFrame, + nextFrame, + encodingFrame, + useLocal, + gifMetadata, + paletteQuantizer, + previousDisposalMode); + + previousFrame = currentFrame; + previousDisposalMode = gifMetadata.DisposalMode; + } + } + finally { - paletteQuantizer.Dispose(); + if (hasPaletteQuantizer) + { + paletteQuantizer.Dispose(); + } } } private void EncodeFirstFrame( Stream stream, GifFrameMetadata metadata, - IndexedImageFrame quantized) + IndexedImageFrame quantized, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { + cancellationToken.ThrowIfCancellationRequested(); + this.WriteGraphicalControlExtension(metadata, stream); Buffer2D indices = ((IPixelSource)quantized).PixelBuffer; @@ -289,7 +356,13 @@ internal sealed class GifEncoderCore // We use it to determine the value to use to replace duplicate pixels. int transparencyIndex = metadata.HasTransparency ? metadata.TransparencyIndex : -1; - ImageFrame? previous = previousDisposalMode == FrameDisposalMode.RestoreToBackground ? null : previousFrame; + ImageFrame? previous = previousDisposalMode == FrameDisposalMode.RestoreToBackground + ? null : + previousFrame; + + Color background = metadata.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; // Deduplicate and quantize the frame capturing only required parts. (bool difference, Rectangle bounds) = @@ -299,9 +372,14 @@ internal sealed class GifEncoderCore currentFrame, nextFrame, encodingFrame, - Color.Transparent, + background, true); + if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + { + EncodingUtilities.ClearTransparentPixels(encodingFrame, background); + } + using IndexedImageFrame quantized = this.QuantizeAdditionalFrameAndUpdateMetadata( encodingFrame, bounds, @@ -428,14 +506,12 @@ internal sealed class GifEncoderCore private static byte ClampIndex(int value) => (byte)Numerics.Clamp(value, byte.MinValue, byte.MaxValue); /// - /// Returns the index of the most transparent color in the palette. + /// Returns the index of the transparent color in the palette. /// /// The current quantized frame. /// The current gif frame metadata. /// The pixel format. - /// - /// The . - /// + /// The . private static int GetTransparentIndex(IndexedImageFrame? quantized, GifFrameMetadata? metadata) where TPixel : unmanaged, IPixel { @@ -463,6 +539,47 @@ internal sealed class GifEncoderCore return index; } + /// + /// Returns the index of the background color in the palette. + /// + /// The current quantized frame. + /// The background color to match. + /// The index in the palette of the background color. + /// The pixel format. + /// The . + private static bool TryGetBackgroundIndex( + IndexedImageFrame? quantized, + Color? background, + out byte index) + where TPixel : unmanaged, IPixel + { + int match = -1; + if (quantized != null && background.HasValue) + { + TPixel backgroundPixel = background.Value.ToPixel(); + ReadOnlySpan palette = quantized.Palette.Span; + for (int i = 0; i < palette.Length; i++) + { + if (!backgroundPixel.Equals(palette[i])) + { + continue; + } + + match = i; + break; + } + } + + if (match >= 0) + { + index = (byte)Numerics.Clamp(match, 0, 255); + return true; + } + + index = 0; + return false; + } + /// /// Writes the file header signature and version to the stream. /// diff --git a/src/ImageSharp/Formats/IAnimatedImageEncoder.cs b/src/ImageSharp/Formats/IAnimatedImageEncoder.cs new file mode 100644 index 0000000000..d2c3ad6907 --- /dev/null +++ b/src/ImageSharp/Formats/IAnimatedImageEncoder.cs @@ -0,0 +1,43 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Defines the contract for all image encoders that allow encoding animation sequences. +/// +public interface IAnimatedImageEncoder +{ + /// + /// Gets the default background color of the canvas when animating in supported encoders. + /// This color may be used to fill the unused space on the canvas around the frames, + /// as well as the transparent pixels of the first frame. + /// The background color is also used when a frame disposal mode is . + /// + Color? BackgroundColor { get; } + + /// + /// Gets the number of times any animation is repeated in supported encoders. + /// + ushort? RepeatCount { get; } + + /// + /// Gets a value indicating whether the root frame is shown as part of the animated sequence in supported encoders. + /// + bool? AnimateRootFrame { get; } +} + +/// +/// Acts as a base class for all image encoders that allow encoding animation sequences. +/// +public abstract class AnimatedImageEncoder : AlphaAwareImageEncoder, IAnimatedImageEncoder +{ + /// + public Color? BackgroundColor { get; init; } + + /// + public ushort? RepeatCount { get; init; } + + /// + public bool? AnimateRootFrame { get; init; } = true; +} diff --git a/src/ImageSharp/Formats/IQuantizingImageEncoder.cs b/src/ImageSharp/Formats/IQuantizingImageEncoder.cs new file mode 100644 index 0000000000..5edf6e40e9 --- /dev/null +++ b/src/ImageSharp/Formats/IQuantizingImageEncoder.cs @@ -0,0 +1,50 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.Processing.Processors.Quantization; + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Defines the contract for all image encoders that allow color palette generation via quantization. +/// +public interface IQuantizingImageEncoder +{ + /// + /// Gets the quantizer used to generate the color palette. + /// + IQuantizer? Quantizer { get; } + + /// + /// Gets the used for quantization when building color palettes. + /// + IPixelSamplingStrategy PixelSamplingStrategy { get; } +} + +/// +/// Acts as a base class for all image encoders that allow color palette generation via quantization. +/// +public abstract class QuantizingImageEncoder : AlphaAwareImageEncoder, IQuantizingImageEncoder +{ + /// + public IQuantizer? Quantizer { get; init; } + + /// + public IPixelSamplingStrategy PixelSamplingStrategy { get; init; } = new DefaultPixelSamplingStrategy(); +} + +/// +/// Acts as a base class for all image encoders that allow color palette generation via quantization when +/// encoding animation sequences. +/// +public abstract class QuantizingAnimatedImageEncoder : QuantizingImageEncoder, IAnimatedImageEncoder +{ + /// + public Color? BackgroundColor { get; } + + /// + public ushort? RepeatCount { get; } + + /// + public bool? AnimateRootFrame { get; } +} diff --git a/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs b/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs index a2d1c01391..62aa705cbe 100644 --- a/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs +++ b/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs @@ -41,13 +41,13 @@ public class IcoFrameMetadata : IFormatFrameMetadata /// Gets or sets the encoding width.
/// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. ///
- public byte EncodingWidth { get; set; } + public byte? EncodingWidth { get; set; } /// /// Gets or sets the encoding height.
/// Can be any number between 0 and 255. Value 0 means a frame height of 256 pixels or greater. ///
- public byte EncodingHeight { get; set; } + public byte? EncodingHeight { get; set; } /// /// Gets or sets the number of bits per pixel.
@@ -73,20 +73,6 @@ public class IcoFrameMetadata : IFormatFrameMetadata }; } - byte encodingWidth = metadata.EncodingWidth switch - { - > 255 => 0, - <= 255 and >= 1 => (byte)metadata.EncodingWidth, - _ => 0 - }; - - byte encodingHeight = metadata.EncodingHeight switch - { - > 255 => 0, - <= 255 and >= 1 => (byte)metadata.EncodingHeight, - _ => 0 - }; - int bpp = metadata.PixelTypeInfo.Value.BitsPerPixel; BmpBitsPerPixel bbpp = bpp switch { @@ -109,8 +95,8 @@ public class IcoFrameMetadata : IFormatFrameMetadata { BmpBitsPerPixel = bbpp, Compression = compression, - EncodingWidth = encodingWidth, - EncodingHeight = encodingHeight, + EncodingWidth = ClampEncodingDimension(metadata.EncodingWidth), + EncodingHeight = ClampEncodingDimension(metadata.EncodingHeight), ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null }; } @@ -131,8 +117,8 @@ public class IcoFrameMetadata : IFormatFrameMetadata { float ratioX = destination.Width / (float)source.Width; float ratioY = destination.Height / (float)source.Height; - this.EncodingWidth = Scale(this.EncodingWidth, destination.Width, ratioX); - this.EncodingHeight = Scale(this.EncodingHeight, destination.Height, ratioY); + this.EncodingWidth = ScaleEncodingDimension(this.EncodingWidth, destination.Width, ratioX); + this.EncodingHeight = ScaleEncodingDimension(this.EncodingHeight, destination.Height, ratioY); } /// @@ -147,7 +133,7 @@ public class IcoFrameMetadata : IFormatFrameMetadata this.EncodingHeight = entry.Height; } - internal IconDirEntry ToIconDirEntry() + internal IconDirEntry ToIconDirEntry(Size size) { byte colorCount = this.Compression == IconFrameCompression.Png || this.BmpBitsPerPixel > BmpBitsPerPixel.Bit8 ? (byte)0 @@ -155,8 +141,8 @@ public class IcoFrameMetadata : IFormatFrameMetadata return new() { - Width = this.EncodingWidth, - Height = this.EncodingHeight, + Width = ClampEncodingDimension(this.EncodingWidth ?? size.Width), + Height = ClampEncodingDimension(this.EncodingHeight ?? size.Height), Planes = 1, ColorCount = colorCount, BitCount = this.Compression switch @@ -228,13 +214,22 @@ public class IcoFrameMetadata : IFormatFrameMetadata }; } - private static byte Scale(byte? value, int destination, float ratio) + private static byte ScaleEncodingDimension(byte? value, int destination, float ratio) { if (value is null) { - return (byte)Math.Clamp(destination, 0, 255); + return ClampEncodingDimension(destination); } - return Math.Min((byte)MathF.Ceiling(value.Value * ratio), (byte)Math.Clamp(destination, 0, 255)); + return ClampEncodingDimension(MathF.Ceiling(value.Value * ratio)); } + + private static byte ClampEncodingDimension(float? dimension) + => dimension switch + { + // Encoding dimensions can be between 0-256 where 0 means 256 or greater. + > 255 => 0, + <= 255 and >= 1 => (byte)dimension, + _ => 0 + }; } diff --git a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs index 4b973d5115..03e01f912f 100644 --- a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs +++ b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs @@ -63,7 +63,6 @@ internal abstract class IconEncoderCore this.entries[i].Entry.ImageOffset = (uint)stream.Position; // We crop the frame to the size specified in the metadata. - // TODO: we can optimize this by cropping the frame only if the new size is both required and different. using Image encodingFrame = new(width, height); for (int y = 0; y < height; y++) { @@ -82,6 +81,8 @@ internal abstract class IconEncoderCore UseDoubleHeight = true, SkipFileHeader = true, SupportTransparency = false, + TransparentColorMode = this.encoder.TransparentColorMode, + PixelSamplingStrategy = this.encoder.PixelSamplingStrategy, BitsPerPixel = encodingMetadata.BmpBitsPerPixel }, IconFrameCompression.Png => new PngEncoder() @@ -90,6 +91,7 @@ internal abstract class IconEncoderCore // https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473 BitDepth = PngBitDepth.Bit8, ColorType = PngColorType.RgbWithAlpha, + TransparentColorMode = this.encoder.TransparentColorMode, CompressionLevel = PngCompressionLevel.BestCompression }, _ => throw new NotSupportedException(), @@ -121,13 +123,13 @@ internal abstract class IconEncoderCore image.Frames.Select(i => { IcoFrameMetadata metadata = i.Metadata.GetIcoMetadata(); - return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry()); + return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry(i.Size)); }).ToArray(), IconFileType.CUR => image.Frames.Select(i => { CurFrameMetadata metadata = i.Metadata.GetCurMetadata(); - return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry()); + return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry(i.Size)); }).ToArray(), _ => throw new NotSupportedException(), }; diff --git a/src/ImageSharp/Formats/ImageEncoder.cs b/src/ImageSharp/Formats/ImageEncoder.cs index fdaa5c35dc..a37a327174 100644 --- a/src/ImageSharp/Formats/ImageEncoder.cs +++ b/src/ImageSharp/Formats/ImageEncoder.cs @@ -51,7 +51,7 @@ public abstract class ImageEncoder : IImageEncoder else { using ChunkedMemoryStream ms = new(configuration.MemoryAllocator); - this.Encode(image, stream, cancellationToken); + this.Encode(image, ms, cancellationToken); ms.Position = 0; ms.CopyTo(stream, configuration.StreamProcessingBufferSize); } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 2320fe1791..707baa1a88 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -16,7 +16,6 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Icc; using SixLabors.ImageSharp.Metadata.Profiles.Iptc; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; -using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg; @@ -1473,7 +1472,7 @@ internal sealed class JpegDecoderCore : ImageDecoderCore, IRawJpegData this.Frame.ComponentOrder[i / 2] = (byte)componentIndex; - IJpegComponent component = this.Frame.Components[componentIndex]; + JpegComponent component = this.Frame.Components[componentIndex]; // 1 byte: Huffman table selectors. // 4 bits - dc diff --git a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs index dddc629b3e..8b379e4d76 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs @@ -17,25 +17,32 @@ internal class BinaryEncoder ///
/// The type of input pixel. /// The configuration. - /// The bytestream to write to. + /// The byte stream to write to. /// The input image. /// The ColorType to use. - /// Data type of the pixles components. - /// + /// Data type of the pixels components. + /// The token to monitor for cancellation requests. + /// /// Thrown if an invalid combination of setting is requested. /// - public static void WritePixels(Configuration configuration, Stream stream, ImageFrame image, PbmColorType colorType, PbmComponentType componentType) + public static void WritePixels( + Configuration configuration, + Stream stream, + ImageFrame image, + PbmColorType colorType, + PbmComponentType componentType, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { if (colorType == PbmColorType.Grayscale) { if (componentType == PbmComponentType.Byte) { - WriteGrayscale(configuration, stream, image); + WriteGrayscale(configuration, stream, image, cancellationToken); } else if (componentType == PbmComponentType.Short) { - WriteWideGrayscale(configuration, stream, image); + WriteWideGrayscale(configuration, stream, image, cancellationToken); } else { @@ -46,31 +53,28 @@ internal class BinaryEncoder { if (componentType == PbmComponentType.Byte) { - WriteRgb(configuration, stream, image); + WriteRgb(configuration, stream, image, cancellationToken); } else if (componentType == PbmComponentType.Short) { - WriteWideRgb(configuration, stream, image); + WriteWideRgb(configuration, stream, image, cancellationToken); } else { throw new ImageFormatException("Component type not supported for Color PBM."); } } - else + else if (componentType == PbmComponentType.Bit) { - if (componentType == PbmComponentType.Bit) - { - WriteBlackAndWhite(configuration, stream, image); - } - else - { - throw new ImageFormatException("Component type not supported for Black & White PBM."); - } + WriteBlackAndWhite(configuration, stream, image, cancellationToken); } } - private static void WriteGrayscale(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteGrayscale( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -82,6 +86,8 @@ internal class BinaryEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL8Bytes( @@ -94,7 +100,11 @@ internal class BinaryEncoder } } - private static void WriteWideGrayscale(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteWideGrayscale( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { const int bytesPerPixel = 2; @@ -107,6 +117,8 @@ internal class BinaryEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL16Bytes( @@ -119,7 +131,11 @@ internal class BinaryEncoder } } - private static void WriteRgb(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteRgb( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { const int bytesPerPixel = 3; @@ -132,6 +148,8 @@ internal class BinaryEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToRgb24Bytes( @@ -144,7 +162,11 @@ internal class BinaryEncoder } } - private static void WriteWideRgb(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteWideRgb( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { const int bytesPerPixel = 6; @@ -157,6 +179,8 @@ internal class BinaryEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToRgb48Bytes( @@ -169,7 +193,12 @@ internal class BinaryEncoder } } - private static void WriteBlackAndWhite(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteBlackAndWhite( + Configuration + configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -181,6 +210,8 @@ internal class BinaryEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL8( diff --git a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs index 843f1880e6..e0330ca6b4 100644 --- a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs @@ -68,8 +68,7 @@ internal sealed class PbmEncoderCore byte signature = this.DeduceSignature(); this.WriteHeader(stream, signature, image.Size); - - this.WritePixels(stream, image.Frames.RootFrame); + this.WritePixels(stream, image.Frames.RootFrame, cancellationToken); stream.Flush(); } @@ -167,16 +166,29 @@ internal sealed class PbmEncoderCore /// /// The containing pixel data. /// - private void WritePixels(Stream stream, ImageFrame image) + /// The token to monitor for cancellation requests. + private void WritePixels(Stream stream, ImageFrame image, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { if (this.encoding == PbmEncoding.Plain) { - PlainEncoder.WritePixels(this.configuration, stream, image, this.colorType, this.componentType); + PlainEncoder.WritePixels( + this.configuration, + stream, + image, + this.colorType, + this.componentType, + cancellationToken); } else { - BinaryEncoder.WritePixels(this.configuration, stream, image, this.colorType, this.componentType); + BinaryEncoder.WritePixels( + this.configuration, + stream, + image, + this.colorType, + this.componentType, + cancellationToken); } } } diff --git a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs index 29260f54aa..bab508720d 100644 --- a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm; /// /// Pixel encoding methods for the PBM plain encoding. /// -internal class PlainEncoder +internal static class PlainEncoder { private const byte NewLine = 0x0a; private const byte Space = 0x20; @@ -31,45 +31,56 @@ internal class PlainEncoder /// /// The type of input pixel. /// The configuration. - /// The bytestream to write to. + /// The byte stream to write to. /// The input image. /// The ColorType to use. - /// Data type of the pixles components. - public static void WritePixels(Configuration configuration, Stream stream, ImageFrame image, PbmColorType colorType, PbmComponentType componentType) + /// Data type of the pixels components. + /// The token to monitor for cancellation requests. + public static void WritePixels( + Configuration configuration, + Stream stream, + ImageFrame image, + PbmColorType colorType, + PbmComponentType componentType, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { if (colorType == PbmColorType.Grayscale) { if (componentType == PbmComponentType.Byte) { - WriteGrayscale(configuration, stream, image); + WriteGrayscale(configuration, stream, image, cancellationToken); } else { - WriteWideGrayscale(configuration, stream, image); + WriteWideGrayscale(configuration, stream, image, cancellationToken); } } else if (colorType == PbmColorType.Rgb) { if (componentType == PbmComponentType.Byte) { - WriteRgb(configuration, stream, image); + WriteRgb(configuration, stream, image, cancellationToken); } else { - WriteWideRgb(configuration, stream, image); + WriteWideRgb(configuration, stream, image, cancellationToken); } } else { - WriteBlackAndWhite(configuration, stream, image); + WriteBlackAndWhite(configuration, stream, image, cancellationToken); } // Write EOF indicator, as some encoders expect it. stream.WriteByte(Space); } - private static void WriteGrayscale(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteGrayscale( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -83,6 +94,8 @@ internal class PlainEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL8( configuration, @@ -102,7 +115,11 @@ internal class PlainEncoder } } - private static void WriteWideGrayscale(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteWideGrayscale( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -116,6 +133,8 @@ internal class PlainEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL16( configuration, @@ -135,7 +154,11 @@ internal class PlainEncoder } } - private static void WriteRgb(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteRgb( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -149,6 +172,8 @@ internal class PlainEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToRgb24( configuration, @@ -174,7 +199,11 @@ internal class PlainEncoder } } - private static void WriteWideRgb(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteWideRgb( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -188,6 +217,8 @@ internal class PlainEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToRgb48( configuration, @@ -213,7 +244,11 @@ internal class PlainEncoder } } - private static void WriteBlackAndWhite(Configuration configuration, Stream stream, ImageFrame image) + private static void WriteBlackAndWhite( + Configuration configuration, + Stream stream, + ImageFrame image, + CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { int width = image.Width; @@ -227,6 +262,8 @@ internal class PlainEncoder for (int y = 0; y < height; y++) { + cancellationToken.ThrowIfCancellationRequested(); + Span pixelSpan = pixelBuffer.DangerousGetRowSpan(y); PixelOperations.Instance.ToL8( configuration, @@ -236,8 +273,7 @@ internal class PlainEncoder int written = 0; for (int x = 0; x < width; x++) { - byte value = (rowSpan[x].PackedValue < 128) ? One : Zero; - plainSpan[written++] = value; + plainSpan[written++] = (rowSpan[x].PackedValue < 128) ? One : Zero; plainSpan[written++] = Space; } diff --git a/src/ImageSharp/Formats/Png/PngEncoder.cs b/src/ImageSharp/Formats/Png/PngEncoder.cs index dcbaf3140d..63e675b505 100644 --- a/src/ImageSharp/Formats/Png/PngEncoder.cs +++ b/src/ImageSharp/Formats/Png/PngEncoder.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using SixLabors.ImageSharp.Processing.Processors.Quantization; @@ -9,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Png; /// /// Image encoder for writing image data to a stream in png format. /// -public class PngEncoder : QuantizingImageEncoder +public class PngEncoder : QuantizingAnimatedImageEncoder { /// /// Initializes a new instance of the class. @@ -69,12 +68,6 @@ public class PngEncoder : QuantizingImageEncoder /// public PngChunkFilter? ChunkFilter { get; init; } - /// - /// Gets a value indicating whether fully transparent pixels that may contain R, G, B values which are not 0, - /// should be converted to transparent black, which can yield in better compression in some cases. - /// - public PngTransparentColorMode TransparentColorMode { get; init; } - /// protected override void Encode(Image image, Stream stream, CancellationToken cancellationToken) { diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 978b9184e9..ea36d9fe1e 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -7,6 +7,7 @@ using System.IO.Hashing; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; using SixLabors.ImageSharp.Common.Helpers; using SixLabors.ImageSharp.Compression.Zlib; using SixLabors.ImageSharp.Formats.Png.Chunks; @@ -123,6 +124,24 @@ internal sealed class PngEncoderCore : IDisposable /// private int derivedTransparencyIndex = -1; + /// + /// The default background color of the canvas when animating. + /// This color may be used to fill the unused space on the canvas around the frames, + /// as well as the transparent pixels of the first frame. + /// The background color is also used when a frame disposal mode is . + /// + private readonly Color? backgroundColor; + + /// + /// The number of times any animation is repeated. + /// + private readonly ushort? repeatCount; + + /// + /// Whether the root frame is shown as part of the animated sequence. + /// + private readonly bool? animateRootFrame; + /// /// A reusable Crc32 hashing instance. /// @@ -139,6 +158,9 @@ internal sealed class PngEncoderCore : IDisposable this.memoryAllocator = configuration.MemoryAllocator; this.encoder = encoder; this.quantizer = encoder.Quantizer; + this.backgroundColor = encoder.BackgroundColor; + this.repeatCount = encoder.RepeatCount; + this.animateRootFrame = encoder.AnimateRootFrame; } /// @@ -167,18 +189,18 @@ internal sealed class PngEncoderCore : IDisposable ImageFrame currentFrame = image.Frames.RootFrame; int currentFrameIndex = 0; - bool clearTransparency = this.encoder.TransparentColorMode is PngTransparentColorMode.Clear; + bool clearTransparency = EncodingUtilities.ShouldClearTransparentPixels(this.encoder.TransparentColorMode); if (clearTransparency) { currentFrame = clonedFrame = currentFrame.Clone(); - ClearTransparentPixels(currentFrame); + EncodingUtilities.ClearTransparentPixels(currentFrame, Color.Transparent); } // Do not move this. We require an accurate bit depth for the header chunk. IndexedImageFrame? quantized = this.CreateQuantizedImageAndUpdateBitDepth( pngMetadata, currentFrame, - currentFrame.Bounds(), + currentFrame.Bounds, null); this.WriteHeaderChunk(stream); @@ -194,89 +216,104 @@ internal sealed class PngEncoderCore : IDisposable if (image.Frames.Count > 1) { - this.WriteAnimationControlChunk(stream, (uint)(image.Frames.Count - (pngMetadata.AnimateRootFrame ? 0 : 1)), pngMetadata.RepeatCount); + this.WriteAnimationControlChunk( + stream, + (uint)(image.Frames.Count - (pngMetadata.AnimateRootFrame ? 0 : 1)), + this.repeatCount ?? pngMetadata.RepeatCount); } // If the first frame isn't animated, write it as usual and skip it when writing animated frames - if (!pngMetadata.AnimateRootFrame || image.Frames.Count == 1) + bool userAnimateRootFrame = this.animateRootFrame == true; + if ((!userAnimateRootFrame && !pngMetadata.AnimateRootFrame) || image.Frames.Count == 1) { + cancellationToken.ThrowIfCancellationRequested(); FrameControl frameControl = new((uint)this.width, (uint)this.height); this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, false); currentFrameIndex++; } - if (image.Frames.Count > 1) + try { - // Write the first animated frame. - currentFrame = image.Frames[currentFrameIndex]; - PngFrameMetadata frameMetadata = currentFrame.Metadata.GetPngMetadata(); - FrameDisposalMode previousDisposal = frameMetadata.DisposalMode; - FrameControl frameControl = this.WriteFrameControlChunk(stream, frameMetadata, currentFrame.Bounds(), 0); - uint sequenceNumber = 1; - if (pngMetadata.AnimateRootFrame) - { - this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, false); - } - else + if (image.Frames.Count > 1) { - sequenceNumber += this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, true); - } + // Write the first animated frame. + currentFrame = image.Frames[currentFrameIndex]; + PngFrameMetadata frameMetadata = currentFrame.Metadata.GetPngMetadata(); + FrameDisposalMode previousDisposal = frameMetadata.DisposalMode; + FrameControl frameControl = this.WriteFrameControlChunk(stream, frameMetadata, currentFrame.Bounds, 0); + uint sequenceNumber = 1; + if (pngMetadata.AnimateRootFrame) + { + this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, false); + } + else + { + sequenceNumber += this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, true); + } - currentFrameIndex++; + currentFrameIndex++; - // Capture the global palette for reuse on subsequent frames. - ReadOnlyMemory? previousPalette = quantized?.Palette.ToArray(); + // Capture the global palette for reuse on subsequent frames. + ReadOnlyMemory? previousPalette = quantized?.Palette.ToArray(); - // Write following frames. - ImageFrame previousFrame = image.Frames.RootFrame; + // Write following frames. + ImageFrame previousFrame = image.Frames.RootFrame; - // This frame is reused to store de-duplicated pixel buffers. - using ImageFrame encodingFrame = new(image.Configuration, previousFrame.Size); + // This frame is reused to store de-duplicated pixel buffers. + using ImageFrame encodingFrame = new(image.Configuration, previousFrame.Size); - for (; currentFrameIndex < image.Frames.Count; currentFrameIndex++) - { - ImageFrame? prev = previousDisposal == FrameDisposalMode.RestoreToBackground ? null : previousFrame; - currentFrame = image.Frames[currentFrameIndex]; - ImageFrame? nextFrame = currentFrameIndex < image.Frames.Count - 1 ? image.Frames[currentFrameIndex + 1] : null; - - frameMetadata = currentFrame.Metadata.GetPngMetadata(); - bool blend = frameMetadata.BlendMode == FrameBlendMode.Over; - - (bool difference, Rectangle bounds) = - AnimationUtilities.DeDuplicatePixels( - image.Configuration, - prev, - currentFrame, - nextFrame, - encodingFrame, - Color.Transparent, - blend); - - if (clearTransparency) + for (; currentFrameIndex < image.Frames.Count; currentFrameIndex++) { - ClearTransparentPixels(encodingFrame); - } + cancellationToken.ThrowIfCancellationRequested(); + + ImageFrame? prev = previousDisposal == FrameDisposalMode.RestoreToBackground ? null : previousFrame; + currentFrame = image.Frames[currentFrameIndex]; + ImageFrame? nextFrame = currentFrameIndex < image.Frames.Count - 1 ? image.Frames[currentFrameIndex + 1] : null; + + frameMetadata = currentFrame.Metadata.GetPngMetadata(); + bool blend = frameMetadata.BlendMode == FrameBlendMode.Over; + Color background = frameMetadata.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; + + (bool difference, Rectangle bounds) = + AnimationUtilities.DeDuplicatePixels( + image.Configuration, + prev, + currentFrame, + nextFrame, + encodingFrame, + background, + blend); + + if (clearTransparency) + { + EncodingUtilities.ClearTransparentPixels(encodingFrame, background); + } - // Each frame control sequence number must be incremented by the number of frame data chunks that follow. - frameControl = this.WriteFrameControlChunk(stream, frameMetadata, bounds, sequenceNumber); + // Each frame control sequence number must be incremented by the number of frame data chunks that follow. + frameControl = this.WriteFrameControlChunk(stream, frameMetadata, bounds, sequenceNumber); - // Dispose of previous quantized frame and reassign. - quantized?.Dispose(); - quantized = this.CreateQuantizedImageAndUpdateBitDepth(pngMetadata, encodingFrame, bounds, previousPalette); - sequenceNumber += this.WriteDataChunks(frameControl, encodingFrame.PixelBuffer.GetRegion(bounds), quantized, stream, true) + 1; + // Dispose of previous quantized frame and reassign. + quantized?.Dispose(); + quantized = this.CreateQuantizedImageAndUpdateBitDepth(pngMetadata, encodingFrame, bounds, previousPalette); + sequenceNumber += this.WriteDataChunks(frameControl, encodingFrame.PixelBuffer.GetRegion(bounds), quantized, stream, true) + 1; - previousFrame = currentFrame; - previousDisposal = frameMetadata.DisposalMode; + previousFrame = currentFrame; + previousDisposal = frameMetadata.DisposalMode; + } } - } - this.WriteEndChunk(stream); + this.WriteEndChunk(stream); - stream.Flush(); - - // Dispose of allocations from final frame. - clonedFrame?.Dispose(); - quantized?.Dispose(); + stream.Flush(); + } + finally + { + // Dispose of allocations from final frame. + clonedFrame?.Dispose(); + quantized?.Dispose(); + } } /// @@ -286,32 +323,6 @@ internal sealed class PngEncoderCore : IDisposable this.currentScanline?.Dispose(); } - /// - /// Convert transparent pixels, to transparent black pixels, which can yield to better compression in some cases. - /// - /// The type of the pixel. - /// The cloned image frame where the transparent pixels will be changed. - private static void ClearTransparentPixels(ImageFrame clone) - where TPixel : unmanaged, IPixel - => clone.ProcessPixelRows(accessor => - { - // TODO: We should be able to speed this up with SIMD and masking. - Rgba32 transparent = Color.Transparent.ToPixel(); - for (int y = 0; y < accessor.Height; y++) - { - Span span = accessor.GetRowSpan(y); - for (int x = 0; x < accessor.Width; x++) - { - ref TPixel pixel = ref span[x]; - Rgba32 rgba = pixel.ToRgba32(); - if (rgba.A is 0) - { - pixel = TPixel.FromRgba32(transparent); - } - } - } - }); - /// /// Creates the quantized image and calculates and sets the bit depth. /// diff --git a/src/ImageSharp/Formats/Png/PngTransparentColorMode.cs b/src/ImageSharp/Formats/Png/PngTransparentColorMode.cs deleted file mode 100644 index 76a89608bd..0000000000 --- a/src/ImageSharp/Formats/Png/PngTransparentColorMode.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -namespace SixLabors.ImageSharp.Formats.Png; - -/// -/// Enum indicating how the transparency should be handled on encoding. -/// -public enum PngTransparentColorMode -{ - /// - /// The transparency will be kept as is. - /// - Preserve = 0, - - /// - /// Converts fully transparent pixels that may contain R, G, B values which are not 0, - /// to transparent black, which can yield in better compression in some cases. - /// - Clear = 1, -} diff --git a/src/ImageSharp/Formats/Qoi/QoiEncoder.cs b/src/ImageSharp/Formats/Qoi/QoiEncoder.cs index b9c2078b3f..1da9caffb5 100644 --- a/src/ImageSharp/Formats/Qoi/QoiEncoder.cs +++ b/src/ImageSharp/Formats/Qoi/QoiEncoder.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Qoi; /// /// Image encoder for writing an image to a stream as a QOI image /// -public class QoiEncoder : ImageEncoder +public class QoiEncoder : AlphaAwareImageEncoder { /// /// Gets the color channels on the image that can be diff --git a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs index 88d87a3825..872cec3fd0 100644 --- a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs +++ b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs @@ -55,7 +55,7 @@ internal class QoiEncoderCore Guard.NotNull(stream, nameof(stream)); this.WriteHeader(image, stream); - this.WritePixels(image, stream); + this.WritePixels(image, stream, cancellationToken); WriteEndOfStream(stream); stream.Flush(); } @@ -78,7 +78,7 @@ internal class QoiEncoderCore stream.WriteByte((byte)qoiColorSpace); } - private void WritePixels(Image image, Stream stream) + private void WritePixels(Image image, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { // Start image encoding @@ -86,137 +86,156 @@ internal class QoiEncoderCore Span previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan(); Rgba32 previousPixel = new(0, 0, 0, 255); Rgba32 currentRgba32 = default; - Buffer2D pixels = image.Frames[0].PixelBuffer; - using IMemoryOwner rgbaRowBuffer = this.memoryAllocator.Allocate(pixels.Width); - Span rgbaRow = rgbaRowBuffer.GetSpan(); - for (int i = 0; i < pixels.Height; i++) + ImageFrame? clonedFrame = null; + try { - Span row = pixels.DangerousGetRowSpan(i); - PixelOperations.Instance.ToRgba32(this.configuration, row, rgbaRow); - for (int j = 0; j < row.Length && i < pixels.Height; j++) + if (EncodingUtilities.ShouldClearTransparentPixels(this.encoder.TransparentColorMode)) { - // We get the RGBA value from pixels - currentRgba32 = rgbaRow[j]; + clonedFrame = image.Frames.RootFrame.Clone(); + EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + } + + ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; + Buffer2D pixels = encodingFrame.PixelBuffer; + + using IMemoryOwner rgbaRowBuffer = this.memoryAllocator.Allocate(pixels.Width); + Span rgbaRow = rgbaRowBuffer.GetSpan(); + Configuration configuration = this.configuration; + for (int i = 0; i < pixels.Height; i++) + { + cancellationToken.ThrowIfCancellationRequested(); - // First, we check if the current pixel is equal to the previous one - // If so, we do a QOI_OP_RUN - if (currentRgba32.Equals(previousPixel)) + Span row = pixels.DangerousGetRowSpan(i); + PixelOperations.Instance.ToRgba32(this.configuration, row, rgbaRow); + for (int j = 0; j < row.Length && i < pixels.Height; j++) { - /* It looks like this isn't an error, but this makes possible that - * files start with a QOI_OP_RUN if their first pixel is a fully opaque - * black. However, the decoder of this project takes that into consideration - * - * To further details, see https://github.com/phoboslab/qoi/issues/258, - * and we should discuss what to do about this approach and - * if it's correct - */ - int repetitions = 0; - do + // We get the RGBA value from pixels + currentRgba32 = rgbaRow[j]; + + // First, we check if the current pixel is equal to the previous one + // If so, we do a QOI_OP_RUN + if (currentRgba32.Equals(previousPixel)) { - repetitions++; - j++; - if (j == row.Length) + /* It looks like this isn't an error, but this makes possible that + * files start with a QOI_OP_RUN if their first pixel is a fully opaque + * black. However, the decoder of this project takes that into consideration + * + * To further details, see https://github.com/phoboslab/qoi/issues/258, + * and we should discuss what to do about this approach and + * if it's correct + */ + int repetitions = 0; + do { - j = 0; - i++; - if (i == pixels.Height) + repetitions++; + j++; + if (j == row.Length) { - break; + j = 0; + i++; + if (i == pixels.Height) + { + break; + } + + row = pixels.DangerousGetRowSpan(i); + PixelOperations.Instance.ToRgba32(configuration, row, rgbaRow); } - row = pixels.DangerousGetRowSpan(i); - PixelOperations.Instance.ToRgba32(this.configuration, row, rgbaRow); + currentRgba32 = rgbaRow[j]; } + while (currentRgba32.Equals(previousPixel) && repetitions < 62); - currentRgba32 = rgbaRow[j]; - } - while (currentRgba32.Equals(previousPixel) && repetitions < 62); - - j--; - stream.WriteByte((byte)((int)QoiChunk.QoiOpRun | (repetitions - 1))); + j--; + stream.WriteByte((byte)((int)QoiChunk.QoiOpRun | (repetitions - 1))); - /* If it's a QOI_OP_RUN, we don't overwrite the previous pixel since - * it will be taken and compared on the next iteration - */ - continue; - } + /* If it's a QOI_OP_RUN, we don't overwrite the previous pixel since + * it will be taken and compared on the next iteration + */ + continue; + } - // else, we check if it exists in the previously seen pixels - // If so, we do a QOI_OP_INDEX - int pixelArrayPosition = GetArrayPosition(currentRgba32); - if (previouslySeenPixels[pixelArrayPosition].Equals(currentRgba32)) - { - stream.WriteByte((byte)pixelArrayPosition); - } - else - { - // else, we check if the difference is less than -2..1 - // Since it wasn't found on the previously seen pixels, we save it - previouslySeenPixels[pixelArrayPosition] = currentRgba32; - - int diffRed = currentRgba32.R - previousPixel.R; - int diffGreen = currentRgba32.G - previousPixel.G; - int diffBlue = currentRgba32.B - previousPixel.B; - - // If so, we do a QOI_OP_DIFF - if (diffRed is >= -2 and <= 1 && - diffGreen is >= -2 and <= 1 && - diffBlue is >= -2 and <= 1 && - currentRgba32.A == previousPixel.A) + // else, we check if it exists in the previously seen pixels + // If so, we do a QOI_OP_INDEX + int pixelArrayPosition = GetArrayPosition(currentRgba32); + if (previouslySeenPixels[pixelArrayPosition].Equals(currentRgba32)) { - // Bottom limit is -2, so we add 2 to make it equal to 0 - int dr = diffRed + 2; - int dg = diffGreen + 2; - int db = diffBlue + 2; - byte valueToWrite = (byte)((int)QoiChunk.QoiOpDiff | (dr << 4) | (dg << 2) | db); - stream.WriteByte(valueToWrite); + stream.WriteByte((byte)pixelArrayPosition); } else { - // else, we check if the green difference is less than -32..31 and the rest -8..7 - // If so, we do a QOI_OP_LUMA - int diffRedGreen = diffRed - diffGreen; - int diffBlueGreen = diffBlue - diffGreen; - if (diffGreen is >= -32 and <= 31 && - diffRedGreen is >= -8 and <= 7 && - diffBlueGreen is >= -8 and <= 7 && + // else, we check if the difference is less than -2..1 + // Since it wasn't found on the previously seen pixels, we save it + previouslySeenPixels[pixelArrayPosition] = currentRgba32; + + int diffRed = currentRgba32.R - previousPixel.R; + int diffGreen = currentRgba32.G - previousPixel.G; + int diffBlue = currentRgba32.B - previousPixel.B; + + // If so, we do a QOI_OP_DIFF + if (diffRed is >= -2 and <= 1 && + diffGreen is >= -2 and <= 1 && + diffBlue is >= -2 and <= 1 && currentRgba32.A == previousPixel.A) { - int dr_dg = diffRedGreen + 8; - int db_dg = diffBlueGreen + 8; - byte byteToWrite1 = (byte)((int)QoiChunk.QoiOpLuma | (diffGreen + 32)); - byte byteToWrite2 = (byte)((dr_dg << 4) | db_dg); - stream.WriteByte(byteToWrite1); - stream.WriteByte(byteToWrite2); + // Bottom limit is -2, so we add 2 to make it equal to 0 + int dr = diffRed + 2; + int dg = diffGreen + 2; + int db = diffBlue + 2; + byte valueToWrite = (byte)((int)QoiChunk.QoiOpDiff | (dr << 4) | (dg << 2) | db); + stream.WriteByte(valueToWrite); } else { - // else, we check if the alpha is equal to the previous pixel - // If so, we do a QOI_OP_RGB - if (currentRgba32.A == previousPixel.A) + // else, we check if the green difference is less than -32..31 and the rest -8..7 + // If so, we do a QOI_OP_LUMA + int diffRedGreen = diffRed - diffGreen; + int diffBlueGreen = diffBlue - diffGreen; + if (diffGreen is >= -32 and <= 31 && + diffRedGreen is >= -8 and <= 7 && + diffBlueGreen is >= -8 and <= 7 && + currentRgba32.A == previousPixel.A) { - stream.WriteByte((byte)QoiChunk.QoiOpRgb); - stream.WriteByte(currentRgba32.R); - stream.WriteByte(currentRgba32.G); - stream.WriteByte(currentRgba32.B); + int dr_dg = diffRedGreen + 8; + int db_dg = diffBlueGreen + 8; + byte byteToWrite1 = (byte)((int)QoiChunk.QoiOpLuma | (diffGreen + 32)); + byte byteToWrite2 = (byte)((dr_dg << 4) | db_dg); + stream.WriteByte(byteToWrite1); + stream.WriteByte(byteToWrite2); } else { - // else, we do a QOI_OP_RGBA - stream.WriteByte((byte)QoiChunk.QoiOpRgba); - stream.WriteByte(currentRgba32.R); - stream.WriteByte(currentRgba32.G); - stream.WriteByte(currentRgba32.B); - stream.WriteByte(currentRgba32.A); + // else, we check if the alpha is equal to the previous pixel + // If so, we do a QOI_OP_RGB + if (currentRgba32.A == previousPixel.A) + { + stream.WriteByte((byte)QoiChunk.QoiOpRgb); + stream.WriteByte(currentRgba32.R); + stream.WriteByte(currentRgba32.G); + stream.WriteByte(currentRgba32.B); + } + else + { + // else, we do a QOI_OP_RGBA + stream.WriteByte((byte)QoiChunk.QoiOpRgba); + stream.WriteByte(currentRgba32.R); + stream.WriteByte(currentRgba32.G); + stream.WriteByte(currentRgba32.B); + stream.WriteByte(currentRgba32.A); + } } } } - } - previousPixel = currentRgba32; + previousPixel = currentRgba32; + } } } + finally + { + clonedFrame?.Dispose(); + } } private static void WriteEndOfStream(Stream stream) diff --git a/src/ImageSharp/Formats/QuantizingImageEncoder.cs b/src/ImageSharp/Formats/QuantizingImageEncoder.cs deleted file mode 100644 index 330d8988c7..0000000000 --- a/src/ImageSharp/Formats/QuantizingImageEncoder.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -using SixLabors.ImageSharp.Processing.Processors.Quantization; - -namespace SixLabors.ImageSharp.Formats; - -/// -/// Acts as a base class for all image encoders that allow color palette generation via quantization. -/// -public abstract class QuantizingImageEncoder : ImageEncoder -{ - /// - /// Gets the quantizer used to generate the color palette. - /// - public IQuantizer? Quantizer { get; init; } - - /// - /// Gets the used for quantization when building color palettes. - /// - public IPixelSamplingStrategy PixelSamplingStrategy { get; init; } = new DefaultPixelSamplingStrategy(); -} diff --git a/src/ImageSharp/Formats/Tga/TgaEncoder.cs b/src/ImageSharp/Formats/Tga/TgaEncoder.cs index 09b12e6081..a4630a464b 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoder.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoder.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Tga; /// /// Image encoder for writing an image to a stream as a Targa true-vision image. /// -public sealed class TgaEncoder : ImageEncoder +public sealed class TgaEncoder : AlphaAwareImageEncoder { /// /// Gets the number of bits per pixel. diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index 1e05a9f716..e2ea9c4fe7 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -29,6 +29,8 @@ internal sealed class TgaEncoderCore /// private readonly TgaCompression compression; + private readonly TransparentColorMode transparentColorMode; + /// /// Initializes a new instance of the class. /// @@ -39,6 +41,7 @@ internal sealed class TgaEncoderCore this.memoryAllocator = memoryAllocator; this.bitsPerPixel = encoder.BitsPerPixel; this.compression = encoder.Compression; + this.transparentColorMode = encoder.TransparentColorMode; } /// @@ -103,16 +106,33 @@ internal sealed class TgaEncoderCore fileHeader.WriteTo(buffer); stream.Write(buffer, 0, TgaFileHeader.Size); - if (this.compression is TgaCompression.RunLength) + + ImageFrame? clonedFrame = null; + try { - this.WriteRunLengthEncodedImage(stream, image.Frames.RootFrame, cancellationToken); + if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + { + clonedFrame = image.Frames.RootFrame.Clone(); + EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + } + + ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; + + if (this.compression is TgaCompression.RunLength) + { + this.WriteRunLengthEncodedImage(stream, encodingFrame, cancellationToken); + } + else + { + this.WriteImage(image.Configuration, stream, encodingFrame, cancellationToken); + } + + stream.Flush(); } - else + finally { - this.WriteImage(image.Configuration, stream, image.Frames.RootFrame, cancellationToken); + clonedFrame?.Dispose(); } - - stream.Flush(); } /// diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index b560067f3f..da55ef9f9b 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -49,6 +49,11 @@ internal sealed class TiffEncoderCore /// private readonly DeflateCompressionLevel compressionLevel; + /// + /// The transparent color mode to use when encoding. + /// + private readonly TransparentColorMode transparentColorMode; + /// /// Whether to skip metadata during encoding. /// @@ -59,20 +64,21 @@ internal sealed class TiffEncoderCore /// /// Initializes a new instance of the class. /// - /// The options for the encoder. + /// The options for the encoder. /// The global configuration. - public TiffEncoderCore(TiffEncoder options, Configuration configuration) + public TiffEncoderCore(TiffEncoder encoder, Configuration configuration) { this.configuration = configuration; this.memoryAllocator = configuration.MemoryAllocator; - this.PhotometricInterpretation = options.PhotometricInterpretation; - this.quantizer = options.Quantizer ?? KnownQuantizers.Octree; - this.pixelSamplingStrategy = options.PixelSamplingStrategy; - this.BitsPerPixel = options.BitsPerPixel; - this.HorizontalPredictor = options.HorizontalPredictor; - this.CompressionType = options.Compression; - this.compressionLevel = options.CompressionLevel ?? DeflateCompressionLevel.DefaultCompression; - this.skipMetadata = options.SkipMetadata; + this.PhotometricInterpretation = encoder.PhotometricInterpretation; + this.quantizer = encoder.Quantizer ?? KnownQuantizers.Octree; + this.pixelSamplingStrategy = encoder.PixelSamplingStrategy; + this.BitsPerPixel = encoder.BitsPerPixel; + this.HorizontalPredictor = encoder.HorizontalPredictor; + this.CompressionType = encoder.Compression; + this.compressionLevel = encoder.CompressionLevel ?? DeflateCompressionLevel.DefaultCompression; + this.skipMetadata = encoder.SkipMetadata; + this.transparentColorMode = encoder.TransparentColorMode; } /// @@ -131,14 +137,30 @@ internal sealed class TiffEncoderCore long ifdMarker = WriteHeader(writer, buffer); - Image? metadataImage = image; + Image? imageMetadata = image; foreach (ImageFrame frame in image.Frames) { - cancellationToken.ThrowIfCancellationRequested(); + ImageFrame? clonedFrame = null; + try + { + cancellationToken.ThrowIfCancellationRequested(); - ifdMarker = this.WriteFrame(writer, frame, image.Metadata, metadataImage, this.BitsPerPixel.Value, this.CompressionType.Value, ifdMarker); - metadataImage = null; + if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + { + clonedFrame = frame.Clone(); + EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + } + + ImageFrame encodingFrame = clonedFrame ?? frame; + + ifdMarker = this.WriteFrame(writer, encodingFrame, image.Metadata, imageMetadata, this.BitsPerPixel.Value, this.CompressionType.Value, ifdMarker); + imageMetadata = null; + } + finally + { + clonedFrame?.Dispose(); + } } long currentOffset = writer.BaseStream.Position; diff --git a/src/ImageSharp/Formats/TransparentColorMode.cs b/src/ImageSharp/Formats/TransparentColorMode.cs new file mode 100644 index 0000000000..39986b5024 --- /dev/null +++ b/src/ImageSharp/Formats/TransparentColorMode.cs @@ -0,0 +1,22 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +namespace SixLabors.ImageSharp.Formats; + +/// +/// Specifies how transparent pixels should be handled during encoding. +/// +public enum TransparentColorMode +{ + /// + /// Retains the original color values of transparent pixels. + /// + Preserve = 0, + + /// + /// Converts transparent pixels with non-zero color components + /// to fully transparent pixels (all components set to zero), + /// which may improve compression. + /// + Clear = 1, +} diff --git a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs index eccd9ede8e..a9e63a3d0e 100644 --- a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs @@ -183,7 +183,7 @@ internal class AlphaDecoder : IDisposable else { this.LosslessDecoder.DecodeImageData(this.Vp8LDec, this.Vp8LDec.Pixels.Memory.Span); - this.ExtractAlphaRows(this.Vp8LDec); + this.ExtractAlphaRows(this.Vp8LDec, this.Width); } } @@ -257,14 +257,15 @@ internal class AlphaDecoder : IDisposable /// Once the image-stream is decoded into ARGB color values, the transparency information will be extracted from the green channel of the ARGB quadruplet. /// /// The VP8L decoder. - private void ExtractAlphaRows(Vp8LDecoder dec) + /// The image width. + private void ExtractAlphaRows(Vp8LDecoder dec, int width) { int numRowsToProcess = dec.Height; - int width = dec.Width; Span input = dec.Pixels.Memory.Span; Span output = this.Alpha.Memory.Span; // Extract alpha (which is stored in the green plane). + // the final width (!= dec->width_) int pixelCount = width * numRowsToProcess; WebpLosslessDecoder.ApplyInverseTransforms(dec, input, this.memoryAllocator); ExtractGreen(input, output, pixelCount); diff --git a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs index 46030dde32..fd6f508e4a 100644 --- a/src/ImageSharp/Formats/Webp/AlphaEncoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaEncoder.cs @@ -49,7 +49,7 @@ internal static class AlphaEncoder quality, skipMetadata, effort, - WebpTransparentColorMode.Preserve, + TransparentColorMode.Preserve, false, 0); diff --git a/src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs b/src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs index 024adb7c23..5287f0b753 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs @@ -269,7 +269,11 @@ internal static unsafe class LosslessUtils /// /// The transform data contains color table size and the entries in the color table. /// The pixel data to apply the reverse transform on. - public static void ColorIndexInverseTransform(Vp8LTransform transform, Span pixelData) + /// The resulting pixel data with the reversed transformation data. + public static void ColorIndexInverseTransform( + Vp8LTransform transform, + Span pixelData, + Span outputSpan) { int bitsPerPixel = 8 >> transform.Bits; int width = transform.XSize; @@ -282,7 +286,6 @@ internal static unsafe class LosslessUtils int countMask = pixelsPerByte - 1; int bitMask = (1 << bitsPerPixel) - 1; - uint[] decodedPixelData = new uint[width * height]; int pixelDataPos = 0; for (int y = 0; y < height; y++) { @@ -298,12 +301,12 @@ internal static unsafe class LosslessUtils packedPixels = GetArgbIndex(pixelData[pixelDataPos++]); } - decodedPixelData[decodedPixels++] = colorMap[(int)(packedPixels & bitMask)]; + outputSpan[decodedPixels++] = colorMap[(int)(packedPixels & bitMask)]; packedPixels >>= bitsPerPixel; } } - decodedPixelData.AsSpan().CopyTo(pixelData); + outputSpan.CopyTo(pixelData); } else { diff --git a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs index 2170eb1985..736070a1c9 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs @@ -47,7 +47,7 @@ internal static unsafe class PredictorEncoder int[][] bestHisto, bool nearLossless, int nearLosslessQuality, - WebpTransparentColorMode transparentColorMode, + TransparentColorMode transparentColorMode, bool usedSubtractGreen, bool lowEffort) { @@ -202,7 +202,7 @@ internal static unsafe class PredictorEncoder int[][] histoArgb, int[][] bestHisto, int maxQuantization, - WebpTransparentColorMode transparentColorMode, + TransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, Span modes, @@ -340,19 +340,20 @@ internal static unsafe class PredictorEncoder int xEnd, int y, int maxQuantization, - WebpTransparentColorMode transparentColorMode, + TransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, Span output, Span scratch) { - if (transparentColorMode == WebpTransparentColorMode.Preserve) + if (transparentColorMode == TransparentColorMode.Preserve) { PredictBatch(mode, xStart, y, xEnd - xStart, currentRowSpan, upperRowSpan, output, scratch); } else { #pragma warning disable SA1503 // Braces should not be omitted +#pragma warning disable RCS1001 // Add braces (when expression spans over multiple lines) fixed (uint* currentRow = currentRowSpan) fixed (uint* upperRow = upperRowSpan) { @@ -466,6 +467,7 @@ internal static unsafe class PredictorEncoder } } } +#pragma warning restore RCS1001 // Add braces (when expression spans over multiple lines) #pragma warning restore SA1503 // Braces should not be omitted /// @@ -577,7 +579,7 @@ internal static unsafe class PredictorEncoder Span argbScratch, Span argb, int maxQuantization, - WebpTransparentColorMode transparentColorMode, + TransparentColorMode transparentColorMode, bool usedSubtractGreen, bool nearLossless, bool lowEffort) diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index 244691e77e..f088448391 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -69,7 +69,7 @@ internal class Vp8LEncoder : IDisposable /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// RGB information for better compression. /// - private readonly WebpTransparentColorMode transparentColorMode; + private readonly TransparentColorMode transparentColorMode; /// /// Whether to skip metadata during encoding. @@ -114,7 +114,7 @@ internal class Vp8LEncoder : IDisposable uint quality, bool skipMetadata, WebpEncodingMethod method, - WebpTransparentColorMode transparentColorMode, + TransparentColorMode transparentColorMode, bool nearLossless, int nearLosslessQuality) { @@ -236,7 +236,7 @@ internal class Vp8LEncoder : IDisposable /// public Vp8LHashChain HashChain { get; } - public WebpVp8X EncodeHeader(Image image, Stream stream, bool hasAnimation) + public WebpVp8X EncodeHeader(Image image, Stream stream, bool hasAnimation, ushort? repeatCount) where TPixel : unmanaged, IPixel { // Write bytes from the bit-writer buffer to the stream. @@ -258,7 +258,7 @@ internal class Vp8LEncoder : IDisposable if (hasAnimation) { WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata(); - BitWriterBase.WriteAnimationParameter(stream, webpMetadata.BackgroundColor, webpMetadata.RepeatCount); + BitWriterBase.WriteAnimationParameter(stream, webpMetadata.BackgroundColor, repeatCount ?? webpMetadata.RepeatCount); } return vp8x; @@ -315,8 +315,8 @@ internal class Vp8LEncoder : IDisposable (uint)bounds.Width, (uint)bounds.Height, frameMetadata.FrameDelay, - frameMetadata.BlendMethod, - frameMetadata.DisposalMethod) + frameMetadata.BlendMode, + frameMetadata.DisposalMode) .WriteHeaderTo(stream); } diff --git a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs index e4c2a7ddf6..6de3ae7497 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs @@ -684,6 +684,7 @@ internal sealed class WebpLosslessDecoder List transforms = decoder.Transforms; for (int i = transforms.Count - 1; i >= 0; i--) { + // TODO: Review these 1D allocations. They could conceivably exceed limits. Vp8LTransform transform = transforms[i]; switch (transform.TransformType) { @@ -701,7 +702,11 @@ internal sealed class WebpLosslessDecoder LosslessUtils.ColorSpaceInverseTransform(transform, pixelData); break; case Vp8LTransformType.ColorIndexingTransform: - LosslessUtils.ColorIndexInverseTransform(transform, pixelData); + using (IMemoryOwner output = memoryAllocator.Allocate(transform.XSize * transform.YSize, AllocationOptions.Clean)) + { + LosslessUtils.ColorIndexInverseTransform(transform, pixelData, output.GetSpan()); + } + break; } } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 3ad72f7d00..e4ebe14731 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -388,7 +388,13 @@ internal class Vp8Encoder : IDisposable /// Flag indicating, if an animation parameter is present. /// The image to encode from. /// A indicating whether the frame contains an alpha channel. - private bool Encode(Stream stream, ImageFrame frame, Rectangle bounds, WebpFrameMetadata frameMetadata, bool hasAnimation, Image image) + private bool Encode( + Stream stream, + ImageFrame frame, + Rectangle bounds, + WebpFrameMetadata frameMetadata, + bool hasAnimation, + Image image) where TPixel : unmanaged, IPixel { int width = bounds.Width; @@ -495,8 +501,8 @@ internal class Vp8Encoder : IDisposable (uint)bounds.Width, (uint)bounds.Height, frameMetadata.FrameDelay, - frameMetadata.BlendMethod, - frameMetadata.DisposalMethod) + frameMetadata.BlendMode, + frameMetadata.DisposalMode) .WriteHeaderTo(stream); } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs index 82f00e8760..c645816d4b 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoding.cs @@ -667,12 +667,12 @@ internal static unsafe class Vp8Encoding // V block. dst = dst[8..]; - if (top != default) + if (!top.IsEmpty) { top = top[8..]; } - if (left != default) + if (!left.IsEmpty) { left = left[16..]; } @@ -701,7 +701,7 @@ internal static unsafe class Vp8Encoding private static void VerticalPred(Span dst, Span top, int size) { - if (top != default) + if (!top.IsEmpty) { for (int j = 0; j < size; j++) { @@ -716,7 +716,7 @@ internal static unsafe class Vp8Encoding public static void HorizontalPred(Span dst, Span left, int size) { - if (left != default) + if (!left.IsEmpty) { left = left[1..]; // in the reference implementation, left starts at - 1. for (int j = 0; j < size; j++) @@ -732,9 +732,9 @@ internal static unsafe class Vp8Encoding public static void TrueMotion(Span dst, Span left, Span top, int size) { - if (left != default) + if (!left.IsEmpty) { - if (top != default) + if (!top.IsEmpty) { Span clip = Clip1.AsSpan(255 - left[0]); // left [0] instead of left[-1], original left starts at -1 for (int y = 0; y < size; y++) @@ -759,7 +759,7 @@ internal static unsafe class Vp8Encoding // is equivalent to VE prediction where you just copy the top samples. // Note that if top samples are not available, the default value is // then 129, and not 127 as in the VerticalPred case. - if (top != default) + if (!top.IsEmpty) { VerticalPred(dst, top, size); } @@ -774,14 +774,14 @@ internal static unsafe class Vp8Encoding { int dc = 0; int j; - if (top != default) + if (!top.IsEmpty) { for (j = 0; j < size; j++) { dc += top[j]; } - if (left != default) + if (!left.IsEmpty) { // top and left present. left = left[1..]; // in the reference implementation, left starts at -1. @@ -798,7 +798,7 @@ internal static unsafe class Vp8Encoding dc = (dc + round) >> shift; } - else if (left != default) + else if (!left.IsEmpty) { // left but no top. left = left[1..]; // in the reference implementation, left starts at -1. diff --git a/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs b/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs index f8e664ed03..40146c6af8 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/YuvConversion.cs @@ -48,7 +48,7 @@ internal static class YuvConversion uint uv0 = ((3 * tluv) + luv + 0x00020002u) >> 2; YuvToBgr(topY[0], (int)(uv0 & 0xff), (int)(uv0 >> 16), topDst); - if (bottomY != default) + if (!bottomY.IsEmpty) { uv0 = ((3 * luv) + tluv + 0x00020002u) >> 2; YuvToBgr(bottomY[0], (int)uv0 & 0xff, (int)(uv0 >> 16), bottomDst); @@ -69,7 +69,7 @@ internal static class YuvConversion YuvToBgr(topY[xMul2 - 1], (int)(uv0 & 0xff), (int)(uv0 >> 16), topDst[((xMul2 - 1) * xStep)..]); YuvToBgr(topY[xMul2 - 0], (int)(uv1 & 0xff), (int)(uv1 >> 16), topDst[((xMul2 - 0) * xStep)..]); - if (bottomY != default) + if (!bottomY.IsEmpty) { uv0 = (diag03 + luv) >> 1; uv1 = (diag12 + uv) >> 1; @@ -85,7 +85,7 @@ internal static class YuvConversion { uv0 = ((3 * tluv) + luv + 0x00020002u) >> 2; YuvToBgr(topY[len - 1], (int)(uv0 & 0xff), (int)(uv0 >> 16), topDst[((len - 1) * xStep)..]); - if (bottomY != default) + if (!bottomY.IsEmpty) { uv0 = ((3 * luv) + tluv + 0x00020002u) >> 2; YuvToBgr(bottomY[len - 1], (int)(uv0 & 0xff), (int)(uv0 >> 16), bottomDst[((len - 1) * xStep)..]); @@ -120,7 +120,7 @@ internal static class YuvConversion int u0t = (topU[0] + uDiag) >> 1; int v0t = (topV[0] + vDiag) >> 1; YuvToBgr(topY[0], u0t, v0t, topDst); - if (bottomY != default) + if (!bottomY.IsEmpty) { int u0b = (curU[0] + uDiag) >> 1; int v0b = (curV[0] + vDiag) >> 1; @@ -134,7 +134,7 @@ internal static class YuvConversion ref byte topVRef = ref MemoryMarshal.GetReference(topV); ref byte curURef = ref MemoryMarshal.GetReference(curU); ref byte curVRef = ref MemoryMarshal.GetReference(curV); - if (bottomY != default) + if (!bottomY.IsEmpty) { for (pos = 1, uvPos = 0; pos + 32 + 1 <= len; pos += 32, uvPos += 16) { @@ -160,12 +160,12 @@ internal static class YuvConversion Span tmpTopDst = ru[(4 * 32)..]; Span tmpBottomDst = tmpTopDst[(4 * 32)..]; Span tmpTop = tmpBottomDst[(4 * 32)..]; - Span tmpBottom = (bottomY == default) ? null : tmpTop[32..]; + Span tmpBottom = bottomY.IsEmpty ? null : tmpTop[32..]; UpSampleLastBlock(topU[uvPos..], curU[uvPos..], leftOver, ru); UpSampleLastBlock(topV[uvPos..], curV[uvPos..], leftOver, rv); topY[pos..len].CopyTo(tmpTop); - if (bottomY != default) + if (!bottomY.IsEmpty) { bottomY[pos..len].CopyTo(tmpBottom); ConvertYuvToBgrWithBottomYSse41(tmpTop, tmpBottom, tmpTopDst, tmpBottomDst, ru, rv, 0, xStep); @@ -176,7 +176,7 @@ internal static class YuvConversion } tmpTopDst[..((len - pos) * xStep)].CopyTo(topDst[(pos * xStep)..]); - if (bottomY != default) + if (!bottomY.IsEmpty) { tmpBottomDst[..((len - pos) * xStep)].CopyTo(bottomDst[(pos * xStep)..]); } diff --git a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs index 72405e480e..b74337ef37 100644 --- a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs @@ -220,8 +220,8 @@ internal class WebpAnimationDecoder : IDisposable { WebpFrameMetadata frameMetadata = meta.GetWebpMetadata(); frameMetadata.FrameDelay = frameData.Duration; - frameMetadata.BlendMethod = frameData.BlendingMethod; - frameMetadata.DisposalMethod = frameData.DisposalMethod; + frameMetadata.BlendMode = frameData.BlendingMethod; + frameMetadata.DisposalMode = frameData.DisposalMethod; } /// @@ -343,7 +343,7 @@ internal class WebpAnimationDecoder : IDisposable return; } - Rectangle interest = Rectangle.Intersect(imageFrame.Bounds(), this.restoreArea.Value); + Rectangle interest = Rectangle.Intersect(imageFrame.Bounds, this.restoreArea.Value); Buffer2DRegion pixelRegion = imageFrame.PixelBuffer.GetRegion(interest); TPixel backgroundPixel = backgroundColor.ToPixel(); pixelRegion.Fill(backgroundPixel); diff --git a/src/ImageSharp/Formats/Webp/WebpEncoder.cs b/src/ImageSharp/Formats/Webp/WebpEncoder.cs index bc93df3a5b..2e459ff58e 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoder.cs @@ -6,8 +6,16 @@ namespace SixLabors.ImageSharp.Formats.Webp; /// /// Image encoder for writing an image to a stream in the Webp format. /// -public sealed class WebpEncoder : ImageEncoder +public sealed class WebpEncoder : AnimatedImageEncoder { + /// + /// Initializes a new instance of the class. + /// + public WebpEncoder() + + // Match the default behavior of the native reference encoder. + => this.TransparentColorMode = TransparentColorMode.Clear; + /// /// Gets the webp file format used. Either lossless or lossy. /// Defaults to lossy. @@ -58,13 +66,6 @@ public sealed class WebpEncoder : ImageEncoder /// public int FilterStrength { get; init; } = 60; - /// - /// Gets a value indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible - /// RGB information for better compression. - /// The default value is Clear. - /// - public WebpTransparentColorMode TransparentColorMode { get; init; } = WebpTransparentColorMode.Clear; - /// /// Gets a value indicating whether near lossless mode should be used. /// This option adjusts pixel values to help compressibility, but has minimal impact on the visual quality. diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index 733801d636..b3270786d7 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -5,7 +5,6 @@ using SixLabors.ImageSharp.Formats.Webp.Chunks; using SixLabors.ImageSharp.Formats.Webp.Lossless; using SixLabors.ImageSharp.Formats.Webp.Lossy; using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Webp; @@ -55,7 +54,7 @@ internal sealed class WebpEncoderCore /// Flag indicating whether to preserve the exact RGB values under transparent area. Otherwise, discard this invisible /// RGB information for better compression. /// - private readonly WebpTransparentColorMode transparentColorMode; + private readonly TransparentColorMode transparentColorMode; /// /// Whether to skip metadata during encoding. @@ -78,6 +77,19 @@ internal sealed class WebpEncoderCore /// private readonly WebpFileFormatType? fileFormat; + /// + /// The default background color of the canvas when animating. + /// This color may be used to fill the unused space on the canvas around the frames, + /// as well as the transparent pixels of the first frame. + /// The background color is also used when a frame disposal mode is . + /// + private readonly Color? backgroundColor; + + /// + /// The number of times any animation is repeated. + /// + private readonly ushort? repeatCount; + /// /// The global configuration. /// @@ -103,6 +115,8 @@ internal sealed class WebpEncoderCore this.skipMetadata = encoder.SkipMetadata; this.nearLossless = encoder.NearLossless; this.nearLosslessQuality = encoder.NearLosslessQuality; + this.backgroundColor = encoder.BackgroundColor; + this.repeatCount = encoder.RepeatCount; } /// @@ -147,16 +161,19 @@ internal sealed class WebpEncoderCore long initialPosition = stream.Position; bool hasAlpha = false; - WebpVp8X vp8x = encoder.EncodeHeader(image, stream, hasAnimation); + WebpVp8X vp8x = encoder.EncodeHeader(image, stream, hasAnimation, this.repeatCount); // Encode the first frame. ImageFrame previousFrame = image.Frames.RootFrame; WebpFrameMetadata frameMetadata = previousFrame.Metadata.GetWebpMetadata(); - hasAlpha |= encoder.Encode(previousFrame, previousFrame.Bounds(), frameMetadata, stream, hasAnimation); + + cancellationToken.ThrowIfCancellationRequested(); + + hasAlpha |= encoder.Encode(previousFrame, previousFrame.Bounds, frameMetadata, stream, hasAnimation); if (hasAnimation) { - FrameDisposalMode previousDisposal = frameMetadata.DisposalMethod; + FrameDisposalMode previousDisposal = frameMetadata.DisposalMode; // Encode additional frames // This frame is reused to store de-duplicated pixel buffers. @@ -164,12 +181,17 @@ internal sealed class WebpEncoderCore for (int i = 1; i < image.Frames.Count; i++) { + cancellationToken.ThrowIfCancellationRequested(); + ImageFrame? prev = previousDisposal == FrameDisposalMode.RestoreToBackground ? null : previousFrame; ImageFrame currentFrame = image.Frames[i]; ImageFrame? nextFrame = i < image.Frames.Count - 1 ? image.Frames[i + 1] : null; frameMetadata = currentFrame.Metadata.GetWebpMetadata(); - bool blend = frameMetadata.BlendMethod == FrameBlendMode.Over; + bool blend = frameMetadata.BlendMode == FrameBlendMode.Over; + Color background = frameMetadata.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; (bool difference, Rectangle bounds) = AnimationUtilities.DeDuplicatePixels( @@ -178,7 +200,7 @@ internal sealed class WebpEncoderCore currentFrame, nextFrame, encodingFrame, - Color.Transparent, + background, blend, ClampingMode.Even); @@ -197,7 +219,7 @@ internal sealed class WebpEncoderCore hasAlpha |= animatedEncoder.Encode(encodingFrame, bounds, frameMetadata, stream, hasAnimation); previousFrame = currentFrame; - previousDisposal = frameMetadata.DisposalMethod; + previousDisposal = frameMetadata.DisposalMode; } } @@ -229,9 +251,9 @@ internal sealed class WebpEncoderCore // Encode the first frame. ImageFrame previousFrame = image.Frames.RootFrame; WebpFrameMetadata frameMetadata = previousFrame.Metadata.GetWebpMetadata(); - FrameDisposalMode previousDisposal = frameMetadata.DisposalMethod; + FrameDisposalMode previousDisposal = frameMetadata.DisposalMode; - hasAlpha |= encoder.EncodeAnimation(previousFrame, stream, previousFrame.Bounds(), frameMetadata); + hasAlpha |= encoder.EncodeAnimation(previousFrame, stream, previousFrame.Bounds, frameMetadata); // Encode additional frames // This frame is reused to store de-duplicated pixel buffers. @@ -239,12 +261,17 @@ internal sealed class WebpEncoderCore for (int i = 1; i < image.Frames.Count; i++) { + cancellationToken.ThrowIfCancellationRequested(); + ImageFrame? prev = previousDisposal == FrameDisposalMode.RestoreToBackground ? null : previousFrame; ImageFrame currentFrame = image.Frames[i]; ImageFrame? nextFrame = i < image.Frames.Count - 1 ? image.Frames[i + 1] : null; frameMetadata = currentFrame.Metadata.GetWebpMetadata(); - bool blend = frameMetadata.BlendMethod == FrameBlendMode.Over; + bool blend = frameMetadata.BlendMode == FrameBlendMode.Over; + Color background = frameMetadata.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; (bool difference, Rectangle bounds) = AnimationUtilities.DeDuplicatePixels( @@ -253,7 +280,7 @@ internal sealed class WebpEncoderCore currentFrame, nextFrame, encodingFrame, - Color.Transparent, + background, blend, ClampingMode.Even); @@ -273,13 +300,14 @@ internal sealed class WebpEncoderCore hasAlpha |= animatedEncoder.EncodeAnimation(encodingFrame, stream, bounds, frameMetadata); previousFrame = currentFrame; - previousDisposal = frameMetadata.DisposalMethod; + previousDisposal = frameMetadata.DisposalMode; } encoder.EncodeFooter(image, in vp8x, hasAlpha, stream, initialPosition); } else { + cancellationToken.ThrowIfCancellationRequested(); encoder.EncodeStatic(stream, image); encoder.EncodeFooter(image, in vp8x, hasAlpha, stream, initialPosition); } diff --git a/src/ImageSharp/Formats/Webp/WebpFrameMetadata.cs b/src/ImageSharp/Formats/Webp/WebpFrameMetadata.cs index 3865f9837f..3f976a6401 100644 --- a/src/ImageSharp/Formats/Webp/WebpFrameMetadata.cs +++ b/src/ImageSharp/Formats/Webp/WebpFrameMetadata.cs @@ -24,19 +24,21 @@ public class WebpFrameMetadata : IFormatFrameMetadata private WebpFrameMetadata(WebpFrameMetadata other) { this.FrameDelay = other.FrameDelay; - this.DisposalMethod = other.DisposalMethod; - this.BlendMethod = other.BlendMethod; + this.DisposalMode = other.DisposalMode; + this.BlendMode = other.BlendMode; } /// - /// Gets or sets how transparent pixels of the current frame are to be blended with corresponding pixels of the previous canvas. + /// Gets or sets how transparent pixels of the current frame are to be blended with corresponding pixels + /// of the previous canvas. /// - public FrameBlendMode BlendMethod { get; set; } + public FrameBlendMode BlendMode { get; set; } /// - /// Gets or sets how the current frame is to be treated after it has been displayed (before rendering the next frame) on the canvas. + /// Gets or sets how the current frame is to be treated after it has been displayed + /// (before rendering the next frame) on the canvas. /// - public FrameDisposalMode DisposalMethod { get; set; } + public FrameDisposalMode DisposalMode { get; set; } /// /// Gets or sets the frame duration. The time to wait before displaying the next frame, @@ -49,8 +51,8 @@ public class WebpFrameMetadata : IFormatFrameMetadata => new() { FrameDelay = (uint)metadata.Duration.TotalMilliseconds, - BlendMethod = metadata.BlendMode, - DisposalMethod = GetMode(metadata.DisposalMode) + BlendMode = metadata.BlendMode, + DisposalMode = GetMode(metadata.DisposalMode) }; /// @@ -59,8 +61,8 @@ public class WebpFrameMetadata : IFormatFrameMetadata { ColorTableMode = FrameColorTableMode.Global, Duration = TimeSpan.FromMilliseconds(this.FrameDelay), - DisposalMode = this.DisposalMethod, - BlendMode = this.BlendMethod, + DisposalMode = this.DisposalMode, + BlendMode = this.BlendMode, }; /// diff --git a/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs b/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs deleted file mode 100644 index c12b8ed976..0000000000 --- a/src/ImageSharp/Formats/Webp/WebpTransparentColorMode.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -namespace SixLabors.ImageSharp.Formats.Webp; - -/// -/// Enum indicating how the transparency should be handled on encoding. -/// -public enum WebpTransparentColorMode -{ - /// - /// Discard the transparency information for better compression. - /// - Clear = 0, - - /// - /// The transparency will be kept as is. - /// - Preserve = 1, -} diff --git a/src/ImageSharp/IO/ChunkedMemoryStream.cs b/src/ImageSharp/IO/ChunkedMemoryStream.cs index 2534548141..760d1d3345 100644 --- a/src/ImageSharp/IO/ChunkedMemoryStream.cs +++ b/src/ImageSharp/IO/ChunkedMemoryStream.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.IO; @@ -14,42 +15,19 @@ namespace SixLabors.ImageSharp.IO; /// internal sealed class ChunkedMemoryStream : Stream { - // The memory allocator. - private readonly MemoryAllocator allocator; - - // Data - private MemoryChunk? memoryChunk; - - // The total number of allocated chunks - private int chunkCount; - - // The length of the largest contiguous buffer that can be handled by the allocator. - private readonly int allocatorCapacity; - - // Has the stream been disposed. + private readonly MemoryChunkBuffer memoryChunkBuffer; + private long length; + private long position; + private int bufferIndex; + private int chunkIndex; private bool isDisposed; - // Current chunk to write to - private MemoryChunk? writeChunk; - - // Offset into chunk to write to - private int writeOffset; - - // Current chunk to read from - private MemoryChunk? readChunk; - - // Offset into chunk to read from - private int readOffset; - /// /// Initializes a new instance of the class. /// /// The memory allocator. public ChunkedMemoryStream(MemoryAllocator allocator) - { - this.allocatorCapacity = allocator.GetBufferCapacityInBytes(); - this.allocator = allocator; - } + => this.memoryChunkBuffer = new(allocator); /// public override bool CanRead => !this.isDisposed; @@ -66,25 +44,7 @@ internal sealed class ChunkedMemoryStream : Stream get { this.EnsureNotDisposed(); - - int length = 0; - MemoryChunk? chunk = this.memoryChunk; - while (chunk != null) - { - MemoryChunk? next = chunk.Next; - if (next != null) - { - length += chunk.Length; - } - else - { - length += this.writeOffset; - } - - chunk = next; - } - - return length; + return this.length; } } @@ -94,93 +54,35 @@ internal sealed class ChunkedMemoryStream : Stream get { this.EnsureNotDisposed(); - - if (this.readChunk is null) - { - return 0; - } - - int pos = 0; - MemoryChunk? chunk = this.memoryChunk; - while (chunk != this.readChunk && chunk is not null) - { - pos += chunk.Length; - chunk = chunk.Next; - } - - pos += this.readOffset; - - return pos; + return this.position; } set { this.EnsureNotDisposed(); - - if (value < 0) - { - ThrowArgumentOutOfRange(nameof(value)); - } - - // Back up current position in case new position is out of range - MemoryChunk? backupReadChunk = this.readChunk; - int backupReadOffset = this.readOffset; - - this.readChunk = null; - this.readOffset = 0; - - int leftUntilAtPos = (int)value; - MemoryChunk? chunk = this.memoryChunk; - while (chunk != null) - { - if ((leftUntilAtPos < chunk.Length) - || ((leftUntilAtPos == chunk.Length) - && (chunk.Next is null))) - { - // The desired position is in this chunk - this.readChunk = chunk; - this.readOffset = leftUntilAtPos; - break; - } - - leftUntilAtPos -= chunk.Length; - chunk = chunk.Next; - } - - if (this.readChunk is null) - { - // Position is out of range - this.readChunk = backupReadChunk; - this.readOffset = backupReadOffset; - } + this.SetPosition(value); } } /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override void Flush() + { + } + + /// public override long Seek(long offset, SeekOrigin origin) { this.EnsureNotDisposed(); - switch (origin) + this.Position = origin switch { - case SeekOrigin.Begin: - this.Position = offset; - break; - - case SeekOrigin.Current: - this.Position += offset; - break; - - case SeekOrigin.End: - this.Position = this.Length + offset; - break; - default: - ThrowInvalidSeek(); - break; - } + SeekOrigin.Begin => (int)offset, + SeekOrigin.Current => (int)(this.Position + offset), + SeekOrigin.End => (int)(this.Length + offset), + _ => throw new ArgumentOutOfRangeException(nameof(offset)), + }; - return this.Position; + return this.position; } /// @@ -188,39 +90,13 @@ internal sealed class ChunkedMemoryStream : Stream => throw new NotSupportedException(); /// - protected override void Dispose(bool disposing) - { - if (this.isDisposed) - { - return; - } - - try - { - this.isDisposed = true; - if (disposing) - { - ReleaseMemoryChunks(this.memoryChunk); - } - - this.memoryChunk = null; - this.writeChunk = null; - this.readChunk = null; - this.chunkCount = 0; - } - finally - { - base.Dispose(disposing); - } - } - - /// - public override void Flush() + public override int ReadByte() { + Unsafe.SkipInit(out byte b); + return this.Read(MemoryMarshal.CreateSpan(ref b, 1)) == 1 ? b : -1; } /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int Read(byte[] buffer, int offset, int count) { Guard.NotNull(buffer, nameof(buffer)); @@ -230,111 +106,70 @@ internal sealed class ChunkedMemoryStream : Stream const string bufferMessage = "Offset subtracted from the buffer length is less than count."; Guard.IsFalse(buffer.Length - offset < count, nameof(buffer), bufferMessage); - return this.ReadImpl(buffer.AsSpan(offset, count)); + return this.Read(buffer.AsSpan(offset, count)); } /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override int Read(Span buffer) => this.ReadImpl(buffer); - - private int ReadImpl(Span buffer) + public override int Read(Span buffer) { this.EnsureNotDisposed(); - if (this.readChunk is null) - { - if (this.memoryChunk is null) - { - return 0; - } + int offset = 0; + int count = buffer.Length; - this.readChunk = this.memoryChunk; - this.readOffset = 0; + long remaining = this.length - this.position; + if (remaining <= 0) + { + // Already at the end of the stream, nothing to read + return 0; } - IMemoryOwner chunkBuffer = this.readChunk.Buffer; - int chunkSize = this.readChunk.Length; - if (this.readChunk.Next is null) + if (remaining > count) { - chunkSize = this.writeOffset; + remaining = count; } + // 'remaining' can be less than the provided buffer length. + int bytesToRead = (int)remaining; int bytesRead = 0; - int offset = 0; - int count = buffer.Length; - while (count > 0) + while (bytesToRead > 0 && this.bufferIndex != this.memoryChunkBuffer.Length) { - if (this.readOffset == chunkSize) + bool moveToNextChunk = false; + MemoryChunk chunk = this.memoryChunkBuffer[this.bufferIndex]; + int n = bytesToRead; + int remainingBytesInCurrentChunk = chunk.Length - this.chunkIndex; + if (n >= remainingBytesInCurrentChunk) { - // Exit if no more chunks are currently available - if (this.readChunk.Next is null) - { - break; - } - - this.readChunk = this.readChunk.Next; - this.readOffset = 0; - chunkBuffer = this.readChunk.Buffer; - chunkSize = this.readChunk.Length; - if (this.readChunk.Next is null) - { - chunkSize = this.writeOffset; - } + n = remainingBytesInCurrentChunk; + moveToNextChunk = true; } - int readCount = Math.Min(count, chunkSize - this.readOffset); - chunkBuffer.Slice(this.readOffset, readCount).CopyTo(buffer[offset..]); - offset += readCount; - count -= readCount; - this.readOffset += readCount; - bytesRead += readCount; - } - - return bytesRead; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override int ReadByte() - { - this.EnsureNotDisposed(); + // Read n bytes from the current chunk + chunk.Buffer.Memory.Span.Slice(this.chunkIndex, n).CopyTo(buffer.Slice(offset, n)); + bytesToRead -= n; + offset += n; + bytesRead += n; - if (this.readChunk is null) - { - if (this.memoryChunk is null) + if (moveToNextChunk) { - return 0; + this.chunkIndex = 0; + this.bufferIndex++; } - - this.readChunk = this.memoryChunk; - this.readOffset = 0; - } - - IMemoryOwner chunkBuffer = this.readChunk.Buffer; - int chunkSize = this.readChunk.Length; - if (this.readChunk.Next is null) - { - chunkSize = this.writeOffset; - } - - if (this.readOffset == chunkSize) - { - // Exit if no more chunks are currently available - if (this.readChunk.Next is null) + else { - return -1; + this.chunkIndex += n; } - - this.readChunk = this.readChunk.Next; - this.readOffset = 0; - chunkBuffer = this.readChunk.Buffer; } - return chunkBuffer.GetSpan()[this.readOffset++]; + this.position += bytesRead; + return bytesRead; } /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override void WriteByte(byte value) + => this.Write(MemoryMarshal.CreateSpan(ref value, 1)); + + /// public override void Write(byte[] buffer, int offset, int count) { Guard.NotNull(buffer, nameof(buffer)); @@ -344,157 +179,198 @@ internal sealed class ChunkedMemoryStream : Stream const string bufferMessage = "Offset subtracted from the buffer length is less than count."; Guard.IsFalse(buffer.Length - offset < count, nameof(buffer), bufferMessage); - this.WriteImpl(buffer.AsSpan(offset, count)); + this.Write(buffer.AsSpan(offset, count)); } /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override void Write(ReadOnlySpan buffer) => this.WriteImpl(buffer); - - private void WriteImpl(ReadOnlySpan buffer) + public override void Write(ReadOnlySpan buffer) { this.EnsureNotDisposed(); - if (this.memoryChunk is null) + int offset = 0; + int count = buffer.Length; + + long remaining = this.memoryChunkBuffer.Length - this.position; + + // Ensure we have enough capacity to write the data. + while (remaining < count) { - this.memoryChunk = this.AllocateMemoryChunk(); - this.writeChunk = this.memoryChunk; - this.writeOffset = 0; + this.memoryChunkBuffer.Expand(); + remaining = this.memoryChunkBuffer.Length - this.position; } - Guard.NotNull(this.writeChunk); - - Span chunkBuffer = this.writeChunk.Buffer.GetSpan(); - int chunkSize = this.writeChunk.Length; - int count = buffer.Length; - int offset = 0; - while (count > 0) + int bytesToWrite = count; + int bytesWritten = 0; + while (bytesToWrite > 0 && this.bufferIndex != this.memoryChunkBuffer.Length) { - if (this.writeOffset == chunkSize) + bool moveToNextChunk = false; + MemoryChunk chunk = this.memoryChunkBuffer[this.bufferIndex]; + int n = bytesToWrite; + int remainingBytesInCurrentChunk = chunk.Length - this.chunkIndex; + if (n >= remainingBytesInCurrentChunk) { - // Allocate a new chunk if the current one is full - this.writeChunk.Next = this.AllocateMemoryChunk(); - this.writeChunk = this.writeChunk.Next; - this.writeOffset = 0; - chunkBuffer = this.writeChunk.Buffer.GetSpan(); - chunkSize = this.writeChunk.Length; + n = remainingBytesInCurrentChunk; + moveToNextChunk = true; } - int copyCount = Math.Min(count, chunkSize - this.writeOffset); - buffer.Slice(offset, copyCount).CopyTo(chunkBuffer[this.writeOffset..]); + // Write n bytes to the current chunk + buffer.Slice(offset, n).CopyTo(chunk.Buffer.Slice(this.chunkIndex, n)); + bytesToWrite -= n; + offset += n; + bytesWritten += n; - offset += copyCount; - count -= copyCount; - this.writeOffset += copyCount; + if (moveToNextChunk) + { + this.chunkIndex = 0; + this.bufferIndex++; + } + else + { + this.chunkIndex += n; + } } + + this.position += bytesWritten; + this.length += bytesWritten; } - /// - public override void WriteByte(byte value) + /// + /// Writes the entire contents of this memory stream to another stream. + /// + /// The stream to write this memory stream to. + /// is . + /// The current or target stream is closed. + public void WriteTo(Stream stream) { + Guard.NotNull(stream, nameof(stream)); this.EnsureNotDisposed(); - if (this.memoryChunk is null) + this.Position = 0; + + long remaining = this.length - this.position; + if (remaining <= 0) { - this.memoryChunk = this.AllocateMemoryChunk(); - this.writeChunk = this.memoryChunk; - this.writeOffset = 0; + // Already at the end of the stream, nothing to read + return; } - Guard.NotNull(this.writeChunk); + int bytesToRead = (int)remaining; + int bytesRead = 0; + while (bytesToRead > 0 && this.bufferIndex != this.memoryChunkBuffer.Length) + { + bool moveToNextChunk = false; + MemoryChunk chunk = this.memoryChunkBuffer[this.bufferIndex]; + int n = bytesToRead; + int remainingBytesInCurrentChunk = chunk.Length - this.chunkIndex; + if (n >= remainingBytesInCurrentChunk) + { + n = remainingBytesInCurrentChunk; + moveToNextChunk = true; + } - IMemoryOwner chunkBuffer = this.writeChunk.Buffer; - int chunkSize = this.writeChunk.Length; + // Read n bytes from the current chunk + stream.Write(chunk.Buffer.Memory.Span.Slice(this.chunkIndex, n)); + bytesToRead -= n; + bytesRead += n; - if (this.writeOffset == chunkSize) - { - // Allocate a new chunk if the current one is full - this.writeChunk.Next = this.AllocateMemoryChunk(); - this.writeChunk = this.writeChunk.Next; - this.writeOffset = 0; - chunkBuffer = this.writeChunk.Buffer; + if (moveToNextChunk) + { + this.chunkIndex = 0; + this.bufferIndex++; + } + else + { + this.chunkIndex += n; + } } - chunkBuffer.GetSpan()[this.writeOffset++] = value; + this.position += bytesRead; } /// - /// Copy entire buffer into an array. + /// Writes the stream contents to a byte array, regardless of the property. /// - /// The . + /// A new . public byte[] ToArray() { - int length = (int)this.Length; // This will throw if stream is closed - byte[] copy = new byte[this.Length]; - - MemoryChunk? backupReadChunk = this.readChunk; - int backupReadOffset = this.readOffset; - - this.readChunk = this.memoryChunk; - this.readOffset = 0; - this.Read(copy, 0, length); - - this.readChunk = backupReadChunk; - this.readOffset = backupReadOffset; + this.EnsureNotDisposed(); + long position = this.position; + byte[] copy = new byte[this.length]; + this.Position = 0; + _ = this.Read(copy, 0, copy.Length); + this.Position = position; return copy; } - /// - /// Write remainder of this stream to another stream. - /// - /// The stream to write to. - public void WriteTo(Stream stream) + /// + protected override void Dispose(bool disposing) { - this.EnsureNotDisposed(); - - Guard.NotNull(stream, nameof(stream)); + if (this.isDisposed) + { + return; + } - if (this.readChunk is null) + try { - if (this.memoryChunk is null) + this.isDisposed = true; + if (disposing) { - return; + this.memoryChunkBuffer.Dispose(); } - this.readChunk = this.memoryChunk; - this.readOffset = 0; + this.bufferIndex = 0; + this.chunkIndex = 0; + this.position = 0; + this.length = 0; + } + finally + { + base.Dispose(disposing); + } + } + + private void SetPosition(long value) + { + long newPosition = value; + if (newPosition < 0) + { + throw new ArgumentOutOfRangeException(nameof(value)); } - IMemoryOwner chunkBuffer = this.readChunk.Buffer; - int chunkSize = this.readChunk.Length; - if (this.readChunk.Next is null) + this.position = newPosition; + + // Find the current chunk & current chunk index + int currentChunkIndex = 0; + long offset = newPosition; + + // If the new position is greater than the length of the stream, set the position to the end of the stream + if (offset > 0 && offset >= this.memoryChunkBuffer.Length) { - chunkSize = this.writeOffset; + this.bufferIndex = this.memoryChunkBuffer.ChunkCount - 1; + this.chunkIndex = this.memoryChunkBuffer[this.bufferIndex].Length - 1; + return; } - // Following code mirrors Read() logic (readChunk/readOffset should - // point just past last byte of last chunk when done) - // loop until end of chunks is found - while (true) + // Loop through the current chunks, as we increment the chunk index, we subtract the length of the chunk + // from the offset. Once the offset is less than the length of the chunk, we have found the correct chunk. + while (offset != 0) { - if (this.readOffset == chunkSize) + int chunkLength = this.memoryChunkBuffer[currentChunkIndex].Length; + if (offset < chunkLength) { - // Exit if no more chunks are currently available - if (this.readChunk.Next is null) - { - break; - } - - this.readChunk = this.readChunk.Next; - this.readOffset = 0; - chunkBuffer = this.readChunk.Buffer; - chunkSize = this.readChunk.Length; - if (this.readChunk.Next is null) - { - chunkSize = this.writeOffset; - } + // Found the correct chunk and the corresponding index + break; } - int writeCount = chunkSize - this.readOffset; - stream.Write(chunkBuffer.GetSpan(), this.readOffset, writeCount); - this.readOffset = chunkSize; + offset -= chunkLength; + currentChunkIndex++; } + + this.bufferIndex = currentChunkIndex; + + // Safe to cast here as we know the offset is less than the chunk length. + this.chunkIndex = (int)offset; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -507,48 +383,66 @@ internal sealed class ChunkedMemoryStream : Stream } [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowDisposed() => throw new ObjectDisposedException(null, "The stream is closed."); + private static void ThrowDisposed() => throw new ObjectDisposedException(nameof(ChunkedMemoryStream), "The stream is closed."); - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentOutOfRange(string value) => throw new ArgumentOutOfRangeException(value); + private sealed class MemoryChunkBuffer : IDisposable + { + private readonly List memoryChunks = new(); + private readonly MemoryAllocator allocator; + private readonly int allocatorCapacity; + private bool isDisposed; - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowInvalidSeek() => throw new ArgumentException("Invalid seek origin."); + public MemoryChunkBuffer(MemoryAllocator allocator) + { + this.allocatorCapacity = allocator.GetBufferCapacityInBytes(); + this.allocator = allocator; + } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private MemoryChunk AllocateMemoryChunk() - { - // Tweak our buffer sizes to take the minimum of the provided buffer sizes - // or the allocator buffer capacity which provides us with the largest - // available contiguous buffer size. - IMemoryOwner buffer = this.allocator.Allocate(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++))); + public int ChunkCount => this.memoryChunks.Count; - return new MemoryChunk(buffer) + public long Length { get; private set; } + + public MemoryChunk this[int index] => this.memoryChunks[index]; + + public void Expand() { - Next = null, - Length = buffer.Length() - }; - } + IMemoryOwner buffer = + this.allocator.Allocate(Math.Min(this.allocatorCapacity, GetChunkSize(this.ChunkCount))); - private static void ReleaseMemoryChunks(MemoryChunk? chunk) - { - while (chunk != null) + MemoryChunk chunk = new(buffer) + { + Length = buffer.Length() + }; + + this.memoryChunks.Add(chunk); + this.Length += chunk.Length; + } + + public void Dispose() { - chunk.Dispose(); - chunk = chunk.Next; + if (!this.isDisposed) + { + foreach (MemoryChunk chunk in this.memoryChunks) + { + chunk.Dispose(); + } + + this.memoryChunks.Clear(); + this.Length = 0; + this.isDisposed = true; + } } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int GetChunkSize(int i) - { - // Increment chunks sizes with moderate speed, but without using too many buffers from the same ArrayPool bucket of the default MemoryAllocator. - // https://github.com/SixLabors/ImageSharp/pull/2006#issuecomment-1066244720 -#pragma warning disable IDE1006 // Naming Styles - const int _128K = 1 << 17; - const int _4M = 1 << 22; - return i < 16 ? _128K * (1 << (int)((uint)i / 4)) : _4M; -#pragma warning restore IDE1006 // Naming Styles + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int GetChunkSize(int i) + { + // Increment chunks sizes with moderate speed, but without using too many buffers from the + // same ArrayPool bucket of the default MemoryAllocator. + // https://github.com/SixLabors/ImageSharp/pull/2006#issuecomment-1066244720 + const int b128K = 1 << 17; + const int b4M = 1 << 22; + return i < 16 ? b128K * (1 << (int)((uint)i / 4)) : b4M; + } } private sealed class MemoryChunk : IDisposable @@ -559,27 +453,15 @@ internal sealed class ChunkedMemoryStream : Stream public IMemoryOwner Buffer { get; } - public MemoryChunk? Next { get; set; } - public int Length { get; init; } - private void Dispose(bool disposing) + public void Dispose() { if (!this.isDisposed) { - if (disposing) - { - this.Buffer.Dispose(); - } - + this.Buffer.Dispose(); this.isDisposed = true; } } - - public void Dispose() - { - this.Dispose(disposing: true); - GC.SuppressFinalize(this); - } } } diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs index 7f58c6ecd8..d2ee0f9061 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image.Decode.cs @@ -128,21 +128,18 @@ public abstract partial class Image // Does the given stream contain enough data to fit in the header for the format // and does that data match the format specification? // Individual formats should still check since they are public. - IImageFormat? format = null; foreach (IImageFormatDetector formatDetector in configuration.ImageFormatsManager.FormatDetectors) { if (formatDetector.HeaderSize <= headersBuffer.Length && formatDetector.TryDetectFormat(headersBuffer, out IImageFormat? attemptFormat)) { - format = attemptFormat; + return attemptFormat; } } - if (format is null) - { - ImageFormatManager.ThrowInvalidDecoder(configuration.ImageFormatsManager); - } + ImageFormatManager.ThrowInvalidDecoder(configuration.ImageFormatsManager); - return format; + // Need to write this otherwise compiler is not happy + return null; } /// diff --git a/src/ImageSharp/ImageFrame.cs b/src/ImageSharp/ImageFrame.cs index fdde5019e1..686292bc7f 100644 --- a/src/ImageSharp/ImageFrame.cs +++ b/src/ImageSharp/ImageFrame.cs @@ -56,7 +56,7 @@ public abstract partial class ImageFrame : IConfigurationProvider, IDisposable /// Gets the bounds of the frame. /// /// The - public Rectangle Bounds() => new(0, 0, this.Width, this.Height); + public Rectangle Bounds => new(0, 0, this.Width, this.Height); /// public void Dispose() diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 2287f65cd8..de71e77ca0 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -429,7 +429,7 @@ public sealed class ImageFrame : ImageFrame, IPixelSource ParallelRowIterator.IterateRowIntervals( configuration, - this.Bounds(), + this.Bounds, in operation); return target; diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index d3c4034717..0d36340bf8 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -13,6 +13,7 @@ Image Resize Crop Gif Jpg Jpeg Bitmap Pbm Png Tga Tiff WebP NetCore A new, fully featured, fully managed, cross-platform, 2D graphics API for .NET Debug;Release + true @@ -29,14 +30,12 @@ - net8.0 - true + net8.0;net9.0 net8.0 - true diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 02403923d2..7ec7918381 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -160,7 +160,7 @@ public sealed class Image : Image /// /// Gets the root frame. /// - private IPixelSource PixelSourceUnsafe => this.frames.RootFrameUnsafe; + private ImageFrame PixelSourceUnsafe => this.frames.RootFrameUnsafe; /// /// Gets or sets the pixel at the specified position. @@ -324,7 +324,7 @@ public sealed class Image : Image } /// - /// Clones the current image + /// Clones the current image. /// /// Returns a new image with all the same metadata as the original. public Image Clone() => this.Clone(this.Configuration); diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs index 2eb05ea935..f0fa1438dd 100644 --- a/src/ImageSharp/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -25,27 +25,65 @@ public static class Buffer2DExtensions return buffer.FastMemoryGroup.View; } + /// + /// Performs a deep clone of the buffer covering the specified . + /// + /// The element type. + /// The source buffer. + /// The configuration. + /// The rectangle to clone. + /// The . + internal static Buffer2D CloneRegion(this Buffer2D source, Configuration configuration, Rectangle rectangle) + where T : unmanaged + { + Buffer2D buffer = configuration.MemoryAllocator.Allocate2D( + rectangle.Width, + rectangle.Height, + configuration.PreferContiguousImageBuffers); + + // Optimization for when the size of the area is the same as the buffer size. + Buffer2DRegion sourceRegion = source.GetRegion(rectangle); + if (sourceRegion.IsFullBufferArea) + { + sourceRegion.Buffer.FastMemoryGroup.CopyTo(buffer.FastMemoryGroup); + } + else + { + for (int y = 0; y < rectangle.Height; y++) + { + sourceRegion.DangerousGetRowSpan(y).CopyTo(buffer.DangerousGetRowSpan(y)); + } + } + + return buffer; + } + /// /// TODO: Does not work with multi-buffer groups, should be specific to Resize. - /// Copy columns of inplace, - /// from positions starting at to positions at . + /// Copy columns of in-place, + /// from positions starting at to positions at . /// + /// The element type. + /// The . + /// The source column index. + /// The destination column index. + /// The number of columns to copy. internal static unsafe void DangerousCopyColumns( this Buffer2D buffer, int sourceIndex, - int destIndex, + int destinationIndex, int columnCount) where T : struct { DebugGuard.NotNull(buffer, nameof(buffer)); DebugGuard.MustBeGreaterThanOrEqualTo(sourceIndex, 0, nameof(sourceIndex)); - DebugGuard.MustBeGreaterThanOrEqualTo(destIndex, 0, nameof(sourceIndex)); - CheckColumnRegionsDoNotOverlap(buffer, sourceIndex, destIndex, columnCount); + DebugGuard.MustBeGreaterThanOrEqualTo(destinationIndex, 0, nameof(sourceIndex)); + CheckColumnRegionsDoNotOverlap(buffer, sourceIndex, destinationIndex, columnCount); int elementSize = Unsafe.SizeOf(); int width = buffer.Width * elementSize; int sOffset = sourceIndex * elementSize; - int dOffset = destIndex * elementSize; + int dOffset = destinationIndex * elementSize; long count = columnCount * elementSize; Span span = MemoryMarshal.AsBytes(buffer.DangerousGetSingleMemory().Span); @@ -73,9 +111,7 @@ public static class Buffer2DExtensions /// The internal static Rectangle FullRectangle(this Buffer2D buffer) where T : struct - { - return new Rectangle(0, 0, buffer.Width, buffer.Height); - } + => new(0, 0, buffer.Width, buffer.Height); /// /// Return a to the subregion represented by 'rectangle' @@ -86,11 +122,11 @@ public static class Buffer2DExtensions /// The internal static Buffer2DRegion GetRegion(this Buffer2D buffer, Rectangle rectangle) where T : unmanaged => - new Buffer2DRegion(buffer, rectangle); + new(buffer, rectangle); internal static Buffer2DRegion GetRegion(this Buffer2D buffer, int x, int y, int width, int height) where T : unmanaged => - new Buffer2DRegion(buffer, new Rectangle(x, y, width, height)); + new(buffer, new Rectangle(x, y, width, height)); /// /// Return a to the whole area of 'buffer' @@ -100,7 +136,7 @@ public static class Buffer2DExtensions /// The internal static Buffer2DRegion GetRegion(this Buffer2D buffer) where T : unmanaged => - new Buffer2DRegion(buffer); + new(buffer); /// /// Returns the size of the buffer. @@ -115,6 +151,8 @@ public static class Buffer2DExtensions /// /// Gets the bounds of the buffer. /// + /// The element type + /// The /// The internal static Rectangle Bounds(this Buffer2D buffer) where T : struct => diff --git a/src/ImageSharp/Memory/Buffer2DRegion{T}.cs b/src/ImageSharp/Memory/Buffer2DRegion{T}.cs index 033b0a25a6..f4b257b587 100644 --- a/src/ImageSharp/Memory/Buffer2DRegion{T}.cs +++ b/src/ImageSharp/Memory/Buffer2DRegion{T}.cs @@ -107,7 +107,7 @@ public readonly struct Buffer2DRegion [MethodImpl(MethodImplOptions.AggressiveInlining)] public Buffer2DRegion GetSubRegion(int x, int y, int width, int height) { - var rectangle = new Rectangle(x, y, width, height); + Rectangle rectangle = new(x, y, width, height); return this.GetSubRegion(rectangle); } diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs index b4b1ffc6f4..e2e933f3cc 100644 --- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs +++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs @@ -36,8 +36,13 @@ internal static class MemoryGroupExtensions /// /// Returns a slice that is expected to be within the bounds of a single buffer. - /// Otherwise is thrown. /// + /// The type of element. + /// The group. + /// The start index of the slice. + /// The length of the slice. + /// Slice is out of bounds. + /// The slice. internal static Memory GetBoundedMemorySlice(this IMemoryGroup group, long start, int length) where T : struct { diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs index 1d2dca8700..cf4a421b44 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs @@ -241,7 +241,7 @@ internal sealed class ExifWriter return true; } - private static uint GetLength(IList values) + private static uint GetLength(List values) { if (values.Count == 0) { diff --git a/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs b/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs index 0d50b98095..ddfc625152 100644 --- a/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs +++ b/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.TagDataEntry.cs @@ -144,7 +144,7 @@ internal sealed partial class IccDataReader ushort channelCount = this.ReadUInt16(); var colorant = (IccColorantEncoding)this.ReadUInt16(); - if (Enum.IsDefined(typeof(IccColorantEncoding), colorant) && colorant != IccColorantEncoding.Unknown) + if (Enum.IsDefined(colorant) && colorant != IccColorantEncoding.Unknown) { // The type is known and so are the values (they are constant) // channelCount should always be 3 but it doesn't really matter if it's not diff --git a/src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs b/src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs index be7350bc44..ac78318f2b 100644 --- a/src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs +++ b/src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs @@ -155,9 +155,9 @@ public sealed class IccProfile : IDeepCloneable } return arrayValid && - Enum.IsDefined(typeof(IccColorSpaceType), this.Header.DataColorSpace) && - Enum.IsDefined(typeof(IccColorSpaceType), this.Header.ProfileConnectionSpace) && - Enum.IsDefined(typeof(IccRenderingIntent), this.Header.RenderingIntent) && + Enum.IsDefined(this.Header.DataColorSpace) && + Enum.IsDefined(this.Header.ProfileConnectionSpace) && + Enum.IsDefined(this.Header.RenderingIntent) && this.Header.Size is >= minSize and < maxSize; } diff --git a/src/ImageSharp/Processing/AffineTransformBuilder.cs b/src/ImageSharp/Processing/AffineTransformBuilder.cs index 4ac9546f39..6d1e8aaa55 100644 --- a/src/ImageSharp/Processing/AffineTransformBuilder.cs +++ b/src/ImageSharp/Processing/AffineTransformBuilder.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing; /// public class AffineTransformBuilder { - private readonly List> transformMatrixFactories = new(); + private readonly List> transformMatrixFactories = []; /// /// Initializes a new instance of the class. @@ -301,7 +301,8 @@ public class AffineTransformBuilder /// /// The source image size. /// The . - public Matrix3x2 BuildMatrix(Size sourceSize) => this.BuildMatrix(new Rectangle(Point.Empty, sourceSize)); + public Matrix3x2 BuildMatrix(Size sourceSize) + => this.BuildMatrix(new Rectangle(Point.Empty, sourceSize)); /// /// Returns the combined transform matrix for a given source rectangle. @@ -345,18 +346,8 @@ public class AffineTransformBuilder /// The . public Size GetTransformedSize(Rectangle sourceRectangle) { - Size size = sourceRectangle.Size; - - // Translate the origin matrix to cater for source rectangle offsets. - Matrix3x2 matrix = Matrix3x2.CreateTranslation(-sourceRectangle.Location); - - foreach (Func factory in this.transformMatrixFactories) - { - matrix *= factory(size); - CheckDegenerate(matrix); - } - - return TransformUtils.GetTransformedSize(matrix, size, this.TransformSpace); + Matrix3x2 matrix = this.BuildMatrix(sourceRectangle); + return TransformUtils.GetTransformedSize(matrix, sourceRectangle.Size, this.TransformSpace); } private static void CheckDegenerate(Matrix3x2 matrix) diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 4d95e060dc..63c4895080 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -61,9 +61,7 @@ internal class DefaultImageProcessorContext : IInternalImageProcessingCo /// public IImageProcessingContext ApplyProcessor(IImageProcessor processor) - { - return this.ApplyProcessor(processor, this.GetCurrentBounds()); - } + => this.ApplyProcessor(processor, this.GetCurrentBounds()); /// public IImageProcessingContext ApplyProcessor(IImageProcessor processor, Rectangle rectangle) @@ -74,11 +72,9 @@ internal class DefaultImageProcessorContext : IInternalImageProcessingCo // interim clone if the first processor in the pipeline is a cloning processor. if (processor is ICloningImageProcessor cloningImageProcessor) { - using (ICloningImageProcessor pixelProcessor = cloningImageProcessor.CreatePixelSpecificCloningProcessor(this.Configuration, this.source, rectangle)) - { - this.destination = pixelProcessor.CloneAndExecute(); - return this; - } + using ICloningImageProcessor pixelProcessor = cloningImageProcessor.CreatePixelSpecificCloningProcessor(this.Configuration, this.source, rectangle); + this.destination = pixelProcessor.CloneAndExecute(); + return this; } // Not a cloning processor? We need to create a clone to operate on. diff --git a/src/ImageSharp/Processing/Extensions/Convolution/BokehBlurExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/BokehBlurExtensions.cs index e3e6f13ed6..71252e0bb0 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/BokehBlurExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/BokehBlurExtensions.cs @@ -44,13 +44,13 @@ public static class BokehBlurExtensions /// Applies a bokeh blur to the image. /// /// The current image processing context. - /// The 'radius' value representing the size of the area to sample. - /// The 'components' value representing the number of kernels to use to approximate the bokeh effect. - /// The gamma highlight factor to use to emphasize bright spots in the source image /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'radius' value representing the size of the area to sample. + /// The 'components' value representing the number of kernels to use to approximate the bokeh effect. + /// The gamma highlight factor to use to emphasize bright spots in the source image /// The . - public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, int radius, int components, float gamma, Rectangle rectangle) + public static IImageProcessingContext BokehBlur(this IImageProcessingContext source, Rectangle rectangle, int radius, int components, float gamma) => source.ApplyProcessor(new BokehBlurProcessor(radius, components, gamma), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/Convolution/BoxBlurExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/BoxBlurExtensions.cs index 6611af742b..73e40b57aa 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/BoxBlurExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/BoxBlurExtensions.cs @@ -44,10 +44,10 @@ public static class BoxBlurExtensions /// Applies a box blur to the image. /// /// The current image processing context. - /// The 'radius' value representing the size of the area to sample. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'radius' value representing the size of the area to sample. /// /// The to use when mapping the pixels outside of the border, in X direction. /// @@ -55,9 +55,11 @@ public static class BoxBlurExtensions /// The to use when mapping the pixels outside of the border, in Y direction. /// /// The . - public static IImageProcessingContext BoxBlur(this IImageProcessingContext source, int radius, Rectangle rectangle, BorderWrappingMode borderWrapModeX, BorderWrappingMode borderWrapModeY) - { - var processor = new BoxBlurProcessor(radius, borderWrapModeX, borderWrapModeY); - return source.ApplyProcessor(processor, rectangle); - } + public static IImageProcessingContext BoxBlur( + this IImageProcessingContext source, + Rectangle rectangle, + int radius, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + => source.ApplyProcessor(new BoxBlurProcessor(radius, borderWrapModeX, borderWrapModeY), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/Convolution/ConvolutionExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/ConvolutionExtensions.cs new file mode 100644 index 0000000000..2980ff44f0 --- /dev/null +++ b/src/ImageSharp/Processing/Extensions/Convolution/ConvolutionExtensions.cs @@ -0,0 +1,89 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.Processing.Processors.Convolution; + +namespace SixLabors.ImageSharp.Processing.Extensions.Convolution; + +/// +/// Defines general convolution extensions to apply on an +/// using Mutate/Clone. +/// +public static class ConvolutionExtensions +{ + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The convolution kernel to apply. + /// The . + public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix kernelXY) + => Convolve(source, kernelXY, false); + + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The convolution kernel to apply. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The . + public static IImageProcessingContext Convolve(this IImageProcessingContext source, DenseMatrix kernelXY, bool preserveAlpha) + => Convolve(source, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); + + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The convolution kernel to apply. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The to use when mapping the pixels outside of the border, in X direction. + /// The to use when mapping the pixels outside of the border, in Y direction. + /// The . + public static IImageProcessingContext Convolve( + this IImageProcessingContext source, + DenseMatrix kernelXY, + bool preserveAlpha, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + => source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY)); + + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The rectangle structure that specifies the portion of the image object to alter. + /// The convolution kernel to apply. + /// The . + public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix kernelXY) + => Convolve(source, rectangle, kernelXY, false); + + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The rectangle structure that specifies the portion of the image object to alter. + /// The convolution kernel to apply. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The . + public static IImageProcessingContext Convolve(this IImageProcessingContext source, Rectangle rectangle, DenseMatrix kernelXY, bool preserveAlpha) + => Convolve(source, rectangle, kernelXY, preserveAlpha, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat); + + /// + /// Applies a convolution filter to the image. + /// + /// The current image processing context. + /// The rectangle structure that specifies the portion of the image object to alter. + /// The convolution kernel to apply. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The to use when mapping the pixels outside of the border, in X direction. + /// The to use when mapping the pixels outside of the border, in Y direction. + /// The . + public static IImageProcessingContext Convolve( + this IImageProcessingContext source, + Rectangle rectangle, + DenseMatrix kernelXY, + bool preserveAlpha, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + => source.ApplyProcessor(new ConvolutionProcessor(kernelXY, preserveAlpha, borderWrapModeX, borderWrapModeY), rectangle); +} diff --git a/src/ImageSharp/Processing/Extensions/Convolution/DetectEdgesExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/DetectEdgesExtensions.cs index b044c3966f..c8fb230559 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/DetectEdgesExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/DetectEdgesExtensions.cs @@ -16,8 +16,8 @@ public static class DetectEdgesExtensions /// /// The current image processing context. /// The . - public static IImageProcessingContext DetectEdges(this IImageProcessingContext source) => - DetectEdges(source, KnownEdgeDetectorKernels.Sobel); + public static IImageProcessingContext DetectEdges(this IImageProcessingContext source) + => DetectEdges(source, KnownEdgeDetectorKernels.Sobel); /// /// Detects any edges within the image. @@ -28,10 +28,8 @@ public static class DetectEdgesExtensions /// The structure that specifies the portion of the image object to alter. /// /// The . - public static IImageProcessingContext DetectEdges( - this IImageProcessingContext source, - Rectangle rectangle) => - DetectEdges(source, KnownEdgeDetectorKernels.Sobel, rectangle); + public static IImageProcessingContext DetectEdges(this IImageProcessingContext source, Rectangle rectangle) + => DetectEdges(source, rectangle, KnownEdgeDetectorKernels.Sobel); /// /// Detects any edges within the image operating in grayscale mode. @@ -39,10 +37,8 @@ public static class DetectEdgesExtensions /// The current image processing context. /// The 2D edge detector kernel. /// The . - public static IImageProcessingContext DetectEdges( - this IImageProcessingContext source, - EdgeDetector2DKernel kernel) => - DetectEdges(source, kernel, true); + public static IImageProcessingContext DetectEdges(this IImageProcessingContext source, EdgeDetector2DKernel kernel) + => DetectEdges(source, kernel, true); /// /// Detects any edges within the image using a . @@ -57,49 +53,41 @@ public static class DetectEdgesExtensions this IImageProcessingContext source, EdgeDetector2DKernel kernel, bool grayscale) - { - var processor = new EdgeDetector2DProcessor(kernel, grayscale); - source.ApplyProcessor(processor); - return source; - } + => source.ApplyProcessor(new EdgeDetector2DProcessor(kernel, grayscale)); /// /// Detects any edges within the image operating in grayscale mode. /// /// The current image processing context. - /// The 2D edge detector kernel. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 2D edge detector kernel. /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, - EdgeDetector2DKernel kernel, - Rectangle rectangle) => - DetectEdges(source, kernel, true, rectangle); + Rectangle rectangle, + EdgeDetector2DKernel kernel) + => DetectEdges(source, rectangle, kernel, true); /// /// Detects any edges within the image using a . /// /// The current image processing context. + /// + /// The structure that specifies the portion of the image object to alter. + /// /// The 2D edge detector kernel. /// /// Whether to convert the image to grayscale before performing edge detection. /// - /// - /// The structure that specifies the portion of the image object to alter. - /// /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, + Rectangle rectangle, EdgeDetector2DKernel kernel, - bool grayscale, - Rectangle rectangle) - { - var processor = new EdgeDetector2DProcessor(kernel, grayscale); - source.ApplyProcessor(processor, rectangle); - return source; - } + bool grayscale) + => source.ApplyProcessor(new EdgeDetector2DProcessor(kernel, grayscale), rectangle); /// /// Detects any edges within the image operating in grayscale mode. @@ -107,10 +95,8 @@ public static class DetectEdgesExtensions /// The current image processing context. /// The edge detector kernel. /// The . - public static IImageProcessingContext DetectEdges( - this IImageProcessingContext source, - EdgeDetectorKernel kernel) => - DetectEdges(source, kernel, true); + public static IImageProcessingContext DetectEdges(this IImageProcessingContext source, EdgeDetectorKernel kernel) + => DetectEdges(source, kernel, true); /// /// Detects any edges within the image using a . @@ -125,66 +111,56 @@ public static class DetectEdgesExtensions this IImageProcessingContext source, EdgeDetectorKernel kernel, bool grayscale) - { - var processor = new EdgeDetectorProcessor(kernel, grayscale); - source.ApplyProcessor(processor); - return source; - } + => source.ApplyProcessor(new EdgeDetectorProcessor(kernel, grayscale)); /// /// Detects any edges within the image operating in grayscale mode. /// /// The current image processing context. - /// The edge detector kernel. /// /// The structure that specifies the portion of the image object to alter. /// + /// The edge detector kernel. /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, - EdgeDetectorKernel kernel, - Rectangle rectangle) => - DetectEdges(source, kernel, true, rectangle); + Rectangle rectangle, + EdgeDetectorKernel kernel) + => DetectEdges(source, rectangle, kernel, true); /// /// Detects any edges within the image using a . /// /// The current image processing context. + /// + /// The structure that specifies the portion of the image object to alter. + /// /// The edge detector kernel. /// /// Whether to convert the image to grayscale before performing edge detection. /// - /// - /// The structure that specifies the portion of the image object to alter. - /// /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, + Rectangle rectangle, EdgeDetectorKernel kernel, - bool grayscale, - Rectangle rectangle) - { - var processor = new EdgeDetectorProcessor(kernel, grayscale); - source.ApplyProcessor(processor, rectangle); - return source; - } + bool grayscale) + => source.ApplyProcessor(new EdgeDetectorProcessor(kernel, grayscale), rectangle); /// /// Detects any edges within the image operating in grayscale mode. /// /// The current image processing context. - /// Thecompass edge detector kernel. + /// The compass edge detector kernel. /// The . - public static IImageProcessingContext DetectEdges( - this IImageProcessingContext source, - EdgeDetectorCompassKernel kernel) => - DetectEdges(source, kernel, true); + public static IImageProcessingContext DetectEdges(this IImageProcessingContext source, EdgeDetectorCompassKernel kernel) + => DetectEdges(source, kernel, true); /// /// Detects any edges within the image using a . /// /// The current image processing context. - /// Thecompass edge detector kernel. + /// The compass edge detector kernel. /// /// Whether to convert the image to grayscale before performing edge detection. /// @@ -193,47 +169,39 @@ public static class DetectEdgesExtensions this IImageProcessingContext source, EdgeDetectorCompassKernel kernel, bool grayscale) - { - var processor = new EdgeDetectorCompassProcessor(kernel, grayscale); - source.ApplyProcessor(processor); - return source; - } + => source.ApplyProcessor(new EdgeDetectorCompassProcessor(kernel, grayscale)); /// /// Detects any edges within the image operating in grayscale mode. /// /// The current image processing context. - /// Thecompass edge detector kernel. /// /// The structure that specifies the portion of the image object to alter. /// + /// The compass edge detector kernel. /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, - EdgeDetectorCompassKernel kernel, - Rectangle rectangle) => - DetectEdges(source, kernel, true, rectangle); + Rectangle rectangle, + EdgeDetectorCompassKernel kernel) + => DetectEdges(source, rectangle, kernel, true); /// /// Detects any edges within the image using a . /// /// The current image processing context. - /// Thecompass edge detector kernel. - /// - /// Whether to convert the image to grayscale before performing edge detection. - /// /// /// The structure that specifies the portion of the image object to alter. /// + /// The compass edge detector kernel. + /// + /// Whether to convert the image to grayscale before performing edge detection. + /// /// The . public static IImageProcessingContext DetectEdges( this IImageProcessingContext source, + Rectangle rectangle, EdgeDetectorCompassKernel kernel, - bool grayscale, - Rectangle rectangle) - { - var processor = new EdgeDetectorCompassProcessor(kernel, grayscale); - source.ApplyProcessor(processor, rectangle); - return source; - } + bool grayscale) + => source.ApplyProcessor(new EdgeDetectorCompassProcessor(kernel, grayscale), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/Convolution/GaussianBlurExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/GaussianBlurExtensions.cs index b851482008..d406bf8d10 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/GaussianBlurExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/GaussianBlurExtensions.cs @@ -32,22 +32,25 @@ public static class GaussianBlurExtensions /// Applies a Gaussian blur to the image. /// /// The current image processing context. - /// The 'sigma' value representing the weight of the blur. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'sigma' value representing the weight of the blur. /// The . - public static IImageProcessingContext GaussianBlur(this IImageProcessingContext source, float sigma, Rectangle rectangle) + public static IImageProcessingContext GaussianBlur( + this IImageProcessingContext source, + Rectangle rectangle, + float sigma) => source.ApplyProcessor(new GaussianBlurProcessor(sigma), rectangle); /// /// Applies a Gaussian blur to the image. /// /// The current image processing context. - /// The 'sigma' value representing the weight of the blur. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'sigma' value representing the weight of the blur. /// /// The to use when mapping the pixels outside of the border, in X direction. /// @@ -55,9 +58,11 @@ public static class GaussianBlurExtensions /// The to use when mapping the pixels outside of the border, in Y direction. /// /// The . - public static IImageProcessingContext GaussianBlur(this IImageProcessingContext source, float sigma, Rectangle rectangle, BorderWrappingMode borderWrapModeX, BorderWrappingMode borderWrapModeY) - { - var processor = new GaussianBlurProcessor(sigma, borderWrapModeX, borderWrapModeY); - return source.ApplyProcessor(processor, rectangle); - } + public static IImageProcessingContext GaussianBlur( + this IImageProcessingContext source, + Rectangle rectangle, + float sigma, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + => source.ApplyProcessor(new GaussianBlurProcessor(sigma, borderWrapModeX, borderWrapModeY), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/Convolution/GaussianSharpenExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/GaussianSharpenExtensions.cs index 4a94df0963..9470cdbdc0 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/GaussianSharpenExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/GaussianSharpenExtensions.cs @@ -16,8 +16,8 @@ public static class GaussianSharpenExtensions /// /// The current image processing context. /// The . - public static IImageProcessingContext GaussianSharpen(this IImageProcessingContext source) => - source.ApplyProcessor(new GaussianSharpenProcessor()); + public static IImageProcessingContext GaussianSharpen(this IImageProcessingContext source) + => source.ApplyProcessor(new GaussianSharpenProcessor()); /// /// Applies a Gaussian sharpening filter to the image. @@ -25,32 +25,32 @@ public static class GaussianSharpenExtensions /// The current image processing context. /// The 'sigma' value representing the weight of the blur. /// The . - public static IImageProcessingContext GaussianSharpen(this IImageProcessingContext source, float sigma) => - source.ApplyProcessor(new GaussianSharpenProcessor(sigma)); + public static IImageProcessingContext GaussianSharpen(this IImageProcessingContext source, float sigma) + => source.ApplyProcessor(new GaussianSharpenProcessor(sigma)); /// /// Applies a Gaussian sharpening filter to the image. /// /// The current image processing context. - /// The 'sigma' value representing the weight of the blur. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'sigma' value representing the weight of the blur. /// The . public static IImageProcessingContext GaussianSharpen( this IImageProcessingContext source, - float sigma, - Rectangle rectangle) => + Rectangle rectangle, + float sigma) => source.ApplyProcessor(new GaussianSharpenProcessor(sigma), rectangle); /// /// Applies a Gaussian sharpening filter to the image. /// /// The current image processing context. - /// The 'sigma' value representing the weight of the blur. /// /// The structure that specifies the portion of the image object to alter. /// + /// The 'sigma' value representing the weight of the blur. /// /// The to use when mapping the pixels outside of the border, in X direction. /// @@ -58,9 +58,11 @@ public static class GaussianSharpenExtensions /// The to use when mapping the pixels outside of the border, in Y direction. /// /// The . - public static IImageProcessingContext GaussianSharpen(this IImageProcessingContext source, float sigma, Rectangle rectangle, BorderWrappingMode borderWrapModeX, BorderWrappingMode borderWrapModeY) - { - var processor = new GaussianSharpenProcessor(sigma, borderWrapModeX, borderWrapModeY); - return source.ApplyProcessor(processor, rectangle); - } + public static IImageProcessingContext GaussianSharpen( + this IImageProcessingContext source, + Rectangle rectangle, + float sigma, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + => source.ApplyProcessor(new GaussianSharpenProcessor(sigma, borderWrapModeX, borderWrapModeY), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/Convolution/MedianBlurExtensions.cs b/src/ImageSharp/Processing/Extensions/Convolution/MedianBlurExtensions.cs index a08a398b75..bc6fef62a6 100644 --- a/src/ImageSharp/Processing/Extensions/Convolution/MedianBlurExtensions.cs +++ b/src/ImageSharp/Processing/Extensions/Convolution/MedianBlurExtensions.cs @@ -20,21 +20,28 @@ public static class MedianBlurExtensions /// Whether the filter is applied to alpha as well as the color channels. /// /// The . - public static IImageProcessingContext MedianBlur(this IImageProcessingContext source, int radius, bool preserveAlpha) + public static IImageProcessingContext MedianBlur( + this IImageProcessingContext source, + int radius, + bool preserveAlpha) => source.ApplyProcessor(new MedianBlurProcessor(radius, preserveAlpha)); /// /// Applies a median blur on the image. /// /// The current image processing context. + /// + /// The structure that specifies the portion of the image object to alter. + /// /// The radius of the area to find the median for. /// /// Whether the filter is applied to alpha as well as the color channels. /// - /// - /// The structure that specifies the portion of the image object to alter. - /// /// The . - public static IImageProcessingContext MedianBlur(this IImageProcessingContext source, int radius, bool preserveAlpha, Rectangle rectangle) + public static IImageProcessingContext MedianBlur( + this IImageProcessingContext source, + Rectangle rectangle, + int radius, + bool preserveAlpha) => source.ApplyProcessor(new MedianBlurProcessor(radius, preserveAlpha), rectangle); } diff --git a/src/ImageSharp/Processing/Extensions/ProcessingExtensions.IntegralImage.cs b/src/ImageSharp/Processing/Extensions/ProcessingExtensions.IntegralImage.cs index 713d4d5b77..676acee0f4 100644 --- a/src/ImageSharp/Processing/Extensions/ProcessingExtensions.IntegralImage.cs +++ b/src/ImageSharp/Processing/Extensions/ProcessingExtensions.IntegralImage.cs @@ -2,7 +2,6 @@ // Licensed under the Six Labors Split License. using System.Buffers; -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -42,7 +41,7 @@ public static partial class ProcessingExtensions /// The containing all the sums. public static Buffer2D CalculateIntegralImage(this ImageFrame source) where TPixel : unmanaged, IPixel - => source.CalculateIntegralImage(source.Bounds()); + => source.CalculateIntegralImage(source.Bounds); /// /// Apply an image integral. @@ -56,7 +55,7 @@ public static partial class ProcessingExtensions { Configuration configuration = source.Configuration; - var interest = Rectangle.Intersect(bounds, source.Bounds()); + Rectangle interest = Rectangle.Intersect(bounds, source.Bounds); int startY = interest.Y; int startX = interest.X; int endY = interest.Height; diff --git a/src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs index 73c7c3302d..e17de49d74 100644 --- a/src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs @@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization; /// /// Performs Bradley Adaptive Threshold filter against an image. /// +/// The pixel format. internal class AdaptiveThresholdProcessor : ImageProcessor where TPixel : unmanaged, IPixel { @@ -30,7 +31,7 @@ internal class AdaptiveThresholdProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); Configuration configuration = this.Configuration; TPixel upper = this.definition.Upper.ToPixel(); @@ -97,19 +98,23 @@ internal class AdaptiveThresholdProcessor : ImageProcessor Span rowSpan = this.source.DangerousGetRowSpan(y).Slice(this.startX, span.Length); PixelOperations.Instance.ToL8(this.configuration, rowSpan, span); + int startY = this.startY; int maxX = this.bounds.Width - 1; int maxY = this.bounds.Height - 1; + int clusterSize = this.clusterSize; + float thresholdLimit = this.thresholdLimit; + Buffer2D image = this.intImage; for (int x = 0; x < rowSpan.Length; x++) { - int x1 = Math.Clamp(x - this.clusterSize + 1, 0, maxX); - int x2 = Math.Min(x + this.clusterSize + 1, maxX); - int y1 = Math.Clamp(y - this.startY - this.clusterSize + 1, 0, maxY); - int y2 = Math.Min(y - this.startY + this.clusterSize + 1, maxY); + int x1 = Math.Clamp(x - clusterSize + 1, 0, maxX); + int x2 = Math.Min(x + clusterSize + 1, maxX); + int y1 = Math.Clamp(y - startY - clusterSize + 1, 0, maxY); + int y2 = Math.Min(y - startY + clusterSize + 1, maxY); uint count = (uint)((x2 - x1) * (y2 - y1)); - ulong sum = Math.Min(this.intImage[x2, y2] - this.intImage[x1, y2] - this.intImage[x2, y1] + this.intImage[x1, y1], ulong.MaxValue); + ulong sum = Math.Min(image[x2, y2] - image[x1, y2] - image[x2, y1] + image[x1, y1], ulong.MaxValue); - if (span[x].PackedValue * count <= sum * this.thresholdLimit) + if (span[x].PackedValue * count <= sum * thresholdLimit) { rowSpan[x] = this.lower; } diff --git a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs index 1c76ea6a45..ad87f36c1c 100644 --- a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor{TPixel}.cs @@ -38,8 +38,8 @@ internal class BinaryThresholdProcessor : ImageProcessor Rectangle sourceRectangle = this.SourceRectangle; Configuration configuration = this.Configuration; - var interest = Rectangle.Intersect(sourceRectangle, source.Bounds()); - var operation = new RowOperation( + Rectangle interest = Rectangle.Intersect(sourceRectangle, source.Bounds); + RowOperation operation = new( interest.X, source.PixelBuffer, upper, @@ -169,10 +169,8 @@ internal class BinaryThresholdProcessor : ImageProcessor { return chroma / (max + min); } - else - { - return chroma / (2F - max - min); - } + + return chroma / (2F - max - min); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs index 5931b7c402..a96fa1993e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs @@ -75,12 +75,12 @@ internal class BokehBlurProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - var sourceRectangle = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle sourceRectangle = Rectangle.Intersect(this.SourceRectangle, source.Bounds); // Preliminary gamma highlight pass if (this.gamma == 3F) { - var gammaOperation = new ApplyGamma3ExposureRowOperation(sourceRectangle, source.PixelBuffer, this.Configuration); + ApplyGamma3ExposureRowOperation gammaOperation = new(sourceRectangle, source.PixelBuffer, this.Configuration); ParallelRowIterator.IterateRows( this.Configuration, sourceRectangle, @@ -88,7 +88,7 @@ internal class BokehBlurProcessor : ImageProcessor } else { - var gammaOperation = new ApplyGammaExposureRowOperation(sourceRectangle, source.PixelBuffer, this.Configuration, this.gamma); + ApplyGammaExposureRowOperation gammaOperation = new(sourceRectangle, source.PixelBuffer, this.Configuration, this.gamma); ParallelRowIterator.IterateRows( this.Configuration, sourceRectangle, @@ -104,7 +104,7 @@ internal class BokehBlurProcessor : ImageProcessor // Apply the inverse gamma exposure pass, and write the final pixel data if (this.gamma == 3F) { - var operation = new ApplyInverseGamma3ExposureRowOperation(sourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration); + ApplyInverseGamma3ExposureRowOperation operation = new(sourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration); ParallelRowIterator.IterateRows( this.Configuration, sourceRectangle, @@ -112,7 +112,7 @@ internal class BokehBlurProcessor : ImageProcessor } else { - var operation = new ApplyInverseGammaExposureRowOperation(sourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, 1 / this.gamma); + ApplyInverseGammaExposureRowOperation operation = new(sourceRectangle, source.PixelBuffer, processingBuffer, this.Configuration, 1 / this.gamma); ParallelRowIterator.IterateRows( this.Configuration, sourceRectangle, @@ -146,7 +146,7 @@ internal class BokehBlurProcessor : ImageProcessor // doing two 1D convolutions with the same kernel, we can use a single kernel sampling map as if // we were using a 2D kernel with each dimension being the same as the length of our kernel, and // use the two sampling offset spans resulting from this same map. This saves some extra work. - using var mapXY = new KernelSamplingMap(configuration.MemoryAllocator); + using KernelSamplingMap mapXY = new(configuration.MemoryAllocator); mapXY.BuildSamplingOffsetMap(this.kernelSize, this.kernelSize, sourceRectangle); @@ -161,7 +161,7 @@ internal class BokehBlurProcessor : ImageProcessor Vector4 parameters = Unsafe.Add(ref paramsRef, (uint)i); // Horizontal convolution - var horizontalOperation = new FirstPassConvolutionRowOperation( + FirstPassConvolutionRowOperation horizontalOperation = new( sourceRectangle, firstPassBuffer, source.PixelBuffer, @@ -175,7 +175,7 @@ internal class BokehBlurProcessor : ImageProcessor in horizontalOperation); // Vertical 1D convolutions to accumulate the partial results on the target buffer - var verticalOperation = new BokehBlurProcessor.SecondPassConvolutionRowOperation( + BokehBlurProcessor.SecondPassConvolutionRowOperation verticalOperation = new( sourceRectangle, processingBuffer, firstPassBuffer, @@ -342,9 +342,7 @@ internal class BokehBlurProcessor : ImageProcessor /// [MethodImpl(InliningOptions.ShortMethod)] public int GetRequiredBufferLength(Rectangle bounds) - { - return bounds.Width; - } + => bounds.Width; /// [MethodImpl(InliningOptions.ShortMethod)] @@ -391,7 +389,7 @@ internal class BokehBlurProcessor : ImageProcessor public void Invoke(int y) { Vector4 low = Vector4.Zero; - var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); + Vector4 high = new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); Span targetPixelSpan = this.targetPixels.DangerousGetRowSpan(y)[this.bounds.X..]; Span sourceRowSpan = this.sourceValues.DangerousGetRowSpan(y)[this.bounds.X..]; diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs index 8a7c424815..02e06db494 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor{TPixel}.cs @@ -62,14 +62,14 @@ internal class Convolution2DProcessor : ImageProcessor source.CopyTo(targetPixels); - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); - using (var map = new KernelSamplingMap(allocator)) + using (KernelSamplingMap map = new(allocator)) { // Since the kernel sizes are identical we can use a single map. map.BuildSamplingOffsetMap(this.KernelY, interest); - var operation = new Convolution2DRowOperation( + Convolution2DRowOperation operation = new( interest, targetPixels, source.PixelBuffer, diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs index 10780a21e2..1bbbdb3501 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs @@ -35,18 +35,48 @@ internal class Convolution2PassProcessor : ImageProcessor Rectangle sourceRectangle, BorderWrappingMode borderWrapModeX, BorderWrappingMode borderWrapModeY) + : this(configuration, kernel, kernel, preserveAlpha, source, sourceRectangle, borderWrapModeX, borderWrapModeY) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The configuration which allows altering default behaviour or extending the library. + /// The 1D convolution kernel. X Direction + /// The 1D convolution kernel. Y Direction + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The source for the current processor instance. + /// The source area to process for the current processor instance. + /// The to use when mapping the pixels outside of the border, in X direction. + /// The to use when mapping the pixels outside of the border, in Y direction. + public Convolution2PassProcessor( + Configuration configuration, + float[] kernelX, + float[] kernelY, + bool preserveAlpha, + Image source, + Rectangle sourceRectangle, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) : base(configuration, source, sourceRectangle) { - this.Kernel = kernel; + this.KernelX = kernelX; + this.KernelY = kernelY; this.PreserveAlpha = preserveAlpha; this.BorderWrapModeX = borderWrapModeX; this.BorderWrapModeY = borderWrapModeY; } /// - /// Gets the convolution kernel. + /// Gets the convolution kernel. X direction. + /// + public float[] KernelX { get; } + + /// + /// Gets the convolution kernel. Y direction. /// - public float[] Kernel { get; } + public float[] KernelY { get; } /// /// Gets a value indicating whether the convolution filter is applied to alpha as well as the color channels. @@ -68,21 +98,21 @@ internal class Convolution2PassProcessor : ImageProcessor { using Buffer2D firstPassPixels = this.Configuration.MemoryAllocator.Allocate2D(source.Size); - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); // We can create a single sampling map with the size as if we were using the non separated 2D kernel // the two 1D kernels represent, and reuse it across both convolution steps, like in the bokeh blur. - using var mapXY = new KernelSamplingMap(this.Configuration.MemoryAllocator); + using KernelSamplingMap mapXY = new(this.Configuration.MemoryAllocator); - mapXY.BuildSamplingOffsetMap(this.Kernel.Length, this.Kernel.Length, interest, this.BorderWrapModeX, this.BorderWrapModeY); + mapXY.BuildSamplingOffsetMap(this.KernelX.Length, this.KernelX.Length, interest, this.BorderWrapModeX, this.BorderWrapModeY); // Horizontal convolution - var horizontalOperation = new HorizontalConvolutionRowOperation( + HorizontalConvolutionRowOperation horizontalOperation = new( interest, firstPassPixels, source.PixelBuffer, mapXY, - this.Kernel, + this.KernelX, this.Configuration, this.PreserveAlpha); @@ -92,12 +122,12 @@ internal class Convolution2PassProcessor : ImageProcessor in horizontalOperation); // Vertical convolution - var verticalOperation = new VerticalConvolutionRowOperation( + VerticalConvolutionRowOperation verticalOperation = new( interest, source.PixelBuffer, firstPassPixels, mapXY, - this.Kernel, + this.KernelY, this.Configuration, this.PreserveAlpha); @@ -140,7 +170,7 @@ internal class Convolution2PassProcessor : ImageProcessor } /// - [MethodImpl(InliningOptions.ShortMethod)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetRequiredBufferLength(Rectangle bounds) => 2 * bounds.Width; @@ -306,7 +336,7 @@ internal class Convolution2PassProcessor : ImageProcessor } /// - [MethodImpl(InliningOptions.ShortMethod)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetRequiredBufferLength(Rectangle bounds) => 2 * bounds.Width; diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs new file mode 100644 index 0000000000..995a5164d9 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs @@ -0,0 +1,79 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors.Convolution; + +/// +/// Defines a processor that uses a 2 dimensional matrix to perform convolution against an image. +/// +public class ConvolutionProcessor : IImageProcessor +{ + /// + /// Initializes a new instance of the class. + /// + /// The 2d gradient operator. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The to use when mapping the pixels outside of the border, in X direction. + /// The to use when mapping the pixels outside of the border, in Y direction. + public ConvolutionProcessor( + in DenseMatrix kernelXY, + bool preserveAlpha, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) + { + this.KernelXY = kernelXY; + this.PreserveAlpha = preserveAlpha; + this.BorderWrapModeX = borderWrapModeX; + this.BorderWrapModeY = borderWrapModeY; + } + + /// + /// Gets the 2d convolution kernel. + /// + public DenseMatrix KernelXY { get; } + + /// + /// Gets a value indicating whether the convolution filter is applied to alpha as well as the color channels. + /// + public bool PreserveAlpha { get; } + + /// + /// Gets the to use when mapping the pixels outside of the border, in X direction. + /// + public BorderWrappingMode BorderWrapModeX { get; } + + /// + /// Gets the to use when mapping the pixels outside of the border, in Y direction. + /// + public BorderWrappingMode BorderWrapModeY { get; } + + /// + public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) + where TPixel : unmanaged, + IPixel + { + if (this.KernelXY.TryGetLinearlySeparableComponents(out float[]? kernelX, out float[]? kernelY)) + { + return new Convolution2PassProcessor( + configuration, + kernelX, + kernelY, + this.PreserveAlpha, + source, + sourceRectangle, + this.BorderWrapModeX, + this.BorderWrapModeY); + } + + return new ConvolutionProcessor( + configuration, + this.KernelXY, + this.PreserveAlpha, + source, + sourceRectangle, + this.BorderWrapModeX, + this.BorderWrapModeY); + } +} diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs index ae79f2c31d..feaaf30ce0 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs @@ -31,10 +31,34 @@ internal class ConvolutionProcessor : ImageProcessor bool preserveAlpha, Image source, Rectangle sourceRectangle) + : this(configuration, kernelXY, preserveAlpha, source, sourceRectangle, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The configuration which allows altering default behaviour or extending the library. + /// The 2d gradient operator. + /// Whether the convolution filter is applied to alpha as well as the color channels. + /// The source for the current processor instance. + /// The source area to process for the current processor instance. + /// The to use when mapping the pixels outside of the border, in X direction. + /// The to use when mapping the pixels outside of the border, in Y direction. + public ConvolutionProcessor( + Configuration configuration, + in DenseMatrix kernelXY, + bool preserveAlpha, + Image source, + Rectangle sourceRectangle, + BorderWrappingMode borderWrapModeX, + BorderWrappingMode borderWrapModeY) : base(configuration, source, sourceRectangle) { this.KernelXY = kernelXY; this.PreserveAlpha = preserveAlpha; + this.BorderWrapModeX = borderWrapModeX; + this.BorderWrapModeY = borderWrapModeY; } /// @@ -47,6 +71,16 @@ internal class ConvolutionProcessor : ImageProcessor /// public bool PreserveAlpha { get; } + /// + /// Gets the to use when mapping the pixels outside of the border, in X direction. + /// + public BorderWrappingMode BorderWrapModeX { get; } + + /// + /// Gets the to use when mapping the pixels outside of the border, in Y direction. + /// + public BorderWrappingMode BorderWrapModeY { get; } + /// protected override void OnFrameApply(ImageFrame source) { @@ -55,13 +89,13 @@ internal class ConvolutionProcessor : ImageProcessor source.CopyTo(targetPixels); - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); - using (var map = new KernelSamplingMap(allocator)) + using (KernelSamplingMap map = new(allocator)) { - map.BuildSamplingOffsetMap(this.KernelXY, interest); + map.BuildSamplingOffsetMap(this.KernelXY.Rows, this.KernelXY.Columns, interest, this.BorderWrapModeX, this.BorderWrapModeY); - var operation = new RowOperation(interest, targetPixels, source.PixelBuffer, map, this.KernelXY, this.Configuration, this.PreserveAlpha); + RowOperation operation = new(interest, targetPixels, source.PixelBuffer, map, this.KernelXY, this.Configuration, this.PreserveAlpha); ParallelRowIterator.IterateRows( this.Configuration, interest, @@ -121,7 +155,7 @@ internal class ConvolutionProcessor : ImageProcessor ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span); Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(boundsX, boundsWidth); - var state = new ConvolutionState(in this.kernel, this.map); + ConvolutionState state = new(in this.kernel, this.map); int row = y - this.bounds.Y; ref int sampleRowBase = ref state.GetSampleRow((uint)row); diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor{TPixel}.cs index ae891f3507..eae7481661 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor{TPixel}.cs @@ -58,12 +58,12 @@ internal class EdgeDetectorCompassProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); // We need a clean copy for each pass to start from using ImageFrame cleanCopy = source.Clone(); - using (var processor = new ConvolutionProcessor(this.Configuration, in this.kernels[0], true, this.Source, interest)) + using (ConvolutionProcessor processor = new(this.Configuration, in this.kernels[0], true, this.Source, interest)) { processor.Apply(source); } @@ -78,12 +78,12 @@ internal class EdgeDetectorCompassProcessor : ImageProcessor { using ImageFrame pass = cleanCopy.Clone(); - using (var processor = new ConvolutionProcessor(this.Configuration, in this.kernels[i], true, this.Source, interest)) + using (ConvolutionProcessor processor = new(this.Configuration, in this.kernels[i], true, this.Source, interest)) { processor.Apply(pass); } - var operation = new RowOperation(source.PixelBuffer, pass.PixelBuffer, interest); + RowOperation operation = new(source.PixelBuffer, pass.PixelBuffer, interest); ParallelRowIterator.IterateRows( this.Configuration, interest, diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorProcessor{TPixel}.cs index c353f46b5f..3139d24bb4 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorProcessor{TPixel}.cs @@ -53,7 +53,7 @@ internal class EdgeDetectorProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - using var processor = new ConvolutionProcessor(this.Configuration, in this.kernelXY, true, this.Source, this.SourceRectangle); + using ConvolutionProcessor processor = new(this.Configuration, in this.kernelXY, true, this.Source, this.SourceRectangle); processor.Apply(source); } } diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor{TPixel}.cs index 6518375b9e..d762dd336b 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor{TPixel}.cs @@ -12,24 +12,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution; internal class GaussianBlurProcessor : ImageProcessor where TPixel : unmanaged, IPixel { - /// - /// Initializes a new instance of the class. - /// - /// The configuration which allows altering default behaviour or extending the library. - /// The defining the processor parameters. - /// The source for the current processor instance. - /// The source area to process for the current processor instance. - public GaussianBlurProcessor( - Configuration configuration, - GaussianBlurProcessor definition, - Image source, - Rectangle sourceRectangle) - : base(configuration, source, sourceRectangle) - { - int kernelSize = (definition.Radius * 2) + 1; - this.Kernel = ConvolutionProcessorHelpers.CreateGaussianBlurKernel(kernelSize, definition.Sigma); - } - /// /// Initializes a new instance of the class. /// @@ -72,7 +54,7 @@ internal class GaussianBlurProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - using var processor = new Convolution2PassProcessor(this.Configuration, this.Kernel, false, this.Source, this.SourceRectangle, this.BorderWrapModeX, this.BorderWrapModeY); + using Convolution2PassProcessor processor = new(this.Configuration, this.Kernel, false, this.Source, this.SourceRectangle, this.BorderWrapModeX, this.BorderWrapModeY); processor.Apply(source); } diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor{TPixel}.cs index a286201dff..bdb3a4b380 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor{TPixel}.cs @@ -12,22 +12,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution; internal class GaussianSharpenProcessor : ImageProcessor where TPixel : unmanaged, IPixel { - /// - /// Initializes a new instance of the class. - /// - /// The configuration which allows altering default behaviour or extending the library. - /// The defining the processor parameters. - /// The source for the current processor instance. - /// The source area to process for the current processor instance. - public GaussianSharpenProcessor( - Configuration configuration, - GaussianSharpenProcessor definition, - Image source, - Rectangle sourceRectangle) - : this(configuration, definition, source, sourceRectangle, BorderWrappingMode.Repeat, BorderWrappingMode.Repeat) - { - } - /// /// Initializes a new instance of the class. /// @@ -70,7 +54,7 @@ internal class GaussianSharpenProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - using var processor = new Convolution2PassProcessor(this.Configuration, this.Kernel, false, this.Source, this.SourceRectangle, this.BorderWrapModeX, this.BorderWrapModeY); + using Convolution2PassProcessor processor = new(this.Configuration, this.Kernel, false, this.Source, this.SourceRectangle, this.BorderWrapModeX, this.BorderWrapModeY); processor.Apply(source); } diff --git a/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs index fe3a29d437..3e75d8b840 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/MedianBlurProcessor{TPixel}.cs @@ -29,7 +29,7 @@ internal sealed class MedianBlurProcessor : ImageProcessor source.CopyTo(targetPixels); - Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); using KernelSamplingMap map = new(this.Configuration.MemoryAllocator); map.BuildSamplingOffsetMap(kernelSize, kernelSize, interest, this.definition.BorderWrapModeX, this.definition.BorderWrapModeY); diff --git a/src/ImageSharp/Processing/Processors/Convolution/Parameters/BokehBlurKernelDataProvider.cs b/src/ImageSharp/Processing/Processors/Convolution/Parameters/BokehBlurKernelDataProvider.cs index a680393c8c..565a5746d1 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Parameters/BokehBlurKernelDataProvider.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Parameters/BokehBlurKernelDataProvider.cs @@ -21,12 +21,12 @@ internal static class BokehBlurKernelDataProvider /// /// Gets the kernel scales to adjust the component values in each kernel /// - private static IReadOnlyList KernelScales { get; } = new[] { 1.4f, 1.2f, 1.2f, 1.2f, 1.2f, 1.2f }; + private static float[] KernelScales { get; } = new[] { 1.4f, 1.2f, 1.2f, 1.2f, 1.2f, 1.2f }; /// /// Gets the available bokeh blur kernel parameters /// - private static IReadOnlyList KernelComponents { get; } = new[] + private static Vector4[][] KernelComponents { get; } = new[] { // 1 component new[] { new Vector4(0.862325f, 1.624835f, 0.767583f, 1.862321f) }, @@ -112,7 +112,7 @@ internal static class BokehBlurKernelDataProvider private static (Vector4[] Parameters, float Scale) GetParameters(int componentsCount) { // Prepare the kernel components - int index = Math.Max(0, Math.Min(componentsCount - 1, KernelComponents.Count)); + int index = Math.Max(0, Math.Min(componentsCount - 1, KernelComponents.Length)); return (KernelComponents[index], KernelScales[index]); } diff --git a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs index 982cc7d46c..7e672393c7 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs @@ -46,7 +46,7 @@ internal sealed class PaletteDitherProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); this.dither.ApplyPaletteDither(in this.ditherProcessor, source, interest); } diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor.cs index 847a211a5a..06cfa49b3d 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor.cs @@ -36,14 +36,12 @@ internal sealed class PixelRowDelegateProcessor : IImageProcessor /// public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel - { - return new PixelRowDelegateProcessor( + => new PixelRowDelegateProcessor( new PixelRowDelegate(this.PixelRowOperation), configuration, this.Modifiers, source, sourceRectangle); - } /// /// A implementing the row processing logic for . @@ -54,9 +52,7 @@ internal sealed class PixelRowDelegateProcessor : IImageProcessor [MethodImpl(InliningOptions.ShortMethod)] public PixelRowDelegate(PixelRowOperation pixelRowOperation) - { - this.pixelRowOperation = pixelRowOperation; - } + => this.pixelRowOperation = pixelRowOperation; /// [MethodImpl(InliningOptions.ShortMethod)] diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor{TPixel,TDelegate}.cs b/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor{TPixel,TDelegate}.cs index 36bb327cf2..d38ffc801e 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor{TPixel,TDelegate}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelRowDelegateProcessor{TPixel,TDelegate}.cs @@ -48,8 +48,8 @@ internal sealed class PixelRowDelegateProcessor : ImageProces /// protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); - var operation = new RowOperation(interest.X, source.PixelBuffer, this.Configuration, this.modifiers, this.rowDelegate); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); + RowOperation operation = new(interest.X, source.PixelBuffer, this.Configuration, this.modifiers, this.rowDelegate); ParallelRowIterator.IterateRows( this.Configuration, diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor{TPixel}.cs index 68000ba3e8..c828b95b62 100644 --- a/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor{TPixel}.cs @@ -32,7 +32,7 @@ internal class PixelateProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); int size = this.Size; Guard.MustBeBetweenOrEqualTo(size, 0, interest.Width, nameof(size)); diff --git a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs index 5109139647..37286086c6 100644 --- a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor{TPixel}.cs @@ -27,15 +27,13 @@ internal class FilterProcessor : ImageProcessor /// The source area to process for the current processor instance. public FilterProcessor(Configuration configuration, FilterProcessor definition, Image source, Rectangle sourceRectangle) : base(configuration, source, sourceRectangle) - { - this.definition = definition; - } + => this.definition = definition; /// protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); - var operation = new RowOperation(interest.X, source.PixelBuffer, this.definition.Matrix, this.Configuration); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); + RowOperation operation = new(interest.X, source.PixelBuffer, this.definition.Matrix, this.Configuration); ParallelRowIterator.IterateRows( this.Configuration, diff --git a/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs index 93e600106a..41560d1200 100644 --- a/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs @@ -23,9 +23,9 @@ internal sealed class OpaqueProcessor : ImageProcessor protected override void OnFrameApply(ImageFrame source) { - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); - var operation = new OpaqueRowOperation(this.Configuration, source.PixelBuffer, interest); + OpaqueRowOperation operation = new(this.Configuration, source.PixelBuffer, interest); ParallelRowIterator.IterateRows(this.Configuration, interest, in operation); } diff --git a/src/ImageSharp/Processing/Processors/Normalization/AutoLevelProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Normalization/AutoLevelProcessor{TPixel}.cs index 6f4493f951..606789af96 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/AutoLevelProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/AutoLevelProcessor{TPixel}.cs @@ -28,9 +28,9 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor /// Indicating whether to clip the histogram bins at a specific value. /// The histogram clip limit. Histogram bins which exceed this limit, will be capped at this value. + /// Whether to apply a synchronized luminance value to each color channel. /// The source for the current processor instance. /// The source area to process for the current processor instance. - /// Whether to apply a synchronized luminance value to each color channel. public AutoLevelProcessor( Configuration configuration, int luminanceLevels, @@ -40,9 +40,7 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor source, Rectangle sourceRectangle) : base(configuration, luminanceLevels, clipHistogram, clipLimit, source, sourceRectangle) - { - this.SyncChannels = syncChannels; - } + => this.SyncChannels = syncChannels; /// /// Gets a value indicating whether to apply a synchronized luminance value to each color channel. @@ -54,12 +52,12 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor histogramBuffer = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean); // Build the histogram of the grayscale levels. - var grayscaleOperation = new GrayscaleLevelsRowOperation(this.Configuration, interest, histogramBuffer, source.PixelBuffer, this.LuminanceLevels); + GrayscaleLevelsRowOperation grayscaleOperation = new(this.Configuration, interest, histogramBuffer, source.PixelBuffer, this.LuminanceLevels); ParallelRowIterator.IterateRows, Vector4>( this.Configuration, interest, @@ -83,7 +81,7 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor( this.Configuration, interest, @@ -91,7 +89,7 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor( this.Configuration, interest, @@ -136,10 +134,10 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor span) { - Span vectorBuffer = span.Slice(0, this.bounds.Width); + Span vectorBuffer = span[..this.bounds.Width]; ref Vector4 vectorRef = ref MemoryMarshal.GetReference(vectorBuffer); ref int cdfBase = ref MemoryMarshal.GetReference(this.cdfBuffer.GetSpan()); - var sourceAccess = new PixelAccessor(this.source); + PixelAccessor sourceAccess = new(this.source); int levels = this.luminanceLevels; float noOfPixelsMinusCdfMin = this.numberOfPixelsMinusCdfMin; @@ -148,12 +146,11 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor.Instance.FromVector4Destructive(this.configuration, vectorBuffer, pixelRow); @@ -197,10 +194,10 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor span) { - Span vectorBuffer = span.Slice(0, this.bounds.Width); + Span vectorBuffer = span[..this.bounds.Width]; ref Vector4 vectorRef = ref MemoryMarshal.GetReference(vectorBuffer); ref int cdfBase = ref MemoryMarshal.GetReference(this.cdfBuffer.GetSpan()); - var sourceAccess = new PixelAccessor(this.source); + PixelAccessor sourceAccess = new(this.source); int levelsMinusOne = this.luminanceLevels - 1; float noOfPixelsMinusCdfMin = this.numberOfPixelsMinusCdfMin; @@ -209,7 +206,7 @@ internal class AutoLevelProcessor : HistogramEqualizationProcessor : HistogramEqualizat { MemoryAllocator memoryAllocator = this.Configuration.MemoryAllocator; int numberOfPixels = source.Width * source.Height; - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); using IMemoryOwner histogramBuffer = memoryAllocator.Allocate(this.LuminanceLevels, AllocationOptions.Clean); // Build the histogram of the grayscale levels. - var grayscaleOperation = new GrayscaleLevelsRowOperation(this.Configuration, interest, histogramBuffer, source.PixelBuffer, this.LuminanceLevels); + GrayscaleLevelsRowOperation grayscaleOperation = new(this.Configuration, interest, histogramBuffer, source.PixelBuffer, this.LuminanceLevels); ParallelRowIterator.IterateRows, Vector4>( this.Configuration, interest, @@ -74,7 +74,7 @@ internal class GlobalHistogramEqualizationProcessor : HistogramEqualizat float numberOfPixelsMinusCdfMin = numberOfPixels - cdfMin; // Apply the cdf to each pixel of the image - var cdfOperation = new CdfApplicationRowOperation(this.Configuration, interest, cdfBuffer, source.PixelBuffer, this.LuminanceLevels, numberOfPixelsMinusCdfMin); + CdfApplicationRowOperation cdfOperation = new(this.Configuration, interest, cdfBuffer, source.PixelBuffer, this.LuminanceLevels, numberOfPixelsMinusCdfMin); ParallelRowIterator.IterateRows( this.Configuration, interest, @@ -118,7 +118,7 @@ internal class GlobalHistogramEqualizationProcessor : HistogramEqualizat [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y, Span span) { - Span vectorBuffer = span.Slice(0, this.bounds.Width); + Span vectorBuffer = span[..this.bounds.Width]; ref Vector4 vectorRef = ref MemoryMarshal.GetReference(vectorBuffer); ref int cdfBase = ref MemoryMarshal.GetReference(this.cdfBuffer.GetSpan()); int levels = this.luminanceLevels; @@ -129,7 +129,7 @@ internal class GlobalHistogramEqualizationProcessor : HistogramEqualizat for (int x = 0; x < this.bounds.Width; x++) { - var vector = Unsafe.Add(ref vectorRef, (uint)x); + Vector4 vector = Unsafe.Add(ref vectorRef, (uint)x); int luminance = ColorNumerics.GetBT709Luminance(ref vector, levels); float luminanceEqualized = Unsafe.Add(ref cdfBase, (uint)luminance) / noOfPixelsMinusCdfMin; Unsafe.Add(ref vectorRef, (uint)x) = new Vector4(luminanceEqualized, luminanceEqualized, luminanceEqualized, vector.W); diff --git a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs index 1e43458253..9ed314ee38 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs @@ -35,7 +35,7 @@ internal class BackgroundColorProcessor : ImageProcessor TPixel color = this.definition.Color.ToPixel(); GraphicsOptions graphicsOptions = this.definition.GraphicsOptions; - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); Configuration configuration = this.Configuration; MemoryAllocator memoryAllocator = configuration.MemoryAllocator; @@ -48,7 +48,7 @@ internal class BackgroundColorProcessor : ImageProcessor PixelBlender blender = PixelOperations.Instance.GetPixelBlender(graphicsOptions); - var operation = new RowOperation(configuration, interest, blender, amount, colors, source.PixelBuffer); + RowOperation operation = new(configuration, interest, blender, amount, colors, source.PixelBuffer); ParallelRowIterator.IterateRows( configuration, interest, diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs index 19ce6c417d..d73e7bea1c 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs @@ -40,7 +40,7 @@ internal class GlowProcessor : ImageProcessor TPixel glowColor = this.definition.GlowColor.ToPixel(); float blendPercent = this.definition.GraphicsOptions.BlendPercentage; - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); Vector2 center = Rectangle.Center(interest); float finalRadius = this.definition.Radius.Calculate(interest.Size); @@ -54,7 +54,7 @@ internal class GlowProcessor : ImageProcessor using IMemoryOwner rowColors = allocator.Allocate(interest.Width); rowColors.GetSpan().Fill(glowColor); - var operation = new RowOperation(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source.PixelBuffer); + RowOperation operation = new(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source.PixelBuffer); ParallelRowIterator.IterateRows( configuration, interest, diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs index a327deec1c..b08cd898e8 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs @@ -40,7 +40,7 @@ internal class VignetteProcessor : ImageProcessor TPixel vignetteColor = this.definition.VignetteColor.ToPixel(); float blendPercent = this.definition.GraphicsOptions.BlendPercentage; - var interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, source.Bounds); Vector2 center = Rectangle.Center(interest); float finalRadiusX = this.definition.RadiusX.Calculate(interest.Size); @@ -62,7 +62,7 @@ internal class VignetteProcessor : ImageProcessor using IMemoryOwner rowColors = allocator.Allocate(interest.Width); rowColors.GetSpan().Fill(vignetteColor); - var operation = new RowOperation(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source.PixelBuffer); + RowOperation operation = new(configuration, interest, rowColors, this.blender, center, maxDistance, blendPercent, source.PixelBuffer); ParallelRowIterator.IterateRows( configuration, interest, diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs index da2580fedf..16bb412c76 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs @@ -32,7 +32,7 @@ internal class QuantizeProcessor : ImageProcessor /// protected override void OnFrameApply(ImageFrame source) { - Rectangle interest = Rectangle.Intersect(source.Bounds(), this.SourceRectangle); + Rectangle interest = Rectangle.Intersect(source.Bounds, this.SourceRectangle); Configuration configuration = this.Configuration; using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs index fc1dda6be5..6d2200b8a7 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs @@ -2,6 +2,7 @@ // Licensed under the Six Labors Split License. using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors.Dithering; @@ -50,7 +51,7 @@ public static class QuantizerUtilities Guard.NotNull(quantizer, nameof(quantizer)); Guard.NotNull(source, nameof(source)); - Rectangle interest = Rectangle.Intersect(source.Bounds(), bounds); + Rectangle interest = Rectangle.Intersect(source.Bounds, bounds); Buffer2DRegion region = source.PixelBuffer.GetRegion(interest); // Collect the palette. Required before the second pass runs. @@ -77,7 +78,7 @@ public static class QuantizerUtilities where TPixel : unmanaged, IPixel { Guard.NotNull(source, nameof(source)); - Rectangle interest = Rectangle.Intersect(source.Bounds(), bounds); + Rectangle interest = Rectangle.Intersect(source.Bounds, bounds); IndexedImageFrame destination = new( quantizer.Configuration, @@ -111,10 +112,39 @@ public static class QuantizerUtilities IPixelSamplingStrategy pixelSamplingStrategy, Image source) where TPixel : unmanaged, IPixel + => quantizer.BuildPalette(source.Configuration, TransparentColorMode.Preserve, pixelSamplingStrategy, source); + + /// + /// Adds colors to the quantized palette from the given pixel regions. + /// + /// The pixel format. + /// The pixel specific quantizer. + /// The configuration. + /// The transparent color mode. + /// The pixel sampling strategy. + /// The source image to sample from. + public static void BuildPalette( + this IQuantizer quantizer, + Configuration configuration, + TransparentColorMode mode, + IPixelSamplingStrategy pixelSamplingStrategy, + Image source) + where TPixel : unmanaged, IPixel { - foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + if (EncodingUtilities.ShouldClearTransparentPixels(mode)) + { + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + { + using Buffer2D clone = region.Buffer.CloneRegion(configuration, region.Rectangle); + quantizer.AddPaletteColors(clone.GetRegion()); + } + } + else { - quantizer.AddPaletteColors(region); + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + { + quantizer.AddPaletteColors(region); + } } } @@ -130,10 +160,39 @@ public static class QuantizerUtilities IPixelSamplingStrategy pixelSamplingStrategy, ImageFrame source) where TPixel : unmanaged, IPixel + => quantizer.BuildPalette(source.Configuration, TransparentColorMode.Preserve, pixelSamplingStrategy, source); + + /// + /// Adds colors to the quantized palette from the given pixel regions. + /// + /// The pixel format. + /// The pixel specific quantizer. + /// The configuration. + /// The transparent color mode. + /// The pixel sampling strategy. + /// The source image frame to sample from. + public static void BuildPalette( + this IQuantizer quantizer, + Configuration configuration, + TransparentColorMode mode, + IPixelSamplingStrategy pixelSamplingStrategy, + ImageFrame source) + where TPixel : unmanaged, IPixel { - foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + if (EncodingUtilities.ShouldClearTransparentPixels(mode)) + { + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + { + using Buffer2D clone = region.Buffer.CloneRegion(configuration, region.Rectangle); + quantizer.AddPaletteColors(clone.GetRegion()); + } + } + else { - quantizer.AddPaletteColors(region); + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + { + quantizer.AddPaletteColors(region); + } } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs index c5c2a778eb..b3919e5844 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs @@ -61,12 +61,12 @@ internal class AffineTransformProcessor : TransformProcessor, IR if (matrix.Equals(Matrix3x2.Identity)) { // The clone will be blank here copy all the pixel data over - var interest = Rectangle.Intersect(this.SourceRectangle, destination.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, destination.Bounds); Buffer2DRegion sourceBuffer = source.PixelBuffer.GetRegion(interest); - Buffer2DRegion destbuffer = destination.PixelBuffer.GetRegion(interest); + Buffer2DRegion destinationBuffer = destination.PixelBuffer.GetRegion(interest); for (int y = 0; y < sourceBuffer.Height; y++) { - sourceBuffer.DangerousGetRowSpan(y).CopyTo(destbuffer.DangerousGetRowSpan(y)); + sourceBuffer.DangerousGetRowSpan(y).CopyTo(destinationBuffer.DangerousGetRowSpan(y)); } return; @@ -77,31 +77,31 @@ internal class AffineTransformProcessor : TransformProcessor, IR if (sampler is NearestNeighborResampler) { - var nnOperation = new NNAffineOperation( + NNAffineOperation nnOperation = new( source.PixelBuffer, - Rectangle.Intersect(this.SourceRectangle, source.Bounds()), + Rectangle.Intersect(this.SourceRectangle, source.Bounds), destination.PixelBuffer, matrix); ParallelRowIterator.IterateRows( configuration, - destination.Bounds(), + destination.Bounds, in nnOperation); return; } - var operation = new AffineOperation( + AffineOperation operation = new( configuration, source.PixelBuffer, - Rectangle.Intersect(this.SourceRectangle, source.Bounds()), + Rectangle.Intersect(this.SourceRectangle, source.Bounds), destination.PixelBuffer, in sampler, matrix); ParallelRowIterator.IterateRowIntervals, Vector4>( configuration, - destination.Bounds(), + destination.Bounds, in operation); } @@ -128,17 +128,17 @@ internal class AffineTransformProcessor : TransformProcessor, IR [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y) { - Span destRow = this.destination.DangerousGetRowSpan(y); + Span destinationRowSpan = this.destination.DangerousGetRowSpan(y); - for (int x = 0; x < destRow.Length; x++) + for (int x = 0; x < destinationRowSpan.Length; x++) { - var point = Vector2.Transform(new Vector2(x, y), this.matrix); + Vector2 point = Vector2.Transform(new Vector2(x, y), this.matrix); int px = (int)MathF.Round(point.X); int py = (int)MathF.Round(point.Y); if (this.bounds.Contains(px, py)) { - destRow[x] = this.source.GetElementUnsafe(px, py); + destinationRowSpan[x] = this.source.GetElementUnsafe(px, py); } } } @@ -195,16 +195,16 @@ internal class AffineTransformProcessor : TransformProcessor, IR for (int y = rows.Min; y < rows.Max; y++) { - Span rowSpan = this.destination.DangerousGetRowSpan(y); + Span destinationRowSpan = this.destination.DangerousGetRowSpan(y); PixelOperations.Instance.ToVector4( this.configuration, - rowSpan, + destinationRowSpan, span, PixelConversionModifiers.Scale); for (int x = 0; x < span.Length; x++) { - var point = Vector2.Transform(new Vector2(x, y), matrix); + Vector2 point = Vector2.Transform(new Vector2(x, y), matrix); float pY = point.Y; float pX = point.X; @@ -221,13 +221,14 @@ internal class AffineTransformProcessor : TransformProcessor, IR Vector4 sum = Vector4.Zero; for (int yK = top; yK <= bottom; yK++) { + Span sourceRowSpan = this.source.DangerousGetRowSpan(yK); float yWeight = sampler.GetValue(yK - pY); for (int xK = left; xK <= right; xK++) { float xWeight = sampler.GetValue(xK - pX); - Vector4 current = this.source.GetElementUnsafe(xK, yK).ToScaledVector4(); + Vector4 current = sourceRowSpan[xK].ToScaledVector4(); Numerics.Premultiply(ref current); sum += current * xWeight * yWeight; } @@ -240,7 +241,7 @@ internal class AffineTransformProcessor : TransformProcessor, IR PixelOperations.Instance.FromVector4Destructive( this.configuration, span, - rowSpan, + destinationRowSpan, PixelConversionModifiers.Scale); } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/FlipProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/FlipProcessor{TPixel}.cs index 14da3ac890..1adda1d043 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/FlipProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/FlipProcessor{TPixel}.cs @@ -72,10 +72,10 @@ internal class FlipProcessor : ImageProcessor /// The configuration. private static void FlipY(ImageFrame source, Configuration configuration) { - var operation = new RowOperation(source.PixelBuffer); + RowOperation operation = new(source.PixelBuffer); ParallelRowIterator.IterateRows( configuration, - source.Bounds(), + source.Bounds, in operation); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/GaussianEliminationSolver.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/GaussianEliminationSolver.cs new file mode 100644 index 0000000000..1190de4352 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/GaussianEliminationSolver.cs @@ -0,0 +1,86 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +namespace SixLabors.ImageSharp.Processing.Processors.Transforms.Linear; + +/// +/// Represents a solver for systems of linear equations using the Gaussian Elimination method. +/// This class applies Gaussian Elimination to transform the matrix into row echelon form and then performs back substitution to find the solution vector. +/// This implementation is based on: +/// +internal static class GaussianEliminationSolver +{ + /// + /// Solves the system of linear equations represented by the given matrix and result vector using Gaussian Elimination. + /// + /// The square matrix representing the coefficients of the linear equations. + /// The vector representing the constants on the right-hand side of the linear equations. + /// Thrown if the matrix is singular and cannot be solved. + /// + /// The matrix passed to this method must be a square matrix. + /// If the matrix is singular (i.e., has no unique solution), an will be thrown. + /// + public static void Solve(double[][] matrix, double[] result) + { + TransformToRowEchelonForm(matrix, result); + ApplyBackSubstitution(matrix, result); + } + + private static void TransformToRowEchelonForm(double[][] matrix, double[] result) + { + int colCount = matrix.Length; + int rowCount = matrix[0].Length; + int pivotRow = 0; + for (int pivotCol = 0; pivotCol < colCount; pivotCol++) + { + double maxValue = double.Abs(matrix[pivotRow][pivotCol]); + int maxIndex = pivotRow; + for (int r = pivotRow + 1; r < rowCount; r++) + { + double value = double.Abs(matrix[r][pivotCol]); + if (value > maxValue) + { + maxIndex = r; + maxValue = value; + } + } + + if (matrix[maxIndex][pivotCol] == 0) + { + throw new NotSupportedException("Matrix is singular and cannot be solve"); + } + + (matrix[pivotRow], matrix[maxIndex]) = (matrix[maxIndex], matrix[pivotRow]); + (result[pivotRow], result[maxIndex]) = (result[maxIndex], result[pivotRow]); + + for (int r = pivotRow + 1; r < rowCount; r++) + { + double fraction = matrix[r][pivotCol] / matrix[pivotRow][pivotCol]; + for (int c = pivotCol + 1; c < colCount; c++) + { + matrix[r][c] -= matrix[pivotRow][c] * fraction; + } + + result[r] -= result[pivotRow] * fraction; + matrix[r][pivotCol] = 0; + } + + pivotRow++; + } + } + + private static void ApplyBackSubstitution(double[][] matrix, double[] result) + { + int rowCount = matrix[0].Length; + + for (int row = rowCount - 1; row >= 0; row--) + { + result[row] /= matrix[row][row]; + + for (int r = 0; r < row; r++) + { + result[r] -= result[row] * matrix[r][row]; + } + } + } +} diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs index b741dc4ee6..16b0070e90 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs @@ -61,12 +61,12 @@ internal class ProjectiveTransformProcessor : TransformProcessor if (matrix.Equals(Matrix4x4.Identity)) { // The clone will be blank here copy all the pixel data over - var interest = Rectangle.Intersect(this.SourceRectangle, destination.Bounds()); + Rectangle interest = Rectangle.Intersect(this.SourceRectangle, destination.Bounds); Buffer2DRegion sourceBuffer = source.PixelBuffer.GetRegion(interest); - Buffer2DRegion destbuffer = destination.PixelBuffer.GetRegion(interest); + Buffer2DRegion destinationBuffer = destination.PixelBuffer.GetRegion(interest); for (int y = 0; y < sourceBuffer.Height; y++) { - sourceBuffer.DangerousGetRowSpan(y).CopyTo(destbuffer.DangerousGetRowSpan(y)); + sourceBuffer.DangerousGetRowSpan(y).CopyTo(destinationBuffer.DangerousGetRowSpan(y)); } return; @@ -77,31 +77,31 @@ internal class ProjectiveTransformProcessor : TransformProcessor if (sampler is NearestNeighborResampler) { - var nnOperation = new NNProjectiveOperation( + NNProjectiveOperation nnOperation = new( source.PixelBuffer, - Rectangle.Intersect(this.SourceRectangle, source.Bounds()), + Rectangle.Intersect(this.SourceRectangle, source.Bounds), destination.PixelBuffer, matrix); ParallelRowIterator.IterateRows( configuration, - destination.Bounds(), + destination.Bounds, in nnOperation); return; } - var operation = new ProjectiveOperation( + ProjectiveOperation operation = new( configuration, source.PixelBuffer, - Rectangle.Intersect(this.SourceRectangle, source.Bounds()), + Rectangle.Intersect(this.SourceRectangle, source.Bounds), destination.PixelBuffer, in sampler, matrix); ParallelRowIterator.IterateRowIntervals, Vector4>( configuration, - destination.Bounds(), + destination.Bounds, in operation); } @@ -128,9 +128,9 @@ internal class ProjectiveTransformProcessor : TransformProcessor [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y) { - Span destRow = this.destination.DangerousGetRowSpan(y); + Span destinationRowSpan = this.destination.DangerousGetRowSpan(y); - for (int x = 0; x < destRow.Length; x++) + for (int x = 0; x < destinationRowSpan.Length; x++) { Vector2 point = TransformUtils.ProjectiveTransform2D(x, y, this.matrix); int px = (int)MathF.Round(point.X); @@ -138,7 +138,7 @@ internal class ProjectiveTransformProcessor : TransformProcessor if (this.bounds.Contains(px, py)) { - destRow[x] = this.source.GetElementUnsafe(px, py); + destinationRowSpan[x] = this.source.GetElementUnsafe(px, py); } } } @@ -195,10 +195,10 @@ internal class ProjectiveTransformProcessor : TransformProcessor for (int y = rows.Min; y < rows.Max; y++) { - Span rowSpan = this.destination.DangerousGetRowSpan(y); + Span destinationRowSpan = this.destination.DangerousGetRowSpan(y); PixelOperations.Instance.ToVector4( this.configuration, - rowSpan, + destinationRowSpan, span, PixelConversionModifiers.Scale); @@ -221,13 +221,14 @@ internal class ProjectiveTransformProcessor : TransformProcessor Vector4 sum = Vector4.Zero; for (int yK = top; yK <= bottom; yK++) { + Span sourceRowSpan = this.source.DangerousGetRowSpan(yK); float yWeight = sampler.GetValue(yK - pY); for (int xK = left; xK <= right; xK++) { float xWeight = sampler.GetValue(xK - pX); - Vector4 current = this.source.GetElementUnsafe(xK, yK).ToScaledVector4(); + Vector4 current = sourceRowSpan[xK].ToScaledVector4(); Numerics.Premultiply(ref current); sum += current * xWeight * yWeight; } @@ -240,7 +241,7 @@ internal class ProjectiveTransformProcessor : TransformProcessor PixelOperations.Instance.FromVector4Destructive( this.configuration, span, - rowSpan, + destinationRowSpan, PixelConversionModifiers.Scale); } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor{TPixel}.cs index 3ffb1ab518..8d3ded09f9 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor{TPixel}.cs @@ -130,40 +130,40 @@ internal class RotateProcessor : AffineTransformProcessor /// The configuration. private static void Rotate180(ImageFrame source, ImageFrame destination, Configuration configuration) { - var operation = new Rotate180RowOperation(source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); + Rotate180RowOperation operation = new(source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); ParallelRowIterator.IterateRows( configuration, - source.Bounds(), + source.Bounds, in operation); } /// - /// Rotates the image 270 degrees clockwise at the centre point. + /// Rotates the image 270 degrees clockwise at the center point. /// /// The source image. /// The destination image. /// The configuration. private static void Rotate270(ImageFrame source, ImageFrame destination, Configuration configuration) { - var operation = new Rotate270RowIntervalOperation(destination.Bounds(), source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); + Rotate270RowIntervalOperation operation = new(destination.Bounds, source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); ParallelRowIterator.IterateRowIntervals( configuration, - source.Bounds(), + source.Bounds, in operation); } /// - /// Rotates the image 90 degrees clockwise at the centre point. + /// Rotates the image 90 degrees clockwise at the center point. /// /// The source image. /// The destination image. /// The configuration. private static void Rotate90(ImageFrame source, ImageFrame destination, Configuration configuration) { - var operation = new Rotate90RowOperation(destination.Bounds(), source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); + Rotate90RowOperation operation = new(destination.Bounds, source.Width, source.Height, source.PixelBuffer, destination.PixelBuffer); ParallelRowIterator.IterateRows( configuration, - source.Bounds(), + source.Bounds, in operation); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformUtils.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformUtils.cs index 62ea5e830d..47b3250b87 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformUtils.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformUtils.cs @@ -3,6 +3,7 @@ using System.Numerics; using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Processing.Processors.Transforms.Linear; namespace SixLabors.ImageSharp.Processing.Processors.Transforms; @@ -278,6 +279,91 @@ internal static class TransformUtils return matrix; } + /// + /// Computes the projection matrix for a quad distortion transformation. + /// + /// The source rectangle. + /// The top-left point of the distorted quad. + /// The top-right point of the distorted quad. + /// The bottom-right point of the distorted quad. + /// The bottom-left point of the distorted quad. + /// The to use when creating the matrix. + /// The computed projection matrix for the quad distortion. + /// + /// This method is based on the algorithm described in the following article: + /// + /// + public static Matrix4x4 CreateQuadDistortionMatrix( + Rectangle rectangle, + PointF topLeft, + PointF topRight, + PointF bottomRight, + PointF bottomLeft, + TransformSpace transformSpace) + { + PointF p1 = new(rectangle.X, rectangle.Y); + PointF p2 = new(rectangle.X + rectangle.Width, rectangle.Y); + PointF p3 = new(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height); + PointF p4 = new(rectangle.X, rectangle.Y + rectangle.Height); + + PointF q1 = topLeft; + PointF q2 = topRight; + PointF q3 = bottomRight; + PointF q4 = bottomLeft; + + double[][] matrixData = + [ + [p1.X, p1.Y, 1, 0, 0, 0, -p1.X * q1.X, -p1.Y * q1.X], + [0, 0, 0, p1.X, p1.Y, 1, -p1.X * q1.Y, -p1.Y * q1.Y], + [p2.X, p2.Y, 1, 0, 0, 0, -p2.X * q2.X, -p2.Y * q2.X], + [0, 0, 0, p2.X, p2.Y, 1, -p2.X * q2.Y, -p2.Y * q2.Y], + [p3.X, p3.Y, 1, 0, 0, 0, -p3.X * q3.X, -p3.Y * q3.X], + [0, 0, 0, p3.X, p3.Y, 1, -p3.X * q3.Y, -p3.Y * q3.Y], + [p4.X, p4.Y, 1, 0, 0, 0, -p4.X * q4.X, -p4.Y * q4.X], + [0, 0, 0, p4.X, p4.Y, 1, -p4.X * q4.Y, -p4.Y * q4.Y], + ]; + + double[] b = + [ + q1.X, + q1.Y, + q2.X, + q2.Y, + q3.X, + q3.Y, + q4.X, + q4.Y, + ]; + + GaussianEliminationSolver.Solve(matrixData, b); + +#pragma warning disable SA1117 + Matrix4x4 projectionMatrix = new( + (float)b[0], (float)b[3], 0, (float)b[6], + (float)b[1], (float)b[4], 0, (float)b[7], + 0, 0, 1, 0, + (float)b[2], (float)b[5], 0, 1); +#pragma warning restore SA1117 + + // Check if the matrix involves only affine transformations by inspecting the relevant components. + // We want to use pixel space for calculations only if the transformation is purely 2D and does not include + // any perspective effects, non-standard scaling, or unusual translations that could distort the image. + if (transformSpace == TransformSpace.Pixel && IsAffineRotationOrSkew(projectionMatrix)) + { + if (projectionMatrix.M41 != 0) + { + projectionMatrix.M41--; + } + + if (projectionMatrix.M42 != 0) + { + projectionMatrix.M42--; + } + } + + return projectionMatrix; + } + /// /// Returns the size relative to the source for the given transformation matrix. /// @@ -293,15 +379,16 @@ internal static class TransformUtils /// /// The transformation matrix. /// The source size. + /// The used when generating the matrix. /// /// The . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Size GetTransformedSize(Matrix4x4 matrix, Size size) + public static Size GetTransformedSize(Matrix4x4 matrix, Size size, TransformSpace transformSpace) { Guard.IsTrue(size.Width > 0 && size.Height > 0, nameof(size), "Source size dimensions cannot be 0!"); - if (matrix.Equals(default) || matrix.Equals(Matrix4x4.Identity)) + if (matrix.IsIdentity || matrix.Equals(default)) { return size; } @@ -309,27 +396,7 @@ internal static class TransformUtils // Check if the matrix involves only affine transformations by inspecting the relevant components. // We want to use pixel space for calculations only if the transformation is purely 2D and does not include // any perspective effects, non-standard scaling, or unusual translations that could distort the image. - // The conditions are as follows: - bool usePixelSpace = - - // 1. Ensure there's no perspective distortion: - // M34 corresponds to the perspective component. For a purely 2D affine transformation, this should be 0. - (matrix.M34 == 0) && - - // 2. Ensure standard affine transformation without any unusual depth or perspective scaling: - // M44 should be 1 for a standard affine transformation. If M44 is not 1, it indicates non-standard depth - // scaling or perspective, which suggests a more complex transformation. - (matrix.M44 == 1) && - - // 3. Ensure no unusual translation in the x-direction: - // M14 represents translation in the x-direction that might be part of a more complex transformation. - // For standard affine transformations, M14 should be 0. - (matrix.M14 == 0) && - - // 4. Ensure no unusual translation in the y-direction: - // M24 represents translation in the y-direction that might be part of a more complex transformation. - // For standard affine transformations, M24 should be 0. - (matrix.M24 == 0); + bool usePixelSpace = transformSpace == TransformSpace.Pixel && IsAffineRotationOrSkew(matrix); // Define an offset size to translate between pixel space and coordinate space. // When using pixel space, apply a scaling sensitive offset to translate to discrete pixel coordinates. @@ -376,7 +443,7 @@ internal static class TransformUtils { Guard.IsTrue(size.Width > 0 && size.Height > 0, nameof(size), "Source size dimensions cannot be 0!"); - if (matrix.Equals(default) || matrix.Equals(Matrix3x2.Identity)) + if (matrix.IsIdentity || matrix.Equals(default)) { return size; } @@ -412,7 +479,7 @@ internal static class TransformUtils /// private static bool TryGetTransformedRectangle(RectangleF rectangle, Matrix3x2 matrix, out Rectangle bounds) { - if (rectangle.Equals(default) || Matrix3x2.Identity.Equals(matrix)) + if (matrix.IsIdentity || rectangle.Equals(default)) { bounds = default; return false; @@ -439,7 +506,7 @@ internal static class TransformUtils [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool TryGetTransformedRectangle(RectangleF rectangle, Matrix4x4 matrix, out Rectangle bounds) { - if (rectangle.Equals(default) || Matrix4x4.Identity.Equals(matrix)) + if (matrix.IsIdentity || rectangle.Equals(default)) { bounds = default; return false; @@ -492,4 +559,44 @@ internal static class TransformUtils (int)Math.Ceiling(right), (int)Math.Ceiling(bottom)); } + + private static bool IsAffineRotationOrSkew(Matrix4x4 matrix) + { + const float epsilon = 1e-6f; + + // Check if the matrix is affine (last column should be [0, 0, 0, 1]) + if (Math.Abs(matrix.M14) > epsilon || + Math.Abs(matrix.M24) > epsilon || + Math.Abs(matrix.M34) > epsilon || + Math.Abs(matrix.M44 - 1f) > epsilon) + { + return false; + } + + // Translation component (M41, m42) are allowed, others are not. + if (Math.Abs(matrix.M43) > epsilon) + { + return false; + } + + // Extract the linear (rotation and skew) part of the matrix + // Upper-left 3x3 matrix + float m11 = matrix.M11, m12 = matrix.M12, m13 = matrix.M13; + float m21 = matrix.M21, m22 = matrix.M22, m23 = matrix.M23; + float m31 = matrix.M31, m32 = matrix.M32, m33 = matrix.M33; + + // Compute the determinant of the linear part + float determinant = (m11 * ((m22 * m33) - (m23 * m32))) - + (m12 * ((m21 * m33) - (m23 * m31))) + + (m13 * ((m21 * m32) - (m22 * m31))); + + // Check if the determinant is approximately ±1 (no scaling) + if (Math.Abs(Math.Abs(determinant) - 1f) > epsilon) + { + return false; + } + + // All checks passed; the matrix represents rotation and/or skew (with possible translation) + return true; + } } diff --git a/src/ImageSharp/Processing/ProjectiveTransformBuilder.cs b/src/ImageSharp/Processing/ProjectiveTransformBuilder.cs index 9027ee7266..82b897ea5d 100644 --- a/src/ImageSharp/Processing/ProjectiveTransformBuilder.cs +++ b/src/ImageSharp/Processing/ProjectiveTransformBuilder.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing; /// public class ProjectiveTransformBuilder { - private readonly List> transformMatrixFactories = new(); + private readonly List> transformMatrixFactories = []; /// /// Initializes a new instance of the class. @@ -279,6 +279,30 @@ public class ProjectiveTransformBuilder public ProjectiveTransformBuilder AppendTranslation(Vector2 position) => this.AppendMatrix(Matrix4x4.CreateTranslation(new Vector3(position, 0))); + /// + /// Prepends a quad distortion matrix using the specified corner points. + /// + /// The top-left corner point of the distorted quad. + /// The top-right corner point of the distorted quad. + /// The bottom-right corner point of the distorted quad. + /// The bottom-left corner point of the distorted quad. + /// The . + public ProjectiveTransformBuilder PrependQuadDistortion(PointF topLeft, PointF topRight, PointF bottomRight, PointF bottomLeft) + => this.Prepend(size => TransformUtils.CreateQuadDistortionMatrix( + new Rectangle(Point.Empty, size), topLeft, topRight, bottomRight, bottomLeft, this.TransformSpace)); + + /// + /// Appends a quad distortion matrix using the specified corner points. + /// + /// The top-left corner point of the distorted quad. + /// The top-right corner point of the distorted quad. + /// The bottom-right corner point of the distorted quad. + /// The bottom-left corner point of the distorted quad. + /// The . + public ProjectiveTransformBuilder AppendQuadDistortion(PointF topLeft, PointF topRight, PointF bottomRight, PointF bottomLeft) + => this.Append(size => TransformUtils.CreateQuadDistortionMatrix( + new Rectangle(Point.Empty, size), topLeft, topRight, bottomRight, bottomLeft, this.TransformSpace)); + /// /// Prepends a raw matrix. /// @@ -361,18 +385,8 @@ public class ProjectiveTransformBuilder /// The . public Size GetTransformedSize(Rectangle sourceRectangle) { - Size size = sourceRectangle.Size; - - // Translate the origin matrix to cater for source rectangle offsets. - Matrix4x4 matrix = Matrix4x4.CreateTranslation(new Vector3(-sourceRectangle.Location, 0)); - - foreach (Func factory in this.transformMatrixFactories) - { - matrix *= factory(size); - CheckDegenerate(matrix); - } - - return TransformUtils.GetTransformedSize(matrix, size); + Matrix4x4 matrix = this.BuildMatrix(sourceRectangle); + return TransformUtils.GetTransformedSize(matrix, sourceRectangle.Size, this.TransformSpace); } private static void CheckDegenerate(Matrix4x4 matrix) diff --git a/tests/Directory.Build.targets b/tests/Directory.Build.targets index d6b35d003f..e20717cdd3 100644 --- a/tests/Directory.Build.targets +++ b/tests/Directory.Build.targets @@ -18,18 +18,23 @@ - - + + + + - - - - - + + + + + - + diff --git a/tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs b/tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs index 1b6663e70e..8728dd6715 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs @@ -47,37 +47,37 @@ public class Pad3Shuffle4Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | // |------------------------- |------------------- |-------------------------------------------------- |------ |------------:|----------:|----------:|------------:|------:|--------:|------:|------:|------:|----------:| -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 120.64 ns | 7.190 ns | 21.200 ns | 114.26 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 120.64 ns | 7.190 ns | 21.200 ns | 114.26 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4 | 2. AVX | Empty | 96 | 23.63 ns | 0.175 ns | 0.155 ns | 23.65 ns | 0.15 | 0.01 | - | - | - | - | -// | Pad3Shuffle4 | 3. SSE | COMPlus_EnableAVX=0 | 96 | 25.25 ns | 0.356 ns | 0.298 ns | 25.27 ns | 0.17 | 0.01 | - | - | - | - | +// | Pad3Shuffle4 | 3. SSE | DOTNET_EnableAVX=0 | 96 | 25.25 ns | 0.356 ns | 0.298 ns | 25.27 ns | 0.17 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 14.80 ns | 0.358 ns | 1.032 ns | 14.64 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 14.80 ns | 0.358 ns | 1.032 ns | 14.64 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4FastFallback | 2. AVX | Empty | 96 | 24.84 ns | 0.376 ns | 0.333 ns | 24.74 ns | 1.57 | 0.06 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 96 | 24.58 ns | 0.471 ns | 0.704 ns | 24.38 ns | 1.60 | 0.09 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 96 | 24.58 ns | 0.471 ns | 0.704 ns | 24.38 ns | 1.60 | 0.09 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 258.92 ns | 4.873 ns | 4.069 ns | 257.95 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 258.92 ns | 4.873 ns | 4.069 ns | 257.95 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4 | 2. AVX | Empty | 384 | 41.41 ns | 0.859 ns | 1.204 ns | 41.33 ns | 0.16 | 0.00 | - | - | - | - | -// | Pad3Shuffle4 | 3. SSE | COMPlus_EnableAVX=0 | 384 | 40.74 ns | 0.848 ns | 0.793 ns | 40.48 ns | 0.16 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 3. SSE | DOTNET_EnableAVX=0 | 384 | 40.74 ns | 0.848 ns | 0.793 ns | 40.48 ns | 0.16 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 74.50 ns | 0.490 ns | 0.383 ns | 74.49 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 74.50 ns | 0.490 ns | 0.383 ns | 74.49 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4FastFallback | 2. AVX | Empty | 384 | 40.74 ns | 0.624 ns | 0.584 ns | 40.72 ns | 0.55 | 0.01 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 384 | 38.28 ns | 0.534 ns | 0.417 ns | 38.22 ns | 0.51 | 0.01 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 384 | 38.28 ns | 0.534 ns | 0.417 ns | 38.22 ns | 0.51 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 503.91 ns | 6.466 ns | 6.048 ns | 501.58 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 503.91 ns | 6.466 ns | 6.048 ns | 501.58 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4 | 2. AVX | Empty | 768 | 62.86 ns | 0.332 ns | 0.277 ns | 62.80 ns | 0.12 | 0.00 | - | - | - | - | -// | Pad3Shuffle4 | 3. SSE | COMPlus_EnableAVX=0 | 768 | 64.59 ns | 0.469 ns | 0.415 ns | 64.62 ns | 0.13 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 3. SSE | DOTNET_EnableAVX=0 | 768 | 64.59 ns | 0.469 ns | 0.415 ns | 64.62 ns | 0.13 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 110.51 ns | 0.592 ns | 0.554 ns | 110.33 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 110.51 ns | 0.592 ns | 0.554 ns | 110.33 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4FastFallback | 2. AVX | Empty | 768 | 64.72 ns | 1.306 ns | 1.090 ns | 64.51 ns | 0.59 | 0.01 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 768 | 62.11 ns | 0.816 ns | 0.682 ns | 61.98 ns | 0.56 | 0.01 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 768 | 62.11 ns | 0.816 ns | 0.682 ns | 61.98 ns | 0.56 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 1,005.84 ns | 13.176 ns | 12.325 ns | 1,004.70 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 1,005.84 ns | 13.176 ns | 12.325 ns | 1,004.70 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4 | 2. AVX | Empty | 1536 | 110.05 ns | 0.256 ns | 0.214 ns | 110.04 ns | 0.11 | 0.00 | - | - | - | - | -// | Pad3Shuffle4 | 3. SSE | COMPlus_EnableAVX=0 | 1536 | 110.23 ns | 0.545 ns | 0.483 ns | 110.09 ns | 0.11 | 0.00 | - | - | - | - | +// | Pad3Shuffle4 | 3. SSE | DOTNET_EnableAVX=0 | 1536 | 110.23 ns | 0.545 ns | 0.483 ns | 110.09 ns | 0.11 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 220.37 ns | 1.601 ns | 1.419 ns | 220.13 ns | 1.00 | 0.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 220.37 ns | 1.601 ns | 1.419 ns | 220.13 ns | 1.00 | 0.00 | - | - | - | - | // | Pad3Shuffle4FastFallback | 2. AVX | Empty | 1536 | 111.54 ns | 2.173 ns | 2.901 ns | 111.27 ns | 0.51 | 0.01 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 1536 | 110.23 ns | 0.456 ns | 0.427 ns | 110.25 ns | 0.50 | 0.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 1536 | 110.23 ns | 0.456 ns | 0.427 ns | 110.25 ns | 0.50 | 0.00 | - | - | - | - | // 2023-02-21 // ########## @@ -94,34 +94,34 @@ public class Pad3Shuffle4Channel // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | // |------------------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|------:|------:|------:|------:|----------:| -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 57.45 ns | 0.126 ns | 0.118 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4 | 2. SSE | COMPlus_EnableAVX=0 | 96 | 14.70 ns | 0.105 ns | 0.098 ns | 0.26 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 57.45 ns | 0.126 ns | 0.118 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4 | 2. SSE | DOTNET_EnableAVX=0 | 96 | 14.70 ns | 0.105 ns | 0.098 ns | 0.26 | - | - | - | - | // | Pad3Shuffle4 | 3. AVX | Empty | 96 | 14.63 ns | 0.070 ns | 0.062 ns | 0.25 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 12.08 ns | 0.028 ns | 0.025 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 96 | 14.04 ns | 0.050 ns | 0.044 ns | 1.16 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 12.08 ns | 0.028 ns | 0.025 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 96 | 14.04 ns | 0.050 ns | 0.044 ns | 1.16 | - | - | - | - | // | Pad3Shuffle4FastFallback | 3. AVX | Empty | 96 | 13.90 ns | 0.086 ns | 0.080 ns | 1.15 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 202.67 ns | 2.010 ns | 1.678 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4 | 2. SSE | COMPlus_EnableAVX=0 | 384 | 25.54 ns | 0.060 ns | 0.053 ns | 0.13 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 202.67 ns | 2.010 ns | 1.678 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4 | 2. SSE | DOTNET_EnableAVX=0 | 384 | 25.54 ns | 0.060 ns | 0.053 ns | 0.13 | - | - | - | - | // | Pad3Shuffle4 | 3. AVX | Empty | 384 | 25.72 ns | 0.139 ns | 0.130 ns | 0.13 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 60.35 ns | 0.080 ns | 0.071 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 384 | 25.18 ns | 0.388 ns | 0.324 ns | 0.42 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 60.35 ns | 0.080 ns | 0.071 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 384 | 25.18 ns | 0.388 ns | 0.324 ns | 0.42 | - | - | - | - | // | Pad3Shuffle4FastFallback | 3. AVX | Empty | 384 | 26.21 ns | 0.067 ns | 0.059 ns | 0.43 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 393.88 ns | 1.353 ns | 1.199 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4 | 2. SSE | COMPlus_EnableAVX=0 | 768 | 39.44 ns | 0.230 ns | 0.204 ns | 0.10 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 393.88 ns | 1.353 ns | 1.199 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4 | 2. SSE | DOTNET_EnableAVX=0 | 768 | 39.44 ns | 0.230 ns | 0.204 ns | 0.10 | - | - | - | - | // | Pad3Shuffle4 | 3. AVX | Empty | 768 | 39.51 ns | 0.108 ns | 0.101 ns | 0.10 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 112.02 ns | 0.140 ns | 0.131 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 768 | 38.60 ns | 0.091 ns | 0.080 ns | 0.34 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 112.02 ns | 0.140 ns | 0.131 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 768 | 38.60 ns | 0.091 ns | 0.080 ns | 0.34 | - | - | - | - | // | Pad3Shuffle4FastFallback | 3. AVX | Empty | 768 | 38.18 ns | 0.100 ns | 0.084 ns | 0.34 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 777.95 ns | 1.719 ns | 1.342 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4 | 2. SSE | COMPlus_EnableAVX=0 | 1536 | 73.11 ns | 0.090 ns | 0.075 ns | 0.09 | - | - | - | - | +// | Pad3Shuffle4 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 777.95 ns | 1.719 ns | 1.342 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4 | 2. SSE | DOTNET_EnableAVX=0 | 1536 | 73.11 ns | 0.090 ns | 0.075 ns | 0.09 | - | - | - | - | // | Pad3Shuffle4 | 3. AVX | Empty | 1536 | 73.41 ns | 0.125 ns | 0.117 ns | 0.09 | - | - | - | - | // | | | | | | | | | | | | | -// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 218.14 ns | 0.377 ns | 0.334 ns | 1.00 | - | - | - | - | -// | Pad3Shuffle4FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 1536 | 72.55 ns | 1.418 ns | 1.184 ns | 0.33 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 218.14 ns | 0.377 ns | 0.334 ns | 1.00 | - | - | - | - | +// | Pad3Shuffle4FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 1536 | 72.55 ns | 1.418 ns | 1.184 ns | 0.33 | - | - | - | - | // | Pad3Shuffle4FastFallback | 3. AVX | Empty | 1536 | 73.15 ns | 0.330 ns | 0.292 ns | 0.34 | - | - | - | - | diff --git a/tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs b/tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs index 8b7b89eb36..e4c12900f5 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs @@ -43,21 +43,21 @@ public class Shuffle3Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | // |--------------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|----------:|------:|--------:|------:|------:|------:|----------:| -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 48.46 ns | 1.034 ns | 2.438 ns | 47.46 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 48.46 ns | 1.034 ns | 2.438 ns | 47.46 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle3 | 2. AVX | Empty | 96 | 32.42 ns | 0.537 ns | 0.476 ns | 32.34 ns | 0.66 | 0.04 | - | - | - | - | -// | Shuffle3 | 3. SSE | COMPlus_EnableAVX=0 | 96 | 32.51 ns | 0.373 ns | 0.349 ns | 32.56 ns | 0.66 | 0.03 | - | - | - | - | +// | Shuffle3 | 3. SSE | DOTNET_EnableAVX=0 | 96 | 32.51 ns | 0.373 ns | 0.349 ns | 32.56 ns | 0.66 | 0.03 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 199.04 ns | 1.512 ns | 1.180 ns | 199.17 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 199.04 ns | 1.512 ns | 1.180 ns | 199.17 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle3 | 2. AVX | Empty | 384 | 71.20 ns | 2.654 ns | 7.784 ns | 69.60 ns | 0.41 | 0.02 | - | - | - | - | -// | Shuffle3 | 3. SSE | COMPlus_EnableAVX=0 | 384 | 63.23 ns | 0.569 ns | 0.505 ns | 63.21 ns | 0.32 | 0.00 | - | - | - | - | +// | Shuffle3 | 3. SSE | DOTNET_EnableAVX=0 | 384 | 63.23 ns | 0.569 ns | 0.505 ns | 63.21 ns | 0.32 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 391.28 ns | 5.087 ns | 3.972 ns | 391.22 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 391.28 ns | 5.087 ns | 3.972 ns | 391.22 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle3 | 2. AVX | Empty | 768 | 109.12 ns | 2.149 ns | 2.010 ns | 108.66 ns | 0.28 | 0.01 | - | - | - | - | -// | Shuffle3 | 3. SSE | COMPlus_EnableAVX=0 | 768 | 106.51 ns | 0.734 ns | 0.613 ns | 106.56 ns | 0.27 | 0.00 | - | - | - | - | +// | Shuffle3 | 3. SSE | DOTNET_EnableAVX=0 | 768 | 106.51 ns | 0.734 ns | 0.613 ns | 106.56 ns | 0.27 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 773.70 ns | 5.516 ns | 4.890 ns | 772.96 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 773.70 ns | 5.516 ns | 4.890 ns | 772.96 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle3 | 2. AVX | Empty | 1536 | 190.41 ns | 1.090 ns | 0.851 ns | 190.38 ns | 0.25 | 0.00 | - | - | - | - | -// | Shuffle3 | 3. SSE | COMPlus_EnableAVX=0 | 1536 | 190.94 ns | 0.985 ns | 0.769 ns | 190.85 ns | 0.25 | 0.00 | - | - | - | - | +// | Shuffle3 | 3. SSE | DOTNET_EnableAVX=0 | 1536 | 190.94 ns | 0.985 ns | 0.769 ns | 190.85 ns | 0.25 | 0.00 | - | - | - | - | // 2023-02-21 // ########## @@ -74,18 +74,18 @@ public class Shuffle3Channel // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | // |--------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|------:|------:|------:|------:|----------:| -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 96 | 44.55 ns | 0.564 ns | 0.528 ns | 1.00 | - | - | - | - | -// | Shuffle3 | 2. SSE | COMPlus_EnableAVX=0 | 96 | 15.46 ns | 0.064 ns | 0.060 ns | 0.35 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 96 | 44.55 ns | 0.564 ns | 0.528 ns | 1.00 | - | - | - | - | +// | Shuffle3 | 2. SSE | DOTNET_EnableAVX=0 | 96 | 15.46 ns | 0.064 ns | 0.060 ns | 0.35 | - | - | - | - | // | Shuffle3 | 3. AVX | Empty | 96 | 15.18 ns | 0.056 ns | 0.053 ns | 0.34 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 384 | 155.68 ns | 0.539 ns | 0.504 ns | 1.00 | - | - | - | - | -// | Shuffle3 | 2. SSE | COMPlus_EnableAVX=0 | 384 | 30.04 ns | 0.100 ns | 0.089 ns | 0.19 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 384 | 155.68 ns | 0.539 ns | 0.504 ns | 1.00 | - | - | - | - | +// | Shuffle3 | 2. SSE | DOTNET_EnableAVX=0 | 384 | 30.04 ns | 0.100 ns | 0.089 ns | 0.19 | - | - | - | - | // | Shuffle3 | 3. AVX | Empty | 384 | 29.70 ns | 0.061 ns | 0.054 ns | 0.19 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 768 | 302.76 ns | 1.023 ns | 0.957 ns | 1.00 | - | - | - | - | -// | Shuffle3 | 2. SSE | COMPlus_EnableAVX=0 | 768 | 50.24 ns | 0.098 ns | 0.092 ns | 0.17 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 768 | 302.76 ns | 1.023 ns | 0.957 ns | 1.00 | - | - | - | - | +// | Shuffle3 | 2. SSE | DOTNET_EnableAVX=0 | 768 | 50.24 ns | 0.098 ns | 0.092 ns | 0.17 | - | - | - | - | // | Shuffle3 | 3. AVX | Empty | 768 | 49.28 ns | 0.156 ns | 0.131 ns | 0.16 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1536 | 596.53 ns | 2.675 ns | 2.503 ns | 1.00 | - | - | - | - | -// | Shuffle3 | 2. SSE | COMPlus_EnableAVX=0 | 1536 | 94.09 ns | 0.312 ns | 0.260 ns | 0.16 | - | - | - | - | +// | Shuffle3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1536 | 596.53 ns | 2.675 ns | 2.503 ns | 1.00 | - | - | - | - | +// | Shuffle3 | 2. SSE | DOTNET_EnableAVX=0 | 1536 | 94.09 ns | 0.312 ns | 0.260 ns | 0.16 | - | - | - | - | // | Shuffle3 | 3. AVX | Empty | 1536 | 93.57 ns | 0.196 ns | 0.183 ns | 0.16 | - | - | - | - | diff --git a/tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs b/tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs index 5ade55c73f..579e2c54db 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs @@ -47,45 +47,45 @@ public class Shuffle4Slice3Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | // |--------------------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|----------:|------:|--------:|------:|------:|------:|----------:| -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 56.44 ns | 2.843 ns | 8.382 ns | 56.70 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 56.44 ns | 2.843 ns | 8.382 ns | 56.70 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3 | 2. AVX | Empty | 128 | 27.15 ns | 0.556 ns | 0.762 ns | 27.34 ns | 0.41 | 0.03 | - | - | - | - | -// | Shuffle4Slice3 | 3. SSE | COMPlus_EnableAVX=0 | 128 | 26.36 ns | 0.321 ns | 0.268 ns | 26.26 ns | 0.38 | 0.02 | - | - | - | - | +// | Shuffle4Slice3 | 3. SSE | DOTNET_EnableAVX=0 | 128 | 26.36 ns | 0.321 ns | 0.268 ns | 26.26 ns | 0.38 | 0.02 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 25.85 ns | 0.494 ns | 0.462 ns | 25.84 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 25.85 ns | 0.494 ns | 0.462 ns | 25.84 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3FastFallback | 2. AVX | Empty | 128 | 26.15 ns | 0.113 ns | 0.106 ns | 26.16 ns | 1.01 | 0.02 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 128 | 25.57 ns | 0.078 ns | 0.061 ns | 25.56 ns | 0.99 | 0.02 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 128 | 25.57 ns | 0.078 ns | 0.061 ns | 25.56 ns | 0.99 | 0.02 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 97.47 ns | 0.327 ns | 0.289 ns | 97.35 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 97.47 ns | 0.327 ns | 0.289 ns | 97.35 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3 | 2. AVX | Empty | 256 | 32.61 ns | 0.107 ns | 0.095 ns | 32.62 ns | 0.33 | 0.00 | - | - | - | - | -// | Shuffle4Slice3 | 3. SSE | COMPlus_EnableAVX=0 | 256 | 33.21 ns | 0.169 ns | 0.150 ns | 33.15 ns | 0.34 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 3. SSE | DOTNET_EnableAVX=0 | 256 | 33.21 ns | 0.169 ns | 0.150 ns | 33.15 ns | 0.34 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 52.34 ns | 0.779 ns | 0.729 ns | 51.94 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 52.34 ns | 0.779 ns | 0.729 ns | 51.94 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3FastFallback | 2. AVX | Empty | 256 | 32.16 ns | 0.111 ns | 0.104 ns | 32.16 ns | 0.61 | 0.01 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 256 | 33.61 ns | 0.342 ns | 0.319 ns | 33.62 ns | 0.64 | 0.01 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 256 | 33.61 ns | 0.342 ns | 0.319 ns | 33.62 ns | 0.64 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 210.74 ns | 3.825 ns | 5.956 ns | 207.70 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 210.74 ns | 3.825 ns | 5.956 ns | 207.70 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3 | 2. AVX | Empty | 512 | 51.03 ns | 0.535 ns | 0.501 ns | 51.18 ns | 0.24 | 0.01 | - | - | - | - | -// | Shuffle4Slice3 | 3. SSE | COMPlus_EnableAVX=0 | 512 | 66.60 ns | 1.313 ns | 1.613 ns | 65.93 ns | 0.31 | 0.01 | - | - | - | - | +// | Shuffle4Slice3 | 3. SSE | DOTNET_EnableAVX=0 | 512 | 66.60 ns | 1.313 ns | 1.613 ns | 65.93 ns | 0.31 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 119.12 ns | 1.905 ns | 1.689 ns | 118.52 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 119.12 ns | 1.905 ns | 1.689 ns | 118.52 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3FastFallback | 2. AVX | Empty | 512 | 50.33 ns | 0.382 ns | 0.339 ns | 50.41 ns | 0.42 | 0.01 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 512 | 49.25 ns | 0.555 ns | 0.492 ns | 49.26 ns | 0.41 | 0.01 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 512 | 49.25 ns | 0.555 ns | 0.492 ns | 49.26 ns | 0.41 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 423.55 ns | 4.891 ns | 4.336 ns | 423.27 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 423.55 ns | 4.891 ns | 4.336 ns | 423.27 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3 | 2. AVX | Empty | 1024 | 77.13 ns | 1.355 ns | 2.264 ns | 76.19 ns | 0.19 | 0.01 | - | - | - | - | -// | Shuffle4Slice3 | 3. SSE | COMPlus_EnableAVX=0 | 1024 | 79.39 ns | 0.103 ns | 0.086 ns | 79.37 ns | 0.19 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 3. SSE | DOTNET_EnableAVX=0 | 1024 | 79.39 ns | 0.103 ns | 0.086 ns | 79.37 ns | 0.19 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 226.57 ns | 2.930 ns | 2.598 ns | 226.10 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 226.57 ns | 2.930 ns | 2.598 ns | 226.10 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3FastFallback | 2. AVX | Empty | 1024 | 80.25 ns | 1.647 ns | 2.082 ns | 80.98 ns | 0.35 | 0.01 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 1024 | 84.99 ns | 1.234 ns | 1.155 ns | 85.60 ns | 0.38 | 0.01 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 1024 | 84.99 ns | 1.234 ns | 1.155 ns | 85.60 ns | 0.38 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 794.96 ns | 1.735 ns | 1.538 ns | 795.15 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 794.96 ns | 1.735 ns | 1.538 ns | 795.15 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3 | 2. AVX | Empty | 2048 | 128.41 ns | 0.417 ns | 0.390 ns | 128.24 ns | 0.16 | 0.00 | - | - | - | - | -// | Shuffle4Slice3 | 3. SSE | COMPlus_EnableAVX=0 | 2048 | 127.24 ns | 0.294 ns | 0.229 ns | 127.23 ns | 0.16 | 0.00 | - | - | - | - | +// | Shuffle4Slice3 | 3. SSE | DOTNET_EnableAVX=0 | 2048 | 127.24 ns | 0.294 ns | 0.229 ns | 127.23 ns | 0.16 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 382.97 ns | 1.064 ns | 0.831 ns | 382.87 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 382.97 ns | 1.064 ns | 0.831 ns | 382.87 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Slice3FastFallback | 2. AVX | Empty | 2048 | 126.93 ns | 0.382 ns | 0.339 ns | 126.94 ns | 0.33 | 0.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 3. SSE | COMPlus_EnableAVX=0 | 2048 | 149.36 ns | 1.875 ns | 1.754 ns | 149.33 ns | 0.39 | 0.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 3. SSE | DOTNET_EnableAVX=0 | 2048 | 149.36 ns | 1.875 ns | 1.754 ns | 149.33 ns | 0.39 | 0.00 | - | - | - | - | // 2023-02-21 // ########## @@ -102,42 +102,42 @@ public class Shuffle4Slice3Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | // |--------------------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|------:|------:|------:|------:|----------:| -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 45.59 ns | 0.166 ns | 0.147 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3 | 2. SSE | COMPlus_EnableAVX=0 | 128 | 15.62 ns | 0.056 ns | 0.052 ns | 0.34 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 45.59 ns | 0.166 ns | 0.147 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3 | 2. SSE | DOTNET_EnableAVX=0 | 128 | 15.62 ns | 0.056 ns | 0.052 ns | 0.34 | - | - | - | - | // | Shuffle4Slice3 | 3. AVX | Empty | 128 | 16.37 ns | 0.047 ns | 0.040 ns | 0.36 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 13.23 ns | 0.028 ns | 0.026 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 128 | 14.41 ns | 0.013 ns | 0.012 ns | 1.09 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 13.23 ns | 0.028 ns | 0.026 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 128 | 14.41 ns | 0.013 ns | 0.012 ns | 1.09 | - | - | - | - | // | Shuffle4Slice3FastFallback | 3. AVX | Empty | 128 | 14.70 ns | 0.050 ns | 0.047 ns | 1.11 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 85.48 ns | 0.192 ns | 0.179 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3 | 2. SSE | COMPlus_EnableAVX=0 | 256 | 19.18 ns | 0.230 ns | 0.204 ns | 0.22 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 85.48 ns | 0.192 ns | 0.179 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3 | 2. SSE | DOTNET_EnableAVX=0 | 256 | 19.18 ns | 0.230 ns | 0.204 ns | 0.22 | - | - | - | - | // | Shuffle4Slice3 | 3. AVX | Empty | 256 | 18.66 ns | 0.017 ns | 0.015 ns | 0.22 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 24.34 ns | 0.078 ns | 0.073 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 256 | 18.58 ns | 0.061 ns | 0.057 ns | 0.76 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 24.34 ns | 0.078 ns | 0.073 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 256 | 18.58 ns | 0.061 ns | 0.057 ns | 0.76 | - | - | - | - | // | Shuffle4Slice3FastFallback | 3. AVX | Empty | 256 | 19.23 ns | 0.018 ns | 0.016 ns | 0.79 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 165.31 ns | 0.742 ns | 0.694 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3 | 2. SSE | COMPlus_EnableAVX=0 | 512 | 28.10 ns | 0.077 ns | 0.068 ns | 0.17 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 165.31 ns | 0.742 ns | 0.694 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3 | 2. SSE | DOTNET_EnableAVX=0 | 512 | 28.10 ns | 0.077 ns | 0.068 ns | 0.17 | - | - | - | - | // | Shuffle4Slice3 | 3. AVX | Empty | 512 | 28.99 ns | 0.018 ns | 0.014 ns | 0.18 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 53.45 ns | 0.270 ns | 0.226 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 512 | 27.50 ns | 0.034 ns | 0.028 ns | 0.51 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 53.45 ns | 0.270 ns | 0.226 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 512 | 27.50 ns | 0.034 ns | 0.028 ns | 0.51 | - | - | - | - | // | Shuffle4Slice3FastFallback | 3. AVX | Empty | 512 | 28.76 ns | 0.017 ns | 0.015 ns | 0.54 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 323.87 ns | 0.549 ns | 0.487 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3 | 2. SSE | COMPlus_EnableAVX=0 | 1024 | 40.81 ns | 0.056 ns | 0.050 ns | 0.13 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 323.87 ns | 0.549 ns | 0.487 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3 | 2. SSE | DOTNET_EnableAVX=0 | 1024 | 40.81 ns | 0.056 ns | 0.050 ns | 0.13 | - | - | - | - | // | Shuffle4Slice3 | 3. AVX | Empty | 1024 | 39.95 ns | 0.075 ns | 0.067 ns | 0.12 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 101.37 ns | 0.080 ns | 0.067 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 1024 | 40.72 ns | 0.049 ns | 0.041 ns | 0.40 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 101.37 ns | 0.080 ns | 0.067 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 1024 | 40.72 ns | 0.049 ns | 0.041 ns | 0.40 | - | - | - | - | // | Shuffle4Slice3FastFallback | 3. AVX | Empty | 1024 | 39.78 ns | 0.029 ns | 0.027 ns | 0.39 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3 | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 642.95 ns | 2.067 ns | 1.933 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3 | 2. SSE | COMPlus_EnableAVX=0 | 2048 | 73.19 ns | 0.082 ns | 0.077 ns | 0.11 | - | - | - | - | +// | Shuffle4Slice3 | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 642.95 ns | 2.067 ns | 1.933 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3 | 2. SSE | DOTNET_EnableAVX=0 | 2048 | 73.19 ns | 0.082 ns | 0.077 ns | 0.11 | - | - | - | - | // | Shuffle4Slice3 | 3. AVX | Empty | 2048 | 69.83 ns | 0.319 ns | 0.267 ns | 0.11 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 196.85 ns | 0.238 ns | 0.211 ns | 1.00 | - | - | - | - | -// | Shuffle4Slice3FastFallback | 2. SSE | COMPlus_EnableAVX=0 | 2048 | 72.89 ns | 0.117 ns | 0.098 ns | 0.37 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 196.85 ns | 0.238 ns | 0.211 ns | 1.00 | - | - | - | - | +// | Shuffle4Slice3FastFallback | 2. SSE | DOTNET_EnableAVX=0 | 2048 | 72.89 ns | 0.117 ns | 0.098 ns | 0.37 | - | - | - | - | // | Shuffle4Slice3FastFallback | 3. AVX | Empty | 2048 | 69.59 ns | 0.073 ns | 0.061 ns | 0.35 | - | - | - | - | diff --git a/tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs b/tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs index 911c4e0a58..6a16bb5710 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs @@ -42,25 +42,25 @@ public class ShuffleByte4Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | // |---------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:| -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 17.39 ns | 0.187 ns | 0.175 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 17.39 ns | 0.187 ns | 0.175 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 128 | 21.72 ns | 0.299 ns | 0.279 ns | 1.25 | 0.02 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 128 | 18.10 ns | 0.346 ns | 0.289 ns | 1.04 | 0.02 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 128 | 18.10 ns | 0.346 ns | 0.289 ns | 1.04 | 0.02 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 35.51 ns | 0.711 ns | 0.790 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 35.51 ns | 0.711 ns | 0.790 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 256 | 23.90 ns | 0.508 ns | 0.820 ns | 0.69 | 0.02 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 256 | 20.40 ns | 0.133 ns | 0.111 ns | 0.57 | 0.01 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 256 | 20.40 ns | 0.133 ns | 0.111 ns | 0.57 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 73.39 ns | 0.310 ns | 0.259 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 73.39 ns | 0.310 ns | 0.259 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 512 | 26.10 ns | 0.418 ns | 0.391 ns | 0.36 | 0.01 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 512 | 27.59 ns | 0.556 ns | 0.571 ns | 0.38 | 0.01 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 512 | 27.59 ns | 0.556 ns | 0.571 ns | 0.38 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 150.64 ns | 2.903 ns | 2.716 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 150.64 ns | 2.903 ns | 2.716 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 1024 | 38.67 ns | 0.801 ns | 1.889 ns | 0.24 | 0.02 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 1024 | 47.13 ns | 0.948 ns | 1.054 ns | 0.31 | 0.01 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 1024 | 47.13 ns | 0.948 ns | 1.054 ns | 0.31 | 0.01 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 315.29 ns | 5.206 ns | 6.583 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 315.29 ns | 5.206 ns | 6.583 ns | 1.00 | 0.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 2048 | 57.37 ns | 1.152 ns | 1.078 ns | 0.18 | 0.01 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 2048 | 65.75 ns | 1.198 ns | 1.600 ns | 0.21 | 0.01 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 2048 | 65.75 ns | 1.198 ns | 1.600 ns | 0.21 | 0.01 | - | - | - | - | // 2023-02-21 // ########## @@ -77,22 +77,22 @@ public class ShuffleByte4Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | // |---------------- |------------------- |-------------------------------------------------- |------ |----------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:| -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 10.76 ns | 0.033 ns | 0.029 ns | 1.00 | 0.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 128 | 11.39 ns | 0.045 ns | 0.040 ns | 1.06 | 0.01 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 10.76 ns | 0.033 ns | 0.029 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 128 | 11.39 ns | 0.045 ns | 0.040 ns | 1.06 | 0.01 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 128 | 14.05 ns | 0.029 ns | 0.024 ns | 1.31 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 32.09 ns | 0.655 ns | 1.000 ns | 1.00 | 0.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 256 | 14.03 ns | 0.047 ns | 0.041 ns | 0.44 | 0.02 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 32.09 ns | 0.655 ns | 1.000 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 256 | 14.03 ns | 0.047 ns | 0.041 ns | 0.44 | 0.02 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 256 | 15.18 ns | 0.052 ns | 0.043 ns | 0.48 | 0.03 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 59.26 ns | 0.084 ns | 0.070 ns | 1.00 | 0.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 512 | 18.80 ns | 0.036 ns | 0.034 ns | 0.32 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 59.26 ns | 0.084 ns | 0.070 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 512 | 18.80 ns | 0.036 ns | 0.034 ns | 0.32 | 0.00 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 512 | 17.69 ns | 0.038 ns | 0.034 ns | 0.30 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 112.48 ns | 0.285 ns | 0.253 ns | 1.00 | 0.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 1024 | 31.57 ns | 0.041 ns | 0.036 ns | 0.28 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 112.48 ns | 0.285 ns | 0.253 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 1024 | 31.57 ns | 0.041 ns | 0.036 ns | 0.28 | 0.00 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 1024 | 28.41 ns | 0.068 ns | 0.064 ns | 0.25 | 0.00 | - | - | - | - | // | | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 218.59 ns | 0.303 ns | 0.283 ns | 1.00 | 0.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 2048 | 53.04 ns | 0.106 ns | 0.099 ns | 0.24 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 218.59 ns | 0.303 ns | 0.283 ns | 1.00 | 0.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 2048 | 53.04 ns | 0.106 ns | 0.099 ns | 0.24 | 0.00 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 2048 | 34.74 ns | 0.061 ns | 0.054 ns | 0.16 | 0.00 | - | - | - | - | diff --git a/tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs b/tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs index 5bb3cf9165..7cc894486f 100644 --- a/tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs +++ b/tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs @@ -42,25 +42,25 @@ public class ShuffleFloat4Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | // |---------------- |------------------- |-------------------------------------------------- |------ |-----------:|----------:|----------:|------:|------:|------:|------:|----------:| -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 63.647 ns | 0.5475 ns | 0.4853 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 63.647 ns | 0.5475 ns | 0.4853 ns | 1.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 128 | 9.818 ns | 0.1457 ns | 0.1292 ns | 0.15 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 128 | 15.267 ns | 0.1005 ns | 0.0940 ns | 0.24 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 128 | 15.267 ns | 0.1005 ns | 0.0940 ns | 0.24 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 125.586 ns | 1.9312 ns | 1.8064 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 125.586 ns | 1.9312 ns | 1.8064 ns | 1.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 256 | 15.878 ns | 0.1983 ns | 0.1758 ns | 0.13 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 256 | 29.170 ns | 0.2925 ns | 0.2442 ns | 0.23 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 256 | 29.170 ns | 0.2925 ns | 0.2442 ns | 0.23 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 263.859 ns | 2.6660 ns | 2.3634 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 263.859 ns | 2.6660 ns | 2.3634 ns | 1.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 512 | 29.452 ns | 0.3334 ns | 0.3118 ns | 0.11 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 512 | 52.912 ns | 0.1932 ns | 0.1713 ns | 0.20 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 512 | 52.912 ns | 0.1932 ns | 0.1713 ns | 0.20 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 495.717 ns | 1.9850 ns | 1.8567 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 495.717 ns | 1.9850 ns | 1.8567 ns | 1.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 1024 | 53.757 ns | 0.3212 ns | 0.2847 ns | 0.11 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 1024 | 107.815 ns | 1.6201 ns | 1.3528 ns | 0.22 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 1024 | 107.815 ns | 1.6201 ns | 1.3528 ns | 0.22 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 980.134 ns | 3.7407 ns | 3.1237 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 980.134 ns | 3.7407 ns | 3.1237 ns | 1.00 | - | - | - | - | // | Shuffle4Channel | 2. AVX | Empty | 2048 | 105.120 ns | 0.6140 ns | 0.5443 ns | 0.11 | - | - | - | - | -// | Shuffle4Channel | 3. SSE | COMPlus_EnableAVX=0 | 2048 | 216.473 ns | 2.3268 ns | 2.0627 ns | 0.22 | - | - | - | - | +// | Shuffle4Channel | 3. SSE | DOTNET_EnableAVX=0 | 2048 | 216.473 ns | 2.3268 ns | 2.0627 ns | 0.22 | - | - | - | - | // 2023-02-21 // ########## @@ -77,22 +77,22 @@ public class ShuffleFloat4Channel // // | Method | Job | EnvironmentVariables | Count | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | // |---------------- |------------------- |-------------------------------------------------- |------ |-----------:|----------:|----------:|------:|------:|------:|------:|----------:| -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 128 | 57.819 ns | 0.2360 ns | 0.1970 ns | 1.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 128 | 11.564 ns | 0.0234 ns | 0.0195 ns | 0.20 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 128 | 57.819 ns | 0.2360 ns | 0.1970 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 128 | 11.564 ns | 0.0234 ns | 0.0195 ns | 0.20 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 128 | 7.770 ns | 0.0696 ns | 0.0617 ns | 0.13 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 256 | 105.282 ns | 0.2713 ns | 0.2405 ns | 1.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 256 | 19.867 ns | 0.0393 ns | 0.0348 ns | 0.19 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 256 | 105.282 ns | 0.2713 ns | 0.2405 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 256 | 19.867 ns | 0.0393 ns | 0.0348 ns | 0.19 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 256 | 17.586 ns | 0.0582 ns | 0.0544 ns | 0.17 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 512 | 200.799 ns | 0.5678 ns | 0.5033 ns | 1.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 512 | 41.137 ns | 0.1524 ns | 0.1351 ns | 0.20 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 512 | 200.799 ns | 0.5678 ns | 0.5033 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 512 | 41.137 ns | 0.1524 ns | 0.1351 ns | 0.20 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 512 | 24.040 ns | 0.0445 ns | 0.0395 ns | 0.12 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 1024 | 401.046 ns | 0.5865 ns | 0.5199 ns | 1.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 1024 | 94.904 ns | 0.4633 ns | 0.4334 ns | 0.24 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 1024 | 401.046 ns | 0.5865 ns | 0.5199 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 1024 | 94.904 ns | 0.4633 ns | 0.4334 ns | 0.24 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 1024 | 68.456 ns | 0.1192 ns | 0.0996 ns | 0.17 | - | - | - | - | // | | | | | | | | | | | | | -// | Shuffle4Channel | 1. No HwIntrinsics | COMPlus_EnableHWIntrinsic=0,COMPlus_FeatureSIMD=0 | 2048 | 772.297 ns | 0.6270 ns | 0.5558 ns | 1.00 | - | - | - | - | -// | Shuffle4Channel | 2. SSE | COMPlus_EnableAVX=0 | 2048 | 184.561 ns | 0.4319 ns | 0.4040 ns | 0.24 | - | - | - | - | +// | Shuffle4Channel | 1. No HwIntrinsics | DOTNET_EnableHWIntrinsic=0,DOTNET_FeatureSIMD=0 | 2048 | 772.297 ns | 0.6270 ns | 0.5558 ns | 1.00 | - | - | - | - | +// | Shuffle4Channel | 2. SSE | DOTNET_EnableAVX=0 | 2048 | 184.561 ns | 0.4319 ns | 0.4040 ns | 0.24 | - | - | - | - | // | Shuffle4Channel | 3. AVX | Empty | 2048 | 133.634 ns | 1.7864 ns | 1.8345 ns | 0.17 | - | - | - | - | diff --git a/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs b/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs index 2ebab5e001..7a3fc0b395 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Tga/DecodeTga.cs @@ -29,22 +29,22 @@ public class DecodeTga [Benchmark(Baseline = true, Description = "ImageMagick Tga")] public int TgaImageMagick() { - var settings = new MagickReadSettings { Format = MagickFormat.Tga }; - using var image = new MagickImage(new MemoryStream(this.data), settings); - return image.Width; + MagickReadSettings settings = new() { Format = MagickFormat.Tga }; + using MagickImage image = new(new MemoryStream(this.data), settings); + return (int)image.Width; } [Benchmark(Description = "ImageSharp Tga")] public int TgaImageSharp() { - using var image = Image.Load(this.data); + using Image image = Image.Load(this.data); return image.Width; } [Benchmark(Description = "Pfim Tga")] public int TgaPfim() { - using var image = Targa.Create(this.data, this.pfimConfig); + using Targa image = Targa.Create(this.data, this.pfimConfig); return image.Width; } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs index 6c71a62b50..4d6252c2b6 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Webp/DecodeWebp.cs @@ -44,34 +44,35 @@ public class DecodeWebp [Benchmark(Description = "Magick Lossy Webp")] public int WebpLossyMagick() { - var settings = new MagickReadSettings { Format = MagickFormat.WebP }; - using var memoryStream = new MemoryStream(this.webpLossyBytes); - using var image = new MagickImage(memoryStream, settings); - return image.Width; + MagickReadSettings settings = new() { Format = MagickFormat.WebP }; + using MemoryStream memoryStream = new(this.webpLossyBytes); + using MagickImage image = new(memoryStream, settings); + return (int)image.Width; } [Benchmark(Description = "ImageSharp Lossy Webp")] public int WebpLossy() { - using var memoryStream = new MemoryStream(this.webpLossyBytes); - using var image = Image.Load(memoryStream); + using MemoryStream memoryStream = new(this.webpLossyBytes); + using Image image = Image.Load(memoryStream); return image.Height; } [Benchmark(Description = "Magick Lossless Webp")] public int WebpLosslessMagick() { - var settings = new MagickReadSettings { Format = MagickFormat.WebP }; - using var memoryStream = new MemoryStream(this.webpLossyBytes); - using var image = new MagickImage(memoryStream, settings); - return image.Width; + MagickReadSettings settings = new() + { Format = MagickFormat.WebP }; + using MemoryStream memoryStream = new(this.webpLossyBytes); + using MagickImage image = new(memoryStream, settings); + return (int)image.Width; } [Benchmark(Description = "ImageSharp Lossless Webp")] public int WebpLossless() { - using var memoryStream = new MemoryStream(this.webpLosslessBytes); - using var image = Image.Load(memoryStream); + using MemoryStream memoryStream = new(this.webpLosslessBytes); + using Image image = Image.Load(memoryStream); return image.Height; } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs b/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs index 5be46a2220..31b6cbdde2 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Webp/EncodeWebp.cs @@ -4,6 +4,7 @@ using BenchmarkDotNet.Attributes; using ImageMagick; using ImageMagick.Formats; +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests; @@ -13,7 +14,9 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs; [MarkdownExporter] [HtmlExporter] [Config(typeof(Config.Short))] +#pragma warning disable CA1001 // Types that own disposable fields should be disposable public class EncodeWebp +#pragma warning restore CA1001 // Types that own disposable fields should be disposable { private MagickImage webpMagick; private Image webp; @@ -52,10 +55,7 @@ public class EncodeWebp AlphaCompression = WebPAlphaCompression.None, FilterStrength = 60, SnsStrength = 50, - Pass = 1, - - // 100 means off. - NearLossless = 100 + Pass = 1 }; this.webpMagick.Quality = 75; @@ -85,9 +85,6 @@ public class EncodeWebp { Lossless = true, Method = 4, - - // 100 means off. - NearLossless = 100 }; this.webpMagick.Quality = 75; @@ -106,7 +103,7 @@ public class EncodeWebp Quality = 75, // This is equal to exact = false in libwebp, which is the default. - TransparentColorMode = WebpTransparentColorMode.Clear + TransparentColorMode = TransparentColorMode.Clear }); } diff --git a/tests/ImageSharp.Benchmarks/Config.HwIntrinsics.cs b/tests/ImageSharp.Benchmarks/Config.HwIntrinsics.cs index 92f8917cf8..e21d0c76d8 100644 --- a/tests/ImageSharp.Benchmarks/Config.HwIntrinsics.cs +++ b/tests/ImageSharp.Benchmarks/Config.HwIntrinsics.cs @@ -33,24 +33,24 @@ public partial class Config // `FeatureSIMD` ends up impacting all SIMD support(including `System.Numerics`) but not things // like `LZCNT`, `BMI1`, or `BMI2` // `EnableSSE3_4` is a legacy switch that exists for compat and is basically the same as `EnableSSE3` - private const string EnableAES = "COMPlus_EnableAES"; - private const string EnableAVX = "COMPlus_EnableAVX"; - private const string EnableAVX2 = "COMPlus_EnableAVX2"; - private const string EnableBMI1 = "COMPlus_EnableBMI1"; - private const string EnableBMI2 = "COMPlus_EnableBMI2"; - private const string EnableFMA = "COMPlus_EnableFMA"; - private const string EnableHWIntrinsic = "COMPlus_EnableHWIntrinsic"; - private const string EnableLZCNT = "COMPlus_EnableLZCNT"; - private const string EnablePCLMULQDQ = "COMPlus_EnablePCLMULQDQ"; - private const string EnablePOPCNT = "COMPlus_EnablePOPCNT"; - private const string EnableSSE = "COMPlus_EnableSSE"; - private const string EnableSSE2 = "COMPlus_EnableSSE2"; - private const string EnableSSE3 = "COMPlus_EnableSSE3"; - private const string EnableSSE3_4 = "COMPlus_EnableSSE3_4"; - private const string EnableSSE41 = "COMPlus_EnableSSE41"; - private const string EnableSSE42 = "COMPlus_EnableSSE42"; - private const string EnableSSSE3 = "COMPlus_EnableSSSE3"; - private const string FeatureSIMD = "COMPlus_FeatureSIMD"; + private const string EnableAES = "DOTNET_EnableAES"; + private const string EnableAVX = "DOTNET_EnableAVX"; + private const string EnableAVX2 = "DOTNET_EnableAVX2"; + private const string EnableBMI1 = "DOTNET_EnableBMI1"; + private const string EnableBMI2 = "DOTNET_EnableBMI2"; + private const string EnableFMA = "DOTNET_EnableFMA"; + private const string EnableHWIntrinsic = "DOTNET_EnableHWIntrinsic"; + private const string EnableLZCNT = "DOTNET_EnableLZCNT"; + private const string EnablePCLMULQDQ = "DOTNET_EnablePCLMULQDQ"; + private const string EnablePOPCNT = "DOTNET_EnablePOPCNT"; + private const string EnableSSE = "DOTNET_EnableSSE"; + private const string EnableSSE2 = "DOTNET_EnableSSE2"; + private const string EnableSSE3 = "DOTNET_EnableSSE3"; + private const string EnableSSE3_4 = "DOTNET_EnableSSE3_4"; + private const string EnableSSE41 = "DOTNET_EnableSSE41"; + private const string EnableSSE42 = "DOTNET_EnableSSE42"; + private const string EnableSSSE3 = "DOTNET_EnableSSSE3"; + private const string FeatureSIMD = "DOTNET_FeatureSIMD"; public class HwIntrinsics_SSE_AVX : Config { diff --git a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj index 4408159ef4..37a991248c 100644 --- a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj +++ b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj @@ -23,7 +23,7 @@ - net8.0 + net8.0;net9.0 @@ -41,8 +41,8 @@ - - + + diff --git a/tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressRunner.cs b/tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressRunner.cs index a06784f1b1..e7d240acd3 100644 --- a/tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressRunner.cs +++ b/tests/ImageSharp.Benchmarks/LoadResizeSave/LoadResizeSaveStressRunner.cs @@ -19,6 +19,7 @@ using SystemDrawingImage = System.Drawing.Image; namespace SixLabors.ImageSharp.Benchmarks.LoadResizeSave; +[Flags] public enum JpegKind { Baseline = 1, @@ -30,7 +31,7 @@ public class LoadResizeSaveStressRunner { private const int Quality = 75; - // Set the quality for ImagSharp + // Set the quality for ImageSharp private readonly JpegEncoder imageSharpJpegEncoder = new() { Quality = Quality }; private readonly ImageCodecInfo systemDrawingJpegCodec = ImageCodecInfo.GetImageEncoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid); @@ -126,7 +127,7 @@ public class LoadResizeSaveStressRunner : Environment.ProcessorCount; int partitionSize = (int)Math.Ceiling((double)this.Images.Length / maxDegreeOfParallelism); - List tasks = new(); + List tasks = []; for (int i = 0; i < this.Images.Length; i += partitionSize) { int end = Math.Min(i + partitionSize, this.Images.Length); @@ -176,13 +177,13 @@ public class LoadResizeSaveStressRunner public void SystemDrawingResize(string input) { - using var image = SystemDrawingImage.FromFile(input, true); + using SystemDrawingImage image = SystemDrawingImage.FromFile(input, true); this.LogImageProcessed(image.Width, image.Height); - (int Width, int Height) scaled = this.ScaledSize(image.Width, image.Height, this.ThumbnailSize); - var resized = new Bitmap(scaled.Width, scaled.Height); - using var graphics = Graphics.FromImage(resized); - using var attributes = new ImageAttributes(); + (int width, int height) = this.ScaledSize(image.Width, image.Height, this.ThumbnailSize); + Bitmap resized = new(width, height); + using Graphics graphics = Graphics.FromImage(resized); + using ImageAttributes attributes = new(); attributes.SetWrapMode(WrapMode.TileFlipXY); graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingMode = CompositingMode.SourceCopy; @@ -191,8 +192,8 @@ public class LoadResizeSaveStressRunner graphics.DrawImage(image, System.Drawing.Rectangle.FromLTRB(0, 0, resized.Width, resized.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); // Save the results - using var encoderParams = new EncoderParameters(1); - using var qualityParam = new EncoderParameter(Encoder.Quality, (long)Quality); + using EncoderParameters encoderParams = new(1); + using EncoderParameter qualityParam = new(Encoder.Quality, (long)Quality); encoderParams.Param[0] = qualityParam; resized.Save(this.OutputPath(input), this.systemDrawingJpegCodec, encoderParams); } @@ -223,7 +224,7 @@ public class LoadResizeSaveStressRunner public async Task ImageSharpResizeAsync(string input) { - using FileStream output = File.Open(this.OutputPath(input), FileMode.Create); + await using FileStream output = File.Open(this.OutputPath(input), FileMode.Create); // Resize it to fit a 150x150 square. DecoderOptions options = new() @@ -246,7 +247,7 @@ public class LoadResizeSaveStressRunner public void MagickResize(string input) { - using var image = new MagickImage(input); + using MagickImage image = new(input); this.LogImageProcessed(image.Width, image.Height); // Resize it to fit a 150x150 square @@ -264,7 +265,7 @@ public class LoadResizeSaveStressRunner public void MagicScalerResize(string input) { - var settings = new ProcessImageSettings() + ProcessImageSettings settings = new() { Width = this.ThumbnailSize, Height = this.ThumbnailSize, @@ -273,19 +274,19 @@ public class LoadResizeSaveStressRunner }; // TODO: Is there a way to capture input dimensions for IncreaseTotalMegapixels? - using var output = new FileStream(this.OutputPath(input), FileMode.Create); + using FileStream output = new(this.OutputPath(input), FileMode.Create); MagicImageProcessor.ProcessImage(input, output, settings); } public void SkiaCanvasResize(string input) { - using var original = SKBitmap.Decode(input); + using SKBitmap original = SKBitmap.Decode(input); this.LogImageProcessed(original.Width, original.Height); - (int Width, int Height) scaled = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize); - using var surface = SKSurface.Create(new SKImageInfo(scaled.Width, scaled.Height, original.ColorType, original.AlphaType)); - using var paint = new SKPaint() { FilterQuality = SKFilterQuality.High }; + (int width, int height) = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize); + using SKSurface surface = SKSurface.Create(new SKImageInfo(width, height, original.ColorType, original.AlphaType)); + using SKPaint paint = new() { FilterQuality = SKFilterQuality.High }; SKCanvas canvas = surface.Canvas; - canvas.Scale((float)scaled.Width / original.Width); + canvas.Scale((float)width / original.Width); canvas.DrawBitmap(original, 0, 0, paint); canvas.Flush(); @@ -297,16 +298,16 @@ public class LoadResizeSaveStressRunner public void SkiaBitmapResize(string input) { - using var original = SKBitmap.Decode(input); + using SKBitmap original = SKBitmap.Decode(input); this.LogImageProcessed(original.Width, original.Height); - (int Width, int Height) scaled = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize); - using var resized = original.Resize(new SKImageInfo(scaled.Width, scaled.Height), SKFilterQuality.High); + (int width, int height) = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize); + using SKBitmap resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High); if (resized == null) { return; } - using var image = SKImage.FromBitmap(resized); + using SKImage image = SKImage.FromBitmap(resized); using FileStream output = File.OpenWrite(this.OutputPath(input)); image.Encode(SKEncodedImageFormat.Jpeg, Quality) .SaveTo(output); @@ -314,21 +315,21 @@ public class LoadResizeSaveStressRunner public void SkiaBitmapDecodeToTargetSize(string input) { - using var codec = SKCodec.Create(input); + using SKCodec codec = SKCodec.Create(input); SKImageInfo info = codec.Info; this.LogImageProcessed(info.Width, info.Height); - (int Width, int Height) scaled = this.ScaledSize(info.Width, info.Height, this.ThumbnailSize); - SKSizeI supportedScale = codec.GetScaledDimensions((float)scaled.Width / info.Width); + (int width, int height) = this.ScaledSize(info.Width, info.Height, this.ThumbnailSize); + SKSizeI supportedScale = codec.GetScaledDimensions((float)width / info.Width); - using var original = SKBitmap.Decode(codec, new SKImageInfo(supportedScale.Width, supportedScale.Height)); - using SKBitmap resized = original.Resize(new SKImageInfo(scaled.Width, scaled.Height), SKFilterQuality.High); + using SKBitmap original = SKBitmap.Decode(codec, new SKImageInfo(supportedScale.Width, supportedScale.Height)); + using SKBitmap resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High); if (resized == null) { return; } - using var image = SKImage.FromBitmap(resized); + using SKImage image = SKImage.FromBitmap(resized); using FileStream output = File.OpenWrite(this.OutputPath(input, nameof(this.SkiaBitmapDecodeToTargetSize))); image.Encode(SKEncodedImageFormat.Jpeg, Quality) @@ -338,7 +339,7 @@ public class LoadResizeSaveStressRunner public void NetVipsResize(string input) { // Thumbnail to fit a 150x150 square - using var thumb = NetVipsImage.Thumbnail(input, this.ThumbnailSize, this.ThumbnailSize); + using NetVipsImage thumb = NetVipsImage.Thumbnail(input, this.ThumbnailSize, this.ThumbnailSize); // Save the results thumb.Jpegsave(this.OutputPath(input), q: Quality, keep: NetVips.Enums.ForeignKeep.None); diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/ImageSharp.Tests.ProfilingSandbox.csproj b/tests/ImageSharp.Tests.ProfilingSandbox/ImageSharp.Tests.ProfilingSandbox.csproj index b93d011910..832f3d171f 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/ImageSharp.Tests.ProfilingSandbox.csproj +++ b/tests/ImageSharp.Tests.ProfilingSandbox/ImageSharp.Tests.ProfilingSandbox.csproj @@ -19,7 +19,7 @@ - net8.0 + net8.0;net9.0 diff --git a/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs b/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs new file mode 100644 index 0000000000..95b8d2013f --- /dev/null +++ b/tests/ImageSharp.Tests/Common/GaussianEliminationSolverTest.cs @@ -0,0 +1,66 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.Processing.Processors.Transforms.Linear; + +namespace SixLabors.ImageSharp.Tests.Common; + +public class GaussianEliminationSolverTest +{ + [Theory] + [MemberData(nameof(MatrixTestData))] + public void CanSolve(double[][] matrix, double[] result, double[] expected) + { + GaussianEliminationSolver.Solve(matrix, result); + + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(result[i], expected[i], 4); + } + } + + public static TheoryData MatrixTestData + { + get + { + TheoryData data = []; + { + double[][] matrix = + [ + [2, 3, 4], + [1, 2, 3], + [3, -4, 0], + ]; + double[] result = [6, 4, 10]; + double[] expected = [18 / 11f, -14 / 11f, 18 / 11f]; + data.Add(matrix, result, expected); + } + + { + double[][] matrix = + [ + [1, 4, -1], + [2, 5, 8], + [1, 3, -3], + ]; + double[] result = [4, 15, 1]; + double[] expected = [1, 1, 1]; + data.Add(matrix, result, expected); + } + + { + double[][] matrix = + [ + [-1, 0, 0], + [0, 1, 0], + [0, 0, 1], + ]; + double[] result = [1, 2, 3]; + double[] expected = [-1, 2, 3]; + data.Add(matrix, result, expected); + } + + return data; + } + } +} diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs index d68ec47557..09ef49a61e 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs @@ -397,6 +397,66 @@ public class BmpEncoderTests reencodedImage.CompareToOriginal(provider); } + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + BmpEncoder encoder = new() + { + BitsPerPixel = BmpBitsPerPixel.Bit32, + SupportTransparency = true, + TransparentColorMode = TransparentColorMode.Clear, + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } + private static void TestBmpEncoderCore( TestImageProvider provider, BmpBitsPerPixel bitsPerPixel, diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index 77ac51e8a1..f12f66186e 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -334,7 +334,7 @@ public class GifEncoderTests Assert.Equal(webpF.FrameDelay, (uint)(gifF.FrameDelay * 10)); - switch (webpF.DisposalMethod) + switch (webpF.DisposalMode) { case FrameDisposalMode.RestoreToBackground: Assert.Equal(FrameDisposalMode.RestoreToBackground, gifF.DisposalMode); @@ -361,4 +361,62 @@ public class GifEncoderTests provider.Utility.SaveTestOutputFile(image, "png", new PngEncoder(), "animated"); provider.Utility.SaveTestOutputFile(image, "gif", new GifEncoder(), "animated"); } + + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + GifEncoder encoder = new() + { + TransparentColorMode = TransparentColorMode.Clear, + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurDecoderTests.cs index f7ee7614af..bac52fc728 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurDecoderTests.cs @@ -20,8 +20,8 @@ public class CurDecoderTests using Image image = provider.GetImage(CurDecoder.Instance); CurFrameMetadata meta = image.Frames[0].Metadata.GetCurMetadata(); - Assert.Equal(image.Width, meta.EncodingWidth); - Assert.Equal(image.Height, meta.EncodingHeight); + Assert.Equal(image.Width, meta.EncodingWidth.Value); + Assert.Equal(image.Height, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit32, meta.BmpBitsPerPixel); } @@ -33,8 +33,8 @@ public class CurDecoderTests { using Image image = provider.GetImage(CurDecoder.Instance); CurFrameMetadata meta = image.Frames[0].Metadata.GetCurMetadata(); - Assert.Equal(image.Width, meta.EncodingWidth); - Assert.Equal(image.Height, meta.EncodingHeight); + Assert.Equal(image.Width, meta.EncodingWidth.Value); + Assert.Equal(image.Height, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit32, meta.BmpBitsPerPixel); } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs index 59c40c9245..bf94e1d489 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Cur; using SixLabors.ImageSharp.Formats.Ico; using SixLabors.ImageSharp.PixelFormats; @@ -63,4 +64,70 @@ public class CurEncoderTests Assert.Equal(icoFrame.EncodingHeight, curFrame.EncodingHeight); } } + + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + CurEncoder encoder = new() + { + TransparentColorMode = TransparentColorMode.Clear, + + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + Span rowSpanOpp = accessor.GetRowSpan(accessor.Height - y - 1); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + if (expectedColor != rowSpan[x]) + { + var xx = 0; + } + + + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoDecoderTests.cs index bc46df0955..e076ccab60 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoDecoderTests.cs @@ -53,8 +53,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit1, meta.BmpBitsPerPixel); } @@ -89,8 +89,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit24, meta.BmpBitsPerPixel); } @@ -125,8 +125,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit32, meta.BmpBitsPerPixel); } @@ -160,8 +160,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit4, meta.BmpBitsPerPixel); } @@ -196,8 +196,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit8, meta.BmpBitsPerPixel); } @@ -226,8 +226,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Png, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit32, meta.BmpBitsPerPixel); } @@ -324,8 +324,8 @@ public class IcoDecoderTests int expectedWidth = image.Width >= 256 ? 0 : image.Width; int expectedHeight = image.Height >= 256 ? 0 : image.Height; - Assert.Equal(expectedWidth, meta.EncodingWidth); - Assert.Equal(expectedHeight, meta.EncodingHeight); + Assert.Equal(expectedWidth, meta.EncodingWidth.Value); + Assert.Equal(expectedHeight, meta.EncodingHeight.Value); Assert.Equal(IconFrameCompression.Bmp, meta.Compression); Assert.Equal(BmpBitsPerPixel.Bit32, meta.BmpBitsPerPixel); } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs index 751db384d7..4c7438d568 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Cur; using SixLabors.ImageSharp.Formats.Ico; using SixLabors.ImageSharp.PixelFormats; @@ -62,4 +63,62 @@ public class IcoEncoderTests Assert.Equal(curFrame.ColorTable, icoFrame.ColorTable); } } + + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + IcoEncoder encoder = new() + { + TransparentColorMode = TransparentColorMode.Clear, + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index 009327c17f..b4995d77b6 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -340,13 +340,13 @@ public partial class PngEncoderTests [InlineData(PngColorType.Palette)] [InlineData(PngColorType.RgbWithAlpha)] [InlineData(PngColorType.GrayscaleWithAlpha)] - public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType colorType) + public void Encode_WithTransparentColorBehaviorClear_Works(PngColorType colorType) { // arrange - Image image = new(50, 50); + using Image image = new(50, 50); PngEncoder encoder = new() { - TransparentColorMode = PngTransparentColorMode.Clear, + TransparentColorMode = TransparentColorMode.Clear, ColorType = colorType }; Rgba32 rgba32 = Color.Blue.ToPixel(); @@ -524,7 +524,7 @@ public partial class PngEncoderTests Assert.Equal(webpF.FrameDelay, (uint)(pngF.FrameDelay.ToDouble() * 1000)); - switch (webpF.BlendMethod) + switch (webpF.BlendMode) { case FrameBlendMode.Source: Assert.Equal(FrameBlendMode.Source, pngF.BlendMode); @@ -535,7 +535,7 @@ public partial class PngEncoderTests break; } - switch (webpF.DisposalMethod) + switch (webpF.DisposalMode) { case FrameDisposalMode.RestoreToBackground: Assert.Equal(FrameDisposalMode.RestoreToBackground, pngF.DisposalMode); diff --git a/tests/ImageSharp.Tests/Formats/Qoi/QoiEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Qoi/QoiEncoderTests.cs index 32ade4a1e9..9da9ad3275 100644 --- a/tests/ImageSharp.Tests/Formats/Qoi/QoiEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Qoi/QoiEncoderTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Qoi; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; @@ -41,4 +42,62 @@ public class QoiEncoderTests Assert.Equal(qoiMetadata.Channels, channels); Assert.Equal(qoiMetadata.ColorSpace, colorSpace); } + + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + QoiEncoder encoder = new() + { + TransparentColorMode = TransparentColorMode.Clear, + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs index 615e0fc921..adf0b4353d 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaEncoderTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Tga; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; @@ -10,6 +11,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Tga; namespace SixLabors.ImageSharp.Tests.Formats.Tga; [Trait("Format", "Tga")] +[ValidateDisposedMemoryAllocations] public class TgaEncoderTests { public static readonly TheoryData BitsPerPixel = @@ -32,43 +34,35 @@ public class TgaEncoderTests [MemberData(nameof(TgaBitsPerPixelFiles))] public void TgaEncoder_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel) { - var options = new TgaEncoder(); + TgaEncoder options = new(); - var testFile = TestFile.Create(imagePath); - using (Image input = testFile.CreateRgba32Image()) - { - using (var memStream = new MemoryStream()) - { - input.Save(memStream, options); - memStream.Position = 0; - using (var output = Image.Load(memStream)) - { - TgaMetadata meta = output.Metadata.GetTgaMetadata(); - Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); - } - } - } + TestFile testFile = TestFile.Create(imagePath); + using Image input = testFile.CreateRgba32Image(); + using MemoryStream memStream = new(); + + input.Save(memStream, options); + memStream.Position = 0; + + using Image output = Image.Load(memStream); + TgaMetadata meta = output.Metadata.GetTgaMetadata(); + Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); } [Theory] [MemberData(nameof(TgaBitsPerPixelFiles))] public void TgaEncoder_WithCompression_PreserveBitsPerPixel(string imagePath, TgaBitsPerPixel bmpBitsPerPixel) { - var options = new TgaEncoder() { Compression = TgaCompression.RunLength }; - var testFile = TestFile.Create(imagePath); - using (Image input = testFile.CreateRgba32Image()) - { - using (var memStream = new MemoryStream()) - { - input.Save(memStream, options); - memStream.Position = 0; - using (var output = Image.Load(memStream)) - { - TgaMetadata meta = output.Metadata.GetTgaMetadata(); - Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); - } - } - } + TgaEncoder options = new() { Compression = TgaCompression.RunLength }; + TestFile testFile = TestFile.Create(imagePath); + using Image input = testFile.CreateRgba32Image(); + using MemoryStream memStream = new(); + + input.Save(memStream, options); + memStream.Position = 0; + + using Image output = Image.Load(memStream); + TgaMetadata meta = output.Metadata.GetTgaMetadata(); + Assert.Equal(bmpBitsPerPixel, meta.BitsPerPixel); } [Theory] @@ -136,17 +130,13 @@ public class TgaEncoderTests [Fact] public void TgaEncoder_RunLengthDoesNotCrossRowBoundaries() { - var options = new TgaEncoder() { Compression = TgaCompression.RunLength }; + TgaEncoder options = new() { Compression = TgaCompression.RunLength }; - using (var input = new Image(30, 30)) - { - using (var memStream = new MemoryStream()) - { - input.Save(memStream, options); - byte[] imageBytes = memStream.ToArray(); - Assert.Equal(138, imageBytes.Length); - } - } + using Image input = new(30, 30); + using MemoryStream memStream = new(); + input.Save(memStream, options); + byte[] imageBytes = memStream.ToArray(); + Assert.Equal(138, imageBytes.Length); } [Theory] @@ -159,6 +149,65 @@ public class TgaEncoderTests TestTgaEncoderCore(provider, bitsPerPixel, TgaCompression.RunLength); } + [Fact] + public void Encode_WithTransparentColorBehaviorClear_Works() + { + // arrange + using Image image = new(50, 50); + TgaEncoder encoder = new() + { + BitsPerPixel = TgaBitsPerPixel.Bit32, + TransparentColorMode = TransparentColorMode.Clear, + }; + Rgba32 rgba32 = Color.Blue.ToPixel(); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < image.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + // Half of the test image should be transparent. + if (y > 25) + { + rgba32.A = 0; + } + + for (int x = 0; x < image.Width; x++) + { + rowSpan[x] = Rgba32.FromRgba32(rgba32); + } + } + }); + + // act + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + + // assert + memStream.Position = 0; + using Image actual = Image.Load(memStream); + Rgba32 expectedColor = Color.Blue.ToPixel(); + + actual.ProcessPixelRows(accessor => + { + Rgba32 transparent = Color.Transparent.ToPixel(); + for (int y = 0; y < accessor.Height; y++) + { + Span rowSpan = accessor.GetRowSpan(y); + + if (y > 25) + { + expectedColor = transparent; + } + + for (int x = 0; x < accessor.Width; x++) + { + Assert.Equal(expectedColor, rowSpan[x]); + } + } + }); + } + private static void TestTgaEncoderCore( TestImageProvider provider, TgaBitsPerPixel bitsPerPixel, @@ -167,20 +216,15 @@ public class TgaEncoderTests float compareTolerance = 0.01f) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var encoder = new TgaEncoder { BitsPerPixel = bitsPerPixel, Compression = compression }; + using Image image = provider.GetImage(); + TgaEncoder encoder = new() { BitsPerPixel = bitsPerPixel, Compression = compression }; - using (var memStream = new MemoryStream()) - { - image.DebugSave(provider, encoder); - image.Save(memStream, encoder); - memStream.Position = 0; - using (var encodedImage = (Image)Image.Load(memStream)) - { - ImageComparingUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance); - } - } - } + using MemoryStream memStream = new(); + image.DebugSave(provider, encoder); + image.Save(memStream, encoder); + memStream.Position = 0; + + using Image encodedImage = (Image)Image.Load(memStream); + ImageComparingUtils.CompareWithReferenceDecoder(provider, encodedImage, useExactComparer, compareTolerance); } } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 8b4aa3d706..09cfe2cbea 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -719,7 +719,7 @@ public class TiffDecoderTests : TiffDecoderBaseTester // ImageMagick cannot decode this image. image.DebugSave(provider); image.CompareToReferenceOutput( - ImageComparer.Exact, + ImageComparer.TolerantPercentage(0.0018F), // NET 9+ Uses zlib-ng to decompress, which manages to decode 2 extra pixels. provider, appendPixelTypeToFileName: false); } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs index 0d59625ca7..674ca5c5bb 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs @@ -256,7 +256,7 @@ public class TiffEncoderTests : TiffEncoderBaseTester TiffEncoder tiffEncoder = new(); using MemoryStream memStream = new(); using Image image = new(1, 1); - byte[] expectedIfdOffsetBytes = { 12, 0 }; + byte[] expectedIfdOffsetBytes = [12, 0]; // act image.Save(memStream, tiffEncoder); @@ -613,8 +613,7 @@ public class TiffEncoderTests : TiffEncoderBaseTester provider.LimitAllocatorBufferCapacity().InPixelsSqrt(200); using Image image = provider.GetImage(); - TiffEncoder encoder = new() - { PhotometricInterpretation = photometricInterpretation }; + TiffEncoder encoder = new() { PhotometricInterpretation = photometricInterpretation }; image.DebugSave(provider, encoder); } } diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index 10492af8a7..f82fa65df8 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -98,13 +98,13 @@ public class WebpEncoderTests switch (gifF.DisposalMode) { case FrameDisposalMode.RestoreToBackground: - Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMethod); + Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMode); break; case FrameDisposalMode.RestoreToPrevious: case FrameDisposalMode.Unspecified: case FrameDisposalMode.DoNotDispose: default: - Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMethod); + Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMode); break; } } @@ -147,22 +147,22 @@ public class WebpEncoderTests switch (pngF.BlendMode) { case FrameBlendMode.Source: - Assert.Equal(FrameBlendMode.Source, webpF.BlendMethod); + Assert.Equal(FrameBlendMode.Source, webpF.BlendMode); break; case FrameBlendMode.Over: default: - Assert.Equal(FrameBlendMode.Over, webpF.BlendMethod); + Assert.Equal(FrameBlendMode.Over, webpF.BlendMode); break; } switch (pngF.DisposalMode) { case FrameDisposalMode.RestoreToBackground: - Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMethod); + Assert.Equal(FrameDisposalMode.RestoreToBackground, webpF.DisposalMode); break; case FrameDisposalMode.DoNotDispose: default: - Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMethod); + Assert.Equal(FrameDisposalMode.DoNotDispose, webpF.DisposalMode); break; } } @@ -311,7 +311,7 @@ public class WebpEncoderTests { FileFormat = WebpFileFormatType.Lossless, Method = method, - TransparentColorMode = WebpTransparentColorMode.Preserve + TransparentColorMode = TransparentColorMode.Preserve }; using Image image = provider.GetImage(); @@ -516,6 +516,21 @@ public class WebpEncoderTests image.VerifyEncoder(provider, "webp", string.Empty, encoder); } + // https://github.com/SixLabors/ImageSharp/issues/2801 + [Theory] + [WithFile(Lossy.Issue2801, PixelTypes.Rgba32)] + public void WebpDecoder_CanDecode_Issue2801(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + WebpEncoder encoder = new() + { + Quality = 100 + }; + + using Image image = provider.GetImage(); + image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.TolerantPercentage(0.0994F)); + } + public static void RunEncodeLossy_WithPeakImage() { TestImageProvider provider = TestImageProvider.File(TestImageLossyFullPath); @@ -531,6 +546,44 @@ public class WebpEncoderTests [Fact] public void RunEncodeLossy_WithPeakImage_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunEncodeLossy_WithPeakImage, HwIntrinsics.DisableHWIntrinsic); + [Theory] + [WithFile(TestPatternOpaque, PixelTypes.Rgba32)] + public void CanSave_NonSeekableStream(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + WebpEncoder encoder = new(); + + using MemoryStream seekable = new(); + image.Save(seekable, encoder); + + using MemoryStream memoryStream = new(); + using NonSeekableStream nonSeekable = new(memoryStream); + + image.Save(nonSeekable, encoder); + + Assert.True(seekable.ToArray().SequenceEqual(memoryStream.ToArray())); + } + + [Theory] + [WithFile(TestPatternOpaque, PixelTypes.Rgba32)] + public async Task CanSave_NonSeekableStream_Async(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + WebpEncoder encoder = new(); + + await using MemoryStream seekable = new(); + image.Save(seekable, encoder); + + await using MemoryStream memoryStream = new(); + await using NonSeekableStream nonSeekable = new(memoryStream); + + await image.SaveAsync(nonSeekable, encoder); + + Assert.True(seekable.ToArray().SequenceEqual(memoryStream.ToArray())); + } + private static ImageComparer GetComparer(int quality) { float tolerance = 0.01f; // ~1.0% diff --git a/tests/ImageSharp.Tests/IO/ChunkedMemoryStreamTests.cs b/tests/ImageSharp.Tests/IO/ChunkedMemoryStreamTests.cs index 1803cfddb9..390170cfef 100644 --- a/tests/ImageSharp.Tests/IO/ChunkedMemoryStreamTests.cs +++ b/tests/ImageSharp.Tests/IO/ChunkedMemoryStreamTests.cs @@ -13,6 +13,8 @@ namespace SixLabors.ImageSharp.Tests.IO; /// public class ChunkedMemoryStreamTests { + private readonly Random bufferFiller = new(123); + /// /// The default length in bytes of each buffer chunk when allocating large buffers. /// @@ -30,7 +32,7 @@ public class ChunkedMemoryStreamTests [Fact] public void MemoryStream_GetPositionTest_Negative() { - using var ms = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream ms = new(this.allocator); long iCurrentPos = ms.Position; for (int i = -1; i > -6; i--) { @@ -42,7 +44,7 @@ public class ChunkedMemoryStreamTests [Fact] public void MemoryStream_ReadTest_Negative() { - var ms2 = new ChunkedMemoryStream(this.allocator); + ChunkedMemoryStream ms2 = new(this.allocator); Assert.Throws(() => ms2.Read(null, 0, 0)); Assert.Throws(() => ms2.Read(new byte[] { 1 }, -1, 0)); @@ -64,7 +66,7 @@ public class ChunkedMemoryStreamTests public void MemoryStream_ReadByteTest(int length) { using MemoryStream ms = this.CreateTestStream(length); - using var cms = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream cms = new(this.allocator); ms.CopyTo(cms); cms.Position = 0; @@ -85,7 +87,7 @@ public class ChunkedMemoryStreamTests public void MemoryStream_ReadByteBufferTest(int length) { using MemoryStream ms = this.CreateTestStream(length); - using var cms = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream cms = new(this.allocator); ms.CopyTo(cms); cms.Position = 0; @@ -105,10 +107,11 @@ public class ChunkedMemoryStreamTests [InlineData(DefaultSmallChunkSize * 4)] [InlineData((int)(DefaultSmallChunkSize * 5.5))] [InlineData(DefaultSmallChunkSize * 16)] + [InlineData(DefaultSmallChunkSize * 32)] public void MemoryStream_ReadByteBufferSpanTest(int length) { using MemoryStream ms = this.CreateTestStream(length); - using var cms = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream cms = new(this.allocator); ms.CopyTo(cms); cms.Position = 0; @@ -122,18 +125,24 @@ public class ChunkedMemoryStreamTests } } - [Fact] - public void MemoryStream_WriteToTests() + [Theory] + [InlineData(DefaultSmallChunkSize)] + [InlineData((int)(DefaultSmallChunkSize * 1.5))] + [InlineData(DefaultSmallChunkSize * 4)] + [InlineData((int)(DefaultSmallChunkSize * 5.5))] + [InlineData(DefaultSmallChunkSize * 16)] + [InlineData(DefaultSmallChunkSize * 32)] + public void MemoryStream_WriteToTests(int length) { - using (var ms2 = new ChunkedMemoryStream(this.allocator)) + using (ChunkedMemoryStream ms2 = new(this.allocator)) { byte[] bytArrRet; - byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; + byte[] bytArr = this.CreateTestBuffer(length); // [] Write to memoryStream, check the memoryStream ms2.Write(bytArr, 0, bytArr.Length); - using var readonlyStream = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream readonlyStream = new(this.allocator); ms2.WriteTo(readonlyStream); readonlyStream.Flush(); readonlyStream.Position = 0; @@ -146,11 +155,11 @@ public class ChunkedMemoryStreamTests } // [] Write to memoryStream, check the memoryStream - using (var ms2 = new ChunkedMemoryStream(this.allocator)) - using (var ms3 = new ChunkedMemoryStream(this.allocator)) + using (ChunkedMemoryStream ms2 = new(this.allocator)) + using (ChunkedMemoryStream ms3 = new(this.allocator)) { byte[] bytArrRet; - byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; + byte[] bytArr = this.CreateTestBuffer(length); ms2.Write(bytArr, 0, bytArr.Length); ms2.WriteTo(ms3); @@ -164,21 +173,29 @@ public class ChunkedMemoryStreamTests } } - [Fact] - public void MemoryStream_WriteToSpanTests() + [Theory] + [InlineData(DefaultSmallChunkSize)] + [InlineData((int)(DefaultSmallChunkSize * 1.5))] + [InlineData(DefaultSmallChunkSize * 4)] + [InlineData((int)(DefaultSmallChunkSize * 5.5))] + [InlineData(DefaultSmallChunkSize * 16)] + [InlineData(DefaultSmallChunkSize * 32)] + public void MemoryStream_WriteToSpanTests(int length) { - using (var ms2 = new ChunkedMemoryStream(this.allocator)) + using (ChunkedMemoryStream ms2 = new(this.allocator)) { Span bytArrRet; - Span bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; + Span bytArr = this.CreateTestBuffer(length); // [] Write to memoryStream, check the memoryStream ms2.Write(bytArr, 0, bytArr.Length); - using var readonlyStream = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream readonlyStream = new(this.allocator); ms2.WriteTo(readonlyStream); + readonlyStream.Flush(); readonlyStream.Position = 0; + bytArrRet = new byte[(int)readonlyStream.Length]; readonlyStream.Read(bytArrRet, 0, (int)readonlyStream.Length); for (int i = 0; i < bytArr.Length; i++) @@ -188,13 +205,14 @@ public class ChunkedMemoryStreamTests } // [] Write to memoryStream, check the memoryStream - using (var ms2 = new ChunkedMemoryStream(this.allocator)) - using (var ms3 = new ChunkedMemoryStream(this.allocator)) + using (ChunkedMemoryStream ms2 = new(this.allocator)) + using (ChunkedMemoryStream ms3 = new(this.allocator)) { Span bytArrRet; - Span bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; + Span bytArr = this.CreateTestBuffer(length); ms2.Write(bytArr, 0, bytArr.Length); + ms2.WriteTo(ms3); ms3.Position = 0; bytArrRet = new byte[(int)ms3.Length]; @@ -209,37 +227,35 @@ public class ChunkedMemoryStreamTests [Fact] public void MemoryStream_WriteByteTests() { - using (var ms2 = new ChunkedMemoryStream(this.allocator)) - { - byte[] bytArrRet; - byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; + using ChunkedMemoryStream ms2 = new(this.allocator); + byte[] bytArrRet; + byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 }; - for (int i = 0; i < bytArr.Length; i++) - { - ms2.WriteByte(bytArr[i]); - } + for (int i = 0; i < bytArr.Length; i++) + { + ms2.WriteByte(bytArr[i]); + } - using var readonlyStream = new ChunkedMemoryStream(this.allocator); - ms2.WriteTo(readonlyStream); - readonlyStream.Flush(); - readonlyStream.Position = 0; - bytArrRet = new byte[(int)readonlyStream.Length]; - readonlyStream.Read(bytArrRet, 0, (int)readonlyStream.Length); - for (int i = 0; i < bytArr.Length; i++) - { - Assert.Equal(bytArr[i], bytArrRet[i]); - } + using ChunkedMemoryStream readonlyStream = new(this.allocator); + ms2.WriteTo(readonlyStream); + readonlyStream.Flush(); + readonlyStream.Position = 0; + bytArrRet = new byte[(int)readonlyStream.Length]; + readonlyStream.Read(bytArrRet, 0, (int)readonlyStream.Length); + for (int i = 0; i < bytArr.Length; i++) + { + Assert.Equal(bytArr[i], bytArrRet[i]); } } [Fact] public void MemoryStream_WriteToTests_Negative() { - using var ms2 = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream ms2 = new(this.allocator); Assert.Throws(() => ms2.WriteTo(null)); ms2.Write(new byte[] { 1 }, 0, 1); - var readonlyStream = new MemoryStream(new byte[1028], false); + MemoryStream readonlyStream = new(new byte[1028], false); Assert.Throws(() => ms2.WriteTo(readonlyStream)); readonlyStream.Dispose(); @@ -286,7 +302,7 @@ public class ChunkedMemoryStreamTests [MemberData(nameof(CopyToData))] public void CopyTo(Stream source, byte[] expected) { - using var destination = new ChunkedMemoryStream(this.allocator); + using ChunkedMemoryStream destination = new(this.allocator); source.CopyTo(destination); Assert.InRange(source.Position, source.Length, int.MaxValue); // Copying the data should have read to the end of the stream or stayed past the end. Assert.Equal(expected, destination.ToArray()); @@ -297,16 +313,16 @@ public class ChunkedMemoryStreamTests IEnumerable allImageFiles = Directory.EnumerateFiles(TestEnvironment.InputImagesDirectoryFullPath, "*.*", SearchOption.AllDirectories) .Where(s => !s.EndsWith("txt", StringComparison.OrdinalIgnoreCase)); - var result = new List(); + List result = new(); foreach (string path in allImageFiles) { - result.Add(path.Substring(TestEnvironment.InputImagesDirectoryFullPath.Length)); + result.Add(path[TestEnvironment.InputImagesDirectoryFullPath.Length..]); } return result; } - public static IEnumerable AllTestImages = GetAllTestImages(); + public static IEnumerable AllTestImages { get; } = GetAllTestImages(); [Theory] [WithFileCollection(nameof(AllTestImages), PixelTypes.Rgba32)] @@ -334,40 +350,77 @@ public class ChunkedMemoryStreamTests ((TestImageProvider.FileProvider)provider).FilePath); using FileStream fs = File.OpenRead(fullPath); - using var nonSeekableStream = new NonSeekableStream(fs); + using NonSeekableStream nonSeekableStream = new(fs); + + using Image actual = Image.Load(nonSeekableStream); + + ImageComparer.Exact.VerifySimilarity(expected, actual); + expected.Dispose(); + } + + [Theory] + [WithFileCollection(nameof(AllTestImages), PixelTypes.Rgba32)] + public void EncoderIntegrationTest(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + if (!TestEnvironment.Is64BitProcess) + { + return; + } + + Image expected; + try + { + expected = provider.GetImage(); + } + catch + { + // The image is invalid + return; + } - var actual = Image.Load(nonSeekableStream); + string fullPath = Path.Combine( + TestEnvironment.InputImagesDirectoryFullPath, + ((TestImageProvider.FileProvider)provider).FilePath); + + using MemoryStream ms = new(); + using NonSeekableStream nonSeekableStream = new(ms); + expected.SaveAsWebp(nonSeekableStream); + + using Image actual = Image.Load(nonSeekableStream); ImageComparer.Exact.VerifySimilarity(expected, actual); + expected.Dispose(); } public static IEnumerable CopyToData() { // Stream is positioned @ beginning of data byte[] data1 = new byte[] { 1, 2, 3 }; - var stream1 = new MemoryStream(data1); + MemoryStream stream1 = new(data1); yield return new object[] { stream1, data1 }; // Stream is positioned in the middle of data byte[] data2 = new byte[] { 0xff, 0xf3, 0xf0 }; - var stream2 = new MemoryStream(data2) { Position = 1 }; + MemoryStream stream2 = new(data2) { Position = 1 }; yield return new object[] { stream2, new byte[] { 0xf3, 0xf0 } }; // Stream is positioned after end of data byte[] data3 = data2; - var stream3 = new MemoryStream(data3) { Position = data3.Length + 1 }; + MemoryStream stream3 = new(data3) { Position = data3.Length + 1 }; yield return new object[] { stream3, Array.Empty() }; } - private MemoryStream CreateTestStream(int length) + private byte[] CreateTestBuffer(int length) { byte[] buffer = new byte[length]; - var random = new Random(); - random.NextBytes(buffer); - - return new MemoryStream(buffer); + this.bufferFiller.NextBytes(buffer); + return buffer; } + + private MemoryStream CreateTestStream(int length) + => new(this.CreateTestBuffer(length)); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs index d8d9f4fe2a..0656f9e0bc 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs @@ -12,8 +12,8 @@ public partial class ImageTests { public Decode_Cancellation() => this.TopLevelConfiguration.StreamProcessingBufferSize = 128; - public static readonly string[] TestFileForEachCodec = new[] - { + public static readonly string[] TestFileForEachCodec = + [ TestImages.Jpeg.Baseline.Snake, // TODO: Figure out Unix cancellation failures, and validate cancellation for each decoder. @@ -24,7 +24,7 @@ public partial class ImageTests //TestImages.Tga.Bit32BottomRight, //TestImages.Webp.Lossless.WithExif, //TestImages.Pbm.GrayscaleBinaryWide - }; + ]; public static object[][] IdentifyData { get; } = TestFileForEachCodec.Select(f => new object[] { f }).ToArray(); @@ -32,16 +32,16 @@ public partial class ImageTests [MemberData(nameof(IdentifyData))] public async Task IdentifyAsync_PreCancelled(string file) { - using FileStream fs = File.OpenRead(TestFile.GetInputFileFullPath(file)); + await using FileStream fs = File.OpenRead(TestFile.GetInputFileFullPath(file)); CancellationToken preCancelled = new(canceled: true); await Assert.ThrowsAnyAsync(async () => await Image.IdentifyAsync(fs, preCancelled)); } private static TheoryData CreateLoadData() { - double[] percentages = new[] { 0, 0.3, 0.7 }; + double[] percentages = [0, 0.3, 0.7]; - TheoryData data = new(); + TheoryData data = []; foreach (string file in TestFileForEachCodec) { diff --git a/tests/ImageSharp.Tests/Image/ImageTests.EncodeCancellation.cs b/tests/ImageSharp.Tests/Image/ImageTests.EncodeCancellation.cs new file mode 100644 index 0000000000..f3b1a01c94 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.EncodeCancellation.cs @@ -0,0 +1,131 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Tests; + +public partial class ImageTests +{ + [ValidateDisposedMemoryAllocations] + public class Encode_Cancellation + { + [Fact] + public async Task Encode_PreCancellation_Bmp() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsBmpAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Cur() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsCurAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Gif() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsGifAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Animated_Gif() + { + using Image image = new(10, 10); + image.Frames.CreateFrame(); + + await Assert.ThrowsAsync( + async () => await image.SaveAsGifAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Ico() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsIcoAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Jpeg() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsJpegAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Pbm() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsPbmAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Png() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsPngAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Animated_Png() + { + using Image image = new(10, 10); + image.Frames.CreateFrame(); + + await Assert.ThrowsAsync( + async () => await image.SaveAsPngAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Qoi() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsQoiAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Tga() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsTgaAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Tiff() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsTiffAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Webp() + { + using Image image = new(10, 10); + await Assert.ThrowsAsync( + async () => await image.SaveAsWebpAsync(Stream.Null, new CancellationToken(canceled: true))); + } + + [Fact] + public async Task Encode_PreCancellation_Animated_Webp() + { + using Image image = new(10, 10); + image.Frames.CreateFrame(); + + await Assert.ThrowsAsync( + async () => await image.SaveAsWebpAsync(Stream.Null, new CancellationToken(canceled: true))); + } + } +} diff --git a/tests/ImageSharp.Tests/Image/NonSeekableStream.cs b/tests/ImageSharp.Tests/Image/NonSeekableStream.cs index 4b1f6e1568..2941490e9a 100644 --- a/tests/ImageSharp.Tests/Image/NonSeekableStream.cs +++ b/tests/ImageSharp.Tests/Image/NonSeekableStream.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. namespace SixLabors.ImageSharp.Tests; @@ -14,7 +14,7 @@ internal class NonSeekableStream : Stream public override bool CanSeek => false; - public override bool CanWrite => false; + public override bool CanWrite => this.dataStream.CanWrite; public override bool CanTimeout => this.dataStream.CanTimeout; @@ -91,5 +91,5 @@ internal class NonSeekableStream : Stream => throw new NotSupportedException(); public override void Write(byte[] buffer, int offset, int count) - => throw new NotImplementedException(); + => this.dataStream.Write(buffer, offset, count); } diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 41e6e525f8..9af4f41a81 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -12,7 +12,7 @@ - net8.0 + net8.0;net9.0 diff --git a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs index 403865e662..5e5887c923 100644 --- a/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs +++ b/tests/ImageSharp.Tests/Processing/BaseImageOperationsExtensionTest.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; diff --git a/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs index 2a1273ebb7..713daa0e9f 100644 --- a/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs @@ -59,7 +59,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetector2DKernelData))] public void DetectEdges_Rect_EdgeDetector2DProcessor_DefaultGrayScale_Set(EdgeDetector2DKernel kernel, bool _) { - this.operations.DetectEdges(kernel, this.rect); + this.operations.DetectEdges(this.rect, kernel); EdgeDetector2DProcessor processor = this.Verify(this.rect); Assert.True(processor.Grayscale); @@ -81,7 +81,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetector2DKernelData))] public void DetectEdges_Rect_EdgeDetector2DProcessorSet(EdgeDetector2DKernel kernel, bool grayscale) { - this.operations.DetectEdges(kernel, grayscale, this.rect); + this.operations.DetectEdges(this.rect, kernel, grayscale); EdgeDetector2DProcessor processor = this.Verify(this.rect); Assert.Equal(grayscale, processor.Grayscale); @@ -114,7 +114,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetectorKernelData))] public void DetectEdges_Rect_EdgeDetectorProcessor_DefaultGrayScale_Set(EdgeDetectorKernel kernel, bool _) { - this.operations.DetectEdges(kernel, this.rect); + this.operations.DetectEdges(this.rect, kernel); EdgeDetectorProcessor processor = this.Verify(this.rect); Assert.True(processor.Grayscale); @@ -136,7 +136,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetectorKernelData))] public void DetectEdges_Rect_EdgeDetectorProcessorSet(EdgeDetectorKernel kernel, bool grayscale) { - this.operations.DetectEdges(kernel, grayscale, this.rect); + this.operations.DetectEdges(this.rect, kernel, grayscale); EdgeDetectorProcessor processor = this.Verify(this.rect); Assert.Equal(grayscale, processor.Grayscale); @@ -167,7 +167,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetectorCompassKernelData))] public void DetectEdges_Rect_EdgeDetectorCompassProcessor_DefaultGrayScale_Set(EdgeDetectorCompassKernel kernel, bool _) { - this.operations.DetectEdges(kernel, this.rect); + this.operations.DetectEdges(this.rect, kernel); EdgeDetectorCompassProcessor processor = this.Verify(this.rect); Assert.True(processor.Grayscale); @@ -189,7 +189,7 @@ public class DetectEdgesTest : BaseImageOperationsExtensionTest [MemberData(nameof(EdgeDetectorCompassKernelData))] public void DetectEdges_Rect_EdgeDetectorCompassProcessorSet(EdgeDetectorCompassKernel kernel, bool grayscale) { - this.operations.DetectEdges(kernel, grayscale, this.rect); + this.operations.DetectEdges(this.rect, kernel, grayscale); EdgeDetectorCompassProcessor processor = this.Verify(this.rect); Assert.Equal(grayscale, processor.Grayscale); diff --git a/tests/ImageSharp.Tests/Processing/Convolution/GaussianBlurTest.cs b/tests/ImageSharp.Tests/Processing/Convolution/GaussianBlurTest.cs index 5142791603..410862ebfa 100644 --- a/tests/ImageSharp.Tests/Processing/Convolution/GaussianBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Convolution/GaussianBlurTest.cs @@ -13,7 +13,7 @@ public class GaussianBlurTest : BaseImageOperationsExtensionTest public void GaussianBlur_GaussianBlurProcessorDefaultsSet() { this.operations.GaussianBlur(); - var processor = this.Verify(); + GaussianBlurProcessor processor = this.Verify(); Assert.Equal(3f, processor.Sigma); } @@ -22,7 +22,7 @@ public class GaussianBlurTest : BaseImageOperationsExtensionTest public void GaussianBlur_amount_GaussianBlurProcessorDefaultsSet() { this.operations.GaussianBlur(0.2f); - var processor = this.Verify(); + GaussianBlurProcessor processor = this.Verify(); Assert.Equal(.2f, processor.Sigma); } @@ -30,8 +30,8 @@ public class GaussianBlurTest : BaseImageOperationsExtensionTest [Fact] public void GaussianBlur_amount_rect_GaussianBlurProcessorDefaultsSet() { - this.operations.GaussianBlur(0.6f, this.rect); - var processor = this.Verify(this.rect); + this.operations.GaussianBlur(this.rect, 0.6f); + GaussianBlurProcessor processor = this.Verify(this.rect); Assert.Equal(.6f, processor.Sigma); } diff --git a/tests/ImageSharp.Tests/Processing/Convolution/GaussianSharpenTest.cs b/tests/ImageSharp.Tests/Processing/Convolution/GaussianSharpenTest.cs index b48ebac0dc..fd9e64c467 100644 --- a/tests/ImageSharp.Tests/Processing/Convolution/GaussianSharpenTest.cs +++ b/tests/ImageSharp.Tests/Processing/Convolution/GaussianSharpenTest.cs @@ -13,7 +13,7 @@ public class GaussianSharpenTest : BaseImageOperationsExtensionTest public void GaussianSharpen_GaussianSharpenProcessorDefaultsSet() { this.operations.GaussianSharpen(); - var processor = this.Verify(); + GaussianSharpenProcessor processor = this.Verify(); Assert.Equal(3f, processor.Sigma); } @@ -22,7 +22,7 @@ public class GaussianSharpenTest : BaseImageOperationsExtensionTest public void GaussianSharpen_amount_GaussianSharpenProcessorDefaultsSet() { this.operations.GaussianSharpen(0.2f); - var processor = this.Verify(); + GaussianSharpenProcessor processor = this.Verify(); Assert.Equal(.2f, processor.Sigma); } @@ -30,8 +30,8 @@ public class GaussianSharpenTest : BaseImageOperationsExtensionTest [Fact] public void GaussianSharpen_amount_rect_GaussianSharpenProcessorDefaultsSet() { - this.operations.GaussianSharpen(0.6f, this.rect); - var processor = this.Verify(this.rect); + this.operations.GaussianSharpen(this.rect, 0.6f); + GaussianSharpenProcessor processor = this.Verify(this.rect); Assert.Equal(.6f, processor.Sigma); } diff --git a/tests/ImageSharp.Tests/Processing/Convolution/MedianBlurTest.cs b/tests/ImageSharp.Tests/Processing/Convolution/MedianBlurTest.cs index dc497628fb..77fbc70823 100644 --- a/tests/ImageSharp.Tests/Processing/Convolution/MedianBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Convolution/MedianBlurTest.cs @@ -13,7 +13,7 @@ public class MedianBlurTest : BaseImageOperationsExtensionTest public void Median_radius_MedianProcessorDefaultsSet() { this.operations.MedianBlur(3, true); - var processor = this.Verify(); + MedianBlurProcessor processor = this.Verify(); Assert.Equal(3, processor.Radius); Assert.True(processor.PreserveAlpha); @@ -22,8 +22,8 @@ public class MedianBlurTest : BaseImageOperationsExtensionTest [Fact] public void Median_radius_rect_MedianProcessorDefaultsSet() { - this.operations.MedianBlur(5, false, this.rect); - var processor = this.Verify(this.rect); + this.operations.MedianBlur(this.rect, 5, false); + MedianBlurProcessor processor = this.Verify(this.rect); Assert.Equal(5, processor.Radius); Assert.False(processor.PreserveAlpha); diff --git a/tests/ImageSharp.Tests/Processing/Normalization/MagickCompareTests.cs b/tests/ImageSharp.Tests/Processing/Normalization/MagickCompareTests.cs index 5fb0a4e934..2b609a9b2a 100644 --- a/tests/ImageSharp.Tests/Processing/Normalization/MagickCompareTests.cs +++ b/tests/ImageSharp.Tests/Processing/Normalization/MagickCompareTests.cs @@ -1,13 +1,12 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using ImageMagick; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Normalization; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; -using ImageMagick; - namespace SixLabors.ImageSharp.Tests.Processing.Normalization; // ReSharper disable InconsistentNaming @@ -22,46 +21,43 @@ public class MagickCompareTests Image imageFromMagick; using (Stream stream = LoadAsStream(provider)) { - var magickImage = new MagickImage(stream); + using MagickImage magickImage = new(stream); // Apply Auto Level using the Grey (BT.709) channel. magickImage.AutoLevel(Channels.Gray); imageFromMagick = ConvertImageFromMagick(magickImage); } - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + HistogramEqualizationOptions options = new() { - var options = new HistogramEqualizationOptions - { - Method = HistogramEqualizationMethod.AutoLevel, - LuminanceLevels = 256, - SyncChannels = true - }; - image.Mutate(x => x.HistogramEqualization(options)); - image.DebugSave(provider); - ExactImageComparer.Instance.CompareImages(imageFromMagick, image); - } + Method = HistogramEqualizationMethod.AutoLevel, + LuminanceLevels = 256, + SyncChannels = true + }; + image.Mutate(x => x.HistogramEqualization(options)); + image.DebugSave(provider); + ExactImageComparer.Instance.CompareImages(imageFromMagick, image); + + imageFromMagick.Dispose(); } - private Stream LoadAsStream(TestImageProvider provider) + private static FileStream LoadAsStream(TestImageProvider provider) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { - string path = TestImageProvider.GetFilePathOrNull(provider); - if (path == null) - { - throw new InvalidOperationException("CompareToMagick() works only with file providers!"); - } + string path = TestImageProvider.GetFilePathOrNull(provider) + ?? throw new InvalidOperationException("CompareToMagick() works only with file providers!"); - var testFile = TestFile.Create(path); + TestFile testFile = TestFile.Create(path); return new FileStream(testFile.FullPath, FileMode.Open); } - private Image ConvertImageFromMagick(MagickImage magickImage) + private static Image ConvertImageFromMagick(MagickImage magickImage) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { Configuration configuration = Configuration.Default.Clone(); configuration.PreferContiguousImageBuffers = true; - var result = new Image(configuration, magickImage.Width, magickImage.Height); + Image result = new(configuration, (int)magickImage.Width, (int)magickImage.Height); Assert.True(result.DangerousTryGetSinglePixelMemory(out Memory resultPixels)); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs index c94983ecd5..f045c981eb 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/BokehBlurTest.cs @@ -3,7 +3,6 @@ using System.Globalization; using System.Text.RegularExpressions; -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Convolution; @@ -165,7 +164,7 @@ public class BokehBlurTest { Size size = x.GetCurrentSize(); Rectangle bounds = new(10, 10, size.Width / 2, size.Height / 2); - x.BokehBlur(value.Radius, value.Components, value.Gamma, bounds); + x.BokehBlur(bounds, value.Radius, value.Components, value.Gamma); }, testOutputDetails: value.ToString(), ImageComparer.TolerantPercentage(0.05f), diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/ConvolutionTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/ConvolutionTests.cs new file mode 100644 index 0000000000..0cb56b732d --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/ConvolutionTests.cs @@ -0,0 +1,50 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Extensions.Convolution; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; + +namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution; + +[GroupOutput("Convolution")] +public class ConvolutionTests +{ + private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F); + + public static readonly TheoryData> Values = new TheoryData> + { + // Sharpening kernel. + new float[,] + { + { -1, -1, -1 }, + { -1, 16, -1 }, + { -1, -1, -1 } + } + }; + + public static readonly string[] InputImages = + [ + TestImages.Bmp.Car, + TestImages.Png.CalliphoraPartial, + TestImages.Png.Blur + ]; + + [Theory] + [WithFileCollection(nameof(InputImages), nameof(Values), PixelTypes.Rgba32)] + public void OnFullImage(TestImageProvider provider, DenseMatrix value) + where TPixel : unmanaged, IPixel + => provider.RunValidatingProcessorTest( + x => x.Convolve(value), + string.Join('_', value.Data), + ValidatorComparer); + + [Theory] + [WithFileCollection(nameof(InputImages), nameof(Values), PixelTypes.Rgba32)] + public void InBox(TestImageProvider provider, DenseMatrix value) + where TPixel : unmanaged, IPixel + => provider.RunRectangleConstrainedValidatingProcessorTest( + (x, rect) => x.Convolve(rect, value), + string.Join('_', value.Data), + ValidatorComparer); +} diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs index 47ccaa46cd..0728bb5d84 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs @@ -12,5 +12,5 @@ public class GaussianBlurTest : Basic1ParameterConvolutionTests protected override void Apply(IImageProcessingContext ctx, int value) => ctx.GaussianBlur(value); protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) => - ctx.GaussianBlur(value, bounds); + ctx.GaussianBlur(bounds, value); } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs index 96c58f7010..c6b7c82363 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs @@ -12,5 +12,5 @@ public class GaussianSharpenTest : Basic1ParameterConvolutionTests protected override void Apply(IImageProcessingContext ctx, int value) => ctx.GaussianSharpen(value); protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) => - ctx.GaussianSharpen(value, bounds); + ctx.GaussianSharpen(bounds, value); } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/MedianBlurTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/MedianBlurTest.cs index 7cc0ef9d50..048b843580 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/MedianBlurTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/MedianBlurTest.cs @@ -12,5 +12,5 @@ public class MedianBlurTest : Basic1ParameterConvolutionTests protected override void Apply(IImageProcessingContext ctx, int value) => ctx.MedianBlur(value, true); protected override void Apply(IImageProcessingContext ctx, int value, Rectangle bounds) => - ctx.MedianBlur(value, true, bounds); + ctx.MedianBlur(bounds, value, true); } diff --git a/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs index 38ac9c18c6..6b6db69c11 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/ProjectiveTransformTests.cs @@ -55,6 +55,14 @@ public class ProjectiveTransformTests { TaperSide.Right, TaperCorner.RightOrBottom }, }; + public static readonly TheoryData QuadDistortionData = new() + { + { new PointF(0, 0), new PointF(150, 0), new PointF(150, 150), new PointF(0, 150) }, // source == destination + { new PointF(25, 50), new PointF(210, 25), new PointF(140, 210), new PointF(15, 125) }, // Distortion + { new PointF(-50, -50), new PointF(200, -50), new PointF(200, 200), new PointF(-50, 200) }, // Scaling + { new PointF(150, 0), new PointF(150, 150), new PointF(0, 150), new PointF(0, 0) }, // Rotation + }; + public ProjectiveTransformTests(ITestOutputHelper output) => this.Output = output; [Theory] @@ -93,6 +101,24 @@ public class ProjectiveTransformTests } } + [Theory] + [WithTestPatternImages(nameof(QuadDistortionData), 150, 150, PixelTypes.Rgba32)] + public void Transform_WithQuadDistortion(TestImageProvider provider, PointF topLeft, PointF topRight, PointF bottomRight, PointF bottomLeft) + where TPixel : unmanaged, IPixel + { + using (Image image = provider.GetImage()) + { + ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() + .AppendQuadDistortion(topLeft, topRight, bottomRight, bottomLeft); + + image.Mutate(i => i.Transform(builder)); + + FormattableString testOutputDetails = $"{topLeft}-{topRight}-{bottomRight}-{bottomLeft}"; + image.DebugSave(provider, testOutputDetails); + image.CompareFirstFrameToReferenceOutput(TolerantComparer, provider, testOutputDetails); + } + } + [Theory] [WithSolidFilledImages(100, 100, 0, 0, 255, PixelTypes.Rgba32)] public void RawTransformMatchesDocumentedExample(TestImageProvider provider) @@ -128,11 +154,11 @@ public class ProjectiveTransformTests using (Image image = provider.GetImage()) { #pragma warning disable SA1117 // Parameters should be on same line or separate lines - Matrix4x4 matrix = new( - 0.260987f, -0.434909f, 0, -0.0022184f, - 0.373196f, 0.949882f, 0, -0.000312129f, - 0, 0, 1, 0, - 52, 165, 0, 1); + Matrix4x4 matrix = new( + 0.260987f, -0.434909f, 0, -0.0022184f, + 0.373196f, 0.949882f, 0, -0.000312129f, + 0, 0, 1, 0, + 52, 165, 0, 1); #pragma warning restore SA1117 // Parameters should be on same line or separate lines ProjectiveTransformBuilder builder = new ProjectiveTransformBuilder() diff --git a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs index 59a0ad4274..b59542482b 100644 --- a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs +++ b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs @@ -13,10 +13,10 @@ public class QuantizedImageTests [Fact] public void QuantizersDitherByDefault() { - var werner = new WernerPaletteQuantizer(); - var webSafe = new WebSafePaletteQuantizer(); - var octree = new OctreeQuantizer(); - var wu = new WuQuantizer(); + WernerPaletteQuantizer werner = new(); + WebSafePaletteQuantizer webSafe = new(); + OctreeQuantizer octree = new(); + WuQuantizer wu = new(); Assert.NotNull(werner.Options.Dither); Assert.NotNull(webSafe.Options.Dither); @@ -52,27 +52,23 @@ public class QuantizedImageTests bool dither) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - Assert.True(image[0, 0].Equals(default)); + using Image image = provider.GetImage(); + Assert.True(image[0, 0].Equals(default)); - var options = new QuantizerOptions(); - if (!dither) - { - options.Dither = null; - } + QuantizerOptions options = new(); + if (!dither) + { + options.Dither = null; + } - var quantizer = new OctreeQuantizer(options); + OctreeQuantizer quantizer = new(options); - foreach (ImageFrame frame in image.Frames) - { - using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration)) - using (IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds())) - { - int index = this.GetTransparentIndex(quantized); - Assert.Equal(index, quantized.DangerousGetRowSpan(0)[0]); - } - } + foreach (ImageFrame frame in image.Frames) + { + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration); + using IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); + int index = this.GetTransparentIndex(quantized); + Assert.Equal(index, quantized.DangerousGetRowSpan(0)[0]); } } @@ -82,27 +78,23 @@ public class QuantizedImageTests public void WuQuantizerYieldsCorrectTransparentPixel(TestImageProvider provider, bool dither) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - Assert.True(image[0, 0].Equals(default)); + using Image image = provider.GetImage(); + Assert.True(image[0, 0].Equals(default)); - var options = new QuantizerOptions(); - if (!dither) - { - options.Dither = null; - } + QuantizerOptions options = new(); + if (!dither) + { + options.Dither = null; + } - var quantizer = new WuQuantizer(options); + WuQuantizer quantizer = new(options); - foreach (ImageFrame frame in image.Frames) - { - using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration)) - using (IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds())) - { - int index = this.GetTransparentIndex(quantized); - Assert.Equal(index, quantized.DangerousGetRowSpan(0)[0]); - } - } + foreach (ImageFrame frame in image.Frames) + { + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration); + using IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); + int index = this.GetTransparentIndex(quantized); + Assert.Equal(index, quantized.DangerousGetRowSpan(0)[0]); } } @@ -112,13 +104,11 @@ public class QuantizedImageTests public void Issue1505(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var octreeQuantizer = new OctreeQuantizer(); - IQuantizer quantizer = octreeQuantizer.CreatePixelSpecificQuantizer(Configuration.Default, new QuantizerOptions() { MaxColors = 128 }); - ImageFrame frame = image.Frames[0]; - quantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds()); - } + using Image image = provider.GetImage(); + OctreeQuantizer octreeQuantizer = new(); + IQuantizer quantizer = octreeQuantizer.CreatePixelSpecificQuantizer(Configuration.Default, new QuantizerOptions() { MaxColors = 128 }); + ImageFrame frame = image.Frames[0]; + quantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); } private int GetTransparentIndex(IndexedImageFrame quantized) diff --git a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs index ccb79debda..74f2fc3b42 100644 --- a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs +++ b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs @@ -12,13 +12,13 @@ public class WuQuantizerTests public void SinglePixelOpaque() { Configuration config = Configuration.Default; - var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }); + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); - using var image = new Image(config, 1, 1, Color.Black.ToPixel()); + using Image image = new(config, 1, 1, Color.Black.ToPixel()); ImageFrame frame = image.Frames.RootFrame; using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); - using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds()); + using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); Assert.Equal(1, result.Palette.Length); Assert.Equal(1, result.Width); @@ -32,13 +32,13 @@ public class WuQuantizerTests public void SinglePixelTransparent() { Configuration config = Configuration.Default; - var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }); + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); - using var image = new Image(config, 1, 1, default(Rgba32)); + using Image image = new(config, 1, 1, default(Rgba32)); ImageFrame frame = image.Frames.RootFrame; using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); - using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds()); + using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); Assert.Equal(1, result.Palette.Length); Assert.Equal(1, result.Width); @@ -66,7 +66,7 @@ public class WuQuantizerTests [Fact] public void Palette256() { - using var image = new Image(1, 256); + using Image image = new(1, 256); for (int i = 0; i < 256; i++) { @@ -79,18 +79,18 @@ public class WuQuantizerTests } Configuration config = Configuration.Default; - var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }); + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); ImageFrame frame = image.Frames.RootFrame; using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); - using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds()); + using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); Assert.Equal(256, result.Palette.Length); Assert.Equal(1, result.Width); Assert.Equal(256, result.Height); - using var actualImage = new Image(1, 256); + using Image actualImage = new(1, 256); actualImage.ProcessPixelRows(accessor => { @@ -123,72 +123,68 @@ public class WuQuantizerTests where TPixel : unmanaged, IPixel { // See https://github.com/SixLabors/ImageSharp/issues/866 - using (Image image = provider.GetImage()) - { - Configuration config = Configuration.Default; - var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }); - ImageFrame frame = image.Frames.RootFrame; + using Image image = provider.GetImage(); + Configuration config = Configuration.Default; + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); + ImageFrame frame = image.Frames.RootFrame; - using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); - using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds()); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); + using IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds); - Assert.Equal(48, result.Palette.Length); - } + Assert.Equal(48, result.Palette.Length); } private static void TestScale(Func pixelBuilder) { - using (var image = new Image(1, 256)) - using (var expectedImage = new Image(1, 256)) - using (var actualImage = new Image(1, 256)) + using Image image = new(1, 256); + using Image expectedImage = new(1, 256); + using Image actualImage = new(1, 256); + for (int i = 0; i < 256; i++) { - for (int i = 0; i < 256; i++) - { - byte c = (byte)i; - image[0, i] = pixelBuilder.Invoke(c); - } + byte c = (byte)i; + image[0, i] = pixelBuilder.Invoke(c); + } - for (int i = 0; i < 256; i++) - { - byte c = (byte)((i & ~7) + 4); - expectedImage[0, i] = pixelBuilder.Invoke(c); - } + for (int i = 0; i < 256; i++) + { + byte c = (byte)((i & ~7) + 4); + expectedImage[0, i] = pixelBuilder.Invoke(c); + } - Configuration config = Configuration.Default; - var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }); + Configuration config = Configuration.Default; + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); - ImageFrame frame = image.Frames.RootFrame; - using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config)) - using (IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds())) - { - Assert.Equal(4 * 8, result.Palette.Length); - Assert.Equal(1, result.Width); - Assert.Equal(256, result.Height); + ImageFrame frame = image.Frames.RootFrame; + using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config)) + using (IndexedImageFrame result = frameQuantizer.BuildPaletteAndQuantizeFrame(frame, frame.Bounds)) + { + Assert.Equal(4 * 8, result.Palette.Length); + Assert.Equal(1, result.Width); + Assert.Equal(256, result.Height); - actualImage.ProcessPixelRows(accessor => + actualImage.ProcessPixelRows(accessor => + { + ReadOnlySpan paletteSpan = result.Palette.Span; + int paletteCount = paletteSpan.Length - 1; + for (int y = 0; y < accessor.Height; y++) { - ReadOnlySpan paletteSpan = result.Palette.Span; - int paletteCount = paletteSpan.Length - 1; - for (int y = 0; y < accessor.Height; y++) - { - Span row = accessor.GetRowSpan(y); - ReadOnlySpan quantizedPixelSpan = result.DangerousGetRowSpan(y); + Span row = accessor.GetRowSpan(y); + ReadOnlySpan quantizedPixelSpan = result.DangerousGetRowSpan(y); - for (int x = 0; x < accessor.Width; x++) - { - row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[x])]; - } + for (int x = 0; x < accessor.Width; x++) + { + row[x] = paletteSpan[Math.Min(paletteCount, quantizedPixelSpan[x])]; } - }); - } - - expectedImage.ProcessPixelRows(actualImage, static (expectedAccessor, actualAccessor) => - { - for (int y = 0; y < expectedAccessor.Height; y++) - { - Assert.True(expectedAccessor.GetRowSpan(y).SequenceEqual(actualAccessor.GetRowSpan(y))); } }); } + + expectedImage.ProcessPixelRows(actualImage, static (expectedAccessor, actualAccessor) => + { + for (int y = 0; y < expectedAccessor.Height; y++) + { + Assert.True(expectedAccessor.GetRowSpan(y).SequenceEqual(actualAccessor.GetRowSpan(y))); + } + }); } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 13f72a6345..4130474b58 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -827,6 +827,7 @@ public static class TestImages public const string Issue2257 = "Webp/issues/Issue2257.webp"; public const string Issue2670 = "Webp/issues/Issue2670.webp"; public const string Issue2763 = "Webp/issues/Issue2763.png"; + public const string Issue2801 = "Webp/issues/Issue2801.webp"; } } diff --git a/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs b/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs index 07ad5e8f03..63126dcbca 100644 --- a/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs +++ b/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs @@ -104,7 +104,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -148,7 +148,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -192,7 +192,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -241,7 +241,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -288,7 +288,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -333,7 +333,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, @@ -379,7 +379,7 @@ public static class FeatureTestRunner ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { - processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; + processStartInfo.Environment[$"DOTNET_{intrinsic.Value}"] = "0"; RemoteExecutor.Invoke( action, diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs index f5c70b0885..05f65cfbbc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs @@ -15,13 +15,10 @@ public static class ImageComparingUtils float compareTolerance = 0.01f) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { - string path = TestImageProvider.GetFilePathOrNull(provider); - if (path == null) - { - throw new InvalidOperationException("CompareToOriginal() works only with file providers!"); - } + string path = TestImageProvider.GetFilePathOrNull(provider) + ?? throw new InvalidOperationException("CompareToOriginal() works only with file providers!"); - var testFile = TestFile.Create(path); + TestFile testFile = TestFile.Create(path); using Image magickImage = DecodeWithMagick(new FileInfo(testFile.FullPath)); if (useExactComparer) { @@ -38,25 +35,23 @@ public static class ImageComparingUtils { Configuration configuration = Configuration.Default.Clone(); configuration.PreferContiguousImageBuffers = true; - using (var magickImage = new MagickImage(fileInfo)) - { - magickImage.AutoOrient(); - var result = new Image(configuration, magickImage.Width, magickImage.Height); + using MagickImage magickImage = new(fileInfo); + magickImage.AutoOrient(); + Image result = new(configuration, (int)magickImage.Width, (int)magickImage.Height); - Assert.True(result.DangerousTryGetSinglePixelMemory(out Memory resultPixels)); + Assert.True(result.DangerousTryGetSinglePixelMemory(out Memory resultPixels)); - using (IUnsafePixelCollection pixels = magickImage.GetPixelsUnsafe()) - { - byte[] data = pixels.ToByteArray(PixelMapping.RGBA); - - PixelOperations.Instance.FromRgba32Bytes( - configuration, - data, - resultPixels.Span, - resultPixels.Length); - } + using (IUnsafePixelCollection pixels = magickImage.GetPixelsUnsafe()) + { + byte[] data = pixels.ToByteArray(PixelMapping.RGBA); - return result; + PixelOperations.Instance.FromRgba32Bytes( + configuration, + data, + resultPixels.Span, + resultPixels.Length); } + + return result; } } diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs index ae4af24f14..d1149dd004 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs @@ -6,9 +6,10 @@ using System.Buffers; namespace SixLabors.ImageSharp.Tests.TestUtilities; /// -/// is a variant of that derives from instead of encapsulating it. -/// It is used to test decoder REacellation without relying on of our standard prefetching of arbitrary streams to -/// on asynchronous path. +/// is a variant of that derives from +/// instead of encapsulating it. +/// It is used to test decoder cancellation without relying on of our standard prefetching of arbitrary streams +/// to on asynchronous path. /// public class PausedMemoryStream : MemoryStream, IPausedStream { @@ -108,11 +109,11 @@ public class PausedMemoryStream : MemoryStream, IPausedStream public override bool CanWrite => base.CanWrite; - public override void Flush() => this.Await(() => base.Flush()); + public override void Flush() => this.Await(base.Flush); public override int Read(byte[] buffer, int offset, int count) => this.Await(() => base.Read(buffer, offset, count)); - public override long Seek(long offset, SeekOrigin origin) => this.Await(() => base.Seek(offset, origin)); + public override long Seek(long offset, SeekOrigin loc) => this.Await(() => base.Seek(offset, loc)); public override void SetLength(long value) => this.Await(() => base.SetLength(value)); @@ -124,7 +125,7 @@ public class PausedMemoryStream : MemoryStream, IPausedStream public override void WriteByte(byte value) => this.Await(() => base.WriteByte(value)); - public override int ReadByte() => this.Await(() => base.ReadByte()); + public override int ReadByte() => this.Await(base.ReadByte); public override void CopyTo(Stream destination, int bufferSize) { diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs index 3c780f3474..42ed6b0d5e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs @@ -115,7 +115,7 @@ public class PausedStream : Stream, IPausedStream public override long Position { get => this.innerStream.Position; set => this.innerStream.Position = value; } - public override void Flush() => this.Await(() => this.innerStream.Flush()); + public override void Flush() => this.Await(this.innerStream.Flush); public override int Read(byte[] buffer, int offset, int count) => this.Await(() => this.innerStream.Read(buffer, offset, count)); @@ -131,7 +131,7 @@ public class PausedStream : Stream, IPausedStream public override void WriteByte(byte value) => this.Await(() => this.innerStream.WriteByte(value)); - public override int ReadByte() => this.Await(() => this.innerStream.ReadByte()); + public override int ReadByte() => this.Await(this.innerStream.ReadByte); protected override void Dispose(bool disposing) { diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs index f96dc19ee0..74015a4eff 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs @@ -67,7 +67,7 @@ public class MagickReferenceDecoder : ImageDecoder List> framesList = []; foreach (IMagickImage magicFrame in magickImageCollection) { - ImageFrame frame = new(configuration, magicFrame.Width, magicFrame.Height); + ImageFrame frame = new(configuration, (int)magicFrame.Width, (int)magicFrame.Height); framesList.Add(frame); MemoryGroup framePixels = frame.PixelBuffer.FastMemoryGroup; diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..ffa8e7cd8a --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b311f117167e17b4611d525aef57146a6757db08f2a36fa093f45f237df9e1a2 +size 326809 diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..682fd8b49b --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0eabd5f7dd2f258d04d3f1db8ee4d958754541e0009ae183d6e512f408e3201 +size 249497 diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..55d592a60f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/InBox_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:048b4a1679852d34d6f74f01b750461ec9db2d3e7e3aa3d2d89d48e5725aa560 +size 142367 diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..f01496ad76 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_CalliphoraPartial_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6f63d117433a93e30b9b41f68f676bb53eb1761f3c665f178ef07ec2c9626c3 +size 338527 diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..99e9737b51 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_Car_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d72a3994fc6dcc461da3af5176d5bf62c95b7abeffcbf5547172377db3b0d9ac +size 287158 diff --git a/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png new file mode 100644 index 0000000000..ecc09e70ff --- /dev/null +++ b/tests/Images/External/ReferenceOutput/Convolution/ConvolutionTests/OnFullImage_Rgba32_blur_-1_-1_-1_-1_16_-1_-1_-1_-1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3fd7906f11e87a2b0043cde179317f53e9a2bf02d9e64763be8f9923d60f057 +size 257492 diff --git a/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=-50, Y=-50 ]-PointF [ X=200, Y=-50 ]-PointF [ X=200, Y=200 ]-PointF [ X=-50, Y=200 ].png b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=-50, Y=-50 ]-PointF [ X=200, Y=-50 ]-PointF [ X=200, Y=200 ]-PointF [ X=-50, Y=200 ].png new file mode 100644 index 0000000000..38c603855c --- /dev/null +++ b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=-50, Y=-50 ]-PointF [ X=200, Y=-50 ]-PointF [ X=200, Y=200 ]-PointF [ X=-50, Y=200 ].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:abce6af307a81a8ebac8e502142b00b2615403b5570c8dbe7b6895cfdd1a6d60 +size 66879 diff --git a/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=0, Y=0 ]-PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ].png b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=0, Y=0 ]-PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ].png new file mode 100644 index 0000000000..f7ea0d0060 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=0, Y=0 ]-PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4cda265a50aa26711efafdbcd947c9a01eff872611df5298920583f9a3d4224 +size 26458 diff --git a/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ]-PointF [ X=0, Y=0 ].png b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ]-PointF [ X=0, Y=0 ].png new file mode 100644 index 0000000000..78c37cc448 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=150, Y=0 ]-PointF [ X=150, Y=150 ]-PointF [ X=0, Y=150 ]-PointF [ X=0, Y=0 ].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:278a488a858b8eda141493fe00c617eb1f664196853da8341d7e5b7f231ddce4 +size 24645 diff --git a/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=25, Y=50 ]-PointF [ X=210, Y=25 ]-PointF [ X=140, Y=210 ]-PointF [ X=15, Y=125 ].png b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=25, Y=50 ]-PointF [ X=210, Y=25 ]-PointF [ X=140, Y=210 ]-PointF [ X=15, Y=125 ].png new file mode 100644 index 0000000000..b4740828d4 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/ProjectiveTransformTests/Transform_WithQuadDistortion_Rgba32_TestPattern150x150_PointF [ X=25, Y=50 ]-PointF [ X=210, Y=25 ]-PointF [ X=140, Y=210 ]-PointF [ X=15, Y=125 ].png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e03e79e6fab3a9e43041e54640a04c7cc3677709e7d879f9f410cf8afc7547a7 +size 42691 diff --git a/tests/Images/Input/Webp/issues/Issue2801.webp b/tests/Images/Input/Webp/issues/Issue2801.webp new file mode 100644 index 0000000000..a3b5fee6e0 --- /dev/null +++ b/tests/Images/Input/Webp/issues/Issue2801.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e90a0d853ddf70d823d8da44eb6c57081e955b1fb7f436a1fd88ca5e5c75a003 +size 261212