Code

fix by Preben S for LP bug 389780, flatness
[inkscape.git] / src / trace / potrace / curve.cpp
1 /* Copyright (C) 2001-2007 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 <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include "potracelib.h"
13 #include "lists.h"
14 #include "curve.h"
16 #define SAFE_MALLOC(var, n, typ) \
17   if ((var = (typ *)malloc((n)*sizeof(typ))) == NULL) goto malloc_error 
19 /* ---------------------------------------------------------------------- */
20 /* allocate and free path objects */
22 path_t *path_new(void) {
23   path_t *p = NULL;
24   privpath_t *priv = NULL;
26   SAFE_MALLOC(p, 1, path_t);
27   memset(p, 0, sizeof(path_t));
28   SAFE_MALLOC(priv, 1, privpath_t);
29   memset(priv, 0, sizeof(privpath_t));
30   p->priv = priv;
31   return p;
33  malloc_error:
34   free(p);
35   free(priv);
36   return NULL;
37 }
39 /* free the members of the given curve structure. Leave errno unchanged. */
40 static void privcurve_free_members(privcurve_t *curve) {
41   free(curve->tag);
42   free(curve->c);
43   free(curve->vertex);
44   free(curve->alpha);
45   free(curve->alpha0);
46   free(curve->beta);
47 }
49 /* free a path. Leave errno untouched. */
50 void path_free(path_t *p) {
51   if (p) {
52     if (p->priv) {
53       free(p->priv->pt);
54       free(p->priv->lon);
55       free(p->priv->sums);
56       free(p->priv->po);
57       privcurve_free_members(&p->priv->curve);
58       privcurve_free_members(&p->priv->ocurve);
59     }
60     free(p->priv);
61     /* do not free p->fcurve ! */
62   }
63   free(p);
64 }  
66 /* free a pathlist, leaving errno untouched. */
67 void pathlist_free(path_t *plist) {
68   path_t *p;
70   list_forall_unlink(p, plist) {
71     path_free(p);
72   }
73 }
75 /* ---------------------------------------------------------------------- */
76 /* initialize and finalize curve structures */
78 typedef dpoint_t dpoint3_t[3];
80 /* initialize the members of the given curve structure to size m.
81    Return 0 on success, 1 on error with errno set. */
82 int privcurve_init(privcurve_t *curve, int n) {
83   memset(curve, 0, sizeof(privcurve_t));
84   curve->n = n;
85   SAFE_MALLOC(curve->tag, n, int);
86   SAFE_MALLOC(curve->c, n, dpoint3_t);
87   SAFE_MALLOC(curve->vertex, n, dpoint_t);
88   SAFE_MALLOC(curve->alpha, n, double);
89   SAFE_MALLOC(curve->alpha0, n, double);
90   SAFE_MALLOC(curve->beta, n, double);
91   return 0;
93  malloc_error:
94   free(curve->tag);
95   free(curve->c);
96   free(curve->vertex);
97   free(curve->alpha);
98   free(curve->alpha0);
99   free(curve->beta);
100   return 1;
103 /* copy private to public curve structure */
104 void privcurve_to_curve(privcurve_t *pc, potrace_curve_t *c) {
105   c->n = pc->n;
106   c->tag = pc->tag;
107   c->c = pc->c;
109