1 /* Copyright (C) 2001-2005 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 /* $Id$ */
6 /* private part of the path and curve data structures */
8 #include <stdlib.h>
9 #include <string.h>
11 #include "lists.h"
12 #include "curve.h"
14 #define SAFE_MALLOC(var, n, typ) \
15 if ((var = (typ *)malloc((n)*sizeof(typ))) == NULL) goto malloc_error
17 /* ---------------------------------------------------------------------- */
18 /* allocate and free path objects */
20 path_t *path_new(void) {
21 path_t *p = NULL;
22 privpath_t *priv = NULL;
24 SAFE_MALLOC(p, 1, path_t);
25 memset(p, 0, sizeof(path_t));
26 SAFE_MALLOC(priv, 1, privpath_t);
27 memset(priv, 0, sizeof(privpath_t));
28 p->priv = priv;
29 return p;
31 malloc_error:
32 free(p);
33 free(priv);
34 return NULL;
35 }
37 /* free a path. Leave errno untouched. */
38 void path_free(path_t *p) {
39 if (p) {
40 if (p->priv) {
41 free(p->priv->pt);
42 free(p->priv->lon);
43 free(p->priv->sums);
44 free(p->priv->po);
45 privcurve_free_members(&p->priv->curve);
46 privcurve_free_members(&p->priv->ocurve);
47 }
48 free(p->priv);
49 /* do not free p->fcurve ! */
50 }
51 free(p);
52 }
54 /* free a pathlist, leaving errno untouched. */
55 void pathlist_free(path_t *plist) {
56 path_t *p;
58 list_forall_unlink(p, plist) {
59 path_free(p);
60 }
61 }
63 /* ---------------------------------------------------------------------- */
64 /* initialize and finalize curve structures */
66 typedef dpoint_t dpoint3_t[3];
68 /* initialize the members of the given curve structure to size m.
69 Return 0 on success, 1 on error with errno set. */
70 int privcurve_init(privcurve_t *curve, int n) {
71 memset(curve, 0, sizeof(privcurve_t));
72 curve->n = n;
73 SAFE_MALLOC(curve->tag, n, int);
74 SAFE_MALLOC(curve->c, n, dpoint3_t);
75 SAFE_MALLOC(curve->vertex, n, dpoint_t);
76 SAFE_MALLOC(curve->alpha, n, double);
77 SAFE_MALLOC(curve->alpha0, n, double);
78 SAFE_MALLOC(curve->beta, n, double);
79 return 0;
81 malloc_error:
82 free(curve->tag);
83 free(curve->c);
84 free(curve->vertex);
85 free(curve->alpha);
86 free(curve->alpha0);
87 free(curve->beta);
88 return 1;
89 }
91 /* free the members of the given curve structure */
92 void privcurve_free_members(privcurve_t *curve) {
93 free(curve->tag);
94 free(curve->c);
95 free(curve->vertex);
96 free(curve->alpha);
97 free(curve->alpha0);
98 free(curve->beta);
99 }
101 /* copy private to public curve structure */
102 void privcurve_to_curve(privcurve_t *pc, potrace_curve_t *c) {
103 c->n = pc->n;
104 c->tag = pc->tag;
105 c->c = pc->c;
106 }