IC Imaging Control C++

YGB1

YGB1

YGB1 is a 10 bit grayscale format. Every pixel is stored in two bytes. Only 10 of the 16 bits of these two bytes contain relevant image data, the other bits are unused. The organization of the pixels in the image buffer is from left to right and top down.

Memory Layout

image

As illustrated above, the high byte contains the bits 0 to 7, while the most significant bits 8 and 9 are stored in the low byte. To convert a 16-bit-pixel from the image buffer to a 16-bit-value (in range 0-1023) that can be used in image processing, the bytes have to be swapped. The following code shows how to convert to and from the YGB1 format:

unsigned __int16 YGB1PixelFromInt16Value( unsigned __int16 value )
{
    return byteSwap( value );
}
 
unsigned __int16 Int16ValueFromYGB1Pixel( unsigned __int16 pixel )
{
    return byteSwap( pixel );
}

The function Int16ValueFromYGB1Pixel takes a 16-bit word from the image buffer and converts it to a logical brightness value for that pixel in the range 0 to 1023.

YGB1PixelFromInt16Value reverses that operation by converting a brightness value to a number that can be stored in the image buffer.

The functions rely on the byteSwap function that is implemented as follows:

unsigned __int16 byteSwap( unsigned __int16 x )
{
    BYTE* p = (BYTE*)&x;
    return ((unsigned int)p[0] << 8) | p[1];
}

How to read and write pixel data

A video capture device, video format, FrameHandlerSink with a MemBufferCollection, 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 YGB1.

First of all, we have to capture an image. Otherwise, the image buffer would be empty. To do so, we start live mode and call Grabber::snapImages.

Accessing the buffer

The following code retrieves a pointer to the image data. Please note, that getPtr() returns a BYTE pointer which will be type-casted to an unsigned __int16 pointer. This makes it much easier to access the pixel data since YGB1 is a 16 bit format.

// YGB1 Images are stored top-down, so the upper left pixel starts at byte 0.
// 2 Byte represent one pixel.
unsigned __int16* pImgData = (unsigned __int16*)pActiveBuf->getPtr();

In this example, we want to output the first (upper left hand) pixel of the image and manipulate the first 3. As previously mentioned, the image data is stored top down. Therefore, pImgData points to the first pixel of the first line in the buffer.

To convert the pixel data to brightness values, the function YGB1PixelFromInt16Value from above is used:

// To get a "value" (in range 0-1023) for the pixel, use
// Int16ValueFromYGB1Pixel:
unsigned __int16 val0 = Int16ValueFromYGB1Pixel( pImgData[0] );
unsigned __int16 val1 = Int16ValueFromYGB1Pixel( pImgData[1] );
 
printf( "\nImage buffer pixel format is eYGB1\n" );
printf( "Pixel 1 (Y16): %d\n", val0 );
printf( "Pixel 2 (Y16): %d\n", val1 );

Manipulating Image Data

Now we set the upper left three pixels to black, gray and white. The function YGB1PixelFromInt16Value from above is used to convert from brightness values to pixel data:

pImgData[0] = YGB1PixelFromInt16Value( 0 );        // Set the first pixel to BLACK
pImgData[1] = YGB1PixelFromInt16Value( 511 );    // Set the second pixel to GRAY
pImgData[2] = YGB1PixelFromInt16Value( 1023 );    // Set the third pixel to WHITE
 
// We save the buffer as RGB24, so that other software can view it.
saveToFileBMP( *pActiveBuf, "YGB1.bmp", eRGB24 );

To check the result, just open the saved image and examine the upper left pixels. They should look as follows:

image

<< 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