@@ -16,9 +16,11 @@ namespace SRanipalExtTrackingInterface
1616{
1717 public class SRanipalExtTrackingInterface : ExtTrackingModule
1818 {
19- LipData_v2 lipData = default ;
20- EyeData_v2 eyeData = default ;
21- private static bool eyeEnabled = false , lipEnabled = false , isViveProEye = false ;
19+ LipData_v2 lipData ;
20+ EyeData_v2 eyeData ;
21+ private static bool eyeEnabled , lipEnabled , isViveProEye ;
22+ private static byte [ ] eyeImageCache , lipImageCache ;
23+
2224 // Kernel32 SetDllDirectory
2325 [ DllImport ( "kernel32.dll" , CharSet = CharSet . Unicode ) ]
2426 private static extern bool SetDllDirectory ( string lpPathName ) ;
@@ -107,17 +109,22 @@ public override (bool eyeSuccess, bool expressionSuccess) Initialize(bool eyeAva
107109 }
108110
109111 UnifiedTracking . EyeImageData . ImageSize = ( 200 , 100 ) ;
112+ UnifiedTracking . EyeImageData . ImageData = new byte [ 200 * 100 * 4 ] ;
113+ eyeImageCache = new byte [ 200 * 100 ] ;
110114 }
111115 }
112116
113117 if ( lipEnabled )
114118 {
115119 UnifiedTracking . LipImageData . SupportsImage = true ;
116120 UnifiedTracking . LipImageData . ImageSize = ( SRanipal_Lip_v2 . ImageWidth , SRanipal_Lip_v2 . ImageHeight ) ;
117- UnifiedTracking . LipImageData . ImageData = new byte [ UnifiedTracking . LipImageData . ImageSize . x *
118- UnifiedTracking . LipImageData . ImageSize . y ] ;
121+ UnifiedTracking . LipImageData . ImageData = new byte [ SRanipal_Lip_v2 . ImageWidth *
122+ SRanipal_Lip_v2 . ImageHeight * 4 ] ;
123+
119124 lipData . image = Marshal . AllocCoTaskMem ( UnifiedTracking . LipImageData . ImageSize . x *
120125 UnifiedTracking . LipImageData . ImageSize . x ) ;
126+
127+ lipImageCache = new byte [ SRanipal_Lip_v2 . ImageWidth * SRanipal_Lip_v2 . ImageHeight ] ;
121128 }
122129
123130 ModuleInformation = new ModuleMetadata ( )
@@ -199,13 +206,13 @@ private static bool Attach()
199206 return true ;
200207 }
201208
202- private static byte [ ] ReadMemory ( IntPtr offset , int size ) {
203- var buffer = new byte [ size ] ;
204-
209+ private static byte [ ] ReadMemory ( IntPtr offset , ref byte [ ] buf ) {
205210 var bytesRead = 0 ;
206- Utils . ReadProcessMemory ( ( int ) _processHandle , offset , buffer , size , ref bytesRead ) ;
211+ var size = buf . Length ;
212+
213+ Utils . ReadProcessMemory ( ( int ) _processHandle , offset , buf , size , ref bytesRead ) ;
207214
208- return bytesRead != size ? null : buffer ;
215+ return bytesRead != size ? null : buf ;
209216 }
210217
211218 private Error UpdateEye ( )
@@ -218,7 +225,7 @@ private Error UpdateEye()
218225 if ( _processHandle == IntPtr . Zero || ! UnifiedTracking . EyeImageData . SupportsImage ) return updateResult ;
219226
220227 // Read 20000 image bytes from the predefined offset. 10000 bytes per eye.
221- var imageBytes = ReadMemory ( _offset , 20000 ) ;
228+ var imageBytes = ReadMemory ( _offset , ref eyeImageCache ) ;
222229
223230 // Concatenate the two images side by side instead of one after the other
224231 byte [ ] leftEye = new byte [ 10000 ] ;
@@ -235,9 +242,21 @@ private Error UpdateEye()
235242 // Add 100 bytes from the right eye to the right side of the image
236243 Array . Copy ( rightEye , i * 100 , imageBytes , leftIndex + 100 , 100 ) ;
237244 }
238-
239- // Write the image to the latest eye data
240- UnifiedTracking . EyeImageData . ImageData = imageBytes ;
245+
246+ for ( int y = 0 ; y < 100 ; y ++ )
247+ {
248+ for ( int x = 0 ; x < 200 ; x ++ )
249+ {
250+ byte grayscaleValue = imageBytes [ y * 200 + x ] ;
251+
252+ // Set the R, G, B, and A channels to the grayscale value
253+ int index = ( y * 200 + x ) * 4 ;
254+ UnifiedTracking . EyeImageData . ImageData [ index + 0 ] = grayscaleValue ; // R
255+ UnifiedTracking . EyeImageData . ImageData [ index + 1 ] = grayscaleValue ; // G
256+ UnifiedTracking . EyeImageData . ImageData [ index + 2 ] = grayscaleValue ; // B
257+ UnifiedTracking . EyeImageData . ImageData [ index + 3 ] = 255 ; // A (fully opaque)
258+ }
259+ }
241260
242261 return updateResult ;
243262 }
@@ -324,8 +343,23 @@ private Error UpdateMouth()
324343
325344 if ( lipData . image == IntPtr . Zero || ! UnifiedTracking . LipImageData . SupportsImage ) return updateResult ;
326345
327- Marshal . Copy ( lipData . image , UnifiedTracking . LipImageData . ImageData , 0 , UnifiedTracking . LipImageData . ImageSize . x *
346+ Marshal . Copy ( lipData . image , lipImageCache , 0 , UnifiedTracking . LipImageData . ImageSize . x *
328347 UnifiedTracking . LipImageData . ImageSize . y ) ;
348+
349+ for ( int y = 0 ; y < 400 ; y ++ )
350+ {
351+ for ( int x = 0 ; x < 800 ; x ++ )
352+ {
353+ byte grayscaleValue = lipImageCache [ y * 800 + x ] ;
354+
355+ // Set the R, G, B, and A channels to the grayscale value
356+ int index = ( y * 800 + x ) * 4 ;
357+ UnifiedTracking . LipImageData . ImageData [ index + 0 ] = grayscaleValue ; // R
358+ UnifiedTracking . LipImageData . ImageData [ index + 1 ] = grayscaleValue ; // G
359+ UnifiedTracking . LipImageData . ImageData [ index + 2 ] = grayscaleValue ; // B
360+ UnifiedTracking . LipImageData . ImageData [ index + 3 ] = 255 ; // A (fully opaque)
361+ }
362+ }
329363
330364 return updateResult ;
331365 }
0 commit comments