summaryrefslogtreecommitdiffstats
path: root/src/trace/potrace/inkscape-potrace.h
blob: 0e4a9c62eeb5118419ea5fcdb62fee9be35e79bb (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.
 *
 */
#ifndef INKSCAPE_TRACE_POTRACE_H
#define INKSCAPE_TRACE_POTRACE_H

#include <optional>
#include <unordered_set>
#include <boost/functional/hash.hpp>
#include <2geom/point.h>
#include <2geom/path-sink.h>
#include "trace/trace.h"
#include "trace/imagemap.h"
using potrace_param_t = struct potrace_param_s;
using potrace_path_t  = struct potrace_path_s;

namespace Inkscape {
namespace Trace {
namespace Potrace {

enum class TraceType
{
    BRIGHTNESS,
    BRIGHTNESS_MULTI,
    CANNY,
    QUANT,
    QUANT_COLOR,
    QUANT_MONO,
    // Used in tracedialog.cpp
    AUTOTRACE_SINGLE,
    AUTOTRACE_MULTI,
    AUTOTRACE_CENTERLINE
};

// Todo: Make lib2geom types hashable.
struct geom_point_hash
{
    std::size_t operator()(Geom::Point const &pt) const
    {
        std::size_t hash = 0;
        boost::hash_combine(hash, pt.x());
        boost::hash_combine(hash, pt.y());
        return hash;
    }
};

class PotraceTracingEngine final
    : public TracingEngine
{
public:
    PotraceTracingEngine();
    PotraceTracingEngine(TraceType traceType,
                         bool invert,
                         int quantizationNrColors,
                         double brightnessThreshold,
                         double brightnessFloor,
                         double cannyHighThreshold,
                         int multiScanNrColors,
                         bool multiScanStack,
                         bool multiScanSmooth ,
                         bool multiScanRemoveBackground);
    ~PotraceTracingEngine() override;

    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;

    TraceResult traceGrayMap(GrayMap const &grayMap, Async::Progress<double> &progress);

    void setOptiCurve(int);
    void setOptTolerance(double);
    void setAlphaMax(double);
    void setTurdSize(int);

private:
    potrace_param_t *potraceParams;

    TraceType traceType = TraceType::BRIGHTNESS;

    // Whether the image should be inverted at the end.
    bool invert = false;

    // Color -> b&w quantization
    int quantizationNrColors = 8;

    // Brightness items
    double brightnessThreshold = 0.45;
    double brightnessFloor = 0.0;

    // Canny items
    double cannyHighThreshold = 0.65;

    // Color -> multiscan quantization
    int multiScanNrColors = 8;
    bool multiScanStack = true; // do we tile or stack?
    bool multiScanSmooth = false; // do we use gaussian filter?
    bool multiScanRemoveBackground = false; // do we remove the bottom trace?

    void common_init();

    TraceResult traceQuant          (Glib::RefPtr<Gdk::Pixbuf> const &pixbuf, Async::Progress<double> &progress);
    TraceResult traceBrightnessMulti(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf, Async::Progress<double> &progress);
    TraceResult traceSingle         (Glib::RefPtr<Gdk::Pixbuf> const &pixbuf, Async::Progress<double> &progress);

    IndexedMap filterIndexed(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf) const;
    std::optional<GrayMap> filter(Glib::RefPtr<Gdk::Pixbuf> const &pixbuf) const;

    Geom::PathVector grayMapToPath(GrayMap const &gm, Async::Progress<double> &progress);

    void writePaths(potrace_path_t *paths, Geom::PathBuilder &builder, std::unordered_set<Geom::Point, geom_point_hash> &points, Async::Progress<double> &progress) const;
};

} // namespace Potrace
} // namespace Trace
} // namespace Inkscape

#endif // INKSCAPE_TRACE_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 :