IC Imaging Control C++



RGB32 is a 32 bit color format. It is like RGB24, except that every pixel has an additional byte to store an alpha value. An alpha value describes the transparency of the pixel. Therefore, every pixel consists of 4 bytes. RGB32 is also referred to as RGBA, where the A stands for Alpha. The A value is not used in IC Imaging Control and is therefore always 0. As for the RGB24 pixel format, IC Imaging Control uses the BGRA byte order for the RGB32 pixel format. The organization of the pixels in the image buffer is from left to right and bottom up.

How to read and write pixel data

A video capture device, video format, FrameSnapSink, which defines the image data color format must have been setup. The following code fragments show step-by-step how to access and manipulate the pixel data of RGB32.

First of all, we have to capture an image. Otherwise, the image buffer would be empty. To do so, live mode is started and FrameSnapSink::snapSingle is called.

Accessing the buffer

The following code retrieves a byte pointer to the image data. We use a structure declared in windows.h, RGBQUAD to access the pixel's components and reduce coordinate calculation to the level of RGB8:

RGBQUAD* pImgData = (RGBQUAD*)pActiveBuf->getPtr();

In this example, we want to output the first (upper left hand) two pixels of the image. In a second step, we are going to manipulate the first 3 pixels. As mentioned before, the image data is stored bottom up. Therefore, pbImgData points to the first pixel of the last line of the image. To get access to the first pixel of the first line of the image, the following calculation has to be performed:

        // Calculate the index of the upper left pixel
        // Images are stored upside down in the image buffer
SIZE dim = pActiveBuf->getFrameType().dim;
int iOffsUpperLeft = (dim.cy - 1) * dim.cx;

At first, we retrieve the width and height of the image in terms of pixels. Then, the offset to the upper left pixel is calculated. Since every pixel is exactly 1 byte in size, we can calculate the offset as:

(Height-1) * Width

Now that we have the offset to the the first pixel, we can read it out:

printf( "\nImage buffer pixel format is MEDIASUBTYPE_RGB32\n" );
printf( "Pixel 1(RGBA): %d %d %d %d\n", pImgData[iOffsUpperLeft + 0].rgbRed,
        pImgData[iOffsUpperLeft + 0].rgbGreen,
        pImgData[iOffsUpperLeft + 0].rgbBlue,
        pImgData[iOffsUpperLeft + 0].rgbReserved );
printf( "Pixel 2(RGBA): %d %d %d %d\n", pImgData[iOffsUpperLeft + 1].rgbRed,
        pImgData[iOffsUpperLeft + 1].rgbGreen,
        pImgData[iOffsUpperLeft + 1].rgbBlue,
        pImgData[iOffsUpperLeft + 1].rgbReserved );

Manipulating Image Data

Instead of only reading pixel data, it is, of course, possible to manipulate the data as well. The following code sets the upper left hand pixel to red and the next 2 pixels to green and blue. After this manipulation, the image is saved as a .bmp file.

        // overwrite first 3 pixels and save image to disk
        // Set the first pixel to RED
pImgData[iOffsUpperLeft + 0].rgbRed = 0xff;
pImgData[iOffsUpperLeft + 0].rgbGreen = 0;
pImgData[iOffsUpperLeft + 0].rgbBlue = 0;
pImgData[iOffsUpperLeft + 0].rgbReserved = 0;
// Set the second pixel to GREEN
pImgData[iOffsUpperLeft + 1].rgbRed = 0;
pImgData[iOffsUpperLeft + 1].rgbGreen = 0xff;
pImgData[iOffsUpperLeft + 1].rgbBlue = 0;
pImgData[iOffsUpperLeft + 1].rgbReserved = 0;
// Set the third pixel to BLUE
pImgData[iOffsUpperLeft + 2].rgbRed = 0;
pImgData[iOffsUpperLeft + 2].rgbGreen = 0;
pImgData[iOffsUpperLeft + 2].rgbBlue = 0xff;
pImgData[iOffsUpperLeft + 2].rgbReserved = 0;
saveToFileBMP( *pActiveBuf, "RGB32.bmp" );

To check the result, open the saved image and take a look at the upper left hand pixels. They should look as follows:


<< Accessing an Image Buffer

Get in touch with us

About The Imaging Source

Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.

Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.

Automated Imaging Association ISO 9001:2015 certified

Contact us