summaryrefslogtreecommitdiffstats
path: root/src/trace/depixelize/inkscape-depixelize.cpp
blob: a61ff8b2a647b0924d4282e30d2c6300cfa0ef2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 :