Skip to content

Commit 26ec2dd

Browse files
added image rendering support again
1 parent 87a471e commit 26ec2dd

File tree

1 file changed

+109
-3
lines changed

1 file changed

+109
-3
lines changed

Assets/Components/CameraView/Scripts/ImageView.cs

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ protected virtual void ParseHeader(HeaderMsg header)
375375

376376
void OnCompressed(CompressedImageMsg msg)
377377
{
378-
// SetupTex();
379378
ParseHeader(msg.header);
380379

381380
try
@@ -416,8 +415,15 @@ void OnImage(ImageMsg msg)
416415

417416
try
418417
{
419-
// _texture2D.LoadRawTextureData(msg.data);
420-
// _texture2D.Apply();
418+
Texture2D temp = new Texture2D((int)msg.width, (int)msg.height, GetTextureFormat(msg.encoding), false);
419+
temp.LoadRawTextureData(msg.data);
420+
temp.Apply();
421+
RenderTexture.active = _texture2D;
422+
Graphics.Blit(temp, _texture2D);
423+
RenderTexture.active = null;
424+
Destroy(temp);
425+
426+
421427
}
422428
catch (System.Exception e)
423429
{
@@ -426,6 +432,106 @@ void OnImage(ImageMsg msg)
426432
Resize();
427433
}
428434

435+
public static TextureFormat GetTextureFormat(string rosEncoding)
436+
{
437+
switch (rosEncoding.ToLowerInvariant())
438+
{
439+
// 8-bit color
440+
case "rgb8":
441+
return TextureFormat.RGB24;
442+
case "bgr8":
443+
// channel order can be swapped later if needed
444+
return TextureFormat.RGB24;
445+
case "rgba8":
446+
return TextureFormat.RGBA32;
447+
case "bgra8":
448+
return TextureFormat.RGBA32;
449+
450+
// 16-bit color (map to 16-bit-per-channel RGBA when available)
451+
case "rgb16":
452+
case "bgr16":
453+
case "rgba16":
454+
case "bgra16":
455+
// Use RGBA64 (16 bits per channel) when available; preserves bit depth.
456+
return TextureFormat.RGBA64;
457+
458+
// Monochrome
459+
case "mono8":
460+
case "8uc1":
461+
return TextureFormat.R8;
462+
case "mono16":
463+
case "16uc1":
464+
return TextureFormat.R16;
465+
466+
// OpenCV-like 8-bit types
467+
case "8uc2":
468+
// two channels -> use RG16 (two 8-bit channels packed) as best fit
469+
return TextureFormat.RG16;
470+
case "8uc3":
471+
return TextureFormat.RGB24;
472+
case "8uc4":
473+
return TextureFormat.RGBA32;
474+
475+
// Signed/other 8-bit types - map to nearest Unity format
476+
case "8sc1":
477+
return TextureFormat.R8;
478+
case "8sc2":
479+
return TextureFormat.RG16;
480+
case "8sc3":
481+
return TextureFormat.RGB24;
482+
case "8sc4":
483+
return TextureFormat.RGBA32;
484+
485+
// 16-bit OpenCV types
486+
case "16uc2":
487+
case "16sc2":
488+
return TextureFormat.RG16;
489+
case "16uc3":
490+
case "16sc3":
491+
return TextureFormat.RGBA64; // map 3-channel 16-bit to 4-channel 16-bit container
492+
case "16uc4":
493+
case "16sc4":
494+
return TextureFormat.RGBA64;
495+
496+
// 32-bit integer/float types -> use float formats
497+
case "32sc1":
498+
case "32sc2":
499+
case "32sc3":
500+
case "32sc4":
501+
return TextureFormat.RGBAFloat;
502+
case "32fc1":
503+
case "32fc2":
504+
case "32fc3":
505+
case "32fc4":
506+
return TextureFormat.RGBAFloat;
507+
508+
// 64-bit float -> map to float texture (precision loss but usable)
509+
case "64fc1":
510+
case "64fc2":
511+
case "64fc3":
512+
case "64fc4":
513+
return TextureFormat.RGBAFloat;
514+
515+
// Bayer patterns
516+
case "bayer_rggb8":
517+
case "bayer_bggr8":
518+
case "bayer_gbrg8":
519+
case "bayer_grbg8":
520+
// raw 8-bit sensor data
521+
return TextureFormat.R8;
522+
case "bayer_rggb16":
523+
case "bayer_bggr16":
524+
case "bayer_gbrg16":
525+
case "bayer_grbg16":
526+
// raw 16-bit sensor data
527+
return TextureFormat.R16;
528+
529+
default:
530+
// Fallback to a safe, widely supported format
531+
return TextureFormat.RGBA32;
532+
}
533+
}
534+
429535
public override void Deserialize(string data)
430536
{
431537
try

0 commit comments

Comments
 (0)