summaryrefslogtreecommitdiffstats
path: root/src/trace/depixelize
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
commitc853ffb5b2f75f5a889ed2e3ef89b818a736e87a (patch)
tree7d13a0883bb7936b84d6ecdd7bc332b41ed04bee /src/trace/depixelize
parentInitial commit. (diff)
downloadinkscape-upstream.tar.xz
inkscape-upstream.zip
Adding upstream version 1.3+ds.upstream/1.3+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/trace/depixelize')
-rw-r--r--src/trace/depixelize/inkscape-depixelize.cpp107
-rw-r--r--src/trace/depixelize/inkscape-depixelize.h60
2 files changed, 167 insertions, 0 deletions
diff --git a/src/trace/depixelize/inkscape-depixelize.cpp b/src/trace/depixelize/inkscape-depixelize.cpp
new file mode 100644
index 0000000..ae3c783
--- /dev/null
+++ b/src/trace/depixelize/inkscape-depixelize.cpp
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This is the C++ glue between Inkscape and Potrace
+ *
+ * Authors:
+ * Bob Jamison <rjamison@titan.com>
+ * Stéphane Gimenez <dev@gim.name>
+ *
+ * Copyright (C) 2004-2006 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ *
+ * Potrace, the wonderful tracer located at http://potrace.sourceforge.net,
+ * is provided by the generosity of Peter Selinger, to whom we are grateful.
+ *
+ */
+#include <iomanip>
+#include <thread>
+#include <glibmm/i18n.h>
+
+#include "inkscape-depixelize.h"
+
+#include "color.h"
+#include "preferences.h"
+#include "async/progress.h"
+#include "svg/svg-color.h"
+#include "svg/css-ostringstream.h"
+
+namespace Inkscape {
+namespace Trace {
+namespace Depixelize {
+
+DepixelizeTracingEngine::DepixelizeTracingEngine(TraceType traceType, double curves, int islands, int sparsePixels, double sparseMultiplier, bool optimize)
+ : traceType(traceType)
+{
+ params.curvesMultiplier = curves;
+ params.islandsWeight = islands;
+ params.sparsePixelsRadius = sparsePixels;
+ params.sparsePixelsMultiplier = sparseMultiplier;
+ params.optimize = optimize;
+ params.nthreads = Inkscape::Preferences::get()->getIntLimited("/options/threading/numthreads", std::thread::hardware_concurrency(), 1, 256);
+}
+
+TraceResult DepixelizeTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf, Async::Progress<double> &progress)
+{
+ TraceResult res;
+
+ ::Tracer::Splines splines;
+
+ if (traceType == TraceType::VORONOI) {
+ splines = ::Tracer::Kopf2011::to_voronoi(pixbuf, params);
+ } else {
+ splines = ::Tracer::Kopf2011::to_splines(pixbuf, params);
+ }
+
+ progress.report_or_throw(0.5);
+
+ auto subprogress = Async::SubProgress(progress, 0.5, 0.5);
+ auto throttled = Async::ProgressStepThrottler(subprogress, 0.02);
+
+ int num_splines = std::distance(splines.begin(), splines.end());
+ int i = 0;
+
+ for (auto &it : splines) {
+ throttled.report_or_throw((double)i / num_splines);
+ i++;
+
+ char b[64];
+ sp_svg_write_color(b, sizeof(b),
+ SP_RGBA32_U_COMPOSE(unsigned(it.rgba[0]),
+ unsigned(it.rgba[1]),
+ unsigned(it.rgba[2]),
+ unsigned(it.rgba[3])));
+ Inkscape::CSSOStringStream osalpha;
+ osalpha << it.rgba[3] / 255.0f;
+ char *style = g_strdup_printf("fill:%s;fill-opacity:%s;", b, osalpha.str().c_str());
+ res.emplace_back(style, std::move(it.pathVector));
+ g_free(style);
+ }
+
+ return res;
+}
+
+Glib::RefPtr<Gdk::Pixbuf> DepixelizeTracingEngine::preview(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf)
+{
+ return pixbuf;
+}
+
+bool DepixelizeTracingEngine::check_image_size(Geom::IntPoint const &size) const
+{
+ return size.x() > 256 || size.y() > 256;
+}
+
+} // namespace Depixelize
+} // namespace Trace
+} // namespace Inkscape
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
diff --git a/src/trace/depixelize/inkscape-depixelize.h b/src/trace/depixelize/inkscape-depixelize.h
new file mode 100644
index 0000000..adeefdc
--- /dev/null
+++ b/src/trace/depixelize/inkscape-depixelize.h
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This is the C++ glue between Inkscape and Potrace
+ *
+ * Copyright (C) 2019 Authors
+ *
+ * Released under GNU GPL v2+, read the file 'COPYING' for more information.
+ *
+ * Potrace, the wonderful tracer located at http://potrace.sourceforge.net,
+ * is provided by the generosity of Peter Selinger, to whom we are grateful.
+ *
+ */
+#ifndef INKSCAPE_TRACE_DEPIXELIZE_H
+#define INKSCAPE_TRACE_DEPIXELIZE_H
+
+#include "trace/trace.h"
+#include "3rdparty/libdepixelize/kopftracer2011.h" // Cannot move to source file due to nested class.
+
+namespace Inkscape {
+namespace Trace {
+namespace Depixelize {
+
+enum class TraceType
+{
+ VORONOI,
+ BSPLINES
+};
+
+class DepixelizeTracingEngine final
+ : public TracingEngine
+{
+public:
+ DepixelizeTracingEngine() = default;
+ DepixelizeTracingEngine(TraceType traceType, double curves, int islands, int sparsePixels, double sparseMultiplier, bool optimize);
+
+ TraceResult trace(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf, Async::Progress<double> &progress) override;
+ Glib::RefPtr<Gdk::Pixbuf> preview(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf) override;
+ bool check_image_size(Geom::IntPoint const &size) const override;
+
+private:
+ ::Tracer::Kopf2011::Options params;
+ TraceType traceType = TraceType::VORONOI;
+};
+
+} // namespace Depixelize
+} // namespace Trace
+} // namespace Inkscape
+
+#endif // INKSCAPE_TRACE_DEPIXELIZE_H
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :