import { WorkerCommand , WorkerResponse , OpenCVConfig , OpenCVOperatipnParams , Wasm } from "./const" ; export let Module = { } ; const ctx = self as any ; let wasm = null ; const predict = async ( data , config , params ) => { const inputImageBufferOffset = wasm ! . _getInputImageBufferOffset ( ) ; wasm ! . HEAPU8 . set ( data , inputImageBufferOffset ) ; if ( params . type === "Blur" ) { wasm ! . _blur ( params . processWidth , params . processHeight , params . blurParams ! . kernelSize ) ; } else if ( params . type === "GausianBlur" ) { wasm ! . _gaussianBlur ( params . processWidth , params . processHeight , params . gausianBlurParams ! . kernelSize , params . gausianBlurParams ! . sigma ) ; } else if ( params . type === "Canny" ) { wasm ! . _canny ( params . processWidth , params . processHeight , params . cannyParams ! . threshold1 , params . cannyParams ! . threshold2 , params . cannyParams ! . apertureSize , params . cannyParams ! . L2gradient ) ; } const outputImageBufferOffset = wasm ! . _getOutputImageBufferOffset ( ) ; const converted = new ImageData ( new Uint8ClampedArray ( wasm ! . HEAPU8 . slice ( outputImageBufferOffset , outputImageBufferOffset + params . processWidth * params . processHeight * 4 ) ) , params . processWidth , params . processHeight ) ; return converted . data ; } ; onmessage = async ( event ) => { if ( event . data . message === WorkerCommand . INITIALIZE ) { const config = event . data . config as OpenCVConfig ; if ( config . useSimd ) { const modSimd = require ( "../resources/custom_opencv-simd.js" ) ; const b = Buffer . from ( config . wasmSimdBase64 ! , "base64" ) ; wasm = await modSimd ( { wasmBinary : b } ) ; } else { const mod = require ( "../resources/custom_opencv.js" ) ; const b = Buffer . from ( config . wasmBase64 ! , "base64" ) ; wasm = await mod ( { wasmBinary : b } ) ; } console . log ( "initialized opencv worker" ) ; ctx . postMessage ( { message : WorkerResponse . INITIALIZED } ) ; } else if ( event . data . message === WorkerCommand . PREDICT ) { const data = event . data . data ; const uid = event . data . uid ; const config = event . data . config ; const params = event . data . params ; const imageData = await predict ( data , config , params ) ; ctx . postMessage ( { message : WorkerResponse . PREDICTED , uid : uid , converted : imageData } , [ imageData . buffer ] ) ; } } ; module . exports = [ ctx ] ;