转: RGB565、RGB888互相转换原理和代码

转:http://www.xuebuyuan.com/935987.html

背景:在我们的计算机中图像是以RGB888格式显示图像的,24位图每个像素保存了32bit的数据,即RGB888+Alpha,Alpha就是半透明填充字节……但是对于真彩的图像而言,肉眼在16bit的时候已经难以分辨了,因此,有些时候,可以讲RGB888转换为RGB565来存储,减少了存储器的容量的同时,降低了数据量;在后端显示的时候,再次把RGB565转换为RGB888,实现数据宽度的匹配!!

 题记: 总的思想就是,低位增加或者移除。

一.RGB888->RGB565

方法只要提取相应单色高位即可(R5 G6 B5),但会导致低位的缺失,影响精度,而且无法恢复。

二.RGB565->RGB888

方法只要补充相应单色低位即可(R3 G2 B3)。

 

RGB888用unsigned int 32位字节存储
  0  0  0  0  0  0  0  0R7R6R5R4R3R2R1R0G7G6G5G4G3G2G1G0B7B6B5B4B3B2B1B0

 

RGB565用unsigned short 16位字节存储
R7R6R5R4R3G7G6G5G4G3G2B7B6B5B4B3
  1. #define RGB888_RED 0x00ff0000

  2. #define RGB888_GREEN 0x0000ff00

  3. #define RGB888_BLUE 0x000000ff

  4.  
  5. #define RGB565_RED 0xf800

  6. #define RGB565_GREEN 0x07e0

  7. #define RGB565_BLUE 0x001f

  8.  
  9. unsigned short RGB888ToRGB565(unsigned int n888Color)

  10. {

  11. unsigned short n565Color = 0;

  12.  
  13. // 获取RGB单色,并截取高位

  14. unsigned char cRed = (n888Color & RGB888_RED) >> 19;

  15. unsigned char cGreen = (n888Color & RGB888_GREEN) >> 10;

  16. unsigned char cBlue = (n888Color & RGB888_BLUE) >> 3;

  17.  
  18. // 连接

  19. n565Color = (cRed << 11) + (cGreen << 5) + (cBlue << 0);

  20. return n565Color;

  21. }

  22.  
  23. unsigned int RGB565ToRGB888(unsigned short n565Color)

  24. {

  25. unsigned int n888Color = 0;

  26.  
  27. // 获取RGB单色,并填充低位

  28. unsigned char cRed = (n565Color & RGB565_RED) >> 8;

  29. unsigned char cGreen = (n565Color & RGB565_GREEN) >> 3;

  30. unsigned char cBlue = (n565Color & RGB565_BLUE) << 3;

  31.  
  32. // 连接

  33. n888Color = (cRed << 16) + (cGreen << 8) + (cBlue << 0);

  34. return n888Color;

  35. }

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值