KiCad PCB EDA Suite
potracelib.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2015 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. */
4 
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "potracelib.h"
13 #include "curve.h"
14 #include "decompose.h"
15 #include "trace.h"
16 #include "progress.h"
17 #include <potrace_version.h>
18 
19 /* default parameters */
21 {
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 };
34 
35 /* Return a fresh copy of the set of default parameters, or NULL on
36  * failure with errno set. */
38 {
39  potrace_param_t* p;
40 
41  p = (potrace_param_t*) malloc( sizeof(potrace_param_t) );
42 
43  if( !p )
44  {
45  return NULL;
46  }
47 
48  memcpy( p, &param_default, sizeof(potrace_param_t) );
49  return p;
50 }
51 
52 
53 /* On success, returns a Potrace state st with st->status ==
54  * POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
55  * could be created (with errno set), or returns an incomplete Potrace
56  * state (with st->status == POTRACE_STATUS_INCOMPLETE, and with errno
57  * set). Complete or incomplete Potrace state can be freed with
58  * potrace_state_free(). */
60 {
61  int r;
62  path_t* plist = NULL;
63  potrace_state_t* st;
64  progress_t prog;
65  progress_t subprog;
66 
67  /* prepare private progress bar state */
68  prog.callback = param->progress.callback;
69  prog.data = param->progress.data;
70  prog.min = param->progress.min;
71  prog.max = param->progress.max;
72  prog.epsilon = param->progress.epsilon;
73  prog.d_prev = param->progress.min;
74 
75  /* allocate state object */
76  st = (potrace_state_t*) malloc( sizeof(potrace_state_t) );
77 
78  if( !st )
79  {
80  return NULL;
81  }
82 
83  progress_subrange_start( 0.0, 0.1, &prog, &subprog );
84 
85  /* process the image */
86  r = bm_to_pathlist( bm, &plist, param, &subprog );
87 
88  if( r )
89  {
90  free( st );
91  return NULL;
92  }
93 
95  st->plist = plist;
96  st->priv = NULL; /* private state currently unused */
97 
98  progress_subrange_end( &prog, &subprog );
99 
100  progress_subrange_start( 0.1, 1.0, &prog, &subprog );
101 
102  /* partial success. */
103  r = process_path( plist, param, &subprog );
104 
105  if( r )
106  {
108  }
109 
110  progress_subrange_end( &prog, &subprog );
111 
112  return st;
113 }
114 
115 
116 /* free a Potrace state, without disturbing errno. */
118 {
119  pathlist_free( st->plist );
120  free( st );
121 }
122 
123 
124 /* free a parameter list, without disturbing errno. */
126 {
127  free( p );
128 }
129 
130 
131 const char* potrace_version( void )
132 {
133  return POTRACELIB_VERSION;
134 }
void pathlist_free(path_t *plist)
Definition: curve.cpp:81
void potrace_param_free(potrace_param_t *p)
Definition: potracelib.cpp:125
#define POTRACE_STATUS_OK
Definition: potracelib.h:110
static void progress_subrange_start(double a, double b, const progress_t *prog, progress_t *sub)
Definition: progress.h:49
static void progress_subrange_end(progress_t *prog, progress_t *sub)
Definition: progress.h:81
static const potrace_param_t param_default
Definition: potracelib.cpp:20
double max
Definition: progress.h:19
int process_path(path_t *plist, const potrace_param_t *param, progress_t *progress)
Definition: trace.cpp:1489
potrace_state_t * potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm)
Definition: potracelib.cpp:59
double d_prev
Definition: progress.h:22
int bm_to_pathlist(const potrace_bitmap_t *bm, path_t **plistp, const potrace_param_t *param, progress_t *progress)
Definition: decompose.cpp:555
#define POTRACELIB_VERSION
double min
Definition: progress.h:19
void * data
Definition: progress.h:18
potrace_progress_t progress
Definition: potracelib.h:45
const char * potrace_version(void)
Definition: potracelib.cpp:131
struct potrace_privstate_s * priv
Definition: potracelib.h:118
double epsilon
Definition: progress.h:20
void(* callback)(double progress, void *privdata)
Definition: potracelib.h:30
#define POTRACE_TURNPOLICY_MINORITY
Definition: potracelib.h:23
void(* callback)(double progress, void *privdata)
Definition: progress.h:17
#define POTRACE_STATUS_INCOMPLETE
Definition: potracelib.h:111
void potrace_state_free(potrace_state_t *st)
Definition: potracelib.cpp:117
potrace_param_t * potrace_param_default(void)
Definition: potracelib.cpp:37
potrace_path_t * plist
Definition: potracelib.h:116