diff options
| author | Marc Jeanmougin <marc@jeanmougin.fr> | 2019-05-27 17:32:53 +0000 |
|---|---|---|
| committer | Marc Jeanmougin <marc@jeanmougin.fr> | 2019-08-21 15:16:54 +0000 |
| commit | ba838aef213e2b99bdedc687429c12657c669df5 (patch) | |
| tree | dff70699a3916f998c60f597329ca2bec755f45d /src/trace | |
| parent | Improve spellcheck dialog (diff) | |
| download | inkscape-ba838aef213e2b99bdedc687429c12657c669df5.tar.gz inkscape-ba838aef213e2b99bdedc687429c12657c669df5.zip | |
new trace Dialog - initial implementation
Diffstat (limited to 'src/trace')
| -rw-r--r-- | src/trace/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/trace/depixelize/inkscape-depixelize.cpp | 152 | ||||
| -rw-r--r-- | src/trace/depixelize/inkscape-depixelize.h | 100 | ||||
| -rw-r--r-- | src/trace/potrace/inkscape-potrace.cpp | 35 | ||||
| -rw-r--r-- | src/trace/potrace/inkscape-potrace.h | 164 |
5 files changed, 279 insertions, 174 deletions
diff --git a/src/trace/CMakeLists.txt b/src/trace/CMakeLists.txt index 02bb6a05d..a680166bf 100644 --- a/src/trace/CMakeLists.txt +++ b/src/trace/CMakeLists.txt @@ -11,6 +11,7 @@ set(trace_SRC potrace/inkscape-potrace.cpp autotrace/inkscape-autotrace.cpp + depixelize/inkscape-depixelize.cpp # ------- # Headers @@ -25,6 +26,7 @@ set(trace_SRC potrace/bitmap.h potrace/inkscape-potrace.h autotrace/inkscape-autotrace.h + depixelize/inkscape-depixelize.h ) # add_inkscape_lib(trace_LIB "${trace_SRC}") diff --git a/src/trace/depixelize/inkscape-depixelize.cpp b/src/trace/depixelize/inkscape-depixelize.cpp new file mode 100644 index 000000000..4b8bde1d8 --- /dev/null +++ b/src/trace/depixelize/inkscape-depixelize.cpp @@ -0,0 +1,152 @@ +// 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 "object/sp-path.h" + +#include <svg/path-string.h> +#include <svg/svg.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) + : keepGoing(1) + , traceType(traceType) +{ + params = new ::Tracer::Kopf2011::Options(); + params->curvesMultiplier = curves; + params->islandsWeight = islands; + params->sparsePixelsRadius = sparsePixels; + params->sparsePixelsMultiplier = sparseMultiplier; + 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) +{ + 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; +// TODO + } + + ::Tracer::Splines splines; + + if (traceType == TRACE_VORONOI) + splines = ::Tracer::Kopf2011::to_voronoi(pixbuf, *params); + else + splines = ::Tracer::Kopf2011::to_splines(pixbuf, *params); + + std::vector<TracingEngineResult> res; + + for (::Tracer::Splines::const_iterator it = splines.begin(), end = splines.end(); it != end; ++it) { + /*{ + SPCSSAttr *css = sp_repr_css_attr_new(); + + { + 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]))); + + sp_repr_css_set_property(css, "fill", b); + } + + { + Inkscape::CSSOStringStream osalpha; + osalpha << float(it->rgba[3]) / 255.; + sp_repr_css_set_property(css, "fill-opacity", + osalpha.str().c_str()); + } + + sp_repr_css_set(repr, css, "style"); + sp_repr_css_attr_unref(css); + } + + gchar *str = sp_svg_write_path(it->pathVector); + */ + + TracingEngineResult r("fill:black;", sp_svg_write_path(it->pathVector), it->pathVector.nodes().size()); //TODO + res.push_back(r); + } + 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 000000000..228e72448 --- /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); + + /** + * + */ + ~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 : diff --git a/src/trace/potrace/inkscape-potrace.cpp b/src/trace/potrace/inkscape-potrace.cpp index 092fba634..710e5cfa7 100644 --- a/src/trace/potrace/inkscape-potrace.cpp +++ b/src/trace/potrace/inkscape-potrace.cpp @@ -98,6 +98,15 @@ PotraceTracingEngine::PotraceTracingEngine() : potraceParams->progress.data = (void *)this; } +PotraceTracingEngine::PotraceTracingEngine(TraceType traceType, bool invert, int quantizationNrColors, double brightnessThreshold, double brightnessFloor, double cannyHighThreshold, int multiScanNrColors, bool multiScanStack, bool multiScanSmooth, bool multiScanRemoveBackground) : + keepGoing(1), traceType(traceType), invert(invert), quantizationNrColors(quantizationNrColors), brightnessThreshold(brightnessThreshold), brightnessFloor(brightnessFloor), cannyHighThreshold(cannyHighThreshold), multiScanNrColors(multiScanNrColors) , multiScanStack(multiScanStack), multiScanSmooth(multiScanSmooth), multiScanRemoveBackground(multiScanRemoveBackground) +{ + potraceParams = potrace_param_default(); + potraceParams->progress.callback = potraceStatusCallback; + potraceParams->progress.data = (void *)this; +} + + PotraceTracingEngine::~PotraceTracingEngine() { potrace_param_free(potraceParams); @@ -212,27 +221,27 @@ static GrayMap *filter(PotraceTracingEngine &engine, GdkPixbuf * pixbuf) GrayMap *newGm = nullptr; /*### Color quantization -- banding ###*/ - if (engine.getTraceType() == TRACE_QUANT) + if (engine.traceType == TRACE_QUANT) { RgbMap *rgbmap = gdkPixbufToRgbMap(pixbuf); //rgbMap->writePPM(rgbMap, "rgb.ppm"); newGm = quantizeBand(rgbmap, - engine.getQuantizationNrColors()); + engine.quantizationNrColors); rgbmap->destroy(rgbmap); //return newGm; } /*### Brightness threshold ###*/ - else if ( engine.getTraceType() == TRACE_BRIGHTNESS || - engine.getTraceType() == TRACE_BRIGHTNESS_MULTI ) + else if ( engine.traceType == TRACE_BRIGHTNESS || + engine.traceType == TRACE_BRIGHTNESS_MULTI ) { GrayMap *gm = gdkPixbufToGrayMap(pixbuf); newGm = GrayMapCreate(gm->width, gm->height); double floor = 3.0 * - ( engine.getBrightnessFloor() * 256.0 ); + ( engine.brightnessFloor * 256.0 ); double cutoff = 3.0 * - ( engine.getBrightnessThreshold() * 256.0 ); + ( engine.brightnessThreshold * 256.0 ); for (int y=0 ; y<gm->height ; y++) { for (int x=0 ; x<gm->width ; x++) @@ -251,17 +260,17 @@ static GrayMap *filter(PotraceTracingEngine &engine, GdkPixbuf * pixbuf) } /*### Canny edge detection ###*/ - else if (engine.getTraceType() == TRACE_CANNY) + else if (engine.traceType == TRACE_CANNY) { GrayMap *gm = gdkPixbufToGrayMap(pixbuf); - newGm = grayMapCanny(gm, 0.1, engine.getCannyHighThreshold()); + newGm = grayMapCanny(gm, 0.1, engine.cannyHighThreshold); gm->destroy(gm); //newGm->writePPM(newGm, "canny.ppm"); //return newGm; } /*### Do I invert the image? ###*/ - if (newGm && engine.getInvert()) + if (newGm && engine.invert) { for (int y=0 ; y<newGm->height ; y++) { @@ -286,19 +295,19 @@ static IndexedMap *filterIndexed(PotraceTracingEngine &engine, GdkPixbuf * pixbu IndexedMap *newGm = nullptr; RgbMap *gm = gdkPixbufToRgbMap(pixbuf); - if (engine.getMultiScanSmooth()) + if (engine.multiScanSmooth) { RgbMap *gaussMap = rgbMapGaussian(gm); - newGm = rgbMapQuantize(gaussMap, engine.getMultiScanNrColors()); + newGm = rgbMapQuantize(gaussMap, engine.multiScanNrColors); gaussMap->destroy(gaussMap); } else { - newGm = rgbMapQuantize(gm, engine.getMultiScanNrColors()); + newGm = rgbMapQuantize(gm, engine.multiScanNrColors); } gm->destroy(gm); - if (engine.getTraceType() == TRACE_QUANT_MONO) + if (engine.traceType == TRACE_QUANT_MONO) { //Turn to grays for (int i=0 ; i<newGm->nrColors ; i++) diff --git a/src/trace/potrace/inkscape-potrace.h b/src/trace/potrace/inkscape-potrace.h index bed5bf457..1385521f1 100644 --- a/src/trace/potrace/inkscape-potrace.h +++ b/src/trace/potrace/inkscape-potrace.h @@ -50,170 +50,13 @@ class PotraceTracingEngine : public TracingEngine * */ PotraceTracingEngine(); + PotraceTracingEngine(TraceType traceType, bool invert, int quantizationNrColors, double brightnessThreshold, double brightnessFloor, double cannyHighThreshold, int multiScanNrColors, bool multiScanStack, bool multiScanSmooth, bool multiScanRemoveBackground); /** * */ ~PotraceTracingEngine() override; - - /** - * Sets/gets potrace parameters - */ - void setParamsTurdSize(int val) - { - potraceParams->turdsize = val; - } - int getParamsTurdSize() - { - return potraceParams->turdsize; - } - - void setParamsAlphaMax(double val) - { - potraceParams->alphamax = val; - } - double getParamsAlphaMax() - { - return potraceParams->alphamax; - } - - void setParamsOptiCurve(bool val) - { - potraceParams->opticurve = val; - } - bool getParamsOptiCurve() - { - return potraceParams->opticurve; - } - - void setParamsOptTolerance(double val) - { - potraceParams->opttolerance = val; - } - double getParamsOptTolerance() - { - return potraceParams->opttolerance; - } - - void setTraceType(TraceType val) - { - traceType = val; - } - TraceType getTraceType() - { - return traceType; - } - - /** - * Sets/gets whether I invert the product of the other filter(s) - */ - void setInvert(bool val) - { - invert = val; - } - bool getInvert() - { - return invert; - } - - /** - * Sets the halfway point for black/white - */ - void setQuantizationNrColors(int val) - { - quantizationNrColors = val; - } - int getQuantizationNrColors() - { - return quantizationNrColors; - } - - /** - * Sets the halfway point for black/white - */ - void setBrightnessThreshold(double val) - { - brightnessThreshold = val; - } - double getBrightnessThreshold() - { - return brightnessThreshold; - } - - /** - * Sets the lower consideration point for black/white - */ - void setBrightnessFloor(double val) - { - brightnessFloor = val; - } - double getBrightnessFloor() - { - return brightnessFloor; - } - - /** - * Sets upper cutoff for canny non-maximalizing - */ - void setCannyHighThreshold(double val) - { - cannyHighThreshold = val; - } - double getCannyHighThreshold() - { - return cannyHighThreshold; - } - - /** - * Sets the number of colors for quant multiscan - */ - void setMultiScanNrColors(int val) - { - multiScanNrColors = val; - } - int getMultiScanNrColors() - { - return multiScanNrColors; - } - - /** - * Sets whether we tile regions side-by-side or stack them - */ - void setMultiScanStack(bool val) - { - multiScanStack = val; - } - bool setMultiScanStack() - { - return multiScanStack; - } - - /** - * Sets whether we want gaussian smoothing of bitmaps before quantizing - */ - void setMultiScanSmooth(bool val) - { - multiScanSmooth = val; - } - bool getMultiScanSmooth() - { - return multiScanSmooth; - } - - /** - * Sets whether we want to remove the background (bottom) trace - */ - void setMultiScanRemoveBackground(bool val) - { - multiScanRemoveBackground= val; - } - bool getMultiScanRemoveBackground() - { - return multiScanRemoveBackground; - } - - /** * This is the working method of this implementing class, and all * implementing classes. Take a GdkPixbuf, trace it, and @@ -240,9 +83,6 @@ class PotraceTracingEngine : public TracingEngine std::vector<TracingEngineResult>traceGrayMap(GrayMap *grayMap); - - private: - potrace_param_t *potraceParams; TraceType traceType; @@ -264,6 +104,8 @@ class PotraceTracingEngine : public TracingEngine bool multiScanStack; //do we tile or stack? bool multiScanSmooth;//do we use gaussian filter? bool multiScanRemoveBackground; //do we remove the bottom trace? + + private: /** * This is the actual wrapper of the call to Potrace. nodeCount * returns the count of nodes created. May be NULL if ignored. |
