Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Filee.Engines/Magick/ImageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ public static void ApplyOptions(IMagickImage<byte> 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
Expand Down Expand Up @@ -517,7 +521,12 @@ public static List<string> Write(IReadOnlyList<IMagickImage<byte>> 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);
Expand All @@ -530,6 +539,8 @@ public static List<string> Write(IReadOnlyList<IMagickImage<byte>> 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 })
Expand Down Expand Up @@ -643,7 +654,11 @@ public static void WriteImage(IMagickImage<byte> 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<byte> image, ImageOptions options)
Expand Down
50 changes: 50 additions & 0 deletions tests/Filee.Engines.Tests/ImageFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/// <summary>
/// #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.
/// </summary>
[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");
}

/// <summary>
/// #1547: Depth clamping in <see cref="ImageEncoder"/> only lowers depths above 8, leaving 1-bit monochrome
/// sources untouched.
/// </summary>
[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);
}
}
Loading