~vaifrax/inkscape/bugfix170049

« back to all changes in this revision

Viewing changes to src/trace/potrace/potracelib.cpp

  • Committer: mental
  • Date: 2006-01-16 02:36:01 UTC
  • Revision ID: mental@users.sourceforge.net-20060116023601-wkr0h7edl5veyudq
moving trunk for module inkscape

Show diffs side-by-side

added added

removed removed

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