curve.c 2.6 KB

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