diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs index e812c7db8..ef837002f 100644 --- a/src/Filee.Engines/Magick/ImageEncoder.cs +++ b/src/Filee.Engines/Magick/ImageEncoder.cs @@ -403,6 +403,10 @@ 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 @@ -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) diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs index a86aa55b9..55a8c6d4e 100644 --- a/tests/Filee.Engines.Tests/ImageFormatTests.cs +++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs @@ -295,4 +295,54 @@ 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); + } }