Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions bindings/c/include/cimg2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ 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,
double sigma_spatial, double sigma_range, const int32_t k,
const int32_t max_iter, const int min_area, const uint8_t color_space);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 20 additions & 2 deletions bindings/c/src/cimg2num.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "cimg2num.h"

#include <cstring>

#include "img2num.h"
#include "img2num/Error.h"

#include <cstring>

extern "C" {

void img2num_gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma) {
Expand Down Expand Up @@ -55,4 +55,22 @@ 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,
double sigma_spatial, double sigma_range, const int32_t k,
const int32_t max_iter, const int min_area, const uint8_t color_space) {
char *result{nullptr};
img2num::clear_last_error_and_catch(
[&](const uint8_t *d, const int w, const int h, double ss, double sr, const int32_t k_,
const int32_t iter, const int min_a, const uint8_t cs) {
std::string svg{img2num::image_to_svg(d, w, h, ss, sr, k_, iter, min_a, cs)};
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, sigma_spatial, sigma_range, k, max_iter, min_area, color_space);
return result;
};
}
8 changes: 8 additions & 0 deletions bindings/js/src/wasm_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ 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) {
return img2num_image_to_svg(data, width, height, sigma_spatial, sigma_range, k, max_iter,
min_area, color_space);
}
}
60 changes: 39 additions & 21 deletions bindings/py/src/img2num_pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cstdlib>

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 All @@ -22,8 +22,8 @@ PYBIND11_MODULE(_img2num, m) {
img2num::gaussian_blur_fft(out_image.mutable_data(), width, height, sigma);
return out_image;
},
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("sigma"),
"Apply Gaussian blur using FFT");
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("sigma"), "Apply Gaussian blur using FFT");

m.def(
"invert_image",
Expand All @@ -48,8 +48,8 @@ PYBIND11_MODULE(_img2num, m) {
img2num::threshold_image(out_image.mutable_data(), width, height, num_thresholds);
return out_image;
},
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("num_thresholds"),
"Apply thresholding to the image");
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("num_thresholds"), "Apply thresholding to the image");

m.def(
"black_threshold_image",
Expand All @@ -62,8 +62,8 @@ PYBIND11_MODULE(_img2num, m) {
img2num::black_threshold_image(out_image.mutable_data(), width, height, num_thresholds);
return out_image;
},
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("num_thresholds"),
"Apply black thresholding to the image");
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("num_thresholds"), "Apply black thresholding to the image");

m.def(
"bilateral_filter",
Expand All @@ -74,17 +74,17 @@ PYBIND11_MODULE(_img2num, m) {
std::memcpy(out_image.mutable_data(), buf.ptr, buf.size * sizeof(uint8_t));

img2num::bilateral_filter(out_image.mutable_data(), width, height, sigma_spatial,
sigma_range, color_space);
sigma_range, color_space);
return out_image;
},
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("sigma_spatial"),
pybind11::arg("sigma_range"), pybind11::arg("color_space"),
pybind11::arg("image"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("sigma_spatial"), pybind11::arg("sigma_range"), pybind11::arg("color_space"),
"Apply bilateral filter");

m.def(
"kmeans",
[](pybind11::array_t<uint8_t, pybind11::array::c_style> data, int32_t width, int32_t height, int32_t k,
int32_t max_iter, uint8_t color_space) {
[](pybind11::array_t<uint8_t, pybind11::array::c_style> data, int32_t width, int32_t height,
int32_t k, int32_t max_iter, uint8_t color_space) {
pybind11::buffer_info data_buf = data.request();

// Allocate NumPy arrays for the outputs
Expand All @@ -96,28 +96,46 @@ PYBIND11_MODULE(_img2num, m) {

// 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);
width, height, k, max_iter, color_space);

// Return a tuple of (out_data, out_labels)
return pybind11::make_tuple(out_data, out_labels);
},
pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("k"), pybind11::arg("max_iter"),
pybind11::arg("color_space"),
pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("k"),
pybind11::arg("max_iter"), pybind11::arg("color_space"),
"Run K-Means clustering. Returns a tuple: (quantized_image, labels_array)");

m.def(
"labels_to_svg",
[](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) {

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)};

std::string svg{img2num::labels_to_svg(data_ptr, labels_ptr, width, height, min_area)};
pybind11::str svg_py_str(std::move(svg));


return svg_py_str;
},
pybind11::arg("data"), pybind11::arg("labels"), pybind11::arg("width"),
pybind11::arg("height"), pybind11::arg("min_area"), "Convert labels to SVG string");

m.def(
"image_to_svg",
[](pybind11::array_t<uint8_t, pybind11::array::c_style> data, int width, int height,
double sigma_spatial, double sigma_range, int32_t k, int32_t max_iter, int min_area,
uint8_t color_space) {
const uint8_t* data_ptr{static_cast<const uint8_t*>(data.request().ptr)};

std::string svg{img2num::image_to_svg(data_ptr, width, height, sigma_spatial,
sigma_range, k, max_iter, min_area, color_space)};
pybind11::str svg_py_str(std::move(svg));

return svg_py_str;
},
pybind11::arg("data"), pybind11::arg("labels"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("min_area"), "Convert labels to SVG string");
pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"),
pybind11::arg("sigma_spatial"), pybind11::arg("sigma_range"), pybind11::arg("k"),
pybind11::arg("max_iter"), pybind11::arg("color_space"), pybind11::arg("min_area"),
"Convert Image to SVG string");
}
10 changes: 8 additions & 2 deletions core/include/img2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ void bilateral_filter(uint8_t *image, size_t width, size_t height, double sigma_
double sigma_range, uint8_t color_space);

/// @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);
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,
double sigma_spatial, double sigma_range, const int32_t k,
const int32_t max_iter, const int min_area, const uint8_t color_space);
Comment thread
Ryan-Millard 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
3 changes: 3 additions & 0 deletions core/src/internal/bilateral_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <vector>

#include "img2num.h"
Expand Down Expand Up @@ -224,8 +225,10 @@ void bilateral_filter(uint8_t *image, size_t width, size_t height, double sigma_
GPU::getClassInstance().init_gpu();

if (GPU::getClassInstance().is_initialized()) {
std::cout << "bilateral_filter gpu" << std::endl;
bilateral_filter_gpu(image, width, height, sigma_spatial, sigma_range, color_space);
} else {
std::cout << "bilateral_filter cpu" << std::endl;
bilateral_filter_cpu(image, width, height, sigma_spatial, sigma_range, color_space);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/internal/bilateral_filter_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void bilateral_filter_gpu(uint8_t* image, size_t width, size_t height, double si
readBuffer.Destroy();
delete waiting;
#if defined(__EMSCRIPTEN__)
emscripten_sleep(50);
emscripten_sleep(100);
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
#endif
result.clear();
result.shrink_to_fit();
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,
double sigma_spatial, double sigma_range, const int32_t k,
const int32_t max_iter, const int min_area, const uint8_t color_space) {
// self deallocate
std::vector<uint8_t> img_data(width * height * 4);
std::vector<uint8_t> out_data(width * height * 4);
std::vector<int32_t> out_labels(width * 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, sigma_spatial, sigma_range, color_space);
kmeans(img_data.data(), out_data.data(), out_labels.data(), width, height, k, max_iter,
color_space);
std::string svg{labels_to_svg(data, out_labels.data(), width, height, min_area)};

return svg;
}
} // namespace img2num
2 changes: 1 addition & 1 deletion core/src/internal/kmeans_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,6 @@ void kmeans_gpu(const uint8_t* data, uint8_t* out_data, int32_t* out_labels, con
labels.clear();
labels.shrink_to_fit();
#if defined(__EMSCRIPTEN__)
emscripten_sleep(50);
emscripten_sleep(100);
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
#endif
}
4 changes: 2 additions & 2 deletions core/src/internal/labels_to_svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ format ([r,g,b,a, r,g,b,a, ...]) labels: int32_t* -> output of labelled regions
from K-Means, should be 1/4 the size of data since data is RGBA labels : width *
height : number of pixels in image = 1 : 1 : 1
*/
std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int width, const int height,
const int min_area) {
std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int width,
const int height, const int min_area) {
const int32_t num_pixels{width * height};
std::vector<int32_t> labels_vector{labels, labels + num_pixels};
std::vector<int32_t> region_labels;
Expand Down
4 changes: 3 additions & 1 deletion example-apps/console-cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ int main(int argc, char** argv) {
// Generate SVG
std::string res_svg{img2num::labels_to_svg(img_data, out_labels, width, height, 100)};

std::string res_svg2{img2num::image_to_svg(img_data, width, height, sigma, 50.0, 32, 100, 100, 0)};

// Save the blurred image
std::string out_path{std::string(OUT_DIR) + "/console-cpp-output.png"};
std::string kmeans_path{std::string(OUT_DIR) + "/console-cpp-kmeans.png"};
Expand All @@ -79,7 +81,7 @@ int main(int argc, char** argv) {
exit_code = 1;
}
if (exit_code == 0) {
svgFile << res_svg;
svgFile << res_svg2;
svgFile.close();
}
Comment thread
Krasner marked this conversation as resolved.
Resolved

Expand Down
5 changes: 5 additions & 0 deletions example-apps/console-py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ def main():
with open(os.path.join(OUTDIR, "result.svg"),"w") as f:
f.writelines(res_svg)

# res_svg2 should match res_svg
res_svg2 = img2num.image_to_svg(img, 3, 50, 16, 100, 100, 0)
with open(os.path.join(OUTDIR, "result2.svg"),"w") as f:
f.writelines(res_svg2)

Comment thread
Krasner marked this conversation as resolved.
Resolved
if __name__ == "__main__":
main()
13 changes: 10 additions & 3 deletions example-apps/react-js/src/components/WasmImageProcessor.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState, useId, useRef, useCallback, useMemo } from "react";
import { Upload } from "lucide-react";
import { imageToUint8ClampedArray, bilateralFilter, kmeans, findContours } from "img2num";
import { imageToUint8ClampedArray, bilateralFilter, kmeans, findContours, imgToSVG } from "img2num";
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
import GlassCard from "@components/GlassCard";
import styles from "./WasmImageProcessor.module.css";
import { useNavigate } from "react-router-dom";
Expand Down Expand Up @@ -74,7 +74,14 @@ const WasmImageProcessor = () => {
try {
const { width, height } = fileData;

step(20);
const { svg } = await imgToSVG({
pixels: fileData.pixels,
width,
height,
num_colors: 16,
});

/*step(20);
// NOTE: Gaussian blur destroys the sharp outlines first, preventing the Bilateral filter from detecting and preserving them
const imgBilateralFiltered = await bilateralFilter({
pixels: fileData.pixels,
Expand All @@ -96,7 +103,7 @@ const WasmImageProcessor = () => {
labels,
width,
height,
});
});*/

navigate("/editor", {
state: { svg },
Expand Down
35 changes: 35 additions & 0 deletions packages/js/safeWasmWrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,38 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1
});
return { svg: result.returnValue, visualization: result.output.pixels };
};

/**
* @summary Convert images to SVG contours.
*
* @description
* Convert an input image into an SVG. A unification of bilateralFilter, kmeans, and findContours
Comment thread
Krasner marked this conversation as resolved.
Resolved
*
* @async
* @function imgToSVG
* @param {Object} __named_parameters - The input options.
* @property {Uint8ClampedArray} __named_parameters.pixels - Original image pixels.
* @property {number} __named_parameters.width - Image width.
* @property {number} __named_parameters.height - Image height.
* @property {number} [__named_parameters.sigma_spatial=3] - Spatial standard deviation.
* @property {number} [__named_parameters.sigma_range=50] - Range (color) standard deviation.
* @property {number} [__named_parameters.num_colors=16] - Number of color clusters.
* @property {number} [__named_parameters.max_iter=100] - Maximum number of iterations.
* @property {number} [__named_parameters.min_area=100] - Minimum area of a region to be considered a contour.
* @property {number} [__named_parameters.color_space=0] - Color space mode.* @returns {Promise<{svg: string, visualization: Uint8ClampedArray}>} Generated SVG and optionally pixels with visualized contours.
* @throws {Error} If the WASM function fails or input labels are invalid.
* @example
* const { svg, visualization } = await findContours({ pixels, labels, width, height });
* @todo Support additional visualization options like color-coded regions.
* @variation Contour extraction with optional visualization
* @since 0.0.0
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
*/
export const imgToSVG = async ({ pixels, width, height, sigma_spatial = 3, sigma_pixels = 50, num_colors = 16, max_iter = 100, min_area = 100, color_space = 0 }) => {
const result = await callWasm({
funcName: "image_to_svg",
args: { pixels, width, height, sigma_spatial, sigma_pixels, num_colors, max_iter, min_area, color_space },
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }],
returnType: "string",
});
return { svg: result.returnValue, visualization: result.output.pixels };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
};
Comment thread
Ryan-Millard marked this conversation as resolved.
Resolved
5 changes: 5 additions & 0 deletions packages/py/img2num/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
bilateral_filter as _bilateral_filter,
kmeans as _kmeans,
labels_to_svg as _labels_to_svg,
image_to_svg as _image_to_svg,
)

def _inject_dims(image_arg="image"):
Expand Down Expand Up @@ -59,3 +60,7 @@ def kmeans(data, k, max_iter, color_space, *, width, height):
@_inject_dims("data")
def labels_to_svg(data, labels, min_area, *, width, height):
return _labels_to_svg(data, labels, width, height, min_area)

@_inject_dims("image")
def image_to_svg(image, sigma_spatial, sigma_range, k, max_iter, min_area, color_space, *, width, height):
return _image_to_svg(image, width, height, sigma_spatial, sigma_range, k, max_iter, min_area, color_space)
Loading
Loading