2015-12-03 15:46:17 +00:00
|
|
|
/* Copyright (C) 2001-2015 Peter Selinger.
|
2010-06-10 18:43:12 +00:00
|
|
|
* This file is part of Potrace. It is free software and it is covered
|
2015-12-03 15:46:17 +00:00
|
|
|
* by the GNU General Public License. See the file COPYING for details. */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "potracelib.h"
|
|
|
|
#include "curve.h"
|
|
|
|
#include "decompose.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "progress.h"
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <potrace_version.h>
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "config.h"
|
2010-06-10 18:43:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* default parameters */
|
|
|
|
static const potrace_param_t param_default =
|
|
|
|
{
|
|
|
|
2, /* turdsize */
|
|
|
|
POTRACE_TURNPOLICY_MINORITY, /* turnpolicy */
|
|
|
|
1.0, /* alphamax */
|
|
|
|
1, /* opticurve */
|
|
|
|
0.2, /* opttolerance */
|
|
|
|
{
|
|
|
|
NULL, /* callback function */
|
|
|
|
NULL, /* callback data */
|
|
|
|
0.0, 1.0, /* progress range */
|
|
|
|
0.0, /* granularity */
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Return a fresh copy of the set of default parameters, or NULL on
|
|
|
|
* failure with errno set. */
|
|
|
|
potrace_param_t* potrace_param_default( void )
|
|
|
|
{
|
|
|
|
potrace_param_t* p;
|
|
|
|
|
|
|
|
p = (potrace_param_t*) malloc( sizeof(potrace_param_t) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !p )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
memcpy( p, ¶m_default, sizeof(potrace_param_t) );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* On success, returns a Potrace state st with st->status ==
|
|
|
|
* POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
|
|
|
|
* could be created (with errno set), or returns an incomplete Potrace
|
2015-12-03 15:46:17 +00:00
|
|
|
* state (with st->status == POTRACE_STATUS_INCOMPLETE, and with errno
|
|
|
|
* set). Complete or incomplete Potrace state can be freed with
|
|
|
|
* potrace_state_free(). */
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_state_t* potrace_trace( const potrace_param_t* param, const potrace_bitmap_t* bm )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
int r;
|
|
|
|
path_t* plist = NULL;
|
2010-06-10 18:43:12 +00:00
|
|
|
potrace_state_t* st;
|
2015-12-03 15:46:17 +00:00
|
|
|
progress_t prog;
|
|
|
|
progress_t subprog;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* prepare private progress bar state */
|
|
|
|
prog.callback = param->progress.callback;
|
2015-12-03 15:46:17 +00:00
|
|
|
prog.data = param->progress.data;
|
|
|
|
prog.min = param->progress.min;
|
|
|
|
prog.max = param->progress.max;
|
|
|
|
prog.epsilon = param->progress.epsilon;
|
|
|
|
prog.d_prev = param->progress.min;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* allocate state object */
|
|
|
|
st = (potrace_state_t*) malloc( sizeof(potrace_state_t) );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( !st )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_subrange_start( 0.0, 0.1, &prog, &subprog );
|
|
|
|
|
|
|
|
/* process the image */
|
|
|
|
r = bm_to_pathlist( bm, &plist, param, &subprog );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( r )
|
|
|
|
{
|
|
|
|
free( st );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
st->status = POTRACE_STATUS_OK;
|
|
|
|
st->plist = plist;
|
|
|
|
st->priv = NULL; /* private state currently unused */
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
progress_subrange_end( &prog, &subprog );
|
|
|
|
|
|
|
|
progress_subrange_start( 0.1, 1.0, &prog, &subprog );
|
|
|
|
|
|
|
|
/* partial success. */
|
|
|
|
r = process_path( plist, param, &subprog );
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( r )
|
|
|
|
{
|
|
|
|
st->status = POTRACE_STATUS_INCOMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_subrange_end( &prog, &subprog );
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free a Potrace state, without disturbing errno. */
|
|
|
|
void potrace_state_free( potrace_state_t* st )
|
|
|
|
{
|
|
|
|
pathlist_free( st->plist );
|
|
|
|
free( st );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free a parameter list, without disturbing errno. */
|
|
|
|
void potrace_param_free( potrace_param_t* p )
|
|
|
|
{
|
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* potrace_version( void )
|
|
|
|
{
|
|
|
|
return POTRACELIB_VERSION;
|
|
|
|
}
|