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