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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
fix(pdf): put black-and-white and 16-bit pictures into PDFs (#605) (#612)
Myeonggu Kime5e09e8b97b4
diff --git a/src/Filee.Engines/Ebooks/ComicBook.cs b/src/Filee.Engines/Ebooks/ComicBook.csindex d757a8c8..a2f9dd53 100644--- a/src/Filee.Engines/Ebooks/ComicBook.cs+++ b/src/Filee.Engines/Ebooks/ComicBook.cs@@ -6,6 +6,7 @@using System.IO.Compression;using Filee.Core.Conversion;using Filee.Core.Platform;+using Filee.Engines.Pdf;using ImageMagick;using PdfSharp.Drawing;using PdfSharp.Pdf;@@ -115,8 +116,9 @@ internal static class ComicBook{cancellationToken.ThrowIfCancellationRequested();// JPEG and PNG go in as they are (PDFsharp embeds JPEG data without re-encoding); others become one of them.- if (EbookFiles.CommonImage(pages[i], converted, gifAndSvg: false) is not ({ } file, _))+ if (EbookFiles.CommonImage(pages[i], converted, gifAndSvg: false) is not ({ } common, _))continue;+ var file = PdfPictures.Readable(common, converted);var info = new MagickImageInfo(file);var dpi = info.Density is { X: >= 36 } density ? density.Units == DensityUnit.PixelsPerCentimeter ? density.X * 2.54 : density.X : 96;using var image = XImage.FromFile(file);diff --git a/src/Filee.Engines/Ocr/VisionConverter.cs b/src/Filee.Engines/Ocr/VisionConverter.csindex 4539df00..2a23e5ff 100644--- a/src/Filee.Engines/Ocr/VisionConverter.cs+++ b/src/Filee.Engines/Ocr/VisionConverter.cs@@ -10,6 +10,7 @@// page of a TIFF.using Filee.Core.Conversion;+using Filee.Engines.Pdf;using ImageMagick;namespace Filee.Engines.Ocr;@@ -152,7 +153,7 @@ public sealed class VisionConverter : IConvertervar dpi = Dpi(frame);frame.AutoOrient();var png = Path.Combine(scratch, $"picture-{index++}.png");- frame.Write(png, MagickFormat.Png);+ PdfPictures.WritePng(frame, png);yield return new Picture(png, frame.Width, frame.Height, dpi);}}diff --git a/src/Filee.Engines/Pdf/PdfPictures.cs b/src/Filee.Engines/Pdf/PdfPictures.csnew file mode 100644index 00000000..b7993f24--- /dev/null+++ b/src/Filee.Engines/Pdf/PdfPictures.cs@@ -0,0 +1,65 @@+// Pictures that go into a PDF through PDFsharp. Its PNG decoder reads 8 bits per channel (grey, grey + alpha, RGB,+// RGBA) and 4- or 8-bit palettes; a black-and-white scan saved as a 1-bit PNG, a grey one with 2 or 4 bits, or any+// 16-bit PNG fails with "Unsupported image format" (#605). ImageMagick writes those on its own for such pictures,+// so Filee writes its PNGs for PDFsharp here, and re-encodes a PNG it is handed when the decoder would refuse it.++using ImageMagick;++namespace Filee.Engines.Pdf;++internal static class PdfPictures+{+ /// <summary>+ /// Writes <paramref name="image"/> as a PNG PDFsharp reads: RGBA with transparency, 8-bit grey for grey and+ /// black-and-white pictures (they stay grey), else RGB. No palette PNGs: PDFsharp rejects them with transparency,+ /// and ImageMagick writes them for pictures with few colours (charts, metafiles, pixel art).+ /// </summary>+ public static void WritePng(IMagickImage<byte> image, Stream stream) => image.Write(stream, Prepare(image));++ /// <inheritdoc cref="WritePng(IMagickImage{byte}, Stream)"/>+ public static void WritePng(IMagickImage<byte> image, string path) => image.Write(path, Prepare(image));++ /// <summary>+ /// <paramref name="png"/> itself when PDFsharp reads it, else a copy in <paramref name="folder"/> written by+ /// <see cref="WritePng(IMagickImage{byte}, string)"/>. Files that aren't PNGs are returned as they are.+ /// </summary>+ public static string Readable(string png, string folder)+ {+ if (!NeedsReencoding(png))+ return png;+ using var image = new MagickImage(png);+ Directory.CreateDirectory(folder);+ var copy = Path.Combine(folder, $"{Guid.NewGuid():N}.png");+ WritePng(image, copy);+ return copy;+ }++ /// <summary>True for a PNG whose header PDFsharp's decoder refuses: a bit depth other than 8, or a palette.</summary>+ internal static bool NeedsReencoding(string path)+ {+ Span<byte> header = stackalloc byte[26];+ using (var file = File.OpenRead(path))+ {+ if (file.ReadAtLeast(header, header.Length, throwOnEndOfStream: false) < header.Length)+ return false;+ }+ ReadOnlySpan<byte> signature = [0x89, (byte)'P', (byte)'N', (byte)'G', 0x0D, 0x0A, 0x1A, 0x0A];+ if (!header[..8].SequenceEqual(signature) || !header[12..16].SequenceEqual("IHDR"u8))+ return false;+ var bitDepth = header[24];+ var colorType = header[25];+ return bitDepth != 8 || colorType is not (0 or 2 or 4 or 6);+ }++ private static MagickFormat Prepare(IMagickImage<byte> image)+ {+ if (image.HasAlpha)+ return MagickFormat.Png32;+ if (image.ColorType is not (ColorType.Grayscale or ColorType.Bilevel))+ return MagickFormat.Png24;+ // Plain "png" picks the smallest bit depth: 1 bit for black and white, which PDFsharp can't read.+ image.Settings.SetDefine(MagickFormat.Png, "bit-depth", "8");+ image.Settings.SetDefine(MagickFormat.Png, "color-type", "0");+ return MagickFormat.Png;+ }+}diff --git a/src/Filee.Engines/Pdf/PdfSharpConverter.cs b/src/Filee.Engines/Pdf/PdfSharpConverter.csindex acfd7051..beecde34 100644--- a/src/Filee.Engines/Pdf/PdfSharpConverter.cs+++ b/src/Filee.Engines/Pdf/PdfSharpConverter.cs@@ -391,12 +391,7 @@ public sealed class PdfSharpConverter : IConverter, IPdfMerger, IPdfFinisher, II}else{- // No palette PNGs: PDFsharp's decoder rejects them with transparency, and ImageMagick writes them for- // pictures with few colours (charts, metafiles, pixel art). Grayscale stays grayscale.- var format = frame.HasAlpha ? MagickFormat.Png32- : frame.ColorType is ColorType.Grayscale or ColorType.Bilevel ? MagickFormat.Png- : MagickFormat.Png24;- frame.Write(stream, format);+ PdfPictures.WritePng(frame, stream);}stream.Position = 0;return stream;diff --git a/tests/Filee.Engines.Tests/EbookTests.cs b/tests/Filee.Engines.Tests/EbookTests.csindex 3e05eded..76ab12f6 100644--- a/tests/Filee.Engines.Tests/EbookTests.cs+++ b/tests/Filee.Engines.Tests/EbookTests.cs@@ -467,6 +467,22 @@ public class EbookTests(EngineFixture fx) : IClassFixture<EngineFixture>}}+ [Fact]+ public async Task A_comic_with_black_and_white_pages_becomes_a_pdf()+ {+ // #605: PNG pages go into the PDF as they are, and PDFsharp refuses a 1-bit PNG.+ var dir = fx.NewFolder();+ var cbz = Path.Combine(dir, "scans.cbz");+ using (var zip = ZipFile.Open(cbz, ZipArchiveMode.Create))+ {+ zip.CreateEntryFromFile(ImageAndPdfTests.MakeScan(dir, "page1.png"), "page1.png");+ zip.CreateEntryFromFile(ImageAndPdfTests.MakeScan(dir, "page2.png", "16", "2"), "page2.png");+ }++ using var document = PdfReader.Open(await ConvertAsync(cbz, "pdf"), PdfDocumentOpenMode.Import);+ Assert.Equal(2, document.PageCount);+ }+[Fact]public async Task Comics_become_fixed_layout_epub_and_pdf_becomes_cbz(){diff --git a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs b/tests/Filee.Engines.Tests/ImageAndPdfTests.csindex e18d0ad1..052f1607 100644--- a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs+++ b/tests/Filee.Engines.Tests/ImageAndPdfTests.cs@@ -223,6 +223,68 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal(2, toPng.Outputs.Count());}+ /// <summary>+ /// A black square on white, written with the given PNG bit depth and colour type, or as a black-and-white TIFF+ /// (a fax or a scan) when <paramref name="name"/> ends in .tif.+ /// </summary>+ internal static string MakeScan(string folder, string name, string bitDepth = "1", string colorType = "0")+ {+ using var image = new MagickImage(MagickColors.White, 64, 48);+ image.Draw(new ImageMagick.Drawing.Drawables().FillColor(MagickColors.Black).Rectangle(10, 10, 40, 30));+ var path = Path.Combine(folder, name);+ if (name.EndsWith(".tif", StringComparison.Ordinal))+ {+ image.Threshold(new Percentage(50));+ image.ColorType = ColorType.Bilevel;+ image.Depth = 1;+ image.Settings.Compression = CompressionMethod.Group4;+ image.Write(path, MagickFormat.Tiff);+ return path;+ }+ image.Settings.SetDefine(MagickFormat.Png, "bit-depth", bitDepth);+ image.Settings.SetDefine(MagickFormat.Png, "color-type", colorType);+ image.Write(path, MagickFormat.Png);+ return path;+ }++ [Theory]+ [InlineData("scan.png", "1", "0")]+ [InlineData("grey.png", "4", "0")]+ [InlineData("deep.png", "16", "2")]+ [InlineData("fax.tif", "", "")]+ public async Task Black_and_white_and_16_bit_pictures_go_into_a_pdf(string name, string bitDepth, string colorType)+ {+ // #605: PDFsharp refuses 1-, 2- and 4-bit grey and 16-bit PNGs ("Unsupported image format").+ var source = MakeScan(fx.NewFolder(), name, bitDepth, colorType);++ var job = await fx.ConvertAsync([source], new Preset { TargetFormat = "pdf" });++ AssertDone(job);+ using var pdf = PdfReader.Open(job.Outputs.Single(), PdfDocumentOpenMode.Import);+ Assert.Equal(1, pdf.PageCount);+ }++ [Fact]+ public void Pngs_pdfsharp_refuses_are_written_again_as_8_bit()+ {+ var folder = fx.NewFolder();+ foreach (var (bitDepth, colorType, refused) in new[] { ("1", "0", true), ("4", "0", true), ("16", "2", true), ("8", "0", false), ("8", "2", false) })+ {+ var png = MakeScan(folder, $"{bitDepth}-{colorType}.png", bitDepth, colorType);+ Assert.Equal(refused, Filee.Engines.Pdf.PdfPictures.NeedsReencoding(png));++ var readable = Filee.Engines.Pdf.PdfPictures.Readable(png, Path.Combine(folder, "readable"));+ Assert.Equal(!refused, readable == png);+ Assert.False(Filee.Engines.Pdf.PdfPictures.NeedsReencoding(readable));+ using var image = new MagickImage(readable);+ Assert.Equal((64u, 48u), (image.Width, image.Height));+ using var x = PdfSharp.Drawing.XImage.FromFile(readable);+ }++ var jpeg = EngineFixture.MakeImage(folder, "photo.jpg", MagickFormat.Jpeg);+ Assert.Equal(jpeg, Filee.Engines.Pdf.PdfPictures.Readable(jpeg, folder));+ }+[Fact]public async Task Pdf_to_multi_page_tiff(){diff --git a/tests/Filee.Engines.Tests/VisionOcrTests.cs b/tests/Filee.Engines.Tests/VisionOcrTests.csindex 1e575e92..a5b57d4e 100644--- a/tests/Filee.Engines.Tests/VisionOcrTests.cs+++ b/tests/Filee.Engines.Tests/VisionOcrTests.cs@@ -217,6 +217,22 @@ public class VisionConverterTests(EngineFixture fx) : IClassFixture<EngineFixturAssert.Equal(3, document.PageCount);}+ [Fact]+ public async Task A_black_and_white_scan_is_recognized_into_a_pdf()+ {+ // #605: the page picture used to stay a 1-bit PNG, which PDFsharp refuses.+ var folder = fx.NewFolder();+ var fax = ImageAndPdfTests.MakeScan(folder, "fax.tif");+ var recognizer = new VisionFakes.Recognizer();+ var output = Path.Combine(folder, "fax.pdf");++ await new VisionConverter(recognizer, new VisionFakes.Tesseract(available: false))+ .ConvertAsync(Step(fax, "tiff", output, Ocr()), null, TestContext.Current.CancellationToken);++ Assert.False(Filee.Engines.Pdf.PdfPictures.NeedsReencoding(Assert.Single(recognizer.Calls).Picture));+ Assert.Equal(VisionFakes.Text, PageText(output, 1));+ }+[Fact]public async Task Only_the_scanned_pages_of_a_pdf_are_recognized(){