summaryrefslogtreecommitdiffstats
path: root/src/trace/depixelize
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
commitcca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch)
tree146f39ded1c938019e1ed42d30923c2ac9e86789 /src/trace/depixelize
parentInitial commit. (diff)
downloadinkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.tar.xz
inkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.zip
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/trace/depixelize/inkscape-depixelize.cpp140
-rw-r--r--src/trace/depixelize/inkscape-depixelize.h100
2 files changed, 240 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..a61ff8b
--- /dev/null
+++ b/src/trace/depixelize/inkscape-depixelize.cpp
@@ -0,0 +1,140 @@
+// 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 "inkscape-depixelize.h"
+
+#include <glibmm/i18n.h>
+#include <gtkmm/main.h>
+#include <gtkmm.h>
+#include <iomanip>
+
+#include "desktop.h"
+#include "message-stack.h"
+#include "helper/geom.h"
+#include "object/sp-path.h"
+
+#include "display/cairo-templates.h"
+
+#include <svg/path-string.h>
+#include <svg/svg.h>
+#include <svg/svg-color.h>
+#include "svg/css-ostringstream.h"
+
+using Glib::ustring;
+
+
+
+namespace Inkscape {
+
+namespace Trace {
+
+namespace Depixelize {
+
+
+/**
+ *
+ */
+DepixelizeTracingEngine::DepixelizeTracingEngine()
+ : keepGoing(1)
+ , traceType(TRACE_VORONOI)
+{
+ params = new ::Tracer::Kopf2011::Options();
+}
+
+
+
+DepixelizeTracingEngine::DepixelizeTracingEngine(TraceType traceType, double curves, int islands, int sparsePixels,
+ double sparseMultiplier, bool optimize)
+ : keepGoing(1)
+ , traceType(traceType)
+{
+ params = new ::Tracer::Kopf2011::Options();
+ params->curvesMultiplier = curves;
+ params->islandsWeight = islands;
+ params->sparsePixelsRadius = sparsePixels;
+ params->sparsePixelsMultiplier = sparseMultiplier;
+ params->optimize = optimize;
+ params->nthreads = Inkscape::Preferences::get()->getIntLimited("/options/threading/numthreads",
+#ifdef HAVE_OPENMP
+ omp_get_num_procs(),
+#else
+ 1,
+#endif // HAVE_OPENMP
+ 1, 256);
+}
+
+DepixelizeTracingEngine::~DepixelizeTracingEngine() { delete params; }
+
+std::vector<TracingEngineResult> DepixelizeTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
+{
+ std::vector<TracingEngineResult> res;
+
+ if (pixbuf->get_width() > 256 || pixbuf->get_height() > 256) {
+ char *msg = _("Image looks too big. Process may take a while and it is"
+ " wise to save your document before continuing."
+ "\n\nContinue the procedure (without saving)?");
+ Gtk::MessageDialog dialog(msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK_CANCEL, true);
+
+ if (dialog.run() != Gtk::RESPONSE_OK) {
+ return res;
+ }
+ }
+
+ ::Tracer::Splines splines;
+
+ if (traceType == TRACE_VORONOI)
+ splines = ::Tracer::Kopf2011::to_voronoi(pixbuf, *params);
+ else
+ splines = ::Tracer::Kopf2011::to_splines(pixbuf, *params);
+
+ for (::Tracer::Splines::const_iterator it = splines.begin(), end = splines.end(); it != end; ++it) {
+ gchar 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 << float(it->rgba[3]) / 255.;
+ gchar* style = g_strdup_printf("fill:%s;fill-opacity:%s;", b, osalpha.str().c_str());
+ printf("%s\n", style);
+ TracingEngineResult r(style, sp_svg_write_path(it->pathVector), count_pathvector_nodes(it->pathVector));
+ res.push_back(r);
+ g_free(style);
+ }
+ return res;
+}
+
+void DepixelizeTracingEngine::abort() { keepGoing = 0; }
+
+Glib::RefPtr<Gdk::Pixbuf> DepixelizeTracingEngine::preview(Glib::RefPtr<Gdk::Pixbuf> pixbuf) { return pixbuf; }
+
+
+} // 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..85f6509
--- /dev/null
+++ b/src/trace/depixelize/inkscape-depixelize.h
@@ -0,0 +1,100 @@
+// 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_DEPIXTRACE_H__
+#define __INKSCAPE_DEPIXTRACE_H__
+
+#include <trace/trace.h>
+#include "3rdparty/libdepixelize/kopftracer2011.h"
+
+struct GrayMap_def;
+typedef GrayMap_def GrayMap;
+
+namespace Inkscape {
+
+namespace Trace {
+
+namespace Depixelize {
+
+enum TraceType
+ {
+ TRACE_VORONOI,
+ TRACE_BSPLINES
+ };
+
+
+class DepixelizeTracingEngine : public TracingEngine
+{
+
+ public:
+
+ /**
+ *
+ */
+ DepixelizeTracingEngine();
+ DepixelizeTracingEngine(TraceType traceType, double curves, int islands, int sparsePixels, double sparseMultiplier, bool optimize);
+
+ /**
+ *
+ */
+ ~DepixelizeTracingEngine() override;
+
+ /**
+ * This is the working method of this implementing class, and all
+ * implementing classes. Take a GdkPixbuf, trace it, and
+ * return the path data that is compatible with the d="" attribute
+ * of an SVG <path> element.
+ */
+ std::vector<TracingEngineResult> trace(
+ Glib::RefPtr<Gdk::Pixbuf> pixbuf) override;
+
+ /**
+ * Abort the thread that is executing getPathDataFromPixbuf()
+ */
+ void abort() override;
+
+ /**
+ *
+ */
+ Glib::RefPtr<Gdk::Pixbuf> preview(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
+
+ /**
+ *
+ */
+ int keepGoing;
+
+ ::Tracer::Kopf2011::Options *params;
+ TraceType traceType;
+
+};//class PotraceTracingEngine
+
+
+
+} // namespace Depixelize
+} // namespace Trace
+} // namespace Inkscape
+
+
+#endif //__INKSCAPE_TRACE_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 :