license fileoverview import { DEBUG_LEVEL , log } from '../utils/logger.js' ; import { Module } from './planar-defs.js' ; import { WasmHeapWriter } from './wasm-heap-writer.js' ; declare global { const Module ; } /**
 * A class for processing image frames and returning any planar target
 * detections.
 */ export class PlanarTargetDetector { private pixelsPtr : number | null ; private pixelsSize : number ; private hasLoaded = false ; constructor ( ) { this . pixelsPtr = null ; this . pixelsSize = 0 ; Module . preRun . push ( ( ) => { this . hasLoaded = true ; } ) ; } param { ! ImageData } imageData param { number } timestamp return { ! QuadVec } process ( imageData , timestamp ) { if ( ! this . hasLoaded ) { return { get : ( r ) => ( { id : 0 } ) , size : ( ) => 0 , } ; } const width = imageData . width ; const height = imageData . height ; const size = 4 * width * height ; if ( this . pixelsSize !== size ) { if ( this . pixelsPtr ) { Module . _free ( this . pixelsPtr ) ; } this . pixelsPtr = Module . _malloc ( size ) ; this . pixelsSize = size ; } if ( this . pixelsPtr === null ) { throw new Error ( 'Unable to reserve pixel pointer with malloc' ) ; } Module . HEAPU8 . set ( imageData . data , this . pixelsPtr ) ; const wasmHeapWriterByteCount = 24 ; const frameDataWriter = new WasmHeapWriter ( wasmHeapWriterByteCount ) ; frameDataWriter . writeInt32 ( 0 ) ; frameDataWriter . writeInt32 ( width ) ; frameDataWriter . writeInt32 ( height ) ; frameDataWriter . writePtr ( this . pixelsPtr ) ; frameDataWriter . writeFloat64 ( timestamp ) ; const frameDataPtr = frameDataWriter . getData ( ) ; if ( frameDataPtr === null ) { throw new Error ( 'Unable to obtain data' ) ; } const outputVec = Module . process ( frameDataPtr ) ; Module . _free ( frameDataPtr ) ; return outputVec ; } param { number } objectId param { ! Uint8Array } detectorIndexData addDetectionWithId ( objectId , detectorIndexData ) { if ( ! this . hasLoaded ) { log ( 'Cannot add detection until detection has started.' , DEBUG_LEVEL . ERROR ) ; return ; } const size = detectorIndexData . length ; const indexPtr = Module . _malloc ( size ) ; if ( indexPtr === null ) { log ( 'Unable to reserve memory' , DEBUG_LEVEL . ERROR , 'Planar Image Detector' ) ; return ; } Module . HEAPU8 . set ( detectorIndexData , indexPtr ) ; Module . _addObjectIndexWithId ( objectId , size , indexPtr ) ; Module . _free ( indexPtr ) ; } param { ! Uint8Array } detectorIndexData addDetection ( detectorIndexData ) { this . addDetectionWithId ( - 1 , detectorIndexData ) ; } param { number } objectId cancelDetection ( objectId ) { if ( ! this . hasLoaded ) { log ( 'Cannot cancel detection until detection has started.' , DEBUG_LEVEL . ERROR ) ; return ; } Module . _cancelObjectId ( objectId ) ; } }