diff --git a/src/ImageSharp/Advanced/AotCompilerTools.cs b/src/ImageSharp/Advanced/AotCompilerTools.cs index 0d5faabe18..23bf85cf3d 100644 --- a/src/ImageSharp/Advanced/AotCompilerTools.cs +++ b/src/ImageSharp/Advanced/AotCompilerTools.cs @@ -138,10 +138,11 @@ private static void Seed() AotCompileResamplers(); AotCompileQuantizers(); AotCompilePixelSamplingStrategys(); + AotCompilePixelMaps(); AotCompileDithers(); AotCompileMemoryManagers(); - Unsafe.SizeOf(); + _ = Unsafe.SizeOf(); // TODO: Do the discovery work to figure out what works and what doesn't. } @@ -514,6 +515,20 @@ private static void AotCompilePixelSamplingStrategys() default(ExtensivePixelSamplingStrategy).EnumeratePixelRegions(default(ImageFrame)); } + /// + /// This method pre-seeds the all in the AoT compiler. + /// + /// The pixel format. + [Preserve] + private static void AotCompilePixelMaps() + where TPixel : unmanaged, IPixel + { + default(EuclideanPixelMap).GetClosestColor(default, out _); + default(EuclideanPixelMap).GetClosestColor(default, out _); + default(EuclideanPixelMap).GetClosestColor(default, out _); + default(EuclideanPixelMap).GetClosestColor(default, out _); + } + /// /// This method pre-seeds the all in the AoT compiler. /// diff --git a/src/ImageSharp/Common/InlineArray.cs b/src/ImageSharp/Common/InlineArray.cs new file mode 100644 index 0000000000..c20c0d8750 --- /dev/null +++ b/src/ImageSharp/Common/InlineArray.cs @@ -0,0 +1,38 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +// + +using System; +using System.Runtime.CompilerServices; + +namespace SixLabors.ImageSharp; + +/// +/// Represents a safe, fixed sized buffer of 4 elements. +/// +[InlineArray(4)] +internal struct InlineArray4 +{ + private T t; +} + +/// +/// Represents a safe, fixed sized buffer of 8 elements. +/// +[InlineArray(8)] +internal struct InlineArray8 +{ + private T t; +} + +/// +/// Represents a safe, fixed sized buffer of 16 elements. +/// +[InlineArray(16)] +internal struct InlineArray16 +{ + private T t; +} + + diff --git a/src/ImageSharp/Common/InlineArray.tt b/src/ImageSharp/Common/InlineArray.tt new file mode 100644 index 0000000000..ab488591a7 --- /dev/null +++ b/src/ImageSharp/Common/InlineArray.tt @@ -0,0 +1,38 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +// + +using System; +using System.Runtime.CompilerServices; + +namespace SixLabors.ImageSharp; + +<#GenerateInlineArrays();#> + +<#+ +private static int[] Lengths = new int[] {4, 8, 16 }; + +void GenerateInlineArrays() +{ + foreach (int length in Lengths) + { +#> +/// +/// Represents a safe, fixed sized buffer of <#=length#> elements. +/// +[InlineArray(<#=length#>)] +internal struct InlineArray<#=length#> +{ + private T t; +} + +<#+ + } +} +#> diff --git a/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs b/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs index f753e7282b..1c1a8b2910 100644 --- a/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs +++ b/src/ImageSharp/Formats/AlphaAwareImageEncoder.cs @@ -1,6 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Processing.Processors.Quantization; + namespace SixLabors.ImageSharp.Formats; /// @@ -10,6 +12,8 @@ public abstract class AlphaAwareImageEncoder : ImageEncoder { /// /// Gets or initializes the mode that determines how transparent pixels are handled during encoding. + /// This overrides any other settings that may affect the encoding of transparent pixels + /// including those passed via . /// public TransparentColorMode TransparentColorMode { get; init; } } diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 321a559b1e..46da463455 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -362,10 +362,13 @@ private void WriteImage( ImageFrame? clonedFrame = null; try { - if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + // No need to clone when quantizing. The quantizer will do it for us. + // TODO: We should really try to avoid the clone entirely. + int bpp = this.bitsPerPixel != null ? (int)this.bitsPerPixel : 32; + if (bpp > 8 && EncodingUtilities.ShouldReplaceTransparentPixels(this.transparentColorMode)) { clonedFrame = image.Frames.RootFrame.Clone(); - EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + EncodingUtilities.ReplaceTransparentPixels(clonedFrame); } ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; diff --git a/src/ImageSharp/Formats/Bmp/BmpMetadata.cs b/src/ImageSharp/Formats/Bmp/BmpMetadata.cs index d0c60421c4..1dac74ba3a 100644 --- a/src/ImageSharp/Formats/Bmp/BmpMetadata.cs +++ b/src/ImageSharp/Formats/Bmp/BmpMetadata.cs @@ -158,6 +158,5 @@ public FormatConnectingMetadata ToFormatConnectingMetadata() /// public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel - { - } + => this.ColorTable = null; } diff --git a/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs b/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs index 01b7fbce08..9854854aad 100644 --- a/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs +++ b/src/ImageSharp/Formats/Cur/CurFrameMetadata.cs @@ -104,7 +104,6 @@ public static CurFrameMetadata FromFormatConnectingFrameMetadata(FormatConnectin Compression = compression, EncodingWidth = ClampEncodingDimension(metadata.EncodingWidth), EncodingHeight = ClampEncodingDimension(metadata.EncodingHeight), - ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null }; } @@ -113,7 +112,6 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() => new() { PixelTypeInfo = this.GetPixelTypeInfo(), - ColorTable = this.ColorTable, EncodingWidth = this.EncodingWidth, EncodingHeight = this.EncodingHeight }; @@ -126,6 +124,7 @@ public void AfterFrameApply(ImageFrame source, ImageFrame diff --git a/src/ImageSharp/Formats/Cur/CurMetadata.cs b/src/ImageSharp/Formats/Cur/CurMetadata.cs index 19de7f434d..d8fdb32902 100644 --- a/src/ImageSharp/Formats/Cur/CurMetadata.cs +++ b/src/ImageSharp/Formats/Cur/CurMetadata.cs @@ -71,8 +71,7 @@ public static CurMetadata FromFormatConnectingMetadata(FormatConnectingMetadata return new CurMetadata { BmpBitsPerPixel = bbpp, - Compression = compression, - ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null + Compression = compression }; } @@ -145,15 +144,13 @@ public FormatConnectingMetadata ToFormatConnectingMetadata() EncodingType = this.Compression == IconFrameCompression.Bmp && this.BmpBitsPerPixel <= BmpBitsPerPixel.Bit8 ? EncodingType.Lossy : EncodingType.Lossless, - PixelTypeInfo = this.GetPixelTypeInfo(), - ColorTable = this.ColorTable + PixelTypeInfo = this.GetPixelTypeInfo() }; /// public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel - { - } + => this.ColorTable = null; /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); diff --git a/src/ImageSharp/Formats/EncodingUtilities.cs b/src/ImageSharp/Formats/EncodingUtilities.cs index a979fdf6fa..3994743933 100644 --- a/src/ImageSharp/Formats/EncodingUtilities.cs +++ b/src/ImageSharp/Formats/EncodingUtilities.cs @@ -3,6 +3,8 @@ using System.Buffers; using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Runtime.Intrinsics; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -14,62 +16,132 @@ namespace SixLabors.ImageSharp.Formats; /// internal static class EncodingUtilities { - public static bool ShouldClearTransparentPixels(TransparentColorMode mode) + /// + /// Determines if transparent pixels can be replaced based on the specified color mode and pixel type. + /// + /// The type of the pixel. + /// Indicates the color mode used to assess the ability to replace transparent pixels. + /// Returns true if transparent pixels can be replaced; otherwise, false. + public static bool ShouldReplaceTransparentPixels(TransparentColorMode mode) + where TPixel : unmanaged, IPixel + => mode == TransparentColorMode.Clear && TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Unassociated; + + /// + /// Replaces pixels with a transparent alpha component with fully transparent pixels. + /// + /// The type of the pixel. + /// The where the transparent pixels will be changed. + public static void ReplaceTransparentPixels(ImageFrame frame) where TPixel : unmanaged, IPixel - => mode == TransparentColorMode.Clear && - TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Unassociated; + => ReplaceTransparentPixels(frame.Configuration, frame.PixelBuffer); /// - /// Convert transparent pixels, to pixels represented by , which can yield - /// to better compression in some cases. + /// Replaces pixels with a transparent alpha component with fully transparent pixels. /// /// 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) + /// The configuration. + /// The where the transparent pixels will be changed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ReplaceTransparentPixels(Configuration configuration, Buffer2D buffer) where TPixel : unmanaged, IPixel { - Buffer2DRegion buffer = clone.PixelBuffer.GetRegion(); - ClearTransparentPixels(clone.Configuration, ref buffer, color); + Buffer2DRegion region = buffer.GetRegion(); + ReplaceTransparentPixels(configuration, in region); } /// - /// Convert transparent pixels, to pixels represented by , which can yield - /// to better compression in some cases. + /// Replaces pixels with a transparent alpha component with fully transparent pixels. /// /// 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( + /// The where the transparent pixels will be changed. + public static void ReplaceTransparentPixels( Configuration configuration, - ref Buffer2DRegion clone, - Color color) + in Buffer2DRegion region) where TPixel : unmanaged, IPixel { - using IMemoryOwner vectors = configuration.MemoryAllocator.Allocate(clone.Width); + using IMemoryOwner vectors = configuration.MemoryAllocator.Allocate(region.Width); Span vectorsSpan = vectors.GetSpan(); - Vector4 replacement = color.ToScaledVector4(); - for (int y = 0; y < clone.Height; y++) + for (int y = 0; y < region.Height; y++) { - Span span = clone.DangerousGetRowSpan(y); + Span span = region.DangerousGetRowSpan(y); PixelOperations.Instance.ToVector4(configuration, span, vectorsSpan, PixelConversionModifiers.Scale); - ClearTransparentPixelRow(vectorsSpan, replacement); + ReplaceTransparentPixels(vectorsSpan); PixelOperations.Instance.FromVector4Destructive(configuration, vectorsSpan, span, PixelConversionModifiers.Scale); } } - private static void ClearTransparentPixelRow( - Span vectorsSpan, - Vector4 replacement) + /// + /// Replaces pixels with a transparent alpha component with fully transparent pixels. + /// + /// A span of color vectors that will be checked for transparency and potentially modified. + public static void ReplaceTransparentPixels(Span source) { - if (Vector128.IsHardwareAccelerated) + if (Vector512.IsHardwareAccelerated && source.Length >= 4) + { + Span> source512 = MemoryMarshal.Cast>(source); + for (int i = 0; i < source512.Length; i++) + { + ref Vector512 v = ref source512[i]; + + // Do `vector < threshold` + Vector512 mask = Vector512.Equals(v, Vector512.Zero); + + // Replicate the result for W to all elements (is AllBitsSet if the W was 0 and Zero otherwise) + mask = Vector512.Shuffle(mask, Vector512.Create(3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15)); + + // Use the mask to select the replacement vector + // (replacement & mask) | (v512 & ~mask) + v = Vector512.ConditionalSelect(mask, Vector512.Zero, v); + } + + int m = Numerics.Modulo4(source.Length); + if (m != 0) + { + for (int i = source.Length - m; i < source.Length; i++) + { + if (source[i].W == 0) + { + source[i] = Vector4.Zero; + } + } + } + } + else if (Vector256.IsHardwareAccelerated && source.Length >= 2) { - Vector128 replacement128 = replacement.AsVector128(); + Span> source256 = MemoryMarshal.Cast>(source); + for (int i = 0; i < source256.Length; i++) + { + ref Vector256 v = ref source256[i]; + + // Do `vector < threshold` + Vector256 mask = Vector256.Equals(v, Vector256.Zero); + + // Replicate the result for W to all elements (is AllBitsSet if the W was 0 and Zero otherwise) + mask = Vector256.Shuffle(mask, Vector256.Create(3, 3, 3, 3, 7, 7, 7, 7)); - for (int i = 0; i < vectorsSpan.Length; i++) + // Use the mask to select the replacement vector + // (replacement & mask) | (v256 & ~mask) + v = Vector256.ConditionalSelect(mask, Vector256.Zero, v); + } + + int m = Numerics.Modulo2(source.Length); + if (m != 0) + { + for (int i = source.Length - m; i < source.Length; i++) + { + if (source[i].W == 0) + { + source[i] = Vector4.Zero; + } + } + } + } + else if (Vector128.IsHardwareAccelerated) + { + for (int i = 0; i < source.Length; i++) { - ref Vector4 v = ref vectorsSpan[i]; + ref Vector4 v = ref source[i]; Vector128 v128 = v.AsVector128(); // Do `vector == 0` @@ -80,16 +152,16 @@ private static void ClearTransparentPixelRow( // Use the mask to select the replacement vector // (replacement & mask) | (v128 & ~mask) - v = Vector128.ConditionalSelect(mask, replacement128, v128).AsVector4(); + v = Vector128.ConditionalSelect(mask, Vector128.Zero, v128).AsVector4(); } } else { - for (int i = 0; i < vectorsSpan.Length; i++) + for (int i = 0; i < source.Length; i++) { - if (vectorsSpan[i].W == 0F) + if (source[i].W == 0F) { - vectorsSpan[i] = replacement; + source[i] = Vector4.Zero; } } } diff --git a/src/ImageSharp/Formats/FormatConnectingFrameMetadata.cs b/src/ImageSharp/Formats/FormatConnectingFrameMetadata.cs index ded220c9ad..15a28c301a 100644 --- a/src/ImageSharp/Formats/FormatConnectingFrameMetadata.cs +++ b/src/ImageSharp/Formats/FormatConnectingFrameMetadata.cs @@ -15,11 +15,6 @@ public class FormatConnectingFrameMetadata /// public PixelTypeInfo? PixelTypeInfo { get; init; } - /// - /// Gets the frame color table if any. - /// - public ReadOnlyMemory? ColorTable { get; init; } - /// /// Gets the frame color table mode. /// diff --git a/src/ImageSharp/Formats/FormatConnectingMetadata.cs b/src/ImageSharp/Formats/FormatConnectingMetadata.cs index 9cfe40f385..efa7acdc86 100644 --- a/src/ImageSharp/Formats/FormatConnectingMetadata.cs +++ b/src/ImageSharp/Formats/FormatConnectingMetadata.cs @@ -28,11 +28,6 @@ public class FormatConnectingMetadata /// public PixelTypeInfo PixelTypeInfo { get; init; } - /// - /// Gets the shared color table if any. - /// - public ReadOnlyMemory? ColorTable { get; init; } - /// /// Gets the shared color table mode. /// diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index e18166c4b8..f6e3643d58 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -89,6 +89,11 @@ internal sealed class GifDecoderCore : ImageDecoderCore /// private GifMetadata? gifMetadata; + /// + /// The background color index. + /// + private byte backgroundColorIndex; + /// /// Initializes a new instance of the class. /// @@ -108,6 +113,10 @@ protected override Image Decode(BufferedReadStream stream, Cance uint frameCount = 0; Image? image = null; ImageFrame? previousFrame = null; + FrameDisposalMode? previousDisposalMode = null; + bool globalColorTableUsed = false; + Color backgroundColor = Color.Transparent; + try { this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); @@ -123,7 +132,7 @@ protected override Image Decode(BufferedReadStream stream, Cance break; } - this.ReadFrame(stream, ref image, ref previousFrame); + globalColorTableUsed |= this.ReadFrame(stream, ref image, ref previousFrame, ref previousDisposalMode, ref backgroundColor); // Reset per-frame state. this.imageDescriptor = default; @@ -158,6 +167,13 @@ protected override Image Decode(BufferedReadStream stream, Cance break; } } + + // We cannot always trust the global GIF palette has actually been used. + // https://github.com/SixLabors/ImageSharp/issues/2866 + if (!globalColorTableUsed) + { + this.gifMetadata.ColorTableMode = FrameColorTableMode.Local; + } } finally { @@ -179,6 +195,8 @@ protected override ImageInfo Identify(BufferedReadStream stream, CancellationTok uint frameCount = 0; ImageFrameMetadata? previousFrame = null; List framesMetadata = []; + bool globalColorTableUsed = false; + try { this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); @@ -194,7 +212,7 @@ protected override ImageInfo Identify(BufferedReadStream stream, CancellationTok break; } - this.ReadFrameMetadata(stream, framesMetadata, ref previousFrame); + globalColorTableUsed |= this.ReadFrameMetadata(stream, framesMetadata, ref previousFrame); // Reset per-frame state. this.imageDescriptor = default; @@ -229,6 +247,13 @@ protected override ImageInfo Identify(BufferedReadStream stream, CancellationTok break; } } + + // We cannot always trust the global GIF palette has actually been used. + // https://github.com/SixLabors/ImageSharp/issues/2866 + if (!globalColorTableUsed) + { + this.gifMetadata.ColorTableMode = FrameColorTableMode.Local; + } } finally { @@ -416,7 +441,15 @@ private void ReadComments(BufferedReadStream stream) /// The containing image data. /// The image to decode the information to. /// The previous frame. - private void ReadFrame(BufferedReadStream stream, ref Image? image, ref ImageFrame? previousFrame) + /// The previous frame disposal mode. + /// The background color. + /// Whether the frame has a global color table. + private bool ReadFrame( + BufferedReadStream stream, + ref Image? image, + ref ImageFrame? previousFrame, + ref FrameDisposalMode? previousDisposalMode, + ref Color backgroundColor) where TPixel : unmanaged, IPixel { this.ReadImageDescriptor(stream); @@ -438,10 +471,52 @@ private void ReadFrame(BufferedReadStream stream, ref Image? ima } ReadOnlySpan colorTable = MemoryMarshal.Cast(rawColorTable); - this.ReadFrameColors(stream, ref image, ref previousFrame, colorTable); + + // First frame + if (image is null) + { + if (this.backgroundColorIndex < colorTable.Length) + { + backgroundColor = Color.FromPixel(colorTable[this.backgroundColorIndex]); + } + else + { + backgroundColor = Color.Transparent; + } + + if (this.graphicsControlExtension.TransparencyFlag) + { + backgroundColor = backgroundColor.WithAlpha(0); + } + } + + this.ReadFrameColors(stream, ref image, ref previousFrame, ref previousDisposalMode, colorTable, backgroundColor.ToPixel()); + + // Update from newly decoded frame. + if (this.graphicsControlExtension.DisposalMethod != FrameDisposalMode.RestoreToPrevious) + { + if (this.backgroundColorIndex < colorTable.Length) + { + backgroundColor = Color.FromPixel(colorTable[this.backgroundColorIndex]); + } + else + { + backgroundColor = Color.Transparent; + } + + // TODO: I don't understand why this is always set to alpha of zero. + // This should be dependent on the transparency flag of the graphics + // control extension. ImageMagick does the same. + // if (this.graphicsControlExtension.TransparencyFlag) + { + backgroundColor = backgroundColor.WithAlpha(0); + } + } // Skip any remaining blocks SkipBlock(stream); + + return !hasLocalColorTable; } /// @@ -451,56 +526,73 @@ private void ReadFrame(BufferedReadStream stream, ref Image? ima /// The containing image data. /// The image to decode the information to. /// The previous frame. + /// The previous frame disposal mode. /// The color table containing the available colors. + /// The background color pixel. private void ReadFrameColors( BufferedReadStream stream, ref Image? image, ref ImageFrame? previousFrame, - ReadOnlySpan colorTable) + ref FrameDisposalMode? previousDisposalMode, + ReadOnlySpan colorTable, + TPixel backgroundPixel) where TPixel : unmanaged, IPixel { GifImageDescriptor descriptor = this.imageDescriptor; int imageWidth = this.logicalScreenDescriptor.Width; int imageHeight = this.logicalScreenDescriptor.Height; bool transFlag = this.graphicsControlExtension.TransparencyFlag; + FrameDisposalMode disposalMethod = this.graphicsControlExtension.DisposalMethod; + ImageFrame currentFrame; + ImageFrame? restoreFrame = null; - ImageFrame? prevFrame = null; - ImageFrame? currentFrame = null; - ImageFrame imageFrame; + if (previousFrame is null && previousDisposalMode is null) + { + image = transFlag + ? new Image(this.configuration, imageWidth, imageHeight, this.metadata) + : new Image(this.configuration, imageWidth, imageHeight, backgroundPixel, this.metadata); - if (previousFrame is null) + this.SetFrameMetadata(image.Frames.RootFrame.Metadata); + currentFrame = image.Frames.RootFrame; + } + else { - if (!transFlag) + if (previousFrame != null) { - image = new Image(this.configuration, imageWidth, imageHeight, Color.Black.ToPixel(), this.metadata); + currentFrame = image!.Frames.AddFrame(previousFrame); } else { - // This initializes the image to become fully transparent because the alpha channel is zero. - image = new Image(this.configuration, imageWidth, imageHeight, this.metadata); + currentFrame = image!.Frames.CreateFrame(backgroundPixel); } - this.SetFrameMetadata(image.Frames.RootFrame.Metadata); + this.SetFrameMetadata(currentFrame.Metadata); - imageFrame = image.Frames.RootFrame; - } - else - { if (this.graphicsControlExtension.DisposalMethod == FrameDisposalMode.RestoreToPrevious) { - prevFrame = previousFrame; + restoreFrame = previousFrame; } - // We create a clone of the frame and add it. - // We will overpaint the difference of pixels on the current frame to create a complete image. - // This ensures that we have enough pixel data to process without distortion. #2450 - currentFrame = image!.Frames.AddFrame(previousFrame); + if (previousDisposalMode == FrameDisposalMode.RestoreToBackground) + { + this.RestoreToBackground(currentFrame, backgroundPixel, transFlag); + } + } - this.SetFrameMetadata(currentFrame.Metadata); + if (this.graphicsControlExtension.DisposalMethod == FrameDisposalMode.RestoreToPrevious) + { + previousFrame = restoreFrame; + } + else + { + previousFrame = currentFrame; + } - imageFrame = currentFrame; + previousDisposalMode = disposalMethod; - this.RestoreToBackground(imageFrame); + if (disposalMethod == FrameDisposalMode.RestoreToBackground) + { + this.restoreArea = Rectangle.Intersect(image.Bounds, new(descriptor.Left, descriptor.Top, descriptor.Width, descriptor.Height)); } if (colorTable.Length == 0) @@ -568,7 +660,7 @@ private void ReadFrameColors( // #403 The left + width value can be larger than the image width int maxX = Math.Min(descriptorRight, imageWidth); - Span row = imageFrame.PixelBuffer.DangerousGetRowSpan(writeY); + Span row = currentFrame.PixelBuffer.DangerousGetRowSpan(writeY); // Take the descriptorLeft..maxX slice of the row, so the loop can be simplified. row = row[descriptorLeft..maxX]; @@ -599,19 +691,6 @@ private void ReadFrameColors( } } } - - if (prevFrame != null) - { - previousFrame = prevFrame; - return; - } - - previousFrame = currentFrame ?? image.Frames.RootFrame; - - if (this.graphicsControlExtension.DisposalMethod == FrameDisposalMode.RestoreToBackground) - { - this.restoreArea = new Rectangle(descriptor.Left, descriptor.Top, descriptor.Width, descriptor.Height); - } } /// @@ -620,7 +699,8 @@ private void ReadFrameColors( /// The containing image data. /// The collection of frame metadata. /// The previous frame metadata. - private void ReadFrameMetadata(BufferedReadStream stream, List frameMetadata, ref ImageFrameMetadata? previousFrame) + /// Whether the frame has a global color table. + private bool ReadFrameMetadata(BufferedReadStream stream, List frameMetadata, ref ImageFrameMetadata? previousFrame) { this.ReadImageDescriptor(stream); @@ -632,6 +712,11 @@ private void ReadFrameMetadata(BufferedReadStream stream, List(768, AllocationOptions.Clean); stream.Read(this.currentLocalColorTable.GetSpan()[..length]); } + else + { + this.currentLocalColorTable = null; + this.currentLocalColorTableSize = 0; + } // Skip the frame indices. Pixels length + mincode size. // The gif format does not tell us the length of the compressed data beforehand. @@ -649,6 +734,8 @@ private void ReadFrameMetadata(BufferedReadStream stream, List @@ -656,7 +743,9 @@ private void ReadFrameMetadata(BufferedReadStream stream, List /// The pixel format. /// The frame. - private void RestoreToBackground(ImageFrame frame) + /// The background color. + /// Whether the background is transparent. + private void RestoreToBackground(ImageFrame frame, TPixel background, bool transparent) where TPixel : unmanaged, IPixel { if (this.restoreArea is null) @@ -666,7 +755,14 @@ private void RestoreToBackground(ImageFrame frame) Rectangle interest = Rectangle.Intersect(frame.Bounds, this.restoreArea.Value); Buffer2DRegion pixelRegion = frame.PixelBuffer.GetRegion(interest); - pixelRegion.Clear(); + if (transparent) + { + pixelRegion.Clear(); + } + else + { + pixelRegion.Fill(background); + } this.restoreArea = null; } @@ -775,7 +871,9 @@ private void ReadLogicalScreenDescriptorAndGlobalColorTable(BufferedReadStream s } } - this.gifMetadata.BackgroundColorIndex = this.logicalScreenDescriptor.BackgroundColorIndex; + byte index = this.logicalScreenDescriptor.BackgroundColorIndex; + this.backgroundColorIndex = index; + this.gifMetadata.BackgroundColorIndex = index; } private unsafe struct ScratchBuffer diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 797e825dc4..43af476f2a 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -9,7 +9,6 @@ using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Quantization; namespace SixLabors.ImageSharp.Formats.Gif; @@ -19,6 +18,8 @@ namespace SixLabors.ImageSharp.Formats.Gif; /// internal sealed class GifEncoderCore { + private readonly GifEncoder encoder; + /// /// Used for allocating memory during processing operations. /// @@ -34,16 +35,6 @@ internal sealed class GifEncoderCore /// private readonly bool skipMetadata; - /// - /// The quantizer used to generate the color palette. - /// - private IQuantizer? quantizer; - - /// - /// Whether the quantizer was supplied via options. - /// - private readonly bool hasQuantizer; - /// /// The color table mode: Global or local. /// @@ -67,6 +58,9 @@ internal sealed class GifEncoderCore /// private readonly ushort? repeatCount; + /// + /// The transparent color mode. + /// private readonly TransparentColorMode transparentColorMode; /// @@ -78,9 +72,8 @@ public GifEncoderCore(Configuration configuration, GifEncoder encoder) { this.configuration = configuration; this.memoryAllocator = configuration.MemoryAllocator; + this.encoder = encoder; this.skipMetadata = encoder.SkipMetadata; - this.quantizer = encoder.Quantizer; - this.hasQuantizer = encoder.Quantizer is not null; this.colorTableMode = encoder.ColorTableMode; this.pixelSamplingStrategy = encoder.PixelSamplingStrategy; this.backgroundColor = encoder.BackgroundColor; @@ -104,70 +97,76 @@ public void Encode(Image image, Stream stream, CancellationToken GifMetadata gifMetadata = image.Metadata.CloneGifMetadata(); this.colorTableMode ??= gifMetadata.ColorTableMode; bool useGlobalTable = this.colorTableMode == FrameColorTableMode.Global; - - // Quantize the first image frame returning a palette. - IndexedImageFrame? quantized = null; + bool useGlobalTableForFirstFrame = useGlobalTable; // Work out if there is an explicit transparent index set for the frame. We use that to ensure the // correct value is set for the background index when quantizing. GifFrameMetadata frameMetadata = GetGifFrameMetadata(image.Frames.RootFrame, -1); + if (frameMetadata.ColorTableMode == FrameColorTableMode.Local) + { + useGlobalTableForFirstFrame = false; + } + + // Quantize the first image frame returning a palette. + IndexedImageFrame? quantized = null; + IQuantizer? globalQuantizer = this.encoder.Quantizer; + TransparentColorMode mode = this.transparentColorMode; - if (this.quantizer is null) + // Create a new quantizer options instance augmenting the transparent color mode to match the encoder. + QuantizerOptions options = (this.encoder.Quantizer?.Options ?? new()).DeepClone(o => o.TransparentColorMode = mode); + + if (globalQuantizer is null) { // Is this a gif with color information. If so use that, otherwise use octree. if (gifMetadata.ColorTableMode == FrameColorTableMode.Global && gifMetadata.GlobalColorTable?.Length > 0) { - // We avoid dithering by default to preserve the original colors. int transparencyIndex = GetTransparentIndex(quantized, frameMetadata); if (transparencyIndex >= 0 || gifMetadata.GlobalColorTable.Value.Length < 256) { - this.quantizer = new PaletteQuantizer(gifMetadata.GlobalColorTable.Value, new() { Dither = null }, transparencyIndex); + // We avoid dithering by default to preserve the original colors. + globalQuantizer = new PaletteQuantizer(gifMetadata.GlobalColorTable.Value, options.DeepClone(o => o.Dither = null)); } else { - this.quantizer = KnownQuantizers.Octree; + globalQuantizer = new OctreeQuantizer(options); } } else { - this.quantizer = KnownQuantizers.Octree; + globalQuantizer = new OctreeQuantizer(options); } } - // 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; + // Quantize the first frame. + IPixelSamplingStrategy strategy = this.pixelSamplingStrategy; + ImageFrame encodingFrame = image.Frames.RootFrame; + if (useGlobalTableForFirstFrame) + { + using IQuantizer firstFrameQuantizer = globalQuantizer.CreatePixelSpecificQuantizer(this.configuration, options); if (useGlobalTable) { - frameQuantizer.BuildPalette(configuration, mode, strategy, image); - quantized = frameQuantizer.QuantizeFrame(encodingFrame, image.Bounds); + firstFrameQuantizer.BuildPalette(strategy, image); } else { - frameQuantizer.BuildPalette(configuration, mode, strategy, encodingFrame); - quantized = frameQuantizer.QuantizeFrame(encodingFrame, image.Bounds); + firstFrameQuantizer.BuildPalette(strategy, encodingFrame); } - clonedFrame?.Dispose(); + quantized = firstFrameQuantizer.QuantizeFrame(encodingFrame, encodingFrame.Bounds); + } + else + { + quantized = this.QuantizeFrameAndUpdateMetadata( + encodingFrame, + globalQuantizer, + default, + encodingFrame.Bounds, + frameMetadata, + true, + false, + frameMetadata.HasTransparency ? frameMetadata.TransparencyIndex : -1, + Color.Transparent); } // Write the header. @@ -181,6 +180,7 @@ public void Encode(Image image, Stream stream, CancellationToken frameMetadata.TransparencyIndex = ClampIndex(derivedTransparencyIndex); } + // TODO: We should be checking the metadata here also I think? if (!TryGetBackgroundIndex(quantized, this.backgroundColor, out byte backgroundIndex)) { backgroundIndex = derivedTransparencyIndex >= 0 @@ -216,13 +216,18 @@ public void Encode(Image image, Stream stream, CancellationToken // Capture the global palette for reuse on subsequent frames and cleanup the quantized frame. TPixel[] globalPalette = image.Frames.Count == 1 ? [] : quantized.Palette.ToArray(); - this.EncodeAdditionalFrames( - stream, - image, - globalPalette, - derivedTransparencyIndex, - frameMetadata.DisposalMode, - cancellationToken); + if (image.Frames.Count > 1) + { + using PaletteQuantizer globalFrameQuantizer = new(this.configuration, globalQuantizer.Options, quantized.Palette.ToArray()); + this.EncodeAdditionalFrames( + stream, + image, + globalQuantizer, + globalFrameQuantizer, + derivedTransparencyIndex, + frameMetadata.DisposalMode, + cancellationToken); + } } finally { @@ -248,69 +253,43 @@ private static GifFrameMetadata GetGifFrameMetadata(ImageFrame f private void EncodeAdditionalFrames( Stream stream, Image image, - ReadOnlyMemory globalPalette, + IQuantizer globalQuantizer, + PaletteQuantizer globalFrameQuantizer, int globalTransparencyIndex, FrameDisposalMode previousDisposalMode, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - if (image.Frames.Count == 1) - { - return; - } - - PaletteQuantizer paletteQuantizer = default; - bool hasPaletteQuantizer = false; - // Store the first frame as a reference for de-duplication comparison. ImageFrame previousFrame = image.Frames.RootFrame; // This frame is reused to store de-duplicated pixel buffers. using ImageFrame encodingFrame = new(previousFrame.Configuration, previousFrame.Size); - try + for (int i = 1; i < image.Frames.Count; i++) { - for (int i = 1; i < image.Frames.Count; i++) - { - cancellationToken.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); - // 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); + // 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) - { - // 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; - } + this.EncodeAdditionalFrame( + stream, + previousFrame, + currentFrame, + nextFrame, + encodingFrame, + globalQuantizer, + globalFrameQuantizer, + useLocal, + gifMetadata, + previousDisposalMode); - this.EncodeAdditionalFrame( - stream, - previousFrame, - currentFrame, - nextFrame, - encodingFrame, - useLocal, - gifMetadata, - paletteQuantizer, - previousDisposalMode); - - previousFrame = currentFrame; - previousDisposalMode = gifMetadata.DisposalMode; - } - } - finally - { - if (hasPaletteQuantizer) - { - paletteQuantizer.Dispose(); - } + previousFrame = currentFrame; + previousDisposalMode = gifMetadata.DisposalMode; } } @@ -346,9 +325,10 @@ private void EncodeAdditionalFrame( ImageFrame currentFrame, ImageFrame? nextFrame, ImageFrame encodingFrame, + IQuantizer globalQuantizer, + PaletteQuantizer globalFrameQuantizer, bool useLocal, GifFrameMetadata metadata, - PaletteQuantizer globalPaletteQuantizer, FrameDisposalMode previousDisposalMode) where TPixel : unmanaged, IPixel { @@ -375,19 +355,16 @@ private void EncodeAdditionalFrame( background, true); - if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) - { - EncodingUtilities.ClearTransparentPixels(encodingFrame, background); - } - - using IndexedImageFrame quantized = this.QuantizeAdditionalFrameAndUpdateMetadata( + using IndexedImageFrame quantized = this.QuantizeFrameAndUpdateMetadata( encodingFrame, + globalQuantizer, + globalFrameQuantizer, bounds, metadata, useLocal, - globalPaletteQuantizer, difference, - transparencyIndex); + transparencyIndex, + background); this.WriteGraphicalControlExtension(metadata, stream); @@ -403,14 +380,16 @@ private void EncodeAdditionalFrame( this.WriteImageData(indices, stream, quantized.Palette.Length, metadata.TransparencyIndex); } - private IndexedImageFrame QuantizeAdditionalFrameAndUpdateMetadata( + private IndexedImageFrame QuantizeFrameAndUpdateMetadata( ImageFrame encodingFrame, + IQuantizer globalQuantizer, + PaletteQuantizer globalFrameQuantizer, Rectangle bounds, GifFrameMetadata metadata, bool useLocal, - PaletteQuantizer globalPaletteQuantizer, bool hasDuplicates, - int transparencyIndex) + int transparencyIndex, + Color transparentColor) where TPixel : unmanaged, IPixel { IndexedImageFrame quantized; @@ -434,15 +413,19 @@ private IndexedImageFrame QuantizeAdditionalFrameAndUpdateMetadata frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration, quantizer.Options); + QuantizerOptions options = globalQuantizer.Options.DeepClone(o => + { + o.MaxColors = palette.Length; + o.Dither = null; + }); + PaletteQuantizer quantizer = new(palette, options, transparencyIndex, transparentColor); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration); quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(encodingFrame, bounds); } else { // We must quantize the frame to generate a local color table. - IQuantizer quantizer = this.hasQuantizer ? this.quantizer! : KnownQuantizers.Octree; - using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration, quantizer.Options); + using IQuantizer frameQuantizer = globalQuantizer.CreatePixelSpecificQuantizer(this.configuration); quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(encodingFrame, bounds); // The transparency index derived by the quantizer will differ from the index @@ -454,7 +437,12 @@ private IndexedImageFrame QuantizeAdditionalFrameAndUpdateMetadata + { + o.MaxColors = palette.Length; + o.Dither = null; + }); + PaletteQuantizer quantizer = new(palette, paletteOptions, transparencyIndex, transparentColor); using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration, quantizer.Options); quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(encodingFrame, bounds); } @@ -462,8 +450,7 @@ private IndexedImageFrame QuantizeAdditionalFrameAndUpdateMetadata frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration, quantizer.Options); + using IQuantizer frameQuantizer = globalQuantizer.CreatePixelSpecificQuantizer(this.configuration); quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(encodingFrame, bounds); // The transparency index derived by the quantizer might differ from the index @@ -486,18 +473,19 @@ private IndexedImageFrame QuantizeAdditionalFrameAndUpdateMetadata()); + quantized = globalFrameQuantizer.QuantizeFrame(encodingFrame, bounds); } return quantized; diff --git a/src/ImageSharp/Formats/Gif/GifFrameMetadata.cs b/src/ImageSharp/Formats/Gif/GifFrameMetadata.cs index 5fe892c656..e1b3354ad2 100644 --- a/src/ImageSharp/Formats/Gif/GifFrameMetadata.cs +++ b/src/ImageSharp/Formats/Gif/GifFrameMetadata.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Numerics; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Gif; @@ -77,34 +76,12 @@ private GifFrameMetadata(GifFrameMetadata other) /// public static GifFrameMetadata FromFormatConnectingFrameMetadata(FormatConnectingFrameMetadata metadata) - { - int index = -1; - const float background = 1f; - if (metadata.ColorTable.HasValue) + => new() { - ReadOnlySpan colorTable = metadata.ColorTable.Value.Span; - for (int i = 0; i < colorTable.Length; i++) - { - Vector4 vector = colorTable[i].ToScaledVector4(); - if (vector.W < background) - { - index = i; - } - } - } - - bool hasTransparency = index >= 0; - - return new() - { - LocalColorTable = metadata.ColorTable, ColorTableMode = metadata.ColorTableMode, FrameDelay = (int)Math.Round(metadata.Duration.TotalMilliseconds / 10), DisposalMode = metadata.DisposalMode, - HasTransparency = hasTransparency, - TransparencyIndex = hasTransparency ? unchecked((byte)index) : byte.MinValue, }; - } /// public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() @@ -118,7 +95,6 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() return new() { - ColorTable = this.LocalColorTable, ColorTableMode = this.ColorTableMode, Duration = TimeSpan.FromMilliseconds(this.FrameDelay * 10), DisposalMode = this.DisposalMode, @@ -129,8 +105,7 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() /// public void AfterFrameApply(ImageFrame source, ImageFrame destination) where TPixel : unmanaged, IPixel - { - } + => this.LocalColorTable = null; /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); diff --git a/src/ImageSharp/Formats/Gif/GifMetadata.cs b/src/ImageSharp/Formats/Gif/GifMetadata.cs index 517609af45..77f600633b 100644 --- a/src/ImageSharp/Formats/Gif/GifMetadata.cs +++ b/src/ImageSharp/Formats/Gif/GifMetadata.cs @@ -71,37 +71,19 @@ private GifMetadata(GifMetadata other) /// public static GifMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) - { - int index = 0; - Color background = metadata.BackgroundColor; - if (metadata.ColorTable.HasValue) + => new() { - ReadOnlySpan colorTable = metadata.ColorTable.Value.Span; - for (int i = 0; i < colorTable.Length; i++) - { - if (background != colorTable[i]) - { - continue; - } - - index = i; - break; - } - } - - return new() - { - GlobalColorTable = metadata.ColorTable, + // Do not copy the color table or bit depth. + // This will lead to a mismatch when the image is comprised of frames + // extracted individually from a multi-frame image. ColorTableMode = metadata.ColorTableMode, RepeatCount = metadata.RepeatCount, - BackgroundColorIndex = (byte)Numerics.Clamp(index, 0, 255), }; - } /// public PixelTypeInfo GetPixelTypeInfo() { - int bpp = this.GlobalColorTable.HasValue + int bpp = this.ColorTableMode == FrameColorTableMode.Global && this.GlobalColorTable.HasValue ? Numerics.Clamp(ColorNumerics.GetBitsNeededForColorDepth(this.GlobalColorTable.Value.Length), 1, 8) : 8; @@ -114,27 +96,18 @@ public PixelTypeInfo GetPixelTypeInfo() /// public FormatConnectingMetadata ToFormatConnectingMetadata() - { - Color color = this.GlobalColorTable.HasValue && this.GlobalColorTable.Value.Span.Length > this.BackgroundColorIndex - ? this.GlobalColorTable.Value.Span[this.BackgroundColorIndex] - : Color.Transparent; - - return new() + => new() { AnimateRootFrame = true, - BackgroundColor = color, - ColorTable = this.GlobalColorTable, ColorTableMode = this.ColorTableMode, PixelTypeInfo = this.GetPixelTypeInfo(), RepeatCount = this.RepeatCount, }; - } /// public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel - { - } + => this.GlobalColorTable = null; /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); diff --git a/src/ImageSharp/Formats/IAnimatedImageEncoder.cs b/src/ImageSharp/Formats/IAnimatedImageEncoder.cs index d2c3ad6907..26f2114df2 100644 --- a/src/ImageSharp/Formats/IAnimatedImageEncoder.cs +++ b/src/ImageSharp/Formats/IAnimatedImageEncoder.cs @@ -14,17 +14,17 @@ public interface IAnimatedImageEncoder /// 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; } + public Color? BackgroundColor { get; } /// /// Gets the number of times any animation is repeated in supported encoders. /// - ushort? RepeatCount { get; } + public 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; } + public bool? AnimateRootFrame { get; } } /// diff --git a/src/ImageSharp/Formats/IFormatFrameMetadata.cs b/src/ImageSharp/Formats/IFormatFrameMetadata.cs index 20f27d050c..261cc12639 100644 --- a/src/ImageSharp/Formats/IFormatFrameMetadata.cs +++ b/src/ImageSharp/Formats/IFormatFrameMetadata.cs @@ -14,7 +14,7 @@ public interface IFormatFrameMetadata : IDeepCloneable /// Converts the metadata to a instance. /// /// The . - FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata(); + public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata(); /// /// This method is called after a process has been applied to the image frame. @@ -22,7 +22,7 @@ public interface IFormatFrameMetadata : IDeepCloneable /// The type of pixel format. /// The source image frame. /// The destination image frame. - void AfterFrameApply(ImageFrame source, ImageFrame destination) + public void AfterFrameApply(ImageFrame source, ImageFrame destination) where TPixel : unmanaged, IPixel; } @@ -39,6 +39,6 @@ public interface IFormatFrameMetadata : IFormatFrameMetadata, IDeepClonea /// The . /// The . #pragma warning disable CA1000 // Do not declare static members on generic types - static abstract TSelf FromFormatConnectingFrameMetadata(FormatConnectingFrameMetadata metadata); + public static abstract TSelf FromFormatConnectingFrameMetadata(FormatConnectingFrameMetadata metadata); #pragma warning restore CA1000 // Do not declare static members on generic types } diff --git a/src/ImageSharp/Formats/IFormatMetadata.cs b/src/ImageSharp/Formats/IFormatMetadata.cs index a351431c94..3142b465cb 100644 --- a/src/ImageSharp/Formats/IFormatMetadata.cs +++ b/src/ImageSharp/Formats/IFormatMetadata.cs @@ -14,20 +14,20 @@ public interface IFormatMetadata : IDeepCloneable /// Converts the metadata to a instance. /// /// The pixel type info. - PixelTypeInfo GetPixelTypeInfo(); + public PixelTypeInfo GetPixelTypeInfo(); /// /// Converts the metadata to a instance. /// /// The . - FormatConnectingMetadata ToFormatConnectingMetadata(); + public FormatConnectingMetadata ToFormatConnectingMetadata(); /// /// This method is called after a process has been applied to the image. /// /// The type of pixel format. /// The destination image . - void AfterImageApply(Image destination) + public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel; } @@ -44,6 +44,6 @@ public interface IFormatMetadata : IFormatMetadata, IDeepCloneable /// The . /// The . #pragma warning disable CA1000 // Do not declare static members on generic types - static abstract TSelf FromFormatConnectingMetadata(FormatConnectingMetadata metadata); + public static abstract TSelf FromFormatConnectingMetadata(FormatConnectingMetadata metadata); #pragma warning restore CA1000 // Do not declare static members on generic types } diff --git a/src/ImageSharp/Formats/IQuantizingImageEncoder.cs b/src/ImageSharp/Formats/IQuantizingImageEncoder.cs index 5edf6e40e9..1ce2aa0918 100644 --- a/src/ImageSharp/Formats/IQuantizingImageEncoder.cs +++ b/src/ImageSharp/Formats/IQuantizingImageEncoder.cs @@ -13,12 +13,12 @@ public interface IQuantizingImageEncoder /// /// Gets the quantizer used to generate the color palette. /// - IQuantizer? Quantizer { get; } + public IQuantizer? Quantizer { get; } /// /// Gets the used for quantization when building color palettes. /// - IPixelSamplingStrategy PixelSamplingStrategy { get; } + public IPixelSamplingStrategy PixelSamplingStrategy { get; } } /// diff --git a/src/ImageSharp/Formats/ISpecializedDecoderOptions.cs b/src/ImageSharp/Formats/ISpecializedDecoderOptions.cs index e0a4c9b62c..881b5bcd44 100644 --- a/src/ImageSharp/Formats/ISpecializedDecoderOptions.cs +++ b/src/ImageSharp/Formats/ISpecializedDecoderOptions.cs @@ -11,5 +11,5 @@ public interface ISpecializedDecoderOptions /// /// Gets the general decoder options. /// - DecoderOptions GeneralOptions { get; init; } + public DecoderOptions GeneralOptions { get; init; } } diff --git a/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs b/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs index 62aa705cbe..31f65133e6 100644 --- a/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs +++ b/src/ImageSharp/Formats/Ico/IcoFrameMetadata.cs @@ -96,8 +96,7 @@ public static IcoFrameMetadata FromFormatConnectingFrameMetadata(FormatConnectin BmpBitsPerPixel = bbpp, Compression = compression, EncodingWidth = ClampEncodingDimension(metadata.EncodingWidth), - EncodingHeight = ClampEncodingDimension(metadata.EncodingHeight), - ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null + EncodingHeight = ClampEncodingDimension(metadata.EncodingHeight) }; } @@ -106,7 +105,6 @@ public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() => new() { PixelTypeInfo = this.GetPixelTypeInfo(), - ColorTable = this.ColorTable, EncodingWidth = this.EncodingWidth, EncodingHeight = this.EncodingHeight }; @@ -119,6 +117,7 @@ public void AfterFrameApply(ImageFrame source, ImageFrame diff --git a/src/ImageSharp/Formats/Ico/IcoMetadata.cs b/src/ImageSharp/Formats/Ico/IcoMetadata.cs index a6c2704b31..f8c2ff40f2 100644 --- a/src/ImageSharp/Formats/Ico/IcoMetadata.cs +++ b/src/ImageSharp/Formats/Ico/IcoMetadata.cs @@ -71,8 +71,7 @@ public static IcoMetadata FromFormatConnectingMetadata(FormatConnectingMetadata return new IcoMetadata { BmpBitsPerPixel = bbpp, - Compression = compression, - ColorTable = compression == IconFrameCompression.Bmp ? metadata.ColorTable : null + Compression = compression }; } @@ -145,15 +144,13 @@ public FormatConnectingMetadata ToFormatConnectingMetadata() EncodingType = this.Compression == IconFrameCompression.Bmp && this.BmpBitsPerPixel <= BmpBitsPerPixel.Bit8 ? EncodingType.Lossy : EncodingType.Lossless, - PixelTypeInfo = this.GetPixelTypeInfo(), - ColorTable = this.ColorTable + PixelTypeInfo = this.GetPixelTypeInfo() }; /// public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel - { - } + => this.ColorTable = null; /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); diff --git a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs index 03e01f912f..76b14832ae 100644 --- a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs +++ b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs @@ -120,17 +120,17 @@ private void InitHeader(Image image) this.entries = this.iconFileType switch { IconFileType.ICO => - image.Frames.Select(i => + [.. image.Frames.Select(i => { IcoFrameMetadata metadata = i.Metadata.GetIcoMetadata(); return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry(i.Size)); - }).ToArray(), + })], IconFileType.CUR => - image.Frames.Select(i => + [.. image.Frames.Select(i => { CurFrameMetadata metadata = i.Metadata.GetCurMetadata(); return new EncodingFrameMetadata(metadata.Compression, metadata.BmpBitsPerPixel, metadata.ColorTable, metadata.ToIconDirEntry(i.Size)); - }).ToArray(), + })], _ => throw new NotSupportedException(), }; } @@ -149,9 +149,15 @@ private void InitHeader(Image image) if (metadata.ColorTable is null) { + int count = metadata.Entry.ColorCount; + if (count == 0) + { + count = 256; + } + return new WuQuantizer(new() { - MaxColors = metadata.Entry.ColorCount + MaxColors = count }); } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 484241d52f..b0ec73d2a1 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1086,7 +1086,7 @@ private void ProcessDefilteredScanline( { PixelBlender blender = PixelOperations.Instance.GetPixelBlender(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver); - blender.Blend(this.configuration, destination, destination, rowSpan, 1f); + blender.Blend(this.configuration, destination, destination, rowSpan, 1F); } } finally @@ -1208,7 +1208,7 @@ private void ProcessInterlacedDefilteredScanline( { PixelBlender blender = PixelOperations.Instance.GetPixelBlender(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver); - blender.Blend(this.configuration, destination, destination, rowSpan, 1f); + blender.Blend(this.configuration, destination, destination, rowSpan, 1F); } } finally @@ -1866,6 +1866,9 @@ private bool TryReadChunk(Span buffer, out PngChunk chunk) return false; } + // Capture the current position so we can revert back to it if we fail to read a valid chunk. + long position = this.currentStream.Position; + if (!this.TryReadChunkLength(buffer, out int length)) { // IEND @@ -1884,7 +1887,48 @@ private bool TryReadChunk(Span buffer, out PngChunk chunk) } } - PngChunkType type = this.ReadChunkType(buffer); + PngChunkType type; + + // Loop until we get a chunk type that is valid. + while (true) + { + type = this.ReadChunkType(buffer); + if (!IsValidChunkType(type)) + { + // The chunk type is invalid. + // Revert back to the next byte past the previous position and try again. + this.currentStream.Position = ++position; + + // If we are now at the end of the stream, we're done. + if (this.currentStream.Position >= this.currentStream.Length) + { + chunk = default; + return false; + } + + // Read the next chunk’s length. + if (!this.TryReadChunkLength(buffer, out length)) + { + chunk = default; + return false; + } + + while (length < 0) + { + if (!this.TryReadChunkLength(buffer, out length)) + { + chunk = default; + return false; + } + } + + // Continue to try reading the next chunk. + continue; + } + + // We have a valid chunk type. + break; + } // If we're reading color metadata only we're only interested in the IHDR and tRNS chunks. // We can skip most other chunk data in the stream for better performance. @@ -1901,7 +1945,7 @@ private bool TryReadChunk(Span buffer, out PngChunk chunk) // A chunk might report a length that exceeds the length of the stream. // Take the minimum of the two values to ensure we don't read past the end of the stream. - long position = this.currentStream.Position; + position = this.currentStream.Position; chunk = new PngChunk( length: (int)Math.Min(length, this.currentStream.Length - position), type: type, @@ -1919,6 +1963,32 @@ private bool TryReadChunk(Span buffer, out PngChunk chunk) return true; } + /// + /// Determines whether the 4-byte chunk type is valid (all ASCII letters). + /// + /// The chunk type. + [MethodImpl(InliningOptions.ShortMethod)] + private static bool IsValidChunkType(PngChunkType type) + { + uint value = (uint)type; + byte b0 = (byte)(value >> 24); + byte b1 = (byte)(value >> 16); + byte b2 = (byte)(value >> 8); + byte b3 = (byte)value; + return IsAsciiLetter(b0) && IsAsciiLetter(b1) && IsAsciiLetter(b2) && IsAsciiLetter(b3); + } + + /// + /// Returns a value indicating whether the given byte is an ASCII letter. + /// + /// The byte to check. + /// + /// if the byte is an ASCII letter; otherwise, . + /// + [MethodImpl(InliningOptions.ShortMethod)] + private static bool IsAsciiLetter(byte b) + => (b >= (byte)'A' && b <= (byte)'Z') || (b >= (byte)'a' && b <= (byte)'z'); + /// /// Validates the png chunk. /// diff --git a/src/ImageSharp/Formats/Png/PngEncoder.cs b/src/ImageSharp/Formats/Png/PngEncoder.cs index 63e675b505..b6031c1640 100644 --- a/src/ImageSharp/Formats/Png/PngEncoder.cs +++ b/src/ImageSharp/Formats/Png/PngEncoder.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using SixLabors.ImageSharp.Processing.Processors.Quantization; - namespace SixLabors.ImageSharp.Formats.Png; /// @@ -10,16 +8,6 @@ namespace SixLabors.ImageSharp.Formats.Png; /// public class PngEncoder : QuantizingAnimatedImageEncoder { - /// - /// Initializes a new instance of the class. - /// - public PngEncoder() - - // Hack. TODO: Investigate means to fix/optimize the Wu quantizer. - // The Wu quantizer does not handle the default sampling strategy well for some larger images. - // It's expensive and the results are not better than the extensive strategy. - => this.PixelSamplingStrategy = new ExtensivePixelSamplingStrategy(); - /// /// Gets the number of bits per sample or per palette index (not per pixel). /// Not all values are allowed for all values. @@ -53,11 +41,6 @@ public PngEncoder() /// The gamma value of the image. public float? Gamma { get; init; } - /// - /// Gets the transparency threshold. - /// - public byte Threshold { get; init; } = byte.MaxValue; - /// /// Gets a value indicating whether this instance should write an Adam7 interlaced image. /// diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index ea36d9fe1e..e9b76522c9 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -3,8 +3,8 @@ using System.Buffers; using System.Buffers.Binary; +using System.Diagnostics.CodeAnalysis; using System.IO.Hashing; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; @@ -119,18 +119,13 @@ internal sealed class PngEncoderCore : IDisposable /// private IQuantizer? quantizer; - /// - /// Any explicit quantized transparent index provided by the background color. - /// - 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; + private Color? backgroundColor; /// /// The number of times any animation is repeated. @@ -158,7 +153,6 @@ public PngEncoderCore(Configuration configuration, PngEncoder encoder) this.memoryAllocator = configuration.MemoryAllocator; this.encoder = encoder; this.quantizer = encoder.Quantizer; - this.backgroundColor = encoder.BackgroundColor; this.repeatCount = encoder.RepeatCount; this.animateRootFrame = encoder.AnimateRootFrame; } @@ -187,74 +181,95 @@ public void Encode(Image image, Stream stream, CancellationToken ImageFrame? clonedFrame = null; ImageFrame currentFrame = image.Frames.RootFrame; - int currentFrameIndex = 0; + IndexedImageFrame? quantized = null; + PaletteQuantizer? paletteQuantizer = null; + Buffer2DRegion currentFrameRegion = currentFrame.PixelBuffer.GetRegion(); - bool clearTransparency = EncodingUtilities.ShouldClearTransparentPixels(this.encoder.TransparentColorMode); - if (clearTransparency) + try { - currentFrame = clonedFrame = currentFrame.Clone(); - EncodingUtilities.ClearTransparentPixels(currentFrame, Color.Transparent); - } + int currentFrameIndex = 0; - // Do not move this. We require an accurate bit depth for the header chunk. - IndexedImageFrame? quantized = this.CreateQuantizedImageAndUpdateBitDepth( - pngMetadata, - currentFrame, - currentFrame.Bounds, - null); - - this.WriteHeaderChunk(stream); - this.WriteGammaChunk(stream); - this.WriteCicpChunk(stream, metadata); - this.WriteColorProfileChunk(stream, metadata); - this.WritePaletteChunk(stream, quantized); - this.WriteTransparencyChunk(stream, pngMetadata); - this.WritePhysicalChunk(stream, metadata); - this.WriteExifChunk(stream, metadata); - this.WriteXmpChunk(stream, metadata); - this.WriteTextChunks(stream, pngMetadata); + bool clearTransparency = EncodingUtilities.ShouldReplaceTransparentPixels(this.encoder.TransparentColorMode); - if (image.Frames.Count > 1) - { - this.WriteAnimationControlChunk( - stream, - (uint)(image.Frames.Count - (pngMetadata.AnimateRootFrame ? 0 : 1)), - this.repeatCount ?? pngMetadata.RepeatCount); - } + // No need to clone when quantizing. The quantizer will do it for us. + // TODO: We should really try to avoid the clone entirely. + if (clearTransparency && this.colorType is not PngColorType.Palette) + { + currentFrame = clonedFrame = currentFrame.Clone(); + currentFrameRegion = currentFrame.PixelBuffer.GetRegion(); + EncodingUtilities.ReplaceTransparentPixels(this.configuration, in currentFrameRegion); + } - // If the first frame isn't animated, write it as usual and skip it when writing animated frames - 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++; - } + // Do not move this. We require an accurate bit depth for the header chunk. + quantized = this.CreateQuantizedImageAndUpdateBitDepth( + pngMetadata, + image, + currentFrame, + currentFrame.Bounds, + null); + + this.WriteHeaderChunk(stream); + this.WriteGammaChunk(stream); + this.WriteCicpChunk(stream, metadata); + this.WriteColorProfileChunk(stream, metadata); + this.WritePaletteChunk(stream, quantized); + this.WriteTransparencyChunk(stream, pngMetadata); + this.WritePhysicalChunk(stream, metadata); + this.WriteExifChunk(stream, metadata); + this.WriteXmpChunk(stream, metadata); + this.WriteTextChunks(stream, pngMetadata); + + if (image.Frames.Count > 1) + { + 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 + 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(in frameControl, in currentFrameRegion, quantized, stream, false); + currentFrameIndex++; + } - try - { if (image.Frames.Count > 1) { // Write the first animated frame. currentFrame = image.Frames[currentFrameIndex]; + currentFrameRegion = currentFrame.PixelBuffer.GetRegion(); + 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); + this.WriteDataChunks(in frameControl, in currentFrameRegion, quantized, stream, false); } else { - sequenceNumber += this.WriteDataChunks(frameControl, currentFrame.PixelBuffer.GetRegion(), quantized, stream, true); + sequenceNumber += this.WriteDataChunks(in frameControl, in currentFrameRegion, quantized, stream, true); } currentFrameIndex++; // Capture the global palette for reuse on subsequent frames. - ReadOnlyMemory? previousPalette = quantized?.Palette.ToArray(); + ReadOnlyMemory previousPalette = quantized?.Palette.ToArray(); + + if (!previousPalette.IsEmpty) + { + // Use the previously derived global palette and a shared quantizer to + // quantize the subsequent frames. This allows us to cache the color matching resolution. + paletteQuantizer ??= new( + this.configuration, + this.quantizer!.Options, + previousPalette); + } // Write following frames. ImageFrame previousFrame = image.Frames.RootFrame; @@ -267,13 +282,26 @@ public void Encode(Image image, Stream stream, CancellationToken cancellationToken.ThrowIfCancellationRequested(); ImageFrame? prev = previousDisposal == FrameDisposalMode.RestoreToBackground ? null : previousFrame; + currentFrame = image.Frames[currentFrameIndex]; + currentFrameRegion = currentFrame.PixelBuffer.GetRegion(); + ImageFrame? nextFrame = currentFrameIndex < image.Frames.Count - 1 ? image.Frames[currentFrameIndex + 1] : null; frameMetadata = currentFrame.Metadata.GetPngMetadata(); - bool blend = frameMetadata.BlendMode == FrameBlendMode.Over; + + // Determine whether to blend the current frame over the existing canvas. + // Blending is applied only when the blend method is 'Over' (source-over blending) + // and when the frame's disposal method is not 'RestoreToPrevious', which indicates that + // the frame should not permanently alter the canvas. + bool blend = frameMetadata.BlendMode == FrameBlendMode.Over + && frameMetadata.DisposalMode != FrameDisposalMode.RestoreToPrevious; + + // Establish the background color for the current frame. + // If the disposal method is 'RestoreToBackground', use the predefined background color; + // otherwise, use transparent, as no explicit background restoration is needed. Color background = frameMetadata.DisposalMode == FrameDisposalMode.RestoreToBackground - ? this.backgroundColor ?? Color.Transparent + ? this.backgroundColor.Value : Color.Transparent; (bool difference, Rectangle bounds) = @@ -286,9 +314,9 @@ public void Encode(Image image, Stream stream, CancellationToken background, blend); - if (clearTransparency) + if (clearTransparency && this.colorType is not PngColorType.Palette) { - EncodingUtilities.ClearTransparentPixels(encodingFrame, background); + EncodingUtilities.ReplaceTransparentPixels(encodingFrame); } // Each frame control sequence number must be incremented by the number of frame data chunks that follow. @@ -296,8 +324,20 @@ public void Encode(Image image, Stream stream, CancellationToken // 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; + + quantized = this.CreateQuantizedFrame( + this.encoder, + this.colorType, + this.bitDepth, + pngMetadata, + image, + encodingFrame, + bounds, + paletteQuantizer, + default); + + Buffer2DRegion encodingFrameRegion = encodingFrame.PixelBuffer.GetRegion(bounds); + sequenceNumber += this.WriteDataChunks(in frameControl, in encodingFrameRegion, quantized, stream, true) + 1; previousFrame = currentFrame; previousDisposal = frameMetadata.DisposalMode; @@ -313,6 +353,7 @@ public void Encode(Image image, Stream stream, CancellationToken // Dispose of allocations from final frame. clonedFrame?.Dispose(); quantized?.Dispose(); + paletteQuantizer?.Dispose(); } } @@ -328,18 +369,35 @@ public void Dispose() /// /// The type of the pixel. /// The image metadata. - /// The frame to quantize. + /// The image. + /// The current image frame. /// The area of interest within the frame. - /// Any previously derived palette. + /// The quantizer containing any previously derived palette. /// The quantized image. private IndexedImageFrame? CreateQuantizedImageAndUpdateBitDepth( PngMetadata metadata, + Image image, ImageFrame frame, Rectangle bounds, - ReadOnlyMemory? previousPalette) + PaletteQuantizer? paletteQuantizer) where TPixel : unmanaged, IPixel { - IndexedImageFrame? quantized = this.CreateQuantizedFrame(this.encoder, this.colorType, this.bitDepth, metadata, frame, bounds, previousPalette); + PngFrameMetadata frameMetadata = frame.Metadata.GetPngMetadata(); + Color background = frameMetadata.DisposalMode == FrameDisposalMode.RestoreToBackground + ? this.backgroundColor ?? Color.Transparent + : Color.Transparent; + + IndexedImageFrame? quantized = this.CreateQuantizedFrame( + this.encoder, + this.colorType, + this.bitDepth, + metadata, + image, + frame, + bounds, + paletteQuantizer, + background); + this.bitDepth = CalculateBitDepth(this.colorType, this.bitDepth, quantized); return quantized; } @@ -734,11 +792,6 @@ private void WritePaletteChunk(Stream stream, IndexedImageFrame? byte alpha = rgba.A; Unsafe.Add(ref colorTableRef, (uint)i) = rgba.Rgb; - if (alpha > this.encoder.Threshold) - { - alpha = byte.MaxValue; - } - hasAlpha = hasAlpha || alpha < byte.MaxValue; Unsafe.Add(ref alphaTableRef, (uint)i) = alpha; } @@ -1105,7 +1158,7 @@ private FrameControl WriteFrameControlChunk(Stream stream, PngFrameMetadata fram /// The quantized pixel data. Can be null. /// The stream. /// Is writing fdAT or IDAT. - private uint WriteDataChunks(FrameControl frameControl, Buffer2DRegion frame, IndexedImageFrame? quantized, Stream stream, bool isFrame) + private uint WriteDataChunks(in FrameControl frameControl, in Buffer2DRegion frame, IndexedImageFrame? quantized, Stream stream, bool isFrame) where TPixel : unmanaged, IPixel { byte[] buffer; @@ -1123,12 +1176,12 @@ private uint WriteDataChunks(FrameControl frameControl, Buffer2DRegionThe image frame pixel buffer. /// The quantized pixels. /// The deflate stream. - private void EncodePixels(Buffer2DRegion pixels, IndexedImageFrame? quantized, ZlibDeflateStream deflateStream) + private void EncodePixels(in Buffer2DRegion pixels, IndexedImageFrame? quantized, ZlibDeflateStream deflateStream) where TPixel : unmanaged, IPixel { int bytesPerScanline = this.CalculateScanlineLength(pixels.Width); @@ -1210,7 +1263,8 @@ private void EncodePixels(Buffer2DRegion pixels, IndexedImageFra Span attempt = attemptBuffer.GetSpan(); for (int y = 0; y < pixels.Height; y++) { - this.CollectAndFilterPixelRow(pixels.DangerousGetRowSpan(y), ref filter, ref attempt, quantized, y); + ReadOnlySpan rowSpan = pixels.DangerousGetRowSpan(y); + this.CollectAndFilterPixelRow(rowSpan, ref filter, ref attempt, quantized, y); deflateStream.Write(filter); this.SwapScanlineBuffers(); } @@ -1222,7 +1276,7 @@ private void EncodePixels(Buffer2DRegion pixels, IndexedImageFra /// The type of the pixel. /// The image frame pixel buffer. /// The deflate stream. - private void EncodeAdam7Pixels(Buffer2DRegion pixels, ZlibDeflateStream deflateStream) + private void EncodeAdam7Pixels(in Buffer2DRegion pixels, ZlibDeflateStream deflateStream) where TPixel : unmanaged, IPixel { for (int pass = 0; pass < 7; pass++) @@ -1258,7 +1312,8 @@ private void EncodeAdam7Pixels(Buffer2DRegion pixels, ZlibDeflat // Encode data // Note: quantized parameter not used // Note: row parameter not used - this.CollectAndFilterPixelRow(block, ref filter, ref attempt, null, -1); + ReadOnlySpan blockSpan = block; + this.CollectAndFilterPixelRow(blockSpan, ref filter, ref attempt, null, -1); deflateStream.Write(filter); this.SwapScanlineBuffers(); @@ -1432,6 +1487,7 @@ private void SwapScanlineBuffers() /// The PNG metadata. /// if set to true [use16 bit]. /// The bytes per pixel. + [MemberNotNull(nameof(backgroundColor))] private void SanitizeAndSetEncoderOptions( PngEncoder encoder, PngMetadata pngMetadata, @@ -1473,6 +1529,7 @@ private void SanitizeAndSetEncoderOptions( this.interlaceMode = encoder.InterlaceMethod ?? pngMetadata.InterlaceMethod; this.chunkFilter = encoder.SkipMetadata ? PngChunkFilter.ExcludeAll : encoder.ChunkFilter ?? PngChunkFilter.None; + this.backgroundColor = encoder.BackgroundColor ?? pngMetadata.TransparentColor ?? Color.Transparent; } /// @@ -1483,17 +1540,21 @@ private void SanitizeAndSetEncoderOptions( /// The color type. /// The bits per component. /// The image metadata. - /// The frame to quantize. + /// The image. + /// The current image frame. /// The frame area of interest. - /// Any previously derived palette. + /// The quantizer containing any previously derived palette. + /// The background color. private IndexedImageFrame? CreateQuantizedFrame( QuantizingImageEncoder encoder, PngColorType colorType, byte bitDepth, PngMetadata metadata, + Image image, ImageFrame frame, Rectangle bounds, - ReadOnlyMemory? previousPalette) + PaletteQuantizer? paletteQuantizer, + Color backgroundColor) where TPixel : unmanaged, IPixel { if (colorType is not PngColorType.Palette) @@ -1501,55 +1562,59 @@ private void SanitizeAndSetEncoderOptions( return null; } - if (previousPalette is not null) + if (paletteQuantizer.HasValue) { - // Use the previously derived palette created by quantizing the root frame to quantize the current frame. - using PaletteQuantizer paletteQuantizer = new( - this.configuration, - this.quantizer!.Options, - previousPalette.Value, - this.derivedTransparencyIndex); - paletteQuantizer.BuildPalette(encoder.PixelSamplingStrategy, frame); - return paletteQuantizer.QuantizeFrame(frame, bounds); + return paletteQuantizer.Value.QuantizeFrame(frame, bounds); } // Use the metadata to determine what quantization depth to use if no quantizer has been set. if (this.quantizer is null) { - if (metadata.ColorTable is not null) + if (metadata.ColorTable?.Length > 0) { // We can use the color data from the decoded metadata here. // We avoid dithering by default to preserve the original colors. - ReadOnlySpan palette = metadata.ColorTable.Value.Span; - - // Certain operations perform alpha premultiplication, which can cause the color to change so we - // must search for the transparency index in the palette. - // Transparent pixels are much more likely to be found at the end of a palette. - int index = -1; - for (int i = palette.Length - 1; i >= 0; i--) - { - Vector4 instance = palette[i].ToScaledVector4(); - if (instance.W == 0f) - { - index = i; - break; - } - } - - this.derivedTransparencyIndex = index; - - this.quantizer = new PaletteQuantizer(metadata.ColorTable.Value, new() { Dither = null }, this.derivedTransparencyIndex); + QuantizerOptions options = new() { Dither = null, TransparentColorMode = encoder.TransparentColorMode }; + this.quantizer = new PaletteQuantizer(metadata.ColorTable.Value, options); } else { - this.quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = ColorNumerics.GetColorCountForBitDepth(bitDepth) }); + // Don't use the default transparency threshold for quantization as PNG can handle multiple transparent colors. + // We choose a value that is close to zero so that edge cases causes by lower bit depths for the alpha channel are handled correctly. + QuantizerOptions options = new() + { + TransparencyThreshold = 0, + MaxColors = ColorNumerics.GetColorCountForBitDepth(bitDepth), + TransparentColorMode = encoder.TransparentColorMode + }; + + this.quantizer = new WuQuantizer(options); } } // Create quantized frame returning the palette and set the bit depth. using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(frame.Configuration); - frameQuantizer.BuildPalette(encoder.PixelSamplingStrategy, frame); + if (image.Frames.Count > 1) + { + // Encoding animated frames with a global palette requires a transparent pixel in the palette + // since we only encode the delta between frames. To ensure that we have a transparent pixel + // we create a fake frame with a containing only transparent pixels and add it to the palette. + using Buffer2D fake = image.Configuration.MemoryAllocator.Allocate2D(Math.Min(256, image.Width), Math.Min(256, image.Height)); + TPixel backGroundPixel = backgroundColor.ToPixel(); + for (int i = 0; i < fake.Height; i++) + { + fake.DangerousGetRowSpan(i).Fill(backGroundPixel); + } + + Buffer2DRegion fakeRegion = fake.GetRegion(); + frameQuantizer.AddPaletteColors(in fakeRegion); + } + + frameQuantizer.BuildPalette( + encoder.PixelSamplingStrategy, + image); + return frameQuantizer.QuantizeFrame(frame, bounds); } diff --git a/src/ImageSharp/Formats/Png/PngMetadata.cs b/src/ImageSharp/Formats/Png/PngMetadata.cs index 00cba088cb..59ca3b17a0 100644 --- a/src/ImageSharp/Formats/Png/PngMetadata.cs +++ b/src/ImageSharp/Formats/Png/PngMetadata.cs @@ -93,25 +93,6 @@ private PngMetadata(PngMetadata other) /// public static PngMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) { - // Should the conversion be from a format that uses a 24bit palette entries (gif) - // we need to clone and adjust the color table to allow for transparency. - Color[]? colorTable = metadata.ColorTable?.ToArray(); - if (colorTable != null) - { - for (int i = 0; i < colorTable.Length; i++) - { - ref Color c = ref colorTable[i]; - if (c != metadata.BackgroundColor) - { - continue; - } - - // Png treats background as fully empty - c = Color.Transparent; - break; - } - } - PngColorType color; PixelColorType colorType = metadata.PixelTypeInfo.ColorType; @@ -152,7 +133,6 @@ public static PngMetadata FromFormatConnectingMetadata(FormatConnectingMetadata { ColorType = color, BitDepth = bitDepth, - ColorTable = colorTable, RepeatCount = metadata.RepeatCount, }; } @@ -241,7 +221,6 @@ public PixelTypeInfo GetPixelTypeInfo() public FormatConnectingMetadata ToFormatConnectingMetadata() => new() { - ColorTable = this.ColorTable, ColorTableMode = FrameColorTableMode.Global, PixelTypeInfo = this.GetPixelTypeInfo(), RepeatCount = (ushort)Numerics.Clamp(this.RepeatCount, 0, ushort.MaxValue), @@ -250,8 +229,7 @@ public FormatConnectingMetadata ToFormatConnectingMetadata() /// public void AfterImageApply(Image destination) where TPixel : unmanaged, IPixel - { - } + => this.ColorTable = null; /// IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); diff --git a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs index 872cec3fd0..a5e1596b37 100644 --- a/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs +++ b/src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs @@ -90,10 +90,12 @@ private void WritePixels(Image image, Stream stream, Cancellatio ImageFrame? clonedFrame = null; try { - if (EncodingUtilities.ShouldClearTransparentPixels(this.encoder.TransparentColorMode)) + // TODO: Try to avoid cloning the frame if possible. + // We should be cloning individual scanlines instead. + if (EncodingUtilities.ShouldReplaceTransparentPixels(this.encoder.TransparentColorMode)) { clonedFrame = image.Frames.RootFrame.Clone(); - EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + EncodingUtilities.ReplaceTransparentPixels(clonedFrame); } ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index e2ea9c4fe7..a587e19608 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -110,10 +110,12 @@ public void Encode(Image image, Stream stream, CancellationToken ImageFrame? clonedFrame = null; try { - if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + // TODO: Try to avoid cloning the frame if possible. + // We should be cloning individual scanlines instead. + if (EncodingUtilities.ShouldReplaceTransparentPixels(this.transparentColorMode)) { clonedFrame = image.Frames.RootFrame.Clone(); - EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + EncodingUtilities.ReplaceTransparentPixels(clonedFrame); } ImageFrame encodingFrame = clonedFrame ?? image.Frames.RootFrame; diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index e16bf9831c..d7508b02e8 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -146,10 +146,12 @@ public void Encode(Image image, Stream stream, CancellationToken { cancellationToken.ThrowIfCancellationRequested(); - if (EncodingUtilities.ShouldClearTransparentPixels(this.transparentColorMode)) + // TODO: Try to avoid cloning the frame if possible. + // We should be cloning individual scanlines instead. + if (EncodingUtilities.ShouldReplaceTransparentPixels(this.transparentColorMode)) { clonedFrame = frame.Clone(); - EncodingUtilities.ClearTransparentPixels(clonedFrame, Color.Transparent); + EncodingUtilities.ReplaceTransparentPixels(clonedFrame); } ImageFrame encodingFrame = clonedFrame ?? frame; diff --git a/src/ImageSharp/Formats/TransparentColorMode.cs b/src/ImageSharp/Formats/TransparentColorMode.cs index 39986b5024..fe88c314f2 100644 --- a/src/ImageSharp/Formats/TransparentColorMode.cs +++ b/src/ImageSharp/Formats/TransparentColorMode.cs @@ -4,7 +4,7 @@ namespace SixLabors.ImageSharp.Formats; /// -/// Specifies how transparent pixels should be handled during encoding. +/// Specifies how pixels with transparent alpha components should be handled during encoding and quantization. /// public enum TransparentColorMode { @@ -18,5 +18,5 @@ public enum TransparentColorMode /// to fully transparent pixels (all components set to zero), /// which may improve compression. /// - Clear = 1, + Clear = 1 } diff --git a/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs b/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs index 83f9e797ab..2b843cc8f6 100644 --- a/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs +++ b/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs @@ -32,7 +32,7 @@ protected BitReaderBase(Stream inputStream, int imageDataSize, MemoryAllocator m /// Used for allocating memory during reading data from the stream. protected static IMemoryOwner ReadImageDataFromStream(Stream input, int bytesToRead, MemoryAllocator memoryAllocator) { - IMemoryOwner data = memoryAllocator.Allocate(bytesToRead); + IMemoryOwner data = memoryAllocator.Allocate(bytesToRead, AllocationOptions.Clean); Span dataSpan = data.Memory.Span; input.Read(dataSpan[..bytesToRead], 0, bytesToRead); diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Decoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Decoder.cs index b3c5bfaf41..3c8bafa1b2 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Decoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Decoder.cs @@ -67,14 +67,14 @@ public Vp8Decoder(Vp8FrameHeader frameHeader, Vp8PictureHeader pictureHeader, Vp int extraY = extraRows * this.CacheYStride; int extraUv = extraRows / 2 * this.CacheUvStride; this.YuvBuffer = memoryAllocator.Allocate((WebpConstants.Bps * 17) + (WebpConstants.Bps * 9) + extraY); - this.CacheY = memoryAllocator.Allocate((16 * this.CacheYStride) + extraY); + this.CacheY = memoryAllocator.Allocate((16 * this.CacheYStride) + extraY, AllocationOptions.Clean); int cacheUvSize = (16 * this.CacheUvStride) + extraUv; this.CacheU = memoryAllocator.Allocate(cacheUvSize); this.CacheV = memoryAllocator.Allocate(cacheUvSize); this.TmpYBuffer = memoryAllocator.Allocate((int)width); this.TmpUBuffer = memoryAllocator.Allocate((int)width); this.TmpVBuffer = memoryAllocator.Allocate((int)width); - this.Pixels = memoryAllocator.Allocate((int)(width * height * 4)); + this.Pixels = memoryAllocator.Allocate((int)(width * height * 4), AllocationOptions.Clean); #if DEBUG // Filling those buffers with 205, is only useful for debugging, diff --git a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs index b74337ef37..173d9436dd 100644 --- a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs @@ -81,16 +81,29 @@ public WebpAnimationDecoder(MemoryAllocator memoryAllocator, Configuration confi /// The width of the image. /// The height of the image. /// The size of the image data in bytes. - public Image Decode(BufferedReadStream stream, WebpFeatures features, uint width, uint height, uint completeDataSize) + public Image Decode( + BufferedReadStream stream, + WebpFeatures features, + uint width, + uint height, + uint completeDataSize) where TPixel : unmanaged, IPixel { Image? image = null; ImageFrame? previousFrame = null; + WebpFrameData? prevFrameData = null; this.metadata = new ImageMetadata(); this.webpMetadata = this.metadata.GetWebpMetadata(); this.webpMetadata.RepeatCount = features.AnimationLoopCount; + Color backgroundColor = this.backgroundColorHandling == BackgroundColorHandling.Ignore + ? Color.Transparent + : features.AnimationBackgroundColor!.Value; + + this.webpMetadata.BackgroundColor = backgroundColor; + TPixel backgroundPixel = backgroundColor.ToPixel(); + Span buffer = stackalloc byte[4]; uint frameCount = 0; int remainingBytes = (int)completeDataSize; @@ -101,10 +114,16 @@ public Image Decode(BufferedReadStream stream, WebpFeatures feat switch (chunkType) { case WebpChunkType.FrameData: - Color backgroundColor = this.backgroundColorHandling == BackgroundColorHandling.Ignore - ? Color.FromPixel(new Bgra32(0, 0, 0, 0)) - : features.AnimationBackgroundColor!.Value; - uint dataSize = this.ReadFrame(stream, ref image, ref previousFrame, width, height, backgroundColor); + + uint dataSize = this.ReadFrame( + stream, + ref image, + ref previousFrame, + ref prevFrameData, + width, + height, + backgroundPixel); + remainingBytes -= (int)dataSize; break; case WebpChunkType.Xmp: @@ -132,10 +151,18 @@ public Image Decode(BufferedReadStream stream, WebpFeatures feat /// The stream, where the image should be decoded from. Cannot be null. /// The image to decode the information to. /// The previous frame. + /// The previous frame data. /// The width of the image. /// The height of the image. /// The default background color of the canvas in. - private uint ReadFrame(BufferedReadStream stream, ref Image? image, ref ImageFrame? previousFrame, uint width, uint height, Color backgroundColor) + private uint ReadFrame( + BufferedReadStream stream, + ref Image? image, + ref ImageFrame? previousFrame, + ref WebpFrameData? prevFrameData, + uint width, + uint height, + TPixel backgroundColor) where TPixel : unmanaged, IPixel { WebpFrameData frameData = WebpFrameData.Parse(stream); @@ -174,40 +201,51 @@ private uint ReadFrame(BufferedReadStream stream, ref Image? ima break; } - ImageFrame? currentFrame = null; - ImageFrame imageFrame; + ImageFrame currentFrame; if (previousFrame is null) { - image = new Image(this.configuration, (int)width, (int)height, backgroundColor.ToPixel(), this.metadata); - - SetFrameMetadata(image.Frames.RootFrame.Metadata, frameData); + image = new Image(this.configuration, (int)width, (int)height, backgroundColor, this.metadata); - imageFrame = image.Frames.RootFrame; + currentFrame = image.Frames.RootFrame; + SetFrameMetadata(currentFrame.Metadata, frameData); } else { - currentFrame = image!.Frames.AddFrame(previousFrame); // This clones the frame and adds it the collection. + // If the frame is a key frame we do not need to clone the frame or clear it. + bool isKeyFrame = prevFrameData?.DisposalMethod is FrameDisposalMode.RestoreToBackground + && this.restoreArea == image!.Bounds; - SetFrameMetadata(currentFrame.Metadata, frameData); + if (isKeyFrame) + { + currentFrame = image!.Frames.CreateFrame(backgroundColor); + } + else + { + // This clones the frame and adds it the collection. + currentFrame = image!.Frames.AddFrame(previousFrame); + if (prevFrameData?.DisposalMethod is FrameDisposalMode.RestoreToBackground) + { + this.RestoreToBackground(currentFrame, backgroundColor); + } + } - imageFrame = currentFrame; + SetFrameMetadata(currentFrame.Metadata, frameData); } - Rectangle regionRectangle = frameData.Bounds; + Rectangle interest = frameData.Bounds; + bool blend = previousFrame != null && frameData.BlendingMethod == FrameBlendMode.Over; + using Buffer2D pixelData = this.DecodeImageFrameData(frameData, webpInfo); + DrawDecodedImageFrameOnCanvas(pixelData, currentFrame, interest, blend); + + webpInfo?.Dispose(); + previousFrame = currentFrame; + prevFrameData = frameData; if (frameData.DisposalMethod is FrameDisposalMode.RestoreToBackground) { - this.RestoreToBackground(imageFrame, backgroundColor); + this.restoreArea = interest; } - using Buffer2D decodedImageFrame = this.DecodeImageFrameData(frameData, webpInfo); - - bool blend = previousFrame != null && frameData.BlendingMethod == FrameBlendMode.Over; - DrawDecodedImageFrameOnCanvas(decodedImageFrame, imageFrame, regionRectangle, blend); - - previousFrame = currentFrame ?? image.Frames.RootFrame; - this.restoreArea = regionRectangle; - return (uint)(stream.Position - streamStartPosition); } @@ -257,31 +295,26 @@ private Buffer2D DecodeImageFrameData(WebpFrameData frameData, W try { - Buffer2D pixelBufferDecoded = decodedFrame.PixelBuffer; + Buffer2D decodeBuffer = decodedFrame.PixelBuffer; if (webpInfo.IsLossless) { - WebpLosslessDecoder losslessDecoder = - new(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration); - losslessDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height); + WebpLosslessDecoder losslessDecoder = new(webpInfo.Vp8LBitReader, this.memoryAllocator, this.configuration); + losslessDecoder.Decode(decodeBuffer, (int)webpInfo.Width, (int)webpInfo.Height); } else { WebpLossyDecoder lossyDecoder = new(webpInfo.Vp8BitReader, this.memoryAllocator, this.configuration); - lossyDecoder.Decode(pixelBufferDecoded, (int)webpInfo.Width, (int)webpInfo.Height, webpInfo, this.alphaData); + lossyDecoder.Decode(decodeBuffer, (int)webpInfo.Width, (int)webpInfo.Height, webpInfo, this.alphaData); } - return pixelBufferDecoded; + return decodeBuffer; } catch { decodedFrame?.Dispose(); throw; } - finally - { - webpInfo.Dispose(); - } } /// @@ -335,7 +368,7 @@ private static void DrawDecodedImageFrameOnCanvas( /// The pixel format. /// The image frame. /// Color of the background. - private void RestoreToBackground(ImageFrame imageFrame, Color backgroundColor) + private void RestoreToBackground(ImageFrame imageFrame, TPixel backgroundColor) where TPixel : unmanaged, IPixel { if (!this.restoreArea.HasValue) @@ -345,8 +378,9 @@ private void RestoreToBackground(ImageFrame imageFrame, Color ba Rectangle interest = Rectangle.Intersect(imageFrame.Bounds, this.restoreArea.Value); Buffer2DRegion pixelRegion = imageFrame.PixelBuffer.GetRegion(interest); - TPixel backgroundPixel = backgroundColor.ToPixel(); - pixelRegion.Fill(backgroundPixel); + pixelRegion.Fill(backgroundColor); + + this.restoreArea = null; } /// diff --git a/src/ImageSharp/Formats/Webp/WebpCommonUtils.cs b/src/ImageSharp/Formats/Webp/WebpCommonUtils.cs index a1e9821c09..1ca409f9a4 100644 --- a/src/ImageSharp/Formats/Webp/WebpCommonUtils.cs +++ b/src/ImageSharp/Formats/Webp/WebpCommonUtils.cs @@ -18,7 +18,7 @@ internal static class WebpCommonUtils /// /// The row to check. /// Returns true if alpha has non-0xff values. - public static unsafe bool CheckNonOpaque(Span row) + public static unsafe bool CheckNonOpaque(ReadOnlySpan row) { if (Avx2.IsSupported) { diff --git a/src/ImageSharp/IDeepCloneable.cs b/src/ImageSharp/IDeepCloneable.cs index 3d00d627e0..8b225da0c7 100644 --- a/src/ImageSharp/IDeepCloneable.cs +++ b/src/ImageSharp/IDeepCloneable.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. namespace SixLabors.ImageSharp; @@ -14,7 +14,7 @@ public interface IDeepCloneable /// Creates a new that is a deep copy of the current instance. /// /// The . - T DeepClone(); + public T DeepClone(); } /// @@ -26,5 +26,5 @@ public interface IDeepCloneable /// Creates a new object that is a deep copy of the current instance. /// /// The . - IDeepCloneable DeepClone(); + public IDeepCloneable DeepClone(); } diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 0d36340bf8..71dd4c8fb2 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -51,6 +51,11 @@ + + True + True + InlineArray.tt + True True @@ -154,6 +159,10 @@ + + TextTemplatingFileGenerator + InlineArray.cs + ImageMetadataExtensions.cs TextTemplatingFileGenerator diff --git a/src/ImageSharp/IndexedImageFrame{TPixel}.cs b/src/ImageSharp/IndexedImageFrame{TPixel}.cs index 6807e77ad2..a88cdb524e 100644 --- a/src/ImageSharp/IndexedImageFrame{TPixel}.cs +++ b/src/ImageSharp/IndexedImageFrame{TPixel}.cs @@ -25,12 +25,12 @@ public sealed class IndexedImageFrame : IPixelSource, IDisposable /// Initializes a new instance of the class. /// /// - /// The configuration which allows altering default behaviour or extending the library. + /// The configuration which allows altering default behavior or extending the library. /// /// The frame width. /// The frame height. /// The color palette. - internal IndexedImageFrame(Configuration configuration, int width, int height, ReadOnlyMemory palette) + public IndexedImageFrame(Configuration configuration, int width, int height, ReadOnlyMemory palette) { Guard.NotNull(configuration, nameof(configuration)); Guard.MustBeLessThanOrEqualTo(palette.Length, QuantizerConstants.MaxColors, nameof(palette)); @@ -42,14 +42,14 @@ internal IndexedImageFrame(Configuration configuration, int width, int height, R this.Height = height; this.pixelBuffer = configuration.MemoryAllocator.Allocate2D(width, height); - // Copy the palette over. We want the lifetime of this frame to be independant of any palette source. + // Copy the palette over. We want the lifetime of this frame to be independent of any palette source. this.paletteOwner = configuration.MemoryAllocator.Allocate(palette.Length); palette.Span.CopyTo(this.paletteOwner.GetSpan()); this.Palette = this.paletteOwner.Memory[..palette.Length]; } /// - /// Gets the configuration which allows altering default behaviour or extending the library. + /// Gets the configuration which allows altering default behavior or extending the library. /// public Configuration Configuration { get; } diff --git a/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs index abe32e3882..bc34f759a0 100644 --- a/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/CloningImageProcessor{TPixel}.cs @@ -132,16 +132,14 @@ protected virtual void BeforeFrameApply(ImageFrame source, ImageFrameThe source image. Cannot be null. /// The cloned/destination image. Cannot be null. protected virtual void AfterFrameApply(ImageFrame source, ImageFrame destination) - { - } + => destination.Metadata.AfterFrameApply(source, destination); /// /// This method is called after the process is applied to prepare the processor. /// /// The cloned/destination image. Cannot be null. protected virtual void AfterImageApply(Image destination) - { - } + => destination.Metadata.AfterImageApply(destination); /// /// Disposes the object and frees resources for the Garbage Collector. diff --git a/src/ImageSharp/Processing/Processors/Dithering/ErroDither.KnownTypes.cs b/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.KnownTypes.cs similarity index 100% rename from src/ImageSharp/Processing/Processors/Dithering/ErroDither.KnownTypes.cs rename to src/ImageSharp/Processing/Processors/Dithering/ErrorDither.KnownTypes.cs diff --git a/src/ImageSharp/Processing/Processors/Dithering/IDither.cs b/src/ImageSharp/Processing/Processors/Dithering/IDither.cs index ac2921b98d..3217601270 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/IDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/IDither.cs @@ -21,7 +21,7 @@ public interface IDither /// The source image. /// The destination quantized frame. /// The region of interest bounds. - void ApplyQuantizationDither( + public void ApplyQuantizationDither( ref TFrameQuantizer quantizer, ImageFrame source, IndexedImageFrame destination, @@ -38,7 +38,7 @@ void ApplyQuantizationDither( /// The palette dithering processor. /// The source image. /// The region of interest bounds. - void ApplyPaletteDither( + public void ApplyPaletteDither( in TPaletteDitherImageProcessor processor, ImageFrame source, Rectangle bounds) diff --git a/src/ImageSharp/Processing/Processors/Dithering/IPaletteDitherImageProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Dithering/IPaletteDitherImageProcessor{TPixel}.cs index e406d82c69..347e2f0ef6 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/IPaletteDitherImageProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/IPaletteDitherImageProcessor{TPixel}.cs @@ -15,22 +15,22 @@ public interface IPaletteDitherImageProcessor /// /// Gets the configuration instance to use when performing operations. /// - Configuration Configuration { get; } + public Configuration Configuration { get; } /// /// Gets the dithering palette. /// - ReadOnlyMemory Palette { get; } + public ReadOnlyMemory Palette { get; } /// /// Gets the dithering scale used to adjust the amount of dither. Range 0..1. /// - float DitherScale { get; } + public float DitherScale { get; } /// /// Returns the color from the dithering palette corresponding to the given color. /// /// The color to match. /// The match. - TPixel GetPaletteColor(TPixel color); + public TPixel GetPaletteColor(TPixel color); } diff --git a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs index 7e672393c7..0d4680e21f 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs @@ -80,7 +80,7 @@ protected override void Dispose(bool disposing) Justification = "https://github.com/dotnet/roslyn-analyzers/issues/6151")] internal readonly struct DitherProcessor : IPaletteDitherImageProcessor, IDisposable { - private readonly EuclideanPixelMap pixelMap; + private readonly PixelMap pixelMap; [MethodImpl(InliningOptions.ShortMethod)] public DitherProcessor( @@ -89,7 +89,7 @@ public DitherProcessor( float ditherScale) { this.Configuration = configuration; - this.pixelMap = new EuclideanPixelMap(configuration, palette); + this.pixelMap = PixelMapFactory.Create(configuration, palette, ColorMatchingMode.Coarse); this.Palette = palette; this.DitherScale = ditherScale; } diff --git a/src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs index 2fa79220e5..e1f7d1fffb 100644 --- a/src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/ImageProcessor{TPixel}.cs @@ -95,7 +95,7 @@ protected virtual void BeforeFrameApply(ImageFrame source) protected abstract void OnFrameApply(ImageFrame source); /// - /// This method is called after the process is applied to prepare the processor. + /// This method is called after the process is applied to each frame. /// /// The source image. Cannot be null. protected virtual void AfterFrameApply(ImageFrame source) @@ -103,11 +103,10 @@ protected virtual void AfterFrameApply(ImageFrame source) } /// - /// This method is called after the process is applied to prepare the processor. + /// This method is called after the process is applied to the complete image. /// protected virtual void AfterImageApply() - { - } + => this.Source.Metadata.AfterImageApply(this.Source); /// /// Disposes the object and frees resources for the Garbage Collector. diff --git a/src/ImageSharp/Processing/Processors/Quantization/ColorMatchingMode.cs b/src/ImageSharp/Processing/Processors/Quantization/ColorMatchingMode.cs new file mode 100644 index 0000000000..26fd7d5d76 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Quantization/ColorMatchingMode.cs @@ -0,0 +1,28 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +namespace SixLabors.ImageSharp.Processing.Processors.Quantization; + +/// +/// Defines the precision level used when matching colors during quantization. +/// +public enum ColorMatchingMode +{ + /// + /// Uses a coarse caching strategy optimized for performance at the expense of exact matches. + /// This provides the fastest matching but may yield approximate results. + /// + Coarse, + + /// + /// Enables an exact color match cache for the first 512 unique colors encountered, + /// falling back to coarse matching thereafter. + /// + Hybrid, + + /// + /// Performs exact color matching without any caching optimizations. + /// This is the slowest but most accurate matching strategy. + /// + Exact +} diff --git a/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel,TCache}.cs b/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel,TCache}.cs new file mode 100644 index 0000000000..5b0c7252cb --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel,TCache}.cs @@ -0,0 +1,184 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors.Quantization; + +/// +/// Gets the closest color to the supplied color based upon the Euclidean distance. +/// +/// The pixel format. +/// The cache type. +/// +/// This class is not thread safe and should not be accessed in parallel. +/// Doing so will result in non-idempotent results. +/// +internal sealed class EuclideanPixelMap : PixelMap + where TPixel : unmanaged, IPixel + where TCache : struct, IColorIndexCache +{ + private Rgba32[] rgbaPalette; + + // Do not make readonly. It's a mutable struct. +#pragma warning disable IDE0044 // Add readonly modifier + private TCache cache; +#pragma warning restore IDE0044 // Add readonly modifier + + private readonly Configuration configuration; + + /// + /// Initializes a new instance of the class. + /// + /// Specifies the settings and resources for the pixel map's operations. + /// Defines the color palette used for pixel mapping. + public EuclideanPixelMap(Configuration configuration, ReadOnlyMemory palette) + { + this.configuration = configuration; + this.Palette = palette; + this.rgbaPalette = new Rgba32[palette.Length]; + this.cache = TCache.Create(configuration.MemoryAllocator); + PixelOperations.Instance.ToRgba32(configuration, this.Palette.Span, this.rgbaPalette); + } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public override int GetClosestColor(TPixel color, out TPixel match) + { + ref TPixel paletteRef = ref MemoryMarshal.GetReference(this.Palette.Span); + Rgba32 rgba = color.ToRgba32(); + + if (this.cache.TryGetValue(rgba, out short index)) + { + match = Unsafe.Add(ref paletteRef, (ushort)index); + return index; + } + + return this.GetClosestColorSlow(rgba, ref paletteRef, out match); + } + + /// + public override void Clear(ReadOnlyMemory palette) + { + this.Palette = palette; + this.rgbaPalette = new Rgba32[palette.Length]; + PixelOperations.Instance.ToRgba32(this.configuration, this.Palette.Span, this.rgbaPalette); + this.cache.Clear(); + } + + [MethodImpl(InliningOptions.ColdPath)] + private int GetClosestColorSlow(Rgba32 rgba, ref TPixel paletteRef, out TPixel match) + { + // Loop through the palette and find the nearest match. + int index = 0; + float leastDistance = float.MaxValue; + for (int i = 0; i < this.rgbaPalette.Length; i++) + { + Rgba32 candidate = this.rgbaPalette[i]; + if (candidate.PackedValue == rgba.PackedValue) + { + index = i; + break; + } + + float distance = DistanceSquared(rgba, candidate); + if (distance == 0) + { + index = i; + break; + } + + if (distance < leastDistance) + { + index = i; + leastDistance = distance; + } + } + + // Now I have the index, pop it into the cache for next time + _ = this.cache.TryAdd(rgba, (short)index); + match = Unsafe.Add(ref paletteRef, (uint)index); + + return index; + } + + /// + /// Returns the Euclidean distance squared between two specified points. + /// + /// The first point. + /// The second point. + /// The distance squared. + [MethodImpl(InliningOptions.ShortMethod)] + private static float DistanceSquared(Rgba32 a, Rgba32 b) + { + float deltaR = a.R - b.R; + float deltaG = a.G - b.G; + float deltaB = a.B - b.B; + float deltaA = a.A - b.A; + return (deltaR * deltaR) + (deltaG * deltaG) + (deltaB * deltaB) + (deltaA * deltaA); + } + + /// + public override void Dispose() => this.cache.Dispose(); +} + +/// +/// Represents a map of colors to indices. +/// +/// The pixel format. +internal abstract class PixelMap : IDisposable + where TPixel : unmanaged, IPixel +{ + /// + /// Gets the color palette of this . + /// + public ReadOnlyMemory Palette { get; private protected set; } + + /// + /// Returns the closest color in the palette and the index of that pixel. + /// + /// The color to match. + /// The matched color. + /// + /// The index. + /// + public abstract int GetClosestColor(TPixel color, out TPixel match); + + /// + /// Clears the map, resetting it to use the given palette. + /// + /// The color palette to map from. + public abstract void Clear(ReadOnlyMemory palette); + + /// + public abstract void Dispose(); +} + +/// +/// A factory for creating instances. +/// +internal static class PixelMapFactory +{ + /// + /// Creates a new instance. + /// + /// The pixel format. + /// The configuration. + /// The color palette to map from. + /// The color matching mode. + /// + /// The . + /// + public static PixelMap Create( + Configuration configuration, + ReadOnlyMemory palette, + ColorMatchingMode colorMatchingMode) + where TPixel : unmanaged, IPixel => colorMatchingMode switch + { + ColorMatchingMode.Hybrid => new EuclideanPixelMap(configuration, palette), + ColorMatchingMode.Exact => new EuclideanPixelMap(configuration, palette), + _ => new EuclideanPixelMap(configuration, palette), + }; +} diff --git a/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel}.cs deleted file mode 100644 index 4fd37d479d..0000000000 --- a/src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel}.cs +++ /dev/null @@ -1,258 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -using System.Buffers; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using SixLabors.ImageSharp.Memory; -using SixLabors.ImageSharp.PixelFormats; - -namespace SixLabors.ImageSharp.Processing.Processors.Quantization; - -/// -/// Gets the closest color to the supplied color based upon the Euclidean distance. -/// -/// The pixel format. -/// -/// This class is not thread safe and should not be accessed in parallel. -/// Doing so will result in non-idempotent results. -/// -internal sealed class EuclideanPixelMap : IDisposable - where TPixel : unmanaged, IPixel -{ - private Rgba32[] rgbaPalette; - private int transparentIndex; - private readonly TPixel transparentMatch; - - /// - /// Do not make this readonly! Struct value would be always copied on non-readonly method calls. - /// - private ColorDistanceCache cache; - private readonly Configuration configuration; - - /// - /// Initializes a new instance of the class. - /// - /// The configuration. - /// The color palette to map from. - public EuclideanPixelMap(Configuration configuration, ReadOnlyMemory palette) - : this(configuration, palette, -1) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The configuration. - /// The color palette to map from. - /// An explicit index at which to match transparent pixels. - public EuclideanPixelMap(Configuration configuration, ReadOnlyMemory palette, int transparentIndex = -1) - { - this.configuration = configuration; - this.Palette = palette; - this.rgbaPalette = new Rgba32[palette.Length]; - this.cache = new ColorDistanceCache(configuration.MemoryAllocator); - PixelOperations.Instance.ToRgba32(configuration, this.Palette.Span, this.rgbaPalette); - - this.transparentIndex = transparentIndex; - this.transparentMatch = TPixel.FromRgba32(default); - } - - /// - /// Gets the color palette of this . - /// The palette memory is owned by the palette source that created it. - /// - public ReadOnlyMemory Palette { get; private set; } - - /// - /// Returns the closest color in the palette and the index of that pixel. - /// The palette contents must match the one used in the constructor. - /// - /// The color to match. - /// The matched color. - /// The index. - [MethodImpl(InliningOptions.ShortMethod)] - public int GetClosestColor(TPixel color, out TPixel match) - { - ref TPixel paletteRef = ref MemoryMarshal.GetReference(this.Palette.Span); - Rgba32 rgba = color.ToRgba32(); - - // Check if the color is in the lookup table - if (!this.cache.TryGetValue(rgba, out short index)) - { - return this.GetClosestColorSlow(rgba, ref paletteRef, out match); - } - - match = Unsafe.Add(ref paletteRef, (ushort)index); - return index; - } - - /// - /// Clears the map, resetting it to use the given palette. - /// - /// The color palette to map from. - public void Clear(ReadOnlyMemory palette) - { - this.Palette = palette; - this.rgbaPalette = new Rgba32[palette.Length]; - PixelOperations.Instance.ToRgba32(this.configuration, this.Palette.Span, this.rgbaPalette); - this.transparentIndex = -1; - this.cache.Clear(); - } - - /// - /// Allows setting the transparent index after construction. - /// - /// An explicit index at which to match transparent pixels. - public void SetTransparentIndex(int index) - { - if (index != this.transparentIndex) - { - this.cache.Clear(); - } - - this.transparentIndex = index; - } - - [MethodImpl(InliningOptions.ShortMethod)] - private int GetClosestColorSlow(Rgba32 rgba, ref TPixel paletteRef, out TPixel match) - { - // Loop through the palette and find the nearest match. - int index = 0; - - if (this.transparentIndex >= 0 && rgba == default) - { - // We have explicit instructions. No need to search. - index = this.transparentIndex; - this.cache.Add(rgba, (byte)index); - match = this.transparentMatch; - return index; - } - - float leastDistance = float.MaxValue; - for (int i = 0; i < this.rgbaPalette.Length; i++) - { - Rgba32 candidate = this.rgbaPalette[i]; - float distance = DistanceSquared(rgba, candidate); - - // If it's an exact match, exit the loop - if (distance == 0) - { - index = i; - break; - } - - if (distance < leastDistance) - { - // Less than... assign. - index = i; - leastDistance = distance; - } - } - - // Now I have the index, pop it into the cache for next time - this.cache.Add(rgba, (byte)index); - match = Unsafe.Add(ref paletteRef, (uint)index); - return index; - } - - /// - /// Returns the Euclidean distance squared between two specified points. - /// - /// The first point. - /// The second point. - /// The distance squared. - [MethodImpl(InliningOptions.ShortMethod)] - private static float DistanceSquared(Rgba32 a, Rgba32 b) - { - float deltaR = a.R - b.R; - float deltaG = a.G - b.G; - float deltaB = a.B - b.B; - float deltaA = a.A - b.A; - return (deltaR * deltaR) + (deltaG * deltaG) + (deltaB * deltaB) + (deltaA * deltaA); - } - - public void Dispose() => this.cache.Dispose(); - - /// - /// A cache for storing color distance matching results. - /// - /// - /// - /// The granularity of the cache has been determined based upon the current - /// suite of test images and provides the lowest possible memory usage while - /// providing enough match accuracy. - /// Entry count is currently limited to 2335905 entries (4MB). - /// - /// - private unsafe struct ColorDistanceCache : IDisposable - { - private const int IndexRBits = 5; - private const int IndexGBits = 5; - private const int IndexBBits = 5; - private const int IndexABits = 6; - private const int IndexRCount = (1 << IndexRBits) + 1; - private const int IndexGCount = (1 << IndexGBits) + 1; - private const int IndexBCount = (1 << IndexBBits) + 1; - private const int IndexACount = (1 << IndexABits) + 1; - private const int RShift = 8 - IndexRBits; - private const int GShift = 8 - IndexGBits; - private const int BShift = 8 - IndexBBits; - private const int AShift = 8 - IndexABits; - private const int Entries = IndexRCount * IndexGCount * IndexBCount * IndexACount; - private MemoryHandle tableHandle; - private readonly IMemoryOwner table; - private readonly short* tablePointer; - - public ColorDistanceCache(MemoryAllocator allocator) - { - this.table = allocator.Allocate(Entries); - this.table.GetSpan().Fill(-1); - this.tableHandle = this.table.Memory.Pin(); - this.tablePointer = (short*)this.tableHandle.Pointer; - } - - [MethodImpl(InliningOptions.ShortMethod)] - public readonly void Add(Rgba32 rgba, byte index) - { - int idx = GetPaletteIndex(rgba); - this.tablePointer[idx] = index; - } - - [MethodImpl(InliningOptions.ShortMethod)] - public readonly bool TryGetValue(Rgba32 rgba, out short match) - { - int idx = GetPaletteIndex(rgba); - match = this.tablePointer[idx]; - return match > -1; - } - - /// - /// Clears the cache resetting each entry to empty. - /// - [MethodImpl(InliningOptions.ShortMethod)] - public readonly void Clear() => this.table.GetSpan().Fill(-1); - - [MethodImpl(InliningOptions.ShortMethod)] - private static int GetPaletteIndex(Rgba32 rgba) - { - int rIndex = rgba.R >> RShift; - int gIndex = rgba.G >> GShift; - int bIndex = rgba.B >> BShift; - int aIndex = rgba.A >> AShift; - - return (aIndex * (IndexRCount * IndexGCount * IndexBCount)) + - (rIndex * (IndexGCount * IndexBCount)) + - (gIndex * IndexBCount) + bIndex; - } - - public void Dispose() - { - if (this.table != null) - { - this.tableHandle.Dispose(); - this.table.Dispose(); - } - } - } -} diff --git a/src/ImageSharp/Processing/Processors/Quantization/IColorIndexCache.cs b/src/ImageSharp/Processing/Processors/Quantization/IColorIndexCache.cs new file mode 100644 index 0000000000..32d95137bc --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Quantization/IColorIndexCache.cs @@ -0,0 +1,569 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Buffers; +using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors.Quantization; + +/// +/// Represents a cache used for efficiently retrieving palette indices for colors. +/// +internal interface IColorIndexCache : IDisposable +{ + /// + /// Adds a color to the cache. + /// + /// The color to add. + /// The index of the color in the palette. + /// + /// if the color was added; otherwise, . + /// + public bool TryAdd(Rgba32 color, short value); + + /// + /// Gets the index of the color in the palette. + /// + /// The color to get the index for. + /// The index of the color in the palette. + /// + /// if the color is in the palette; otherwise, . + /// + public bool TryGetValue(Rgba32 color, out short value); + + /// + /// Clears the cache. + /// + public void Clear(); +} + +/// +/// Represents a cache used for efficiently retrieving palette indices for colors. +/// +/// The type of the cache. +internal interface IColorIndexCache : IColorIndexCache + where T : struct, IColorIndexCache +{ + /// + /// Creates a new instance of the cache. + /// + /// The memory allocator to use. + /// + /// The new instance of the cache. + /// + public static abstract T Create(MemoryAllocator allocator); +} + +/// +/// A hybrid color distance cache that combines a small, fixed-capacity exact-match dictionary +/// (ExactCache, ~4–5 KB for up to 512 entries) with a coarse lookup table (CoarseCache) for 5,5,5,6 precision. +/// +/// +/// ExactCache provides O(1) lookup for common cases using a simple 256-entry hash-based dictionary, while CoarseCache +/// quantizes RGB channels to 5 bits (yielding 32^3 buckets) and alpha to 6 bits, storing up to 4 alpha entries per bucket +/// (a design chosen based on probability theory to capture most real-world variations) for a total memory footprint of +/// roughly 576 KB. Lookups and insertions are performed in constant time, making the overall design both fast and memory-predictable. +/// +internal unsafe struct HybridCache : IColorIndexCache +{ + private CoarseCache coarseCache; + private AccurateCache accurateCache; + + public HybridCache(MemoryAllocator allocator) + { + this.accurateCache = AccurateCache.Create(allocator); + this.coarseCache = CoarseCache.Create(allocator); + } + + /// + public static HybridCache Create(MemoryAllocator allocator) => new(allocator); + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public bool TryAdd(Rgba32 color, short index) + { + if (this.accurateCache.TryAdd(color, index)) + { + return true; + } + + return this.coarseCache.TryAdd(color, index); + } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public readonly bool TryGetValue(Rgba32 color, out short value) + { + if (this.accurateCache.TryGetValue(color, out value)) + { + return true; + } + + return this.coarseCache.TryGetValue(color, out value); + } + + /// + public readonly void Clear() + { + this.accurateCache.Clear(); + this.coarseCache.Clear(); + } + + /// + public void Dispose() + { + this.accurateCache.Dispose(); + this.coarseCache.Dispose(); + } +} + +/// +/// A coarse cache for color distance lookups that uses a fixed-size lookup table. +/// +/// +/// This cache uses a fixed lookup table with 2,097,152 bins, each storing a 2-byte value, +/// resulting in a memory usage of approximately 4 MB. Lookups and insertions are +/// performed in constant time (O(1)) via direct table indexing. This design is optimized for +/// speed while maintaining a predictable, fixed memory footprint. +/// +internal unsafe struct CoarseCache : IColorIndexCache +{ + private const int IndexRBits = 5; + private const int IndexGBits = 5; + private const int IndexBBits = 5; + private const int IndexABits = 6; + private const int IndexRCount = 1 << IndexRBits; // 32 bins for red + private const int IndexGCount = 1 << IndexGBits; // 32 bins for green + private const int IndexBCount = 1 << IndexBBits; // 32 bins for blue + private const int IndexACount = 1 << IndexABits; // 64 bins for alpha + private const int TotalBins = IndexRCount * IndexGCount * IndexBCount * IndexACount; // 2,097,152 bins + + private readonly IMemoryOwner binsOwner; + private readonly short* binsPointer; + private MemoryHandle binsHandle; + + private CoarseCache(MemoryAllocator allocator) + { + this.binsOwner = allocator.Allocate(TotalBins); + this.binsOwner.GetSpan().Fill(-1); + this.binsHandle = this.binsOwner.Memory.Pin(); + this.binsPointer = (short*)this.binsHandle.Pointer; + } + + /// + public static CoarseCache Create(MemoryAllocator allocator) => new(allocator); + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public readonly bool TryAdd(Rgba32 color, short value) + { + this.binsPointer[GetCoarseIndex(color)] = value; + return true; + } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public readonly bool TryGetValue(Rgba32 color, out short value) + { + value = this.binsPointer[GetCoarseIndex(color)]; + return value > -1; // Coarse match found + } + + [MethodImpl(InliningOptions.ShortMethod)] + private static int GetCoarseIndex(Rgba32 color) + { + int rIndex = color.R >> (8 - IndexRBits); + int gIndex = color.G >> (8 - IndexGBits); + int bIndex = color.B >> (8 - IndexBBits); + int aIndex = color.A >> (8 - IndexABits); + + return (aIndex * IndexRCount * IndexGCount * IndexBCount) + + (rIndex * IndexGCount * IndexBCount) + + (gIndex * IndexBCount) + + bIndex; + } + + /// + public readonly void Clear() + => this.binsOwner.GetSpan().Fill(-1); + + /// + public void Dispose() + { + this.binsHandle.Dispose(); + this.binsOwner.Dispose(); + } +} + +/// +/// +/// CoarseCache is a fast, low-memory lookup structure for caching palette indices associated with RGBA values, +/// using a quantized representation of 5,5,5,6 (RGB: 5 bits each, Alpha: 6 bits). +/// +/// +/// The cache quantizes the RGB channels to 5 bits each, resulting in 32 levels per channel and a total of 32³ = 32,768 buckets. +/// Each bucket is represented by an , which holds a small, inline array of alpha entries. +/// Each alpha entry stores the alpha value quantized to 6 bits (0–63) along with a palette index (a 16-bit value). +/// +/// +/// Performance Characteristics: +/// - Lookup: O(1) for computing the bucket index from the RGB channels, plus a small constant time (up to 8 iterations) +/// to search through the alpha entries in the bucket. +/// - Insertion: O(1) for bucket index computation and a quick linear search over a very small (fixed) number of entries. +/// +/// +/// Memory Characteristics: +/// - The cache consists of 32,768 buckets. +/// - Each is implemented using an inline array with a capacity of 8 entries. +/// - Each bucket occupies approximately 1 byte (Count) + (8 entries × 3 bytes each) ≈ 25 bytes. +/// - Overall, the buckets occupy roughly 32,768 × 25 bytes = 819,200 bytes (≈ 800 KB). +/// +/// +/// This design provides nearly constant-time lookup and insertion with minimal memory usage, +/// making it ideal for applications such as color distance caching in images with a limited palette (up to 256 entries). +/// +/// +internal unsafe struct CoarseCacheLite : IColorIndexCache +{ + // Use 5 bits per channel for R, G, and B: 32 levels each. + // Total buckets = 32^3 = 32768. + private const int RgbBits = 5; + private const int RgbShift = 8 - RgbBits; // 3 + private const int BucketCount = 1 << (RgbBits * 3); // 32768 + private readonly IMemoryOwner bucketsOwner; + private readonly AlphaBucket* buckets; + private MemoryHandle bucketHandle; + + private CoarseCacheLite(MemoryAllocator allocator) + { + this.bucketsOwner = allocator.Allocate(BucketCount, AllocationOptions.Clean); + this.bucketHandle = this.bucketsOwner.Memory.Pin(); + this.buckets = (AlphaBucket*)this.bucketHandle.Pointer; + } + + /// + public static CoarseCacheLite Create(MemoryAllocator allocator) => new(allocator); + + /// + public readonly bool TryAdd(Rgba32 color, short paletteIndex) + { + int bucketIndex = GetBucketIndex(color.R, color.G, color.B); + byte quantAlpha = QuantizeAlpha(color.A); + this.buckets[bucketIndex].Add(quantAlpha, paletteIndex); + return true; + } + + /// + public readonly bool TryGetValue(Rgba32 color, out short paletteIndex) + { + int bucketIndex = GetBucketIndex(color.R, color.G, color.B); + byte quantAlpha = QuantizeAlpha(color.A); + return this.buckets[bucketIndex].TryGetValue(quantAlpha, out paletteIndex); + } + + /// + public readonly void Clear() + { + Span bucketsSpan = this.bucketsOwner.GetSpan(); + bucketsSpan.Clear(); + } + + /// + public void Dispose() + { + this.bucketHandle.Dispose(); + this.bucketsOwner.Dispose(); + } + + [MethodImpl(InliningOptions.ShortMethod)] + private static int GetBucketIndex(byte r, byte g, byte b) + { + int qr = r >> RgbShift; + int qg = g >> RgbShift; + int qb = b >> RgbShift; + + // Combine the quantized channels into a single index. + return (qr << (RgbBits << 1)) | (qg << RgbBits) | qb; + } + + [MethodImpl(InliningOptions.ShortMethod)] + private static byte QuantizeAlpha(byte a) + + // Quantize to 6 bits: shift right by (8 - 6) = 2 bits. + => (byte)(a >> 2); + + public struct AlphaEntry + { + // Store the alpha value quantized to 6 bits (0..63) + public byte QuantizedAlpha; + public short PaletteIndex; + } + + public struct AlphaBucket + { + // Fixed capacity for alpha entries in this bucket. + // We choose a capacity of 8 for several reasons: + // + // 1. The alpha channel is quantized to 6 bits, so there are 64 possible distinct values. + // In the worst-case, a given RGB bucket might encounter up to 64 different alpha values. + // + // 2. However, in practice (based on probability theory and typical image data), + // the number of unique alpha values that actually occur for a given quantized RGB + // bucket is usually very small. If you randomly sample 8 values out of 64, + // the probability that these 4 samples are all unique is high if the distribution + // of alpha values is skewed or if only a few alpha values are used. + // + // 3. Statistically, for many real-world images, most RGB buckets will have only a couple + // of unique alpha values. Allocating 8 slots per bucket provides a good trade-off: + // it captures the common-case scenario while keeping overall memory usage low. + // + // 4. Even if more than 8 unique alpha values occur in a bucket, + // our design overwrites the first entry. This behavior gives us some "wriggle room" + // while preserving the most frequently encountered or most recent values. + public const int Capacity = 8; + public byte Count; + private InlineArray8 entries; + + [MethodImpl(InliningOptions.ShortMethod)] + public bool TryGetValue(byte quantizedAlpha, out short paletteIndex) + { + for (int i = 0; i < this.Count; i++) + { + ref AlphaEntry entry = ref this.entries[i]; + if (entry.QuantizedAlpha == quantizedAlpha) + { + paletteIndex = entry.PaletteIndex; + return true; + } + } + + paletteIndex = -1; + return false; + } + + [MethodImpl(InliningOptions.ShortMethod)] + public void Add(byte quantizedAlpha, short paletteIndex) + { + // Check for an existing entry with the same quantized alpha. + for (int i = 0; i < this.Count; i++) + { + ref AlphaEntry entry = ref this.entries[i]; + if (entry.QuantizedAlpha == quantizedAlpha) + { + // Update palette index if found. + entry.PaletteIndex = paletteIndex; + return; + } + } + + // If there's room, add a new entry. + if (this.Count < Capacity) + { + ref AlphaEntry newEntry = ref this.entries[this.Count]; + newEntry.QuantizedAlpha = quantizedAlpha; + newEntry.PaletteIndex = paletteIndex; + this.Count++; + } + else + { + // Bucket is full. Overwrite the first entry to give us some wriggle room. + this.entries[0].QuantizedAlpha = quantizedAlpha; + this.entries[0].PaletteIndex = paletteIndex; + } + } + } +} + +/// +/// A fixed-capacity dictionary with exactly 512 entries mapping a key +/// to a value. +/// +/// +/// The dictionary is implemented using a fixed array of 512 buckets and an entries array +/// of the same size. The bucket for a key is computed as (key & 0x1FF), and collisions are +/// resolved through a linked chain stored in the field. +/// The overall memory usage is approximately 4–5 KB. Both lookup and insertion operations are, +/// on average, O(1) since the bucket is determined via a simple bitmask and collision chains are +/// typically very short; in the worst-case, the number of iterations is bounded by 256. +/// This guarantees highly efficient and predictable performance for small, fixed-size color palettes. +/// +internal unsafe struct AccurateCache : IColorIndexCache +{ + // Buckets array: each bucket holds the index (0-based) into the entries array + // of the first entry in the chain, or -1 if empty. + private readonly IMemoryOwner bucketsOwner; + private MemoryHandle bucketsHandle; + private short* buckets; + + // Entries array: stores up to 256 entries. + private readonly IMemoryOwner entriesOwner; + private MemoryHandle entriesHandle; + private Entry* entries; + + public const int Capacity = 512; + + private AccurateCache(MemoryAllocator allocator) + { + this.Count = 0; + + // Allocate exactly 512 indexes for buckets. + this.bucketsOwner = allocator.Allocate(Capacity, AllocationOptions.Clean); + Span bucketSpan = this.bucketsOwner.GetSpan(); + bucketSpan.Fill(-1); + this.bucketsHandle = this.bucketsOwner.Memory.Pin(); + this.buckets = (short*)this.bucketsHandle.Pointer; + + // Allocate exactly 512 entries. + this.entriesOwner = allocator.Allocate(Capacity, AllocationOptions.Clean); + this.entriesHandle = this.entriesOwner.Memory.Pin(); + this.entries = (Entry*)this.entriesHandle.Pointer; + } + + public int Count { get; private set; } + + /// + public static AccurateCache Create(MemoryAllocator allocator) => new(allocator); + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public bool TryAdd(Rgba32 color, short value) + { + if (this.Count == Capacity) + { + return false; // Dictionary is full. + } + + uint key = color.PackedValue; + + // The key is a 32-bit unsigned integer representing an RGBA color, where the bytes are laid out as R|G|B|A + // (with R in the most significant byte and A in the least significant). + // To compute the bucket index: + // 1. (key >> 16) extracts the top 16 bits, effectively giving us the R and G channels. + // 2. (key >> 8) shifts the key right by 8 bits, bringing R, G, and B into the lower 24 bits (dropping A). + // 3. XORing these two values with the original key mixes bits from all four channels (R, G, B, and A), + // which helps to counteract situations where one or more channels have a limited range. + // 4. Finally, we apply a bitmask of 0x1FF to keep only the lowest 9 bits, ensuring the result is between 0 and 511, + // which corresponds to our fixed bucket count of 512. + int bucket = (int)(((key >> 16) ^ (key >> 8) ^ key) & 0x1FF); + int i = this.buckets[bucket]; + + // Traverse the collision chain. + Entry* entries = this.entries; + while (i != -1) + { + Entry e = entries[i]; + if (e.Key == key) + { + // Key already exists; do not overwrite. + return false; + } + + i = e.Next; + } + + short index = (short)this.Count; + this.Count++; + + // Insert the new entry: + entries[index].Key = key; + entries[index].Value = value; + + // Link this new entry into the bucket chain. + entries[index].Next = this.buckets[bucket]; + this.buckets[bucket] = index; + return true; + } + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public bool TryGetValue(Rgba32 color, out short value) + { + uint key = color.PackedValue; + int bucket = (int)(((key >> 16) ^ (key >> 8) ^ key) & 0x1FF); + int i = this.buckets[bucket]; + + // If the bucket is empty, return immediately. + if (i == -1) + { + value = -1; + return false; + } + + // Traverse the chain. + Entry* entries = this.entries; + do + { + Entry e = entries[i]; + if (e.Key == key) + { + value = e.Value; + return true; + } + + i = e.Next; + } + while (i != -1); + + value = -1; + return false; + } + + /// + /// Clears the dictionary. + /// + public void Clear() + { + Span bucketSpan = this.bucketsOwner.GetSpan(); + bucketSpan.Fill(-1); + this.Count = 0; + } + + public void Dispose() + { + this.bucketsHandle.Dispose(); + this.bucketsOwner.Dispose(); + this.entriesHandle.Dispose(); + this.entriesOwner.Dispose(); + this.buckets = null; + this.entries = null; + } + + private struct Entry + { + public uint Key; // The key (packed RGBA) + public short Value; // The value; -1 means unused. + public short Next; // Index of the next entry in the chain, or -1 if none. + } +} + +/// +/// Represents a cache that does not store any values. +/// It allows adding colors, but always returns false when trying to retrieve them. +/// +internal readonly struct NullCache : IColorIndexCache +{ + /// + public static NullCache Create(MemoryAllocator allocator) => default; + + /// + public bool TryAdd(Rgba32 color, short value) => true; + + /// + public bool TryGetValue(Rgba32 color, out short value) + { + value = -1; + return false; + } + + /// + public void Clear() + { + } + + /// + public void Dispose() + { + } +} diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index 9d5b606040..02dce8ca48 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -13,7 +13,7 @@ public interface IQuantizer /// /// Gets the quantizer options defining quantization rules. /// - QuantizerOptions Options { get; } + public QuantizerOptions Options { get; } /// /// Creates the generic frame quantizer. @@ -21,7 +21,7 @@ public interface IQuantizer /// The to configure internal operations. /// The pixel format. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel; /// @@ -31,6 +31,6 @@ IQuantizer CreatePixelSpecificQuantizer(Configuration configurat /// The to configure internal operations. /// The options to create the quantizer with. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer{TPixel}.cs index 35bbb1289e..1e6420eaa9 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer{TPixel}.cs @@ -16,26 +16,26 @@ public interface IQuantizer : IDisposable /// /// Gets the configuration. /// - Configuration Configuration { get; } + public Configuration Configuration { get; } /// /// Gets the quantizer options defining quantization rules. /// - QuantizerOptions Options { get; } + public QuantizerOptions Options { get; } /// /// Gets the quantized color palette. /// /// - /// The palette has not been built via . + /// The palette has not been built via . /// - ReadOnlyMemory Palette { get; } + public ReadOnlyMemory Palette { get; } /// /// Adds colors to the quantized palette from the given pixel source. /// /// The of source pixels to register. - void AddPaletteColors(Buffer2DRegion pixelRegion); + public void AddPaletteColors(in Buffer2DRegion pixelRegion); /// /// Quantizes an image frame and return the resulting output pixels. @@ -46,10 +46,10 @@ public interface IQuantizer : IDisposable /// A representing a quantized version of the source frame pixels. /// /// - /// Only executes the second (quantization) step. The palette has to be built by calling . - /// To run both steps, use . + /// Only executes the second (quantization) step. The palette has to be built by calling . + /// To run both steps, use . /// - IndexedImageFrame QuantizeFrame(ImageFrame source, Rectangle bounds); + public IndexedImageFrame QuantizeFrame(ImageFrame source, Rectangle bounds); /// /// Returns the index and color from the quantized palette corresponding to the given color. @@ -57,7 +57,7 @@ public interface IQuantizer : IDisposable /// The color to match. /// The matched color. /// The index. - byte GetQuantizedColor(TPixel color, out TPixel match); + public byte GetQuantizedColor(TPixel color, out TPixel match); // TODO: Enable bulk operations. // void GetQuantizedColors(ReadOnlySpan colors, ReadOnlySpan palette, Span indices, Span matches); diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizingPixelRowDelegate{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizingPixelRowDelegate{TPixel}.cs new file mode 100644 index 0000000000..3cf4c93d62 --- /dev/null +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizingPixelRowDelegate{TPixel}.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Processing.Processors.Quantization; + +/// +/// Defines a delegate for processing a row of pixels in an image for quantization. +/// +/// Represents a pixel type that can be processed in a quantizing operation. +internal interface IQuantizingPixelRowDelegate + where TPixel : unmanaged, IPixel +{ + /// + /// Processes a row of pixels for quantization. + /// + /// The row of pixels to process. + /// The index of the row being processed. + public void Invoke(ReadOnlySpan row, int rowIndex); +} diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index 8b39b74579..07596b68a8 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -16,11 +16,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// /// /// The pixel format. -[SuppressMessage( - "Design", - "CA1001:Types that own disposable fields should be disposable", - Justification = "https://github.com/dotnet/roslyn-analyzers/issues/6151")] +#pragma warning disable CA1001 // Types that own disposable fields should be disposable +// See https://github.com/dotnet/roslyn-analyzers/issues/6151 public struct OctreeQuantizer : IQuantizer +#pragma warning restore CA1001 // Types that own disposable fields should be disposable where TPixel : unmanaged, IPixel { private readonly int maxColors; @@ -28,14 +27,14 @@ public struct OctreeQuantizer : IQuantizer private readonly Octree octree; private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; - private EuclideanPixelMap? pixelMap; + private PixelMap? pixelMap; private readonly bool isDithering; private bool isDisposed; /// /// Initializes a new instance of the struct. /// - /// The configuration which allows altering default behaviour or extending the library. + /// The configuration which allows altering default behavior or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] public OctreeQuantizer(Configuration configuration, QuantizerOptions options) @@ -45,7 +44,7 @@ public OctreeQuantizer(Configuration configuration, QuantizerOptions options) this.maxColors = this.Options.MaxColors; this.bitDepth = Numerics.Clamp(ColorNumerics.GetBitsNeededForColorDepth(this.maxColors), 1, 8); - this.octree = new Octree(this.bitDepth); + this.octree = new Octree(configuration, this.bitDepth, this.maxColors, this.Options.TransparencyThreshold); this.paletteOwner = configuration.MemoryAllocator.Allocate(this.maxColors, AllocationOptions.Clean); this.pixelMap = default; this.palette = default; @@ -60,64 +59,41 @@ public OctreeQuantizer(Configuration configuration, QuantizerOptions options) public QuantizerOptions Options { get; } /// - public readonly ReadOnlyMemory Palette + public ReadOnlyMemory Palette { get { - QuantizerUtilities.CheckPaletteState(in this.palette); + if (this.palette.IsEmpty) + { + this.ResolvePalette(); + QuantizerUtilities.CheckPaletteState(in this.palette); + } + return this.palette; } } /// - public void AddPaletteColors(Buffer2DRegion pixelRegion) + public readonly void AddPaletteColors(in Buffer2DRegion pixelRegion) { - using (IMemoryOwner buffer = this.Configuration.MemoryAllocator.Allocate(pixelRegion.Width)) - { - Span bufferSpan = buffer.GetSpan(); - - // Loop through each row - for (int y = 0; y < pixelRegion.Height; y++) - { - Span row = pixelRegion.DangerousGetRowSpan(y); - PixelOperations.Instance.ToRgba32(this.Configuration, row, bufferSpan); - - for (int x = 0; x < bufferSpan.Length; x++) - { - Rgba32 rgba = bufferSpan[x]; - - // Add the color to the Octree - this.octree.AddColor(rgba); - } - } - } + PixelRowDelegate pixelRowDelegate = new(this.octree); + QuantizerUtilities.AddPaletteColors, TPixel, Rgba32, PixelRowDelegate>( + ref Unsafe.AsRef(in this), + in pixelRegion, + in pixelRowDelegate); + } - int paletteIndex = 0; + private void ResolvePalette() + { + short paletteIndex = 0; Span paletteSpan = this.paletteOwner.GetSpan(); - // On very rare occasions, (blur.png), the quantizer does not preserve a - // transparent entry when palletizing the captured colors. - // To workaround this we ensure the palette ends with the default color - // for higher bit depths. Lower bit depths will correctly reduce the palette. - // TODO: Investigate more evenly reduced palette reduction. - int max = this.maxColors; - if (this.bitDepth >= 4) - { - max--; - } - - this.octree.Palletize(paletteSpan, max, ref paletteIndex); + this.octree.Palettize(paletteSpan, ref paletteIndex); ReadOnlyMemory result = this.paletteOwner.Memory[..paletteSpan.Length]; - // When called multiple times by QuantizerUtilities.BuildPalette - // this prevents memory churn caused by reallocation. - if (this.pixelMap is null) - { - this.pixelMap = new EuclideanPixelMap(this.Configuration, result); - } - else + if (this.isDithering) { - this.pixelMap.Clear(result); + this.pixelMap = PixelMapFactory.Create(this.Configuration, result, this.Options.ColorMatchingMode); } this.palette = result; @@ -132,18 +108,19 @@ public readonly IndexedImageFrame QuantizeFrame(ImageFrame sourc [MethodImpl(InliningOptions.ShortMethod)] public readonly byte GetQuantizedColor(TPixel color, out TPixel match) { - // Octree only maps the RGB component of a color - // so cannot tell the difference between a fully transparent - // pixel and a black one. - if (this.isDithering || color.Equals(default)) + // Due to the addition of new colors by dithering that are not part of the original histogram, + // the octree nodes might not match the correct color. + // In this case, we must use the pixel map to get the closest color. + if (this.isDithering) { return (byte)this.pixelMap!.GetClosestColor(color, out match); } ref TPixel paletteRef = ref MemoryMarshal.GetReference(this.palette.Span); - byte index = (byte)this.octree.GetPaletteIndex(color); - match = Unsafe.Add(ref paletteRef, index); - return index; + + int index = this.octree.GetPaletteIndex(color); + match = Unsafe.Add(ref paletteRef, (nuint)index); + return (byte)index; } /// @@ -155,413 +132,529 @@ public void Dispose() this.paletteOwner.Dispose(); this.pixelMap?.Dispose(); this.pixelMap = null; + this.octree.Dispose(); } } + private readonly struct PixelRowDelegate : IQuantizingPixelRowDelegate + { + private readonly Octree octree; + + public PixelRowDelegate(Octree octree) => this.octree = octree; + + public void Invoke(ReadOnlySpan row, int rowIndex) => this.octree.AddColors(row); + } + /// - /// Class which does the actual quantization. + /// A hexadecatree-based color quantization structure used for fast color distance lookups and palette generation. + /// This tree maintains a fixed pool of nodes (capacity 4096) where each node can have up to 16 children, stores + /// color accumulation data, and supports dynamic node allocation and reduction. It offers near-constant-time insertions + /// and lookups while consuming roughly 240 KB for the node pool. /// - private sealed class Octree + internal sealed class Octree : IDisposable { - /// - /// The root of the Octree - /// - private readonly OctreeNode root; + // The memory allocator. + private readonly MemoryAllocator allocator; - /// - /// Maximum number of significant bits in the image - /// + // Pooled buffer for OctreeNodes. + private readonly IMemoryOwner nodesOwner; + + // Reducible nodes: one per level; we use an integer index; -1 means “no node.” + private readonly short[] reducibleNodes; + + // Maximum number of allowable colors. + private readonly int maxColors; + + // Maximum significant bits. private readonly int maxColorBits; - /// - /// Store the last node quantized - /// - private OctreeNode? previousNode; + // The threshold for transparent colors. + private readonly int transparencyThreshold255; - /// - /// Cache the previous color quantized - /// + // Instead of a reference to the root, we store the index of the root node. + // Index 0 is reserved for the root. + private readonly short rootIndex; + + // Running index for node allocation. Start at 1 so that index 0 is reserved for the root. + private short nextNode = 1; + + // Previously quantized node (index; -1 if none) and its color. + private int previousNode; private Rgba32 previousColor; + // Free list for reclaimed node indices. + private readonly Stack freeIndices = new(); + /// /// Initializes a new instance of the class. /// - /// - /// The maximum number of significant bits in the image - /// - public Octree(int maxColorBits) + /// The configuration which allows altering default behavior or extending the library. + /// The maximum number of significant bits in the image. + /// The maximum number of colors to allow in the palette. + /// The threshold for transparent colors. + public Octree( + Configuration configuration, + int maxColorBits, + int maxColors, + float transparencyThreshold) { this.maxColorBits = maxColorBits; + this.maxColors = maxColors; + this.transparencyThreshold255 = (int)(transparencyThreshold * 255F); this.Leaves = 0; - this.ReducibleNodes = new OctreeNode[9]; - this.root = new OctreeNode(0, this.maxColorBits, this); + this.previousNode = -1; this.previousColor = default; - this.previousNode = null; + + // Allocate a conservative buffer for nodes. + const int capacity = 4096; + this.allocator = configuration.MemoryAllocator; + this.nodesOwner = this.allocator.Allocate(capacity, AllocationOptions.Clean); + + // Create the reducible nodes array (one per level 0 .. maxColorBits-1). + this.reducibleNodes = new short[this.maxColorBits]; + this.reducibleNodes.AsSpan().Fill(-1); + + // Reserve index 0 for the root. + this.rootIndex = 0; + ref OctreeNode root = ref this.Nodes[this.rootIndex]; + root.Initialize(0, this.maxColorBits, this, this.rootIndex); } /// - /// Gets or sets the number of leaves in the tree + /// Gets or sets the number of leaves in the tree. /// - public int Leaves - { - [MethodImpl(InliningOptions.ShortMethod)] - get; + public int Leaves { get; set; } - [MethodImpl(InliningOptions.ShortMethod)] - set; - } + /// + /// Gets the full collection of nodes as a span. + /// + internal Span Nodes => this.nodesOwner.Memory.Span; /// - /// Gets the array of reducible nodes + /// Adds a span of colors to the octree. /// - private OctreeNode?[] ReducibleNodes + /// A span of color values to be added. + public void AddColors(ReadOnlySpan row) { - [MethodImpl(InliningOptions.ShortMethod)] - get; + for (int x = 0; x < row.Length; x++) + { + this.AddColor(row[x]); + } } /// - /// Add a given color value to the Octree + /// Add a color to the Octree. /// /// The color to add. - public void AddColor(Rgba32 color) + private void AddColor(Rgba32 color) { - // Check if this request is for the same color as the last + // Ensure that the tree is not already full. + if (this.nextNode >= this.Nodes.Length && this.freeIndices.Count == 0) + { + while (this.Leaves > this.maxColors) + { + this.Reduce(); + } + } + + // If the color is the same as the previous color, increment the node. + // Otherwise, add a new node. if (this.previousColor.Equals(color)) { - // If so, check if I have a previous node setup. - // This will only occur if the first color in the image - // happens to be black, with an alpha component of zero. - if (this.previousNode is null) + if (this.previousNode == -1) { this.previousColor = color; - this.root.AddColor(ref color, this.maxColorBits, 0, this); + OctreeNode.AddColor(this.rootIndex, color, this.maxColorBits, 0, this); } else { - // Just update the previous node - this.previousNode.Increment(ref color); + OctreeNode.Increment(this.previousNode, color, this); } } else { this.previousColor = color; - this.root.AddColor(ref color, this.maxColorBits, 0, this); + OctreeNode.AddColor(this.rootIndex, color, this.maxColorBits, 0, this); } } /// - /// Convert the nodes in the Octree to a palette with a maximum of colorCount colors + /// Construct the palette from the octree. /// - /// The palette to fill. - /// The maximum number of colors - /// The palette index, used to calculate the final size of the palette. - [MethodImpl(InliningOptions.ShortMethod)] - public void Palletize(Span palette, int colorCount, ref int paletteIndex) + /// The palette to construct. + /// The current palette index. + public void Palettize(Span palette, ref short paletteIndex) { - while (this.Leaves > colorCount) + while (this.Leaves > this.maxColors) { this.Reduce(); } - this.root.ConstructPalette(palette, ref paletteIndex); + this.Nodes[this.rootIndex].ConstructPalette(this, palette, ref paletteIndex); } /// - /// Get the palette index for the passed color + /// Get the palette index for the passed color. /// - /// The color to match. - /// - /// The index. - /// - [MethodImpl(InliningOptions.ShortMethod)] + /// The color to get the palette index for. + /// The . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetPaletteIndex(TPixel color) - { - Rgba32 rgba = color.ToRgba32(); - return this.root.GetPaletteIndex(ref rgba, 0); - } + => this.Nodes[this.rootIndex].GetPaletteIndex(color.ToRgba32(), 0, this); /// - /// Keep track of the previous node that was quantized + /// Track the previous node and color. /// - /// - /// The node last quantized - /// - [MethodImpl(InliningOptions.ShortMethod)] - public void TrackPrevious(OctreeNode node) => this.previousNode = node; + /// The node index. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void TrackPrevious(int nodeIndex) + => this.previousNode = nodeIndex; /// - /// Reduce the depth of the tree + /// Reduce the depth of the tree. /// private void Reduce() { // Find the deepest level containing at least one reducible node int index = this.maxColorBits - 1; - while ((index > 0) && (this.ReducibleNodes[index] is null)) + while ((index > 0) && (this.reducibleNodes[index] == -1)) { index--; } // Reduce the node most recently added to the list at level 'index' - OctreeNode node = this.ReducibleNodes[index]!; - this.ReducibleNodes[index] = node.NextReducible; + ref OctreeNode node = ref this.Nodes[this.reducibleNodes[index]]; + this.reducibleNodes[index] = node.NextReducibleIndex; // Decrement the leaf count after reducing the node - this.Leaves -= node.Reduce(); + node.Reduce(this); // And just in case I've reduced the last color to be added, and the next color to // be added is the same, invalidate the previousNode... - this.previousNode = null; + this.previousNode = -1; } - /// - /// Class which encapsulates each node in the tree - /// - public sealed class OctreeNode + // Allocate a new OctreeNode from the pooled buffer. + // First check the freeIndices stack. + internal short AllocateNode() { - /// - /// Pointers to any child nodes - /// - private readonly OctreeNode?[]? children; - - /// - /// Flag indicating that this is a leaf node - /// - private bool leaf; + if (this.freeIndices.Count > 0) + { + return this.freeIndices.Pop(); + } - /// - /// Number of pixels in this node - /// - private int pixelCount; + if (this.nextNode >= this.Nodes.Length) + { + return -1; + } - /// - /// Red component - /// - private int red; + short newIndex = this.nextNode; + this.nextNode++; + return newIndex; + } - /// - /// Green Component - /// - private int green; + /// + /// Free a node index, making it available for re-allocation. + /// + /// The index to free. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void FreeNode(short index) + { + this.freeIndices.Push(index); + this.Leaves--; + } - /// - /// Blue component - /// - private int blue; + /// + public void Dispose() => this.nodesOwner.Dispose(); - /// - /// The index of this node in the palette - /// - private int paletteIndex; + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct OctreeNode + { + public bool Leaf; + public int PixelCount; + public int Red; + public int Green; + public int Blue; + public int Alpha; + public short PaletteIndex; + public short NextReducibleIndex; + private InlineArray16 children; + + [UnscopedRef] + public Span Children => this.children; /// - /// Initializes a new instance of the class. + /// Initialize the . /// - /// The level in the tree = 0 - 7. + /// The level of the node. /// The number of significant color bits in the image. - /// The tree to which this node belongs. - public OctreeNode(int level, int colorBits, Octree octree) + /// The parent octree. + /// The index of the node. + public void Initialize(int level, int colorBits, Octree octree, short index) { - // Construct the new node - this.leaf = level == colorBits; - - this.red = this.green = this.blue = 0; - this.pixelCount = 0; - - // If a leaf, increment the leaf count - if (this.leaf) + // Construct the new node. + this.Leaf = level == colorBits; + this.Red = 0; + this.Green = 0; + this.Blue = 0; + this.Alpha = 0; + this.PixelCount = 0; + this.PaletteIndex = 0; + this.NextReducibleIndex = -1; + + // Always clear the Children array. + this.Children.Fill(-1); + + if (this.Leaf) { octree.Leaves++; - this.NextReducible = null; - this.children = null; } else { - // Otherwise add this to the reducible nodes - this.NextReducible = octree.ReducibleNodes[level]; - octree.ReducibleNodes[level] = this; - this.children = new OctreeNode[8]; + // Add this node to the reducible nodes list for its level. + this.NextReducibleIndex = octree.reducibleNodes[level]; + octree.reducibleNodes[level] = index; } } /// - /// Gets the next reducible node - /// - public OctreeNode? NextReducible - { - [MethodImpl(InliningOptions.ShortMethod)] - get; - } - - /// - /// Add a color into the tree + /// Add a color to the Octree. /// + /// The node index. /// The color to add. - /// The number of significant color bits. - /// The level in the tree. - /// The tree to which this node belongs. - public void AddColor(ref Rgba32 color, int colorBits, int level, Octree octree) + /// The number of significant color bits in the image. + /// The level of the node. + /// The parent octree. + public static void AddColor(int nodeIndex, Rgba32 color, int colorBits, int level, Octree octree) { - // Update the color information if this is a leaf - if (this.leaf) + ref OctreeNode node = ref octree.Nodes[nodeIndex]; + if (node.Leaf) { - this.Increment(ref color); - - // Setup the previous node - octree.TrackPrevious(this); + Increment(nodeIndex, color, octree); + octree.TrackPrevious(nodeIndex); } else { - // Go to the next level down in the tree - int index = GetColorIndex(ref color, level); + int index = GetColorIndex(color, level); + short childIndex; - OctreeNode? child = this.children![index]; - if (child is null) + Span children = node.Children; + childIndex = children[index]; + + if (childIndex == -1) { - // Create a new child node and store it in the array - child = new OctreeNode(level + 1, colorBits, octree); - this.children[index] = child; + childIndex = octree.AllocateNode(); + + if (childIndex == -1) + { + // No room in the tree, so increment the count and return. + Increment(nodeIndex, color, octree); + octree.TrackPrevious(nodeIndex); + return; + } + + ref OctreeNode child = ref octree.Nodes[childIndex]; + child.Initialize(level + 1, colorBits, octree, childIndex); + children[index] = childIndex; } - // Add the color to the child node - child.AddColor(ref color, colorBits, level + 1, octree); + AddColor(childIndex, color, colorBits, level + 1, octree); } } /// - /// Reduce this node by removing all of its children + /// Increment the color components of this node. + /// + /// The node index. + /// The color to increment by. + /// The parent octree. + public static void Increment(int nodeIndex, Rgba32 color, Octree octree) + { + ref OctreeNode node = ref octree.Nodes[nodeIndex]; + node.PixelCount++; + node.Red += color.R; + node.Green += color.G; + node.Blue += color.B; + node.Alpha += color.A; + } + + /// + /// Reduce this node by ensuring its children are all reduced (i.e. leaves) and then merging their data. /// - /// The number of leaves removed - public int Reduce() + /// The parent octree. + public void Reduce(Octree octree) { - this.red = this.green = this.blue = 0; - int childNodes = 0; + // If already a leaf, do nothing. + if (this.Leaf) + { + return; + } - // Loop through all children and add their information to this node - for (int index = 0; index < 8; index++) + // Now merge the (presumably reduced) children. + int pixelCount = 0; + int sumRed = 0, sumGreen = 0, sumBlue = 0, sumAlpha = 0; + Span children = this.Children; + for (int i = 0; i < children.Length; i++) { - OctreeNode? child = this.children![index]; - if (child != null) + short childIndex = children[i]; + if (childIndex != -1) { - this.red += child.red; - this.green += child.green; - this.blue += child.blue; - this.pixelCount += child.pixelCount; - ++childNodes; - this.children[index] = null; + ref OctreeNode child = ref octree.Nodes[childIndex]; + int pixels = child.PixelCount; + + sumRed += child.Red; + sumGreen += child.Green; + sumBlue += child.Blue; + sumAlpha += child.Alpha; + pixelCount += pixels; + + // Free the child immediately. + children[i] = -1; + octree.FreeNode(childIndex); } } - // Now change this to a leaf node - this.leaf = true; + if (pixelCount > 0) + { + this.Red = sumRed; + this.Green = sumGreen; + this.Blue = sumBlue; + this.Alpha = sumAlpha; + this.PixelCount = pixelCount; + } + else + { + this.Red = this.Green = this.Blue = this.Alpha = 0; + this.PixelCount = 0; + } - // Return the number of nodes to decrement the leaf count by - return childNodes - 1; + this.Leaf = true; + octree.Leaves++; } /// - /// Traverse the tree, building up the color palette + /// Traverse the tree to construct the palette. /// - /// The palette - /// The current palette index - [MethodImpl(InliningOptions.ColdPath)] - public void ConstructPalette(Span palette, ref int index) + /// The parent octree. + /// The palette to construct. + /// The current palette index. + public void ConstructPalette(Octree octree, Span palette, ref short paletteIndex) { - if (this.leaf) + if (this.Leaf) { - // Set the color of the palette entry - Vector3 vector = Vector3.Clamp( - new Vector3(this.red, this.green, this.blue) / this.pixelCount, - Vector3.Zero, - new Vector3(255)); + Vector4 sum = new(this.Red, this.Green, this.Blue, this.Alpha); + Vector4 offset = new(this.PixelCount >> 1); + Vector4 vector = Vector4.Clamp( + (sum + offset) / this.PixelCount, + Vector4.Zero, + new Vector4(255)); + + if (vector.W < octree.transparencyThreshold255) + { + vector = Vector4.Zero; + } - palette[index] = TPixel.FromRgba32(new Rgba32((byte)vector.X, (byte)vector.Y, (byte)vector.Z)); + palette[paletteIndex] = TPixel.FromRgba32(new Rgba32((byte)vector.X, (byte)vector.Y, (byte)vector.Z, (byte)vector.W)); - // Consume the next palette index - this.paletteIndex = index++; + this.PaletteIndex = paletteIndex++; } else { - // Loop through children looking for leaves - for (int i = 0; i < 8; i++) + Span children = this.Children; + for (int i = 0; i < children.Length; i++) { - this.children![i]?.ConstructPalette(palette, ref index); + int childIndex = children[i]; + if (childIndex != -1) + { + octree.Nodes[childIndex].ConstructPalette(octree, palette, ref paletteIndex); + } } } } /// - /// Return the palette index for the passed color + /// Get the palette index for the passed color. /// - /// The pixel data. - /// The level. - /// - /// The representing the index of the pixel in the palette. - /// - [MethodImpl(InliningOptions.ColdPath)] - public int GetPaletteIndex(ref Rgba32 pixel, int level) + /// The color to get the palette index for. + /// The level of the node. + /// The parent octree. + public int GetPaletteIndex(Rgba32 color, int level, Octree octree) { - if (this.leaf) + if (this.Leaf) { - return this.paletteIndex; + return this.PaletteIndex; } - int colorIndex = GetColorIndex(ref pixel, level); - OctreeNode? child = this.children![colorIndex]; - - int index = 0; - if (child != null) + int colorIndex = GetColorIndex(color, level); + Span children = this.Children; + int childIndex = children[colorIndex]; + if (childIndex != -1) { - index = child.GetPaletteIndex(ref pixel, level + 1); + return octree.Nodes[childIndex].GetPaletteIndex(color, level + 1, octree); } - else + + for (int i = 0; i < children.Length; i++) { - // Check other children. - for (int i = 0; i < this.children.Length; i++) + childIndex = children[i]; + if (childIndex != -1) { - child = this.children[i]; - if (child != null) + int childPaletteIndex = octree.Nodes[childIndex].GetPaletteIndex(color, level + 1, octree); + if (childPaletteIndex != -1) { - int childIndex = child.GetPaletteIndex(ref pixel, level + 1); - if (childIndex != 0) - { - return childIndex; - } + return childPaletteIndex; } } } - return index; + return -1; } /// /// Gets the color index at the given level. /// - /// The color. - /// The node level. - /// The index. - [MethodImpl(InliningOptions.ShortMethod)] - private static int GetColorIndex(ref Rgba32 color, int level) + /// The color to get the index for. + /// The level to get the index at. + public static int GetColorIndex(Rgba32 color, int level) { + // Determine how many bits to shift based on the current tree level. + // At level 0, shift = 7; as level increases, the shift decreases. int shift = 7 - level; byte mask = (byte)(1 << shift); - return ((color.R & mask) >> shift) - | (((color.G & mask) >> shift) << 1) - | (((color.B & mask) >> shift) << 2); - } + // Compute the luminance of the RGB components using the BT.709 standard. + // This gives a measure of brightness for the color. + int luminance = ColorNumerics.Get8BitBT709Luminance(color.R, color.G, color.B); - /// - /// Increment the color count and add to the color information - /// - /// The pixel to add. - [MethodImpl(InliningOptions.ShortMethod)] - public void Increment(ref Rgba32 color) - { - this.pixelCount++; - this.red += color.R; - this.green += color.G; - this.blue += color.B; + // Define thresholds for determining when to include the alpha bit in the index. + // The thresholds are scaled according to the current level. + // 128 is the midpoint of the 8-bit range (0–255), so shifting it right by 'level' + // produces a threshold that scales with the color cube subdivision. + int darkThreshold = 128 >> level; + + // The light threshold is set symmetrically: 255 minus the scaled midpoint. + int lightThreshold = 255 - (128 >> level); + + // If the pixel is fully opaque and its brightness falls between the dark and light thresholds, + // ignore the alpha channel to maximize RGB resolution. + // Otherwise (if the pixel is dark, light, or semi-transparent), include the alpha bit + // to preserve any gradient that may be present. + if (color.A == 255 && luminance > darkThreshold && luminance < lightThreshold) + { + // Extract one bit each from R, G, and B channels and combine them into a 3-bit index. + int rBits = ((color.R & mask) >> shift) << 2; + int gBits = ((color.G & mask) >> shift) << 1; + int bBits = (color.B & mask) >> shift; + return rBits | gBits | bBits; + } + else + { + // Extract one bit from each channel including alpha (alpha becomes the most significant bit). + int aBits = ((color.A & mask) >> shift) << 3; + int rBits = ((color.R & mask) >> shift) << 2; + int gBits = ((color.G & mask) >> shift) << 1; + int bBits = (color.B & mask) >> shift; + return aBits | rBits | gBits | bBits; + } } } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index 13a59a26de..a49691515a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -11,7 +11,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; public class PaletteQuantizer : IQuantizer { private readonly ReadOnlyMemory colorPalette; - private readonly int transparentIndex; + private readonly int transparencyIndex; + private readonly Color transparentColor; /// /// Initializes a new instance of the class. @@ -25,27 +26,33 @@ public PaletteQuantizer(ReadOnlyMemory palette) /// /// Initializes a new instance of the class. /// - /// The color palette. + /// The color palette to use. /// The quantizer options defining quantization rules. public PaletteQuantizer(ReadOnlyMemory palette, QuantizerOptions options) - : this(palette, options, -1) + : this(palette, options, -1, default) { } /// /// Initializes a new instance of the class. /// - /// The color palette. + /// The color palette to use. /// The quantizer options defining quantization rules. - /// An explicit index at which to match transparent pixels. - internal PaletteQuantizer(ReadOnlyMemory palette, QuantizerOptions options, int transparentIndex) + /// The index of the color in the palette that should be considered as transparent. + /// The color that should be considered as transparent. + internal PaletteQuantizer( + ReadOnlyMemory palette, + QuantizerOptions options, + int transparencyIndex, + Color transparentColor) { Guard.MustBeGreaterThan(palette.Length, 0, nameof(palette)); Guard.NotNull(options, nameof(options)); this.colorPalette = palette; this.Options = options; - this.transparentIndex = transparentIndex; + this.transparencyIndex = transparencyIndex; + this.transparentColor = transparentColor; } /// @@ -66,6 +73,6 @@ public IQuantizer CreatePixelSpecificQuantizer(Configuration con // treat the buffer as FILO. TPixel[] palette = new TPixel[Math.Min(options.MaxColors, this.colorPalette.Length)]; Color.ToPixel(this.colorPalette.Span[..palette.Length], palette.AsSpan()); - return new PaletteQuantizer(configuration, options, palette, this.transparentIndex); + return new PaletteQuantizer(configuration, options, palette, this.transparencyIndex, this.transparentColor.ToPixel()); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index 092975d28a..4fd044ab40 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -17,10 +17,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; "Design", "CA1001:Types that own disposable fields should be disposable", Justification = "https://github.com/dotnet/roslyn-analyzers/issues/6151")] -internal readonly struct PaletteQuantizer : IQuantizer +internal struct PaletteQuantizer : IQuantizer where TPixel : unmanaged, IPixel { - private readonly EuclideanPixelMap pixelMap; + private readonly PixelMap pixelMap; + private int transparencyIndex; + private TPixel transparentColor; /// /// Initializes a new instance of the struct. @@ -28,20 +30,37 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// The configuration which allows altering default behavior or extending the library. /// The quantizer options defining quantization rules. /// The palette to use. - /// An explicit index at which to match transparent pixels. [MethodImpl(InliningOptions.ShortMethod)] + public PaletteQuantizer(Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette) + : this(configuration, options, palette, -1, default) + { + Guard.NotNull(configuration, nameof(configuration)); + Guard.NotNull(options, nameof(options)); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The configuration which allows altering default behavior or extending the library. + /// The quantizer options defining quantization rules. + /// The palette to use. + /// The index of the color in the palette that should be considered as transparent. + /// The color that should be considered as transparent. public PaletteQuantizer( Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette, - int transparentIndex) + int transparencyIndex, + TPixel transparentColor) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); this.Configuration = configuration; this.Options = options; - this.pixelMap = new EuclideanPixelMap(configuration, palette, transparentIndex); + this.pixelMap = PixelMapFactory.Create(this.Configuration, palette, options.ColorMatchingMode); + this.transparencyIndex = transparencyIndex; + this.transparentColor = transparentColor; } /// @@ -51,7 +70,13 @@ public PaletteQuantizer( public QuantizerOptions Options { get; } /// - public ReadOnlyMemory Palette => this.pixelMap.Palette; + public readonly ReadOnlyMemory Palette => this.pixelMap.Palette; + + /// + [MethodImpl(InliningOptions.ShortMethod)] + public readonly void AddPaletteColors(in Buffer2DRegion pixelRegion) + { + } /// [MethodImpl(InliningOptions.ShortMethod)] @@ -60,21 +85,23 @@ public readonly IndexedImageFrame QuantizeFrame(ImageFrame sourc /// [MethodImpl(InliningOptions.ShortMethod)] - public void AddPaletteColors(Buffer2DRegion pixelRegion) + public readonly byte GetQuantizedColor(TPixel color, out TPixel match) { - } + if (this.transparencyIndex >= 0 && color.Equals(this.transparentColor)) + { + match = this.transparentColor; + return (byte)this.transparencyIndex; + } - /// - /// Allows setting the transparent index after construction. - /// - /// An explicit index at which to match transparent pixels. - public void SetTransparentIndex(int index) => this.pixelMap.SetTransparentIndex(index); + return (byte)this.pixelMap.GetClosestColor(color, out match); + } - /// - [MethodImpl(InliningOptions.ShortMethod)] - public readonly byte GetQuantizedColor(TPixel color, out TPixel match) - => (byte)this.pixelMap.GetClosestColor(color, out match); + public void SetTransparencyIndex(int transparencyIndex, TPixel transparentColor) + { + this.transparencyIndex = transparencyIndex; + this.transparentColor = transparentColor; + } /// - public void Dispose() => this.pixelMap.Dispose(); + public readonly void Dispose() => this.pixelMap.Dispose(); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerConstants.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerConstants.cs index 2bf4c6d56d..3b515e372d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerConstants.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerConstants.cs @@ -21,15 +21,30 @@ public static class QuantizerConstants public const int MaxColors = 256; /// - /// The minumim dithering scale used to adjust the amount of dither. + /// The minimum dithering scale used to adjust the amount of dither. /// public const float MinDitherScale = 0; /// - /// The max dithering scale used to adjust the amount of dither. + /// The maximum dithering scale used to adjust the amount of dither. /// public const float MaxDitherScale = 1F; + /// + /// The default threshold at which to consider a pixel transparent. + /// + public const float DefaultTransparencyThreshold = 64 / 255F; + + /// + /// The minimum threshold at which to consider a pixel transparent. + /// + public const float MinTransparencyThreshold = 0F; + + /// + /// The maximum threshold at which to consider a pixel transparent. + /// + public const float MaxTransparencyThreshold = 1F; + /// /// Gets the default dithering algorithm to use. /// diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs index a6bb265a81..16dfd5b330 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Processing.Processors.Dithering; namespace SixLabors.ImageSharp.Processing.Processors.Quantization; @@ -8,10 +9,34 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// /// Defines options for quantization. /// -public class QuantizerOptions +public class QuantizerOptions : IDeepCloneable { +#pragma warning disable IDE0032 // Use auto property private float ditherScale = QuantizerConstants.MaxDitherScale; private int maxColors = QuantizerConstants.MaxColors; + private float threshold = QuantizerConstants.DefaultTransparencyThreshold; +#pragma warning restore IDE0032 // Use auto property + + /// + /// Initializes a new instance of the class. + /// + public QuantizerOptions() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The options to clone. + private QuantizerOptions(QuantizerOptions options) + { + this.Dither = options.Dither; + this.DitherScale = options.DitherScale; + this.MaxColors = options.MaxColors; + this.TransparencyThreshold = options.TransparencyThreshold; + this.ColorMatchingMode = options.ColorMatchingMode; + this.TransparentColorMode = options.TransparentColorMode; + } /// /// Gets or sets the algorithm to apply to the output image. @@ -38,4 +63,30 @@ public int MaxColors get => this.maxColors; set => this.maxColors = Numerics.Clamp(value, QuantizerConstants.MinColors, QuantizerConstants.MaxColors); } + + /// + /// Gets or sets the color matching mode used for matching pixel values to palette colors. + /// Defaults to . + /// + public ColorMatchingMode ColorMatchingMode { get; set; } = ColorMatchingMode.Coarse; + + /// + /// Gets or sets the threshold at which to consider a pixel transparent. Range 0..1. + /// Defaults to . + /// + public float TransparencyThreshold + { + get => this.threshold; + set => this.threshold = Numerics.Clamp(value, QuantizerConstants.MinTransparencyThreshold, QuantizerConstants.MaxTransparencyThreshold); + } + + /// + /// Gets or sets the transparent color mode used for handling transparent colors + /// when not using thresholding. + /// Defaults to . + /// + public TransparentColorMode TransparentColorMode { get; set; } = TransparentColorMode.Preserve; + + /// + public QuantizerOptions DeepClone() => new(this); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs index 6d2200b8a7..e121aff90b 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs @@ -1,7 +1,11 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Buffers; +using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -14,6 +18,130 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// public static class QuantizerUtilities { + /// + /// Performs a deep clone the instance and optionally mutates the clone. + /// + /// The instance to clone. + /// An optional delegate to mutate the cloned instance. + /// The cloned instance. + public static QuantizerOptions DeepClone(this QuantizerOptions options, Action? mutate) + { + QuantizerOptions clone = options.DeepClone(); + mutate?.Invoke(clone); + return clone; + } + + /// + /// Determines if transparent pixels can be replaced based on the specified color mode and pixel type. + /// + /// The type of the pixel. + /// The alpha threshold used to determine if a pixel is transparent. + /// Returns true if transparent pixels can be replaced; otherwise, false. + public static bool ShouldReplacePixelsByAlphaThreshold(float threshold) + where TPixel : unmanaged, IPixel + => threshold > 0 && TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Unassociated; + + /// + /// Replaces pixels in a span with fully transparent pixels based on an alpha threshold. + /// + /// A span of color vectors that will be checked for transparency and potentially modified. + /// The alpha threshold used to determine if a pixel is transparent. + public static void ReplacePixelsByAlphaThreshold(Span source, float threshold) + { + if (Vector512.IsHardwareAccelerated && source.Length >= 4) + { + Vector512 threshold512 = Vector512.Create(threshold); + Span> source512 = MemoryMarshal.Cast>(source); + for (int i = 0; i < source512.Length; i++) + { + ref Vector512 v = ref source512[i]; + + // Do `vector < threshold` + Vector512 mask = Vector512.LessThan(v, threshold512); + + // Replicate the result for W to all elements (is AllBitsSet if the W was less than threshold and Zero otherwise) + mask = Vector512.Shuffle(mask, Vector512.Create(3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15)); + + // Use the mask to select the replacement vector + // (replacement & mask) | (v512 & ~mask) + v = Vector512.ConditionalSelect(mask, Vector512.Zero, v); + } + + int m = Numerics.Modulo4(source.Length); + if (m != 0) + { + for (int i = source.Length - m; i < source.Length; i++) + { + if (source[i].W < threshold) + { + source[i] = Vector4.Zero; + } + } + } + } + else if (Vector256.IsHardwareAccelerated && source.Length >= 2) + { + Vector256 threshold256 = Vector256.Create(threshold); + Span> source256 = MemoryMarshal.Cast>(source); + for (int i = 0; i < source256.Length; i++) + { + ref Vector256 v = ref source256[i]; + + // Do `vector < threshold` + Vector256 mask = Vector256.LessThan(v, threshold256); + + // Replicate the result for W to all elements (is AllBitsSet if the W was less than threshold and Zero otherwise) + mask = Vector256.Shuffle(mask, Vector256.Create(3, 3, 3, 3, 7, 7, 7, 7)); + + // Use the mask to select the replacement vector + // (replacement & mask) | (v256 & ~mask) + v = Vector256.ConditionalSelect(mask, Vector256.Zero, v); + } + + int m = Numerics.Modulo2(source.Length); + if (m != 0) + { + for (int i = source.Length - m; i < source.Length; i++) + { + if (source[i].W < threshold) + { + source[i] = Vector4.Zero; + } + } + } + } + else if (Vector128.IsHardwareAccelerated) + { + Vector128 threshold128 = Vector128.Create(threshold); + + for (int i = 0; i < source.Length; i++) + { + ref Vector4 v = ref source[i]; + Vector128 v128 = v.AsVector128(); + + // Do `vector < threshold` + Vector128 mask = Vector128.LessThan(v128, threshold128); + + // Replicate the result for W to all elements (is AllBitsSet if the W was less than threshold 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, Vector128.Zero, v128).AsVector4(); + } + } + else + { + for (int i = 0; i < source.Length; i++) + { + if (source[i].W < threshold) + { + source[i] = Vector4.Zero; + } + } + } + } + /// /// Helper method for throwing an exception when a frame quantizer palette has /// been requested but not built yet. @@ -21,12 +149,13 @@ public static class QuantizerUtilities /// The pixel format. /// The frame quantizer palette. /// - /// The palette has not been built via + /// The palette has not been built via /// + [MethodImpl(InliningOptions.ColdPath)] public static void CheckPaletteState(in ReadOnlyMemory palette) where TPixel : unmanaged, IPixel { - if (palette.Equals(default)) + if (palette.IsEmpty) { throw new InvalidOperationException("Frame Quantizer palette has not been built."); } @@ -54,8 +183,7 @@ public static IndexedImageFrame BuildPaletteAndQuantizeFrame( Rectangle interest = Rectangle.Intersect(source.Bounds, bounds); Buffer2DRegion region = source.PixelBuffer.GetRegion(interest); - // Collect the palette. Required before the second pass runs. - quantizer.AddPaletteColors(region); + quantizer.AddPaletteColors(in region); return quantizer.QuantizeFrame(source, bounds); } @@ -112,39 +240,10 @@ public static void BuildPalette( 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 { - 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 + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) { - foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) - { - quantizer.AddPaletteColors(region); - } + quantizer.AddPaletteColors(in region); } } @@ -160,43 +259,75 @@ public static void BuildPalette( IPixelSamplingStrategy pixelSamplingStrategy, ImageFrame source) where TPixel : unmanaged, IPixel - => quantizer.BuildPalette(source.Configuration, TransparentColorMode.Preserve, pixelSamplingStrategy, source); + { + foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + { + quantizer.AddPaletteColors(in region); + } + } - /// - /// 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) + internal static void AddPaletteColors( + ref TFrameQuantizer quantizer, + in Buffer2DRegion source, + in TDelegate rowDelegate) + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel + where TPixel2 : unmanaged, IPixel + where TDelegate : struct, IQuantizingPixelRowDelegate { - if (EncodingUtilities.ShouldClearTransparentPixels(mode)) + Configuration configuration = quantizer.Configuration; + float threshold = quantizer.Options.TransparencyThreshold; + TransparentColorMode mode = quantizer.Options.TransparentColorMode; + + using IMemoryOwner delegateRowOwner = configuration.MemoryAllocator.Allocate(source.Width); + Span delegateRow = delegateRowOwner.Memory.Span; + + bool replaceByThreshold = ShouldReplacePixelsByAlphaThreshold(threshold); + bool replaceTransparent = EncodingUtilities.ShouldReplaceTransparentPixels(mode); + + if (replaceByThreshold || replaceTransparent) { - foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + using IMemoryOwner vectorRowOwner = configuration.MemoryAllocator.Allocate(source.Width); + Span vectorRow = vectorRowOwner.Memory.Span; + + if (replaceByThreshold) { - using Buffer2D clone = region.Buffer.CloneRegion(configuration, region.Rectangle); - quantizer.AddPaletteColors(clone.GetRegion()); + for (int y = 0; y < source.Height; y++) + { + Span sourceRow = source.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + ReplacePixelsByAlphaThreshold(vectorRow, threshold); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, delegateRow, PixelConversionModifiers.Scale); + rowDelegate.Invoke(delegateRow, y); + } + } + else + { + for (int y = 0; y < source.Height; y++) + { + Span sourceRow = source.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + EncodingUtilities.ReplaceTransparentPixels(vectorRow); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, delegateRow, PixelConversionModifiers.Scale); + rowDelegate.Invoke(delegateRow, y); + } } } else { - foreach (Buffer2DRegion region in pixelSamplingStrategy.EnumeratePixelRegions(source)) + for (int y = 0; y < source.Height; y++) { - quantizer.AddPaletteColors(region); + Span sourceRow = source.DangerousGetRowSpan(y); + PixelOperations.Instance.To(configuration, sourceRow, delegateRow); + rowDelegate.Invoke(delegateRow, y); } } } - [MethodImpl(InliningOptions.ShortMethod)] private static void SecondPass( ref TFrameQuantizer quantizer, ImageFrame source, @@ -205,28 +336,111 @@ private static void SecondPass( where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { + float threshold = quantizer.Options.TransparencyThreshold; + bool replaceByThreshold = ShouldReplacePixelsByAlphaThreshold(threshold); + + TransparentColorMode mode = quantizer.Options.TransparentColorMode; + bool replaceTransparent = EncodingUtilities.ShouldReplaceTransparentPixels(mode); + IDither? dither = quantizer.Options.Dither; Buffer2D sourceBuffer = source.PixelBuffer; + Buffer2DRegion region = sourceBuffer.GetRegion(bounds); + + Configuration configuration = quantizer.Configuration; + using IMemoryOwner vectorOwner = configuration.MemoryAllocator.Allocate(region.Width); + Span vectorRow = vectorOwner.Memory.Span; if (dither is null) { - int offsetY = bounds.Top; - int offsetX = bounds.Left; + using IMemoryOwner quantizingRowOwner = configuration.MemoryAllocator.Allocate(region.Width); + Span quantizingRow = quantizingRowOwner.Memory.Span; + + // This is NOT a clone so we DO NOT write back to the source. + if (replaceByThreshold || replaceTransparent) + { + if (replaceByThreshold) + { + for (int y = 0; y < region.Height; y++) + { + Span sourceRow = region.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + ReplacePixelsByAlphaThreshold(vectorRow, threshold); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, quantizingRow, PixelConversionModifiers.Scale); + + Span destinationRow = destination.GetWritablePixelRowSpanUnsafe(y); + for (int x = 0; x < destinationRow.Length; x++) + { + destinationRow[x] = quantizer.GetQuantizedColor(quantizingRow[x], out TPixel _); + } + } + } + else + { + for (int y = 0; y < region.Height; y++) + { + Span sourceRow = region.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + EncodingUtilities.ReplaceTransparentPixels(vectorRow); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, quantizingRow, PixelConversionModifiers.Scale); + + Span destinationRow = destination.GetWritablePixelRowSpanUnsafe(y); + for (int x = 0; x < destinationRow.Length; x++) + { + destinationRow[x] = quantizer.GetQuantizedColor(quantizingRow[x], out TPixel _); + } + } + } - for (int y = 0; y < destination.Height; y++) + return; + } + + for (int y = 0; y < region.Height; y++) { - ReadOnlySpan sourceRow = sourceBuffer.DangerousGetRowSpan(y + offsetY); + ReadOnlySpan sourceRow = region.DangerousGetRowSpan(y); Span destinationRow = destination.GetWritablePixelRowSpanUnsafe(y); for (int x = 0; x < destinationRow.Length; x++) { - destinationRow[x] = Unsafe.AsRef(in quantizer).GetQuantizedColor(sourceRow[x + offsetX], out TPixel _); + destinationRow[x] = quantizer.GetQuantizedColor(sourceRow[x], out TPixel _); } } return; } + // This is a clone so we write back to the source. + if (replaceByThreshold || replaceTransparent) + { + if (replaceByThreshold) + { + for (int y = 0; y < region.Height; y++) + { + Span sourceRow = region.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + ReplacePixelsByAlphaThreshold(vectorRow, threshold); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, sourceRow, PixelConversionModifiers.Scale); + } + } + else + { + for (int y = 0; y < region.Height; y++) + { + Span sourceRow = region.DangerousGetRowSpan(y); + PixelOperations.Instance.ToVector4(configuration, sourceRow, vectorRow, PixelConversionModifiers.Scale); + + EncodingUtilities.ReplaceTransparentPixels(vectorRow); + + PixelOperations.Instance.FromVector4Destructive(configuration, vectorRow, sourceRow, PixelConversionModifiers.Scale); + } + } + } + dither.ApplyQuantizationDither(ref quantizer, source, destination, bounds); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs index 604cae6681..fa1763367c 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WebSafePaletteQuantizer.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// /// A palette quantizer consisting of web safe colors as defined in the CSS Color Module Level 4. /// -public class WebSafePaletteQuantizer : PaletteQuantizer +public sealed class WebSafePaletteQuantizer : PaletteQuantizer { /// /// Initializes a new instance of the class. diff --git a/src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs index 023ee7f2e0..cd7b80e81d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WernerPaletteQuantizer.cs @@ -7,7 +7,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; /// A palette quantizer consisting of colors as defined in the original second edition of Werner’s Nomenclature of Colours 1821. /// The hex codes were collected and defined by Nicholas Rougeux /// -public class WernerPaletteQuantizer : PaletteQuantizer +public sealed class WernerPaletteQuantizer : PaletteQuantizer { /// /// Initializes a new instance of the class. diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index ba2ab825ad..03d6ac0da6 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -43,30 +43,10 @@ internal struct WuQuantizer : IQuantizer // The following two variables determine the amount of bits to preserve when calculating the histogram. // Reducing the value of these numbers the granularity of the color maps produced, making it much faster // and using much less memory but potentially less accurate. Current results are very good though! - - /// - /// The index bits. 6 in original code. - /// private const int IndexBits = 5; - - /// - /// The index alpha bits. 3 in original code. - /// private const int IndexAlphaBits = 5; - - /// - /// The index count. - /// private const int IndexCount = (1 << IndexBits) + 1; - - /// - /// The index alpha count. - /// private const int IndexAlphaCount = (1 << IndexAlphaBits) + 1; - - /// - /// The table length. Now 1185921. originally 2471625. - /// private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount; private readonly IMemoryOwner momentsOwner; @@ -75,14 +55,14 @@ internal struct WuQuantizer : IQuantizer private ReadOnlyMemory palette; private int maxColors; private readonly Box[] colorCube; - private EuclideanPixelMap? pixelMap; + private PixelMap? pixelMap; private readonly bool isDithering; private bool isDisposed; /// /// Initializes a new instance of the struct. /// - /// The configuration which allows altering default behaviour or extending the library. + /// The configuration which allows altering default behavior or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] public WuQuantizer(Configuration configuration, QuantizerOptions options) @@ -101,7 +81,7 @@ public WuQuantizer(Configuration configuration, QuantizerOptions options) this.isDisposed = false; this.pixelMap = default; this.palette = default; - this.isDithering = this.isDithering = this.Options.Dither is not null; + this.isDithering = this.Options.Dither is not null; } /// @@ -111,57 +91,71 @@ public WuQuantizer(Configuration configuration, QuantizerOptions options) public QuantizerOptions Options { get; } /// - public readonly ReadOnlyMemory Palette + public ReadOnlyMemory Palette { get { - QuantizerUtilities.CheckPaletteState(in this.palette); + if (this.palette.IsEmpty) + { + this.ResolvePalette(); + QuantizerUtilities.CheckPaletteState(in this.palette); + } + return this.palette; } } /// - public void AddPaletteColors(Buffer2DRegion pixelRegion) + public readonly void AddPaletteColors(in Buffer2DRegion pixelRegion) + { + PixelRowDelegate pixelRowDelegate = new(ref Unsafe.AsRef(in this)); + QuantizerUtilities.AddPaletteColors, TPixel, Rgba32, PixelRowDelegate>( + ref Unsafe.AsRef(in this), + in pixelRegion, + in pixelRowDelegate); + } + + /// + /// Once all histogram data has been accumulated, this method computes the moments, + /// splits the color cube, and resolves the final palette from the accumulated histogram. + /// + private void ResolvePalette() { - // TODO: Something is destroying the existing palette when adding new colors. - // When the QuantizingImageEncoder.PixelSamplingStrategy is DefaultPixelSamplingStrategy - // this leads to performance issues + the palette is not preserved. - // https://github.com/SixLabors/ImageSharp/issues/2498 - this.Build3DHistogram(pixelRegion); + // Calculate the cumulative moments from the accumulated histogram. this.Get3DMoments(this.memoryAllocator); + + // Partition the histogram into color cubes. this.BuildCube(); - // Slice again since maxColors has been updated since the buffer was created. + // Compute the palette colors from the resolved cubes. Span paletteSpan = this.paletteOwner.GetSpan()[..this.maxColors]; ReadOnlySpan momentsSpan = this.momentsOwner.GetSpan(); + + float transparencyThreshold = this.Options.TransparencyThreshold; for (int k = 0; k < paletteSpan.Length; k++) { this.Mark(ref this.colorCube[k], (byte)k); - Moment moment = Volume(ref this.colorCube[k], momentsSpan); - if (moment.Weight > 0) { - paletteSpan[k] = TPixel.FromScaledVector4(moment.Normalize()); + Vector4 normalized = moment.Normalize(); + if (normalized.W < transparencyThreshold) + { + normalized = Vector4.Zero; + } + + paletteSpan[k] = TPixel.FromScaledVector4(normalized); } } - ReadOnlyMemory result = this.paletteOwner.Memory[..paletteSpan.Length]; - if (this.isDithering) + // Update the palette to the new computed colors. + this.palette = this.paletteOwner.Memory[..paletteSpan.Length]; + + // Create the pixel map if dithering is enabled. + if (this.isDithering && this.pixelMap is null) { - // When called multiple times by QuantizerUtilities.BuildPalette - // this prevents memory churn caused by reallocation. - if (this.pixelMap is null) - { - this.pixelMap = new EuclideanPixelMap(this.Configuration, result); - } - else - { - this.pixelMap.Clear(result); - } + this.pixelMap = PixelMapFactory.Create(this.Configuration, this.palette, this.Options.ColorMatchingMode); } - - this.palette = result; } /// @@ -172,6 +166,9 @@ public readonly IndexedImageFrame QuantizeFrame(ImageFrame sourc /// public readonly byte GetQuantizedColor(TPixel color, out TPixel match) { + // Due to the addition of new colors by dithering that are not part of the original histogram, + // the color cube might not match the correct color. + // In this case, we must use the pixel map to get the closest color. if (this.isDithering) { return (byte)this.pixelMap!.GetClosestColor(color, out match); @@ -188,7 +185,7 @@ public readonly byte GetQuantizedColor(TPixel color, out TPixel match) ReadOnlySpan tagSpan = this.tagsOwner.GetSpan(); byte index = tagSpan[GetPaletteIndex(r + 1, g + 1, b + 1, a + 1)]; ref TPixel paletteRef = ref MemoryMarshal.GetReference(this.palette.Span); - match = Unsafe.Add(ref paletteRef, index); + match = Unsafe.Add(ref paletteRef, (nuint)index); return index; } @@ -359,31 +356,19 @@ private static Moment Top(ref Box cube, int direction, int position, ReadOnlySpa /// /// Builds a 3-D color histogram of counts, r/g/b, c^2. /// - /// The source pixel data. - private readonly void Build3DHistogram(Buffer2DRegion source) + /// The source pixel data. + private readonly void Build3DHistogram(ReadOnlySpan pixels) { - Span momentSpan = this.momentsOwner.GetSpan(); - - // Build up the 3-D color histogram - using IMemoryOwner buffer = this.memoryAllocator.Allocate(source.Width); - Span bufferSpan = buffer.GetSpan(); - - for (int y = 0; y < source.Height; y++) + Span moments = this.momentsOwner.GetSpan(); + for (int x = 0; x < pixels.Length; x++) { - Span row = source.DangerousGetRowSpan(y); - PixelOperations.Instance.ToRgba32(this.Configuration, row, bufferSpan); - - for (int x = 0; x < bufferSpan.Length; x++) - { - Rgba32 rgba = bufferSpan[x]; - - int r = (rgba.R >> (8 - IndexBits)) + 1; - int g = (rgba.G >> (8 - IndexBits)) + 1; - int b = (rgba.B >> (8 - IndexBits)) + 1; - int a = (rgba.A >> (8 - IndexAlphaBits)) + 1; + Rgba32 rgba = pixels[x]; + int r = (rgba.R >> (8 - IndexBits)) + 1; + int g = (rgba.G >> (8 - IndexBits)) + 1; + int b = (rgba.B >> (8 - IndexBits)) + 1; + int a = (rgba.A >> (8 - IndexAlphaBits)) + 1; - momentSpan[GetPaletteIndex(r, g, b, a)] += rgba; - } + moments[GetPaletteIndex(r, g, b, a)] += rgba; } } @@ -895,4 +880,13 @@ public override readonly int GetHashCode() return hash.ToHashCode(); } } + + private readonly struct PixelRowDelegate : IQuantizingPixelRowDelegate + { + private readonly WuQuantizer quantizer; + + public PixelRowDelegate(ref WuQuantizer quantizer) => this.quantizer = quantizer; + + public void Invoke(ReadOnlySpan row, int rowIndex) => this.quantizer.Build3DHistogram(row); + } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor{TPixel}.cs index bdfac00366..a8455a06ea 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/TransformProcessor{TPixel}.cs @@ -3,6 +3,7 @@ using SixLabors.ImageSharp.PixelFormats; +// TODO: DO we need this class? namespace SixLabors.ImageSharp.Processing.Processors.Transforms; /// @@ -22,18 +23,4 @@ protected TransformProcessor(Configuration configuration, Image source, : base(configuration, source, sourceRectangle) { } - - /// - protected override void AfterFrameApply(ImageFrame source, ImageFrame destination) - { - base.AfterFrameApply(source, destination); - destination.Metadata.AfterFrameApply(source, destination); - } - - /// - protected override void AfterImageApply(Image destination) - { - base.AfterImageApply(destination); - destination.Metadata.AfterImageApply(destination); - } } diff --git a/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeEncodeGif.cs b/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeEncodeGif.cs new file mode 100644 index 0000000000..3238e8dac2 --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Codecs/Gif/DecodeEncodeGif.cs @@ -0,0 +1,59 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Drawing.Imaging; +using BenchmarkDotNet.Attributes; +using SixLabors.ImageSharp.Formats.Gif; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Quantization; +using SixLabors.ImageSharp.Tests; +using SDImage = System.Drawing.Image; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs; + +public abstract class DecodeEncodeGif +{ + private MemoryStream outputStream; + + protected abstract GifEncoder Encoder { get; } + + [Params(TestImages.Gif.Leo, TestImages.Gif.Cheers)] + public string TestImage { get; set; } + + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + + [GlobalSetup] + public void Setup() => this.outputStream = new MemoryStream(); + + [GlobalCleanup] + public void Cleanup() => this.outputStream.Close(); + + [Benchmark(Baseline = true)] + public void SystemDrawing() + { + this.outputStream.Position = 0; + using SDImage image = SDImage.FromFile(this.TestImageFullPath); + image.Save(this.outputStream, ImageFormat.Gif); + } + + [Benchmark] + public void ImageSharp() + { + this.outputStream.Position = 0; + using Image image = Image.Load(this.TestImageFullPath); + image.SaveAsGif(this.outputStream, this.Encoder); + } +} + +public class DecodeEncodeGif_DefaultEncoder : DecodeEncodeGif +{ + protected override GifEncoder Encoder => new(); +} + +public class DecodeEncodeGif_CoarsePaletteEncoder : DecodeEncodeGif +{ + protected override GifEncoder Encoder => new() + { + Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse }) + }; +} diff --git a/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs b/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs index beedbbe07c..b8f8f78517 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/Gif/EncodeGif.cs @@ -3,6 +3,7 @@ using System.Drawing.Imaging; using BenchmarkDotNet.Attributes; +using ImageMagick; using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; @@ -12,21 +13,17 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs; -[Config(typeof(Config.Short))] -public class EncodeGif +public abstract class EncodeGif { // System.Drawing needs this. private FileStream bmpStream; private SDImage bmpDrawing; private Image bmpCore; + private MagickImageCollection magickImage; - // Try to get as close to System.Drawing's output as possible - private readonly GifEncoder encoder = new() - { - Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4 }) - }; + protected abstract GifEncoder Encoder { get; } - [Params(TestImages.Bmp.Car, TestImages.Png.Rgb48Bpp)] + [Params(TestImages.Gif.Leo, TestImages.Gif.Cheers)] public string TestImage { get; set; } [GlobalSetup] @@ -34,10 +31,14 @@ public void ReadImages() { if (this.bmpStream == null) { - this.bmpStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage)); + string filePath = Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + this.bmpStream = File.OpenRead(filePath); this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = SDImage.FromStream(this.bmpStream); + + this.bmpStream.Position = 0; + this.magickImage = new MagickImageCollection(this.bmpStream); } } @@ -48,6 +49,7 @@ public void Cleanup() this.bmpStream = null; this.bmpCore.Dispose(); this.bmpDrawing.Dispose(); + this.magickImage.Dispose(); } [Benchmark(Baseline = true, Description = "System.Drawing Gif")] @@ -61,6 +63,26 @@ public void GifSystemDrawing() public void GifImageSharp() { using MemoryStream memoryStream = new(); - this.bmpCore.SaveAsGif(memoryStream, this.encoder); + this.bmpCore.SaveAsGif(memoryStream, this.Encoder); } + + [Benchmark(Description = "Magick.NET Gif")] + public void GifMagickNet() + { + using MemoryStream ms = new(); + this.magickImage.Write(ms, MagickFormat.Gif); + } +} + +public class EncodeGif_DefaultEncoder : EncodeGif +{ + protected override GifEncoder Encoder => new(); +} + +public class EncodeGif_CoarsePaletteEncoder : EncodeGif +{ + protected override GifEncoder Encoder => new() + { + Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = KnownDitherings.Bayer4x4, ColorMatchingMode = ColorMatchingMode.Coarse }) + }; } diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index bc6eeedcbe..6593b8df70 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -34,6 +34,41 @@ public void Decode_VerifyAllFrames(TestImageProvider provider) image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact); } + [Theory] + [WithFile(TestImages.Gif.AnimatedLoop, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.AnimatedLoopInterlaced, PixelTypes.Rgba32)] + public void Decode_Animated(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + image.DebugSaveMultiFrame(provider); + image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact); + } + + [Theory] + [WithFile(TestImages.Gif.AnimatedTransparentNoRestore, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.AnimatedTransparentRestorePrevious, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.AnimatedTransparentLoop, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.AnimatedTransparentFirstFrameRestorePrev, PixelTypes.Rgba32)] + public void Decode_Animated_WithTransparency(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + image.DebugSaveMultiFrame(provider); + image.CompareToReferenceOutputMultiFrame(provider, ImageComparer.Exact); + } + + [Theory] + [WithFile(TestImages.Gif.StaticNontransparent, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.StaticTransparent, PixelTypes.Rgba32)] + public void Decode_Static_No_Animation(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + image.DebugSave(provider); + image.CompareFirstFrameToReferenceOutput(ImageComparer.Exact, provider); + } + [Theory] [WithFile(TestImages.Gif.Issues.Issue2450_A, PixelTypes.Rgba32)] [WithFile(TestImages.Gif.Issues.Issue2450_B, PixelTypes.Rgba32)] diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index f12f66186e..44ed5e38dd 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -56,7 +56,7 @@ public void EncodeGeneratedPatterns(TestImageProvider provider, { // Use the palette quantizer without dithering to ensure results // are consistent - Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = null }) + Quantizer = new WebSafePaletteQuantizer(new QuantizerOptions { Dither = null, TransparencyThreshold = 0 }) }; // Always save as we need to compare the encoded output. @@ -419,4 +419,21 @@ public void Encode_WithTransparentColorBehaviorClear_Works() } }); } + + [Theory] + [WithFile(TestImages.Gif.Issues.Issue2866, PixelTypes.Rgba32)] + public void GifEncoder_CanDecode_AndEncode_Issue2866(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + + // Save the image for visual inspection. + provider.Utility.SaveTestOutputFile(image, "gif", new GifEncoder(), "animated"); + + // Now compare the debug output with the reference output. + // We do this because the gif encoding is lossy and encoding will lead to differences in the 10s of percent. + // From the unencoded image, we can see that the image is visually the same. + static bool Predicate(int i, int _) => i % 8 == 0; // Image has many frames, only compare a selection of them. + image.CompareDebugOutputToReferenceOutputMultiFrame(provider, ImageComparer.Exact, extension: "gif", predicate: Predicate); + } } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs index bf94e1d489..69c6317a75 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Cur/CurEncoderTests.cs @@ -4,6 +4,7 @@ using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Cur; using SixLabors.ImageSharp.Formats.Ico; +using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using static SixLabors.ImageSharp.Tests.TestImages.Cur; @@ -49,8 +50,8 @@ public void CanConvertFromIco(TestImageProvider provider) using Image encoded = Image.Load(memStream); encoded.DebugSaveMultiFrame(provider); - // Despite preservation of the palette. The process can still be lossy - encoded.CompareToOriginalMultiFrame(provider, ImageComparer.TolerantPercentage(.23f), IcoDecoder.Instance); + // Color palettes are not preserved when transcoding. + encoded.CompareToOriginalMultiFrame(provider, ImageComparer.TolerantPercentage(.05F), IcoDecoder.Instance); for (int i = 0; i < image.Frames.Count; i++) { diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index b4995d77b6..b0d0563ccb 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -407,6 +407,7 @@ public void Encode_WithTransparentColorBehaviorClear_Works(PngColorType colorTyp [WithFile(TestImages.Png.APng, PixelTypes.Rgba32)] [WithFile(TestImages.Png.DefaultNotAnimated, PixelTypes.Rgba32)] [WithFile(TestImages.Png.FrameOffset, PixelTypes.Rgba32)] + [WithFile(TestImages.Png.Issue2882, PixelTypes.Rgba32)] public void Encode_APng(TestImageProvider provider) where TPixel : unmanaged, IPixel { @@ -419,8 +420,8 @@ public void Encode_APng(TestImageProvider provider) using Image output = Image.Load(memStream); - // some loss from original, due to compositing - ImageComparer.TolerantPercentage(0.01f).VerifySimilarity(output, image); + // Some loss from original, due to palette matching accuracy. + ImageComparer.TolerantPercentage(0.172F).VerifySimilarity(output, image); Assert.Equal(image.Frames.Count, output.Frames.Count); @@ -443,6 +444,7 @@ public void Encode_APng(TestImageProvider provider) [Theory] [WithFile(TestImages.Gif.Leo, PixelTypes.Rgba32)] + [WithFile(TestImages.Gif.Issues.Issue2866, PixelTypes.Rgba32)] public void Encode_AnimatedFormatTransform_FromGif(TestImageProvider provider) where TPixel : unmanaged, IPixel { @@ -453,26 +455,30 @@ public void Encode_AnimatedFormatTransform_FromGif(TestImageProvider image = provider.GetImage(GifDecoder.Instance); + // Save the image for visual inspection. + provider.Utility.SaveTestOutputFile(image, "png", PngEncoder, "animated"); + + // Now compare the debug output with the reference output. + // We do this because the transcoding encoding is lossy and encoding will lead to differences. + // From the unencoded image, we can see that the image is visually the same. + static bool Predicate(int i, int _) => i % 8 == 0; // Image has many frames, only compare a selection of them. + image.CompareDebugOutputToReferenceOutputMultiFrame(provider, ImageComparer.Exact, extension: "png", encoder: PngEncoder, predicate: Predicate); + + // Now save the image and load it again to compare the metadata. using MemoryStream memStream = new(); image.Save(memStream, PngEncoder); memStream.Position = 0; - using Image output = Image.Load(memStream); - - // TODO: Find a better way to compare. - // The image has been visually checked but the quantization pattern used in the png encoder - // means we cannot use an exact comparison nor replicate using the quantizing processor. - ImageComparer.TolerantPercentage(0.613f).VerifySimilarity(output, image); - + using Image encoded = Image.Load(memStream); GifMetadata gif = image.Metadata.GetGifMetadata(); - PngMetadata png = output.Metadata.GetPngMetadata(); + PngMetadata png = encoded.Metadata.GetPngMetadata(); Assert.Equal(gif.RepeatCount, png.RepeatCount); for (int i = 0; i < image.Frames.Count; i++) { GifFrameMetadata gifF = image.Frames[i].Metadata.GetGifMetadata(); - PngFrameMetadata pngF = output.Frames[i].Metadata.GetPngMetadata(); + PngFrameMetadata pngF = encoded.Frames[i].Metadata.GetPngMetadata(); Assert.Equal(gifF.FrameDelay, (int)(pngF.FrameDelay.ToDouble() * 100)); @@ -641,7 +647,7 @@ public void Issue2469_Quantized_Encode_Artifacts(TestImageProvider(TestImageProvider provider) @@ -657,6 +663,39 @@ public void Issue2668_Quantized_Encode_Alpha(TestImageProvider p encoded.CompareToReferenceOutput(ImageComparer.Exact, provider); } + [Fact] + public void Issue_2862() + { + // Create a grayscale palette (or any other palette with colors that are very close to each other): + Rgba32[] palette = [.. Enumerable.Range(0, 256).Select(i => new Rgba32((byte)i, (byte)i, (byte)i))]; + + using Image image = new(254, 4); + for (int y = 0; y < image.Height; y++) + { + for (int x = 0; x < image.Width; x++) + { + image[x, y] = palette[x]; + } + } + + PaletteQuantizer quantizer = new( + palette.Select(Color.FromPixel).ToArray(), + new QuantizerOptions() { ColorMatchingMode = ColorMatchingMode.Hybrid }); + + using MemoryStream ms = new(); + image.Save(ms, new PngEncoder + { + ColorType = PngColorType.Palette, + BitDepth = PngBitDepth.Bit8, + Quantizer = quantizer + }); + + ms.Position = 0; + + using Image encoded = Image.Load(ms); + ImageComparer.Exact.VerifySimilarity(image, encoded); + } + private static void TestPngEncoderCore( TestImageProvider provider, PngColorType pngColorType, diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpCommonUtilsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpCommonUtilsTests.cs index 80302db393..1491cd13cf 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpCommonUtilsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpCommonUtilsTests.cs @@ -106,7 +106,7 @@ private static void RunCheckNoneOpaqueWithNoneOpaquePixelsTest() 174, 183, 189, 255, 148, 158, 158, 255, }; - Span row = MemoryMarshal.Cast((Span)rowBytes); + ReadOnlySpan row = MemoryMarshal.Cast(rowBytes); bool noneOpaque; for (int length = 8; length < row.Length; length += 8) @@ -188,7 +188,7 @@ private static void RunCheckNoneOpaqueWithOpaquePixelsTest() 174, 183, 189, 255, 148, 158, 158, 255, }; - Span row = MemoryMarshal.Cast((Span)rowBytes); + ReadOnlySpan row = MemoryMarshal.Cast(rowBytes); bool noneOpaque; for (int length = 8; length < row.Length; length += 8) diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs index 657ab25546..adabb727d8 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs @@ -450,6 +450,22 @@ public void WebpDecoder_CanDecode_Issue2670(TestImageProvider pr image.CompareToOriginal(provider, ReferenceDecoder); } + // https://github.com/SixLabors/ImageSharp/issues/2866 + [Theory] + [WithFile(Lossy.Issue2866, PixelTypes.Rgba32)] + public void WebpDecoder_CanDecode_Issue2866(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // Web + using Image image = provider.GetImage( + WebpDecoder.Instance, + new WebpDecoderOptions() { BackgroundColorHandling = BackgroundColorHandling.Ignore }); + + // We can't use the reference decoder here. + // It creates frames of different size without blending the frames. + image.DebugSave(provider, extension: "webp", encoder: new WebpEncoder()); + } + [Theory] [WithFile(Lossless.LossLessCorruptImage3, PixelTypes.Rgba32)] public void WebpDecoder_ThrowImageFormatException_OnInvalidImages(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs index f82fa65df8..af6f7eea17 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs @@ -8,6 +8,8 @@ using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Quantization; using SixLabors.ImageSharp.Tests.TestUtilities; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; @@ -110,6 +112,63 @@ public void Encode_AnimatedFormatTransform_FromGif(TestImageProvider(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + WebpEncoder encoder = new() + { + FileFormat = WebpFileFormatType.Lossless + }; + + QuantizerOptions options = new() + { + TransparencyThreshold = 128 / 255F + }; + + // First save as gif to gif using different quantizers with default options. + // Alpha thresholding is 64/255F. + GifEncoder gifEncoder = new() + { + Quantizer = new OctreeQuantizer(options) + }; + provider.Utility.SaveTestOutputFile(image, "gif", gifEncoder, "octree"); + + gifEncoder = new GifEncoder() + { + Quantizer = new WuQuantizer(options) + }; + provider.Utility.SaveTestOutputFile(image, "gif", gifEncoder, "wu"); + + // Now clone and quantize the image using the same quantizers without alpha thresholding and save as webp. + options = new() + { + TransparencyThreshold = 0 + }; + + using Image cloned1 = image.Clone(); + cloned1.Mutate(c => c.Quantize(new OctreeQuantizer(options))); + provider.Utility.SaveTestOutputFile(cloned1, "webp", encoder, "octree"); + + using Image cloned2 = image.Clone(); + cloned2.Mutate(c => c.Quantize(new WuQuantizer(options))); + provider.Utility.SaveTestOutputFile(cloned2, "webp", encoder, "wu"); + + // Now blend the images with a blue background and save as webp. + using Image background1 = new(image.Width, image.Height, Color.White.ToPixel()); + background1.Mutate(c => c.DrawImage(cloned1, 1)); + provider.Utility.SaveTestOutputFile(background1, "webp", encoder, "octree-blended"); + + using Image background2 = new(image.Width, image.Height, Color.White.ToPixel()); + background2.Mutate(c => c.DrawImage(cloned2, 1)); + provider.Utility.SaveTestOutputFile(background2, "webp", encoder, "wu-blended"); + } + [Theory] [WithFile(TestImages.Png.APng, PixelTypes.Rgba32)] public void Encode_AnimatedFormatTransform_FromPng(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs index 74f2fc3b42..28a7c49e51 100644 --- a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs +++ b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs @@ -79,7 +79,7 @@ public void Palette256() } Configuration config = Configuration.Default; - WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null, TransparencyThreshold = 0 }); ImageFrame frame = image.Frames.RootFrame; @@ -152,7 +152,7 @@ private static void TestScale(Func pixelBuilder) } Configuration config = Configuration.Default; - WuQuantizer quantizer = new(new QuantizerOptions { Dither = null }); + WuQuantizer quantizer = new(new QuantizerOptions { Dither = null, TransparencyThreshold = 0 }); ImageFrame frame = image.Frames.RootFrame; using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config)) diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 53f512df8b..8622dc37ad 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -76,6 +76,7 @@ public static class Png public const string FrameOffset = "Png/animated/frame-offset.png"; public const string DefaultNotAnimated = "Png/animated/default-not-animated.png"; public const string Issue2666 = "Png/issues/Issue_2666.png"; + public const string Issue2882 = "Png/issues/Issue_2882.png"; // Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html public const string Filter0 = "Png/filter0.png"; @@ -509,6 +510,31 @@ public static class Gif public const string Bit18RGBCube = "Gif/18-bit_RGB_Cube.gif"; public const string Global256NoTrans = "Gif/global-256-no-trans.gif"; + // Test images from: https://github.com/peterdn/gif-test-suite.git + // Animated gif with 4 frames, looping forever, no transparency. + public const string AnimatedLoop = "Gif/animated_loop.gif"; + + // Animated gif with 4 frames, interlaced, looping forever, no transparency. + public const string AnimatedLoopInterlaced = "Gif/animated_loop_interlaced.gif"; + + // Transparent gif with 4 frames, loops forever. + public const string AnimatedTransparentLoop = "Gif/animated_transparent_loop.gif"; + + // Transparent gif with 4 frames, loops forever, first frame restore previous. + public const string AnimatedTransparentFirstFrameRestorePrev = "Gif/animated_transparent_firstframerestoreprev_loop.gif"; + + // Transparent gif with 4 transparent frames, loops forever, no dispose + public const string AnimatedTransparentNoRestore = "Gif/animated_transparent_frame_norestore_loop.gif"; + + // Transparent gif with 4 transparent frames, loops forever, restore previous. + public const string AnimatedTransparentRestorePrevious = "Gif/animated_transparent_frame_restoreprev_loop.gif"; + + // Static gif with no animation, no transparency. + public const string StaticNontransparent = "Gif/static_nontransparent.gif"; + + // Static transparent gif with no animation. + public const string StaticTransparent = "Gif/static_transparent.gif"; + // Test images from https://github.com/robert-ancell/pygif/tree/master/test-suite public const string ZeroSize = "Gif/image-zero-size.gif"; public const string ZeroHeight = "Gif/image-zero-height.gif"; @@ -537,6 +563,7 @@ public static class Issues public const string Issue2450_B = "Gif/issues/issue_2450_2.gif"; public const string Issue2198 = "Gif/issues/issue_2198.gif"; public const string Issue2758 = "Gif/issues/issue_2758.gif"; + public const string Issue2866 = "Gif/issues/issue_2866.gif"; public const string Issue2859_A = "Gif/issues/issue_2859_A.gif"; public const string Issue2859_B = "Gif/issues/issue_2859_B.gif"; } @@ -831,7 +858,13 @@ public static class Lossy public const string Issue2670 = "Webp/issues/Issue2670.webp"; public const string Issue2763 = "Webp/issues/Issue2763.png"; public const string Issue2801 = "Webp/issues/Issue2801.webp"; + public const string Issue2866 = "Webp/issues/Issue2866.webp"; } + + public const string AlphaBlend = "Webp/alpha-blend.webp"; + public const string AlphaBlend2 = "Webp/alpha-blend-2.webp"; + public const string AlphaBlend3 = "Webp/alpha-blend-3.webp"; + public const string AlphaBlend4 = "Webp/alpha-blend-4.webp"; } public static class Tiff diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs index 57813f66ac..bdc78f3f54 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/MagickReferenceDecoder.cs @@ -64,20 +64,27 @@ protected override Image Decode(DecoderOptions options, Stream s settings.SetDefines(pngReadDefines); using MagickImageCollection magickImageCollection = new(stream, settings); + int imageWidth = magickImageCollection.Max(x => x.Width); + int imageHeight = magickImageCollection.Max(x => x.Height); + List> framesList = []; foreach (IMagickImage magicFrame in magickImageCollection) { - ImageFrame frame = new(configuration, (int)magicFrame.Width, (int)magicFrame.Height); + ImageFrame frame = new(configuration, imageWidth, imageHeight); framesList.Add(frame); - MemoryGroup framePixels = frame.PixelBuffer.FastMemoryGroup; + Buffer2DRegion buffer = frame.PixelBuffer.GetRegion( + imageWidth - magicFrame.Width, + imageHeight - magicFrame.Height, + magicFrame.Width, + magicFrame.Height); using IUnsafePixelCollection pixels = magicFrame.GetPixelsUnsafe(); if (magicFrame.Depth is 12 or 10 or 8 or 6 or 5 or 4 or 3 or 2 or 1) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); - FromRgba32Bytes(configuration, data, framePixels); + FromRgba32Bytes(configuration, data, buffer); } else if (magicFrame.Depth is 14 or 16 or 32) { @@ -88,7 +95,7 @@ protected override Image Decode(DecoderOptions options, Stream s ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span bytes = MemoryMarshal.Cast(data.AsSpan()); - FromRgba64Bytes(configuration, bytes, framePixels); + FromRgba64Bytes(configuration, bytes, buffer); } else { @@ -111,33 +118,40 @@ protected override ImageInfo Identify(DecoderOptions options, Stream stream, Can PixelType = metadata.GetDecodedPixelTypeInfo() }; } - - private static void FromRgba32Bytes(Configuration configuration, Span rgbaBytes, IMemoryGroup destinationGroup) + private static void FromRgba32Bytes( + Configuration configuration, + Span rgbaBytes, + Buffer2DRegion destinationGroup) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { Span sourcePixels = MemoryMarshal.Cast(rgbaBytes); - foreach (Memory m in destinationGroup) + for (int y = 0; y < destinationGroup.Height; y++) { - Span destBuffer = m.Span; + Span destBuffer = destinationGroup.DangerousGetRowSpan(y); PixelOperations.Instance.FromRgba32( configuration, sourcePixels[..destBuffer.Length], destBuffer); + sourcePixels = sourcePixels[destBuffer.Length..]; } } - private static void FromRgba64Bytes(Configuration configuration, Span rgbaBytes, IMemoryGroup destinationGroup) + private static void FromRgba64Bytes( + Configuration configuration, + Span rgbaBytes, + Buffer2DRegion destinationGroup) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel { - foreach (Memory m in destinationGroup) + for (int y = 0; y < destinationGroup.Height; y++) { - Span destBuffer = m.Span; + Span destBuffer = destinationGroup.DangerousGetRowSpan(y); PixelOperations.Instance.FromRgba64Bytes( configuration, rgbaBytes, destBuffer, destBuffer.Length); + rgbaBytes = rgbaBytes[(destBuffer.Length * 8)..]; } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 05abedbd8e..263df8f3a6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -107,6 +107,7 @@ public static Image DebugSaveMultiFrame( ITestImageProvider provider, object testOutputDetails = null, string extension = "png", + IImageEncoder encoder = null, bool appendPixelTypeToFileName = true, Func predicate = null) where TPixel : unmanaged, IPixel @@ -119,6 +120,7 @@ public static Image DebugSaveMultiFrame( provider.Utility.SaveTestOutputFileMultiFrame( image, extension, + encoder: encoder, testOutputDetails: testOutputDetails, appendPixelTypeToFileName: appendPixelTypeToFileName, predicate: predicate); @@ -277,6 +279,47 @@ public static Image CompareFirstFrameToReferenceOutput( return image; } + public static Image CompareDebugOutputToReferenceOutputMultiFrame( + this Image image, + ITestImageProvider provider, + ImageComparer comparer, + object testOutputDetails = null, + string extension = "png", + IImageEncoder encoder = null, + bool appendPixelTypeToFileName = true, + Func predicate = null) + where TPixel : unmanaged, IPixel + { + image.DebugSaveMultiFrame( + provider, + testOutputDetails, + extension, + encoder, + appendPixelTypeToFileName, + predicate: predicate); + + using (Image debugImage = GetDebugOutputImageMultiFrame( + provider, + image.Frames.Count, + testOutputDetails, + extension, + appendPixelTypeToFileName, + predicate: predicate)) + + using (Image referenceImage = GetReferenceOutputImageMultiFrame( + provider, + image.Frames.Count, + testOutputDetails, + extension, + appendPixelTypeToFileName, + predicate: predicate)) + { + comparer.VerifySimilarity(referenceImage, debugImage); + } + + return image; + } + public static Image CompareToReferenceOutputMultiFrame( this Image image, ITestImageProvider provider, @@ -375,6 +418,54 @@ public static Image GetReferenceOutputImageMultiFrame( return result; } + public static Image GetDebugOutputImageMultiFrame( + this ITestImageProvider provider, + int frameCount, + object testOutputDetails = null, + string extension = "png", + bool appendPixelTypeToFileName = true, + Func predicate = null) + where TPixel : unmanaged, IPixel + { + (int Index, string FileName)[] frameFiles = [.. provider.Utility.GetTestOutputFileNamesMultiFrame( + frameCount, + extension, + testOutputDetails, + appendPixelTypeToFileName, + predicate: predicate)]; + + List> temporaryFrameImages = []; + + IImageDecoder decoder = TestEnvironment.GetReferenceDecoder(frameFiles[0].FileName); + + for (int i = 0; i < frameFiles.Length; i++) + { + string path = frameFiles[i].FileName; + if (!File.Exists(path)) + { + throw new FileNotFoundException("Reference output file missing: " + path); + } + + using FileStream stream = File.OpenRead(path); + Image tempImage = decoder.Decode(DecoderOptions.Default, stream); + temporaryFrameImages.Add(tempImage); + } + + Image firstTemp = temporaryFrameImages[0]; + + Image result = new(firstTemp.Width, firstTemp.Height); + + foreach (Image fi in temporaryFrameImages) + { + result.Frames.AddFrame(fi.Frames.RootFrame); + fi.Dispose(); + } + + // Remove the initial empty frame: + result.Frames.RemoveFrame(0); + return result; + } + public static IEnumerable GetReferenceOutputSimilarityReports( this Image image, ITestImageProvider provider, diff --git a/tests/Images/External/ReferenceOutput/BmpEncoderTests/Encode_8BitColor_WithOctreeQuantizer_rgb32.bmp b/tests/Images/External/ReferenceOutput/BmpEncoderTests/Encode_8BitColor_WithOctreeQuantizer_rgb32.bmp index 2b8e05b070..f4ae3b9b68 100644 --- a/tests/Images/External/ReferenceOutput/BmpEncoderTests/Encode_8BitColor_WithOctreeQuantizer_rgb32.bmp +++ b/tests/Images/External/ReferenceOutput/BmpEncoderTests/Encode_8BitColor_WithOctreeQuantizer_rgb32.bmp @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:11375b15df083d98335f4a4baf0717e7fdd6b21ab2132a6815cadc787ac17e7d +oid sha256:a98b1ec707af066f77fad7d1a64b858d460986beb6d27682717dd5e221310fd4 size 9270 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/ApplyDitherFilterInBox_Rgba32_CalliphoraPartial - Copy.png b/tests/Images/External/ReferenceOutput/DitherTests/ApplyDitherFilterInBox_Rgba32_CalliphoraPartial - Copy.png new file mode 100644 index 0000000000..43e414da6d --- /dev/null +++ b/tests/Images/External/ReferenceOutput/DitherTests/ApplyDitherFilterInBox_Rgba32_CalliphoraPartial - Copy.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a899a84c6af24bfad89f9fde75957c7a979d65bcf096ab667cb976efd71cb560 +size 271171 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Atkinson.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Atkinson.png index cc2327b23f..70acb3f32e 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Atkinson.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Atkinson.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7e22401dddf6552cd91517c1cdd142d3b9a66a7ad5c80d2e52ae07a7f583708e -size 57657 +oid sha256:e44c49a8f2ab1280c38e6ba71da29a93803b2aa4cf117e1e919909521b0373e6 +size 57636 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Burks.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Burks.png index e3ae6508e1..af35177491 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Burks.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Burks.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:819a0ce38e27e2adfa454d8c5ad5b24e818bf8954c9f2406f608dcecf506c2c4 -size 59838 +oid sha256:359a44bb957481c85d5acd65559b43ffc0acf806d4f4e57d6a791ca65b28295b +size 59839 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_FloydSteinberg.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_FloydSteinberg.png index 2b897a5d6d..a14c2cb1f6 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_FloydSteinberg.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_FloydSteinberg.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:007ac609ec61b39c7bdd04bc87a698f5cdc76eadd834c1457f41eb9c135c3f7b +oid sha256:7fb3743098a8147fd24294d933d93a61ec0155d754f52544650f6589719905be size 60688 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_JarvisJudiceNinke.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_JarvisJudiceNinke.png index 10ba90ae86..683f59ea1e 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_JarvisJudiceNinke.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_JarvisJudiceNinke.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:46892c07e9a93f1df71f0e38b331a437fb9b7c52d8f40cf62780cb6bd35d3b13 -size 58963 +oid sha256:41fa7d92a10db450f3b3729ab9e36074224baaefeda21cffd0466e37a111e138 +size 59113 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra2.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra2.png index 9608289e84..813289a26d 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra2.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra2.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1b83345ca3de8d1fc0fbb5d8e68329b94ad79fc29b9f10a1392a97ffe9a0733e -size 58985 +oid sha256:bebf3b3762b339874891e3d434511e5f2557be90d66d6d7fe827b50334ede6c2 +size 58976 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra3.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra3.png index 79d2c5eb14..d4da100376 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra3.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Sierra3.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c775a5b19ba09e1b335389e0dc12cb0c3feaff6072e904da750a676fcd6b07dc -size 59202 +oid sha256:fd4358826739db2c22064e8aa90597f8b6403b9d7e2866ec280e743c51d2f41f +size 59203 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Stucki.png b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Stucki.png index edec46a92a..fa8eea57a9 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Stucki.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DiffusionFilter_WorksWithAllErrorDiffusers_CalliphoraPartial_Stucki.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8cc216ed952216d203836dc559234216614f1ed059651677cc0ea714010bd932 -size 58855 +oid sha256:174ee39c08eb9a174b48b19dc618d043bf6b71eee68ab7127407eb713e164e61 +size 58934 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Bgra32_filter0.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Bgra32_filter0.png index e2e4147f68..2c67b3bf23 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Bgra32_filter0.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Bgra32_filter0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a3253003b088c9975725cf321c2fc827547a5feb199f2d1aa515c69bde59deb7 -size 871 +oid sha256:1110b46ec3296a1631420e0bb915f6fdc3d1cead4b0fc5a63a7a280fbf841ea2 +size 870 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgb24_filter0.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgb24_filter0.png index aa0e9a4824..1305c5ede9 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgb24_filter0.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgb24_filter0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bb3e3b9b3001e76505fb0e2db7ad200cad2a016c06f1993c60c3cab42c134863 -size 867 +oid sha256:e51abcab66201997deda99637de604330ef977fd2d1dbebaa0416c621d03b8f9 +size 869 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgba32_filter0.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgba32_filter0.png index e2e4147f68..2c67b3bf23 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgba32_filter0.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_Rgba32_filter0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a3253003b088c9975725cf321c2fc827547a5feb199f2d1aa515c69bde59deb7 -size 871 +oid sha256:1110b46ec3296a1631420e0bb915f6fdc3d1cead4b0fc5a63a7a280fbf841ea2 +size 870 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_RgbaVector_filter0.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_RgbaVector_filter0.png index e2e4147f68..2c67b3bf23 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_RgbaVector_filter0.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_ShouldNotDependOnSinglePixelType_RgbaVector_filter0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a3253003b088c9975725cf321c2fc827547a5feb199f2d1aa515c69bde59deb7 -size 871 +oid sha256:1110b46ec3296a1631420e0bb915f6fdc3d1cead4b0fc5a63a7a280fbf841ea2 +size 870 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer16x16.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer16x16.png index 4175cf40b7..da1f62b728 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer16x16.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer16x16.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9316cbbcb137ae6ff31646f6a5ba1d0aec100db4512509f7684187e74d16a111 -size 51074 +oid sha256:eb86f2037a0aff48a84c0161f22eb2e2495daadbfa9c33185ddfd7b8429a4ea9 +size 51266 diff --git a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer8x8.png b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer8x8.png index ac56fa9236..03848e81ce 100644 --- a/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer8x8.png +++ b/tests/Images/External/ReferenceOutput/DitherTests/DitherFilter_WorksWithAllDitherers_CalliphoraPartial_Bayer8x8.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6d2289ed4fa0c679f0f120d260fec8ab40b1599043cc0a1fbebc6b67e238ff87 -size 51428 +oid sha256:ef033a419e2e1b06b57a66175bad9068f71ae4c862a66c5734f65cdaae8a27f0 +size 51461 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/00.png new file mode 100644 index 0000000000..65b2c6ff72 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b980fb1927a70f88bd26b039c54e4ea20a6a1ad68aacd6f1a68a46eb1997a29 +size 1180 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/01.png new file mode 100644 index 0000000000..8c9e125ade --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4006374b88ff4c4ed665333608a19e693fc083ae72beb71850d0e39ad45c9943 +size 1144 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/02.png new file mode 100644 index 0000000000..a2fc2ab3bc --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f5054e1e464c9e9fc999eec00b9949a6dc256ee062e9910b5718b6d4658661a +size 1303 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/03.png new file mode 100644 index 0000000000..9d5b54c718 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49e8bcbcc5dc63fbd555f90a52b4e111cfc058f3adba2ca9c52dec966dbbae8f +size 1371 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/00.png new file mode 100644 index 0000000000..65b2c6ff72 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b980fb1927a70f88bd26b039c54e4ea20a6a1ad68aacd6f1a68a46eb1997a29 +size 1180 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/01.png new file mode 100644 index 0000000000..8c9e125ade --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4006374b88ff4c4ed665333608a19e693fc083ae72beb71850d0e39ad45c9943 +size 1144 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/02.png new file mode 100644 index 0000000000..a2fc2ab3bc --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f5054e1e464c9e9fc999eec00b9949a6dc256ee062e9910b5718b6d4658661a +size 1303 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/03.png new file mode 100644 index 0000000000..9d5b54c718 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_Rgba32_animated_loop_interlaced.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49e8bcbcc5dc63fbd555f90a52b4e111cfc058f3adba2ca9c52dec966dbbae8f +size 1371 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/00.png new file mode 100644 index 0000000000..193cde24d9 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b60d2066cb53d41988da37b8c521ddcb5355b995320a8413b95522a0492140 +size 687 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/01.png new file mode 100644 index 0000000000..4d2d255108 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:30ff7708250c5f02dc02d74238d398b319d8fc6c071178f32f82a17e3b637afd +size 542 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/02.png new file mode 100644 index 0000000000..0654e49d45 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d21f4576486692122b6ee719d75883849f65ddb07f632ea1c62b42651c289688 +size 591 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/03.png new file mode 100644 index 0000000000..0c1090f662 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_firstframerestoreprev_loop.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:88db68f2d59301b8ff9326143455a03c94cb616220f6e8e3832f13effe0c09bc +size 545 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/00.png new file mode 100644 index 0000000000..193cde24d9 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b60d2066cb53d41988da37b8c521ddcb5355b995320a8413b95522a0492140 +size 687 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/01.png new file mode 100644 index 0000000000..f289fdca31 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:034b0b6b94c13fbef8c44d650daa07362f113aae6600d63230a3f96e29b16dec +size 790 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/02.png new file mode 100644 index 0000000000..07537b9df0 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4b3684db6e3df52a9eb520d562b51b54632e897e9e39bff5ce904ae00799f2f +size 924 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/03.png new file mode 100644 index 0000000000..e376be6890 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_norestore_loop.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e33c564f55b653a693105612949401002014821abaecaf654c96d0f2b5d59b4 +size 962 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/00.png new file mode 100644 index 0000000000..193cde24d9 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b60d2066cb53d41988da37b8c521ddcb5355b995320a8413b95522a0492140 +size 687 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/01.png new file mode 100644 index 0000000000..f289fdca31 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:034b0b6b94c13fbef8c44d650daa07362f113aae6600d63230a3f96e29b16dec +size 790 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/02.png new file mode 100644 index 0000000000..27f29acbb0 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a4f4eb12da36cd43c620aa5ad1c793bb6eb8431c61d2cc1b77c1118f35a741cc +size 876 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/03.png new file mode 100644 index 0000000000..684c2fa389 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_frame_restoreprev_loop.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15bf02e06c6819d74a0a79cbfc5c86913c248a8812ff0ec613c0e747a000241b +size 789 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/00.png new file mode 100644 index 0000000000..193cde24d9 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b60d2066cb53d41988da37b8c521ddcb5355b995320a8413b95522a0492140 +size 687 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/01.png new file mode 100644 index 0000000000..7818cf380f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:01f389a2e93023f3132927a9565c04c8c1f827e36111ebe682177adecc3a27ee +size 774 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/02.png new file mode 100644 index 0000000000..18bc408639 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00ef57db2fef89112ac7d1808afb6803612c6a20fc589166be6d6b7007c46400 +size 946 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/03.png new file mode 100644 index 0000000000..bd2ea67aed --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Animated_WithTransparency_Rgba32_animated_transparent_loop.gif/03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:deafa4d4f8fd84489c061ca1042c2ad9e655fff3b6419248cfb35fa4ea40d9e6 +size 1000 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_nontransparent.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_nontransparent.png new file mode 100644 index 0000000000..65b2c6ff72 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_nontransparent.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b980fb1927a70f88bd26b039c54e4ea20a6a1ad68aacd6f1a68a46eb1997a29 +size 1180 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_transparent.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_transparent.png new file mode 100644 index 0000000000..193cde24d9 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Decode_Static_No_Animation_Rgba32_static_transparent.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b60d2066cb53d41988da37b8c521ddcb5355b995320a8413b95522a0492140 +size 687 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue1962_Rgba32_issue1962_tiniest_gif_1st.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue1962_Rgba32_issue1962_tiniest_gif_1st.png index 24f5e9c0cd..a52b27708a 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue1962_Rgba32_issue1962_tiniest_gif_1st.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue1962_Rgba32_issue1962_tiniest_gif_1st.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4f8c6d416f09671777934e57bc67fb52ccc97145dc6f1869e628d9ffd7d8f6e7 -size 119 +oid sha256:9ab8374e77865606a2426e3d22628f717914472431de1d9d8ee9690d319850a0 +size 118 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png index b07e806620..5d443b52aa 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/Issue2012BadMinCode_Rgba32_issue2012_drona1.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:588d055a93c7b4fdb62e8b77f3ae08753a9e8990151cb0523f5e761996189b70 -size 142244 +oid sha256:a0e1677baade797de1eaec390f2e475865d24de8bd344edddbb3fce200d6bcb0 +size 135418 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/00.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/00.png index 923fbc1225..52f14e0f72 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/00.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/00.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:800d1ec2d7c7c99d449db1f49ef202cf18214016eae65ebc4216d6f4b1f4d328 -size 537 +oid sha256:473c5629d7a9f8b3d6c809e8ede40f8fd38e90beddf71851b352c726fc0570d6 +size 534 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/01.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/01.png index 6c2134d8b8..b47f34ba04 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/01.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/01.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:94dcd97831b16165f3331e429d72d7ef546e04038cab754c7918f9cf535ff30a -size 542 +oid sha256:4b9b9f856c0347b460f824d6b027b343c65c67a29360793181c9a29a76f9002b +size 538 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/02.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/02.png index 6f50397ea4..64869ca3c6 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/02.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/02.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ec1a589a8fae1b17a82b70a9583ea2ee012a476b1fa8fdba27fee2b7ce0403b2 -size 540 +oid sha256:74b8015c60d215808b1d663ae4af956d1454414206ba21326ad35b8952b0cab6 +size 534 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/03.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/03.png index 82061ba0aa..ab52225f8b 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/03.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/03.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0c8751f4fafd5c56066dbb8d64a3890fc420a3bd66881a55e309ba274b6d14e4 -size 542 +oid sha256:8dacb6a468d3cdc94613d56264ddf34d3649846edf33619fc13b9522fcf982d6 +size 539 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/04.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/04.png index 8902eb824a..78988aa60a 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/04.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/04.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b78516c9874cb15de4c4b98ed307e8105d962fc6bfa7aa3490b2c7e13b455a2d -size 544 +oid sha256:8af74db6e01928ad54444fa122e4b87929741052c85abe9c0ffc998adffcbdfc +size 542 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/05.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/05.png index 82061ba0aa..ab52225f8b 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/05.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/05.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0c8751f4fafd5c56066dbb8d64a3890fc420a3bd66881a55e309ba274b6d14e4 -size 542 +oid sha256:8dacb6a468d3cdc94613d56264ddf34d3649846edf33619fc13b9522fcf982d6 +size 539 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/06.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/06.png index 6f50397ea4..64869ca3c6 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/06.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/06.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ec1a589a8fae1b17a82b70a9583ea2ee012a476b1fa8fdba27fee2b7ce0403b2 -size 540 +oid sha256:74b8015c60d215808b1d663ae4af956d1454414206ba21326ad35b8952b0cab6 +size 534 diff --git a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/07.png b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/07.png index efba40c99d..97610dbc00 100644 --- a/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/07.png +++ b/tests/Images/External/ReferenceOutput/GifDecoderTests/IssueTooLargeLzwBits_Rgba32_issue_2743.gif/07.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5016a323018f09e292165ad5392d82dcbad5e79c2b6b93aff3322dffff80b309 -size 126 +oid sha256:c816ca1e58d14361b84ba47454e4cbf4d3e4d29dfb7827756eb52ef2604f297c +size 161 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/00.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/00.gif new file mode 100644 index 0000000000..b219975ade --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/00.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb4bbef09dc6618380e34c5dcf8612fa5a51ba81a09edc5500be9191f0554d9c +size 49665 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/08.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/08.gif new file mode 100644 index 0000000000..2d50761636 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/08.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:81a0d629326bb39cfced1a261542e5f94b423527f95bc45422670091b91583b4 +size 50730 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/104.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/104.gif new file mode 100644 index 0000000000..b1b7781a21 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/104.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e64d9f2f7a8346f62c9b41a14b3e6b71f76a48e07fa42ac9e0d4a5b146a8a9da +size 58856 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/112.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/112.gif new file mode 100644 index 0000000000..f058764b4f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/112.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b01517c53b19f6b151a76cc75142ba3a8a45da8c6e94416447703cbd54ce1a8a +size 48282 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/16.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/16.gif new file mode 100644 index 0000000000..b9f1e2d099 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/16.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1db212159613778c962883de9067852da3bea5f3483dd9f967c0aabbcdc1b2f6 +size 64655 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/24.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/24.gif new file mode 100644 index 0000000000..c7a1368ce1 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/24.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d09ddfff1f26ed7842df5bf4b8938373700658322c85b154a878dd5e3a90dc1 +size 64432 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/32.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/32.gif new file mode 100644 index 0000000000..ffd61e5123 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/32.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18ca31ff631ecc33fe33a893e94e23af8b086a78c3684461e449c02800fffb2b +size 66510 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/40.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/40.gif new file mode 100644 index 0000000000..eb93ea4d4a --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/40.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:823358342cbc25a9f7ae34abc2669096acd7c0e0c93a8a0b371e548822ed0897 +size 66912 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/48.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/48.gif new file mode 100644 index 0000000000..99f0e64dc6 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/48.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:90558311a7b7127d9f970a17ae0630d81507be246f511f1cc3b10c6ee953a25c +size 61986 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/56.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/56.gif new file mode 100644 index 0000000000..8e6410f9bf --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/56.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9514b736d946d4e93ba3f59b586d2c29e0c031155f7824756ecf468ef87ea8e6 +size 61367 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/64.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/64.gif new file mode 100644 index 0000000000..2257625c41 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/64.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0334c551b9efcaa9f5c16c4599884b4aabe5129e3f023222be3214cf8623242e +size 60825 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/72.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/72.gif new file mode 100644 index 0000000000..efc9569f4d --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/72.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:32a9fecdad6508c1c6beae839717d1854cca1f7b247bff36a00a93cc953f608c +size 57370 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/80.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/80.gif new file mode 100644 index 0000000000..9f7ae53fb0 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/80.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d50d4ccba947ef95b9e8a2c4acd08f57c414f4e38a0d03d65b5fee093e4481a +size 67784 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/88.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/88.gif new file mode 100644 index 0000000000..22dc30784c --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/88.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:840780f2916cb9d010a95802d9c123c3051bcee5dde7b173a50854e3b5f3636a +size 72552 diff --git a/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/96.gif b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/96.gif new file mode 100644 index 0000000000..53e1a35cbf --- /dev/null +++ b/tests/Images/External/ReferenceOutput/GifEncoderTests/GifEncoder_CanDecode_AndEncode_Issue2866_Rgba32_issue_2866.gif/96.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:41db6ded3de84d43dec1175c1481f75a045c5ad126369e4e82ae29ec4bad0bc4 +size 76868 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/00.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/00.png new file mode 100644 index 0000000000..17c8e35c6f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4aa1dea5da7a94fddd2259cd86e7171a57cb5cd2198decedd86149778b9aa20a +size 64030 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/08.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/08.png new file mode 100644 index 0000000000..f2ec385a88 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/08.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:16d4159e492162372a16b08fb3a4fdf36908ce759c6a0ad72a0da4bbeb477b72 +size 68619 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/104.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/104.png new file mode 100644 index 0000000000..dc57ed717f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/104.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1095bc829b8fab3c5fda66b90cdd751b81439ad80e9fc7d564a5212c75155b2c +size 76100 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/112.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/112.png new file mode 100644 index 0000000000..0158b28407 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/112.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0212cd29319c0d90c18a5f90716156a888c9925e4a36a6d9bbbe8d67a706db8f +size 66282 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/16.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/16.png new file mode 100644 index 0000000000..52867f468e --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/16.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:620ea89afc2e41b6199c47b2e4777f5ef9afe139e22bfabca9ec094b0adcbdc2 +size 73862 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/24.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/24.png new file mode 100644 index 0000000000..26ae147489 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/24.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d39997f18af5b97ed32ea30c15da879f70906b4f5cf12439ea6ff2d229e40199 +size 75734 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/32.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/32.png new file mode 100644 index 0000000000..6cd5074ecf --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/32.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4b459edf763ffff8a8a016c1b7d9e13b7e99f6752e4452b52539d2eb0f7f31be +size 79505 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/40.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/40.png new file mode 100644 index 0000000000..8b9ea7c239 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/40.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f1957c82709b22abbaa354ecd679159eeddf135aabb301c266518c23ff4918f9 +size 77588 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/48.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/48.png new file mode 100644 index 0000000000..4430290427 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/48.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae33b303bd1feae301cf645ee6596c499ad4eaa9164c52a37fca760169f9f363 +size 80317 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/56.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/56.png new file mode 100644 index 0000000000..8feae54c6a --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/56.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d86360bd43de392a9cf4b671a4cf3cd7ba9f61952a6d0400a5baab7dbb5eeb6d +size 78578 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/64.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/64.png new file mode 100644 index 0000000000..284ad99233 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/64.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60e1694b121d0d3fa17d62f4477de349376aa6c2a98b83f9a4525da9ea471662 +size 75204 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/72.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/72.png new file mode 100644 index 0000000000..d09b77afca --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/72.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9348ef330a239bea501f41a3256107fc10a6bb323642118b1fcbe5227b742c88 +size 79048 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/80.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/80.png new file mode 100644 index 0000000000..d31fd5a808 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/80.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:452dd2339b6ad3e35528601c289d88eafcc7e9a6e717f25da2bbf4267b85be97 +size 81553 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/88.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/88.png new file mode 100644 index 0000000000..669dfb8600 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/88.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b67e3b57b62f9e1c924de6fbd2699f93a6594812ad8c4cafd6160323547b1a88 +size 86402 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/96.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/96.png new file mode 100644 index 0000000000..f9a5aee191 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_issue_2866.gif/96.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:887e94402f93a491820d3d25d641c5901cf796c1275a2feb9e1c072a6a63ce28 +size 87335 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/00.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/00.png new file mode 100644 index 0000000000..961e205f9f --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/00.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6baac5db47ef2ee3ba72c7e8d5e04fc888c5e1460d4d7e348f63b597e1b5ff7 +size 23660 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/08.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/08.png new file mode 100644 index 0000000000..82cb6168e8 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Encode_AnimatedFormatTransform_FromGif_Rgba32_leo.gif/08.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7fad45a0682a70a1fb84eec76d4a80ee054e85df59e6729508f3698d39999e31 +size 29509 diff --git a/tests/Images/External/ReferenceOutput/PngEncoderTests/Issue2668_Quantized_Encode_Alpha_Rgba32_Issue_2668.png b/tests/Images/External/ReferenceOutput/PngEncoderTests/Issue2668_Quantized_Encode_Alpha_Rgba32_Issue_2668.png index 7af5391f70..b292138707 100644 --- a/tests/Images/External/ReferenceOutput/PngEncoderTests/Issue2668_Quantized_Encode_Alpha_Rgba32_Issue_2668.png +++ b/tests/Images/External/ReferenceOutput/PngEncoderTests/Issue2668_Quantized_Encode_Alpha_Rgba32_Issue_2668.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f934af128b85b9e8f557d71ac8b1f1473a0922d0754fc0c4ece0d0e3d8d94c39 -size 7702 +oid sha256:df34f8f3640b145add4f24f8003c288fe7991373b079a87b4be90842e18c82ae +size 8236 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_ErrorDither.png index 4948c7adee..327366f5b6 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a51d04953c1c82d99884af62912d2271108c6bc62f18d4b32d0b5290c01fa7f7 -size 247462 +oid sha256:0086044f12a7c58e49733f203af29a8aff2826ea654730274720eada15669254 +size 249163 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_NoDither.png index d993923d48..3e0be536e3 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9f165908729d723818b6c5843bd75298d987448e2cd4278dfe3f388a62025add -size 238396 +oid sha256:85ee8479984aa52f837badbc49085c5448597fbfd987438fe25b58bad475e85f +size 239498 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_OrderedDither.png index 223d3bc012..816fdf704b 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_Bike_OctreeQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:34eaa0696da00838e591b2c48e7797641521f7f3feb01abbd774591c4dd6f200 -size 265546 +oid sha256:4ee35a7c21e90a2de1bf953e1c0be96d4f63492d0c8b2809fe9f39a9d0e64191 +size 266755 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_ErrorDither.png index ebb9ff6b00..f29db004f5 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f414473561bfa792c2e6342ff5e5dddffbdec5286932781b11a093803593b52a -size 313787 +oid sha256:ce381c2d261b9b1ca61d8f6e2ff07b992283c327dc6b7cf53c7e5c9317abb7d3 +size 316443 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_NoDither.png index 7e3080562c..284c3a2702 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0203ecb9e4665e7c3992b7da4777c6d35b539790506fc9ca2acbcbc2bdb5db18 -size 303979 +oid sha256:2bfc23a95df8a88ac6e2777d67f381e800d23647c162a9a97131a101bbb97143 +size 306703 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_OrderedDither.png index 5626fa1b83..5911faa723 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationInBox_CalliphoraPartial_OctreeQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:62cdce27fc46a38a16995df8ed1501f65091d69315288479b1d613b1d87c8239 -size 321123 +oid sha256:9d3f58a108d933ec9ac0a5271af5b65d0a8ab9d521d54e48312b280cc42d71ac +size 322049 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.25.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.25.png index 82b965123d..a2fb2a6760 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.25.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.25.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4bed69d43856ebd4b1af4055f8d3aacabd50c361a4e1e1f9cad080d799d6b744 -size 13853 +oid sha256:18a47a6fa0f7949daef6969a847d8bc04deeb16bb482211ec3a958bc63f23f89 +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.5.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.5.png index 571b0db4b9..8d99eb49b2 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.5.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.5.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4c8c8393708002f06f9d8ed1ff8979db820035585c08b66ae463d94724fa64d3 -size 14330 +oid sha256:abfdd1e40c2c1d7fde419bda1da6e534ed989598e790b8ae4de35152a83f77a0 +size 13686 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.75.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.75.png index a1b3da6816..bf93c39ff8 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.75.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.75.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fda13875f4c762a95001426487cc04c9add39821eb793168fdbe5cc18e705643 -size 14566 +oid sha256:60c28eb1dc3c0416b20cec230917c0e4a70dd2929467bbab796ecbb04fe5a178 +size 13886 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.png index 82b965123d..a2fb2a6760 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4bed69d43856ebd4b1af4055f8d3aacabd50c361a4e1e1f9cad080d799d6b744 -size 13853 +oid sha256:18a47a6fa0f7949daef6969a847d8bc04deeb16bb482211ec3a958bc63f23f89 +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_1.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_1.png index e0fc792026..457298b544 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_1.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_ErrorDither_1.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cb826afb127fe4175e6e47253b8a8313b9d10aee193c316731f34e5d327a2591 -size 14580 +oid sha256:a523f097bf3b155f3823c5e400190b5d5e0d4470db7136576472c3257db76600 +size 13909 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.25.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.25.png index 491847e491..5431ffdefd 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.25.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.25.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:37018ecc499651833208d846a0f446db94cc11eae002ab6e7ce45b3e7c09e86c -size 17734 +oid sha256:dd8b648b89f9420a0004a5f95dd54dc3769d1f78816b6708c5c6e1c14e8533a1 +size 17802 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.5.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.5.png index 013bb4a3b7..02ade5b868 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.5.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.5.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c2f9ed902882f58704b22460bc64a7b27bc6f47fc2c822ee09f52345cc0d6ebf -size 19255 +oid sha256:7e559263bd8c293797d59166e723fdaf8b1b6ff9ae20fabac0efc86d8306123e +size 19266 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.75.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.75.png index 31fd7a5445..cfbb9c2d5c 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.75.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.75.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:aad3f26f2939f3679afa2b6165db29885fff40bbb1d171d5ffecc7861b5fac31 -size 19654 +oid sha256:5ae4ba533dd19271937d7e02e8ed3d243f164820b7d3bb20a4390e2deaa1d0d6 +size 19639 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.png index 82b965123d..a2fb2a6760 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4bed69d43856ebd4b1af4055f8d3aacabd50c361a4e1e1f9cad080d799d6b744 -size 13853 +oid sha256:18a47a6fa0f7949daef6969a847d8bc04deeb16bb482211ec3a958bc63f23f89 +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_1.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_1.png index e2a05b9bd5..ce546cbfe1 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_1.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_OctreeQuantizer_OrderedDither_1.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6d21029fa22dbe72cdc60b90c758cb9becd9fce03a33580d9466c1aedd323c1c -size 20000 +oid sha256:ac17dd1abc6405cb84e9d8a6404da2c2bdb220b3390d09613d3644baad6e8e99 +size 20128 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.25.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.25.png index dba9232097..eaf7e8241d 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.25.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.25.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:faa91657288e6a6797d8459d41d5fecca3c0a2e8e63317ebaf47df28688d13d7 -size 13853 +oid sha256:4baf0e7bc4ae8b8a911d87f3a7af2bf3ef0235f77f3f509251f2d2f26cfb639d +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.5.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.5.png index ea062d5be6..02879b7a38 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.5.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.5.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:79e48506430f3a9b25f484ef191fd820819c438392a4e588c2ecafb6db9a2210 -size 13775 +oid sha256:c4ac8b88b317281738d833fc71f52348d9f4f45ea5a1303dd91fdb8b42be4267 +size 13186 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.75.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.75.png index ae90ea9b5f..ba05094800 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.75.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.75.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f56c884a0e4666cd662d36ec3a0d4e751c899c0122595378154507fffc69fda4 -size 14010 +oid sha256:1305d54f2139d4577490317051d6ce94a7fc8dd45b902d87a30fb04098dd4594 +size 13407 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.png index dba9232097..eaf7e8241d 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:faa91657288e6a6797d8459d41d5fecca3c0a2e8e63317ebaf47df28688d13d7 -size 13853 +oid sha256:4baf0e7bc4ae8b8a911d87f3a7af2bf3ef0235f77f3f509251f2d2f26cfb639d +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_1.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_1.png index 1e1795063b..b16a5a5c7b 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_1.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_ErrorDither_1.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4095927693b3cd49df58c0c1d7c5430255350c9ae595408a52ad83b1a65614ac -size 14269 +oid sha256:a3fc3a7ace123c330ea06072eb36dd5d65ed9154d4d0f55a828fc542c8a422c1 +size 13472 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.25.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.25.png index 29a3ed7ffd..6adac16cf5 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.25.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.25.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d062d4b79ee01942776ae13467e9bcbb529a7eeb5ad7c28ff3d0ccd3d88dcde6 -size 15962 +oid sha256:35757f2e0831cae2fbd3cc11ffaaae855e853ebaa9a1a5564b6568a5e1c442e9 +size 16031 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.png index dba9232097..eaf7e8241d 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantizationWithDitheringScale_david_WuQuantizer_OrderedDither_0.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:faa91657288e6a6797d8459d41d5fecca3c0a2e8e63317ebaf47df28688d13d7 -size 13853 +oid sha256:4baf0e7bc4ae8b8a911d87f3a7af2bf3ef0235f77f3f509251f2d2f26cfb639d +size 13158 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_ErrorDither.png index 09c471914a..e9782bc076 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a40b319d264f046159722cb57599eda51de9ba3795272b3785901cdc51053fab -size 83010 +oid sha256:b380eda5646fe97ee217ef711103001e54ee023fb8d95f7f3bbad19d886130da +size 83702 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_NoDither.png index 3bd7cbabbb..5e1556dc45 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3bc93509a983e20986614f4937f66d5d979bbb433a30a7736150934cf14b452a -size 55213 +oid sha256:af9e6c3b9e9e90186fb66be188bad9f3f0738d558aab915b3c8dd78652010674 +size 55419 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_OrderedDither.png index 34490e602d..601abdeb4c 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_OctreeQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b92f3320120d53444cefc79b4684933cfe2b933dc79c2414496785743b5c8f18 -size 80808 +oid sha256:aba024f962e5dc96e118b68c19903045f43a327c6cf45643da195382ef79a778 +size 84368 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_ErrorDither.png index 747ca70c1d..37e5035d86 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d58c425ce5b1ca56450095a66dea24b379935b0087aec7b4102f15a99f95a017 -size 101999 +oid sha256:3802cfe67638a24869d6cc9ace1d94460b4c0c26f2c91b12b95fa8f979de64bb +size 101579 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_NoDither.png index de464b94cc..e72ea4b246 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:93a4822e39babba059a88536a965e4f3207e4402d2b92d7d18485fec5e9e69da -size 84378 +oid sha256:bf2021eba9edbb2295924f8394472ac0bb237f0c462c39aa32a2074ef15f9acc +size 81771 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_OrderedDither.png index ce54548279..0997945e52 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_Bike_WuQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:35969c8dc96de4dacc3048ae760a0681278a2011993a0edbceaacc93d6fc3a67 -size 102713 +oid sha256:2d11b18946d373b995ecbb449c8c4cfcc7078aad1c8705997bcbf83131acde03 +size 102439 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_ErrorDither.png index 5efcaedc94..314a056060 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:40d012f4ecb4e36c94d086f8ec7bc199fbfd9fb30a9427a07b35df1b1e430a71 -size 95601 +oid sha256:2236e81d33fcfb50afb9d5fd1a38c5ddf5d33fbb52de1c3204a4a9892fd334ce +size 99084 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_NoDither.png index 916dc37566..5293046724 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fa64863f73dfd1c5daef645c54e9275136f66513a87750bee0ec8e13ac357da5 -size 79649 +oid sha256:c4b59097d1507236af2556ae5f2638360b223b7752cd4c8f760bc14673d811d0 +size 81709 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_OrderedDither.png index f039dd222e..d253a321d5 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_OctreeQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8f5138589c606de20ba193d4279f049ee1ecb3f1801b949d3436995bbf242cbe -size 92683 +oid sha256:3fa41128fd3f7a6b4b74b758186494ce7ed780561b9825277fae0c345116b1d7 +size 95067 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_ErrorDither.png index 2b897a5d6d..a14c2cb1f6 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:007ac609ec61b39c7bdd04bc87a698f5cdc76eadd834c1457f41eb9c135c3f7b +oid sha256:7fb3743098a8147fd24294d933d93a61ec0155d754f52544650f6589719905be size 60688 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_OrderedDither.png index ac56fa9236..03848e81ce 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WebSafePaletteQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6d2289ed4fa0c679f0f120d260fec8ab40b1599043cc0a1fbebc6b67e238ff87 -size 51428 +oid sha256:ef033a419e2e1b06b57a66175bad9068f71ae4c862a66c5734f65cdaae8a27f0 +size 51461 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_ErrorDither.png index 8b79a19e05..0d548ca79d 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ef2b6073b75a2de97a78d47d3b3e40c264687c5756f153d3d85bc5b2714cf85a -size 68226 +oid sha256:e2f4f4e2237925403fd0228344f9fce9be96c0f26e3465775763aca775779763 +size 68222 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_OrderedDither.png index 88cf83a306..b51076bd17 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WernerPaletteQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:513844ed95c2b50e792d3346398256846b8b280dbadf7ef3f4e11d58c1e679c0 -size 69529 +oid sha256:7a8d9c0d81525d9f37d2f36946939040aea30edfc2b7ec0bf329fb49f6c7d73f +size 69896 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_ErrorDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_ErrorDither.png index a3eefcba20..7204abff47 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_ErrorDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_ErrorDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:32b269d62d4eebe555d5d9f12b9958b41206848504bb985dcd1ff9c81a5003c6 -size 117073 +oid sha256:4474b94e2d563938e10ec0526e7d94ba06b440db51b910604e752f7f9e814d66 +size 110757 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_NoDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_NoDither.png index 3b0c46ac38..691623fc88 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_NoDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_NoDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:12f58b00a16913cd85ffa18fcea580a59550dcc201295b060d55a870230f37f7 -size 113995 +oid sha256:58a61c1d9a1d05acd484948c3e5c0496dbc74c0060f5de71741de39eae04ffa8 +size 103875 diff --git a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_OrderedDither.png b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_OrderedDither.png index 328f863307..e80e6c6e81 100644 --- a/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_OrderedDither.png +++ b/tests/Images/External/ReferenceOutput/QuantizerTests/ApplyQuantization_CalliphoraPartial_WuQuantizer_OrderedDither.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:867d7b727de278cbc01b7d2b8e968f1fc0d0a81a3e4af636ce4a6598a8709be6 -size 114630 +oid sha256:b6649918c0394ead13c016a57b6a08561290651bccac88f7f15ba0e29dc5faa4 +size 110422 diff --git a/tests/Images/Input/Gif/animated_loop.gif b/tests/Images/Input/Gif/animated_loop.gif new file mode 100644 index 0000000000..5fad702a10 --- /dev/null +++ b/tests/Images/Input/Gif/animated_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a8750149c953e9e910472684158c07a2cb551c1f7e95744ab48db1a67f63f342 +size 873 diff --git a/tests/Images/Input/Gif/animated_loop_interlaced.gif b/tests/Images/Input/Gif/animated_loop_interlaced.gif new file mode 100644 index 0000000000..9577a84658 --- /dev/null +++ b/tests/Images/Input/Gif/animated_loop_interlaced.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2bc2f895f03092b1c26381a32b5dd5838aacd3331e07f7e4dae55d5cbb4e149 +size 878 diff --git a/tests/Images/Input/Gif/animated_transparent_firstframerestoreprev_loop.gif b/tests/Images/Input/Gif/animated_transparent_firstframerestoreprev_loop.gif new file mode 100644 index 0000000000..5012324caf --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_firstframerestoreprev_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6b0cf18d386dc979fcf853d6a9adac673a2709a8751d31a94930199dededa25f +size 536 diff --git a/tests/Images/Input/Gif/animated_transparent_frame_norestore_loop.gif b/tests/Images/Input/Gif/animated_transparent_frame_norestore_loop.gif new file mode 100644 index 0000000000..712f334aba --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_frame_norestore_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad7359801fa6ed89fb041de1e88faea856b1028d9f477fdc4eda774df6e5f1ce +size 685 diff --git a/tests/Images/Input/Gif/animated_transparent_frame_restorebackground_loop.gif b/tests/Images/Input/Gif/animated_transparent_frame_restorebackground_loop.gif new file mode 100644 index 0000000000..b6f675dcaf --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_frame_restorebackground_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:12254f22eeee9eac6babbfbfb34b6ae9302342454fd6677e8c7c9937656cc127 +size 685 diff --git a/tests/Images/Input/Gif/animated_transparent_frame_restoreprev_loop.gif b/tests/Images/Input/Gif/animated_transparent_frame_restoreprev_loop.gif new file mode 100644 index 0000000000..be7fdf85d8 --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_frame_restoreprev_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0cdfba6efea653bb94ede6edd0577ba6af1f7c130307b94903dd94f0f8bbc4f9 +size 685 diff --git a/tests/Images/Input/Gif/animated_transparent_loop.gif b/tests/Images/Input/Gif/animated_transparent_loop.gif new file mode 100644 index 0000000000..cb001ece8f --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3297987894ba27c2acc6a5c447c3d3a52cc169447b451409535decccc1743e55 +size 536 diff --git a/tests/Images/Input/Gif/animated_transparent_restoreprev_loop.gif b/tests/Images/Input/Gif/animated_transparent_restoreprev_loop.gif new file mode 100644 index 0000000000..f51d02433e --- /dev/null +++ b/tests/Images/Input/Gif/animated_transparent_restoreprev_loop.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9418561ef2f2307456bb068ecc1a9d5aa02da5e314e7aadd722985e27503926b +size 536 diff --git a/tests/Images/Input/Gif/issues/issue_2866.gif b/tests/Images/Input/Gif/issues/issue_2866.gif new file mode 100644 index 0000000000..0ead86bf89 --- /dev/null +++ b/tests/Images/Input/Gif/issues/issue_2866.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b2a9e3728c41e1b45d6f865e4692eadbed28dcaec65806e6bda22a9a16f930f +size 7526725 diff --git a/tests/Images/Input/Gif/static_nontransparent.gif b/tests/Images/Input/Gif/static_nontransparent.gif new file mode 100644 index 0000000000..17ab1e2ec7 --- /dev/null +++ b/tests/Images/Input/Gif/static_nontransparent.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d0a5e1b2f0c5c1763eb950a1d92c5317f048875e04e88dca7f1a966552c2774c +size 678 diff --git a/tests/Images/Input/Gif/static_transparent.gif b/tests/Images/Input/Gif/static_transparent.gif new file mode 100644 index 0000000000..89039a732a --- /dev/null +++ b/tests/Images/Input/Gif/static_transparent.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f73f56bbe2206bd1cd8a4625b6a4d61506214b37b61ff3e8194e2030b28abca5 +size 341 diff --git a/tests/Images/Input/Png/issues/Issue_2882.png b/tests/Images/Input/Png/issues/Issue_2882.png new file mode 100644 index 0000000000..2d7a51dacb --- /dev/null +++ b/tests/Images/Input/Png/issues/Issue_2882.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cebc98e62bcfe31df73ae7b6980382f4b56bdf7e7e6e9037946f5a84cb51c7d2 +size 1117 diff --git a/tests/Images/Input/Png/issues/issue_2469-i.png b/tests/Images/Input/Png/issues/issue_2469-i.png new file mode 100644 index 0000000000..bd651a3f2d --- /dev/null +++ b/tests/Images/Input/Png/issues/issue_2469-i.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4e0da0601ca5f479684359633c4dbd82881a35631d63477c01e8fd180e31482 +size 2521324 diff --git a/tests/Images/Input/Webp/alpha-blend-2.webp b/tests/Images/Input/Webp/alpha-blend-2.webp new file mode 100644 index 0000000000..3ca4c77cff --- /dev/null +++ b/tests/Images/Input/Webp/alpha-blend-2.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b07a49ab8b3af82fa123faf897ec537cd26d57175b1d6301b617372c06432899 +size 1580484 diff --git a/tests/Images/Input/Webp/alpha-blend-3.webp b/tests/Images/Input/Webp/alpha-blend-3.webp new file mode 100644 index 0000000000..1922f561d5 --- /dev/null +++ b/tests/Images/Input/Webp/alpha-blend-3.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7244a2cfb42285a196fc7846c49da65fac47e5b85f735bc07b131707c8a2d46 +size 948 diff --git a/tests/Images/Input/Webp/alpha-blend-4.webp b/tests/Images/Input/Webp/alpha-blend-4.webp new file mode 100644 index 0000000000..6f4db231f6 --- /dev/null +++ b/tests/Images/Input/Webp/alpha-blend-4.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8c8b98f188d006715bd5bc60593ff2a379078f28a7fc14a51d28ae1cfb279aac +size 2185502 diff --git a/tests/Images/Input/Webp/alpha-blend.webp b/tests/Images/Input/Webp/alpha-blend.webp new file mode 100644 index 0000000000..110d2a594c --- /dev/null +++ b/tests/Images/Input/Webp/alpha-blend.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5dc2762d1c1030fb951e1af57eaa7e66035a7b5e63bd9dc9f9bd50f0ff5c4c3a +size 1297692 diff --git a/tests/Images/Input/Webp/issues/Issue2866.webp b/tests/Images/Input/Webp/issues/Issue2866.webp new file mode 100644 index 0000000000..845569624d --- /dev/null +++ b/tests/Images/Input/Webp/issues/Issue2866.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15e8a52a6d528fe071e73b037543b682bf62da7bab6d98ab690f25dd97f7298e +size 248688