Debian Codegraph @

converter/other/pamtosvg/autotrace.c

raw symbols SCIP
1 /* autotrace.c --- Autotrace API
2
3 Copyright (C) 2000, 2001, 2002 Martin Weber
4
5 The author can be contacted at <[email protected]>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "mallocvar.h"
22
23 #include "autotrace.h"
24 #include "exception.h"
25
26 #include "fit.h"
27 #include "bitmap.h"
28 #include "spline.h"
29
30 #include "image-header.h"
31 #include "image-proc.h"
32 #include "thin-image.h"
33
34
35 #define AT_DEFAULT_DPI 72
36
37 at_fitting_opts_type *
38 at_fitting_opts_new(void)
39 {
40 at_fitting_opts_type * opts;
41 MALLOCVAR_NOFAIL(opts);
42 return opts;
43 }
44
45
46
47 at_fitting_opts_type *
48 at_fitting_opts_copy (at_fitting_opts_type * original)
49 {
50 at_fitting_opts_type * new_opts;
51 if (original == NULL)
52 return NULL;
53
54 new_opts = at_fitting_opts_new ();
55 *new_opts = *original;
56 new_opts->backgroundSpec = original->backgroundSpec;
57 new_opts->background_color = original->background_color;
58 return new_opts;
59 }
60
61
62
63 void
64 at_fitting_opts_free(at_fitting_opts_type * opts)
65 {
66 free(opts);
67 }
68
69
70
71 at_output_opts_type *
72 at_output_opts_new(void)
73 {
74 at_output_opts_type * opts;
75 MALLOCVAR_NOFAIL(opts);
76 opts->dpi = AT_DEFAULT_DPI;
77 return opts;
78 }
79
80
81
82 at_output_opts_type *
83 at_output_opts_copy(at_output_opts_type * original)
84 {
85 at_output_opts_type * opts = at_output_opts_new();
86 *opts = *original;
87 return opts;
88 }
89
90
91
92 void
93 at_output_opts_free(at_output_opts_type * opts)
94 {
95 free(opts);
96 }
97
98
99
100 /* at_splines_new_full modifies its 'bitmap' argument
101 when it does the thin_image thing.
102 */
103 at_spline_list_array_type *
104 at_splines_new_full(at_bitmap_type * const bitmap,
105 at_fitting_opts_type * const opts,
106 at_msg_func msg_func,
107 void * const msg_data,
108 at_progress_func notify_progress,
109 void * const progress_data,
110 at_testcancel_func test_cancel,
111 void * const testcancel_data) {
112
113 at_spline_list_array_type * retval;
114 image_header_type image_header;
115 pixel_outline_list_type pixelOutlineList;
116 at_exception_type exp;
117 distance_map_type distanceMap;
118 bool haveDistMap;
119
120 exp = at_exception_new(msg_func, msg_data);
121
122 image_header.width = at_bitmap_get_width(bitmap);
123 image_header.height = at_bitmap_get_height(bitmap);
124
125 if (opts->centerline) {
126 if (opts->preserve_width) {
127 /* Preserve line width prior to thinning. */
128 bool const paddedTrue = true;
129 distanceMap = new_distance_map(*bitmap, 255, paddedTrue, &exp);
130 haveDistMap = true;
131 } else
132 haveDistMap = false;
133 thin_image(bitmap, opts->backgroundSpec, opts->background_color, &exp);
134 } else
135 haveDistMap = false;
136
137 if (at_exception_got_fatal(&exp))
138 retval = NULL;
139 else {
140 if (opts->centerline) {
141 pixel background_color;
142
143 if (opts->backgroundSpec)
144 background_color = opts->background_color;
145 else
146 PPM_ASSIGN(background_color, 255, 255, 255);
147
148 pixelOutlineList =
149 find_centerline_pixels(*bitmap, background_color,
150 notify_progress, progress_data,
151 test_cancel, testcancel_data, &exp);
152 } else
153 pixelOutlineList =
154 find_outline_pixels(*bitmap,
155 opts->backgroundSpec,
156 opts->background_color,
157 notify_progress, progress_data,
158 test_cancel, testcancel_data, &exp);
159
160 if (at_exception_got_fatal(&exp) ||
161 (test_cancel && test_cancel(testcancel_data)))
162 retval = NULL;
163 else {
164 at_spline_list_array_type * splinesP;
165
166 MALLOCVAR_NOFAIL(splinesP);
167 fit_outlines_to_splines(pixelOutlineList, opts,
168 haveDistMap ? &distanceMap : NULL,
169 image_header.width,
170 image_header.height,
171 &exp,
172 notify_progress, progress_data,
173 test_cancel, testcancel_data,
174 splinesP);
175
176 if (at_exception_got_fatal(&exp) ||
177 (test_cancel && test_cancel(testcancel_data)))
178 retval = NULL;
179 else {
180 if (notify_progress)
181 notify_progress(1.0, progress_data);
182
183 retval = splinesP;
184 }
185 free_pixel_outline_list(&pixelOutlineList);
186 }
187 if (haveDistMap)
188 free_distance_map(&distanceMap);
189 }
190 return retval;
191 }
192
193
194
195 void
196 at_splines_write(at_output_write_func outputWriter,
197 FILE * const writeto,
198 at_output_opts_type * const optsArg,
199 at_spline_list_array_type * const splinesP,
200 at_msg_func msgFunc,
201 void * const msgData) {
202
203 at_output_opts_type * optsP;
204 bool newOpts;
205 int llx, lly, urx, ury;
206 llx = 0;
207 lly = 0;
208 urx = splinesP->width;
209 ury = splinesP->height;
210
211 if (optsArg == NULL) {
212 newOpts = true;
213 optsP = at_output_opts_new();
214 } else {
215 newOpts = false;
216 optsP = optsArg;
217 }
218 (*outputWriter)(writeto, "DUMMYFILENAME",
219 llx, lly, urx, ury, optsP, *splinesP,
220 msgFunc, msgData);
221 if (newOpts)
222 at_output_opts_free(optsP);
223 }
224
225
226
227 void
228 at_splines_free(at_spline_list_array_type * const splines) {
229
230 free_spline_list_array(splines);
231 free(splines);
232 }
233
234
235