25using PathSegment = potrace_dpoint_t[3];
27static int to_potrace(
const TurnPolicy turn_policy)
29 switch (turn_policy) {
30 case TurnPolicy ::Foreground:
31 return POTRACE_TURNPOLICY_BLACK;
32 case TurnPolicy ::Background:
33 return POTRACE_TURNPOLICY_WHITE;
34 case TurnPolicy ::Left:
35 return POTRACE_TURNPOLICY_LEFT;
36 case TurnPolicy ::Right:
37 return POTRACE_TURNPOLICY_RIGHT;
38 case TurnPolicy ::Minority:
39 return POTRACE_TURNPOLICY_MINORITY;
40 case TurnPolicy ::Majority:
41 return POTRACE_TURNPOLICY_MAJORITY;
42 case TurnPolicy ::Random:
43 return POTRACE_TURNPOLICY_RANDOM;
46 return POTRACE_TURNPOLICY_MINORITY;
51 constexpr int BM_WORDSIZE = int(
sizeof(potrace_word));
52 constexpr int BM_WORDBITS = 8 * BM_WORDSIZE;
55 const int32_t dy = (size.x + BM_WORDBITS - 1) / BM_WORDBITS;
79 constexpr int BM_WORDSIZE = int(
sizeof(potrace_word));
80 constexpr int BM_WORDBITS = 8 * BM_WORDSIZE;
81 constexpr potrace_word BM_HIBIT = potrace_word(1) << (BM_WORDBITS - 1);
87 const int num_words =
bm.dy *
bm.h;
88 const int words_per_scanline =
bm.dy;
94 for (const int y : range) {
95 Span<potrace_word> scanline_words = words.slice(
96 IndexRange(words_per_scanline * y, words_per_scanline));
97 const MutableSpan<ColorGeometry4b> scanline_colors = colors.slice(
98 IndexRange(y * ibuf->x, ibuf->x));
99 for (uint32_t x = 0; x < ibuf->x; x++) {
100 const potrace_word &word = scanline_words[x / BM_WORDBITS];
101 const potrace_word mask = BM_HIBIT >> (x & (BM_WORDBITS - 1));
102 scanline_colors[x] = ((word & mask) != 0 ? ColorGeometry4b(255, 0, 0, 255) :
103 ColorGeometry4b(0, 0, 255, 255));
111Trace *trace_bitmap(
const TraceParams ¶ms, Bitmap &
bm)
113 potrace_param_t *po_params = potrace_param_default();
117 po_params->turdsize = params.size_threshold;
118 po_params->turnpolicy = to_potrace(params.turn_policy);
119 po_params->alphamax = params.alpha_max;
120 po_params->opticurve = params.optimize_curves;
121 po_params->opttolerance = params.optimize_tolerance;
123 potrace_state_t *st = potrace_trace(po_params, &
bm);
124 potrace_param_free(po_params);
126 if (!st || st->status != POTRACE_STATUS_OK) {
128 potrace_state_free(st);
137 potrace_state_free(trace);
147 FunctionRef<
float3(
const int2 &)> pixel_to_position)
149 auto project_pixel = [&](
const potrace_dpoint_t &point) ->
float3 {
150 return pixel_to_position(
int2(point.x, point.y));
155 for (
const potrace_path_t *path =
trace.plist; path !=
nullptr; path = path->next) {
156 const Span<int> path_tags = {path->curve.tag, path->curve.n};
160 for (
const int segment_i : path_segments.
index_range()) {
161 switch (path_tags[segment_i]) {
165 case POTRACE_CURVETO:
173 offsets.append(point_num);
177 const OffsetIndices points_by_curve = offset_indices::accumulate_counts_to_offsets(offsets);
178 if (points_by_curve.is_empty()) {
182 bke::CurvesGeometry
curves(points_by_curve.total_size(), points_by_curve.size());
183 curves.offsets_for_write().copy_from(offsets);
188 curves.attributes_for_write().add<
bool>(
189 "cyclic", bke::AttrDomain::Curve, bke::AttributeInitValue(
true));
191 MutableSpan<int8_t> handle_types_left =
curves.handle_types_left_for_write();
192 MutableSpan<int8_t> handle_types_right =
curves.handle_types_right_for_write();
193 MutableSpan<float3> handle_positions_left =
curves.handle_positions_left_for_write();
194 MutableSpan<float3> handle_positions_right =
curves.handle_positions_right_for_write();
195 MutableSpan<float3> positions =
curves.positions_for_write();
199 for (
const potrace_path_t *path =
trace.plist; path !=
nullptr; path = path->next, ++curve_i) {
200 const Span<int> path_tags = {path->curve.tag, path->curve.n};
203 const IndexRange points = points_by_curve[curve_i];
204 if (points.is_empty()) {
210 int point_i = points.
last();
211 auto next_point = [&]() {
212 point_i = (point_i == points.last() ? points.first() : point_i + 1);
215 for (
const int segment_i : path_segments.
index_range()) {
216 const PathSegment &
segment = path_segments[segment_i];
217 switch (path_tags[segment_i]) {
225 positions[point_i] = project_pixel(segment[1]);
230 positions[point_i] = project_pixel(segment[2]);
233 case POTRACE_CURVETO:
237 handle_positions_right[point_i] = project_pixel(segment[0]);
240 positions[point_i] = project_pixel(segment[2]);
242 handle_positions_left[point_i] = project_pixel(segment[1]);
251 curves.tag_topology_changed();
252 curves.tag_positions_changed();
253 curves.tag_radii_changed();
256 curves.calculate_bezier_auto_handles();