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>
8 #include "potracelib.h"
9 #include <inkscape_version.h>
10 #include "curve.h"
11 #include "decompose.h"
12 #include "trace.h"
13 #include "progress.h"
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
19 /* default parameters */
20 static const potrace_param_t param_default = {
21 2, /* turdsize */
22 POTRACE_TURNPOLICY_MINORITY, /* turnpolicy */
23 1.0, /* alphamax */
24 1, /* opticurve */
25 0.2, /* opttolerance */
26 {
27 NULL, /* callback function */
28 NULL, /* callback data */
29 0.0, 1.0, /* progress range */
30 0.0, /* granularity */
31 },
32 };
34 /* Return a fresh copy of the set of default parameters, or NULL on
35 failure with errno set. */
36 potrace_param_t *potrace_param_default(void) {
37 potrace_param_t *p;
39 p = (potrace_param_t *) malloc(sizeof(potrace_param_t));
40 if (!p) {
41 return NULL;
42 }
43 memcpy(p, ¶m_default, sizeof(potrace_param_t));
44 return p;
45 }
47 /* On success, returns a Potrace state st with st->status ==
48 POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
49 could be created (with errno set), or returns an incomplete Potrace
50 state (with st->status == POTRACE_STATUS_INCOMPLETE). Complete or
51 incomplete Potrace state can be freed with potrace_state_free(). */
52 potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm) {
53 int r;
54 path_t *plist = NULL;
55 potrace_state_t *st;
56 progress_t prog;
57 progress_t subprog;
59 /* prepare private progress bar state */
60 prog.callback = param->progress.callback;
61 prog.data = param->progress.data;
62 prog.min = param->progress.min;
63 prog.max = param->progress.max;
64 prog.epsilon = param->progress.epsilon;
65 prog.d_prev = param->progress.min;
67 /* allocate state object */
68 st = (potrace_state_t *)malloc(sizeof(potrace_state_t));
69 if (!st) {
70 return NULL;
71 }
73 progress_subrange_start(0.0, 0.1, &prog, &subprog);
75 /* process the image */
76 r = bm_to_pathlist(bm, &plist, param, &subprog);
77 if (r) {
78 free(st);
79 return NULL;
80 }
82 st->status = POTRACE_STATUS_OK;
83 st->plist = plist;
84 st->priv = NULL; /* private state currently unused */
86 progress_subrange_end(&prog, &subprog);
88 progress_subrange_start(0.1, 1.0, &prog, &subprog);
90 /* partial success. */
91 r = process_path(plist, param, &subprog);
92 if (r) {
93 st->status = POTRACE_STATUS_INCOMPLETE;
94 }
96 progress_subrange_end(&prog, &subprog);
98 return st;
99 }
101 /* free a Potrace state, without disturbing errno. */
102 void potrace_state_free(potrace_state_t *st) {
103 pathlist_free(st->plist);
104 free(st);
105 }
107 /* free a parameter list, without disturbing errno. */
108 void potrace_param_free(potrace_param_t *p) {
109 free(p);
110 }
112 char *potrace_version(void) {
113 return "potracelib "INKSCAPE_VERSION"";
114 }