Code

Update Potrace to 1.8
[inkscape.git] / src / trace / potrace / potracelib.cpp
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 "curve.h"
10 #include "decompose.h"
11 #include "trace.h"
12 #include "progress.h"
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
18 /* default parameters */
19 static const potrace_param_t param_default = {
20   2,                             /* turdsize */
21   POTRACE_TURNPOLICY_MINORITY,   /* turnpolicy */
22   1.0,                           /* alphamax */
23   1,                             /* opticurve */
24   0.2,                           /* opttolerance */
25   {
26     NULL,                        /* callback function */
27     NULL,                        /* callback data */
28     0.0, 1.0,                    /* progress range */
29     0.0,                         /* granularity */
30   },
31 };
33 /* Return a fresh copy of the set of default parameters, or NULL on
34    failure with errno set. */
35 potrace_param_t *potrace_param_default(void) {
36   potrace_param_t *p;
38   p = (potrace_param_t *) malloc(sizeof(potrace_param_t));
39   if (!p) {
40     return NULL;
41   }
42   memcpy(p, &param_default, sizeof(potrace_param_t));
43   return p;
44 }
46 /* On success, returns a Potrace state st with st->status ==
47    POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
48    could be created (with errno set), or returns an incomplete Potrace
49    state (with st->status == POTRACE_STATUS_INCOMPLETE). Complete or
50    incomplete Potrace state can be freed with potrace_state_free(). */
51 potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm) {
52   int r;
53   path_t *plist = NULL;
54   potrace_state_t *st;
55   progress_t prog;
56   progress_t subprog;
57   
58   /* prepare private progress bar state */
59   prog.callback = param->progress.callback;
60   prog.data = param->progress.data;
61   prog.min = param->progress.min;
62   prog.max = param->progress.max;
63   prog.epsilon = param->progress.epsilon;
64   prog.d_prev = param->progress.min;
66   /* allocate state object */
67   st = (potrace_state_t *)malloc(sizeof(potrace_state_t));
68   if (!st) {
69     return NULL;
70   }
72   progress_subrange_start(0.0, 0.1, &prog, &subprog);
74   /* process the image */
75   r = bm_to_pathlist(bm, &plist, param, &subprog);
76   if (r) {
77     free(st);
78     return NULL;
79   }
81   st->status = POTRACE_STATUS_OK;
82   st->plist = plist;
83   st->priv = NULL;  /* private state currently unused */
85   progress_subrange_end(&prog, &subprog);
87   progress_subrange_start(0.1, 1.0, &prog, &subprog);
89   /* partial success. */
90   r = process_path(plist, param, &subprog);
91   if (r) {
92     st->status = POTRACE_STATUS_INCOMPLETE;
93   }
95   progress_subrange_end(&prog, &subprog);
97   return st;
98 }
100 /* free a Potrace state, without disturbing errno. */
101 void potrace_state_free(potrace_state_t *st) {
102   pathlist_free(st->plist);
103   free(st);
106 /* free a parameter list, without disturbing errno. */
107 void potrace_param_free(potrace_param_t *p) {
108   free(p);
111 char *potrace_version(void) {
112   return "potracelib "VERSION"";