Skip to content
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6db1632
unified img_to_svg function
Krasner Apr 29, 2026
e9e5b4a
update py bindings
Krasner Apr 29, 2026
7d203d1
run formatting
Krasner Apr 29, 2026
06e01a3
Merge branch 'main' into feat/img_to_svg
Krasner Apr 29, 2026
17b0b06
introduce img2num::ImageToSvgConfig with c and py bindings... js bind…
Krasner Apr 30, 2026
477e812
Merge branch 'feat/img_to_svg' of https://github.com/Krasner/Img2Num …
Krasner Apr 30, 2026
dd4bec2
update js bindings
Krasner Apr 30, 2026
35299a1
fix name
Krasner Apr 30, 2026
70a73c1
resolve merge conflicts
Krasner Apr 30, 2026
7e75ab1
remove debug prints
Krasner Apr 30, 2026
63ffecd
pybind fix and add repr to ImageToSvgConfig
Krasner Apr 30, 2026
771e0cf
formatting
Krasner Apr 30, 2026
b1230d4
add python badge
Krasner Apr 30, 2026
7f58edf
define transparent ImageToSvgConfig struct in c bindings
Krasner Apr 30, 2026
1802f5a
free res_svg2
Krasner Apr 30, 2026
dbce793
clean up. add arguments to pybind ImageToSvgConfig to allow for argum…
Krasner May 1, 2026
3108e75
minor updates
Krasner May 1, 2026
dd7be65
Merge branch 'main' into feat/img_to_svg
Krasner May 1, 2026
608f557
fixes
Krasner May 1, 2026
085ed54
rework c bindings. separate BilateralFilterConfig and KMeansConfig in…
Krasner May 3, 2026
90aa282
Merge branch 'main' into feat/img_to_svg
Krasner May 3, 2026
1161b53
revert to nested struct
Krasner May 3, 2026
54f3e71
Merge branch 'main' into feat/img_to_svg
Krasner May 3, 2026
c1e99ef
Very explicit c to cpp translation in c bindings
Krasner May 4, 2026
2ca495d
Merge branch 'feat/img_to_svg' of https://github.com/Krasner/Img2Num …
Krasner May 4, 2026
4fec31e
formatting
Krasner May 4, 2026
72b1ba0
address pr
Krasner May 4, 2026
972e335
Merge branch 'main' into feat/img_to_svg
Krasner May 4, 2026
51bc81b
support None config in pybindings - i.e. user calls image_to_svg with…
Krasner May 4, 2026
c676e58
Merge branch 'feat/img_to_svg' of https://github.com/Krasner/Img2Num …
Krasner May 4, 2026
491d0b0
image_to_svg config becomes kwarg
Krasner May 4, 2026
7356417
pybind use constructor arguments 'bilateral_filter' and 'kmeans' for …
Krasner May 5, 2026
f6236c7
revert WasmImageProcessor to run pipeline step by step
Krasner May 5, 2026
926f9f3
format
Krasner May 5, 2026
2680187
make wasm_wrapper a C file. Update bindings/js CMakeLists
Krasner May 5, 2026
2514e2d
small cleanup
Krasner May 5, 2026
dcceba0
add python typing hints
Krasner May 5, 2026
d7715d3
docs(core): document image_to_svg and its config struct
Ryan-Millard May 5, 2026
0161b91
docs(packages/js): improve JSDocs in safeWasmWrappers.js
Ryan-Millard May 5, 2026
b3674b0
docs(bindings/c): match core's docs
Ryan-Millard May 5, 2026
57aa0d3
fix(bindings/c): NULL tolerance via defaulting in image_to_svg
Ryan-Millard May 5, 2026
42aaeb3
build(bindings/js): switch to C18 from C11
Ryan-Millard May 5, 2026
8007a02
build(bindings/js): switch to C17 from C18 - blame Emscripten
Ryan-Millard May 5, 2026
5d7db6f
refactor(packages/py): move config defaults into Python wrapper from …
Ryan-Millard May 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Open in Codespaces](https://img.shields.io/badge/-Open%20in%20Codespaces-black?logo=github)](https://codespaces.new/Ryan-Millard/Img2Num)

<a href="https://skillicons.dev">
<img height="25px" src="https://skillicons.dev/icons?i=cpp,c,js" />
<img height="25px" src="https://skillicons.dev/icons?i=cpp,c,js,py" alt="C++, C, JavaScript, Python" />
</a>

_Img2Num_ converts raster images into clean SVGs with _high accuracy and performance_.
Expand Down
38 changes: 38 additions & 0 deletions bindings/c/include/cimg2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@
extern "C" {
#endif
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// @brief Configuration options for image_to_svg.
/// @ingroup CIMG2NUM_H
typedef struct img2num_ImageToSvgConfig {

/// Configuration settings for the bilateral filter in image_to_svg.
struct BilateralFilterConfig {
/// Standard deviation for spatial Gaussian (proximity weight).
/// Higher values smooth over larger spatial neighborhoods.
double sigma_spatial;
/// Standard deviation for range Gaussian (intensity similarity weight).
/// Higher values allow blending of more dissimilar pixel intensities.
double sigma_range;
} bilateral_filter;

/// Configuration settings for K-Means in image_to_svg.
struct KMeansConfig {
/// Number of clusters to compute in K-Means.
/// Roughly represents number of unique colors discovered.
int32_t k;
/// Maximum number of iterations for the K-Means algorithm.
/// The algorithm may terminate earlier if it converges.
int32_t max_iter;
} kmeans;

/// Minimum area (in pixels) for a region to be included in the SVG.
int min_cluster_area;

/// Color space flag.
/// - 0 = CIE LAB (more perceptually accurate)
/// - 1 = sRGB (faster).
uint8_t color_space;
} img2num_ImageToSvgConfig;

img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default(void);

/// @copydoc ::IMG2NUM_H_GAUSSIAN_BLUR_DOC
void img2num_gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma);

Expand Down Expand Up @@ -43,6 +78,9 @@ void img2num_bilateral_filter(uint8_t *image, size_t width, size_t height, doubl
char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const int width,
const int height, const int min_area);

/// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_DOC
Comment thread
Krasner marked this conversation as resolved.
Resolved
char *img2num_image_to_svg(const uint8_t *data, const int width, const int height,
const img2num_ImageToSvgConfig *config);
#ifdef __cplusplus
}
#endif
Expand Down
59 changes: 59 additions & 0 deletions bindings/c/src/cimg2num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@

extern "C" {

static img2num::ImageToSvgConfig to_cpp(const img2num_ImageToSvgConfig &c) {
return {
.bilateral_filter{
.sigma_spatial = c.bilateral_filter.sigma_spatial,
.sigma_range = c.bilateral_filter.sigma_range
},
.kmeans{
.k = c.kmeans.k,
.max_iter = c.kmeans.max_iter
},
.min_cluster_area = c.min_cluster_area,
.color_space = c.color_space
};
}

static img2num_ImageToSvgConfig to_c(const img2num::ImageToSvgConfig &cpp) {
return {
.bilateral_filter{
.sigma_spatial = cpp.bilateral_filter.sigma_spatial,
.sigma_range = cpp.bilateral_filter.sigma_range
},
.kmeans{
.k = cpp.kmeans.k,
.max_iter = cpp.kmeans.max_iter
},
.min_cluster_area = cpp.min_cluster_area,
.color_space = cpp.color_space
};
}
Comment thread
Krasner marked this conversation as resolved.
Resolved

img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default(void) {
// Use the defaults from the C++ struct
return to_c(img2num::ImageToSvgConfig{});
}

void img2num_gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma) {
img2num::clear_last_error_and_catch(img2num::gaussian_blur_fft, image, width, height, sigma);
}
Expand Down Expand Up @@ -55,4 +90,28 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in
data, labels, width, height, min_area);
return result;
}

char *img2num_image_to_svg(const uint8_t *data, const int width, const int height,
const img2num_ImageToSvgConfig *config) {
img2num_ImageToSvgConfig default_cfg;

const img2num_ImageToSvgConfig &cfg{config ? *config : default_cfg};

char *result{nullptr};

img2num::clear_last_error_and_catch(
[&](const uint8_t *d, const int w, const int h) {
std::string svg{ img2num::image_to_svg(d, w, h, to_cpp(cfg)) };

result = static_cast<char *>(std::malloc(svg.size() + 1));
if (!result) {
return; // Allocation failed
}
std::memcpy(result, svg.c_str(), svg.size() + 1);
},
data, width, height
);

return result;
}
}
8 changes: 4 additions & 4 deletions bindings/js/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cmake_minimum_required(VERSION 3.16)
project(JSBindings LANGUAGES CXX)
project(JSBindings LANGUAGES C)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 18)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(NOT EMSCRIPTEN)
message(FATAL_ERROR "Emscripten is required to build JS bindings.")
endif()

file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")

add_executable(img2num_wasm ${BINDING_SRC})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <cimg2num.h>
#include <emscripten/emscripten.h>

extern "C" {
EMSCRIPTEN_KEEPALIVE void gaussian_blur_fft(uint8_t *image, size_t width, size_t height,
double sigma) {
img2num_gaussian_blur_fft(image, width, height, sigma);
Expand Down Expand Up @@ -37,4 +36,19 @@ EMSCRIPTEN_KEEPALIVE char *labels_to_svg(uint8_t *data, int32_t *labels, const i
const int height, const int min_area) {
return img2num_labels_to_svg(data, labels, width, height, min_area);
}

EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, const int height,
double sigma_spatial, double sigma_range, const int32_t k,
const int32_t max_iter, const int min_area,
const uint8_t color_space) {
img2num_ImageToSvgConfig config = img2num_ImageToSvgConfig_default();

config.bilateral_filter.sigma_spatial = sigma_spatial;
config.bilateral_filter.sigma_range = sigma_range;
config.kmeans.k = k;
config.kmeans.max_iter = max_iter;
config.min_cluster_area = min_area;
config.color_space = color_space;

return img2num_image_to_svg(data, width, height, &config);
}
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
99 changes: 92 additions & 7 deletions bindings/py/src/img2num_pybind.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include <img2num.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <cstdlib>
#include <memory>
Comment thread
Krasner marked this conversation as resolved.
Resolved
#include <sstream>
#include <optional>

PYBIND11_MODULE(_img2num, m) {
m.doc() = "Python bindings for the img2num C library";
m.doc() = "Python bindings for the img2num C++ library";

// -----------------------------------------------------------------------
// All Functions return new image data
Expand Down Expand Up @@ -91,12 +95,12 @@ PYBIND11_MODULE(_img2num, m) {
auto out_data = pybind11::array_t<uint8_t>(data_buf.shape);
auto out_labels = pybind11::array_t<int32_t>({(ssize_t)height, (ssize_t)width});

auto out_data_ptr = static_cast<uint8_t*>(out_data.mutable_data());
auto out_labels_ptr = static_cast<int32_t*>(out_labels.mutable_data());
auto out_data_ptr = static_cast<uint8_t *>(out_data.mutable_data());
auto out_labels_ptr = static_cast<int32_t *>(out_labels.mutable_data());

// Call C function
img2num::kmeans(static_cast<const uint8_t*>(data_buf.ptr), out_data_ptr, out_labels_ptr,
width, height, k, max_iter, color_space);
img2num::kmeans(static_cast<const uint8_t *>(data_buf.ptr), out_data_ptr,
out_labels_ptr, width, height, k, max_iter, color_space);

// Return a tuple of (out_data, out_labels)
return pybind11::make_tuple(out_data, out_labels);
Expand All @@ -110,8 +114,8 @@ PYBIND11_MODULE(_img2num, m) {
[](pybind11::array_t<uint8_t, pybind11::array::c_style> data,
pybind11::array_t<int32_t, pybind11::array::c_style> labels, int width, int height,
int min_area) {
const uint8_t* data_ptr{static_cast<const uint8_t*>(data.request().ptr)};
const int32_t* labels_ptr{static_cast<const int32_t*>(labels.request().ptr)};
const uint8_t *data_ptr{static_cast<const uint8_t *>(data.request().ptr)};
const int32_t *labels_ptr{static_cast<const int32_t *>(labels.request().ptr)};

std::string svg{img2num::labels_to_svg(data_ptr, labels_ptr, width, height, min_area)};
pybind11::str svg_py_str(std::move(svg));
Expand All @@ -120,4 +124,85 @@ PYBIND11_MODULE(_img2num, m) {
},
pybind11::arg("data"), pybind11::arg("labels"), pybind11::arg("width"),
pybind11::arg("height"), pybind11::arg("min_area"), "Convert labels to SVG string");

// ---------------------- Config Structs ----------------------
pybind11::class_<img2num::ImageToSvgConfig> config(m, "ImageToSvgConfig");
pybind11::class_<img2num::ImageToSvgConfig::BilateralFilterConfig>(config,
"BilateralFilterConfig")
.def(pybind11::init<>())
.def_readwrite("sigma_spatial",
&img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial)
.def_readwrite("sigma_range",
&img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range)
.def("__repr__", [](const img2num::ImageToSvgConfig::BilateralFilterConfig &c) {
return "{'sigma_spatial': " + std::to_string(c.sigma_spatial) +
", 'sigma_range': " + std::to_string(c.sigma_range) + "}";
});
pybind11::class_<img2num::ImageToSvgConfig::KMeansConfig>(config, "KMeansConfig")
.def(pybind11::init<>())
.def_readwrite("k", &img2num::ImageToSvgConfig::KMeansConfig::k)
.def_readwrite("max_iter", &img2num::ImageToSvgConfig::KMeansConfig::max_iter)
.def("__repr__", [](const img2num::ImageToSvgConfig::KMeansConfig &c) {
return "{'k': " + std::to_string(c.k) + ", 'max_iter': " + std::to_string(c.max_iter) +
"}";
});

config
.def(pybind11::init([](pybind11::dict bf_dict, pybind11::dict km_dict,
pybind11::kwargs kwargs) {
// hand over ownership to python
std::unique_ptr<img2num::ImageToSvgConfig> c =
std::make_unique<img2num::ImageToSvgConfig>();
if (bf_dict.contains("sigma_spatial"))
c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast<double>();
if (bf_dict.contains("sigma_range"))
c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast<double>();

// 3. Process KMeans overrides from the 'km' dictionary
if (km_dict.contains("k")) c->kmeans.k = km_dict["k"].cast<int>();
if (km_dict.contains("max_iter"))
c->kmeans.max_iter = km_dict["max_iter"].cast<int>();

// 4. Process remaining top-level kwargs (like color_space or min_cluster_area)
if (kwargs.contains("min_cluster_area"))
c->min_cluster_area = kwargs["min_cluster_area"].cast<int>();
if (kwargs.contains("color_space"))
c->color_space = kwargs["color_space"].cast<uint8_t>();
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved

return c;
}),
pybind11::arg("bilateral_filter") = pybind11::dict(), // Defaults to empty dict
pybind11::arg("kmeans") = pybind11::dict() // Defaults to empty dict
)
.def_readwrite("bilateral_filter", &img2num::ImageToSvgConfig::bilateral_filter)
.def_readwrite("min_cluster_area", &img2num::ImageToSvgConfig::min_cluster_area)
.def_readwrite("color_space", &img2num::ImageToSvgConfig::color_space)
.def_readwrite("kmeans", &img2num::ImageToSvgConfig::kmeans)
.def("__repr__", [](const img2num::ImageToSvgConfig &c) {
// We use pybind11::repr() to trigger the __repr__ of the nested objects
std::stringstream ss;
ss << "<ImageToSvgConfig {"
<< "bilateral_filter: "
<< pybind11::repr(pybind11::cast(c.bilateral_filter)).cast<std::string>() << ", "
<< "min_cluster_area: " << c.min_cluster_area << ", "
<< "color_space: " << (int)c.color_space << ", "
<< "kmeans: " << pybind11::repr(pybind11::cast(c.kmeans)).cast<std::string>()
<< "}>";
return ss.str();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

m.def(
"image_to_svg",
[](pybind11::array_t<uint8_t, pybind11::array::c_style> data, int width, int height,
std::optional<const img2num::ImageToSvgConfig> cfg) {
const uint8_t *data_ptr{static_cast<const uint8_t *>(data.request().ptr)};
const auto& _cfg = cfg.has_value() ? *cfg : img2num::ImageToSvgConfig{};

std::string svg{img2num::image_to_svg(data_ptr, width, height, _cfg)};
pybind11::str svg_py_str(std::move(svg));
return svg_py_str;
},
pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("cfg") = pybind11::none(),
"Convert Image to SVG string");
}
38 changes: 38 additions & 0 deletions core/include/img2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@
/// @note All image buffers are assumed to be stored in row-major order, unless otherwise noted.
namespace img2num {

/// @brief Configuration options for image_to_svg.
/// @ingroup IMG2NUM_H
struct ImageToSvgConfig {

/// Configuration settings for the bilateral filter in image_to_svg.
struct BilateralFilterConfig {
/// Standard deviation for spatial Gaussian (proximity weight).
/// Higher values smooth over larger spatial neighborhoods.
double sigma_spatial = 3.0;
/// Standard deviation for range Gaussian (intensity similarity weight).
/// Higher values allow blending of more dissimilar pixel intensities.
double sigma_range = 50.0;
} bilateral_filter;

/// Configuration settings for K-Means in image_to_svg.
struct KMeansConfig {
/// Number of clusters to compute in K-Means.
/// Roughly represents number of unique colors discovered.
int32_t k = 16;
/// Maximum number of iterations for the K-Means algorithm.
/// The algorithm may terminate earlier if it converges.
int32_t max_iter = 100;
} kmeans;

/// Minimum area (in pixels) for a region to be included in the SVG.
int min_cluster_area = 100;

/// Color space flag.
/// - 0 = CIE LAB (more perceptually accurate)
/// - 1 = sRGB (faster).
uint8_t color_space = 0;
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved

/// @copydoc IMG2NUM_H_GAUSSIAN_BLUR_DOC
void gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma);

Expand All @@ -40,6 +73,11 @@ void bilateral_filter(uint8_t *image, size_t width, size_t height, double sigma_
/// @copydoc IMG2NUM_H_LABELS_TO_SVG_DOC
std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int width,
const int height, const int min_area);

/// @copydoc IMG2NUM_H_IMAGE_TO_SVG_DOC
Comment thread
Krasner marked this conversation as resolved.
Resolved
std::string image_to_svg(const uint8_t *data, const int width, const int height,
const ImageToSvgConfig &config);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

} // namespace img2num

#endif // IMG2NUM_H
1 change: 1 addition & 0 deletions core/include/internal/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class GPU {
adapter = nullptr;
queue = nullptr;
instance = nullptr;
gpu_initialized = false;
};
};

Expand Down
24 changes: 24 additions & 0 deletions core/src/internal/image_to_svg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <cstring>
#include <vector>

#include "img2num.h"

namespace img2num {
std::string image_to_svg(const uint8_t* data, const int width, const int height,
const ImageToSvgConfig& config) {
// self deallocate
std::vector<uint8_t> img_data(static_cast<size_t>(width) * static_cast<size_t>(height) * 4);
std::vector<uint8_t> out_data(static_cast<size_t>(width) * static_cast<size_t>(height) * 4);
std::vector<int32_t> out_labels(static_cast<size_t>(width) * static_cast<size_t>(height));

std::memcpy(img_data.data(), data,
static_cast<size_t>(width) * static_cast<size_t>(height) * 4);
bilateral_filter(img_data.data(), width, height, config.bilateral_filter.sigma_spatial,
config.bilateral_filter.sigma_range, config.color_space);
kmeans(img_data.data(), out_data.data(), out_labels.data(), width, height, config.kmeans.k,
config.kmeans.max_iter, config.color_space);
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
std::string svg{labels_to_svg(data, out_labels.data(), width, height, config.min_cluster_area)};

return svg;
}
} // namespace img2num
Loading
Loading