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.

129 lines
3.3 KiB

  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.
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "potracelib.h"
  8. #include "curve.h"
  9. #include "decompose.h"
  10. #include "trace.h"
  11. #include "progress.h"
  12. #include "potrace_version.h"
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. /* default parameters */
  17. static const potrace_param_t param_default =
  18. {
  19. 2, /* turdsize */
  20. POTRACE_TURNPOLICY_MINORITY, /* turnpolicy */
  21. 1.0, /* alphamax */
  22. 1, /* opticurve */
  23. 0.2, /* opttolerance */
  24. {
  25. NULL, /* callback function */
  26. NULL, /* callback data */
  27. 0.0, 1.0, /* progress range */
  28. 0.0, /* granularity */
  29. },
  30. };
  31. /* Return a fresh copy of the set of default parameters, or NULL on
  32. * failure with errno set. */
  33. potrace_param_t* potrace_param_default( void )
  34. {
  35. potrace_param_t* p;
  36. p = (potrace_param_t*) malloc( sizeof(potrace_param_t) );
  37. if( !p )
  38. {
  39. return NULL;
  40. }
  41. memcpy( p, &param_default, sizeof(potrace_param_t) );
  42. return p;
  43. }
  44. /* On success, returns a Potrace state st with st->status ==
  45. * POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state
  46. * could be created (with errno set), or returns an incomplete Potrace
  47. * state (with st->status == POTRACE_STATUS_INCOMPLETE). Complete or
  48. * incomplete Potrace state can be freed with potrace_state_free(). */
  49. potrace_state_t* potrace_trace( const potrace_param_t* param, const potrace_bitmap_t* bm )
  50. {
  51. int r;
  52. path_t* plist = NULL;
  53. potrace_state_t* st;
  54. progress_t prog;
  55. progress_t subprog;
  56. /* prepare private progress bar state */
  57. prog.callback = param->progress.callback;
  58. prog.data = param->progress.data;
  59. prog.min = param->progress.min;
  60. prog.max = param->progress.max;
  61. prog.epsilon = param->progress.epsilon;
  62. prog.d_prev = param->progress.min;
  63. /* allocate state object */
  64. st = (potrace_state_t*) malloc( sizeof(potrace_state_t) );
  65. if( !st )
  66. {
  67. return NULL;
  68. }
  69. progress_subrange_start( 0.0, 0.1, &prog, &subprog );
  70. /* process the image */
  71. r = bm_to_pathlist( bm, &plist, param, &subprog );
  72. if( r )
  73. {
  74. free( st );
  75. return NULL;
  76. }
  77. st->status = POTRACE_STATUS_OK;
  78. st->plist = plist;
  79. st->priv = NULL; /* private state currently unused */
  80. progress_subrange_end( &prog, &subprog );
  81. progress_subrange_start( 0.1, 1.0, &prog, &subprog );
  82. /* partial success. */
  83. r = process_path( plist, param, &subprog );
  84. if( r )
  85. {
  86. st->status = POTRACE_STATUS_INCOMPLETE;
  87. }
  88. progress_subrange_end( &prog, &subprog );
  89. return st;
  90. }
  91. /* free a Potrace state, without disturbing errno. */
  92. void potrace_state_free( potrace_state_t* st )
  93. {
  94. pathlist_free( st->plist );
  95. free( st );
  96. }
  97. /* free a parameter list, without disturbing errno. */
  98. void potrace_param_free( potrace_param_t* p )
  99. {
  100. free( p );
  101. }
  102. const char* potrace_version( void )
  103. {
  104. return POTRACELIB_VERSION;
  105. }