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.

Only raster images are traced into SVG

KnifeLemonfd82ff2fd889
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index f51a38b0..cc1a0646 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -105,8 +105,9 @@ current.
Built in (`Vector/Tracing`), no Potrace or other program: Filee's own code, written from published descriptions of the
steps (no code of Potrace, AutoTrace or VTracer is used). Every raster format ImageMagick reads (PNG, JPG, BMP, GIF,
-WEBP, TIFF, HEIC, PSD, RAW, …) is traced; anything else that becomes a PNG first (ICNS, PDF pages, …) is traced from
-that PNG. The first frame or page is used, turned upright, in sRGB. The SVG has the picture's pixel size
+WEBP, TIFF, HEIC, PSD, RAW, …) is traced, and other raster images through PNG (ICNS). Only raster images are traced:
+PDF, EPS, PS, AI, EMF/WMF, CDR, documents and videos are never redrawn from a rendering, so without a real vector
+route they have no SVG target. The first frame or page is used, turned upright, in sRGB. The SVG has the picture's pixel size
(`width`/`height`/`viewBox` in pixels), so it lines up with the original.
- **Black and white** (*Line art to SVG*: logos, signatures, scans): pixels darker than Otsu's threshold are ink
@@ -137,8 +138,9 @@ that PNG. The first frame or page is used, turned upright, in sRGB. The SVG has
20 MB SVG). Photos are not what tracing is for: they come out as posterized patches.
- **Routes**: tracing is the only raster → SVG route and costs 40 (`VectorConverter.TraceCost`), more than any route
from an image to a format SVG leads to, so it is never a step on the way to PDF, PNG, EPS, …; a test checks every
- pair of formats. Through SVG, pictures reach DXF and DWG. Formats without a vector route to SVG (PDF, EPS, AI;
- EMF/WMF without LibreOffice) are traced from their rendering.
+ pair of formats. Its edges are marked `ImageSourcesOnly`: the planner uses them only when the file being
+ converted is a raster image (`FormatCategory.Image`), so a vector file rendered to PNG can't be traced; a test
+ checks this for every pair of formats too. Through SVG, pictures reach DXF and DWG.
## SVG → DXF / DWG
diff --git a/src/Filee.Core/Conversion/IConverter.cs b/src/Filee.Core/Conversion/IConverter.cs
index 2d69a089..cad7fbb4 100644
--- a/src/Filee.Core/Conversion/IConverter.cs
+++ b/src/Filee.Core/Conversion/IConverter.cs
@@ -14,7 +14,11 @@ namespace Filee.Core.Conversion;
/// Relative cost used by <see cref="RoutePlanner"/>. Use 10 for a normal direct conversion,
/// higher for lossy or slow paths. Multi-step routes add up their costs.
/// </param>
-public readonly record struct ConversionEdge(string From, string To, int Cost = 10);
+/// <param name="ImageSourcesOnly">
+/// The edge is used only in routes that start from a raster image (<see cref="Formats.FormatCategory.Image"/>): image
+/// tracing may follow ICNS → PNG, but must never redraw a PDF, a vector drawing or a video frame that became a PNG.
+/// </param>
+public readonly record struct ConversionEdge(string From, string To, int Cost = 10, bool ImageSourcesOnly = false);
/// <summary>Whether an engine can currently be used.</summary>
/// <param name="IsAvailable">True if conversions can run.</param>
diff --git a/src/Filee.Core/Conversion/RoutePlanner.cs b/src/Filee.Core/Conversion/RoutePlanner.cs
index 29b1e732..684ec201 100644
--- a/src/Filee.Core/Conversion/RoutePlanner.cs
+++ b/src/Filee.Core/Conversion/RoutePlanner.cs
@@ -4,6 +4,8 @@
// two hops, e.g. HWPX → PDF (rhwp) → PNG (PDFium). Treating formats as nodes and converter edges as
// weighted arcs lets new engines automatically unlock new routes without touching any other code.
+using Filee.Core.Formats;
+
namespace Filee.Core.Conversion;
/// <summary>One hop of a route.</summary>
@@ -54,6 +56,9 @@ public sealed class RoutePlanner
: new Route([new RouteStep(best.Converter, from, to)], best.Cost);
}
+ // Edges for raster images only (tracing) are left out unless the file itself is one.
+ var imageSource = FormatRegistry.FindById(from)?.Category == FormatCategory.Image;
+
// Dijkstra over formats. The graph is small (a few hundred nodes).
var dist = new Dictionary<string, int> { [from] = 0 };
var prev = new Dictionary<string, (string Node, RouteStep Step)>();
@@ -72,7 +77,7 @@ public sealed class RoutePlanner
foreach (var (converter, edge, edgeCost) in EdgesFrom(node))
{
- if (edge.To == edge.From)
+ if (edge.To == edge.From || (edge.ImageSourcesOnly && !imageSource))
continue;
var next = cost + edgeCost;
if (dist.TryGetValue(edge.To, out var known) && known <= next)
diff --git a/src/Filee.Engines/Vector/VectorConverter.cs b/src/Filee.Engines/Vector/VectorConverter.cs
index 4814c00c..b601b3d6 100644
--- a/src/Filee.Engines/Vector/VectorConverter.cs
+++ b/src/Filee.Engines/Vector/VectorConverter.cs
@@ -18,7 +18,8 @@ public sealed class VectorConverter : IConverter
/// <summary>
/// Cost of tracing a raster image into SVG. Tracing redraws the picture, so it must never be a step on the way to
/// another format (PNG → SVG → PDF instead of PNG → PDF): its cost is higher than any route from an image to a
- /// format SVG leads to, and the planner only takes it when SVG (or DXF, DWG through SVG) is the target.
+ /// format SVG leads to, and the planner only takes it when SVG (or DXF, DWG through SVG) is the target. Its edges
+ /// are for raster sources only: a PDF, EPS or other vector file that can be rendered to PNG is never traced.
/// </summary>
public const int TraceCost = 40;
@@ -37,8 +38,8 @@ public sealed class VectorConverter : IConverter
new("svgz", "dxf"),
// Illustrator files are PDF inside (or PostScript, see ConvertIllustratorAsync).
new("ai", "pdf"),
- // Raster images (and, through PNG, anything that becomes one) are traced.
- .. ImageEncoder.Readable.Select(source => new ConversionEdge(source, "svg", TraceCost)),
+ // Raster images are traced (other raster formats through PNG, e.g. ICNS; never vector files or documents).
+ .. ImageEncoder.Readable.Select(source => new ConversionEdge(source, "svg", TraceCost, ImageSourcesOnly: true)),
];
public EngineStatus GetStatus() => EngineStatus.Available(version: $"{EngineVersions.BuiltIn} · {EngineVersions.Library("Svg.Skia", typeof(Svg.Skia.SKSvg))}");
diff --git a/tests/Filee.Core.Tests/RoutePlannerTests.cs b/tests/Filee.Core.Tests/RoutePlannerTests.cs
index 766fd035..5c7a9483 100644
--- a/tests/Filee.Core.Tests/RoutePlannerTests.cs
+++ b/tests/Filee.Core.Tests/RoutePlannerTests.cs
@@ -27,6 +27,18 @@ public class RoutePlannerTests
Assert.Equal(["hwpx→pdf", "pdf→png"], route.Steps.Select(s => $"{s.From}→{s.To}"));
}
+ [Fact]
+ public void Edges_for_image_sources_only_are_skipped_for_other_sources()
+ {
+ var tracer = new FakeConverter("trace", new ConversionEdge("png", "svg", 40, ImageSourcesOnly: true));
+ var render = new FakeConverter("render", new ConversionEdge("pdf", "png"), new ConversionEdge("icns", "png"));
+ var planner = new RoutePlanner([tracer, render]);
+
+ Assert.Equal(["png→svg"], planner.Plan("png", "svg")!.Steps.Select(s => $"{s.From}→{s.To}"));
+ Assert.Equal(["icns→png", "png→svg"], planner.Plan("icns", "svg")!.Steps.Select(s => $"{s.From}→{s.To}")); // a raster image too
+ Assert.Null(planner.Plan("pdf", "svg")); // a document rendered to PNG is never traced
+ }
+
[Fact]
public void Cheaper_route_wins()
{
diff --git a/tests/Filee.Engines.Tests/TracingTests.cs b/tests/Filee.Engines.Tests/TracingTests.cs
index c5a4e940..dcd3f8e8 100644
--- a/tests/Filee.Engines.Tests/TracingTests.cs
+++ b/tests/Filee.Engines.Tests/TracingTests.cs
@@ -399,6 +399,36 @@ public class TracingTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.DoesNotContain(planner.Plan(from, "eps")?.Steps ?? [], s => s.To == "svg");
}
+ [Fact]
+ public void Icns_is_traced_through_png()
+ {
+ var route = fx.Catalog.CreatePlanner().Plan("icns", "svg");
+
+ Assert.NotNull(route);
+ Assert.Equal(["png", "svg"], route.Steps.Select(s => s.To));
+ }
+
+ [Theory]
+ [InlineData("pdf")]
+ [InlineData("eps")]
+ [InlineData("ps")]
+ [InlineData("ai")]
+ [InlineData("emf")]
+ [InlineData("wmf")]
+ [InlineData("cdr")]
+ [InlineData("docx")]
+ [InlineData("mp4")]
+ public void Files_that_are_not_raster_images_are_never_traced(string from)
+ {
+ // With every optional engine as if installed: a vector route (LibreOffice for EMF, CDR) is fine, tracing a
+ // rendering is not, so PDF and EPS have no SVG target at all.
+ var planner = fx.Catalog.CreatePlanner(fx.Converters.Select(c => c.Id).ToArray());
+ foreach (var to in new[] { "svg", "svgz", "dxf", "dwg" })
+ Assert.DoesNotContain(planner.Plan(from, to)?.Steps ?? [], IsTracing);
+ if (from is "pdf" or "eps" or "ps" or "ai")
+ Assert.Null(planner.Plan(from, "svg"));
+ }
+
[Fact]
public void Tracing_is_never_a_step_towards_a_format_that_has_another_route()
{
@@ -416,10 +446,13 @@ public class TracingTests(EngineFixture fx) : IClassFixture<EngineFixture>
{
foreach (var to in formats.Where(t => t != from))
{
+ var route = withTracing.Plan(from, to);
+ // Only raster images are ever traced.
+ if (FormatRegistry.FindById(from)?.Category != FormatCategory.Image)
+ Assert.False(route?.Steps.Any(IsTracing) ?? false, $"{from} → {to} traces a rendering");
var other = without.Plan(from, to);
if (other is null)
continue;
- var route = withTracing.Plan(from, to);
Assert.NotNull(route);
Assert.False(route.Steps.Any(IsTracing), $"{from} → {to} traces: {string.Join(" → ", route.Steps.Select(s => s.To))}");
Assert.Equal(other.Cost, route.Cost);
@@ -437,7 +470,7 @@ public class TracingTests(EngineFixture fx) : IClassFixture<EngineFixture>
public string Id => inner.Id;
public string DisplayName => inner.DisplayName;
public int MaxParallelism => inner.MaxParallelism;
- public IReadOnlyList<ConversionEdge> Edges { get; } = inner.Edges.Where(e => e.Cost != VectorConverter.TraceCost).ToList();
+ public IReadOnlyList<ConversionEdge> Edges { get; } = inner.Edges.Where(e => !e.ImageSourcesOnly).ToList();
public EngineStatus GetStatus() => inner.GetStatus();
public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken) =>
inner.ConvertAsync(step, progress, cancellationToken);