From 6db16325d1bd64b5e878ed3965af660ab547a44a Mon Sep 17 00:00:00 2001 From: Krasner Date: Wed, 29 Apr 2026 15:24:18 +0000 Subject: [PATCH 01/35] unified img_to_svg function --- bindings/c/include/cimg2num.h | 13 +++++++ bindings/c/src/cimg2num.cpp | 35 ++++++++++++++++++ bindings/js/src/wasm_wrapper.cpp | 14 +++++++ core/include/img2num.h | 14 +++++++ core/include/internal/gpu.h | 1 + core/src/internal/bilateral_filter.cpp | 3 ++ core/src/internal/bilateral_filter_gpu.cpp | 2 +- core/src/internal/image_to_svg.cpp | 29 +++++++++++++++ core/src/internal/kmeans_gpu.cpp | 2 +- example-apps/console-cpp/main.cpp | 4 +- .../src/components/WasmImageProcessor.jsx | 13 +++++-- packages/js/safeWasmWrappers.js | 37 +++++++++++++++++++ 12 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 core/src/internal/image_to_svg.cpp diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index bb6d32587..023f3c115 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -43,6 +43,19 @@ 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 +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 diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index fa38cf58c..ff4a7e3db 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -55,4 +55,39 @@ 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(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; +}; } diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index 3e3cf2726..a8f0e1203 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -37,4 +37,18 @@ 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); +} } diff --git a/core/include/img2num.h b/core/include/img2num.h index 9290351de..5832eb65d 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -40,6 +40,20 @@ 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 +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 +); + } // namespace img2num #endif // IMG2NUM_H diff --git a/core/include/internal/gpu.h b/core/include/internal/gpu.h index 5b1e2ae75..19e7e7eda 100644 --- a/core/include/internal/gpu.h +++ b/core/include/internal/gpu.h @@ -257,6 +257,7 @@ class GPU { adapter = nullptr; queue = nullptr; instance = nullptr; + gpu_initialized = false; }; }; diff --git a/core/src/internal/bilateral_filter.cpp b/core/src/internal/bilateral_filter.cpp index 03600e4d1..2c8fe50d9 100644 --- a/core/src/internal/bilateral_filter.cpp +++ b/core/src/internal/bilateral_filter.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "img2num.h" #include "internal/bilateral_filter_gpu.h" @@ -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); } } diff --git a/core/src/internal/bilateral_filter_gpu.cpp b/core/src/internal/bilateral_filter_gpu.cpp index 4fe41bf58..1239c2df5 100644 --- a/core/src/internal/bilateral_filter_gpu.cpp +++ b/core/src/internal/bilateral_filter_gpu.cpp @@ -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); #endif result.clear(); result.shrink_to_fit(); diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp new file mode 100644 index 000000000..23c73aa35 --- /dev/null +++ b/core/src/internal/image_to_svg.cpp @@ -0,0 +1,29 @@ +#include "img2num.h" +#include +#include + +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 img_data(width * height * 4); + std::vector out_data(width * height * 4); + std::vector out_labels(width * height); + + std::memcpy(img_data.data(), data, static_cast(width) * static_cast(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 \ No newline at end of file diff --git a/core/src/internal/kmeans_gpu.cpp b/core/src/internal/kmeans_gpu.cpp index 7ff81f084..d947ff2d7 100644 --- a/core/src/internal/kmeans_gpu.cpp +++ b/core/src/internal/kmeans_gpu.cpp @@ -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); #endif } diff --git a/example-apps/console-cpp/main.cpp b/example-apps/console-cpp/main.cpp index 01a229a78..b96142a37 100644 --- a/example-apps/console-cpp/main.cpp +++ b/example-apps/console-cpp/main.cpp @@ -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"}; @@ -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(); } diff --git a/example-apps/react-js/src/components/WasmImageProcessor.jsx b/example-apps/react-js/src/components/WasmImageProcessor.jsx index 67c2c7b01..0d0220b27 100644 --- a/example-apps/react-js/src/components/WasmImageProcessor.jsx +++ b/example-apps/react-js/src/components/WasmImageProcessor.jsx @@ -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"; import GlassCard from "@components/GlassCard"; import styles from "./WasmImageProcessor.module.css"; import { useNavigate } from "react-router-dom"; @@ -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, @@ -96,7 +103,7 @@ const WasmImageProcessor = () => { labels, width, height, - }); + });*/ navigate("/editor", { state: { svg }, diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index 78d4a3dd7..9290f13f1 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -204,3 +204,40 @@ 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 + * + * @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 + */ +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 }, + bufferKeys: [ + { key: "pixels", type: "Uint8ClampedArray" }, + ], + returnType: "string", + }); + return { svg: result.returnValue, visualization: result.output.pixels }; +}; From e9e5b4a747286ae278f491c54040b9f42552b3c3 Mon Sep 17 00:00:00 2001 From: Krasner Date: Wed, 29 Apr 2026 15:48:15 +0000 Subject: [PATCH 02/35] update py bindings --- bindings/py/src/img2num_pybind.cpp | 20 +++++++++++++++++++- example-apps/console-py/main.py | 5 +++++ packages/py/img2num/api.py | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 1fb553938..9058e5406 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -5,7 +5,7 @@ #include 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 @@ -120,4 +120,22 @@ 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"); + + m.def( + "image_to_svg", + [](pybind11::array_t 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(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("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"); } diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index 94cb07334..97590ada2 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -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) + if __name__ == "__main__": main() diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 2f4729b94..4ce7846be 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -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"): @@ -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) From 7d203d1606d15f554d60fba474d6217d6f114006 Mon Sep 17 00:00:00 2001 From: Krasner Date: Wed, 29 Apr 2026 15:55:07 +0000 Subject: [PATCH 03/35] run formatting --- bindings/c/include/cimg2num.h | 14 +--- bindings/c/src/cimg2num.cpp | 31 ++------ bindings/js/src/wasm_wrapper.cpp | 18 ++--- bindings/py/src/img2num_pybind.cpp | 60 ++++++++-------- core/include/img2num.h | 18 ++--- core/src/internal/bilateral_filter.cpp | 2 +- core/src/internal/image_to_svg.cpp | 27 +++---- core/src/internal/labels_to_svg.cpp | 4 +- packages/js/safeWasmWrappers.js | 4 +- uv.lock | 98 +------------------------- 10 files changed, 69 insertions(+), 207 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 023f3c115..008b35fe5 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -44,17 +44,9 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in const int height, const int min_area); /// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_DOC -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 *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 } diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index ff4a7e3db..55c810d8f 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -1,10 +1,10 @@ #include "cimg2num.h" +#include + #include "img2num.h" #include "img2num/Error.h" -#include - extern "C" { void img2num_gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma) { @@ -56,30 +56,13 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in 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 *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 - ) { + [&](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(std::malloc(svg.size() + 1)); if (!result) { diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index a8f0e1203..c62f7357f 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -38,17 +38,11 @@ EMSCRIPTEN_KEEPALIVE char *labels_to_svg(uint8_t *data, int32_t *labels, const i 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); +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); } } diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 9058e5406..a759804f1 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -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", @@ -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", @@ -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", @@ -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 data, int32_t width, int32_t height, int32_t k, - int32_t max_iter, uint8_t color_space) { + [](pybind11::array_t 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 @@ -96,46 +96,46 @@ PYBIND11_MODULE(_img2num, m) { // Call C function img2num::kmeans(static_cast(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 data, - pybind11::array_t labels, int width, int height, int min_area) { - + pybind11::array_t labels, int width, int height, + int min_area) { const uint8_t* data_ptr{static_cast(data.request().ptr)}; const int32_t* labels_ptr{static_cast(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"); + 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 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) { - + [](pybind11::array_t 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(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)}; + + 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("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"); + 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"); } diff --git a/core/include/img2num.h b/core/include/img2num.h index 5832eb65d..96becdb79 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -38,21 +38,13 @@ 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 -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 -); +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); } // namespace img2num diff --git a/core/src/internal/bilateral_filter.cpp b/core/src/internal/bilateral_filter.cpp index 2c8fe50d9..f8701200e 100644 --- a/core/src/internal/bilateral_filter.cpp +++ b/core/src/internal/bilateral_filter.cpp @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include "img2num.h" #include "internal/bilateral_filter_gpu.h" diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp index 23c73aa35..12dc2be35 100644 --- a/core/src/internal/image_to_svg.cpp +++ b/core/src/internal/image_to_svg.cpp @@ -1,29 +1,24 @@ -#include "img2num.h" #include #include +#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 -){ +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 img_data(width * height * 4); std::vector out_data(width * height * 4); std::vector out_labels(width * height); - - std::memcpy(img_data.data(), data, static_cast(width) * static_cast(height) * 4); + + std::memcpy(img_data.data(), data, + static_cast(width) * static_cast(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); + 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 \ No newline at end of file +} // namespace img2num \ No newline at end of file diff --git a/core/src/internal/labels_to_svg.cpp b/core/src/internal/labels_to_svg.cpp index 66d0d0093..719e57887 100644 --- a/core/src/internal/labels_to_svg.cpp +++ b/core/src/internal/labels_to_svg.cpp @@ -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 labels_vector{labels, labels + num_pixels}; std::vector region_labels; diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index 9290f13f1..092590d6c 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -234,9 +234,7 @@ export const imgToSVG = async ({ pixels, width, height, sigma_spatial = 3, sigma const result = await callWasm({ funcName: "image_to_svg", args: { pixels, width, height, sigma_spatial, sigma_pixels, num_colors, max_iter, min_area, color_space }, - bufferKeys: [ - { key: "pixels", type: "Uint8ClampedArray" }, - ], + bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }], returnType: "string", }); return { svg: result.returnValue, visualization: result.output.pixels }; diff --git a/uv.lock b/uv.lock index 0d17ec598..b738142d0 100644 --- a/uv.lock +++ b/uv.lock @@ -1,9 +1,5 @@ version = 1 requires-python = ">=3.10" -resolution-markers = [ - "python_full_version >= '3.11'", - "python_full_version < '3.11'", -] [manifest] members = [ @@ -17,8 +13,7 @@ name = "console-py" version = "0.0.0" source = { editable = "example-apps/console-py" } dependencies = [ - { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, - { name = "numpy", version = "2.4.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, + { name = "numpy" }, { name = "opencv-python" }, ] @@ -33,8 +28,7 @@ name = "img2num" version = "0.0.0" source = { editable = "packages/py" } dependencies = [ - { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, - { name = "numpy", version = "2.4.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, + { name = "numpy" }, ] [package.metadata] @@ -55,9 +49,6 @@ requires-dist = [{ name = "img2num", editable = "packages/py" }] name = "numpy" version = "2.2.6" source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version < '3.11'", -] sdist = { url = "https://files.pythonhosted.org/packages/76/21/7d2a95e4bba9dc13d043ee156a356c0a8f0c6309dff6b21b4d71a073b8a8/numpy-2.2.6.tar.gz", hash = "sha256:e29554e2bef54a90aa5cc07da6ce955accb83f21ab5de01a62c8478897b264fd", size = 20276440 } wheels = [ { url = "https://files.pythonhosted.org/packages/9a/3e/ed6db5be21ce87955c0cbd3009f2803f59fa08df21b5df06862e2d8e2bdd/numpy-2.2.6-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:b412caa66f72040e6d268491a59f2c43bf03eb6c96dd8f0307829feb7fa2b6fb", size = 21165245 }, @@ -116,95 +107,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/37/48/ac2a9584402fb6c0cd5b5d1a91dcf176b15760130dd386bbafdbfe3640bf/numpy-2.2.6-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:d042d24c90c41b54fd506da306759e06e568864df8ec17ccc17e9e884634fd00", size = 12812666 }, ] -[[package]] -name = "numpy" -version = "2.4.4" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version >= '3.11'", -] -sdist = { url = "https://files.pythonhosted.org/packages/d7/9f/b8cef5bffa569759033adda9481211426f12f53299629b410340795c2514/numpy-2.4.4.tar.gz", hash = "sha256:2d390634c5182175533585cc89f3608a4682ccb173cc9bb940b2881c8d6f8fa0", size = 20731587 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/ef/c6/4218570d8c8ecc9704b5157a3348e486e84ef4be0ed3e38218ab473c83d2/numpy-2.4.4-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:f983334aea213c99992053ede6168500e5f086ce74fbc4acc3f2b00f5762e9db", size = 16976799 }, - { url = "https://files.pythonhosted.org/packages/dd/92/b4d922c4a5f5dab9ed44e6153908a5c665b71acf183a83b93b690996e39b/numpy-2.4.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:72944b19f2324114e9dc86a159787333b77874143efcf89a5167ef83cfee8af0", size = 14971552 }, - { url = "https://files.pythonhosted.org/packages/8a/dc/df98c095978fa6ee7b9a9387d1d58cbb3d232d0e69ad169a4ce784bde4fd/numpy-2.4.4-cp311-cp311-macosx_14_0_arm64.whl", hash = "sha256:86b6f55f5a352b48d7fbfd2dbc3d5b780b2d79f4d3c121f33eb6efb22e9a2015", size = 5476566 }, - { url = "https://files.pythonhosted.org/packages/28/34/b3fdcec6e725409223dd27356bdf5a3c2cc2282e428218ecc9cb7acc9763/numpy-2.4.4-cp311-cp311-macosx_14_0_x86_64.whl", hash = "sha256:ba1f4fc670ed79f876f70082eff4f9583c15fb9a4b89d6188412de4d18ae2f40", size = 6806482 }, - { url = "https://files.pythonhosted.org/packages/68/62/63417c13aa35d57bee1337c67446761dc25ea6543130cf868eace6e8157b/numpy-2.4.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8a87ec22c87be071b6bdbd27920b129b94f2fc964358ce38f3822635a3e2e03d", size = 15973376 }, - { url = "https://files.pythonhosted.org/packages/cf/c5/9fcb7e0e69cef59cf10c746b84f7d58b08bc66a6b7d459783c5a4f6101a6/numpy-2.4.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:df3775294accfdd75f32c74ae39fcba920c9a378a2fc18a12b6820aa8c1fb502", size = 16925137 }, - { url = "https://files.pythonhosted.org/packages/7e/43/80020edacb3f84b9efdd1591120a4296462c23fd8db0dde1666f6ef66f13/numpy-2.4.4-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:0d4e437e295f18ec29bc79daf55e8a47a9113df44d66f702f02a293d93a2d6dd", size = 17329414 }, - { url = "https://files.pythonhosted.org/packages/fd/06/af0658593b18a5f73532d377188b964f239eb0894e664a6c12f484472f97/numpy-2.4.4-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:6aa3236c78803afbcb255045fbef97a9e25a1f6c9888357d205ddc42f4d6eba5", size = 18658397 }, - { url = "https://files.pythonhosted.org/packages/e6/ce/13a09ed65f5d0ce5c7dd0669250374c6e379910f97af2c08c57b0608eee4/numpy-2.4.4-cp311-cp311-win32.whl", hash = "sha256:30caa73029a225b2d40d9fae193e008e24b2026b7ee1a867b7ee8d96ca1a448e", size = 6239499 }, - { url = "https://files.pythonhosted.org/packages/bd/63/05d193dbb4b5eec1eca73822d80da98b511f8328ad4ae3ca4caf0f4db91d/numpy-2.4.4-cp311-cp311-win_amd64.whl", hash = "sha256:6bbe4eb67390b0a0265a2c25458f6b90a409d5d069f1041e6aff1e27e3d9a79e", size = 12614257 }, - { url = "https://files.pythonhosted.org/packages/87/c5/8168052f080c26fa984c413305012be54741c9d0d74abd7fbeeccae3889f/numpy-2.4.4-cp311-cp311-win_arm64.whl", hash = "sha256:fcfe2045fd2e8f3cb0ce9d4ba6dba6333b8fa05bb8a4939c908cd43322d14c7e", size = 10486775 }, - { url = "https://files.pythonhosted.org/packages/28/05/32396bec30fb2263770ee910142f49c1476d08e8ad41abf8403806b520ce/numpy-2.4.4-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:15716cfef24d3a9762e3acdf87e27f58dc823d1348f765bbea6bef8c639bfa1b", size = 16689272 }, - { url = "https://files.pythonhosted.org/packages/c5/f3/a983d28637bfcd763a9c7aafdb6d5c0ebf3d487d1e1459ffdb57e2f01117/numpy-2.4.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:23cbfd4c17357c81021f21540da84ee282b9c8fba38a03b7b9d09ba6b951421e", size = 14699573 }, - { url = "https://files.pythonhosted.org/packages/9b/fd/e5ecca1e78c05106d98028114f5c00d3eddb41207686b2b7de3e477b0e22/numpy-2.4.4-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:8b3b60bb7cba2c8c81837661c488637eee696f59a877788a396d33150c35d842", size = 5204782 }, - { url = "https://files.pythonhosted.org/packages/de/2f/702a4594413c1a8632092beae8aba00f1d67947389369b3777aed783fdca/numpy-2.4.4-cp312-cp312-macosx_14_0_x86_64.whl", hash = "sha256:e4a010c27ff6f210ff4c6ef34394cd61470d01014439b192ec22552ee867f2a8", size = 6552038 }, - { url = "https://files.pythonhosted.org/packages/7f/37/eed308a8f56cba4d1fdf467a4fc67ef4ff4bf1c888f5fc980481890104b1/numpy-2.4.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:f9e75681b59ddaa5e659898085ae0eaea229d054f2ac0c7e563a62205a700121", size = 15670666 }, - { url = "https://files.pythonhosted.org/packages/0a/0d/0e3ecece05b7a7e87ab9fb587855548da437a061326fff64a223b6dcb78a/numpy-2.4.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:81f4a14bee47aec54f883e0cad2d73986640c1590eb9bfaaba7ad17394481e6e", size = 16645480 }, - { url = "https://files.pythonhosted.org/packages/34/49/f2312c154b82a286758ee2f1743336d50651f8b5195db18cdb63675ff649/numpy-2.4.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:62d6b0f03b694173f9fcb1fb317f7222fd0b0b103e784c6549f5e53a27718c44", size = 17020036 }, - { url = "https://files.pythonhosted.org/packages/7b/e9/736d17bd77f1b0ec4f9901aaec129c00d59f5d84d5e79bba540ef12c2330/numpy-2.4.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:fbc356aae7adf9e6336d336b9c8111d390a05df88f1805573ebb0807bd06fd1d", size = 18368643 }, - { url = "https://files.pythonhosted.org/packages/63/f6/d417977c5f519b17c8a5c3bc9e8304b0908b0e21136fe43bf628a1343914/numpy-2.4.4-cp312-cp312-win32.whl", hash = "sha256:0d35aea54ad1d420c812bfa0385c71cd7cc5bcf7c65fed95fc2cd02fe8c79827", size = 5961117 }, - { url = "https://files.pythonhosted.org/packages/2d/5b/e1deebf88ff431b01b7406ca3583ab2bbb90972bbe1c568732e49c844f7e/numpy-2.4.4-cp312-cp312-win_amd64.whl", hash = "sha256:b5f0362dc928a6ecd9db58868fca5e48485205e3855957bdedea308f8672ea4a", size = 12320584 }, - { url = "https://files.pythonhosted.org/packages/58/89/e4e856ac82a68c3ed64486a544977d0e7bdd18b8da75b78a577ca31c4395/numpy-2.4.4-cp312-cp312-win_arm64.whl", hash = "sha256:846300f379b5b12cc769334464656bc882e0735d27d9726568bc932fdc49d5ec", size = 10221450 }, - { url = "https://files.pythonhosted.org/packages/14/1d/d0a583ce4fefcc3308806a749a536c201ed6b5ad6e1322e227ee4848979d/numpy-2.4.4-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:08f2e31ed5e6f04b118e49821397f12767934cfdd12a1ce86a058f91e004ee50", size = 16684933 }, - { url = "https://files.pythonhosted.org/packages/c1/62/2b7a48fbb745d344742c0277f01286dead15f3f68e4f359fbfcf7b48f70f/numpy-2.4.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:e823b8b6edc81e747526f70f71a9c0a07ac4e7ad13020aa736bb7c9d67196115", size = 14694532 }, - { url = "https://files.pythonhosted.org/packages/e5/87/499737bfba066b4a3bebff24a8f1c5b2dee410b209bc6668c9be692580f0/numpy-2.4.4-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:4a19d9dba1a76618dd86b164d608566f393f8ec6ac7c44f0cc879011c45e65af", size = 5199661 }, - { url = "https://files.pythonhosted.org/packages/cd/da/464d551604320d1491bc345efed99b4b7034143a85787aab78d5691d5a0e/numpy-2.4.4-cp313-cp313-macosx_14_0_x86_64.whl", hash = "sha256:d2a8490669bfe99a233298348acc2d824d496dee0e66e31b66a6022c2ad74a5c", size = 6547539 }, - { url = "https://files.pythonhosted.org/packages/7d/90/8d23e3b0dafd024bf31bdec225b3bb5c2dbfa6912f8a53b8659f21216cbf/numpy-2.4.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:45dbed2ab436a9e826e302fcdcbe9133f9b0006e5af7168afb8963a6520da103", size = 15668806 }, - { url = "https://files.pythonhosted.org/packages/d1/73/a9d864e42a01896bb5974475438f16086be9ba1f0d19d0bb7a07427c4a8b/numpy-2.4.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c901b15172510173f5cb310eae652908340f8dede90fff9e3bf6c0d8dfd92f83", size = 16632682 }, - { url = "https://files.pythonhosted.org/packages/34/fb/14570d65c3bde4e202a031210475ae9cde9b7686a2e7dc97ee67d2833b35/numpy-2.4.4-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:99d838547ace2c4aace6c4f76e879ddfe02bb58a80c1549928477862b7a6d6ed", size = 17019810 }, - { url = "https://files.pythonhosted.org/packages/8a/77/2ba9d87081fd41f6d640c83f26fb7351e536b7ce6dd9061b6af5904e8e46/numpy-2.4.4-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:0aec54fd785890ecca25a6003fd9a5aed47ad607bbac5cd64f836ad8666f4959", size = 18357394 }, - { url = "https://files.pythonhosted.org/packages/a2/23/52666c9a41708b0853fa3b1a12c90da38c507a3074883823126d4e9d5b30/numpy-2.4.4-cp313-cp313-win32.whl", hash = "sha256:07077278157d02f65c43b1b26a3886bce886f95d20aabd11f87932750dfb14ed", size = 5959556 }, - { url = "https://files.pythonhosted.org/packages/57/fb/48649b4971cde70d817cf97a2a2fdc0b4d8308569f1dd2f2611959d2e0cf/numpy-2.4.4-cp313-cp313-win_amd64.whl", hash = "sha256:5c70f1cc1c4efbe316a572e2d8b9b9cc44e89b95f79ca3331553fbb63716e2bf", size = 12317311 }, - { url = "https://files.pythonhosted.org/packages/ba/d8/11490cddd564eb4de97b4579ef6bfe6a736cc07e94c1598590ae25415e01/numpy-2.4.4-cp313-cp313-win_arm64.whl", hash = "sha256:ef4059d6e5152fa1a39f888e344c73fdc926e1b2dd58c771d67b0acfbf2aa67d", size = 10222060 }, - { url = "https://files.pythonhosted.org/packages/99/5d/dab4339177a905aad3e2221c915b35202f1ec30d750dd2e5e9d9a72b804b/numpy-2.4.4-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:4bbc7f303d125971f60ec0aaad5e12c62d0d2c925f0ab1273debd0e4ba37aba5", size = 14822302 }, - { url = "https://files.pythonhosted.org/packages/eb/e4/0564a65e7d3d97562ed6f9b0fd0fb0a6f559ee444092f105938b50043876/numpy-2.4.4-cp313-cp313t-macosx_14_0_arm64.whl", hash = "sha256:4d6d57903571f86180eb98f8f0c839fa9ebbfb031356d87f1361be91e433f5b7", size = 5327407 }, - { url = "https://files.pythonhosted.org/packages/29/8d/35a3a6ce5ad371afa58b4700f1c820f8f279948cca32524e0a695b0ded83/numpy-2.4.4-cp313-cp313t-macosx_14_0_x86_64.whl", hash = "sha256:4636de7fd195197b7535f231b5de9e4b36d2c440b6e566d2e4e4746e6af0ca93", size = 6647631 }, - { url = "https://files.pythonhosted.org/packages/f4/da/477731acbd5a58a946c736edfdabb2ac5b34c3d08d1ba1a7b437fa0884df/numpy-2.4.4-cp313-cp313t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ad2e2ef14e0b04e544ea2fa0a36463f847f113d314aa02e5b402fdf910ef309e", size = 15727691 }, - { url = "https://files.pythonhosted.org/packages/e6/db/338535d9b152beabeb511579598418ba0212ce77cf9718edd70262cc4370/numpy-2.4.4-cp313-cp313t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5a285b3b96f951841799528cd1f4f01cd70e7e0204b4abebac9463eecfcf2a40", size = 16681241 }, - { url = "https://files.pythonhosted.org/packages/e2/a9/ad248e8f58beb7a0219b413c9c7d8151c5d285f7f946c3e26695bdbbe2df/numpy-2.4.4-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:f8474c4241bc18b750be2abea9d7a9ec84f46ef861dbacf86a4f6e043401f79e", size = 17085767 }, - { url = "https://files.pythonhosted.org/packages/b5/1a/3b88ccd3694681356f70da841630e4725a7264d6a885c8d442a697e1146b/numpy-2.4.4-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:4e874c976154687c1f71715b034739b45c7711bec81db01914770373d125e392", size = 18403169 }, - { url = "https://files.pythonhosted.org/packages/c2/c9/fcfd5d0639222c6eac7f304829b04892ef51c96a75d479214d77e3ce6e33/numpy-2.4.4-cp313-cp313t-win32.whl", hash = "sha256:9c585a1790d5436a5374bac930dad6ed244c046ed91b2b2a3634eb2971d21008", size = 6083477 }, - { url = "https://files.pythonhosted.org/packages/d5/e3/3938a61d1c538aaec8ed6fd6323f57b0c2d2d2219512434c5c878db76553/numpy-2.4.4-cp313-cp313t-win_amd64.whl", hash = "sha256:93e15038125dc1e5345d9b5b68aa7f996ec33b98118d18c6ca0d0b7d6198b7e8", size = 12457487 }, - { url = "https://files.pythonhosted.org/packages/97/6a/7e345032cc60501721ef94e0e30b60f6b0bd601f9174ebd36389a2b86d40/numpy-2.4.4-cp313-cp313t-win_arm64.whl", hash = "sha256:0dfd3f9d3adbe2920b68b5cd3d51444e13a10792ec7154cd0a2f6e74d4ab3233", size = 10292002 }, - { url = "https://files.pythonhosted.org/packages/6e/06/c54062f85f673dd5c04cbe2f14c3acb8c8b95e3384869bb8cc9bff8cb9df/numpy-2.4.4-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:f169b9a863d34f5d11b8698ead99febeaa17a13ca044961aa8e2662a6c7766a0", size = 16684353 }, - { url = "https://files.pythonhosted.org/packages/4c/39/8a320264a84404c74cc7e79715de85d6130fa07a0898f67fb5cd5bd79908/numpy-2.4.4-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:2483e4584a1cb3092da4470b38866634bafb223cbcd551ee047633fd2584599a", size = 14704914 }, - { url = "https://files.pythonhosted.org/packages/91/fb/287076b2614e1d1044235f50f03748f31fa287e3dbe6abeb35cdfa351eca/numpy-2.4.4-cp314-cp314-macosx_14_0_arm64.whl", hash = "sha256:2d19e6e2095506d1736b7d80595e0f252d76b89f5e715c35e06e937679ea7d7a", size = 5210005 }, - { url = "https://files.pythonhosted.org/packages/63/eb/fcc338595309910de6ecabfcef2419a9ce24399680bfb149421fa2df1280/numpy-2.4.4-cp314-cp314-macosx_14_0_x86_64.whl", hash = "sha256:6a246d5914aa1c820c9443ddcee9c02bec3e203b0c080349533fae17727dfd1b", size = 6544974 }, - { url = "https://files.pythonhosted.org/packages/44/5d/e7e9044032a716cdfaa3fba27a8e874bf1c5f1912a1ddd4ed071bf8a14a6/numpy-2.4.4-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:989824e9faf85f96ec9c7761cd8d29c531ad857bfa1daa930cba85baaecf1a9a", size = 15684591 }, - { url = "https://files.pythonhosted.org/packages/98/7c/21252050676612625449b4807d6b695b9ce8a7c9e1c197ee6216c8a65c7c/numpy-2.4.4-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:27a8d92cd10f1382a67d7cf4db7ce18341b66438bdd9f691d7b0e48d104c2a9d", size = 16637700 }, - { url = "https://files.pythonhosted.org/packages/b1/29/56d2bbef9465db24ef25393383d761a1af4f446a1df9b8cded4fe3a5a5d7/numpy-2.4.4-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:e44319a2953c738205bf3354537979eaa3998ed673395b964c1176083dd46252", size = 17035781 }, - { url = "https://files.pythonhosted.org/packages/e3/2b/a35a6d7589d21f44cea7d0a98de5ddcbb3d421b2622a5c96b1edf18707c3/numpy-2.4.4-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:e892aff75639bbef0d2a2cfd55535510df26ff92f63c92cd84ef8d4ba5a5557f", size = 18362959 }, - { url = "https://files.pythonhosted.org/packages/64/c9/d52ec581f2390e0f5f85cbfd80fb83d965fc15e9f0e1aec2195faa142cde/numpy-2.4.4-cp314-cp314-win32.whl", hash = "sha256:1378871da56ca8943c2ba674530924bb8ca40cd228358a3b5f302ad60cf875fc", size = 6008768 }, - { url = "https://files.pythonhosted.org/packages/fa/22/4cc31a62a6c7b74a8730e31a4274c5dc80e005751e277a2ce38e675e4923/numpy-2.4.4-cp314-cp314-win_amd64.whl", hash = "sha256:715d1c092715954784bc79e1174fc2a90093dc4dc84ea15eb14dad8abdcdeb74", size = 12449181 }, - { url = "https://files.pythonhosted.org/packages/70/2e/14cda6f4d8e396c612d1bf97f22958e92148801d7e4f110cabebdc0eef4b/numpy-2.4.4-cp314-cp314-win_arm64.whl", hash = "sha256:2c194dd721e54ecad9ad387c1d35e63dce5c4450c6dc7dd5611283dda239aabb", size = 10496035 }, - { url = "https://files.pythonhosted.org/packages/b1/e8/8fed8c8d848d7ecea092dc3469643f9d10bc3a134a815a3b033da1d2039b/numpy-2.4.4-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:2aa0613a5177c264ff5921051a5719d20095ea586ca88cc802c5c218d1c67d3e", size = 14824958 }, - { url = "https://files.pythonhosted.org/packages/05/1a/d8007a5138c179c2bf33ef44503e83d70434d2642877ee8fbb230e7c0548/numpy-2.4.4-cp314-cp314t-macosx_14_0_arm64.whl", hash = "sha256:42c16925aa5a02362f986765f9ebabf20de75cdefdca827d14315c568dcab113", size = 5330020 }, - { url = "https://files.pythonhosted.org/packages/99/64/ffb99ac6ae93faf117bcbd5c7ba48a7f45364a33e8e458545d3633615dda/numpy-2.4.4-cp314-cp314t-macosx_14_0_x86_64.whl", hash = "sha256:874f200b2a981c647340f841730fc3a2b54c9d940566a3c4149099591e2c4c3d", size = 6650758 }, - { url = "https://files.pythonhosted.org/packages/6e/6e/795cc078b78a384052e73b2f6281ff7a700e9bf53bcce2ee579d4f6dd879/numpy-2.4.4-cp314-cp314t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c9b39d38a9bd2ae1becd7eac1303d031c5c110ad31f2b319c6e7d98b135c934d", size = 15729948 }, - { url = "https://files.pythonhosted.org/packages/5f/86/2acbda8cc2af5f3d7bfc791192863b9e3e19674da7b5e533fded124d1299/numpy-2.4.4-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b268594bccac7d7cf5844c7732e3f20c50921d94e36d7ec9b79e9857694b1b2f", size = 16679325 }, - { url = "https://files.pythonhosted.org/packages/bc/59/cafd83018f4aa55e0ac6fa92aa066c0a1877b77a615ceff1711c260ffae8/numpy-2.4.4-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:ac6b31e35612a26483e20750126d30d0941f949426974cace8e6b5c58a3657b0", size = 17084883 }, - { url = "https://files.pythonhosted.org/packages/f0/85/a42548db84e65ece46ab2caea3d3f78b416a47af387fcbb47ec28e660dc2/numpy-2.4.4-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8e3ed142f2728df44263aaf5fb1f5b0b99f4070c553a0d7f033be65338329150", size = 18403474 }, - { url = "https://files.pythonhosted.org/packages/ed/ad/483d9e262f4b831000062e5d8a45e342166ec8aaa1195264982bca267e62/numpy-2.4.4-cp314-cp314t-win32.whl", hash = "sha256:dddbbd259598d7240b18c9d87c56a9d2fb3b02fe266f49a7c101532e78c1d871", size = 6155500 }, - { url = "https://files.pythonhosted.org/packages/c7/03/2fc4e14c7bd4ff2964b74ba90ecb8552540b6315f201df70f137faa5c589/numpy-2.4.4-cp314-cp314t-win_amd64.whl", hash = "sha256:a7164afb23be6e37ad90b2f10426149fd75aee07ca55653d2aa41e66c4ef697e", size = 12637755 }, - { url = "https://files.pythonhosted.org/packages/58/78/548fb8e07b1a341746bfbecb32f2c268470f45fa028aacdbd10d9bc73aab/numpy-2.4.4-cp314-cp314t-win_arm64.whl", hash = "sha256:ba203255017337d39f89bdd58417f03c4426f12beed0440cfd933cb15f8669c7", size = 10566643 }, - { url = "https://files.pythonhosted.org/packages/6b/33/8fae8f964a4f63ed528264ddf25d2b683d0b663e3cba26961eb838a7c1bd/numpy-2.4.4-pp311-pypy311_pp73-macosx_10_15_x86_64.whl", hash = "sha256:58c8b5929fcb8287cbd6f0a3fae19c6e03a5c48402ae792962ac465224a629a4", size = 16854491 }, - { url = "https://files.pythonhosted.org/packages/bc/d0/1aabee441380b981cf8cdda3ae7a46aa827d1b5a8cce84d14598bc94d6d9/numpy-2.4.4-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:eea7ac5d2dce4189771cedb559c738a71512768210dc4e4753b107a2048b3d0e", size = 14895830 }, - { url = "https://files.pythonhosted.org/packages/a5/b8/aafb0d1065416894fccf4df6b49ef22b8db045187949545bced89c034b8e/numpy-2.4.4-pp311-pypy311_pp73-macosx_14_0_arm64.whl", hash = "sha256:51fc224f7ca4d92656d5a5eb315f12eb5fe2c97a66249aa7b5f562528a3be38c", size = 5400927 }, - { url = "https://files.pythonhosted.org/packages/d6/77/063baa20b08b431038c7f9ff5435540c7b7265c78cf56012a483019ca72d/numpy-2.4.4-pp311-pypy311_pp73-macosx_14_0_x86_64.whl", hash = "sha256:28a650663f7314afc3e6ec620f44f333c386aad9f6fc472030865dc0ebb26ee3", size = 6715557 }, - { url = "https://files.pythonhosted.org/packages/c7/a8/379542d45a14f149444c5c4c4e7714707239ce9cc1de8c2803958889da14/numpy-2.4.4-pp311-pypy311_pp73-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:19710a9ca9992d7174e9c52f643d4272dcd1558c5f7af7f6f8190f633bd651a7", size = 15804253 }, - { url = "https://files.pythonhosted.org/packages/a2/c8/f0a45426d6d21e7ea3310a15cf90c43a14d9232c31a837702dba437f3373/numpy-2.4.4-pp311-pypy311_pp73-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9b2aec6af35c113b05695ebb5749a787acd63cafc83086a05771d1e1cd1e555f", size = 16753552 }, - { url = "https://files.pythonhosted.org/packages/04/74/f4c001f4714c3ad9ce037e18cf2b9c64871a84951eaa0baf683a9ca9301c/numpy-2.4.4-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:f2cf083b324a467e1ab358c105f6cad5ea950f50524668a80c486ff1db24e119", size = 12509075 }, -] - [[package]] name = "opencv-python" version = "4.13.0.92" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, - { name = "numpy", version = "2.4.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, + { name = "numpy" }, ] wheels = [ { url = "https://files.pythonhosted.org/packages/fc/6f/5a28fef4c4a382be06afe3938c64cc168223016fa520c5abaf37e8862aa5/opencv_python-4.13.0.92-cp37-abi3-macosx_13_0_arm64.whl", hash = "sha256:caf60c071ec391ba51ed00a4a920f996d0b64e3e46068aac1f646b5de0326a19", size = 46247052 }, From 17b0b0679e44c286971ae5cb41b6ff0094a7f9da Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:10:33 +0000 Subject: [PATCH 04/35] introduce img2num::ImageToSvgConfig with c and py bindings... js binding WIP --- bindings/c/include/cimg2num.h | 12 +++++++--- bindings/c/src/cimg2num.cpp | 16 ++++++++++++- bindings/py/src/img2num_pybind.cpp | 37 ++++++++++++++++++++++++++++++ core/include/img2num.h | 28 ++++++++++++++++++++-- core/src/internal/image_to_svg.cpp | 15 ++++++------ example-apps/console-cpp/main.cpp | 4 +++- example-apps/console-py/main.py | 11 ++++++++- packages/py/img2num/api.py | 9 ++++++-- 8 files changed, 115 insertions(+), 17 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 008b35fe5..efbf42c5f 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -13,7 +13,13 @@ #include #ifdef __cplusplus +#include "img2num.h" extern "C" { + typedef img2num::ImageToSvgConfig ImageToSvgConfig; +#else + // 3. For the C compiler, we provide an "Opaque Struct" definition. + // C doesn't need to know what's inside, just that it's a type. + typedef struct ImageToSvgConfig ImageToSvgConfig; #endif /// @copydoc ::IMG2NUM_H_GAUSSIAN_BLUR_DOC @@ -44,10 +50,10 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in const int height, const int min_area); /// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_DOC -char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, +/*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); - + const int32_t max_iter, const int min_area, const uint8_t color_space);*/ +char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig* config); #ifdef __cplusplus } #endif diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 55c810d8f..78a5f4a47 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -56,7 +56,7 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in return result; } -char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, +/*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}; @@ -72,5 +72,19 @@ char *img2num_image_to_svg(const uint8_t *data, const int width, const int heigh }, data, width, height, sigma_spatial, sigma_range, k, max_iter, min_area, color_space); return result; +};*/ +char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig* config) { + char *result{nullptr}; + img2num::clear_last_error_and_catch( + [&](const uint8_t *d, const int w, const int h, const ImageToSvgConfig* cfg) { + std::string svg{img2num::image_to_svg(d, w, h, *cfg)}; + result = static_cast(std::malloc(svg.size() + 1)); + if (!result) { + return; // Allocation failed + } + std::memcpy(result, svg.c_str(), svg.size() + 1); + }, + data, width, height, config); + return result; }; } diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index a759804f1..b155f882f 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -121,13 +121,49 @@ 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"); + pybind11::class_ config(m, "ImageToSvgConfig"); + pybind11::class_(config, "BilateralFilterConfig") + .def(pybind11::init<>()) + .def_readwrite("sigma_spatial", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial) + .def_readwrite("sigma_range", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range); + pybind11::class_(config, "KMeansConfig") + .def(pybind11::init<>()) + .def_readwrite("k", &img2num::ImageToSvgConfig::KMeansConfig::k) + .def_readwrite("max_iter", &img2num::ImageToSvgConfig::KMeansConfig::max_iter); + /*config.def(pybind11::init<>()) + .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);*/ + config.def(pybind11::init([]() { + return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); + })); + m.def( + "image_to_svg", + [](pybind11::array_t data, int width, int height, const img2num::ImageToSvgConfig &cfg) { + const uint8_t* data_ptr{static_cast(data.request().ptr)}; + 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("config"), + "Convert Image to SVG string"); + + /*m.def( "image_to_svg", [](pybind11::array_t 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(data.request().ptr)}; + img2num::ImageToSvgConfig config { + .bilateral_filter{.sigma_spatial = 3.0, .sigma_range=50.0}, + .min_cluster_area = 100, + .color_space = 0, + .kmeans{.k=16, .max_iter=100} + }; + 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)); @@ -138,4 +174,5 @@ PYBIND11_MODULE(_img2num, m) { 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"); + */ } diff --git a/core/include/img2num.h b/core/include/img2num.h index 96becdb79..f7ea017d8 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -15,6 +15,28 @@ /// @note All image buffers are assumed to be stored in row-major order, unless otherwise noted. namespace img2num { +struct ImageToSvgConfig { + struct BilateralFilterConfig { + double sigma_spatial = 3.0; + double sigma_range = 50.0; + } bilateral_filter; + + int min_cluster_area = 100; + uint8_t color_space = 0; + + struct KMeansConfig { + int32_t k = 16; + int32_t max_iter = 100; + } kmeans; +}; + +const ImageToSvgConfig IMAGE_TO_SVG_DEFAULT_CONFIG = { + .bilateral_filter{.sigma_spatial = 3.0, .sigma_range=50.0}, + .min_cluster_area = 100, + .color_space = 0, + .kmeans{.k=16, .max_iter=100} +}; + /// @copydoc IMG2NUM_H_GAUSSIAN_BLUR_DOC void gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma); @@ -42,9 +64,11 @@ std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int const int height, const int min_area); /// @copydoc IMG2NUM_H_IMAGE_TO_SVG_DOC -std::string image_to_svg(const uint8_t *data, const int width, const int height, +/*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); + const int32_t max_iter, const int min_area, const uint8_t color_space);*/ + +std::string image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig& config = IMAGE_TO_SVG_DEFAULT_CONFIG); } // namespace img2num diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp index 12dc2be35..3c7de7c6a 100644 --- a/core/src/internal/image_to_svg.cpp +++ b/core/src/internal/image_to_svg.cpp @@ -4,9 +4,10 @@ #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) { +//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) { +std::string image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig& config) { // self deallocate std::vector img_data(width * height * 4); std::vector out_data(width * height * 4); @@ -14,10 +15,10 @@ std::string image_to_svg(const uint8_t *data, const int width, const int height, std::memcpy(img_data.data(), data, static_cast(width) * static_cast(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)}; + 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); + std::string svg{labels_to_svg(data, out_labels.data(), width, height, config.min_cluster_area)}; return svg; } diff --git a/example-apps/console-cpp/main.cpp b/example-apps/console-cpp/main.cpp index b96142a37..0e56bfb3e 100644 --- a/example-apps/console-cpp/main.cpp +++ b/example-apps/console-cpp/main.cpp @@ -64,7 +64,9 @@ 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)}; + img2num::ImageToSvgConfig config = img2num::IMAGE_TO_SVG_DEFAULT_CONFIG; + config.kmeans.k = 32; + std::string res_svg2{img2num::image_to_svg(img_data, width, height, config)}; // Save the blurred image std::string out_path{std::string(OUT_DIR) + "/console-cpp-output.png"}; diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index 97590ada2..ef262fe68 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -31,7 +31,16 @@ def main(): f.writelines(res_svg) # res_svg2 should match res_svg - res_svg2 = img2num.image_to_svg(img, 3, 50, 16, 100, 100, 0) + cfg = img2num.ImageToSvgConfig() # instanitates defaults + + # cfg.bilateral_filter.sigma_spatial = 3 + # cfg.bilateral_filter.sigma_range = 50 + # cfg.kmeans.k = 16 + # cfg.kmeans.max_iter = 100 + # cfg.min_cluster_size = 100 + # cfg.color_space = 0 + + res_svg2 = img2num.image_to_svg(img, cfg) with open(os.path.join(OUTDIR, "result2.svg"),"w") as f: f.writelines(res_svg2) diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 4ce7846be..953be4b89 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -10,6 +10,7 @@ kmeans as _kmeans, labels_to_svg as _labels_to_svg, image_to_svg as _image_to_svg, + ImageToSvgConfig ) def _inject_dims(image_arg="image"): @@ -61,6 +62,10 @@ def kmeans(data, k, max_iter, color_space, *, width, height): 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) + @_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) +def image_to_svg(image, config, *, width, height): + return _image_to_svg(image, width, height, config) \ No newline at end of file From dd4bec24bd390957ee5ff27d5c216e43d3b4be2c Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:22:19 +0000 Subject: [PATCH 05/35] update js bindings --- bindings/js/CMakeLists.txt | 2 +- bindings/js/src/wasm_wrapper.cpp | 12 ++++++++++-- bindings/py/src/img2num_pybind.cpp | 5 ----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index 678972c58..a01408a17 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -12,7 +12,7 @@ file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src add_executable(img2num_wasm ${BINDING_SRC}) -target_link_libraries(img2num_wasm PRIVATE CImg2Num) +target_link_libraries(img2num_wasm PRIVATE CImg2Num Img2Num) target_include_directories(img2num_wasm PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index c62f7357f..55b2c0285 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -42,7 +42,15 @@ EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, co 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); + /*return img2num_image_to_svg(data, width, height, sigma_spatial, sigma_range, k, max_iter, + min_area, color_space);*/ + ImageToSvgConfig config; + 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); } } diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index b155f882f..3f58b7565 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -130,11 +130,6 @@ PYBIND11_MODULE(_img2num, m) { .def(pybind11::init<>()) .def_readwrite("k", &img2num::ImageToSvgConfig::KMeansConfig::k) .def_readwrite("max_iter", &img2num::ImageToSvgConfig::KMeansConfig::max_iter); - /*config.def(pybind11::init<>()) - .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);*/ config.def(pybind11::init([]() { return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); })); From 35299a100001a634b96906887925a1dec1f5b3f4 Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:24:56 +0000 Subject: [PATCH 06/35] fix name --- packages/js/safeWasmWrappers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index 092590d6c..ea02a3f7a 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -230,10 +230,10 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1 * @variation Contour extraction with optional visualization * @since 0.0.0 */ -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 }) => { +export const imgToSVG = async ({ pixels, width, height, sigma_spatial = 3, sigma_range = 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 }, + args: { pixels, width, height, sigma_spatial, sigma_range, num_colors, max_iter, min_area, color_space }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }], returnType: "string", }); From 7e75ab1c782e8fd08fb679756faefedd02fe2074 Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:31:54 +0000 Subject: [PATCH 07/35] remove debug prints --- core/src/internal/bilateral_filter.cpp | 3 --- core/src/internal/kmeans.cpp | 3 --- example-apps/react-js/src/components/WasmImageProcessor.jsx | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/core/src/internal/bilateral_filter.cpp b/core/src/internal/bilateral_filter.cpp index f8701200e..03600e4d1 100644 --- a/core/src/internal/bilateral_filter.cpp +++ b/core/src/internal/bilateral_filter.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include "img2num.h" @@ -225,10 +224,8 @@ 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); } } diff --git a/core/src/internal/kmeans.cpp b/core/src/internal/kmeans.cpp index 768495b7d..23457dec7 100644 --- a/core/src/internal/kmeans.cpp +++ b/core/src/internal/kmeans.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -243,10 +242,8 @@ void kmeans(const uint8_t *data, uint8_t *out_data, int32_t *out_labels, const i GPU::getClassInstance().init_gpu(); if (GPU::getClassInstance().is_initialized()) { - std::cout << "kmeans gpu" << std::endl; kmeans_gpu(data, out_data, out_labels, width, height, k, max_iter, color_space); } else { - std::cout << "kmeans cpu" << std::endl; kmeans_cpu(data, out_data, out_labels, width, height, k, max_iter, color_space); } } diff --git a/example-apps/react-js/src/components/WasmImageProcessor.jsx b/example-apps/react-js/src/components/WasmImageProcessor.jsx index 0d0220b27..85ab0b225 100644 --- a/example-apps/react-js/src/components/WasmImageProcessor.jsx +++ b/example-apps/react-js/src/components/WasmImageProcessor.jsx @@ -1,6 +1,6 @@ import { useEffect, useState, useId, useRef, useCallback, useMemo } from "react"; import { Upload } from "lucide-react"; -import { imageToUint8ClampedArray, bilateralFilter, kmeans, findContours, imgToSVG } from "img2num"; +import { imageToUint8ClampedArray, imgToSVG } from "img2num"; import GlassCard from "@components/GlassCard"; import styles from "./WasmImageProcessor.module.css"; import { useNavigate } from "react-router-dom"; From 63ffecdbfe2423e8e8a9353aa25add16c18dc1d5 Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:40:19 +0000 Subject: [PATCH 08/35] pybind fix and add repr to ImageToSvgConfig --- bindings/py/src/img2num_pybind.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 3f58b7565..037a17b74 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -125,14 +125,37 @@ PYBIND11_MODULE(_img2num, m) { pybind11::class_(config, "BilateralFilterConfig") .def(pybind11::init<>()) .def_readwrite("sigma_spatial", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial) - .def_readwrite("sigma_range", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range); + .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_(config, "KMeansConfig") .def(pybind11::init<>()) .def_readwrite("k", &img2num::ImageToSvgConfig::KMeansConfig::k) - .def_readwrite("max_iter", &img2num::ImageToSvgConfig::KMeansConfig::max_iter); + .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([]() { return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); - })); + })) + .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 << "() << ", " + << "min_cluster_area: " << c.min_cluster_area << ", " + << "color_space: " << (int)c.color_space << ", " + << "kmeans: " << pybind11::repr(pybind11::cast(c.kmeans)).cast() + << "}>"; + return ss.str(); + }); m.def( "image_to_svg", From 771e0cfff8b0bdc91fdc112459a9f9078f94a98a Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:50:24 +0000 Subject: [PATCH 09/35] formatting --- bindings/c/include/cimg2num.h | 11 +++--- bindings/c/src/cimg2num.cpp | 5 +-- bindings/py/src/img2num_pybind.cpp | 55 ++++++++++++++++-------------- core/include/img2num.h | 16 ++++----- core/src/internal/image_to_svg.cpp | 16 +++++---- packages/js/safeWasmWrappers.js | 3 +- 6 files changed, 58 insertions(+), 48 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index efbf42c5f..003400f87 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -15,11 +15,11 @@ #ifdef __cplusplus #include "img2num.h" extern "C" { - typedef img2num::ImageToSvgConfig ImageToSvgConfig; +typedef img2num::ImageToSvgConfig ImageToSvgConfig; #else - // 3. For the C compiler, we provide an "Opaque Struct" definition. - // C doesn't need to know what's inside, just that it's a type. - typedef struct ImageToSvgConfig ImageToSvgConfig; +// 3. For the C compiler, we provide an "Opaque Struct" definition. +// C doesn't need to know what's inside, just that it's a type. +typedef struct ImageToSvgConfig ImageToSvgConfig; #endif /// @copydoc ::IMG2NUM_H_GAUSSIAN_BLUR_DOC @@ -53,7 +53,8 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in /*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 *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig* config); +char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, + const ImageToSvgConfig *config); #ifdef __cplusplus } #endif diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 78a5f4a47..d381466c7 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -73,10 +73,11 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in data, width, height, sigma_spatial, sigma_range, k, max_iter, min_area, color_space); return result; };*/ -char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig* config) { +char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, + const ImageToSvgConfig *config) { char *result{nullptr}; img2num::clear_last_error_and_catch( - [&](const uint8_t *d, const int w, const int h, const ImageToSvgConfig* cfg) { + [&](const uint8_t *d, const int w, const int h, const ImageToSvgConfig *cfg) { std::string svg{img2num::image_to_svg(d, w, h, *cfg)}; result = static_cast(std::malloc(svg.size() + 1)); if (!result) { diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 037a17b74..970e6ff68 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -91,12 +91,12 @@ PYBIND11_MODULE(_img2num, m) { auto out_data = pybind11::array_t(data_buf.shape); auto out_labels = pybind11::array_t({(ssize_t)height, (ssize_t)width}); - auto out_data_ptr = static_cast(out_data.mutable_data()); - auto out_labels_ptr = static_cast(out_labels.mutable_data()); + auto out_data_ptr = static_cast(out_data.mutable_data()); + auto out_labels_ptr = static_cast(out_labels.mutable_data()); // Call C function - img2num::kmeans(static_cast(data_buf.ptr), out_data_ptr, out_labels_ptr, - width, height, k, max_iter, color_space); + img2num::kmeans(static_cast(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); @@ -110,8 +110,8 @@ PYBIND11_MODULE(_img2num, m) { [](pybind11::array_t data, pybind11::array_t labels, int width, int height, int min_area) { - const uint8_t* data_ptr{static_cast(data.request().ptr)}; - const int32_t* labels_ptr{static_cast(labels.request().ptr)}; + const uint8_t *data_ptr{static_cast(data.request().ptr)}; + const int32_t *labels_ptr{static_cast(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)); @@ -122,25 +122,28 @@ PYBIND11_MODULE(_img2num, m) { pybind11::arg("height"), pybind11::arg("min_area"), "Convert labels to SVG string"); pybind11::class_ config(m, "ImageToSvgConfig"); - pybind11::class_(config, "BilateralFilterConfig") + pybind11::class_(config, + "BilateralFilterConfig") .def(pybind11::init<>()) - .def_readwrite("sigma_spatial", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial) - .def_readwrite("sigma_range", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range) + .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) + "}"; + return "{'sigma_spatial': " + std::to_string(c.sigma_spatial) + + ", 'sigma_range': " + std::to_string(c.sigma_range) + "}"; }); pybind11::class_(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) + "}"; + return "{'k': " + std::to_string(c.k) + ", 'max_iter': " + std::to_string(c.max_iter) + + "}"; }); - config.def(pybind11::init([]() { - return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); - })) + config + .def(pybind11::init( + []() { return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); })) .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) @@ -149,24 +152,26 @@ PYBIND11_MODULE(_img2num, m) { // We use pybind11::repr() to trigger the __repr__ of the nested objects std::stringstream ss; ss << "() << ", " - << "min_cluster_area: " << c.min_cluster_area << ", " - << "color_space: " << (int)c.color_space << ", " - << "kmeans: " << pybind11::repr(pybind11::cast(c.kmeans)).cast() - << "}>"; + << "bilateral_filter: " + << pybind11::repr(pybind11::cast(c.bilateral_filter)).cast() << ", " + << "min_cluster_area: " << c.min_cluster_area << ", " + << "color_space: " << (int)c.color_space << ", " + << "kmeans: " << pybind11::repr(pybind11::cast(c.kmeans)).cast() + << "}>"; return ss.str(); }); m.def( "image_to_svg", - [](pybind11::array_t data, int width, int height, const img2num::ImageToSvgConfig &cfg) { - const uint8_t* data_ptr{static_cast(data.request().ptr)}; + [](pybind11::array_t data, int width, int height, + const img2num::ImageToSvgConfig &cfg) { + const uint8_t *data_ptr{static_cast(data.request().ptr)}; 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("config"), - "Convert Image to SVG string"); + pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"), + pybind11::arg("config"), "Convert Image to SVG string"); /*m.def( "image_to_svg", diff --git a/core/include/img2num.h b/core/include/img2num.h index f7ea017d8..f8986f31f 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -17,25 +17,24 @@ namespace img2num { struct ImageToSvgConfig { struct BilateralFilterConfig { - double sigma_spatial = 3.0; - double sigma_range = 50.0; + double sigma_spatial = 3.0; + double sigma_range = 50.0; } bilateral_filter; int min_cluster_area = 100; - uint8_t color_space = 0; + uint8_t color_space = 0; struct KMeansConfig { - int32_t k = 16; + int32_t k = 16; int32_t max_iter = 100; } kmeans; }; const ImageToSvgConfig IMAGE_TO_SVG_DEFAULT_CONFIG = { - .bilateral_filter{.sigma_spatial = 3.0, .sigma_range=50.0}, + .bilateral_filter{.sigma_spatial = 3.0, .sigma_range = 50.0}, .min_cluster_area = 100, .color_space = 0, - .kmeans{.k=16, .max_iter=100} -}; + .kmeans{.k = 16, .max_iter = 100}}; /// @copydoc IMG2NUM_H_GAUSSIAN_BLUR_DOC void gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma); @@ -68,7 +67,8 @@ std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int double sigma_spatial, double sigma_range, const int32_t k, const int32_t max_iter, const int min_area, const uint8_t color_space);*/ -std::string image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig& config = IMAGE_TO_SVG_DEFAULT_CONFIG); +std::string image_to_svg(const uint8_t *data, const int width, const int height, + const ImageToSvgConfig &config = IMAGE_TO_SVG_DEFAULT_CONFIG); } // namespace img2num diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp index 3c7de7c6a..488178bc1 100644 --- a/core/src/internal/image_to_svg.cpp +++ b/core/src/internal/image_to_svg.cpp @@ -4,10 +4,11 @@ #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) { -std::string image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig& config) { +// 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) { +std::string image_to_svg(const uint8_t* data, const int width, const int height, + const ImageToSvgConfig& config) { // self deallocate std::vector img_data(width * height * 4); std::vector out_data(width * height * 4); @@ -15,9 +16,10 @@ std::string image_to_svg(const uint8_t *data, const int width, const int height, std::memcpy(img_data.data(), data, static_cast(width) * static_cast(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); + 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); std::string svg{labels_to_svg(data, out_labels.data(), width, height, config.min_cluster_area)}; return svg; diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index ea02a3f7a..531e50718 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -222,7 +222,8 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1 * @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. + * @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 }); From b1230d46bb746038e22adfc0154ab0417b927aff Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 04:52:21 +0000 Subject: [PATCH 10/35] add python badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc1a1caeb..f52e433ea 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ [![Open in Codespaces](https://img.shields.io/badge/-Open%20in%20Codespaces-black?logo=github)](https://codespaces.new/Ryan-Millard/Img2Num) - + _Img2Num_ converts raster images into clean SVGs with _high accuracy and performance_. From 7f58edfbdd8278ab2c6ea3637631cd1906b2f7ec Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 15:08:33 +0000 Subject: [PATCH 11/35] define transparent ImageToSvgConfig struct in c bindings --- bindings/c/include/cimg2num.h | 20 +++++++++++++++++--- bindings/c/src/cimg2num.cpp | 9 +++++++++ bindings/js/src/wasm_wrapper.cpp | 20 ++++++++++++-------- example-apps/console-c/main.c | 4 ++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 003400f87..64e505f04 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -17,11 +17,25 @@ extern "C" { typedef img2num::ImageToSvgConfig ImageToSvgConfig; #else -// 3. For the C compiler, we provide an "Opaque Struct" definition. -// C doesn't need to know what's inside, just that it's a type. -typedef struct ImageToSvgConfig ImageToSvgConfig; + +typedef struct ImageToSvgConfig { + struct { + double sigma_spatial; + double sigma_range; + } bilateral_filter; + int min_cluster_area; + uint8_t color_space; + struct { + int32_t k; + int32_t max_iter; + } kmeans; +} ImageToSvgConfig; + #endif +ImageToSvgConfig* img2num_config_create(); +void img2num_config_free(ImageToSvgConfig* config); + /// @copydoc ::IMG2NUM_H_GAUSSIAN_BLUR_DOC void img2num_gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma); diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index d381466c7..03bb8e8c8 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -7,6 +7,15 @@ extern "C" { +ImageToSvgConfig* img2num_config_create() { + // Returns a copy of your C++ default constant + return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); +} + +void img2num_config_free(ImageToSvgConfig* config) { + delete reinterpret_cast(config); +} + 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); } diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index 55b2c0285..9ae306a17 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -44,13 +44,17 @@ EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, co const uint8_t color_space) { /*return img2num_image_to_svg(data, width, height, sigma_spatial, sigma_range, k, max_iter, min_area, color_space);*/ - ImageToSvgConfig config; - 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); + ImageToSvgConfig* config = img2num_config_create(); + + 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; + + char* svg = img2num_image_to_svg(data, width, height, config); + img2num_config_free(config); + return svg; } } diff --git a/example-apps/console-c/main.c b/example-apps/console-c/main.c index 0faf5e2e4..2f7d88996 100644 --- a/example-apps/console-c/main.c +++ b/example-apps/console-c/main.c @@ -73,6 +73,10 @@ int main(int argc, char** argv) { // Generate SVG char* res_svg = img2num_labels_to_svg(img_data, out_labels, width, height, 100); + ImageToSvgConfig* config = img2num_config_create(); + char* res_svg2 = img2num_image_to_svg(image_data_original, width, height, config); + img2num_config_free(config); + if (res_svg == NULL) { fprintf(stderr, "Failed to generate SVG\n"); stbi_image_free(image_data_original); From 1802f5a96e241f8b1f78a1f62a01b428cbe70102 Mon Sep 17 00:00:00 2001 From: Krasner Date: Thu, 30 Apr 2026 15:24:43 +0000 Subject: [PATCH 12/35] free res_svg2 --- example-apps/console-c/main.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/example-apps/console-c/main.c b/example-apps/console-c/main.c index 2f7d88996..35a789691 100644 --- a/example-apps/console-c/main.c +++ b/example-apps/console-c/main.c @@ -78,7 +78,16 @@ int main(int argc, char** argv) { img2num_config_free(config); if (res_svg == NULL) { - fprintf(stderr, "Failed to generate SVG\n"); + fprintf(stderr, "Failed to generate SVG step-by-step\n"); + stbi_image_free(image_data_original); + free(img_data); + free(out_data); + free(out_labels); + return 1; + } + + if (res_svg2 == NULL) { + fprintf(stderr, "Failed to generate SVG in unified call\n"); stbi_image_free(image_data_original); free(img_data); free(out_data); @@ -124,5 +133,6 @@ int main(int argc, char** argv) { free(out_data); free(out_labels); free(res_svg); + free(res_svg2); return exit_code; } From dbce7930062663784dfe1e25cf07c548f2d78924 Mon Sep 17 00:00:00 2001 From: Krasner Date: Fri, 1 May 2026 01:58:45 +0000 Subject: [PATCH 13/35] clean up. add arguments to pybind ImageToSvgConfig to allow for arguments in constructor --- bindings/c/include/cimg2num.h | 3 -- bindings/c/src/cimg2num.cpp | 17 --------- bindings/py/src/img2num_pybind.cpp | 60 ++++++++++++++++-------------- core/include/img2num.h | 4 -- core/src/internal/image_to_svg.cpp | 3 -- example-apps/console-py/main.py | 5 ++- 6 files changed, 36 insertions(+), 56 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 64e505f04..bf6881369 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -64,9 +64,6 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in const int height, const int min_area); /// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_DOC -/*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 *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig *config); #ifdef __cplusplus diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 03bb8e8c8..fec0c0764 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -65,23 +65,6 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in 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(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; -};*/ char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig *config) { char *result{nullptr}; diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 970e6ff68..14b31f875 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -143,7 +143,39 @@ PYBIND11_MODULE(_img2num, m) { }); config .def(pybind11::init( - []() { return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); })) + [](pybind11::dict bf_dict, pybind11::dict km_dict, pybind11::kwargs kwargs) { + /* + cfg = img2num.ImageToSvgConfig( + bf = {"sigma_spatial": 5.0, "sigma_range": 30.0}, + km = {"k": 32}, + min_cluster_area = 250, + color_space = 1 + ) + + cfg = img2num.ImageToSvgConfig() will use default values + */ + auto c = new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); + if (bf_dict.contains("sigma_spatial")) + c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); + if (bf_dict.contains("sigma_range")) + c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); + + // 3. Process KMeans overrides from the 'km' dictionary + if (km_dict.contains("k")) + c->kmeans.k = km_dict["k"].cast(); + if (km_dict.contains("max_iter")) + c->kmeans.max_iter = km_dict["max_iter"].cast(); + + // 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(); + if (kwargs.contains("color_space")) + c->color_space = kwargs["color_space"].cast(); + + return c; + }), pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict + pybind11::arg("km") = 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) @@ -172,30 +204,4 @@ PYBIND11_MODULE(_img2num, m) { }, pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"), pybind11::arg("config"), "Convert Image to SVG string"); - - /*m.def( - "image_to_svg", - [](pybind11::array_t 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(data.request().ptr)}; - - img2num::ImageToSvgConfig config { - .bilateral_filter{.sigma_spatial = 3.0, .sigma_range=50.0}, - .min_cluster_area = 100, - .color_space = 0, - .kmeans{.k=16, .max_iter=100} - }; - - 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("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"); - */ } diff --git a/core/include/img2num.h b/core/include/img2num.h index f8986f31f..d2983f5c5 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -63,10 +63,6 @@ std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int const int height, const int min_area); /// @copydoc IMG2NUM_H_IMAGE_TO_SVG_DOC -/*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);*/ - std::string image_to_svg(const uint8_t *data, const int width, const int height, const ImageToSvgConfig &config = IMAGE_TO_SVG_DEFAULT_CONFIG); diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp index 488178bc1..6b2efeecc 100644 --- a/core/src/internal/image_to_svg.cpp +++ b/core/src/internal/image_to_svg.cpp @@ -4,9 +4,6 @@ #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) { std::string image_to_svg(const uint8_t* data, const int width, const int height, const ImageToSvgConfig& config) { // self deallocate diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index ef262fe68..7de0a1aa1 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -31,8 +31,9 @@ def main(): f.writelines(res_svg) # res_svg2 should match res_svg - cfg = img2num.ImageToSvgConfig() # instanitates defaults - + cfg = img2num.ImageToSvgConfig(km = {"k": 32}) + print(cfg) + # cfg.bilateral_filter.sigma_spatial = 3 # cfg.bilateral_filter.sigma_range = 50 # cfg.kmeans.k = 16 From 3108e75e82a60cf85bcca257f5ad3bb60dd4d12a Mon Sep 17 00:00:00 2001 From: Krasner Date: Fri, 1 May 2026 02:26:17 +0000 Subject: [PATCH 14/35] minor updates --- core/src/internal/bilateral_filter_gpu.cpp | 2 +- core/src/internal/image_to_svg.cpp | 6 +++--- core/src/internal/kmeans_gpu.cpp | 2 +- example-apps/console-py/main.py | 2 +- packages/js/safeWasmWrappers.js | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/internal/bilateral_filter_gpu.cpp b/core/src/internal/bilateral_filter_gpu.cpp index 1239c2df5..4fe41bf58 100644 --- a/core/src/internal/bilateral_filter_gpu.cpp +++ b/core/src/internal/bilateral_filter_gpu.cpp @@ -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(100); + emscripten_sleep(50); #endif result.clear(); result.shrink_to_fit(); diff --git a/core/src/internal/image_to_svg.cpp b/core/src/internal/image_to_svg.cpp index 6b2efeecc..d02459a6b 100644 --- a/core/src/internal/image_to_svg.cpp +++ b/core/src/internal/image_to_svg.cpp @@ -7,9 +7,9 @@ namespace img2num { std::string image_to_svg(const uint8_t* data, const int width, const int height, const ImageToSvgConfig& config) { // self deallocate - std::vector img_data(width * height * 4); - std::vector out_data(width * height * 4); - std::vector out_labels(width * height); + std::vector img_data(static_cast(width) * static_cast(height) * 4); + std::vector out_data(static_cast(width) * static_cast(height) * 4); + std::vector out_labels(static_cast(width) * static_cast(height)); std::memcpy(img_data.data(), data, static_cast(width) * static_cast(height) * 4); diff --git a/core/src/internal/kmeans_gpu.cpp b/core/src/internal/kmeans_gpu.cpp index d947ff2d7..7ff81f084 100644 --- a/core/src/internal/kmeans_gpu.cpp +++ b/core/src/internal/kmeans_gpu.cpp @@ -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(100); + emscripten_sleep(50); #endif } diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index 7de0a1aa1..602da945d 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -31,7 +31,7 @@ def main(): f.writelines(res_svg) # res_svg2 should match res_svg - cfg = img2num.ImageToSvgConfig(km = {"k": 32}) + cfg = img2num.ImageToSvgConfig(km = {"k": 16}) print(cfg) # cfg.bilateral_filter.sigma_spatial = 3 diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index 531e50718..cdd35e630 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -206,10 +206,10 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1 }; /** - * @summary Convert images to SVG contours. + * @summary Convert raster images (e.g., JPEG, PNG) to SVGs. * * @description - * Convert an input image into an SVG. A unification of bilateralFilter, kmeans, and findContours + * Convert an input raster image into an SVG. A unification of `bilateralFilter`, `kmeans`, and `findContours`. * * @async * @function imgToSVG From 608f5577c0b24f36537d65b2ee8713a9084bedc7 Mon Sep 17 00:00:00 2001 From: Krasner Date: Fri, 1 May 2026 02:59:13 +0000 Subject: [PATCH 15/35] fixes --- bindings/py/src/img2num_pybind.cpp | 14 +++++++------- packages/py/img2num/api.py | 4 ---- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 14b31f875..48884369a 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -154,23 +154,23 @@ PYBIND11_MODULE(_img2num, m) { cfg = img2num.ImageToSvgConfig() will use default values */ - auto c = new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); + img2num::ImageToSvgConfig c{img2num::IMAGE_TO_SVG_DEFAULT_CONFIG}; if (bf_dict.contains("sigma_spatial")) - c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); + c.bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); if (bf_dict.contains("sigma_range")) - c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); + c.bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); // 3. Process KMeans overrides from the 'km' dictionary if (km_dict.contains("k")) - c->kmeans.k = km_dict["k"].cast(); + c.kmeans.k = km_dict["k"].cast(); if (km_dict.contains("max_iter")) - c->kmeans.max_iter = km_dict["max_iter"].cast(); + c.kmeans.max_iter = km_dict["max_iter"].cast(); // 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(); + c.min_cluster_area = kwargs["min_cluster_area"].cast(); if (kwargs.contains("color_space")) - c->color_space = kwargs["color_space"].cast(); + c.color_space = kwargs["color_space"].cast(); return c; }), pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 953be4b89..2bb00f480 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -62,10 +62,6 @@ def kmeans(data, k, max_iter, color_space, *, width, height): 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) - @_inject_dims("image") def image_to_svg(image, config, *, width, height): return _image_to_svg(image, width, height, config) \ No newline at end of file From 085ed540e1223b41d39236b06d4ac4bb101ddcea Mon Sep 17 00:00:00 2001 From: Krasner Date: Sun, 3 May 2026 15:04:36 +0000 Subject: [PATCH 16/35] rework c bindings. separate BilateralFilterConfig and KMeansConfig into separate structs. update py bindings --- bindings/c/include/cimg2num.h | 29 +++++++------- bindings/c/src/cimg2num.cpp | 5 +-- bindings/py/src/img2num_pybind.cpp | 61 +++++++++++++++++++++--------- core/include/img2num.h | 30 +++++++-------- example-apps/console-cpp/main.cpp | 2 +- packages/py/img2num/api.py | 2 + 6 files changed, 76 insertions(+), 53 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index bf6881369..9cce6b56a 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -13,26 +13,27 @@ #include #ifdef __cplusplus -#include "img2num.h" extern "C" { -typedef img2num::ImageToSvgConfig ImageToSvgConfig; -#else +#endif + +// mirrors struct definitions in img2num.h +typedef struct img2num_BilateralFilterConfig { + double sigma_spatial; + double sigma_range; +} BilateralFilterConfig; + +typedef struct img2num_KMeansConfig { + int32_t k; + int32_t max_iter; +} KMeansConfig; -typedef struct ImageToSvgConfig { - struct { - double sigma_spatial; - double sigma_range; - } bilateral_filter; +typedef struct img2num_ImageToSvgConfig { + BilateralFilterConfig bilateral_filter; + KMeansConfig kmeans; int min_cluster_area; uint8_t color_space; - struct { - int32_t k; - int32_t max_iter; - } kmeans; } ImageToSvgConfig; -#endif - ImageToSvgConfig* img2num_config_create(); void img2num_config_free(ImageToSvgConfig* config); diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index fec0c0764..e88420232 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -8,8 +8,7 @@ extern "C" { ImageToSvgConfig* img2num_config_create() { - // Returns a copy of your C++ default constant - return new img2num::ImageToSvgConfig(img2num::IMAGE_TO_SVG_DEFAULT_CONFIG); + return reinterpret_cast(new img2num::ImageToSvgConfig); } void img2num_config_free(ImageToSvgConfig* config) { @@ -70,7 +69,7 @@ char *img2num_image_to_svg(const uint8_t *data, const int width, const int heigh char *result{nullptr}; img2num::clear_last_error_and_catch( [&](const uint8_t *d, const int w, const int h, const ImageToSvgConfig *cfg) { - std::string svg{img2num::image_to_svg(d, w, h, *cfg)}; + std::string svg{img2num::image_to_svg(d, w, h, *reinterpret_cast(cfg))}; result = static_cast(std::malloc(svg.size() + 1)); if (!result) { return; // Allocation failed diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 48884369a..165e8ab9d 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -3,6 +3,7 @@ #include #include +#include PYBIND11_MODULE(_img2num, m) { m.doc() = "Python bindings for the img2num C++ library"; @@ -121,27 +122,33 @@ 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"); - pybind11::class_ config(m, "ImageToSvgConfig"); - pybind11::class_(config, - "BilateralFilterConfig") + // ---------------------- Config Structs ---------------------- + + + pybind11::class_ bf_config(m, "BilateralFilterConfig"); + bf_config .def(pybind11::init<>()) .def_readwrite("sigma_spatial", - &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial) + &img2num::BilateralFilterConfig::sigma_spatial) .def_readwrite("sigma_range", - &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range) - .def("__repr__", [](const img2num::ImageToSvgConfig::BilateralFilterConfig &c) { + &img2num::BilateralFilterConfig::sigma_range) + .def("__repr__", [](const img2num::BilateralFilterConfig &c) { return "{'sigma_spatial': " + std::to_string(c.sigma_spatial) + ", 'sigma_range': " + std::to_string(c.sigma_range) + "}"; }); - pybind11::class_(config, "KMeansConfig") + + pybind11::class_ km_config(m, "KMeansConfig"); + km_config .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) { + .def_readwrite("k", &img2num::KMeansConfig::k) + .def_readwrite("max_iter", &img2num::KMeansConfig::max_iter) + .def("__repr__", [](const img2num::KMeansConfig &c) { return "{'k': " + std::to_string(c.k) + ", 'max_iter': " + std::to_string(c.max_iter) + "}"; }); - config + + pybind11::class_ img2num_config(m, "ImageToSvgConfig"); + img2num_config .def(pybind11::init( [](pybind11::dict bf_dict, pybind11::dict km_dict, pybind11::kwargs kwargs) { /* @@ -154,28 +161,46 @@ PYBIND11_MODULE(_img2num, m) { cfg = img2num.ImageToSvgConfig() will use default values */ - img2num::ImageToSvgConfig c{img2num::IMAGE_TO_SVG_DEFAULT_CONFIG}; + + // hand over ownership to python + std::unique_ptr c = std::make_unique(); if (bf_dict.contains("sigma_spatial")) - c.bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); + c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); if (bf_dict.contains("sigma_range")) - c.bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); + c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); // 3. Process KMeans overrides from the 'km' dictionary if (km_dict.contains("k")) - c.kmeans.k = km_dict["k"].cast(); + c->kmeans.k = km_dict["k"].cast(); if (km_dict.contains("max_iter")) - c.kmeans.max_iter = km_dict["max_iter"].cast(); + c->kmeans.max_iter = km_dict["max_iter"].cast(); // 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(); + c->min_cluster_area = kwargs["min_cluster_area"].cast(); if (kwargs.contains("color_space")) - c.color_space = kwargs["color_space"].cast(); + c->color_space = kwargs["color_space"].cast(); return c; }), pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict pybind11::arg("km") = pybind11::dict() // Defaults to empty dict ) + .def(pybind11::init( + [](const img2num::BilateralFilterConfig& bf_cfg, const img2num::KMeansConfig& km_cfg, pybind11::kwargs kwargs) { + + std::unique_ptr c = std::make_unique(); + c->bilateral_filter.sigma_spatial = bf_cfg.sigma_spatial; + c->bilateral_filter.sigma_range = bf_cfg.sigma_range; + c->kmeans.k = km_cfg.k; + c->kmeans.max_iter = km_cfg.max_iter; + + if (kwargs.contains("min_cluster_area")) + c->min_cluster_area = kwargs["min_cluster_area"].cast(); + if (kwargs.contains("color_space")) + c->color_space = kwargs["color_space"].cast(); + + return c; + })) .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) diff --git a/core/include/img2num.h b/core/include/img2num.h index d2983f5c5..4e1593c10 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -15,27 +15,23 @@ /// @note All image buffers are assumed to be stored in row-major order, unless otherwise noted. namespace img2num { -struct ImageToSvgConfig { - struct BilateralFilterConfig { - double sigma_spatial = 3.0; - double sigma_range = 50.0; - } bilateral_filter; +struct BilateralFilterConfig { + double sigma_spatial = 3.0; + double sigma_range = 50.0; +}; + +struct KMeansConfig { + int32_t k = 16; + int32_t max_iter = 100; +}; +struct ImageToSvgConfig { + BilateralFilterConfig bilateral_filter; + KMeansConfig kmeans; int min_cluster_area = 100; uint8_t color_space = 0; - - struct KMeansConfig { - int32_t k = 16; - int32_t max_iter = 100; - } kmeans; }; -const ImageToSvgConfig IMAGE_TO_SVG_DEFAULT_CONFIG = { - .bilateral_filter{.sigma_spatial = 3.0, .sigma_range = 50.0}, - .min_cluster_area = 100, - .color_space = 0, - .kmeans{.k = 16, .max_iter = 100}}; - /// @copydoc IMG2NUM_H_GAUSSIAN_BLUR_DOC void gaussian_blur_fft(uint8_t *image, size_t width, size_t height, double sigma); @@ -64,7 +60,7 @@ std::string labels_to_svg(const uint8_t *data, const int32_t *labels, const int /// @copydoc IMG2NUM_H_IMAGE_TO_SVG_DOC std::string image_to_svg(const uint8_t *data, const int width, const int height, - const ImageToSvgConfig &config = IMAGE_TO_SVG_DEFAULT_CONFIG); + const ImageToSvgConfig &config); } // namespace img2num diff --git a/example-apps/console-cpp/main.cpp b/example-apps/console-cpp/main.cpp index 2d8c10e63..47c6dbe63 100644 --- a/example-apps/console-cpp/main.cpp +++ b/example-apps/console-cpp/main.cpp @@ -66,7 +66,7 @@ int main(int argc, char** argv) { // Generate SVG std::string res_svg{img2num::labels_to_svg(img_data, out_labels, width, height, 100)}; - img2num::ImageToSvgConfig config = img2num::IMAGE_TO_SVG_DEFAULT_CONFIG; + img2num::ImageToSvgConfig config; config.kmeans.k = 32; std::string res_svg2{img2num::image_to_svg(img_data, width, height, config)}; diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 2bb00f480..ab6892ffa 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -10,6 +10,8 @@ kmeans as _kmeans, labels_to_svg as _labels_to_svg, image_to_svg as _image_to_svg, + BilateralFilterConfig, + KMeansConfig, ImageToSvgConfig ) From 1161b53538c6a40b5390628629cc1ced86ccf842 Mon Sep 17 00:00:00 2001 From: Krasner Date: Sun, 3 May 2026 16:49:03 +0000 Subject: [PATCH 17/35] revert to nested struct --- bindings/c/include/cimg2num.h | 21 ++++++------ bindings/py/src/img2num_pybind.cpp | 51 ++++++++---------------------- core/include/img2num.h | 20 +++++------- packages/py/img2num/api.py | 2 -- 4 files changed, 31 insertions(+), 63 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 9cce6b56a..dd7ddacec 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -16,20 +16,17 @@ extern "C" { #endif -// mirrors struct definitions in img2num.h -typedef struct img2num_BilateralFilterConfig { - double sigma_spatial; - double sigma_range; -} BilateralFilterConfig; - -typedef struct img2num_KMeansConfig { - int32_t k; - int32_t max_iter; -} KMeansConfig; +// mirrors struct definition in img2num.h typedef struct img2num_ImageToSvgConfig { - BilateralFilterConfig bilateral_filter; - KMeansConfig kmeans; + struct { + double sigma_spatial; + double sigma_range; + } bilateral_filter; + struct { + int32_t k; + int32_t max_iter; + } kmeans; int min_cluster_area; uint8_t color_space; } ImageToSvgConfig; diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 165e8ab9d..af5d54425 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -123,32 +123,25 @@ PYBIND11_MODULE(_img2num, m) { pybind11::arg("height"), pybind11::arg("min_area"), "Convert labels to SVG string"); // ---------------------- Config Structs ---------------------- - - - pybind11::class_ bf_config(m, "BilateralFilterConfig"); - bf_config + pybind11::class_ config(m, "ImageToSvgConfig"); + pybind11::class_(config, "BilateralFilterConfig") .def(pybind11::init<>()) - .def_readwrite("sigma_spatial", - &img2num::BilateralFilterConfig::sigma_spatial) - .def_readwrite("sigma_range", - &img2num::BilateralFilterConfig::sigma_range) - .def("__repr__", [](const img2num::BilateralFilterConfig &c) { - return "{'sigma_spatial': " + std::to_string(c.sigma_spatial) + - ", 'sigma_range': " + std::to_string(c.sigma_range) + "}"; + .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_ km_config(m, "KMeansConfig"); - km_config + pybind11::class_(config, "KMeansConfig") .def(pybind11::init<>()) - .def_readwrite("k", &img2num::KMeansConfig::k) - .def_readwrite("max_iter", &img2num::KMeansConfig::max_iter) - .def("__repr__", [](const img2num::KMeansConfig &c) { - return "{'k': " + std::to_string(c.k) + ", 'max_iter': " + std::to_string(c.max_iter) + - "}"; + .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) + "}"; }); - pybind11::class_ img2num_config(m, "ImageToSvgConfig"); - img2num_config + config .def(pybind11::init( [](pybind11::dict bf_dict, pybind11::dict km_dict, pybind11::kwargs kwargs) { /* @@ -185,22 +178,6 @@ PYBIND11_MODULE(_img2num, m) { }), pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict pybind11::arg("km") = pybind11::dict() // Defaults to empty dict ) - .def(pybind11::init( - [](const img2num::BilateralFilterConfig& bf_cfg, const img2num::KMeansConfig& km_cfg, pybind11::kwargs kwargs) { - - std::unique_ptr c = std::make_unique(); - c->bilateral_filter.sigma_spatial = bf_cfg.sigma_spatial; - c->bilateral_filter.sigma_range = bf_cfg.sigma_range; - c->kmeans.k = km_cfg.k; - c->kmeans.max_iter = km_cfg.max_iter; - - if (kwargs.contains("min_cluster_area")) - c->min_cluster_area = kwargs["min_cluster_area"].cast(); - if (kwargs.contains("color_space")) - c->color_space = kwargs["color_space"].cast(); - - return c; - })) .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) diff --git a/core/include/img2num.h b/core/include/img2num.h index 4e1593c10..2ecd35e94 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -15,19 +15,15 @@ /// @note All image buffers are assumed to be stored in row-major order, unless otherwise noted. namespace img2num { -struct BilateralFilterConfig { - double sigma_spatial = 3.0; - double sigma_range = 50.0; -}; - -struct KMeansConfig { - int32_t k = 16; - int32_t max_iter = 100; -}; - struct ImageToSvgConfig { - BilateralFilterConfig bilateral_filter; - KMeansConfig kmeans; + struct BilateralFilterConfig { + double sigma_spatial = 3.0; + double sigma_range = 50.0; + } bilateral_filter; + struct KMeansConfig { + int32_t k = 16; + int32_t max_iter = 100; + } kmeans; int min_cluster_area = 100; uint8_t color_space = 0; }; diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index ab6892ffa..2bb00f480 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -10,8 +10,6 @@ kmeans as _kmeans, labels_to_svg as _labels_to_svg, image_to_svg as _image_to_svg, - BilateralFilterConfig, - KMeansConfig, ImageToSvgConfig ) From c1e99ef07a987b5b202af5ab05b7b078bc742a9c Mon Sep 17 00:00:00 2001 From: Krasner Date: Mon, 4 May 2026 02:12:43 +0000 Subject: [PATCH 18/35] Very explicit c to cpp translation in c bindings --- bindings/c/include/cimg2num.h | 7 +++--- bindings/c/src/cimg2num.cpp | 43 ++++++++++++++++++++++++++------ bindings/js/src/wasm_wrapper.cpp | 22 ++++++++-------- example-apps/console-c/main.c | 5 ++-- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index dd7ddacec..eb9eb38d8 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -29,10 +29,9 @@ typedef struct img2num_ImageToSvgConfig { } kmeans; int min_cluster_area; uint8_t color_space; -} ImageToSvgConfig; +} img2num_ImageToSvgConfig; -ImageToSvgConfig* img2num_config_create(); -void img2num_config_free(ImageToSvgConfig* config); +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); @@ -63,7 +62,7 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in /// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_DOC char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, - const ImageToSvgConfig *config); + const img2num_ImageToSvgConfig *config); #ifdef __cplusplus } #endif diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index e88420232..4f9f65308 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -7,12 +7,39 @@ extern "C" { -ImageToSvgConfig* img2num_config_create() { - return reinterpret_cast(new img2num::ImageToSvgConfig); +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 + }; } -void img2num_config_free(ImageToSvgConfig* config) { - delete reinterpret_cast(config); +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 + }; +} + +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) { @@ -65,11 +92,11 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in } char *img2num_image_to_svg(const uint8_t *data, const int width, const int height, - const ImageToSvgConfig *config) { + const img2num_ImageToSvgConfig *config) { char *result{nullptr}; img2num::clear_last_error_and_catch( - [&](const uint8_t *d, const int w, const int h, const ImageToSvgConfig *cfg) { - std::string svg{img2num::image_to_svg(d, w, h, *reinterpret_cast(cfg))}; + [&](const uint8_t *d, const int w, const int h, const img2num_ImageToSvgConfig *cfg) { + std::string svg{img2num::image_to_svg(d, w, h, to_cpp(*config))}; result = static_cast(std::malloc(svg.size() + 1)); if (!result) { return; // Allocation failed @@ -78,5 +105,5 @@ char *img2num_image_to_svg(const uint8_t *data, const int width, const int heigh }, data, width, height, config); return result; -}; +} } diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index 9ae306a17..5394c1ea1 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -44,17 +44,15 @@ EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, co const uint8_t color_space) { /*return img2num_image_to_svg(data, width, height, sigma_spatial, sigma_range, k, max_iter, min_area, color_space);*/ - ImageToSvgConfig* config = img2num_config_create(); - - 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; - - char* svg = img2num_image_to_svg(data, width, height, config); - img2num_config_free(config); - return svg; + 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); } } diff --git a/example-apps/console-c/main.c b/example-apps/console-c/main.c index 35a789691..579f69104 100644 --- a/example-apps/console-c/main.c +++ b/example-apps/console-c/main.c @@ -73,9 +73,8 @@ int main(int argc, char** argv) { // Generate SVG char* res_svg = img2num_labels_to_svg(img_data, out_labels, width, height, 100); - ImageToSvgConfig* config = img2num_config_create(); - char* res_svg2 = img2num_image_to_svg(image_data_original, width, height, config); - img2num_config_free(config); + img2num_ImageToSvgConfig cfg = img2num_ImageToSvgConfig_default(); + char* res_svg2 = img2num_image_to_svg(image_data_original, width, height, &cfg); if (res_svg == NULL) { fprintf(stderr, "Failed to generate SVG step-by-step\n"); From 4fec31ec54b6c2a421fa56515814d00068fcbd57 Mon Sep 17 00:00:00 2001 From: Krasner Date: Mon, 4 May 2026 02:18:52 +0000 Subject: [PATCH 19/35] formatting --- bindings/c/src/cimg2num.cpp | 38 +++----- bindings/py/src/img2num_pybind.cpp | 90 ++++++++++--------- .../2026-05-02-maintainers-expansion/index.md | 42 +++++---- 3 files changed, 84 insertions(+), 86 deletions(-) diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 4f9f65308..4f3bf1627 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -7,34 +7,20 @@ 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_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 - }; +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}; } img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default(void) { diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index af5d54425..c70a18521 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -124,60 +124,64 @@ PYBIND11_MODULE(_img2num, m) { // ---------------------- Config Structs ---------------------- pybind11::class_ config(m, "ImageToSvgConfig"); - pybind11::class_(config, "BilateralFilterConfig") + pybind11::class_(config, + "BilateralFilterConfig") .def(pybind11::init<>()) - .def_readwrite("sigma_spatial", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_spatial) - .def_readwrite("sigma_range", &img2num::ImageToSvgConfig::BilateralFilterConfig::sigma_range) + .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) + "}"; + return "{'sigma_spatial': " + std::to_string(c.sigma_spatial) + + ", 'sigma_range': " + std::to_string(c.sigma_range) + "}"; }); pybind11::class_(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) + "}"; + 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) { - /* - cfg = img2num.ImageToSvgConfig( - bf = {"sigma_spatial": 5.0, "sigma_range": 30.0}, - km = {"k": 32}, - min_cluster_area = 250, - color_space = 1 - ) - - cfg = img2num.ImageToSvgConfig() will use default values - */ - - // hand over ownership to python - std::unique_ptr c = std::make_unique(); - if (bf_dict.contains("sigma_spatial")) - c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); - if (bf_dict.contains("sigma_range")) - c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); - - // 3. Process KMeans overrides from the 'km' dictionary - if (km_dict.contains("k")) - c->kmeans.k = km_dict["k"].cast(); - if (km_dict.contains("max_iter")) - c->kmeans.max_iter = km_dict["max_iter"].cast(); - - // 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(); - if (kwargs.contains("color_space")) - c->color_space = kwargs["color_space"].cast(); - - return c; - }), pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict - pybind11::arg("km") = pybind11::dict() // Defaults to empty dict - ) + .def(pybind11::init([](pybind11::dict bf_dict, pybind11::dict km_dict, + pybind11::kwargs kwargs) { + /* + cfg = img2num.ImageToSvgConfig( + bf = {"sigma_spatial": 5.0, "sigma_range": 30.0}, + km = {"k": 32}, + min_cluster_area = 250, + color_space = 1 + ) + + cfg = img2num.ImageToSvgConfig() will use default values + */ + + // hand over ownership to python + std::unique_ptr c = + std::make_unique(); + if (bf_dict.contains("sigma_spatial")) + c->bilateral_filter.sigma_spatial = bf_dict["sigma_spatial"].cast(); + if (bf_dict.contains("sigma_range")) + c->bilateral_filter.sigma_range = bf_dict["sigma_range"].cast(); + + // 3. Process KMeans overrides from the 'km' dictionary + if (km_dict.contains("k")) c->kmeans.k = km_dict["k"].cast(); + if (km_dict.contains("max_iter")) + c->kmeans.max_iter = km_dict["max_iter"].cast(); + + // 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(); + if (kwargs.contains("color_space")) + c->color_space = kwargs["color_space"].cast(); + + return c; + }), + pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict + pybind11::arg("km") = 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) diff --git a/docs/blog/2026-05-02-maintainers-expansion/index.md b/docs/blog/2026-05-02-maintainers-expansion/index.md index 14c23410d..cfe02f5c7 100644 --- a/docs/blog/2026-05-02-maintainers-expansion/index.md +++ b/docs/blog/2026-05-02-maintainers-expansion/index.md @@ -32,7 +32,7 @@ We're looking for experienced contributors ready to take on scoped, ongoing resp Img2Num is a layered system. Here's how the pieces fit together: -````mermaid +```mermaid flowchart TD A["core/
(C++ Library)"] --> B["bindings/c/
(C ABI Library)"] A --> C["bindings/py/
(Python - pybind11)"] @@ -53,56 +53,64 @@ flowchart TD class A core; class B,D,F layer; class C,E intermediary; -```` +``` -| Layer | Path | Notes | -|---|---|---| -| C++ Core | `core/` | Source of truth for all algorithms | -| C ABI | `bindings/c/` | Stable ABI boundary; feeds the Wasm pipeline | -| WebAssembly | `bindings/js/` | Built via Emscripten from the C ABI | -| JavaScript package | `packages/js/` | Safe runtime wrapper over the Wasm module | -| Python bindings | `bindings/py/` | pybind11 directly on the C++ core | -| Python package | `packages/py/` | Exposes the native Python module | +| Layer | Path | Notes | +| ------------------ | -------------- | -------------------------------------------- | +| C++ Core | `core/` | Source of truth for all algorithms | +| C ABI | `bindings/c/` | Stable ABI boundary; feeds the Wasm pipeline | +| WebAssembly | `bindings/js/` | Built via Emscripten from the C ABI | +| JavaScript package | `packages/js/` | Safe runtime wrapper over the Wasm module | +| Python bindings | `bindings/py/` | pybind11 directly on the C++ core | +| Python package | `packages/py/` | Exposes the native Python module | ## Open Maintainer Areas ### CI/CD + - GitHub Actions workflows, Docker builds, release pipelines, automated publishing. ### C ABI (`bindings/c`) + - Maintain the C ABI over the C++ core. Must stay compatible with external C consumers and the Wasm pipeline. ### JavaScript / WebAssembly (`bindings/js`, `packages/js`) + - Emscripten integration, browser and Node.js compatibility, pnpm workspace tooling, React example app. ### Python (`bindings/py`, `packages/py`) + - pybind11 bindings, uv workspace tooling, console example app. ### Docs & DX (`docs/`, `doxygen/`) + - Docusaurus site, Doxygen integration, tutorials, onboarding experience. ### Releases & Packaging + - Versioning, tagging, GitHub Releases, npm and Docker Hub publishing. ### Testing & Validation + - CI test coverage, linting/formatting enforcement, regression testing. ### Internal Tooling (`scripts/`) + - Cross-platform CLI utilities, developer automation, local workflow improvements. ## Maintainer Tiers -````mermaid +```mermaid flowchart LR A[Junior Maintainer
PR review & learning] --> B[Scoped Maintainer
Domain ownership] B --> C[Core Maintainer
Architecture & final decisions] -```` +``` -| Tier | Merge Access | Responsibilities | -|---|---|---| -| **Junior** | None | Review small PRs, contribute within a defined scope | -| **Scoped** | Within their domain | Own a specific area — review, approve, maintain quality | -| **Core** | Broad | Cross-domain architecture, final merge authority, long-term direction | +| Tier | Merge Access | Responsibilities | +| ---------- | ------------------- | --------------------------------------------------------------------- | +| **Junior** | None | Review small PRs, contribute within a defined scope | +| **Scoped** | Within their domain | Own a specific area — review, approve, maintain quality | +| **Core** | Broad | Cross-domain architecture, final merge authority, long-term direction | Trust is earned through consistent contributions. Everyone starts scoped. From 72b1ba066bf747a160317ec994e5488bc09f8ca0 Mon Sep 17 00:00:00 2001 From: Krasner Date: Mon, 4 May 2026 17:14:08 +0000 Subject: [PATCH 20/35] address pr --- README.md | 2 +- bindings/c/include/cimg2num.h | 3 +-- bindings/c/src/cimg2num.cpp | 34 +++++++++++++++++++++--------- bindings/js/CMakeLists.txt | 2 +- bindings/js/src/wasm_wrapper.cpp | 2 -- bindings/py/src/img2num_pybind.cpp | 12 +---------- doxygen/img2num.h.dox | 26 +++++++++++++++++++++++ example-apps/console-py/main.py | 7 ------ 8 files changed, 54 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index f52e433ea..4341b852a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ [![Open in Codespaces](https://img.shields.io/badge/-Open%20in%20Codespaces-black?logo=github)](https://codespaces.new/Ryan-Millard/Img2Num) - + C++, C, JavaScript, Python _Img2Num_ converts raster images into clean SVGs with _high accuracy and performance_. diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index eb9eb38d8..3aafbb667 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -16,8 +16,7 @@ extern "C" { #endif -// mirrors struct definition in img2num.h - +/// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC typedef struct img2num_ImageToSvgConfig { struct { double sigma_spatial; diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 4f3bf1627..583847757 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -8,19 +8,33 @@ 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}; + 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}; + 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 + }; } img2num_ImageToSvgConfig img2num_ImageToSvgConfig_default(void) { diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index a01408a17..678972c58 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -12,7 +12,7 @@ file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src add_executable(img2num_wasm ${BINDING_SRC}) -target_link_libraries(img2num_wasm PRIVATE CImg2Num Img2Num) +target_link_libraries(img2num_wasm PRIVATE CImg2Num) target_include_directories(img2num_wasm PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.cpp index 5394c1ea1..25b9574d0 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.cpp @@ -42,8 +42,6 @@ EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, co 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);*/ img2num_ImageToSvgConfig config = img2num_ImageToSvgConfig_default(); config.bilateral_filter.sigma_spatial = sigma_spatial; diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index c70a18521..71384e7f8 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -4,6 +4,7 @@ #include #include +#include PYBIND11_MODULE(_img2num, m) { m.doc() = "Python bindings for the img2num C++ library"; @@ -147,17 +148,6 @@ PYBIND11_MODULE(_img2num, m) { config .def(pybind11::init([](pybind11::dict bf_dict, pybind11::dict km_dict, pybind11::kwargs kwargs) { - /* - cfg = img2num.ImageToSvgConfig( - bf = {"sigma_spatial": 5.0, "sigma_range": 30.0}, - km = {"k": 32}, - min_cluster_area = 250, - color_space = 1 - ) - - cfg = img2num.ImageToSvgConfig() will use default values - */ - // hand over ownership to python std::unique_ptr c = std::make_unique(); diff --git a/doxygen/img2num.h.dox b/doxygen/img2num.h.dox index e44d517e3..b699bbcaf 100644 --- a/doxygen/img2num.h.dox +++ b/doxygen/img2num.h.dox @@ -1,3 +1,17 @@ +#define IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC +/// @def IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC +/// @brief Configuration settings to run img2num_image_to_svg. +/// @ingroup IMG2NUM_H +/// @param nilateral_filter.sigma_spatial Standard deviation for spatial Gaussian (proximity weight). +/// @param nilateral_filter.sigma_range Standard deviation for range Gaussian (intensity similarity weight). +/// @param kmeans.k Number of clusters to compute. +/// @param kmeans.max_iter Maximum number of iterations for the algorithm. +/// @param min_cluster_area Minimum area (in pixels) for a region to be included in the SVG. +/// @param color_space Color space flag (0 = CIE LAB, 1 = RGB). +/// @note This applies only to setting bilateral filter and kmeans properties. +/// @note Dox File: `doxygen/img2num.h.dox` +/// + #define IMG2NUM_H_GAUSSIAN_BLUR_DOC /// @def IMG2NUM_H_GAUSSIAN_BLUR_DOC /// @brief Apply a Gaussian blur to an image using FFT. @@ -87,3 +101,15 @@ /// @return std::string A valid SVG string containing the data. /// @note Dox File: `doxygen/img2num.h.dox` /// + +#define IMG2NUM_H_IMAGE_TO_SVG_DOC +/// @def IMG2NUM_H_IMAGE_TO_SVG_DOC +/// @brief Convert labeled regions of an image into an SVG string. +/// @ingroup IMG2NUM_H +/// @param data Pointer to image data buffer. +/// @param width Width of the image in pixels. +/// @param height Height of the image in pixels. +/// @param config img2num_ImageToSvgConfig Configuration Struct +/// @return std::string A valid SVG string containing the data. +/// @note Dox File: `doxygen/img2num.h.dox` +/// diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index 602da945d..aa3bf335c 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -34,13 +34,6 @@ def main(): cfg = img2num.ImageToSvgConfig(km = {"k": 16}) print(cfg) - # cfg.bilateral_filter.sigma_spatial = 3 - # cfg.bilateral_filter.sigma_range = 50 - # cfg.kmeans.k = 16 - # cfg.kmeans.max_iter = 100 - # cfg.min_cluster_size = 100 - # cfg.color_space = 0 - res_svg2 = img2num.image_to_svg(img, cfg) with open(os.path.join(OUTDIR, "result2.svg"),"w") as f: f.writelines(res_svg2) From 51bc81bdb4dde7b9e8817940a03d66a43da7a153 Mon Sep 17 00:00:00 2001 From: Krasner Date: Mon, 4 May 2026 17:41:49 +0000 Subject: [PATCH 21/35] support None config in pybindings - i.e. user calls image_to_svg without giving configs struct --- bindings/py/src/img2num_pybind.cpp | 11 ++++++++--- packages/py/img2num/api.py | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 71384e7f8..47b64ef2e 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -1,10 +1,12 @@ #include #include #include +#include #include #include #include +#include PYBIND11_MODULE(_img2num, m) { m.doc() = "Python bindings for the img2num C++ library"; @@ -192,12 +194,15 @@ PYBIND11_MODULE(_img2num, m) { m.def( "image_to_svg", [](pybind11::array_t data, int width, int height, - const img2num::ImageToSvgConfig &cfg) { + std::optional cfg) { const uint8_t *data_ptr{static_cast(data.request().ptr)}; - std::string svg{img2num::image_to_svg(data_ptr, width, height, cfg)}; + 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("config"), "Convert Image to SVG string"); + pybind11::arg("cfg") = pybind11::none(), + "Convert Image to SVG string"); } diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 2bb00f480..29063e888 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -63,5 +63,6 @@ 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, config, *, width, height): +def image_to_svg(image, *, width, height, config=None): + # if config=None use default configs return _image_to_svg(image, width, height, config) \ No newline at end of file From 491d0b04defbbc3a3d752b9c28445e49307e73c7 Mon Sep 17 00:00:00 2001 From: Krasner Date: Mon, 4 May 2026 17:44:41 +0000 Subject: [PATCH 22/35] image_to_svg config becomes kwarg --- example-apps/console-py/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index aa3bf335c..257a1e0ed 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -34,7 +34,7 @@ def main(): cfg = img2num.ImageToSvgConfig(km = {"k": 16}) print(cfg) - res_svg2 = img2num.image_to_svg(img, cfg) + res_svg2 = img2num.image_to_svg(img, config=cfg) with open(os.path.join(OUTDIR, "result2.svg"),"w") as f: f.writelines(res_svg2) From 7356417248c8c68cfae8fe712a521137fceb9c1b Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 01:56:47 +0000 Subject: [PATCH 23/35] pybind use constructor arguments 'bilateral_filter' and 'kmeans' for Img2NumConfig --- bindings/py/src/img2num_pybind.cpp | 4 ++-- example-apps/console-py/main.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 47b64ef2e..9c759f249 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -171,8 +171,8 @@ PYBIND11_MODULE(_img2num, m) { return c; }), - pybind11::arg("bf") = pybind11::dict(), // Defaults to empty dict - pybind11::arg("km") = pybind11::dict() // Defaults to empty dict + 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) diff --git a/example-apps/console-py/main.py b/example-apps/console-py/main.py index 257a1e0ed..461d91745 100644 --- a/example-apps/console-py/main.py +++ b/example-apps/console-py/main.py @@ -31,7 +31,7 @@ def main(): f.writelines(res_svg) # res_svg2 should match res_svg - cfg = img2num.ImageToSvgConfig(km = {"k": 16}) + cfg = img2num.ImageToSvgConfig(kmeans = {"k": 16}) print(cfg) res_svg2 = img2num.image_to_svg(img, config=cfg) From f6236c73185d858c355842fba8e5268dd8cf2135 Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 02:25:41 +0000 Subject: [PATCH 24/35] revert WasmImageProcessor to run pipeline step by step --- .../react-js/src/components/WasmImageProcessor.jsx | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/example-apps/react-js/src/components/WasmImageProcessor.jsx b/example-apps/react-js/src/components/WasmImageProcessor.jsx index 85ab0b225..370d503ea 100644 --- a/example-apps/react-js/src/components/WasmImageProcessor.jsx +++ b/example-apps/react-js/src/components/WasmImageProcessor.jsx @@ -1,6 +1,6 @@ import { useEffect, useState, useId, useRef, useCallback, useMemo } from "react"; import { Upload } from "lucide-react"; -import { imageToUint8ClampedArray, imgToSVG } from "img2num"; +import { imageToUint8ClampedArray, bilateralFilter, kmeans, findContours} from "img2num"; import GlassCard from "@components/GlassCard"; import styles from "./WasmImageProcessor.module.css"; import { useNavigate } from "react-router-dom"; @@ -74,14 +74,7 @@ const WasmImageProcessor = () => { try { const { width, height } = fileData; - const { svg } = await imgToSVG({ - pixels: fileData.pixels, - width, - height, - num_colors: 16, - }); - - /*step(20); + 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, @@ -103,7 +96,7 @@ const WasmImageProcessor = () => { labels, width, height, - });*/ + }); navigate("/editor", { state: { svg }, From 926f9f3f539f6e1a4a29706fb6b272571afdb2aa Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 02:30:37 +0000 Subject: [PATCH 25/35] format --- example-apps/react-js/src/components/WasmImageProcessor.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example-apps/react-js/src/components/WasmImageProcessor.jsx b/example-apps/react-js/src/components/WasmImageProcessor.jsx index 370d503ea..67c2c7b01 100644 --- a/example-apps/react-js/src/components/WasmImageProcessor.jsx +++ b/example-apps/react-js/src/components/WasmImageProcessor.jsx @@ -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 } from "img2num"; import GlassCard from "@components/GlassCard"; import styles from "./WasmImageProcessor.module.css"; import { useNavigate } from "react-router-dom"; From 2680187984d78d95c2c57b349cc20e56ca3b08fb Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 12:43:54 +0000 Subject: [PATCH 26/35] make wasm_wrapper a C file. Update bindings/js CMakeLists --- bindings/js/CMakeLists.txt | 12 ++++++++---- bindings/js/src/{wasm_wrapper.cpp => wasm_wrapper.c} | 2 -- example-apps/console-c/CMakeLists.txt | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) rename bindings/js/src/{wasm_wrapper.cpp => wasm_wrapper.c} (99%) diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index 678972c58..6e59d7f3e 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -1,14 +1,18 @@ 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_CXX_STANDARD 17) +# set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(CMAKE_C_STANDARD 11) +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") +message(STATUS ${BINDING_SRC}) add_executable(img2num_wasm ${BINDING_SRC}) diff --git a/bindings/js/src/wasm_wrapper.cpp b/bindings/js/src/wasm_wrapper.c similarity index 99% rename from bindings/js/src/wasm_wrapper.cpp rename to bindings/js/src/wasm_wrapper.c index 25b9574d0..ef5cd7143 100644 --- a/bindings/js/src/wasm_wrapper.cpp +++ b/bindings/js/src/wasm_wrapper.c @@ -1,7 +1,6 @@ #include #include -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); @@ -53,4 +52,3 @@ EMSCRIPTEN_KEEPALIVE char *image_to_svg(const uint8_t *data, const int width, co return img2num_image_to_svg(data, width, height, &config); } -} diff --git a/example-apps/console-c/CMakeLists.txt b/example-apps/console-c/CMakeLists.txt index c73945981..f318d071b 100644 --- a/example-apps/console-c/CMakeLists.txt +++ b/example-apps/console-c/CMakeLists.txt @@ -24,4 +24,4 @@ target_compile_definitions(console_c_app PRIVATE ) # Link against the C bindings -target_link_libraries(console_c_app PRIVATE CImg2Num Img2Num m) +target_link_libraries(console_c_app PRIVATE CImg2Num m) From 2514e2d067c9fbd5251627b78f39ffa77c15ef3d Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 12:45:10 +0000 Subject: [PATCH 27/35] small cleanup --- bindings/js/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index 6e59d7f3e..c73c150ed 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -1,9 +1,6 @@ cmake_minimum_required(VERSION 3.16) project(JSBindings LANGUAGES C) -# set(CMAKE_CXX_STANDARD 17) -# set(CMAKE_CXX_STANDARD_REQUIRED ON) - set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) @@ -12,7 +9,6 @@ if(NOT EMSCRIPTEN) endif() file(GLOB_RECURSE BINDING_SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c") -message(STATUS ${BINDING_SRC}) add_executable(img2num_wasm ${BINDING_SRC}) From dcceba03b151d7c31aed18b58abf95058d330891 Mon Sep 17 00:00:00 2001 From: Krasner Date: Tue, 5 May 2026 13:10:38 +0000 Subject: [PATCH 28/35] add python typing hints --- packages/py/img2num/api.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 29063e888..04fcc3c69 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -1,4 +1,6 @@ +from typing import Tuple import numpy as np +import numpy.typing as npt import inspect import functools from ._img2num import ( @@ -35,34 +37,34 @@ def wrapper(*args, **kwargs): @_inject_dims("image") -def gaussian_blur_fft(image, sigma, *, width, height): +def gaussian_blur_fft(image: npt.NDArray[np.uint8], sigma: float, *, width: int, height: int) -> npt.NDArray[np.uint8]: return _gaussian_blur_fft(image, width, height, sigma) @_inject_dims("image") -def invert_image(image, *, width, height): +def invert_image(image: npt.NDArray[np.uint8], *, width: int, height: int) -> npt.NDArray[np.uint8]: return _invert_image(image, width, height) @_inject_dims("image") -def threshold_image(image, num_thresholds, *, width, height): +def threshold_image(image: npt.NDArray[np.uint8], num_thresholds: int, *, width: int, height: int) -> npt.NDArray[np.uint8]: return _threshold_image(image, width, height, num_thresholds) @_inject_dims("image") -def black_threshold_image(image, num_thresholds, *, width, height): +def black_threshold_image(image: npt.NDArray[np.uint8], num_thresholds: int, *, width: int, height: int) -> npt.NDArray[np.uint8]: return _black_threshold_image(image, width, height, num_thresholds) @_inject_dims("image") -def bilateral_filter(image, sigma_spatial, sigma_range, color_space, *, width, height): +def bilateral_filter(image: npt.NDArray[np.uint8], sigma_spatial: float, sigma_range: float, color_space: int, *, width: int, height: int) -> npt.NDArray[np.uint8]: return _bilateral_filter(image, width, height, sigma_spatial, sigma_range, color_space) @_inject_dims("data") -def kmeans(data, k, max_iter, color_space, *, width, height): +def kmeans(data: npt.NDArray[np.uint8], k: int, max_iter: int, color_space: int, *, width: int, height: int) -> Tuple[npt.NDArray[np.uint8], npt.NDArray[int]]: return _kmeans(data, width, height, k, max_iter, color_space) @_inject_dims("data") -def labels_to_svg(data, labels, min_area, *, width, height): +def labels_to_svg(data: npt.NDArray[np.uint8], labels: npt.NDArray[int], min_area: int, *, width: int, height: int) -> str: return _labels_to_svg(data, labels, width, height, min_area) @_inject_dims("image") -def image_to_svg(image, *, width, height, config=None): +def image_to_svg(image: npt.NDArray[np.uint8], *, width: int, height: int, config=None) -> str: # if config=None use default configs return _image_to_svg(image, width, height, config) \ No newline at end of file From d7715d31a40f53a4ee5a177de2ab12119128b99f Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 20:44:32 +0200 Subject: [PATCH 29/35] docs(core): document image_to_svg and its config struct --- core/include/img2num.h | 20 ++++++++++++++++++++ doxygen/img2num.h.dox | 19 +++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/core/include/img2num.h b/core/include/img2num.h index 2ecd35e94..97d361a17 100644 --- a/core/include/img2num.h +++ b/core/include/img2num.h @@ -15,16 +15,36 @@ /// @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; }; diff --git a/doxygen/img2num.h.dox b/doxygen/img2num.h.dox index b699bbcaf..291aee375 100644 --- a/doxygen/img2num.h.dox +++ b/doxygen/img2num.h.dox @@ -1,17 +1,3 @@ -#define IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC -/// @def IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC -/// @brief Configuration settings to run img2num_image_to_svg. -/// @ingroup IMG2NUM_H -/// @param nilateral_filter.sigma_spatial Standard deviation for spatial Gaussian (proximity weight). -/// @param nilateral_filter.sigma_range Standard deviation for range Gaussian (intensity similarity weight). -/// @param kmeans.k Number of clusters to compute. -/// @param kmeans.max_iter Maximum number of iterations for the algorithm. -/// @param min_cluster_area Minimum area (in pixels) for a region to be included in the SVG. -/// @param color_space Color space flag (0 = CIE LAB, 1 = RGB). -/// @note This applies only to setting bilateral filter and kmeans properties. -/// @note Dox File: `doxygen/img2num.h.dox` -/// - #define IMG2NUM_H_GAUSSIAN_BLUR_DOC /// @def IMG2NUM_H_GAUSSIAN_BLUR_DOC /// @brief Apply a Gaussian blur to an image using FFT. @@ -109,7 +95,8 @@ /// @param data Pointer to image data buffer. /// @param width Width of the image in pixels. /// @param height Height of the image in pixels. -/// @param config img2num_ImageToSvgConfig Configuration Struct -/// @return std::string A valid SVG string containing the data. +/// @param config img2num_ImageToSvgConfig Configuration Struct. +/// > See @ref img2num::ImageToSvgConfig. +/// @return std::string An SVG string containing data roughly approximate to the input image. /// @note Dox File: `doxygen/img2num.h.dox` /// From 0161b91cc7c321604901b57e9d8a0a1d03e734a2 Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 21:20:46 +0200 Subject: [PATCH 30/35] docs(packages/js): improve JSDocs in safeWasmWrappers.js --- packages/js/safeWasmWrappers.js | 108 ++++++++++++++++---------------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/packages/js/safeWasmWrappers.js b/packages/js/safeWasmWrappers.js index cdd35e630..504c43556 100644 --- a/packages/js/safeWasmWrappers.js +++ b/packages/js/safeWasmWrappers.js @@ -31,16 +31,16 @@ initWasmWorker(); * * @async * @function gaussianBlur - * @param {Object} __named_parameters - The input options. - * @property {Uint8ClampedArray} __named_parameters.pixels - The image pixel data (flat RGBA array). - * @property {number} __named_parameters.width - The width of the image. - * @property {number} __named_parameters.height - The height of the image. - * @property {number} [__named_parameters.sigma_pixels=width*0.005] - Standard deviation of the Gaussian blur (default=width*0.005; 5% of width). + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array). + * @param {number} options.width - The width of the image. + * @param {number} options.height - The height of the image. + * @param {number} [options.sigma_pixels=width*0.005] - Standard deviation of the Gaussian blur (default=width*0.005; 5% of width). * @returns {Promise} The blurred image pixels. * @throws {Error} If the WASM function fails or memory allocation fails. * @example * const blurred = await gaussianBlur({ pixels, width, height }); - * @todo Consider adding support for multiple channels or alpha-only blurs. + * @todo Fix FFT zero-padding bug around edges of the image. * @variation Standard Gaussian blur using FFT * @since 0.0.0 */ @@ -65,13 +65,13 @@ export const gaussianBlur = async ({ pixels, width, height, sigma_pixels = width * * @async * @function bilateralFilter - * @param {Object} __named_parameters - The input options. - * @property {Uint8ClampedArray} __named_parameters.pixels - The image pixel data (flat RGBA array). - * @property {number} __named_parameters.width - The width of the image. - * @property {number} __named_parameters.height - The height of the image. - * @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.color_space=0] - Color space mode (0: CIE LAB; 1: sRGB). + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array). + * @param {number} options.width - The width of the image. + * @param {number} options.height - The height of the image. + * @param {number} [options.sigma_spatial=3] - Spatial standard deviation. + * @param {number} [options.sigma_range=50] - Range (color) standard deviation. + * @param {number} [options.color_space=0] - Color space mode (0: CIE LAB; 1: sRGB). * @returns {Promise} The filtered image pixels. * @throws {Error} If the WASM function fails. * @example @@ -97,11 +97,11 @@ export const bilateralFilter = async ({ pixels, width, height, sigma_spatial = 3 * * @async * @function blackThreshold - * @param {Object} __named_parameters - The input options. - * @property {Uint8ClampedArray} __named_parameters.pixels - The image pixel data (flat RGBA array). - * @property {number} __named_parameters.width - The width of the image. - * @property {number} __named_parameters.height - The height of the image. - * @property {number} __named_parameters.num_colors - Number of colors to reduce the image to. + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - The image pixel data (flat RGBA array). + * @param {number} options.width - The width of the image. + * @param {number} options.height - The height of the image. + * @param {number} options.num_colors - Number of colors to reduce the image to. * @returns {Promise} The thresholded image pixels. * @throws {Error} If the WASM function fails. * @example @@ -132,15 +132,15 @@ export const blackThreshold = async ({ pixels, width, height, num_colors }) => { * * @async * @function kmeans - * @param {Object} __named_parameters - The input options. - * @property {Uint8ClampedArray} __named_parameters.pixels - Original image pixels. - * @property {Uint8ClampedArray} [__named_parameters.out_pixels=new Uint8ClampedArray(pixels.length)] - Output pixels array. - * @property {Int32Array} [__named_parameters.out_labels=new Int32Array(pixels.length/4)] - Output labels array. - * @property {number} __named_parameters.width - Image width. - * @property {number} __named_parameters.height - Image height. - * @property {number} __named_parameters.num_colors - Number of color clusters. - * @property {number} [__named_parameters.max_iter=100] - Maximum number of iterations. - * @property {number} [__named_parameters.color_space=0] - Color space mode. + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - Original image pixels. + * @param {Uint8ClampedArray} [options.out_pixels=new Uint8ClampedArray(pixels.length)] - Output pixels array. + * @param {Int32Array} [options.out_labels=new Int32Array(pixels.length/4)] - Output labels array. + * @param {number} options.width - Image width. + * @param {number} options.height - Image height. + * @param {number} options.num_colors - Number of color clusters. + * @param {number} [options.max_iter=100] - Maximum number of iterations. + * @param {number} [options.color_space=0] - Color space mode. * @returns {Promise<{pixels: Uint8ClampedArray, labels: Int32Array}>} Clustered pixels and labels. * @throws {Error} If the WASM function fails or iterations do not converge. * @example @@ -178,18 +178,17 @@ export const kmeans = async ({ * * @async * @function findContours - * @param {Object} __named_parameters - The input options. - * @property {Uint8ClampedArray} __named_parameters.pixels - Original image pixels. - * @property {Int32Array} __named_parameters.labels - Label array from clustering (e.g., K-Means) or segmentation. - * @property {number} __named_parameters.width - Image width. - * @property {number} __named_parameters.height - Image height. - * @property {number} [__named_parameters.min_area=100] - Minimum area of a region to be considered a contour. - * @returns {Promise<{svg: string, visualization: Uint8ClampedArray}>} Generated SVG and optionally pixels with visualized contours. + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - Original image pixels. + * @param {Int32Array} options.labels - Label array from clustering (e.g., K-Means) or segmentation. + * @param {number} options.width - Image width. + * @param {number} options.height - Image height. + * @param {number} [options.min_area=100] - Minimum area of a region to be considered a contour. + * @returns {Promise<{svg: string>} Generated SVG. * @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 + * const { svg } = await findContours({ pixels, labels, width, height }); + * @variation Converts labeled (from a clustering algorithm, e.g. K-Means) image into an SVG. * @since 0.0.0 */ export const findContours = async ({ pixels, labels, width, height, min_area = 100 }) => { @@ -202,7 +201,7 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1 ], returnType: "string", }); - return { svg: result.returnValue, visualization: result.output.pixels }; + return { svg: result.returnValue }; }; /** @@ -212,31 +211,30 @@ export const findContours = async ({ pixels, labels, width, height, min_area = 1 * Convert an input raster image into an SVG. A unification of `bilateralFilter`, `kmeans`, and `findContours`. * * @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. + * @function imageToSvg + * @param {Object} options - The input options. + * @param {Uint8ClampedArray} options.pixels - Original image pixels. + * @param {number} options.width - Image width. + * @param {number} options.height - Image height. + * @param {number} [options.sigma_spatial=3] - Spatial standard deviation. + * @param {number} [options.sigma_range=50] - Range (color) standard deviation. + * @param {number} [options.num_colors=16] - Number of color clusters. + * @param {number} [options.max_iter=100] - Maximum number of iterations. + * @param {number} [options.min_area=100] - Minimum area of a region to be considered a contour. + * @param {number} [options.color_space=0] - Color space mode. + * @returns {Promise<{svg: string}>} Generated SVG. * @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 + * const { svg } = await findContours({ pixels, labels, width, height }); + * @variation Convert a raster image (e.g., PNG, JPG) into an SVG. * @since 0.0.0 */ -export const imgToSVG = async ({ pixels, width, height, sigma_spatial = 3, sigma_range = 50, num_colors = 16, max_iter = 100, min_area = 100, color_space = 0 }) => { +export const imageToSvg = async ({ pixels, width, height, sigma_spatial = 3, sigma_range = 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_range, num_colors, max_iter, min_area, color_space }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }], returnType: "string", }); - return { svg: result.returnValue, visualization: result.output.pixels }; + return { svg: result.returnValue }; }; From b3674b07d7158e441f3619f29f4f87d05d5e2882 Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 21:44:24 +0200 Subject: [PATCH 31/35] docs(bindings/c): match core's docs --- bindings/c/include/cimg2num.h | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/bindings/c/include/cimg2num.h b/bindings/c/include/cimg2num.h index 3aafbb667..4da5d36f1 100644 --- a/bindings/c/include/cimg2num.h +++ b/bindings/c/include/cimg2num.h @@ -16,17 +16,36 @@ extern "C" { #endif -/// @copydoc ::IMG2NUM_H_IMAGE_TO_SVG_CONFIG_DOC +/// @brief Configuration options for image_to_svg. +/// @ingroup CIMG2NUM_H typedef struct img2num_ImageToSvgConfig { - struct { + + /// 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; - struct { + + /// 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; From 57aa0d3023b8a72c08de2e7e5288d1551be5c86f Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 21:57:55 +0200 Subject: [PATCH 32/35] fix(bindings/c): NULL tolerance via defaulting in image_to_svg --- bindings/c/src/cimg2num.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bindings/c/src/cimg2num.cpp b/bindings/c/src/cimg2num.cpp index 583847757..74777be72 100644 --- a/bindings/c/src/cimg2num.cpp +++ b/bindings/c/src/cimg2num.cpp @@ -14,7 +14,7 @@ static img2num::ImageToSvgConfig to_cpp(const img2num_ImageToSvgConfig &c) { .sigma_range = c.bilateral_filter.sigma_range }, .kmeans{ - .k = c.kmeans.k, + .k = c.kmeans.k, .max_iter = c.kmeans.max_iter }, .min_cluster_area = c.min_cluster_area, @@ -29,7 +29,7 @@ static img2num_ImageToSvgConfig to_c(const img2num::ImageToSvgConfig &cpp) { .sigma_range = cpp.bilateral_filter.sigma_range }, .kmeans{ - .k = cpp.kmeans.k, + .k = cpp.kmeans.k, .max_iter = cpp.kmeans.max_iter }, .min_cluster_area = cpp.min_cluster_area, @@ -93,17 +93,25 @@ char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const in 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, const img2num_ImageToSvgConfig *cfg) { - std::string svg{img2num::image_to_svg(d, w, h, to_cpp(*config))}; + [&](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(std::malloc(svg.size() + 1)); if (!result) { return; // Allocation failed } std::memcpy(result, svg.c_str(), svg.size() + 1); }, - data, width, height, config); + data, width, height + ); + return result; } } From 42aaeb3caf11d36542310e478075cd38d54225ed Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 22:00:33 +0200 Subject: [PATCH 33/35] build(bindings/js): switch to C18 from C11 --- bindings/js/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index c73c150ed..40f234bf7 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.16) project(JSBindings LANGUAGES C) -set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD 18) set(CMAKE_C_STANDARD_REQUIRED ON) if(NOT EMSCRIPTEN) From 8007a021a9ca48fa5a530a5f5fcf5b727efac17b Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Tue, 5 May 2026 22:07:48 +0200 Subject: [PATCH 34/35] build(bindings/js): switch to C17 from C18 - blame Emscripten --- bindings/js/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/js/CMakeLists.txt b/bindings/js/CMakeLists.txt index 40f234bf7..23e4bc7b8 100644 --- a/bindings/js/CMakeLists.txt +++ b/bindings/js/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.16) project(JSBindings LANGUAGES C) -set(CMAKE_C_STANDARD 18) +set(CMAKE_C_STANDARD 17) set(CMAKE_C_STANDARD_REQUIRED ON) if(NOT EMSCRIPTEN) From 5d7db6ff1f25ad20c66890435a037561efefb220 Mon Sep 17 00:00:00 2001 From: Ryan-Millard Date: Mon, 11 May 2026 18:49:52 +0200 Subject: [PATCH 35/35] refactor(packages/py): move config defaults into Python wrapper from Pybind11 --- bindings/py/src/img2num_pybind.cpp | 18 ++++++++++-------- packages/py/img2num/api.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/bindings/py/src/img2num_pybind.cpp b/bindings/py/src/img2num_pybind.cpp index 9c759f249..5ba50bf6d 100644 --- a/bindings/py/src/img2num_pybind.cpp +++ b/bindings/py/src/img2num_pybind.cpp @@ -194,15 +194,17 @@ PYBIND11_MODULE(_img2num, m) { m.def( "image_to_svg", [](pybind11::array_t data, int width, int height, - std::optional cfg) { - const uint8_t *data_ptr{static_cast(data.request().ptr)}; - const auto& _cfg = cfg.has_value() ? *cfg : img2num::ImageToSvgConfig{}; + const img2num::ImageToSvgConfig& cfg) { - std::string svg{img2num::image_to_svg(data_ptr, width, height, _cfg)}; - pybind11::str svg_py_str(std::move(svg)); - return svg_py_str; + const uint8_t* data_ptr{static_cast(data.request().ptr)}; + + std::string svg{img2num::image_to_svg(data_ptr, width, height, cfg)}; + + return pybind11::str(std::move(svg)); }, - pybind11::arg("data"), pybind11::arg("width"), pybind11::arg("height"), - pybind11::arg("cfg") = pybind11::none(), + pybind11::arg("data"), + pybind11::arg("width"), + pybind11::arg("height"), + pybind11::arg("cfg"), "Convert Image to SVG string"); } diff --git a/packages/py/img2num/api.py b/packages/py/img2num/api.py index 04fcc3c69..09b995941 100644 --- a/packages/py/img2num/api.py +++ b/packages/py/img2num/api.py @@ -66,5 +66,12 @@ def labels_to_svg(data: npt.NDArray[np.uint8], labels: npt.NDArray[int], min_are @_inject_dims("image") def image_to_svg(image: npt.NDArray[np.uint8], *, width: int, height: int, config=None) -> str: - # if config=None use default configs - return _image_to_svg(image, width, height, config) \ No newline at end of file + _config = ImageToSvgConfig() if config is None else config + + return _image_to_svg( + image, + width, + height, + # Use default + _config, + )