王朝百科
分享
 
 
 

RGB555

王朝百科·作者佚名  2010-03-03  
宽屏版  字体: |||超大  

RGB555

RGB555 is a 16 bit color format. Every pixel is represented by two bytes. The characters 555 stand for the number of bits used for every color value. In this case, 5 bits are used for every color value, so only 15 bits are needed. The last bit (most significant bit) is unused. The organization of the pixels in the image buffer is from left to right and bottom up.

Memory Layout

此处为图1

As illustrated above, the 5 least significant bits of the WORD correspond to the blue value, bits 5 - 9 correspond to the green value and bits 10 to 14 correspond to the red value. Bit 15 is unused in this case. Please note that on the x86 architecture WORDs are stored in little endian order, which means the LOW BYTE is saved first. This is important when accessing the image data with a byte pointer.

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 first have been setup. The following code fragments show step-by-step how to access and manipulate the pixel data of RGB555.

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 a WORD pointer. This makes it much easier to access the pixel data since RGB555 is a 16 bit color format.

WORD* pwImgData = (WORD*) 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 bottom up. Therefore, pwImgData points to the first byte of the first pixel of the last line in the buffer. To get access to this first byte, the following calculation has to be performed:

// Calculate the index of the upper left pixel

// Images are stored upside down in the image buffer

// * 1: a pixel is 2 byte, but since we have a WORD pointer (which is also 2 bytes)

// we count in pixel not in bytes

SIZE dim = pActiveBuf->getFrameType().dim;

int iOffsUpperLeft = (dim.cy-1) * dim.cx * 1;

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. Please note that we multiply with Width * 1 and not Width * 2. This is because we use a WORD pointer to access the image data. Of course, the multiplication by 1 is just for illustration and can be left out.

(Height-1) * Width

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

// Please note: RGB values are stored within a WORD in the following order: R,G,B

// A binary AND operation is done with the color mask to extract the specific color.

// After the AND operation, a right shift is done so that the output is displayed correctly.

printf( "

Image buffer pixel format is eRGB555

" );

printf( "Pixel 1(RGB): %d %d %d

", ( pwImgData[iOffsUpperLeft] & eRGB555_R ) >> 10 ,

( pwImgData[iOffsUpperLeft] & eRGB555_G ) >> 5,

( pwImgData[iOffsUpperLeft] & eRGB555_B ) );

printf( "Pixel 2(RGB): %d %d %d

", ( pwImgData[iOffsUpperLeft+1] & eRGB555_R ) >> 10 ,

( pwImgData[iOffsUpperLeft+1] & eRGB555_G ) >> 5,

( pwImgData[iOffsUpperLeft+1] & eRGB555_B ) );

As seen in the code above, we perform a binary AND operation with the appropriate pixel mask on the current pixel to extract the color value. After that, the values for red and green must be shifted to the right to get the correct value (otherwise the value would be 1024 (32 times too large)).

Manipulating Image Data

Shifting is also important when assigning values. For example, if 7 is assigned to the red value, it must be shifted 10 times to the left. Instead of writing:

// Assign 7 to the red value

pwImgData[iOffsUpperLeft] = 7; // this is WRONG

which assigns 7 to the blue value, the following code should be used:

// Assign 7 to the red value

pwImgData[iOffsUpperLeft] = 7 << 10;

Another important thing to note is that the assignment above will overwrite the values for green and blue. To prevent this, the value must be ORed to the pixel data as the following code will show:

// Clear the red value (set all bits to 0)

pwImgData[iOffsUpperLeft] &= ~eRGB555_R;

// Assign 7 to the red value without overwriting green and blue values

pwImgData[iOffsUpperLeft] |= 7 << 10;

Please note that all bits of the appropriate color should be set to 0, as does the code above. Consider, for example, that the previous red value could have been 16 (or 10000 binary). If we then perform a binary OR operation with 7 (or 111 binary), the result will be 23 (or 10111 binary). Therefore, it is better to set all bits of the appropriate channel to 0.

Now we set the upper left pixel to red, the next to green and the third to blue.

// overwrite the first 3 pixels and save image to disk

// set the first pixel to RED

pwImgData[iOffsUpperLeft] = 0; // clear the pixel

pwImgData[iOffsUpperLeft] |= 31 << 10; // Assign the value for red

// set the second pixel to GREEN

pwImgData[iOffsUpperLeft+1] = 0; // clear the pixel

pwImgData[iOffsUpperLeft+1] |= 31 << 5; // Assign the value for green

// set the third pixel to BLUE

pwImgData[iOffsUpperLeft+2] = 0; // clear the pixel

pwImgData[iOffsUpperLeft+2] |= 31; // Assign the value for blue

pActiveBuf->save( "RGB555.bmp" );

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

此处为图2

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
中国古代四大美女:背后隐藏惊人秘密
 女性   2025-06-20
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
 
>>返回首页<<
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有