From 16a5ae6963a3333d538814d574b3fe9dc24c283e Mon Sep 17 00:00:00 2001 From: Mohamed El Ghazali <48661966+mohamedelghazali@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:08:17 +0100 Subject: [PATCH 1/5] fix(images): cap Q8 output depth at 8 bits (#1547) Ensure image depth is set to 8 bits for images with higher depth to prevent issues with file writing. --- src/Filee.Engines/Magick/ImageEncoder.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs index e812c7db8..db6739414 100644 --- a/src/Filee.Engines/Magick/ImageEncoder.cs +++ b/src/Filee.Engines/Magick/ImageEncoder.cs @@ -403,7 +403,11 @@ public static void ApplyOptions(IMagickImage image, ImageOptions options, image.BackgroundColor = ParseColor(options.Background); image.Alpha(AlphaAction.Remove); } - + // Magick.NET-Q8 stores 8 bits per channel; a 16-bit source still keeps Depth = 16, which makes + // ImageMagick write 16-bit files (or 12-bit AVIF) padded with duplicated 8-bit samples (#1547). + if (image.Depth > 8) + image.Depth = 8; + var quality = (uint)Math.Clamp(options.Quality, 1, 100); // Turning a JPEG has to re-encode it: at the photo's own quality (Magick's estimate when it read the file), not // above it, or a phone photo saved at 75 comes back at 95, bigger and no better. @@ -517,7 +521,12 @@ public static List Write(IReadOnlyList> frames, strin return written; using var collection = new MagickImageCollection(); foreach (var frame in frames) - collection.Add(frame.Clone()); + { + var copy = frame.Clone(); + if (copy.Depth > 8) + copy.Depth = 8; + collection.Add(copy); + } if (targetFormat == "avif") MatchAlpha(collection); collection.Write(path, format); @@ -530,6 +539,8 @@ public static List Write(IReadOnlyList> frames, strin var path = output.Allocate(extension, frames.Count > 1 ? $"_p{i + 1}" : null); if (path is null) continue; + if (frames[i].Depth > 8) + frames[i].Depth = 8; frames[i].Write(path, format); // Animations and multi-page files written above aren't fitted: the queue refuses them when they're too large. if (options is { MaxSizeMb: > 0 } or { MinSizeMb: > 0 }) @@ -643,7 +654,11 @@ public static void WriteImage(IMagickImage image, string path, string targ else if (targetFormat == "vtf") VtfWriter.Write([image], path); else + { + if (image.Depth > 8) + image.Depth = 8; image.Write(path, ToMagickFormat(targetFormat)); + } } private static void PrepareIcon(IMagickImage image, ImageOptions options) From ea9566ff82c592e0475516a26e3966f7e0a23729 Mon Sep 17 00:00:00 2001 From: Mohamed El Ghazali <48661966+mohamedelghazali@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:17:16 +0100 Subject: [PATCH 2/5] test(images): add 16-bit source depth regression tests (#1547) --- tests/Filee.Engines.Tests/ImageFormatTests.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs index a86aa55b9..5c197328d 100644 --- a/tests/Filee.Engines.Tests/ImageFormatTests.cs +++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs @@ -295,4 +295,55 @@ public void Dds_red_and_blue_are_swapped_only_when_imagemagick_reads_them_swappe DdsChannelOrder.Swap(image, readsSwapped: true); Assert.Equal([0, 0, 255], image.GetPixels().ToByteArray(PixelMapping.RGB)); } + + /// +/// #1547: Magick.NET-Q8 holds 8 bits per channel, so converting a 16-bit source must write an 8-bit file rather +/// than a 16-bit (or 12-bit AVIF) file padded with duplicated 8-bit samples. +/// + [Theory] + [InlineData("tiff")] + [InlineData("psd")] + [InlineData("psb")] + [InlineData("ppm")] + [InlineData("pam")] + [InlineData("miff")] + [InlineData("jxl")] + [InlineData("jp2")] + [InlineData("avif")] + public async Task Sixteen_bit_source_converts_to_eight_bit_output(string format) + { + var dir = fx.NewFolder(); + var source = Path.Combine(dir, "source16.tiff"); + using (var img = new MagickImage(MagickColors.CornflowerBlue, 32, 24)) + { + img.Depth = 16; + img.Write(source, MagickFormat.Tiff); + } + using (var input = new MagickImage(source)) + Assert.Equal(16u, input.Depth); + + var job = await fx.ConvertAsync([source], new Preset { TargetFormat = format }); + + AssertDone(job); + var output = job.Outputs.Single(); + using var result = new MagickImage(output, new MagickReadSettings { Format = ImageEncoder.ToMagickFormat(format) }); + Assert.True(result.Depth <= 8, $"{format} wrote {result.Depth}-bit output for a 16-bit source"); + } + +/// +/// #1547: Depth clamping in only lowers depths above 8, leaving 1-bit monochrome +/// sources untouched. +/// + [Fact] + public void ApplyOptions_keeps_sub_eight_bit_depth_and_caps_sixteen_bit_depth() + { + using var mono = new MagickImage(MagickColors.Black, 8, 8) { Depth = 1 }; + ImageEncoder.ApplyOptions(mono, new ImageOptions(), "tiff"); + Assert.Equal(1u, mono.Depth); + + using var high = new MagickImage(MagickColors.CornflowerBlue, 8, 8) { Depth = 16 }; + ImageEncoder.ApplyOptions(high, new ImageOptions(), "tiff"); + Assert.Equal(8u, high.Depth); + } +} } From a7435356db1ad1c14c30c83c627230f5e98d0f28 Mon Sep 17 00:00:00 2001 From: Mohamed El Ghazali <48661966+mohamedelghazali@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:23:26 +0100 Subject: [PATCH 3/5] fix(tests): remove stray closing brace in ImageFormatTests.cs --- tests/Filee.Engines.Tests/ImageFormatTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs index 5c197328d..a649e3aee 100644 --- a/tests/Filee.Engines.Tests/ImageFormatTests.cs +++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs @@ -346,4 +346,3 @@ public void ApplyOptions_keeps_sub_eight_bit_depth_and_caps_sixteen_bit_depth() Assert.Equal(8u, high.Depth); } } -} From 447baa8d1d030b256610aff617bdaff0537a67c0 Mon Sep 17 00:00:00 2001 From: Mohamed El Ghazali <48661966+mohamedelghazali@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:31:34 +0100 Subject: [PATCH 4/5] style(images): fix whitespace formatting in ImageEncoder.cs --- src/Filee.Engines/Magick/ImageEncoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs index db6739414..ef837002f 100644 --- a/src/Filee.Engines/Magick/ImageEncoder.cs +++ b/src/Filee.Engines/Magick/ImageEncoder.cs @@ -407,7 +407,7 @@ public static void ApplyOptions(IMagickImage image, ImageOptions options, // ImageMagick write 16-bit files (or 12-bit AVIF) padded with duplicated 8-bit samples (#1547). if (image.Depth > 8) image.Depth = 8; - + var quality = (uint)Math.Clamp(options.Quality, 1, 100); // Turning a JPEG has to re-encode it: at the photo's own quality (Magick's estimate when it read the file), not // above it, or a phone photo saved at 75 comes back at 95, bigger and no better. From e706fd57e13543a63e52a876a0b0f7ac350efa36 Mon Sep 17 00:00:00 2001 From: Mohamed El Ghazali <48661966+mohamedelghazali@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:36:15 +0100 Subject: [PATCH 5/5] style(tests): fix whitespace formatting in ImageFormatTests.cs --- tests/Filee.Engines.Tests/ImageFormatTests.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs index a649e3aee..55a8c6d4e 100644 --- a/tests/Filee.Engines.Tests/ImageFormatTests.cs +++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs @@ -297,9 +297,9 @@ public void Dds_red_and_blue_are_swapped_only_when_imagemagick_reads_them_swappe } /// -/// #1547: Magick.NET-Q8 holds 8 bits per channel, so converting a 16-bit source must write an 8-bit file rather -/// than a 16-bit (or 12-bit AVIF) file padded with duplicated 8-bit samples. -/// + /// #1547: Magick.NET-Q8 holds 8 bits per channel, so converting a 16-bit source must write an 8-bit file rather + /// than a 16-bit (or 12-bit AVIF) file padded with duplicated 8-bit samples. + /// [Theory] [InlineData("tiff")] [InlineData("psd")] @@ -312,8 +312,8 @@ public void Dds_red_and_blue_are_swapped_only_when_imagemagick_reads_them_swappe [InlineData("avif")] public async Task Sixteen_bit_source_converts_to_eight_bit_output(string format) { - var dir = fx.NewFolder(); - var source = Path.Combine(dir, "source16.tiff"); + var dir = fx.NewFolder(); + var source = Path.Combine(dir, "source16.tiff"); using (var img = new MagickImage(MagickColors.CornflowerBlue, 32, 24)) { img.Depth = 16; @@ -330,10 +330,10 @@ public async Task Sixteen_bit_source_converts_to_eight_bit_output(string format) Assert.True(result.Depth <= 8, $"{format} wrote {result.Depth}-bit output for a 16-bit source"); } -/// -/// #1547: Depth clamping in only lowers depths above 8, leaving 1-bit monochrome -/// sources untouched. -/// + /// + /// #1547: Depth clamping in only lowers depths above 8, leaving 1-bit monochrome + /// sources untouched. + /// [Fact] public void ApplyOptions_keeps_sub_eight_bit_depth_and_caps_sixteen_bit_depth() {