// TypeScript wrapper for SVGit4Me: VTracer (color) and Potrace (B/W) // Assumes vtracer_webapp.js, vtracer_webapp_bg.wasm, and vtracer_webapp.d.ts are in '../wasm/' // No static import of vtracer glue code! // Temporary module declaration for esm-potrace-wasm types // Remove this when upstream types are compatible with your tsconfig // @ts-ignore declare module 'esm-potrace-wasm'; // Utility to convert a File/Blob to ArrayBuffer async function fileToArrayBuffer(file: File | Blob): Promise { return await file.arrayBuffer(); } export type SVGit4MeOptions = { color?: boolean; // true = color (VTracer), false = B/W (Potrace) color_mode?: 'color' | 'bw'; // alternative to color wasmPath?: string; // custom path to vtracer_webapp_bg.wasm potraceOptions?: any; // options for Potrace // VTracer and Potrace options can be passed through [key: string]: any; }; /** * Convert PNG/JPG to SVG using VTracer (color) or Potrace (B/W) * @param image File, Blob, or ArrayBuffer * @param options SVGit4MeOptions (use 'wasmPath' to customize WASM location for VTracer) * @returns SVG string */ export async function convertToSVG( image: File | Blob | ArrayBuffer, options: SVGit4MeOptions = {} ): Promise { // Decide which engine to use const useColor = options.color === true || options.color_mode === 'color'; const useBW = options.color === false || options.color_mode === 'bw'; // Convert input to ArrayBuffer if needed const buffer = image instanceof ArrayBuffer ? image : await fileToArrayBuffer(image); if (useBW) { // Use esm-potrace-wasm for B/W tracing // Dynamically import to ensure ESM compatibility const { trace } = await import('esm-potrace-wasm'); // Potrace expects Uint8Array (image data) const uint8 = new Uint8Array(buffer); // Pass options.potraceOptions or options directly const svgString = await trace(uint8, options.potraceOptions || options); return svgString; } else { // Use VTracer for color (default) let vtracerInit: any; let ColorImageConverter: any; if (typeof window !== 'undefined' && (window as any).ColorImageConverter) { // Loaded globally (e.g. via