You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.9 KiB

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