File indexing completed on 2024-12-22 04:04:14
0001 /* Copyright (C) 2001-2019 Peter Selinger. 0002 This file is part of Potrace. It is free software and it is covered 0003 by the GNU General Public License. See the file COPYING for details. */ 0004 0005 #ifdef HAVE_CONFIG_H 0006 #include <config.h> 0007 #endif 0008 0009 #include <stdlib.h> 0010 #include <string.h> 0011 0012 #include "potracelib.h" 0013 #include "curve.h" 0014 #include "decompose.h" 0015 #include "trace.h" 0016 #include "progress.h" 0017 0018 /* default parameters */ 0019 static const potrace_param_t param_default = { 0020 2, /* turdsize */ 0021 POTRACE_TURNPOLICY_MINORITY, /* turnpolicy */ 0022 1.0, /* alphamax */ 0023 1, /* opticurve */ 0024 0.2, /* opttolerance */ 0025 { 0026 NULL, /* callback function */ 0027 NULL, /* callback data */ 0028 0.0, 1.0, /* progress range */ 0029 0.0, /* granularity */ 0030 }, 0031 }; 0032 0033 /* Return a fresh copy of the set of default parameters, or NULL on 0034 failure with errno set. */ 0035 potrace_param_t *potrace_param_default(void) { 0036 potrace_param_t *p; 0037 0038 p = (potrace_param_t *) malloc(sizeof(potrace_param_t)); 0039 if (!p) { 0040 return NULL; 0041 } 0042 memcpy(p, ¶m_default, sizeof(potrace_param_t)); 0043 return p; 0044 } 0045 0046 /* On success, returns a Potrace state st with st->status == 0047 POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state 0048 could be created (with errno set), or returns an incomplete Potrace 0049 state (with st->status == POTRACE_STATUS_INCOMPLETE, and with errno 0050 set). Complete or incomplete Potrace state can be freed with 0051 potrace_state_free(). */ 0052 potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm) { 0053 int r; 0054 path_t *plist = NULL; 0055 potrace_state_t *st; 0056 progress_t prog; 0057 progress_t subprog; 0058 0059 /* prepare private progress bar state */ 0060 prog.callback = param->progress.callback; 0061 prog.data = param->progress.data; 0062 prog.min = param->progress.min; 0063 prog.max = param->progress.max; 0064 prog.epsilon = param->progress.epsilon; 0065 prog.d_prev = param->progress.min; 0066 0067 /* allocate state object */ 0068 st = (potrace_state_t *)malloc(sizeof(potrace_state_t)); 0069 if (!st) { 0070 return NULL; 0071 } 0072 0073 progress_subrange_start(0.0, 0.1, &prog, &subprog); 0074 0075 /* process the image */ 0076 r = bm_to_pathlist(bm, &plist, param, &subprog); 0077 if (r) { 0078 free(st); 0079 return NULL; 0080 } 0081 0082 st->status = POTRACE_STATUS_OK; 0083 st->plist = plist; 0084 st->priv = NULL; /* private state currently unused */ 0085 0086 progress_subrange_end(&prog, &subprog); 0087 0088 progress_subrange_start(0.1, 1.0, &prog, &subprog); 0089 0090 /* partial success. */ 0091 r = process_path(plist, param, &subprog); 0092 if (r) { 0093 st->status = POTRACE_STATUS_INCOMPLETE; 0094 } 0095 0096 progress_subrange_end(&prog, &subprog); 0097 0098 return st; 0099 } 0100 0101 /* free a Potrace state, without disturbing errno. */ 0102 void potrace_state_free(potrace_state_t *st) { 0103 pathlist_free(st->plist); 0104 free(st); 0105 } 0106 0107 /* free a parameter list, without disturbing errno. */ 0108 void potrace_param_free(potrace_param_t *p) { 0109 free(p); 0110 } 0111 0112 const char *potrace_version(void) { 0113 return "potracelib " VERSION ""; 0114 }