Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(dds): swap red and blue back where ImageMagick reads them swapped (#621) (#627)

10^8Doggy1d3887cea005
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 32387c3d..42fdc3a0 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -196,7 +196,8 @@ pinned source in `build/build-linux-native.py` like rhwp (PR #113 did this for S
still picture through PNG.
- **More raster formats** through ImageMagick's own coders, each checked against `MagickNET.SupportedFormats` and
converted there and back in `ImageFormatTests`: QOI, MNG (animated), DDS (DXT block compression, which is lossy; of
- a texture with mipmaps the largest image is read), PCX, CUR (at most 256 × 256, larger pictures are scaled down), XPM,
+ a texture with mipmaps the largest image is read; uncompressed 32-bit pixels stored R, G, B, A, which ImageMagick
+ reads with red and blue swapped, are swapped back when converting and in the crop tool), PCX, CUR (at most 256 × 256, larger pictures are scaled down), XPM,
XBM and WBMP (1-bit), PAM, PFM, SGI (`.sgi`, `.rgb`, `.rgba`, `.bw`, always read as SGI; ImageMagick would take
`.rgb` for headerless pixels), Sun Raster (`.ras`, `.sun`) and MIFF. XBM, WBMP, PFM, PCX and Sun Raster are written
without transparency (flattened onto the preset's background): PFM has none, and the alpha planes ImageMagick can
diff --git a/src/Filee.App/Services/CropService.cs b/src/Filee.App/Services/CropService.cs
index a09994de..26030ece 100644
--- a/src/Filee.App/Services/CropService.cs
+++ b/src/Filee.App/Services/CropService.cs
@@ -69,6 +69,8 @@ public static class CropService
? new MagickImage(inputPath, readSettings)
: new MagickImage(inputPath);
+ if (readSettings.Format == MagickFormat.Dds)
+ DdsChannelOrder.Correct(inputPath, image);
image.AutoOrient();
var origWidth = (int)image.Width;
var origHeight = (int)image.Height;
@@ -141,6 +143,8 @@ public static class CropService
? new MagickImage(inputPath, readSettings)
: new MagickImage(inputPath);
+ if (readSettings.Format == MagickFormat.Dds)
+ DdsChannelOrder.Correct(inputPath, image);
image.AutoOrient();
var clampedX = Math.Clamp(x, 0, Math.Max(0, (int)image.Width - 1));
diff --git a/src/Filee.Engines/Magick/DdsChannelOrder.cs b/src/Filee.Engines/Magick/DdsChannelOrder.cs
new file mode 100644
index 00000000..cae60e8a
--- /dev/null
+++ b/src/Filee.Engines/Magick/DdsChannelOrder.cs
@@ -0,0 +1,118 @@
+// Puts red and blue back where ImageMagick swaps them in one uncompressed DDS layout: 32-bit pixels stored R, G, B, A
+// (masks 0xFF, 0xFF00, 0xFF0000, 0xFF000000, as DirectXTex writes R8G8B8A8 without a DX10 header). ImageMagick's coder
+// compares the alpha mask with a mistyped constant and reads these bytes as B, G, R, A. Whether the bundled ImageMagick
+// still does is checked once on a 1 × 1 picture of that layout, so a fixed coder is left alone.
+
+using System.Buffers.Binary;
+using ImageMagick;
+
+namespace Filee.Engines.Magick;
+
+/// <summary>Corrects the red/blue order of the DDS pictures ImageMagick reads swapped.</summary>
+internal static class DdsChannelOrder
+{
+ private const int HeaderLength = 128;
+ private const uint RgbFlag = 0x40;
+ private const uint FourCcFlag = 0x4;
+ private const uint AlphaPixelsFlag = 0x1;
+
+ private enum Layout { Other, Alpha, NoAlpha }
+
+ private static readonly Lazy<bool> AlphaReadsSwapped = new(() => ReadsSwapped(Layout.Alpha));
+ private static readonly Lazy<bool> NoAlphaReadsSwapped = new(() => ReadsSwapped(Layout.NoAlpha));
+
+ /// <summary>Swaps red and blue back in every frame ImageMagick read from the DDS at <paramref name="path"/>, when needed.</summary>
+ public static void Correct(string path, IMagickImageCollection<byte> images)
+ {
+ var swapped = NeedsCorrection(path);
+ foreach (var image in images)
+ Swap(image, swapped);
+ }
+
+ /// <summary>Swaps red and blue back in the picture ImageMagick read from the DDS at <paramref name="path"/>, when needed.</summary>
+ public static void Correct(string path, IMagickImage<byte> image) => Swap(image, NeedsCorrection(path));
+
+ /// <summary>Swaps red and blue of <paramref name="image"/> when <paramref name="readsSwapped"/>.</summary>
+ internal static void Swap(IMagickImage<byte> image, bool readsSwapped)
+ {
+ if (!readsSwapped)
+ return;
+ var (read, written) = image.HasAlpha ? (PixelMapping.RGBA, PixelMapping.BGRA) : (PixelMapping.RGB, PixelMapping.BGR);
+ var pixels = image.GetPixels().ToByteArray(read)!;
+ image.ImportPixels(pixels, new PixelImportSettings(image.Width, image.Height, StorageType.Char, written));
+ }
+
+ private static bool NeedsCorrection(string path) => ReadLayout(path) switch
+ {
+ Layout.Alpha => AlphaReadsSwapped.Value,
+ Layout.NoAlpha => NoAlphaReadsSwapped.Value,
+ _ => false,
+ };
+
+ /// <summary>The layout of the file's header, read after ImageMagick has read the file; only its first 128 bytes.</summary>
+ private static Layout ReadLayout(string path)
+ {
+ Span<byte> header = stackalloc byte[HeaderLength];
+ try
+ {
+ using var file = File.OpenRead(path);
+ if (file.ReadAtLeast(header, HeaderLength, throwOnEndOfStream: false) < HeaderLength)
+ return Layout.Other;
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ return Layout.Other;
+ }
+ return LayoutOf(header);
+ }
+
+ private static Layout LayoutOf(ReadOnlySpan<byte> header)
+ {
+ var flags = At(header, 80);
+ var matches = header[..4].SequenceEqual("DDS "u8) && At(header, 4) == 124 && At(header, 76) == 32
+ && (flags & RgbFlag) != 0 && (flags & FourCcFlag) == 0 && At(header, 88) == 32
+ && At(header, 92) == 0x000000FF && At(header, 96) == 0x0000FF00 && At(header, 100) == 0x00FF0000
+ && At(header, 104) == 0xFF000000;
+ if (!matches)
+ return Layout.Other;
+ return (flags & AlphaPixelsFlag) != 0 ? Layout.Alpha : Layout.NoAlpha;
+ }
+
+ private static uint At(ReadOnlySpan<byte> header, int offset) => BinaryPrimitives.ReadUInt32LittleEndian(header[offset..]);
+
+ /// <summary>Whether the bundled ImageMagick reads a red pixel of <paramref name="layout"/> as blue.</summary>
+ private static bool ReadsSwapped(Layout layout)
+ {
+ var data = new byte[HeaderLength + 4];
+ var header = data.AsSpan();
+ "DDS "u8.CopyTo(header);
+ Put(header, 4, 124);
+ Put(header, 8, 0x100F); // caps, height, width, pitch and pixel format are set
+ Put(header, 12, 1);
+ Put(header, 16, 1);
+ Put(header, 20, 4);
+ Put(header, 76, 32);
+ Put(header, 80, layout == Layout.Alpha ? RgbFlag | AlphaPixelsFlag : RgbFlag);
+ Put(header, 88, 32);
+ Put(header, 92, 0x000000FF);
+ Put(header, 96, 0x0000FF00);
+ Put(header, 100, 0x00FF0000);
+ Put(header, 104, 0xFF000000);
+ Put(header, 108, 0x1000); // a texture
+ byte[] opaqueRed = [0xFF, 0, 0, 0xFF];
+ opaqueRed.CopyTo(data, HeaderLength);
+ try
+ {
+ using var image = new MagickImage(data, new MagickReadSettings { Format = MagickFormat.Dds });
+ var color = image.GetPixels().GetPixel(0, 0).ToColor()!;
+ return color.B > color.R;
+ }
+ catch (MagickException)
+ {
+ return false;
+ }
+ }
+
+ private static void Put(Span<byte> header, int offset, uint value) =>
+ BinaryPrimitives.WriteUInt32LittleEndian(header[offset..], value);
+}
diff --git a/src/Filee.Engines/Magick/ImageReader.cs b/src/Filee.Engines/Magick/ImageReader.cs
index d88b5185..8bd050ef 100644
--- a/src/Filee.Engines/Magick/ImageReader.cs
+++ b/src/Filee.Engines/Magick/ImageReader.cs
@@ -37,6 +37,8 @@ internal static class ImageReader
try
{
images.Read(path, settings);
+ if (formatId == "dds")
+ DdsChannelOrder.Correct(path, images);
}
catch (MagickException ex) when (formatId == "dcm")
{
diff --git a/tests/Filee.App.Tests/CropServiceTests.cs b/tests/Filee.App.Tests/CropServiceTests.cs
index b0413cfa..a8a8c24a 100644
--- a/tests/Filee.App.Tests/CropServiceTests.cs
+++ b/tests/Filee.App.Tests/CropServiceTests.cs
@@ -266,6 +266,86 @@ public class CropServiceTests
}
}
+ /// <summary>An uncompressed DDS stored R, G, B, A (masks 0xFF … 0xFF000000), which ImageMagick reads with red and blue swapped.</summary>
+ private static string WriteRgbaDds(string folder, string name, uint width, uint flags, byte[] pixels)
+ {
+ var data = new byte[128 + pixels.Length];
+ var header = data.AsSpan();
+ "DDS "u8.CopyTo(header);
+ foreach (var (offset, value) in new (int, uint)[]
+ {
+ (4, 124), (8, 0x100F), (12, 4), (16, width), (20, width * 4), (76, 32), (80, flags), (88, 32),
+ (92, 0xFF), (96, 0xFF00), (100, 0xFF0000), (104, 0xFF000000), (108, 0x1000),
+ })
+ System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(header[offset..], value);
+ pixels.CopyTo(data, 128);
+ var path = Path.Combine(folder, name);
+ File.WriteAllBytes(path, data);
+ return path;
+ }
+
+ private static byte[] PreviewPixel(Avalonia.Media.Imaging.Bitmap bitmap, int x, int y)
+ {
+ var buffer = new byte[4];
+ var native = System.Runtime.InteropServices.Marshal.AllocHGlobal(4);
+ try
+ {
+ bitmap.CopyPixels(new Avalonia.PixelRect(x, y, 1, 1), native, 4, 4);
+ System.Runtime.InteropServices.Marshal.Copy(native, buffer, 0, 4);
+ }
+ finally
+ {
+ System.Runtime.InteropServices.Marshal.FreeHGlobal(native);
+ }
+ return bitmap.Format == Avalonia.Platform.PixelFormat.Rgba8888 ? buffer : [buffer[2], buffer[1], buffer[0], buffer[3]];
+ }
+
+ /// <summary>#621: the crop tool shows and saves a DDS stored R, G, B, A in its own colours.</summary>
+ [AvaloniaTheory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public async Task Dds_stored_rgba_is_previewed_and_cropped_in_its_own_colours(uint flags)
+ {
+ TestServices.EnsureInitialized("en");
+ var folder = Directory.CreateTempSubdirectory("filee-dds-crop-test-").FullName;
+ try
+ {
+ byte[] red = [255, 0, 0, 255], green = [0, 255, 0, 255], blue = [0, 0, 255, 255];
+ byte[][] colours = [red, green, blue];
+ // Three 4 × 4 blocks side by side, matching DXT blocks so the saved DDS keeps each colour pure.
+ var pixels = Enumerable.Range(0, 4).SelectMany(_ => colours.SelectMany(c => Enumerable.Repeat(c, 4).SelectMany(p => p))).ToArray();
+ var source = WriteRgbaDds(folder, "rgba.dds", 12, flags, pixels);
+ var store = AppHost.Get<UserDataStore>();
+ var loc = AppHost.Get<ILocalizer>();
+ var preset = BuiltInData.CreatePresets().Single(p => p.Id == "tool-crop");
+
+ var preview = await CropService.LoadPreviewAsync(source);
+ using (preview.Bitmap)
+ {
+ for (var block = 0; block < 3; block++)
+ Assert.Equal(colours[block], PreviewPixel(preview.Bitmap, block * 4 + 1, 1));
+ }
+
+ for (var block = 0; block < 3; block++)
+ {
+ var result = await CropService.CropFileAsync(source, block * 4, 0, 4, 4, preset, store, loc);
+
+ Assert.Equal(CropStatus.Success, result.Status);
+ using (var cropped = new MagickImage(result.OutputPath!, new MagickReadSettings { Format = MagickFormat.Dds }))
+ {
+ var rgb = cropped.GetPixels().ToByteArray(PixelMapping.RGB)!;
+ Assert.All(rgb.Chunk(3), p => Assert.All(Enumerable.Range(0, 3).Where(c => c != block),
+ c => Assert.True(p[block] - p[c] >= 128, $"block {block}: {string.Join(',', p)}")));
+ }
+ File.Delete(result.OutputPath!);
+ }
+ }
+ finally
+ {
+ try { Directory.Delete(folder, true); } catch (IOException) { }
+ }
+ }
+
private static async Task WaitFor(Func<bool> condition)
{
for (var i = 0; i < 500 && !condition(); i++)
diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs
index 673be188..8a846c44 100644
--- a/tests/Filee.Engines.Tests/ImageFormatTests.cs
+++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs
@@ -155,4 +155,74 @@ public class ImageFormatTests(EngineFixture fx) : IClassFixture<EngineFixture>
using var cursor = new MagickImage(job.Outputs.Single(), new MagickReadSettings { Format = MagickFormat.Cur });
Assert.Equal((256u, 128u), (cursor.Width, cursor.Height));
}
+
+ /// <summary>4 × 4 RGBA pixels: red, green, blue and half-transparent yellow, four times.</summary>
+ private static readonly byte[] DdsColours =
+ [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 0, 128], 4).SelectMany(b => b)];
+
+ /// <summary>An uncompressed 4 × 4 DDS without a DX10 header: pixel format flags, channel masks and pixel bytes.</summary>
+ private static byte[] Dds(uint flags, uint red, uint green, uint blue, uint alpha, byte[] pixels)
+ {
+ var data = new byte[128 + pixels.Length];
+ var header = data.AsSpan();
+ "DDS "u8.CopyTo(header);
+ foreach (var (offset, value) in new (int, uint)[]
+ {
+ (4, 124), (8, 0x100F), (12, 4), (16, 4), (20, 16), (76, 32), (80, flags), (88, 32),
+ (92, red), (96, green), (100, blue), (104, alpha), (108, 0x1000),
+ })
+ System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(header[offset..], value);
+ pixels.CopyTo(data, 128);
+ return data;
+ }
+
+ /// <summary>
+ /// #621: 32-bit DDS pixels stored R, G, B, A (masks 0xFF … 0xFF000000), which ImageMagick reads with red and blue
+ /// swapped, keep their colours whether or not the file says it has alpha (without, the fourth byte is padding).
+ /// </summary>
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task Dds_stored_rgba_converts_with_its_own_colours(bool alphaFlag)
+ {
+ var dir = fx.NewFolder();
+ var source = Path.Combine(dir, "rgba.dds");
+ File.WriteAllBytes(source, Dds(alphaFlag ? 0x41u : 0x40u, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, DdsColours));
+
+ var job = await fx.ConvertAsync([source], new Preset { TargetFormat = "png" });
+
+ AssertDone(job);
+ using var png = new MagickImage(job.Outputs.Single());
+ var expected = alphaFlag ? DdsColours : [.. DdsColours.Where((_, i) => i % 4 != 3)];
+ Assert.Equal(expected, png.GetPixels().ToByteArray(alphaFlag ? PixelMapping.RGBA : PixelMapping.RGB));
+ }
+
+ /// <summary>The usual layout, stored B, G, R, A, is read right by ImageMagick and left as it is.</summary>
+ [Fact]
+ public async Task Dds_stored_bgra_keeps_its_colours()
+ {
+ var dir = fx.NewFolder();
+ var source = Path.Combine(dir, "bgra.dds");
+ byte[] bgra = [.. DdsColours.Chunk(4).SelectMany(p => new[] { p[2], p[1], p[0], p[3] })];
+ File.WriteAllBytes(source, Dds(0x41, 0xFF0000, 0xFF00, 0xFF, 0xFF000000, bgra));
+
+ var job = await fx.ConvertAsync([source], new Preset { TargetFormat = "png" });
+
+ AssertDone(job);
+ using var png = new MagickImage(job.Outputs.Single());
+ Assert.Equal(DdsColours, png.GetPixels().ToByteArray(PixelMapping.RGBA));
+ }
+
+ /// <summary>The swap only happens when ImageMagick was found to read the layout swapped, so a fixed coder is left alone.</summary>
+ [Fact]
+ public void Dds_red_and_blue_are_swapped_only_when_imagemagick_reads_them_swapped()
+ {
+ using var image = new MagickImage(MagickColors.Red, 1, 1);
+
+ DdsChannelOrder.Swap(image, readsSwapped: false);
+ Assert.Equal([255, 0, 0], image.GetPixels().ToByteArray(PixelMapping.RGB));
+
+ DdsChannelOrder.Swap(image, readsSwapped: true);
+ Assert.Equal([0, 0, 255], image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
}