From 8f4b8d2f0a5e506b4b4b3b0989f401b54056849e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 13 Nov 2026 12:40:25 +1000 Subject: [PATCH 1/2] Keep decoded palette colors exact when encoding The PNG, GIF and icon encoders reuse a decoded palette without dithering to keep the original colors. They used the default coarse color matching, which maps colors that share a 5-bit bucket to one entry. They now use exact matching. The first GIF frame quantized with the encoder options instead of the options of the decoded global palette, so it dithered against that palette. It now uses the palette options. The 1-, 2- and 4-bit BMP paths built new quantizer options from the dither settings only. They dropped the color matching mode, the transparent color mode and the transparency threshold of the configured quantizer. They now clone the quantizer options and set only the color count. The Issue2866 reference frames are updated. Against the decoded source, the new output has less error on each compared frame, and frame 0 is exact. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 27 ++++------- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 19 +++++--- .../Formats/Icon/IconEncoderCore.cs | 4 +- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 9 +++- .../Formats/Bmp/BmpEncoderTests.cs | 21 +++++++++ .../Formats/Gif/GifEncoderTests.cs | 28 +++++++++++ .../Formats/Icon/Ico/IcoEncoderTests.cs | 17 +++++++ .../Formats/NearColorPalette.cs | 47 +++++++++++++++++++ .../Formats/Png/PngEncoderTests.cs | 17 +++++++ .../00.gif | 4 +- .../08.gif | 4 +- .../104.gif | 4 +- .../112.gif | 4 +- .../16.gif | 4 +- .../24.gif | 4 +- .../32.gif | 4 +- .../40.gif | 4 +- .../48.gif | 4 +- .../56.gif | 4 +- .../64.gif | 4 +- .../72.gif | 4 +- .../80.gif | 4 +- .../88.gif | 4 +- .../96.gif | 4 +- 24 files changed, 190 insertions(+), 59 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/NearColorPalette.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index af6582420e..b0411e6c71 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -671,12 +671,9 @@ private void Write4BitPixelData( CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions - { - MaxColors = 16, - Dither = this.quantizer.Options.Dither, - DitherScale = this.quantizer.Options.DitherScale - }); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer( + configuration, + this.quantizer.Options.DeepClone(o => o.MaxColors = 16)); frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); @@ -728,12 +725,9 @@ private void Write2BitPixelData( CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions - { - MaxColors = 4, - Dither = this.quantizer.Options.Dither, - DitherScale = this.quantizer.Options.DitherScale - }); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer( + configuration, + this.quantizer.Options.DeepClone(o => o.MaxColors = 4)); frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); @@ -794,12 +788,9 @@ private void Write1BitPixelData( CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions - { - MaxColors = 2, - Dither = this.quantizer.Options.Dither, - DitherScale = this.quantizer.Options.DitherScale - }); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer( + configuration, + this.quantizer.Options.DeepClone(o => o.MaxColors = 2)); frameQuantizer.BuildPalette(this.pixelSamplingStrategy, encodingFrame); diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index d4a6be000a..34ad765fb9 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -124,12 +124,15 @@ public void Encode(Image image, Stream stream, CancellationToken int ti = GetTransparentIndex(quantized, frameMetadata); if (ti >= 0 || gifMetadata.GlobalColorTable.Value.Length < 256) { - // We avoid dithering by default to preserve the original colors. - globalQuantizer = new PaletteQuantizer( - gifMetadata.GlobalColorTable.Value, - options.DeepClone(o => o.Dither = null), - ti, - Color.Transparent); + // We avoid dithering and match colors exactly to preserve the original colors. The first frame + // quantizes with these options too, so they replace the encoder options. + options = options.DeepClone(o => + { + o.Dither = null; + o.ColorMatchingMode = ColorMatchingMode.Exact; + }); + + globalQuantizer = new PaletteQuantizer(gifMetadata.GlobalColorTable.Value, options, ti, Color.Transparent); } else { @@ -418,7 +421,7 @@ private IndexedImageFrame QuantizeFrameAndUpdateMetadata( if (metadata.LocalColorTable?.Length > 0) { // We can use the color data from the decoded metadata here. - // We avoid dithering by default to preserve the original colors. + // We avoid dithering and match colors exactly to preserve the original colors. ReadOnlyMemory palette = metadata.LocalColorTable.Value; if (requiresTransparency && !metadata.HasTransparency) { @@ -438,6 +441,7 @@ private IndexedImageFrame QuantizeFrameAndUpdateMetadata( { o.MaxColors = palette.Length; o.Dither = null; + o.ColorMatchingMode = ColorMatchingMode.Exact; }); PaletteQuantizer quantizer = new(palette, options, transparencyIndex, transparentColor); using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration); @@ -462,6 +466,7 @@ private IndexedImageFrame QuantizeFrameAndUpdateMetadata( { o.MaxColors = palette.Length; o.Dither = null; + o.ColorMatchingMode = ColorMatchingMode.Exact; }); PaletteQuantizer quantizer = new(palette, paletteOptions, transparencyIndex, transparentColor); using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.configuration, quantizer.Options); diff --git a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs index 8d043d7279..bda7c7b1cd 100644 --- a/src/ImageSharp/Formats/Icon/IconEncoderCore.cs +++ b/src/ImageSharp/Formats/Icon/IconEncoderCore.cs @@ -251,8 +251,8 @@ internal void Encode(Image image, Stream stream, int }); } - // Don't dither if we have a palette. We want to preserve as much information as possible. - return new PaletteQuantizer(colorTable.Value, new QuantizerOptions { Dither = null }); + // Don't dither, and match colors exactly, if we have a palette. We want to preserve as much information as possible. + return new PaletteQuantizer(colorTable.Value, new QuantizerOptions { Dither = null, ColorMatchingMode = ColorMatchingMode.Exact }); } /// diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 3d0002774a..3cf91e1620 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -1733,8 +1733,13 @@ private void SanitizeAndSetEncoderOptions( 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. - QuantizerOptions options = new() { Dither = null, TransparentColorMode = encoder.TransparentColorMode }; + // We avoid dithering and match colors exactly to preserve the original colors. + QuantizerOptions options = new() + { + Dither = null, + ColorMatchingMode = ColorMatchingMode.Exact, + TransparentColorMode = encoder.TransparentColorMode + }; this.quantizer = new PaletteQuantizer(metadata.ColorTable.Value, options); } else diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs index 6bd7e0103f..0f47fc3f80 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs @@ -484,4 +484,25 @@ private static void TestBmpEncoderCore( // Does DebugSave & load reference CompareToReferenceInput(): image.VerifyEncoder(provider, "bmp", bitsPerPixel, encoder, customComparer); } + + [Fact] + public void Encode_1BitWithPaletteQuantizer_KeepsQuantizerOptions() + { + // The 1-, 2- and 4-bit paths build their own quantizer options, so they must keep the exact color matching of + // the configured quantizer. + using Image image = NearColorPalette.CreateImage(); + QuantizerOptions options = new() { Dither = null, ColorMatchingMode = ColorMatchingMode.Exact }; + BmpEncoder encoder = new() + { + BitsPerPixel = BmpBitsPerPixel.Bit1, + Quantizer = new PaletteQuantizer(NearColorPalette.Palette, options) + }; + + using MemoryStream memStream = new(); + image.Save(memStream, encoder); + memStream.Position = 0; + using Image output = Image.Load(memStream); + + ImageComparer.Exact.VerifySimilarity(image, output); + } } diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index a465b073b5..47a3bea581 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -454,4 +454,32 @@ public void GifEncoder_CanDecode_AndEncode_Issue3142(TestImageProvider i % 8 == 0; // Image has many frames, only compare a selection of them. image.CompareDebugOutputToReferenceOutputMultiFrame(provider, ImageComparer.Exact, extension: "gif", predicate: Predicate); } + + [Theory] + [InlineData(FrameColorTableMode.Global)] + [InlineData(FrameColorTableMode.Local)] + public void Encode_PaletteFromMetadata_KeepsExactColors(FrameColorTableMode colorTableMode) + { + using Image image = NearColorPalette.CreateImage(); + GifMetadata metadata = image.Metadata.GetGifMetadata(); + GifFrameMetadata frameMetadata = image.Frames.RootFrame.Metadata.GetGifMetadata(); + metadata.ColorTableMode = colorTableMode; + frameMetadata.ColorTableMode = colorTableMode; + + if (colorTableMode == FrameColorTableMode.Global) + { + metadata.GlobalColorTable = NearColorPalette.Palette; + } + else + { + frameMetadata.LocalColorTable = NearColorPalette.Palette; + } + + using MemoryStream stream = new(); + image.Save(stream, new GifEncoder()); + stream.Position = 0; + using Image output = Image.Load(stream); + + ImageComparer.Exact.VerifySimilarity(image, output); + } } diff --git a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs index 08bb537ad7..1b778adf07 100644 --- a/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Icon/Ico/IcoEncoderTests.cs @@ -172,4 +172,21 @@ public void BmpEntry_WritesPartialAlphaMaskByte(int width) Assert.Equal((byte)(0b10000000 >> bitIndex), mask[byteIndex]); } + + [Fact] + public void Encode_PaletteFromMetadata_KeepsExactColors() + { + using Image image = NearColorPalette.CreateImage(); + IcoFrameMetadata metadata = image.Frames.RootFrame.Metadata.GetIcoMetadata(); + metadata.Compression = IconFrameCompression.Bmp; + metadata.BmpBitsPerPixel = BmpBitsPerPixel.Bit8; + metadata.ColorTable = NearColorPalette.Palette; + + using MemoryStream stream = new(); + image.Save(stream, new IcoEncoder()); + stream.Position = 0; + using Image output = Image.Load(stream); + + ImageComparer.Exact.VerifySimilarity(image, output); + } } diff --git a/tests/ImageSharp.Tests/Formats/NearColorPalette.cs b/tests/ImageSharp.Tests/Formats/NearColorPalette.cs new file mode 100644 index 0000000000..7ceec5277a --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/NearColorPalette.cs @@ -0,0 +1,47 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.PixelFormats; + +namespace SixLabors.ImageSharp.Tests.Formats; + +/// +/// Provides an image and a palette for the reuse of a decoded palette. The two colors differ only in red, by 8 and 12. +/// Both fall in the same bucket of the coarse color cache, which keeps 5 bits of each channel, so coarse matching maps +/// both colors to one entry. Only exact matching keeps each pixel's color. +/// +internal static class NearColorPalette +{ + /// + /// The first palette color. + /// + private static readonly Rgba32 First = new(8, 16, 16, 255); + + /// + /// The second palette color. + /// + private static readonly Rgba32 Second = new(12, 16, 16, 255); + + /// + /// Gets the palette: the two colors of . + /// + public static ReadOnlyMemory Palette { get; } = new[] { Color.FromPixel(First), Color.FromPixel(Second) }; + + /// + /// Creates an 8x2 image with the two palette colors in a checkerboard. + /// + /// The image. + public static Image CreateImage() + { + Image image = new(8, 2); + for (int y = 0; y < image.Height; y++) + { + for (int x = 0; x < image.Width; x++) + { + image[x, y] = (x + y) % 2 == 0 ? First : Second; + } + } + + return image; + } +} diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs index eef8d5ba84..0f10c25a7c 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs @@ -750,4 +750,21 @@ private static void TestPngEncoderCore( using Image referenceImage = referenceDecoder.Decode(DecoderOptions.Default, fileStream); ImageComparer.Exact.VerifySimilarity(referenceImage, imageSharpImage); } + + [Fact] + public void Encode_PaletteFromMetadata_KeepsExactColors() + { + using Image image = NearColorPalette.CreateImage(); + PngMetadata metadata = image.Metadata.GetPngMetadata(); + metadata.ColorType = PngColorType.Palette; + metadata.BitDepth = PngBitDepth.Bit8; + metadata.ColorTable = NearColorPalette.Palette; + + using MemoryStream stream = new(); + image.Save(stream, new PngEncoder()); + stream.Position = 0; + using Image output = Image.Load(stream); + + ImageComparer.Exact.VerifySimilarity(image, output); + } } 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 index b219975ade..8668f7102a 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cb4bbef09dc6618380e34c5dcf8612fa5a51ba81a09edc5500be9191f0554d9c -size 49665 +oid sha256:e738470c04121376b42937f0a72eed3f068bc32bdc788cc0d5877bc7f705f419 +size 67611 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 index 2d50761636..fa21a98767 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:81a0d629326bb39cfced1a261542e5f94b423527f95bc45422670091b91583b4 -size 50730 +oid sha256:8dd88420b3855a130213fd2e1f9c4b0cf693d9d5745fbd91914637998f0d69d5 +size 58693 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 index b1b7781a21..5bc203dff5 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e64d9f2f7a8346f62c9b41a14b3e6b71f76a48e07fa42ac9e0d4a5b146a8a9da -size 58856 +oid sha256:5dd3302f5407ff6dca4dd9a202c3cdce2dbf78ba96b58a8db36eed5960cbd4a6 +size 88107 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 index f058764b4f..30192b3ad8 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b01517c53b19f6b151a76cc75142ba3a8a45da8c6e94416447703cbd54ce1a8a -size 48282 +oid sha256:b3eed6eac71ae9a503b6c88ae30496d184327ecbe49df87fc289ff2ce9677273 +size 64570 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 index b9f1e2d099..c8dd6b6f5f 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1db212159613778c962883de9067852da3bea5f3483dd9f967c0aabbcdc1b2f6 -size 64655 +oid sha256:0218a1d54e7b3128a5af68e36eeb80caabd18943febb17afcb2dccef01b16462 +size 72790 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 index c7a1368ce1..3207887584 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9d09ddfff1f26ed7842df5bf4b8938373700658322c85b154a878dd5e3a90dc1 -size 64432 +oid sha256:1c6ee11002ab48ce7b27ef79372b1b13301104769d80ef1d2163cfb0d586d265 +size 69213 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 index ffd61e5123..20560a0c5e 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:18ca31ff631ecc33fe33a893e94e23af8b086a78c3684461e449c02800fffb2b -size 66510 +oid sha256:882d4d81a99b1106e85e60937632a070b9fb0fc45ce92c5a0d6b81dfec9c1e9b +size 69205 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 index eb93ea4d4a..9e65929fb7 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:823358342cbc25a9f7ae34abc2669096acd7c0e0c93a8a0b371e548822ed0897 -size 66912 +oid sha256:e637c9114f0b86ad2f8c9b9045797c6e3570b054156423330d16a0ed45c93fa1 +size 77578 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 index 99f0e64dc6..d5d30dc27a 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:90558311a7b7127d9f970a17ae0630d81507be246f511f1cc3b10c6ee953a25c -size 61986 +oid sha256:bc3932da5adbf696fa39a46752c6f3226fc419ce1160b9e5d5bbb05f1f79fbb5 +size 73044 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 index 8e6410f9bf..27e849063b 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9514b736d946d4e93ba3f59b586d2c29e0c031155f7824756ecf468ef87ea8e6 -size 61367 +oid sha256:45b035a5f676500d5342e2414c9490f5cb9357e1cde180e85308204fcafc9cd4 +size 80432 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 index 2257625c41..41a198aa09 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0334c551b9efcaa9f5c16c4599884b4aabe5129e3f023222be3214cf8623242e -size 60825 +oid sha256:4363f93b7597b383bf41cd24e58af0d26c38b7a193a4579fe482f5040bd6c338 +size 81019 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 index efc9569f4d..c8d3d21e69 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:32a9fecdad6508c1c6beae839717d1854cca1f7b247bff36a00a93cc953f608c -size 57370 +oid sha256:087f1d108c11c1fb324872c2cd87509528d0f2c50ddcb9e8580cdc605c69536f +size 72218 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 index 9f7ae53fb0..a376d5c418 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5d50d4ccba947ef95b9e8a2c4acd08f57c414f4e38a0d03d65b5fee093e4481a -size 67784 +oid sha256:45bb410e3a84b2d6a3969603ba3910bb8d78119082d0596392aeb2742f610aef +size 83018 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 index 22dc30784c..e561220ba3 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:840780f2916cb9d010a95802d9c123c3051bcee5dde7b173a50854e3b5f3636a -size 72552 +oid sha256:7568d209bf268f428d877e79682ad6a34848b394904962f41314b22842c83807 +size 91157 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 index 53e1a35cbf..5bd2dfbe75 100644 --- 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 @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:41db6ded3de84d43dec1175c1481f75a045c5ad126369e4e82ae29ec4bad0bc4 -size 76868 +oid sha256:a25cf03f51458b39323627dcea6ce7eb2d202dc7ebf2c9cb786b210b6af4b956 +size 99523 From dde3eb4b1e945832aec31f3d6b6ecf105a9e1cc6 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 13 Nov 2026 12:54:50 +1000 Subject: [PATCH 2/2] Correct two comments --- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 2 +- tests/ImageSharp.Tests/Formats/NearColorPalette.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 34ad765fb9..262f0c8f52 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -125,7 +125,7 @@ public void Encode(Image image, Stream stream, CancellationToken if (ti >= 0 || gifMetadata.GlobalColorTable.Value.Length < 256) { // We avoid dithering and match colors exactly to preserve the original colors. The first frame - // quantizes with these options too, so they replace the encoder options. + // quantizes with these options too, so they replace the default options. options = options.DeepClone(o => { o.Dither = null; diff --git a/tests/ImageSharp.Tests/Formats/NearColorPalette.cs b/tests/ImageSharp.Tests/Formats/NearColorPalette.cs index 7ceec5277a..804a2148f3 100644 --- a/tests/ImageSharp.Tests/Formats/NearColorPalette.cs +++ b/tests/ImageSharp.Tests/Formats/NearColorPalette.cs @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Tests.Formats; /// -/// Provides an image and a palette for the reuse of a decoded palette. The two colors differ only in red, by 8 and 12. +/// Provides an image and a palette for the reuse of a decoded palette. The two colors differ only in red: 8 and 12. /// Both fall in the same bucket of the coarse color cache, which keeps 5 bits of each channel, so coarse matching maps /// both colors to one entry. Only exact matching keeps each pixel's color. ///