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): keep the colours of DDS pictures inside other files

SeoJCalive9985dc3af8f6
diff --git a/src/Filee.Engines/Ebooks/Book.cs b/src/Filee.Engines/Ebooks/Book.cs
index 18e12820..be845909 100644
--- a/src/Filee.Engines/Ebooks/Book.cs
+++ b/src/Filee.Engines/Ebooks/Book.cs
@@ -3,6 +3,7 @@
using System.IO.Compression;
using Filee.Engines.Hwp.Hwpx;
+using Filee.Engines.Magick;
namespace Filee.Engines.Ebooks;
@@ -130,6 +131,7 @@ internal static class EbookFiles
try
{
using var image = new ImageMagick.MagickImage(path);
+ DdsChannelOrder.Correct(path, image);
var jpeg = !image.HasAlpha && mediaType is "image/webp" or "image/avif" or "image/jxl" or "image/tiff";
Directory.CreateDirectory(folder);
var file = Path.Combine(folder, $"{Guid.NewGuid():N}.{(jpeg ? "jpg" : "png")}");
diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs
index 6664d61b..04814c0e 100644
--- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxWriter.cs
@@ -12,6 +12,7 @@
using System.Globalization;
using System.IO.Compression;
using System.Text;
+using Filee.Engines.Magick;
using ImageMagick;
namespace Filee.Engines.Hwp.Hwpx.Docx;
@@ -200,6 +201,7 @@ internal sealed partial class DocxWriter
{
// WEBP, TIFF, SVG, AVIF ... → PNG, which every Word version shows.
using var image = new MagickImage(source);
+ DdsChannelOrder.Correct(source, image);
image.Write(target, MagickFormat.Png);
}
}
diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.cs
index 5c2e1707..51b71edb 100644
--- a/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxWriter.cs
@@ -12,6 +12,7 @@ using System.IO.Compression;
using System.Security;
using System.Text;
using System.Xml.Linq;
+using Filee.Engines.Magick;
using ImageMagick;
namespace Filee.Engines.Hwp.Hwpx;
@@ -119,6 +120,7 @@ internal sealed partial class HwpxWriter
{
// WEBP, TIFF, SVG, EMF, WMF ... → PNG, which 한글 always displays.
using var image = new MagickImage(path);
+ DdsChannelOrder.Correct(path, image);
image.Write(target, MagickFormat.Png);
}
}
diff --git a/src/Filee.Engines/Magick/DdsChannelOrder.cs b/src/Filee.Engines/Magick/DdsChannelOrder.cs
index cae60e8a..c2ccf6b7 100644
--- a/src/Filee.Engines/Magick/DdsChannelOrder.cs
+++ b/src/Filee.Engines/Magick/DdsChannelOrder.cs
@@ -32,6 +32,9 @@ internal static class DdsChannelOrder
/// <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 back in the picture ImageMagick read from DDS <paramref name="data"/>, when needed.</summary>
+ public static void Correct(ReadOnlySpan<byte> data, IMagickImage<byte> image) => Swap(image, NeedsCorrection(data));
+
/// <summary>Swaps red and blue of <paramref name="image"/> when <paramref name="readsSwapped"/>.</summary>
internal static void Swap(IMagickImage<byte> image, bool readsSwapped)
{
@@ -42,7 +45,11 @@ internal static class DdsChannelOrder
image.ImportPixels(pixels, new PixelImportSettings(image.Width, image.Height, StorageType.Char, written));
}
- private static bool NeedsCorrection(string path) => ReadLayout(path) switch
+ private static bool NeedsCorrection(string path) => NeedsCorrection(ReadLayout(path));
+
+ internal static bool NeedsCorrection(ReadOnlySpan<byte> data) => NeedsCorrection(LayoutOf(data));
+
+ private static bool NeedsCorrection(Layout layout) => layout switch
{
Layout.Alpha => AlphaReadsSwapped.Value,
Layout.NoAlpha => NoAlphaReadsSwapped.Value,
@@ -68,6 +75,8 @@ internal static class DdsChannelOrder
private static Layout LayoutOf(ReadOnlySpan<byte> header)
{
+ if (header.Length < HeaderLength)
+ return Layout.Other;
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
diff --git a/src/Filee.Engines/Models3D/GltfFormat.cs b/src/Filee.Engines/Models3D/GltfFormat.cs
index c90d83fd..aa768156 100644
--- a/src/Filee.Engines/Models3D/GltfFormat.cs
+++ b/src/Filee.Engines/Models3D/GltfFormat.cs
@@ -7,6 +7,7 @@
using System.Numerics;
using System.Text.Json.Nodes;
+using Filee.Engines.Magick;
using ImageMagick;
using SharpGLTF.Memory;
using SharpGLTF.Schema2;
@@ -314,6 +315,7 @@ internal static class GltfFormat
try
{
using var image = new MagickImage(texture.Data);
+ DdsChannelOrder.Correct(texture.Data, image);
return image.ToByteArray(MagickFormat.Png);
}
catch (MagickException ex)
diff --git a/tests/Filee.Engines.Tests/DocxWriterTests.cs b/tests/Filee.Engines.Tests/DocxWriterTests.cs
index 6a715d70..a1473d66 100644
--- a/tests/Filee.Engines.Tests/DocxWriterTests.cs
+++ b/tests/Filee.Engines.Tests/DocxWriterTests.cs
@@ -3,6 +3,7 @@
// installed, converted to PDF by it as an independent reader. The round trip DOCX → model → DOCX → model checks that
// the layout the model carries survives (page setup, sections, headers, tables, objects, notes, formatting).
+using System.IO.Compression;
using Filee.Core.Conversion;
using Filee.Core.Presets;
using Filee.Engines.Hwp.Hwpx;
@@ -39,6 +40,28 @@ public class DocxWriterTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal("docx-writer", Assert.Single(BuiltInPlanner().Plan("md", "docx")!.Steps).Converter.Id);
}
+ [Theory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public async Task Markdown_dds_picture_keeps_its_colours_in_docx(uint flags)
+ {
+ var dir = fx.NewFolder();
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ File.WriteAllBytes(Path.Combine(dir, "red.dds"), ImageFormatTests.Dds(flags, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels));
+ var md = Path.Combine(dir, "document.md");
+ await File.WriteAllTextAsync(md, "![red](red.dds)", TestContext.Current.CancellationToken);
+
+ var docx = await ConvertAsync(md);
+
+ using var zip = ZipFile.OpenRead(docx);
+ using var stream = Assert.Single(zip.Entries, e => e.FullName.StartsWith("word/media/", StringComparison.Ordinal)
+ && e.FullName.EndsWith(".png", StringComparison.Ordinal)).Open();
+ using var image = new MagickImage(stream);
+ Assert.Equal(MagickFormat.Png, image.Format);
+ Assert.Equal((4u, 4u), (image.Width, image.Height));
+ Assert.Equal(pixels.Where((_, i) => i % 4 != 3).ToArray(), image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
[Fact]
public async Task Markdown_becomes_a_word_document_with_real_headings_lists_and_tables()
{
diff --git a/tests/Filee.Engines.Tests/EbookTests.cs b/tests/Filee.Engines.Tests/EbookTests.cs
index 76ab12f6..690df0b5 100644
--- a/tests/Filee.Engines.Tests/EbookTests.cs
+++ b/tests/Filee.Engines.Tests/EbookTests.cs
@@ -202,6 +202,28 @@ public class EbookTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.DoesNotContain("![", withoutPictures);
}
+ [Theory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public async Task Markdown_dds_picture_keeps_its_colours_in_epub(uint flags)
+ {
+ var dir = fx.NewFolder();
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ File.WriteAllBytes(Path.Combine(dir, "red.dds"), ImageFormatTests.Dds(flags, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels));
+ var md = Path.Combine(dir, "book.md");
+ await File.WriteAllTextAsync(md, "![red](red.dds)", TestContext.Current.CancellationToken);
+
+ var epub = await ConvertAsync(md, "epub");
+
+ using var zip = ZipFile.OpenRead(epub);
+ using var stream = Assert.Single(zip.Entries, e => e.FullName.StartsWith("OEBPS/images/", StringComparison.Ordinal)
+ && e.FullName.EndsWith(".png", StringComparison.Ordinal)).Open();
+ using var image = new MagickImage(stream);
+ Assert.Equal(MagickFormat.Png, image.Format);
+ Assert.Equal((4u, 4u), (image.Width, image.Height));
+ Assert.Equal(pixels.Where((_, i) => i % 4 != 3).ToArray(), image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
[Fact]
public async Task Markdown_to_epub_is_a_valid_package_that_reads_back()
{
diff --git a/tests/Filee.Engines.Tests/HwpxWriterTests.cs b/tests/Filee.Engines.Tests/HwpxWriterTests.cs
index ef92c7a8..6eaec485 100644
--- a/tests/Filee.Engines.Tests/HwpxWriterTests.cs
+++ b/tests/Filee.Engines.Tests/HwpxWriterTests.cs
@@ -2,6 +2,7 @@
// Output is checked structurally, read back with Unhwp and rendered with rhwp. DOCX has its own tests
// (DocxToHwpxTests) because it is read without Pandoc.
+using System.IO.Compression;
using System.Text;
using Filee.Core.Conversion;
using Filee.Core.Presets;
@@ -57,6 +58,28 @@ public class HwpxWriterTests(EngineFixture fx) : IClassFixture<EngineFixture>
return job.Outputs.Single();
}
+ [Theory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public async Task Markdown_dds_picture_keeps_its_colours_in_hwpx(uint flags)
+ {
+ var dir = fx.NewFolder();
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ File.WriteAllBytes(Path.Combine(dir, "red.dds"), ImageFormatTests.Dds(flags, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels));
+ var md = Path.Combine(dir, "document.md");
+ await File.WriteAllTextAsync(md, "![red](red.dds)", TestContext.Current.CancellationToken);
+
+ var hwpx = await ConvertAsync(md);
+
+ using var zip = ZipFile.OpenRead(hwpx);
+ using var stream = Assert.Single(zip.Entries, e => e.FullName.StartsWith("BinData/", StringComparison.Ordinal)
+ && e.FullName.EndsWith(".png", StringComparison.Ordinal)).Open();
+ using var image = new MagickImage(stream);
+ Assert.Equal(MagickFormat.Png, image.Format);
+ Assert.Equal((4u, 4u), (image.Width, image.Height));
+ Assert.Equal(pixels.Where((_, i) => i % 4 != 3).ToArray(), image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
[Fact]
public async Task Markdown_becomes_a_valid_hwpx_that_other_readers_understand()
{
diff --git a/tests/Filee.Engines.Tests/ImageFormatTests.cs b/tests/Filee.Engines.Tests/ImageFormatTests.cs
index 8a846c44..7588695a 100644
--- a/tests/Filee.Engines.Tests/ImageFormatTests.cs
+++ b/tests/Filee.Engines.Tests/ImageFormatTests.cs
@@ -161,7 +161,7 @@ public class ImageFormatTests(EngineFixture fx) : IClassFixture<EngineFixture>
[.. 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)
+ internal 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();
@@ -176,6 +176,76 @@ public class ImageFormatTests(EngineFixture fx) : IClassFixture<EngineFixture>
return data;
}
+ [Fact]
+ public void Dds_invalid_bytes_do_not_request_or_apply_a_swap()
+ {
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ var valid = Dds(0x41, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels);
+ var expected = pixels.Where((_, i) => i % 4 != 3).ToArray();
+ void Unchanged(ReadOnlySpan<byte> data)
+ {
+ Assert.False(DdsChannelOrder.NeedsCorrection(data));
+ using var image = new MagickImage(MagickColors.Red, 4, 4);
+ DdsChannelOrder.Correct(data, image);
+ Assert.Equal(expected, image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
+ Unchanged(default);
+ Unchanged((byte[]?)null);
+ Unchanged([]);
+ for (var length = 1; length < 128; length++)
+ Unchanged(valid.AsSpan(0, length));
+ foreach (var (offset, value) in new (int, uint)[]
+ {
+ (0, 0), (4, 123), (76, 31), (80, 0x1), (80, 0x45), (88, 24),
+ (92, 0), (96, 0), (100, 0), (104, 0),
+ })
+ {
+ var invalid = valid.ToArray();
+ System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(invalid.AsSpan(offset), value);
+ Unchanged(invalid.AsSpan(0, 128));
+ Unchanged(invalid);
+ }
+ var dx10 = valid.ToArray();
+ System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(dx10.AsSpan(80), 0x45);
+ "DX10"u8.CopyTo(dx10.AsSpan(84));
+ Unchanged(dx10.AsSpan(0, 128));
+ Unchanged(dx10);
+ var bgra = Dds(0x41, 0xFF0000, 0xFF00, 0xFF, 0xFF000000, pixels);
+ Unchanged(bgra.AsSpan(0, 128));
+ Unchanged(bgra);
+ }
+
+ [Theory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public void Dds_byte_and_file_correction_use_the_same_probe(uint flags)
+ {
+ var dir = fx.NewFolder();
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ var data = Dds(flags, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels);
+ var header = data.AsSpan(0, 128);
+ var path = Path.Combine(dir, "red.dds");
+ File.WriteAllBytes(path, data);
+ using var fromFile = new MagickImage(path);
+ var color = fromFile.GetPixels().GetPixel(0, 0).ToColor()!;
+ var readsSwapped = color.B > color.R;
+ TestContext.Current.TestOutputHelper!.WriteLine($"flags=0x{flags:X2}: R={color.R}, B={color.B}, B > R={readsSwapped}");
+ Assert.Equal(readsSwapped, DdsChannelOrder.NeedsCorrection(data));
+ Assert.Equal(readsSwapped, DdsChannelOrder.NeedsCorrection(header));
+ using var fromBytes = new MagickImage(data);
+ using var fromHeader = new MagickImage(data);
+
+ DdsChannelOrder.Correct(path, fromFile);
+ DdsChannelOrder.Correct(data, fromBytes);
+ DdsChannelOrder.Correct(header, fromHeader);
+
+ var expected = pixels.Where((_, i) => i % 4 != 3).ToArray();
+ Assert.Equal(expected, fromFile.GetPixels().ToByteArray(PixelMapping.RGB));
+ Assert.Equal(expected, fromBytes.GetPixels().ToByteArray(PixelMapping.RGB));
+ Assert.Equal(expected, fromHeader.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
/// <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).
diff --git a/tests/Filee.Engines.Tests/ModelTests.cs b/tests/Filee.Engines.Tests/ModelTests.cs
index c5a5b09d..9b2499e4 100644
--- a/tests/Filee.Engines.Tests/ModelTests.cs
+++ b/tests/Filee.Engines.Tests/ModelTests.cs
@@ -215,6 +215,59 @@ public class ModelTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal(ModelBuilders.Checker(), File.ReadAllBytes(png));
}
+ [Theory]
+ [InlineData(0x41u)]
+ [InlineData(0x40u)]
+ public async Task Obj_dds_texture_keeps_its_colours_in_glb(uint flags)
+ {
+ var folder = fx.NewFolder();
+ byte[] pixels = [.. Enumerable.Repeat<byte[]>([255, 0, 0, 255], 16).SelectMany(p => p)];
+ File.WriteAllBytes(Path.Combine(folder, "red.dds"), ImageFormatTests.Dds(flags, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, pixels));
+ await File.WriteAllTextAsync(Path.Combine(folder, "model.mtl"), "newmtl red\nmap_Kd red.dds\n", TestContext.Current.CancellationToken);
+ var obj = Path.Combine(folder, "model.obj");
+ await File.WriteAllTextAsync(obj, """
+ mtllib model.mtl
+ v 0 0 0
+ v 1 0 0
+ v 0 1 0
+ vt 0 0
+ vt 1 0
+ vt 0 1
+ usemtl red
+ f 1/1 2/2 3/3
+ """, TestContext.Current.CancellationToken);
+
+ var job = await RunAsync(obj, "glb");
+
+ var data = await File.ReadAllBytesAsync(job.Outputs.Single(), TestContext.Current.CancellationToken);
+ Assert.Equal(0x46546C67u, BinaryPrimitives.ReadUInt32LittleEndian(data));
+ Assert.Equal(2u, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(4)));
+ Assert.Equal((uint)data.Length, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(8)));
+ var jsonLength = BinaryPrimitives.ReadInt32LittleEndian(data.AsSpan(12));
+ Assert.Equal(0x4E4F534Au, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(16)));
+ var json = JsonNode.Parse(data.AsSpan(20, jsonLength))!;
+ var binHeader = 20 + jsonLength;
+ var binLength = BinaryPrimitives.ReadInt32LittleEndian(data.AsSpan(binHeader));
+ Assert.Equal(0x004E4942u, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(binHeader + 4)));
+ Assert.Equal(data.Length, binHeader + 8 + binLength);
+ var material = Assert.Single(json["materials"]!.AsArray())!;
+ var textureIndex = (int)material["pbrMetallicRoughness"]!["baseColorTexture"]!["index"]!;
+ var imageIndex = (int)json["textures"]![textureIndex]!["source"]!;
+ var embedded = json["images"]![imageIndex]!;
+ Assert.Equal("image/png", (string)embedded["mimeType"]!);
+ var view = json["bufferViews"]![(int)embedded["bufferView"]!]!;
+ Assert.Equal(0, (int)view["buffer"]!);
+ var offset = (int?)view["byteOffset"] ?? 0;
+ var length = (int)view["byteLength"]!;
+ Assert.InRange(offset, 0, binLength - length);
+ var png = data.AsSpan(binHeader + 8 + offset, length).ToArray();
+ Assert.Equal(new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }, png[..8]);
+ using var image = new MagickImage(png);
+ Assert.Equal(MagickFormat.Png, image.Format);
+ Assert.Equal((4u, 4u), (image.Width, image.Height));
+ Assert.Equal(pixels.Where((_, i) => i % 4 != 3).ToArray(), image.GetPixels().ToByteArray(PixelMapping.RGB));
+ }
+
[Fact]
public async Task Gltf_output_is_one_file_with_the_buffer_inside()
{