UNPKG

img2num

Version:

Img2Num is a raster vectorization library - it converts images to SVGs

180 lines (179 loc) 4.44 kB
//#region src/imageToUint8ClampedArray.js function e(e) { return new Promise((t) => { let n = new Image(); n.onload = () => { let e = document.createElement("canvas"); e.width = n.width, e.height = n.height; let r = e.getContext("2d"); r.drawImage(n, 0, 0); let { data: i } = r.getImageData(0, 0, n.width, n.height); t({ pixels: i, width: n.width, height: n.height }); }, n.src = URL.createObjectURL(e); }); } //#endregion //#region src/target/browser/worker.js async function t() { let e = new Worker(new URL( /* @vite-ignore */ "" + new URL("wasmWorker-BVnzGxvB.js", import.meta.url).href, "" + import.meta.url ), { type: "module" }); return { postMessage: (t) => e.postMessage(t), onMessage: (t) => e.onmessage = (e) => t(e.data), onError: (t) => e.onerror = t, terminate: () => e.terminate() }; } //#endregion //#region src/wasmClient.js var n, r = 0, i = /* @__PURE__ */ new Map(), a = !1; function o(e) { let t = i.get(e.id); t && (i.delete(e.id), e.error ? t.reject(Error(e.error)) : t.resolve({ output: e.output, returnValue: e.returnValue })); } async function s() { a ||= (n = await t(), n.onMessage(o), n.onError((e) => { let t = e.message || "WASM worker error", n = /* @__PURE__ */ Error(`[Img2Num wasmClient] Error: ${t}`); for (let [e, t] of i) t.reject(n); i.clear(); }), !0); } async function c({ funcName: e, args: t = {}, bufferKeys: o = [], returnType: s = "void" }) { if (!a) throw Error("WASM worker not initialized. Call initWasmWorker() first."); let c = r++; return new Promise((r, a) => { i.set(c, { resolve: r, reject: a }), n.postMessage({ id: c, funcName: e, args: t, bufferKeys: o, returnType: s }); }); } //#endregion //#region src/safeWasmWrappers.js await s(); var l = async ({ pixels: e, width: t, height: n, sigma_pixels: r = t * .005 }) => (await c({ funcName: "gaussian_blur_fft", args: { pixels: e, width: t, height: n, sigma_pixels: r }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }] })).output.pixels, u = async ({ pixels: e, width: t, height: n, sigma_spatial: r = 3, sigma_range: i = 50, color_space: a = 0 }) => (await c({ funcName: "bilateral_filter", args: { pixels: e, width: t, height: n, sigma_spatial: r, sigma_range: i, color_space: a }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }] })).output.pixels, d = async ({ pixels: e, width: t, height: n, num_colors: r }) => (await c({ funcName: "black_threshold_image", args: { pixels: e, width: t, height: n, num_colors: r }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }] })).output.pixels, f = async ({ pixels: e, out_pixels: t = new Uint8ClampedArray(e.length), out_labels: n = new Int32Array(e.length / 4), width: r, height: i, num_colors: a, max_iter: o = 100, color_space: s = 0 }) => { let l = await c({ funcName: "kmeans", args: { pixels: e, out_pixels: t, out_labels: n, width: r, height: i, num_colors: a, max_iter: o, color_space: s }, bufferKeys: [ { key: "pixels", type: "Uint8ClampedArray" }, { key: "out_pixels", type: "Uint8ClampedArray" }, { key: "out_labels", type: "Int32Array" } ] }); return { pixels: l.output.out_pixels, labels: l.output.out_labels }; }, p = async ({ pixels: e, labels: t, width: n, height: r, min_area: i = 100, min_thickness: a = 10 }) => ({ svg: (await c({ funcName: "labels_to_svg", args: { pixels: e, labels: t, width: n, height: r, min_area: i, min_thickness: a }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }, { key: "labels", type: "Int32Array" }], returnType: "string" })).returnValue }), m = async ({ pixels: e, width: t, height: n, sigma_spatial: r = 3, sigma_range: i = 50, num_colors: a = 16, max_iter: o = 100, min_area: s = 100, min_thickness: l = 10, color_space: u = 0 }) => ({ svg: (await c({ funcName: "image_to_svg", args: { pixels: e, width: t, height: n, sigma_spatial: r, sigma_range: i, num_colors: a, max_iter: o, min_area: s, min_thickness: l, color_space: u }, bufferKeys: [{ key: "pixels", type: "Uint8ClampedArray" }], returnType: "string" })).returnValue }); //#endregion export { u as bilateralFilter, d as blackThreshold, p as findContours, l as gaussianBlur, m as imageToSvg, e as imageToUint8ClampedArray, f as kmeans };