1 /* Copyright (C) 2001-2017 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 /* private part of the path and curve data structures */
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "curve.h"
16 #include "lists.h"
17 #include "potracelib.h"
18 
19 #define SAFE_CALLOC( var, n, typ )                            \
20     if( ( var = (typ*) calloc( n, sizeof( typ ) ) ) == NULL ) \
21         goto calloc_error
22 
23 /* ---------------------------------------------------------------------- */
24 /* allocate and free path objects */
25 
path_new(void)26 path_t* path_new( void )
27 {
28     path_t* p = NULL;
29     privpath_t* priv = NULL;
30 
31     SAFE_CALLOC( p, 1, path_t );
32     memset( p, 0, sizeof( path_t ) );
33     SAFE_CALLOC( priv, 1, privpath_t );
34     memset( priv, 0, sizeof( privpath_t ) );
35     p->priv = priv;
36     return p;
37 
38 calloc_error:
39     free( p );
40     free( priv );
41     return NULL;
42 }
43 
44 
45 /* free the members of the given curve structure. Leave errno unchanged. */
privcurve_free_members(privcurve_t * curve)46 static void privcurve_free_members( privcurve_t* curve )
47 {
48     free( curve->tag );
49     free( curve->c );
50     free( curve->vertex );
51     free( curve->alpha );
52     free( curve->alpha0 );
53     free( curve->beta );
54 }
55 
56 
57 /* free a path. Leave errno untouched. */
path_free(path_t * p)58 void path_free( path_t* p )
59 {
60     if( p )
61     {
62         if( p->priv )
63         {
64             free( p->priv->pt );
65             free( p->priv->lon );
66             free( p->priv->sums );
67             free( p->priv->po );
68             privcurve_free_members( &p->priv->curve );
69             privcurve_free_members( &p->priv->ocurve );
70         }
71 
72         free( p->priv );
73         /* do not free p->fcurve ! */
74     }
75 
76     free( p );
77 }
78 
79 
80 /* free a pathlist, leaving errno untouched. */
pathlist_free(path_t * plist)81 void pathlist_free( path_t* plist )
82 {
83     path_t* p;
84 
85     list_forall_unlink( p, plist ) {
86         path_free( p );
87     }
88 }
89 
90 
91 /* ---------------------------------------------------------------------- */
92 /* initialize and finalize curve structures */
93 
94 typedef dpoint_t dpoint3_t[3];
95 
96 /* initialize the members of the given curve structure to size m.
97  *  Return 0 on success, 1 on error with errno set. */
privcurve_init(privcurve_t * curve,int n)98 int privcurve_init( privcurve_t* curve, int n )
99 {
100     memset( curve, 0, sizeof( privcurve_t ) );
101     curve->n = n;
102     SAFE_CALLOC( curve->tag, n, int );
103     SAFE_CALLOC( curve->c, n, dpoint3_t );
104     SAFE_CALLOC( curve->vertex, n, dpoint_t );
105     SAFE_CALLOC( curve->alpha, n, double );
106     SAFE_CALLOC( curve->alpha0, n, double );
107     SAFE_CALLOC( curve->beta, n, double );
108     return 0;
109 
110 calloc_error:
111     free( curve->tag );
112     free( curve->c );
113     free( curve->vertex );
114     free( curve->alpha );
115     free( curve->alpha0 );
116     free( curve->beta );
117     return 1;
118 }
119 
120 
121 /* copy private to public curve structure */
privcurve_to_curve(privcurve_t * pc,potrace_curve_t * c)122 void privcurve_to_curve( privcurve_t* pc, potrace_curve_t* c )
123 {
124     c->n = pc->n;
125     c->tag = pc->tag;
126     c->c = pc->c;
127 }
128