How to convert 24 bit RGB to 8 bit RGB

徘徊边缘 提交于 2019-11-30 17:27:15

问题


I was wondering what is the best way to convert a 24-bit RGB color (8 bits per color) into an 8-bit color (2bits Blue, 3 Green, 3 Red). I'd like C code for doing this.


回答1:


8 bit RGB is normally an indexed (palettized) color format, see Palette (computing).

The way you described it though, getting 8 bpp out of 24 bpp is pretty straightforward - you would need to strip least significant bits and merge the bits into single byte value, per pixel. AFAIK it is not a standard or well-known pixel color representation.




回答2:


Not in C but in javascript.

encodedData = (Math.floor((red / 32)) << 5) + (Math.floor((green / 32)) << 2) + Math.floor((blue / 64));

https://stackoverflow.com/a/25258278/2130509




回答3:


To convert 8bit [0 - 255] value into 3bit [0, 7], the 0 is not a problem, but remember 255 should be converted to 7, so the formula should be Red3 = Red8 * 7 / 255.

To convert 24bit color into 8bit (RRRGGGBB),

8bit Color = (Red * 7 / 255) << 5 + (Green * 7 / 255) << 2 + (Blue * 3 / 255)


来源:https://stackoverflow.com/questions/7657704/how-to-convert-24-bit-rgb-to-8-bit-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!