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
|
|
|
|
* by the GNU General Public License. See the file COPYING for details. */
|
|
|
|
|
|
|
|
/* private part of the path and curve data structures */
|
|
|
|
|
2016-02-28 17:04:51 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "potracelib.h"
|
|
|
|
#include "lists.h"
|
|
|
|
#include "curve.h"
|
2010-06-10 18:43:12 +00:00
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#define SAFE_CALLOC( var, n, typ ) \
|
|
|
|
if( ( var = (typ*) calloc( n, sizeof(typ) ) ) == NULL ) \
|
|
|
|
goto calloc_error
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* allocate and free path objects */
|
|
|
|
|
|
|
|
path_t* path_new( void )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
path_t* p = NULL;
|
2010-06-10 18:43:12 +00:00
|
|
|
privpath_t* priv = NULL;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
SAFE_CALLOC( p, 1, path_t );
|
2010-06-10 18:43:12 +00:00
|
|
|
memset( p, 0, sizeof(path_t) );
|
2015-12-03 15:46:17 +00:00
|
|
|
SAFE_CALLOC( priv, 1, privpath_t );
|
2010-06-10 18:43:12 +00:00
|
|
|
memset( priv, 0, sizeof(privpath_t) );
|
|
|
|
p->priv = priv;
|
|
|
|
return p;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
calloc_error:
|
2010-06-10 18:43:12 +00:00
|
|
|
free( p );
|
|
|
|
free( priv );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free the members of the given curve structure. Leave errno unchanged. */
|
|
|
|
static void privcurve_free_members( privcurve_t* curve )
|
|
|
|
{
|
|
|
|
free( curve->tag );
|
|
|
|
free( curve->c );
|
|
|
|
free( curve->vertex );
|
|
|
|
free( curve->alpha );
|
|
|
|
free( curve->alpha0 );
|
|
|
|
free( curve->beta );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free a path. Leave errno untouched. */
|
|
|
|
void path_free( path_t* p )
|
|
|
|
{
|
|
|
|
if( p )
|
|
|
|
{
|
|
|
|
if( p->priv )
|
|
|
|
{
|
|
|
|
free( p->priv->pt );
|
|
|
|
free( p->priv->lon );
|
|
|
|
free( p->priv->sums );
|
|
|
|
free( p->priv->po );
|
|
|
|
privcurve_free_members( &p->priv->curve );
|
|
|
|
privcurve_free_members( &p->priv->ocurve );
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
free( p->priv );
|
|
|
|
/* do not free p->fcurve ! */
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* free a pathlist, leaving errno untouched. */
|
|
|
|
void pathlist_free( path_t* plist )
|
|
|
|
{
|
|
|
|
path_t* p;
|
|
|
|
|
|
|
|
list_forall_unlink( p, plist ) {
|
|
|
|
path_free( p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* initialize and finalize curve structures */
|
|
|
|
|
|
|
|
typedef dpoint_t dpoint3_t[3];
|
|
|
|
|
|
|
|
/* initialize the members of the given curve structure to size m.
|
|
|
|
* Return 0 on success, 1 on error with errno set. */
|
|
|
|
int privcurve_init( privcurve_t* curve, int n )
|
|
|
|
{
|
|
|
|
memset( curve, 0, sizeof(privcurve_t) );
|
|
|
|
curve->n = n;
|
2015-12-03 15:46:17 +00:00
|
|
|
SAFE_CALLOC( curve->tag, n, int );
|
|
|
|
SAFE_CALLOC( curve->c, n, dpoint3_t );
|
|
|
|
SAFE_CALLOC( curve->vertex, n, dpoint_t );
|
|
|
|
SAFE_CALLOC( curve->alpha, n, double );
|
|
|
|
SAFE_CALLOC( curve->alpha0, n, double );
|
|
|
|
SAFE_CALLOC( curve->beta, n, double );
|
2010-06-10 18:43:12 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
calloc_error:
|
2010-06-10 18:43:12 +00:00
|
|
|
free( curve->tag );
|
|
|
|
free( curve->c );
|
|
|
|
free( curve->vertex );
|
|
|
|
free( curve->alpha );
|
|
|
|
free( curve->alpha0 );
|
|
|
|
free( curve->beta );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* copy private to public curve structure */
|
|
|
|
void privcurve_to_curve( privcurve_t* pc, potrace_curve_t* c )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
c->n = pc->n;
|
2010-06-10 18:43:12 +00:00
|
|
|
c->tag = pc->tag;
|
2015-12-03 15:46:17 +00:00
|
|
|
c->c = pc->c;
|
2010-06-10 18:43:12 +00:00
|
|
|
}
|