summaryrefslogtreecommitdiffstats
path: root/src/trace/autotrace/inkscape-autotrace.cpp
blob: d1478901b4f3d864e538f207d056c3214eb53943 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * This is the C++ glue between Inkscape and Autotrace
 *//*
 *
 * Authors:
 *   Marc Jeanmougin
 *
 * Copyright (C) 2018 Authors
 *
 * Released under GNU GPL v2+, 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 {

static guchar* to_3channels(GdkPixbuf* input) {
    if (!input) {
        return nullptr;
    }
    int imgsize = gdk_pixbuf_get_height(input) * gdk_pixbuf_get_width(input);
    guchar *out = (guchar*)malloc(3 * imgsize);
    if (!out) {
        g_warning("Autotrace::to_3channels: can not allocate memory for %d pixel image.", imgsize);
        return nullptr;
    }
    int x=0;
    guchar* pix = gdk_pixbuf_get_pixels (input);
    int rs = gdk_pixbuf_get_rowstride (input);
    for(int row=0;row<gdk_pixbuf_get_height(input);row++) {
        for (int col=0;col<gdk_pixbuf_get_width(input);col++) {
            guchar alpha = *(pix + row * rs + col * 4 + 3);
            guchar white = 255 - alpha;
            for(int chan=0;chan<3;chan++) {
                // guchar *pnew = (pix + row * rs + col * 3 + chan);
                guchar *pold = (pix + row * rs + col * 4 + chan);
                out[x++] = (guchar)(((int)(*pold) * (int)alpha / 256) + white);
            }
        }
    }
    return out;
}


/**
 *
 */
AutotraceTracingEngine::AutotraceTracingEngine()
    : keepGoing(1)
    , traceType(TRACE_OUTLINE)
    , invert(false)
{
    /* get default parameters */
    opts = at_fitting_opts_new();
    opts->background_color = at_color_new(255,255,255);
    autotrace_init();
}

AutotraceTracingEngine::~AutotraceTracingEngine() { at_fitting_opts_free(opts); }



// TODO
Glib::RefPtr<Gdk::Pixbuf> AutotraceTracingEngine::preview(Glib::RefPtr<Gdk::Pixbuf> thePixbuf) { 
    //auto x = thePixbuf.copy();
    guchar *pb = to_3channels(thePixbuf->gobj());
    if (!pb) {
        return Glib::RefPtr<Gdk::Pixbuf>();
    }
    return Gdk::Pixbuf::create_from_data(pb, thePixbuf->get_colorspace(), false, 8, thePixbuf->get_width(),
                                         thePixbuf->get_height(), (thePixbuf->get_width() * 3),
                                         [](const guint8 *pb) { free(const_cast<guint8 *>(pb)); });
}

int test_cancel (void* keepGoing){return !(* ((int*)keepGoing));}

/**
 *  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 *pb1 = pixbuf->gobj();
    guchar *pb = to_3channels(pb1);
    if (!pb) {
        return std::vector<TracingEngineResult>();
    }
    
    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_bitmap_new(gdk_pixbuf_get_width(pb1), gdk_pixbuf_get_height(pb1), 3);
    free(bitmap->bitmap); // should create at_bitmap with bitmap->bitmap = pb
    bitmap->bitmap = pb;
    
    at_splines_type *splines = at_splines_new_full(bitmap, opts, NULL, NULL, NULL, NULL, test_cancel, &keepGoing);
    // 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 :