@@ -89,6 +89,29 @@ export class ImageViewPanel {
8989 }
9090 }
9191
92+ public static handleOpenCVVariableFromContext ( debugContext : {
93+ container : {
94+ expensive : boolean ;
95+ name : string ;
96+ variablesReference : number ;
97+ } ;
98+ sessionId : string ;
99+ variable : {
100+ evaluateName : string ;
101+ memoryReference : string ;
102+ name : string ;
103+ value : string ;
104+ variablesReference : number ;
105+ type : string ;
106+ } ;
107+ } ) {
108+ if ( ImageViewPanel . instance ) {
109+ ImageViewPanel . instance . handleExtractOpenCVImageProperties (
110+ debugContext . variable
111+ ) ;
112+ }
113+ }
114+
92115 private constructor ( panel : vscode . WebviewPanel , extensionPath : string ) {
93116 this . panel = panel ;
94117 this . extensionPath = extensionPath ;
@@ -506,6 +529,142 @@ export class ImageViewPanel {
506529 }
507530 }
508531
532+ private async handleExtractOpenCVImageProperties ( variableName : {
533+ evaluateName : string ;
534+ memoryReference : string ;
535+ name : string ;
536+ value : string ;
537+ variablesReference : number ;
538+ type : string ;
539+ } ) {
540+ try {
541+ const session = vscode . debug . activeDebugSession ;
542+ if ( ! session ) {
543+ this . panel . webview . postMessage ( {
544+ command : "showError" ,
545+ error : "No active debug session found" ,
546+ } ) ;
547+ return ;
548+ }
549+
550+ // Check if the variable is of type cv::Mat
551+ if ( variableName . type . indexOf ( "cv::Mat" ) === - 1 && variableName . type . indexOf ( "Mat" ) === - 1 ) {
552+ this . panel . webview . postMessage ( {
553+ command : "showError" ,
554+ error : `Variable ${ variableName . name } is not of type cv::Mat` ,
555+ } ) ;
556+ return ;
557+ }
558+
559+ // Get the Mat object's children
560+ const matChildren = await session . customRequest ( "variables" , {
561+ variablesReference : variableName . variablesReference ,
562+ } ) ;
563+
564+ if ( ! matChildren || ! matChildren . variables ) {
565+ this . panel . webview . postMessage ( {
566+ command : "showError" ,
567+ error : `No children found for variable ${ variableName . name } ` ,
568+ } ) ;
569+ return ;
570+ }
571+
572+ // Extract OpenCV Mat properties
573+ const imageProperties : ImageWithDimensionsElement = {
574+ name : variableName . name ,
575+ data : new Uint8Array ( ) ,
576+ width : 0 ,
577+ height : 0 ,
578+ format : 0 ,
579+ } ;
580+
581+ // Extract rows, cols, and data from the Mat structure
582+ matChildren . variables . forEach ( ( child : any ) => {
583+ if ( child . name === "rows" ) {
584+ imageProperties . height = parseInt ( child . value , 10 ) ;
585+ } else if ( child . name === "cols" ) {
586+ imageProperties . width = parseInt ( child . value , 10 ) ;
587+ } else if ( child . name === "data" ) {
588+ const match = child . value . match ( / 0 x [ 0 - 9 a - f A - F ] + / ) ;
589+ if ( match ) {
590+ imageProperties . dataAddress = match [ 0 ] ;
591+ }
592+ } else if ( child . name === "step" ) {
593+ // step[0] contains bytes per row
594+ // We'll need to get the step array children
595+ }
596+ } ) ;
597+
598+ // Get step array to determine bytes per row
599+ const stepObj = matChildren . variables . find (
600+ ( child : any ) => child . name === "step"
601+ ) ;
602+
603+ let bytesPerRow = 0 ;
604+ if ( stepObj ) {
605+ const stepChildren = await session . customRequest ( "variables" , {
606+ variablesReference : stepObj . variablesReference ,
607+ } ) ;
608+
609+ if ( stepChildren && stepChildren . variables ) {
610+ // step[0] is bytes per row
611+ const step0 = stepChildren . variables . find ( ( child : any ) => child . name === "[0]" ) ;
612+ if ( step0 ) {
613+ bytesPerRow = parseInt ( step0 . value , 10 ) ;
614+ }
615+ }
616+ }
617+
618+ // Calculate data size: rows * bytes_per_row
619+ if ( imageProperties . height > 0 && bytesPerRow > 0 ) {
620+ imageProperties . dataSize = imageProperties . height * bytesPerRow ;
621+ } else {
622+ // Fallback: estimate based on common OpenCV formats
623+ // Assume 3 channels (BGR) if we can't determine
624+ imageProperties . dataSize = imageProperties . width * imageProperties . height * 3 ;
625+ }
626+
627+ // Validate that we have the required data
628+ if ( ! imageProperties . dataAddress || ! imageProperties . dataSize ||
629+ imageProperties . width <= 0 || imageProperties . height <= 0 ) {
630+ this . panel . webview . postMessage ( {
631+ command : "showError" ,
632+ error : `Could not extract complete OpenCV Mat properties from variable ${ variableName . name } . Width: ${ imageProperties . width } , Height: ${ imageProperties . height } , DataSize: ${ imageProperties . dataSize } ` ,
633+ } ) ;
634+ return ;
635+ }
636+
637+ // Read memory data
638+ const readResponse = await session . customRequest ( "readMemory" , {
639+ memoryReference : imageProperties . dataAddress ,
640+ count : imageProperties . dataSize ,
641+ } ) ;
642+
643+ if ( readResponse && readResponse . data ) {
644+ const binaryData = Buffer . from ( readResponse . data , "base64" ) ;
645+ imageProperties . data = new Uint8Array ( binaryData ) ;
646+
647+ // OpenCV Mat typically uses BGR888 format (3 bytes per pixel)
648+ // Map this to our bgr888 format
649+ imageProperties . format = 0x0E ; // Map to BGR888 equivalent
650+
651+ // Update the panel title and send the data
652+ this . panel . title = `Image Viewer: ${ variableName . name } (OpenCV Mat)` ;
653+ this . sendImageWithDimensionsData ( imageProperties ) ;
654+ } else {
655+ this . panel . webview . postMessage ( {
656+ command : "showError" ,
657+ error : `Could not read memory data for variable ${ variableName . name } ` ,
658+ } ) ;
659+ }
660+ } catch ( error ) {
661+ this . panel . webview . postMessage ( {
662+ command : "showError" ,
663+ error : `Error extracting OpenCV Mat image properties: ${ error } ` ,
664+ } ) ;
665+ }
666+ }
667+
509668 private getHtmlContent ( webview : vscode . Webview ) : string {
510669 const scriptPath = webview . asWebviewUri (
511670 vscode . Uri . file (
0 commit comments