1 /* Copyright (C) 2001-2007 Peter Selinger.
2 This file is part of Potrace. It is free software and it is covered
3 by the GNU General Public License. See the file COPYING for details. */
5 #include <stdlib.h>
6 #include <string.h>
7 #include <glib/gstrfuncs.h>
9 #include "potracelib.h"
10 #include "inkscape-version.h"
11 #include "curve.h"
12 #include "decompose.h"
13 #include "trace.h"
14 #include "progress.h"
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 /* default parameters */
21 static const potrace_param_t param_default = {
22 2, /* turdsize */
23 POTRACE_TURNPOLICY_MINORITY, /* turnpolicy */
24 1.0, /* alphamax */
25 1, /* opticurve */
26 0.2, /* opttolerance */
27 {
28 NULL, /* callback function */
29 NULL, /* callback data */
30 0.0, 1.0, /* progress range */
31 0.0, /* granularity */
32 },
33 };
35 /* Return a fresh copy of the set of default parameters, or NULL on
36 failure with errno set. */
37 potrace_param_t *potrace_param_default(void) {
38 potrace_param_t *p;
40 p = (potrace_param_t *) malloc(sizeof(potrace_param_t));
41 if (!p) {
42 return NULL;
43 }
44 memcpy(p, ¶m_default, sizeof(potrace_param_t));
45 return p;
46 }
48 /* On success, returns a Potrace state st with st->status ==
49 POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
50 could be created (with errno set), or returns an incomplete Potrace
51 state (with st->status == POTRACE_STATUS_INCOMPLETE). Complete or
52 incomplete Potrace state can be freed with potrace_state_free(). */
53 potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm) {
54 int r;
55 path_t *plist = NULL;
56 potrace_state_t *st;
57 progress_t prog;
58 progress_t subprog;
60 /* prepare private progress bar state */
61 prog.callback = param->progress.callback;
62 prog.data = param->progress.data;
63 prog.min = param->progress.min;
64 prog.max = param->progress.max;
65 prog.epsilon = param->progress.epsilon;
66 prog.d_prev = param->progress.min;
68 /* allocate state object */
69 st = (potrace_state_t *)malloc(sizeof(potrace_state_t));
70 if (!st) {
71 return NULL;
72 }
74 progress_subrange_start(0.0, 0.1, &prog, &subprog);
76 /* process the image */
77 r = bm_to_pathlist(bm, &plist, param, &subprog);
78 if (r) {
79 free(st);
80 return NULL;
81 }
83 st->status = POTRACE_STATUS_OK;
84 st->plist = plist;
85 st->priv = NULL; /* private state currently unused */
87 progress_subrange_end(&prog, &subprog);
89 progress_subrange_start(0.1, 1.0, &prog, &subprog);
91 /* partial success. */
92 r = process_path(plist, param, &subprog);
93 if (r) {
94 st->status = POTRACE_STATUS_INCOMPLETE;
95 }
97 progress_subrange_end(&prog, &subprog);
99 return st;
100 }
102 /* free a Potrace state, without disturbing errno. */
103 void potrace_state_free(potrace_state_t *st) {
104 pathlist_free(st->plist);
105 free(st);
106 }
108 /* free a parameter list, without disturbing errno. */
109 void potrace_param_free(potrace_param_t *p) {
110 free(p);
111 }
113 char *potrace_version(void) {
114 static char *ver = g_strdup_printf("potracelib %s", Inkscape::version_string);
115 return ver;
116 }
118 /*
119 Local Variables:
120 mode:c++
121 c-file-style:"stroustrup"
122 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
123 indent-tabs-mode:nil
124 fill-column:99
125 End:
126 */
127 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :