# BGR detection at runtime

**URL:** <https://forum.commonvisionblox.com/t/bgr-detection-at-runtime/340>\
**Category:** .Net API\
**Tags:** bitmap, buffer, layout\
**Created:** [January 15, 2018, 4:44pm UTC](https://forum.commonvisionblox.com/t/bgr-detection-at-runtime/340 "2018-01-15T16:44:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomes](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tomes](https://forum.commonvisionblox.com/u/Tomes)\
**Post date:** [January 15, 2018, 4:44pm UTC](https://forum.commonvisionblox.com/t/bgr-detection-at-runtime/340/1 "2018-01-15T16:44:21Z")

</div>

Hi,

In one of the examples, the bytes from an image are copied to a bitmap. The way of copying depends on the pixel format of the image (RGB or BGR). In the example, this is done as follows:

```
#if BGR
              *(pDst++) = *pSrc[2];
              *(pDst++) = *pSrc[1];
              *(pDst++) = *pSrc[0];
#else
              *(pDst++) = *pSrc[0];
              *(pDst++) = *pSrc[1];
              *(pDst++) = *pSrc[2];
#endif

```

Is there a check for this which can be executed at runtime?

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [January 15, 2018, 8:03pm UTC](https://forum.commonvisionblox.com/t/bgr-detection-at-runtime/340/2 "2018-01-15T20:03:17Z")

</div>

Hi,

do you want to detect whether the memory buffer layout of a :cvb: `Image` is BGR or do you want to create a BGR bitmap (like e.g. a `System.Drawing.Bitmap`)?

The latter is simplest: you just need to reference the _Stemmer.Cvb.Extensions.dll_ which adds an extension method to :cvb: `Image` objects: `.ToBitmap()`. This produces a BGR8 bitmap:

> `Image cvbImage = ...;`  
> `System.Drawing.Bitmap bitmap = cvbImage.ToBitmap()`

To determine whether the `Image` memory buffer layout is BGR, you need to check the pointers and `DataType`:

```cs
static class ImageExtensions
{
  public static bool IsBgr8(this Image image)
  {
    if (image == null)
      throw new ArgumentNullException(nameof(image));
			
    if (image.Planes.Any(plane => plane.DataType != DataTypes.Int8BppUnsigned))
      return false; // every plane must be of byte type

    if (!TryGetLinearAccessData(image, out var planeData))
      return false; // BGR image must be linear
			
    if (planeData.Length != 3)
      return false; // B-G-R planes
			
    var basePtrs = planeData
      .Select(data => data.BasePtr.ToInt64())
      .ToArray();
		
    // cvb's plane #0 ist red, #1 green and #2 blue
    return (basePtrs[2] == (basePtrs[1] - 1))
      && (basePtrs[1] == (basePtrs[0] - 1));
  }
	
  private static bool TryGetLinearAccessData(Image image, out LinearAccessData[] planeData)
  {
    Debug.Assert(image != null);
		
    planeData = new LinearAccessData[image.Planes.Count];
    for (int planeIndex = 0; planeIndex < planeData.Length; planeIndex++)
    {
      var plane = image.Planes[planeIndex];
      if (!plane.TryGetLinearAccess(out planeData[planeIndex]))
        return false;
    }
    return true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Tomes](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Tomes](https://forum.commonvisionblox.com/u/Tomes)\
**Post date:** [January 16, 2018, 9:53am UTC](https://forum.commonvisionblox.com/t/bgr-detection-at-runtime/340/3 "2018-01-16T09:53:42Z")

</div>

I need to determine the memory layout. 🙂
