Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bindings/c/include/cimg2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ char* img2num_labels_to_svg(
char* img2num_image_to_svg(
const uint8_t* data, const int width, const int height, const img2num_ImageToSvgConfig* config
);

void img2num_terminate();

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions bindings/c/src/cimg2num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ char* img2num_image_to_svg(

return result;
}

void img2num_terminate() {
img2num::terminate();
}

}
2 changes: 2 additions & 0 deletions bindings/js/src/wasm_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ EMSCRIPTEN_KEEPALIVE char* image_to_svg(

return img2num_image_to_svg(data, width, height, &config);
}

EMSCRIPTEN_KEEPALIVE void terminate() { img2num_terminate(); };
3 changes: 3 additions & 0 deletions core/include/img2num.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ std::string image_to_svg(
const uint8_t* data, const int width, const int height, const ImageToSvgConfig& config
);

/// @copydoc IMG2NUM_H_TERMINATE_DOC
void terminate();

} // namespace img2num

#endif // IMG2NUM_H
7 changes: 7 additions & 0 deletions core/src/internal/image_to_svg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "img2num.h"
#include "internal/gpu.h"

#include <cstring>
#include <vector>
Expand Down Expand Up @@ -29,4 +30,10 @@ std::string image_to_svg(

return svg;
}

void terminate() {
// force deallocation of GPU if hasn't happened automatically
GPU::getClassInstance().~GPU();
}

} // namespace img2num
3 changes: 3 additions & 0 deletions example-apps/console-c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ int main(int argc, char** argv) {
}

stbi_image_free(image_data_original);

img2num_terminate();

free(img_data);
free(out_data);
free(out_labels);
Expand Down
3 changes: 3 additions & 0 deletions example-apps/console-cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ int main(int argc, char** argv) {
}

stbi_image_free(image_data_original);

img2num::terminate();

delete[] img_data;
delete[] out_data;
delete[] out_labels;
Expand Down
9 changes: 7 additions & 2 deletions example-apps/console-js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from "fs";
import { imageToSvg } from "img2num";
import { imageToSvg, terminate } from "img2num";
import sharp from "sharp";

const imagePath = process.argv[2];
Expand All @@ -23,4 +23,9 @@ const { svg } = await imageToSvg({ pixels, width, height });

writeFileSync("output.svg", svg);
console.log("Done! SVG saved to output.svg");
process.exit();

terminate();

setImmediate(() => {
process.exit(0);
});
7 changes: 6 additions & 1 deletion packages/js/src/safeWasmWrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Each function handles memory management and exposes a JavaScript-friendly API.
*/

import { callWasm, initWasmWorker } from "./wasmClient.js";
import { callWasm, initWasmWorker, terminateWasmWorker } from "./wasmClient.js";

// Ensure worker is ready as soon as this module is imported
await initWasmWorker(); //it's an async function as of #433
Expand Down Expand Up @@ -240,3 +240,8 @@ export const imageToSvg = async ({ pixels, width, height, sigma_spatial = 3, sig
});
return { svg: result.returnValue };
};

export const terminate = async () => {
await callWasm({funcName: "terminate"});
await terminateWasmWorker();
}
10 changes: 9 additions & 1 deletion packages/js/src/wasmClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,16 @@ export async function callWasm({ funcName, args = {}, bufferKeys = [], returnTyp
*
* @since 0.0.0
*/
export function terminateWasmWorker() {
export async function terminateWasmWorker() {
if (!worker) return;
if (__TARGET__ === "node") {
const { initWebGPU, destroyWebGPU } = await import("./target/node/webgpu.js");
try {
await destroyWebGPU();
} catch (err) {
console.error(`[Img2Num node/wasmClient.js terminateWasmWorker] Error: ${err}`);
}
}
worker.terminate();
// Reject any pending calls before clearing
for (const [_id, cb] of callbacks) {
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/workers/wasmWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ if (__TARGET__ === "node") {
// (Node passes the raw payload directly, no nested event wrapper needed)
parentPort.on("message", async (data) => {
await handleMessage(data);
await destroyWebGPU();
parentPort.close();
// await destroyWebGPU();
// parentPort.close();
});
} else {
// Browser Worker setup: Standard event-unwrapping listener
Expand Down