Blender V5.3
trace_util.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_color_types.hh"
10#include "BLI_span.hh"
11#include "BLI_string_ref.hh"
12#include "BLI_task.hh"
13
14#include "IMB_imbuf_types.hh"
15
16#ifdef WITH_POTRACE
17# include "potracelib.h"
18#endif
19
20namespace blender {
21
22namespace bke {
23class CurvesGeometry;
24}
25
26namespace ed::image_trace {
27
28#ifdef WITH_POTRACE
29using Bitmap = potrace_bitmap_t;
30using Trace = potrace_state_t;
31#else
32struct Bitmap;
33struct Trace;
34#endif
35
36Bitmap *create_bitmap(const int2 &size);
37void free_bitmap(Bitmap *bm);
38
45template<typename ThresholdFn> Bitmap *image_to_bitmap(const ImBuf &ibuf, ThresholdFn fn);
46ImBuf *bitmap_to_image(const Bitmap &bm);
47
48/* Policy for resolving ambiguity during decomposition of bitmaps into paths. */
49enum class TurnPolicy : int8_t {
50 /* Prefers to connect foreground pixels. */
52 /* Prefers to connect background pixels. */
54 /* Always take a left turn. */
55 Left = 2,
56 /* Always take a right turn. */
57 Right = 3,
58 /* Prefers to connect minority color in the neighborhood. */
60 /* Prefers to connect majority color in the neighborhood. */
62 /* Chose direction randomly. */
63 Random = 6,
64};
65
67 /* Area of the largest path to be ignored. */
69 /* Resolves ambiguous turns in path decomposition. */
71 /* Corner threshold. */
72 float alpha_max = 1.0f;
73 /* True to enable curve optimization. */
74 bool optimize_curves = true;
75 /* Curve optimization tolerance. */
76 float optimize_tolerance = 0.2f;
77};
78
82Trace *trace_bitmap(const TraceParams &params, Bitmap &bm);
83void free_trace(Trace *trace);
84
95 FunctionRef<float3(const int2 &)> pixel_to_position);
96
97/* Inline functions. */
98
103template<typename ThresholdFn> Bitmap *image_to_bitmap(const ImBuf &ibuf, ThresholdFn fn)
104{
105#ifdef WITH_POTRACE
106 constexpr int BM_WORDSIZE = int(sizeof(potrace_word));
107 constexpr int BM_WORDBITS = 8 * BM_WORDSIZE;
108 constexpr potrace_word BM_HIBIT = potrace_word(1) << (BM_WORDBITS - 1);
109
110 potrace_bitmap_t *bm = create_bitmap({ibuf.x, ibuf.y});
111 const int num_words = bm->dy * bm->h;
112 const int words_per_scanline = bm->dy;
113 /* Note: bitmap stores one bit per pixel, but can't easily use a BitSpan, because the bit order
114 * is reversed in each word (most-significant bit is on the left). */
115 MutableSpan<potrace_word> words = {bm->map, num_words};
116
117 if (const float *float_data = ibuf.float_data()) {
118 const Span<ColorGeometry4f> colors(reinterpret_cast<const ColorGeometry4f *>(float_data),
119 ibuf.x * ibuf.y);
120 threading::parallel_for(IndexRange(ibuf.y), 4096, [&](const IndexRange range) {
121 /* Use callback with the correct color conversion. */
122 constexpr bool is_float_color_fn =
123 std::is_invocable_r_v<void, ThresholdFn, const ColorGeometry4f &>;
124 for (const int y : range) {
125 MutableSpan<potrace_word> scanline_words = words.slice(
126 IndexRange(words_per_scanline * y, words_per_scanline));
127 const Span<ColorGeometry4f> scanline_colors = colors.slice(IndexRange(y * ibuf.x, ibuf.x));
128 for (int x = 0; x < ibuf.x; x++) {
129 potrace_word &word = scanline_words[x / BM_WORDBITS];
130 const potrace_word mask = BM_HIBIT >> (x & (BM_WORDBITS - 1));
131
132 const ColorGeometry4f &fcolor = scanline_colors[x];
133 bool is_foreground;
134 if constexpr (!is_float_color_fn) {
135 is_foreground = fn(
136 ColorGeometry4b(fcolor.r * 255, fcolor.g * 255, fcolor.b * 255, fcolor.a * 255));
137 }
138 else {
139 is_foreground = fn(fcolor);
140 }
141
142 if (is_foreground) {
143 word |= mask;
144 }
145 else {
146 word &= ~mask;
147 }
148 }
149 }
150 });
151 return bm;
152 }
153
154 const Span<ColorGeometry4b> colors(reinterpret_cast<const ColorGeometry4b *>(ibuf.byte_data()),
155 ibuf.x * ibuf.y);
156 threading::parallel_for(IndexRange(ibuf.y), 4096, [&](const IndexRange range) {
157 /* Use callback with the correct color conversion. */
158 constexpr bool is_float_color_fn =
159 std::is_invocable_r_v<void, ThresholdFn, const ColorGeometry4f &>;
160 for (const int y : range) {
161 MutableSpan<potrace_word> scanline_words = words.slice(
162 IndexRange(words_per_scanline * y, words_per_scanline));
163 const Span<ColorGeometry4b> scanline_colors = colors.slice(IndexRange(y * ibuf.x, ibuf.x));
164 for (uint32_t x = 0; x < ibuf.x; x++) {
165 potrace_word &word = scanline_words[x / BM_WORDBITS];
166 const potrace_word mask = BM_HIBIT >> (x & (BM_WORDBITS - 1));
167
168 const ColorGeometry4b bcolor = scanline_colors[x];
169 bool is_foreground;
170 if constexpr (is_float_color_fn) {
171 is_foreground = fn(ColorGeometry4f(
172 bcolor.r / 255.0f, bcolor.r / 255.0f, bcolor.r / 255.0f, bcolor.r / 255.0f));
173 }
174 else {
175 is_foreground = fn(bcolor);
176 }
177
178 if (is_foreground) {
179 word |= mask;
180 }
181 else {
182 word &= ~mask;
183 }
184 }
185 }
186 });
187 return bm;
188#else
189 UNUSED_VARS(ibuf, fn);
190 return nullptr;
191#endif
192}
193
194} // namespace ed::image_trace
195
196} // namespace blender
#define UNUSED_VARS(...)
constexpr IndexRange()=default
void free_bitmap(Bitmap *bm)
bke::CurvesGeometry trace_to_curves(const Trace &trace, const float4x4 &transform)
Bitmap * create_bitmap(const int2 &size)
Bitmap * image_to_bitmap(const ImBuf &ibuf, ThresholdFn fn)
void free_trace(Trace *trace)
Trace * trace_bitmap(const TraceParams &params, Bitmap &bm)
ImBuf * bitmap_to_image(const Bitmap &bm)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
const float * float_data() const