diff options
| author | Marc Jeanmougin <marc@jeanmougin.fr> | 2018-09-13 11:21:24 +0000 |
|---|---|---|
| committer | Marc Jeanmougin <marc@jeanmougin.fr> | 2018-09-13 11:21:24 +0000 |
| commit | 7a29d33b8c11e3c2efa8508c3a7a2d943dcadbcd (patch) | |
| tree | 52159e960ddc909e78de6b558f90c67505170c57 /src/trace | |
| parent | make /options/yaxisdown true the default (diff) | |
| download | inkscape-7a29d33b8c11e3c2efa8508c3a7a2d943dcadbcd.tar.gz inkscape-7a29d33b8c11e3c2efa8508c3a7a2d943dcadbcd.zip | |
add autotrace basic support
Diffstat (limited to 'src/trace')
| -rw-r--r-- | src/trace/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/trace/autotrace/inkscape-autotrace.cpp | 189 | ||||
| -rw-r--r-- | src/trace/autotrace/inkscape-autotrace.h | 99 | ||||
| -rw-r--r-- | src/trace/trace.h | 7 |
4 files changed, 292 insertions, 5 deletions
diff --git a/src/trace/CMakeLists.txt b/src/trace/CMakeLists.txt index 12cf495f6..43cc5f76d 100644 --- a/src/trace/CMakeLists.txt +++ b/src/trace/CMakeLists.txt @@ -9,6 +9,7 @@ set(trace_SRC trace.cpp potrace/inkscape-potrace.cpp + autotrace/inkscape-autotrace.cpp # ------- # Headers @@ -22,6 +23,7 @@ set(trace_SRC potrace/bitmap.h potrace/inkscape-potrace.h + autotrace/inkscape-autotrace.h ) # add_inkscape_lib(trace_LIB "${trace_SRC}") diff --git a/src/trace/autotrace/inkscape-autotrace.cpp b/src/trace/autotrace/inkscape-autotrace.cpp new file mode 100644 index 000000000..68b49a0cb --- /dev/null +++ b/src/trace/autotrace/inkscape-autotrace.cpp @@ -0,0 +1,189 @@ +/* + * This is the C++ glue between Inkscape and Autotrace + * + * Authors: + * Marc Jeanmougin + * + * Copyright (C) 2018 Authors + * + * Released under GNU GPL v2 and later, read the file 'COPYING' for more information + * + */ + +#include "inkscape-autotrace.h" + +extern "C" { +#include "3rdparty/autotrace/autotrace.h" +#include "3rdparty/autotrace/output.h" +#include "3rdparty/autotrace/spline.h" +} + +#include <glibmm/i18n.h> +#include <gtkmm/main.h> +#include <iomanip> + +#include "trace/filterset.h" +#include "trace/imagemap-gdk.h" +#include "trace/quantize.h" + +#include "desktop.h" +#include "message-stack.h" +#include <inkscape.h> + +#include "object/sp-path.h" + +#include <svg/path-string.h> + +using Glib::ustring; + +static void updateGui() +{ + //## Allow the GUI to update + Gtk::Main::iteration(false); // at least once, non-blocking + while (Gtk::Main::events_pending()) + Gtk::Main::iteration(); +} + +namespace Inkscape { + +namespace Trace { + +namespace Autotrace { + + +/** + * + */ +AutotraceTracingEngine::AutotraceTracingEngine() + : keepGoing(1) + , traceType(TRACE_OUTLINE) + , invert(false) +{ + /* get default parameters */ + opts = at_fitting_opts_new(); +} + +AutotraceTracingEngine::~AutotraceTracingEngine() { at_fitting_opts_free(opts); } + + + +// TODO +Glib::RefPtr<Gdk::Pixbuf> AutotraceTracingEngine::preview(Glib::RefPtr<Gdk::Pixbuf> thePixbuf) { return thePixbuf; } + +/** + * This is the working method of this interface, 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> AutotraceTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf) +{ + GdkPixbuf *pb = pixbuf->gobj(); + + at_bitmap *bitmap = + at_bitmap_new(gdk_pixbuf_get_width(pb), gdk_pixbuf_get_height(pb), gdk_pixbuf_get_n_channels(pb)); + bitmap->bitmap = gdk_pixbuf_get_pixels(pb); + + at_splines_type *splines = at_splines_new_full(bitmap, opts, NULL, NULL, NULL, NULL, NULL, NULL); + // at_output_write_func wfunc = at_output_get_handler_by_suffix("svg"); + at_spline_writer *wfunc = at_output_get_handler_by_suffix("svg"); + + + int height = splines->height; + // const at_splines_type spline = *splines; + at_spline_list_array_type spline = *splines; + + unsigned this_list; + at_spline_list_type list; + at_color last_color = { 0, 0, 0 }; + + std::stringstream theStyle; + std::stringstream thePath; + char color[10]; + int nNodes = 0; + + std::vector<TracingEngineResult> res; + + // at_splines_write(wfunc, stdout, "", NULL, splines, NULL, NULL); + + for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH(spline); this_list++) { + unsigned this_spline; + at_spline_type first; + + list = SPLINE_LIST_ARRAY_ELT(spline, this_list); + first = SPLINE_LIST_ELT(list, 0); + + if (this_list == 0 || !at_color_equal(&list.color, &last_color)) { + if (this_list > 0) { + if (!(spline.centerline || list.open)) { + thePath << "z"; + nNodes++; + } + TracingEngineResult ter(theStyle.str(), thePath.str(), nNodes); + res.push_back(ter); + theStyle.clear(); + thePath.clear(); + nNodes = 0; + } + sprintf(color, "#%02x%02x%02x;", list.color.r, list.color.g, list.color.b); + + theStyle << ((spline.centerline || list.open) ? "stroke:" : "fill:") << color + << ((spline.centerline || list.open) ? "fill:" : "stroke:") << "none"; + } + thePath << "M" << START_POINT(first).x << " " << height - START_POINT(first).y; + nNodes++; + for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH(list); this_spline++) { + at_spline_type s = SPLINE_LIST_ELT(list, this_spline); + + if (SPLINE_DEGREE(s) == AT_LINEARTYPE) { + thePath << "L" << END_POINT(s).x << " " << height - END_POINT(s).y; + nNodes++; + } + else { + thePath << "C" << CONTROL1(s).x << " " << height - CONTROL1(s).y << " " << CONTROL2(s).x << " " + << height - CONTROL2(s).y << " " << END_POINT(s).x << " " << height - END_POINT(s).y; + nNodes++; + } + last_color = list.color; + } + } + if (!(spline.centerline || list.open)) + thePath << "z"; + nNodes++; + if (SPLINE_LIST_ARRAY_LENGTH(spline) > 0) { + TracingEngineResult ter(theStyle.str(), thePath.str(), nNodes); + res.push_back(ter); + theStyle.clear(); + thePath.clear(); + nNodes = 0; + } + + return res; +} + + +/** + * Abort the thread that is executing getPathDataFromPixbuf() + */ +void AutotraceTracingEngine::abort() +{ + // g_message("PotraceTracingEngine::abort()\n"); + keepGoing = 0; +} + + + +} // namespace Autotrace +} // 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/autotrace/inkscape-autotrace.h b/src/trace/autotrace/inkscape-autotrace.h new file mode 100644 index 000000000..62a73c9c3 --- /dev/null +++ b/src/trace/autotrace/inkscape-autotrace.h @@ -0,0 +1,99 @@ +/* + * This is the C++ glue between Inkscape and Autotrace + * + * Authors: + * Marc Jeanmougin + * + * Copyright (C) 2018 Authors + * + * Released under GNU GPL v2 and later, read the file 'COPYING' for more information + * + * Autotrace is available at http://github.com/autotrace/autotrace. + * + */ + +#ifndef __INKSCAPE_AUTOTRACE_H__ +#define __INKSCAPE_AUTOTRACE_H__ + +#include "3rdparty/autotrace/autotrace.h" +#include <trace/trace.h> + +namespace Inkscape { + +namespace Trace { + +namespace Autotrace { +enum TraceType { TRACE_CENTERLINE, TRACE_OUTLINE }; + +class AutotraceTracingEngine : public TracingEngine { + + public: + /** + * + */ + AutotraceTracingEngine(); + + /** + * + */ + ~AutotraceTracingEngine() override; + + + /** + * Sets/gets parameters + */ + // TODO + + /** + * 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; + + private: + // autotrace_param_t *autotraceParams; + TraceType traceType; + at_fitting_opts_type *opts; + + //## do I invert at the end? + bool invert; + +}; // class AutotraceTracingEngine + + + +} // namespace Autotrace +} // namespace Trace +} // namespace Inkscape + + +#endif //__INKSCAPE_POTRACE_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 : diff --git a/src/trace/trace.h b/src/trace/trace.h index e6768cb4e..e258fe027 100644 --- a/src/trace/trace.h +++ b/src/trace/trace.h @@ -129,15 +129,12 @@ class TracingEngine * of an SVG <path> element. */ virtual std::vector<TracingEngineResult> trace( - Glib::RefPtr<Gdk::Pixbuf> /*pixbuf*/) - { std::vector<TracingEngineResult> dummy; return dummy; } - + Glib::RefPtr<Gdk::Pixbuf> /*pixbuf*/) = 0; /** * Abort the thread that is executing getPathDataFromPixbuf() */ - virtual void abort() - {} + virtual void abort() = 0; |
