Class MessageExtensions
Extension and utility methods for common ROS message types, including image conversion, camera projection, and sensor message helpers.
Inherited Members
Namespace: Unity.SimulationPro.Foundation
Assembly: Unity.SimulationPro.Foundation.dll
Syntax
public static class MessageExtensions
Methods
ApplyCameraInfoProjection(Texture2D, CameraInfoMsg)
Creates a new Texture2D by remapping each pixel of tex through the
inverse K matrix described in cameraInfo.
Declaration
public static Texture2D ApplyCameraInfoProjection(this Texture2D tex, CameraInfoMsg cameraInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | tex | The source texture to remap. |
| CameraInfoMsg | cameraInfo | The camera intrinsics message providing the K matrix. |
Returns
| Type | Description |
|---|---|
| Texture2D | A new Texture2D with the projection applied. |
ColorRGBAMsg(Color)
Converts a Unity Color to a ColorRGBAMsg(Color).
Declaration
public static ColorRGBAMsg ColorRGBAMsg(this Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | color | RGBA color. |
Returns
| Type | Description |
|---|---|
| ColorRGBAMsg | The equivalent ColorRGBAMsg(Color). |
DebayerConvert(ImageMsg, bool)
Converts a Bayer-encoded ImageMsg in-place to RGBA, optionally flipping the image vertically.
Declaration
public static void DebayerConvert(this ImageMsg image, bool flipY = true)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The Bayer-encoded image message to convert in-place. |
| bool | flipY | Whether to flip the image vertically before debayering. Defaults to true. |
Remarks
Supports all eight ROS Bayer pattern encodings (rggb/bggr/gbrg/grbg at 8 and 16 bits).
After conversion, encoding is set to rgba8 or rgba16
and the width/height are halved. The method is a no-op for non-Bayer encodings.
EncodingConversion(ImageMsg, bool, bool)
Converts a byte array from BGR to RGB.
Declaration
public static byte[] EncodingConversion(ImageMsg image, bool convertBGR = true, bool flipY = true)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image data. |
| bool | convertBGR | Whether to convert from RGB to BGR channel order. |
| bool | flipY | Whether to flip the image vertically. |
Returns
| Type | Description |
|---|---|
| byte[] | The result. |
EncodingRequiresBGRConversion(ImageMsg)
Returns true if the image encoding requires a BGR-to-RGB channel swap before use in Unity.
Declaration
public static bool EncodingRequiresBGRConversion(this ImageMsg image)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image data. |
Returns
| Type | Description |
|---|---|
| bool |
GetBytesPerChannel(ImageMsg)
Returns the number of bytes per channel for the encoding specified in image.
Declaration
public static int GetBytesPerChannel(this ImageMsg image)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image message whose bytes-per-channel to query. |
Returns
| Type | Description |
|---|---|
| int | 1, 2, 4, or 8 bytes per channel. Defaults to 1 for unknown encodings. |
GetNumChannels(ImageMsg)
Returns the number of color channels for the encoding specified in image.
Declaration
public static int GetNumChannels(this ImageMsg image)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image data. |
Returns
| Type | Description |
|---|---|
| int | The number of channels (1, 2, 3, or 4). Defaults to 4 for unknown encodings. |
GetPixelsInCameraSpace(CameraInfoMsg)
Computes the normalized camera-space direction for every pixel in the image described by cameraInfo.
Declaration
public static PointMsg[] GetPixelsInCameraSpace(this CameraInfoMsg cameraInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| CameraInfoMsg | cameraInfo | The camera intrinsics message providing the K matrix and resolution. |
Returns
| Type | Description |
|---|---|
| PointMsg[] | An array of PointMsg values, one per pixel, in camera-space coordinates. |
GetTextureFormat(ImageMsg)
Returns the Unity TextureFormat that best matches the encoding of image.
Declaration
public static TextureFormat GetTextureFormat(this ImageMsg image)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image data. |
Returns
| Type | Description |
|---|---|
| TextureFormat | The corresponding TextureFormat. Defaults to RGB24 for unknown encodings. |
Exceptions
| Type | Condition |
|---|---|
| NotImplementedException | Thrown for 32-bit integer or 64-bit floating-point encodings that Unity does not support. |
IsBayerEncoded(ImageMsg)
Returns true if the image uses one of the ROS Bayer pattern encodings.
Declaration
public static bool IsBayerEncoded(this ImageMsg image)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | image | The image data. |
Returns
| Type | Description |
|---|---|
| bool |
RegionOfInterestTexture(RegionOfInterestMsg, Texture2D, int, int)
Creates a new Texture2D that grabs a region of interest of a given texture, if applicable. Otherwise, an approximated empty texture with a highlighted region is returned.
Declaration
public static Texture2D RegionOfInterestTexture(this RegionOfInterestMsg message, Texture2D rawTex, int height = 0, int width = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| RegionOfInterestMsg | message | The message data to visualize. |
| Texture2D | rawTex | Raw texture data to encode. |
| int | height | Image height in pixels. |
| int | width | Image width in pixels. |
Returns
| Type | Description |
|---|---|
| Texture2D | The result. |
ToCompressedImageMsg_JPG(Texture2D, HeaderMsg)
Encodes a Texture2D as a JPEG-compressed CompressedImageMsg.
Declaration
public static CompressedImageMsg ToCompressedImageMsg_JPG(this Texture2D tex, HeaderMsg header)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | tex | The texture to encode. |
| HeaderMsg | header | ROS message header containing timestamp and coordinate frame identifier. |
Returns
| Type | Description |
|---|---|
| CompressedImageMsg | A CompressedImageMsg with JPEG encoding. |
ToCompressedImageMsg_PNG(Texture2D, HeaderMsg)
Encodes a Texture2D as a PNG-compressed CompressedImageMsg.
Declaration
public static CompressedImageMsg ToCompressedImageMsg_PNG(this Texture2D tex, HeaderMsg header)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | tex | The texture to encode. |
| HeaderMsg | header | ROS message header containing timestamp and coordinate frame identifier. |
Returns
| Type | Description |
|---|---|
| CompressedImageMsg | A CompressedImageMsg with PNG encoding. |
ToDateTime(TimeMsg)
Converts a TimeMsg to a DateTime using the seconds field as ticks and adding the nanosecond remainder as milliseconds.
Declaration
public static DateTime ToDateTime(this TimeMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| DateTime | A DateTime approximating the ROS timestamp. |
ToImageMsg(Texture2D, HeaderMsg)
Converts a Unity Texture2D to a raw ImageMsg, vertically flipping the pixel data to match ROS convention.
Declaration
public static ImageMsg ToImageMsg(this Texture2D tex, HeaderMsg header)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | tex | The texture to convert. |
| HeaderMsg | header | ROS message header containing timestamp and coordinate frame identifier. |
Returns
| Type | Description |
|---|---|
| ImageMsg | An ImageMsg containing the raw pixel data from the texture. |
ToLatLongString(NavSatFixMsg)
Formats the latitude and longitude from a NavSatFixMsg as a human-readable string with cardinal direction suffixes.
Declaration
public static string ToLatLongString(this NavSatFixMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| NavSatFixMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| string | A string such as |
ToLongTime(TimeMsg)
Packs a TimeMsg into a single long by storing the seconds in the upper 32 bits and nanoseconds in the lower 32 bits.
Declaration
public static long ToLongTime(this TimeMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| long | A long encoding both the seconds and nanoseconds fields. |
ToTexture2D(CompressedImageMsg)
Decodes a CompressedImageMsg into a Unity Texture2D using Unity's built-in image loader.
Declaration
public static Texture2D ToTexture2D(this CompressedImageMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| CompressedImageMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| Texture2D | A new Texture2D containing the decoded image. |
ToTexture2D(ImageMsg, bool, bool, bool)
Converts a raw ImageMsg into a Unity Texture2D, optionally debayering, color-converting, and flipping vertically.
Declaration
public static Texture2D ToTexture2D(this ImageMsg message, bool debayer = false, bool convertBGR = true, bool flipY = true)
Parameters
| Type | Name | Description |
|---|---|---|
| ImageMsg | message | The message data to visualize. |
| bool | debayer | Whether to apply Bayer demosaicing for Bayer-encoded images. Defaults to false. |
| bool | convertBGR | Whether to swap BGR channels to RGB. Defaults to true. |
| bool | flipY | Whether to flip the image vertically. Defaults to true. |
Returns
| Type | Description |
|---|---|
| Texture2D | A new Texture2D populated with the image data. |
ToTimestampString(TimeMsg)
Returns a human-readable timestamp string from a TimeMsg, formatted as a short date/long time followed by the nanosecond remainder.
Declaration
public static string ToTimestampString(this TimeMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| string | A formatted timestamp string. |
ToUnityColor(ColorRGBAMsg)
Converts a ColorRGBAMsg(Color) to a Unity Color.
Declaration
public static Color ToUnityColor(this ColorRGBAMsg message)
Parameters
| Type | Name | Description |
|---|---|---|
| ColorRGBAMsg | message | The message data to visualize. |
Returns
| Type | Description |
|---|---|
| Color | The equivalent Unity Color. |