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.

EPS and PS to PNG keep a transparent background (#39)

KnifeLemond0047a4b9ba2
diff --git a/src/Filee.Engines/Vector/GhostscriptConverter.cs b/src/Filee.Engines/Vector/GhostscriptConverter.cs
index fd936109..13362eae 100644
--- a/src/Filee.Engines/Vector/GhostscriptConverter.cs
+++ b/src/Filee.Engines/Vector/GhostscriptConverter.cs
@@ -1,9 +1,12 @@
// PostScript and EPS with Ghostscript (AGPL-3.0, a separate program downloaded on demand into engines/ghostscript).
-// EPS/PS → PDF with pdfwrite; PDF → EPS/PS with eps2write/ps2write. Everything else (EPS → PNG, SVG → EPS, …) is
-// planned through PDF by the route planner.
+// EPS/PS → PDF with pdfwrite; PDF → EPS/PS with eps2write/ps2write; EPS/PS → PNG with pngalpha, which keeps a
+// transparent background (through PDF the page would be drawn on white paper). Everything else (EPS → JPG,
+// SVG → EPS, …) is planned through PDF or PNG by the route planner.
using Filee.Core.Conversion;
using Filee.Engines.Infrastructure;
+using Filee.Engines.Magick;
+using ImageMagick;
using PdfSharp.Pdf.IO;
namespace Filee.Engines.Vector;
@@ -36,6 +39,8 @@ public sealed class GhostscriptConverter : IConverter
new("ps", "pdf"),
new("pdf", "eps"),
new("pdf", "ps"),
+ new("eps", "png"),
+ new("ps", "png"),
];
public EngineStatus GetStatus()
@@ -53,6 +58,7 @@ public sealed class GhostscriptConverter : IConverter
"pdf" => await ToPdfAsync(gs, step, cancellationToken, progress),
"ps" => await ToPostScriptAsync(gs, step, cancellationToken, progress),
"eps" => await ToEpsAsync(gs, step, cancellationToken, progress),
+ "png" => await ToPngAsync(gs, step, cancellationToken, progress),
_ => throw new NotSupportedException($"Ghostscript cannot write '{step.To}'."),
};
progress?.Report(1);
@@ -139,6 +145,41 @@ public sealed class GhostscriptConverter : IConverter
return written;
}
+ /// <summary>
+ /// EPS/PS → PNG with a transparent background where the drawing leaves the page empty, at the preset's render
+ /// DPI, EPS cropped to its bounding box. A multi-page PS gives one PNG per page (<c>_p{n}</c>). The preset's image
+ /// options (resize, grayscale, DPI, metadata) apply as for any other PNG.
+ /// </summary>
+ private static async Task<IReadOnlyList<string>> ToPngAsync(string gs, ConversionStep step, CancellationToken ct, IProgress<double>? progress)
+ {
+ var scratch = NewScratch(step.WorkDirectory);
+ var dpi = Math.Clamp(step.Preset.Pdf.RenderDpi, 36, 1200);
+ List<string> args = ["-sDEVICE=pngalpha", $"-r{dpi}", "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4"];
+ if (step.From == "eps")
+ args.Add("-dEPSCrop");
+ await RunAsync(gs, args, Path.Combine(scratch, "page-%d.png"), step.InputPath, ct, progress);
+
+ var files = Directory.GetFiles(scratch, "page-*.png")
+ .OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)["page-".Length..], System.Globalization.CultureInfo.InvariantCulture))
+ .ToList();
+ if (files.Count == 0)
+ throw new InvalidOperationException("Ghostscript wrote no PNG file.");
+
+ var (format, extension) = ImageEncoder.OutputFormat("png", step.Preset.Image);
+ var written = new List<string>();
+ for (var i = 0; i < files.Count; i++)
+ {
+ var path = step.Output.Allocate(extension, files.Count > 1 ? $"_p{i + 1}" : null);
+ if (path is null)
+ continue;
+ using var image = new MagickImage(files[i]);
+ ImageEncoder.ApplyOptions(image, step.Preset.Image, "png");
+ image.Write(path, format);
+ written.Add(path);
+ }
+ return written;
+ }
+
/// <summary>The preset's page range as zero-based pages, or null for every page (also when the PDF can't be read).</summary>
private static IReadOnlyList<int>? SelectedPages(ConversionStep step)
{
diff --git a/tests/Filee.Engines.Tests/GraphicsTests.cs b/tests/Filee.Engines.Tests/GraphicsTests.cs
index 634a2f5f..cef351d1 100644
--- a/tests/Filee.Engines.Tests/GraphicsTests.cs
+++ b/tests/Filee.Engines.Tests/GraphicsTests.cs
@@ -511,6 +511,9 @@ public class GraphicsTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal((400u, 200u), (image.Width, image.Height));
var triangle = Pixel(image, 360, 170);
Assert.True(triangle.B > 150 && triangle.R < 100, triangle.ToString());
+ // Where the drawing leaves the page empty, the PNG stays transparent (#39), not white.
+ Assert.True(image.HasAlpha);
+ Assert.Equal(0, Pixel(image, 2, 2).A);
}
[Fact]
@@ -560,7 +563,7 @@ public class GraphicsTests(EngineFixture fx) : IClassFixture<EngineFixture>
[InlineData("svg", "png", "vector")]
[InlineData("svg", "pdf", "vector")]
[InlineData("ai", "png", "vector,pdfium")]
- [InlineData("eps", "png", "ghostscript,pdfium")]
+ [InlineData("eps", "png", "ghostscript")] // straight to PNG: keeps transparency (#39)
[InlineData("svg", "eps", "vector,ghostscript")]
[InlineData("raw", "jpg", "magick")]
[InlineData("xcf", "psd", "magick")]