将 24 位 RGB 转换为 ARGB16
我必须读取 24bpp 位图
并将每个像素从 RGB24
转换为 ARGB16
。
我使用了以下代码,
#define ARGB16(a, r, g, b) ( ((a) << 15) | (r)|((g)<<5)|((b)<<10))
但没有得到所需的输出。
任何帮助将不胜感激。
I have to read a 24bpp Bitmap
and convert each pixel from RGB24
to ARGB16
.
I used the following code,
#define ARGB16(a, r, g, b) ( ((a) << 15) | (r)|((g)<<5)|((b)<<10))
But I am not getting the required Output.
Any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
打破它。让我们继续使用宏:
这里假设 alpha 只是一位。
Break it up. Let's continue to use macros:
This assumes the alpha is just a single bit.
由于 RGB 值可能每个都是 8 位,因此您仍然需要将它们截断为 5 位,这样它们就不会在 ARGB16 值中“重叠”。
截断它们的最简单方法可能是将它们向右移动三位。
Since the RGB values are probably 8 bits each, you still need to truncate them to five bits so they won't "overlap" in the ARGB16 value.
The easiest way to truncate them is probably to bitshift them to the right by three places.
在我看来,您可能想从 r、g 和 b 中屏蔽掉不需要的位。也许是这样的:
编辑:哎呀,我认为迈克尔·巴丁的答案可能是正确的 - 你会想要转移,这会让你得到最重要的位。
It looks to me like you probably want to mask off the bits you don't need from r, g and b. Maybe something like this:
Edit: whoops, I think the answer by Michael Buddingh is probably right - you'll want to shift off, this gets you the most significant bits.
我要么使用位掩码来删除不需要的位:(颜色值和0x1F)。
将颜色值向右移动 3 位(似乎是更好的选择)。
并且是一个<< 15.你真正需要什么?您将依赖 0 位是 0 或 1 的事实来设置 alpha 位的打开或关闭。因此,如果您的 alpha 值是 0xFE,那么 alpha 位将为 0,而如果是 0x01,则为 1。
I would either use a bitmask to get rid of the bits you don't need: (colorvalue & 0x1F).
Either shift the color value to the right 3 bits (seems like the better option).
And is a << 15 really what you need? You would be relying on the fact that the 0 bit is 0 or 1 to set the alpha bit on or off. So if your alpha value is 0xFE then the alpha bit would 0, whereas if it were 0x01 it would be 1.
您可能想要 SCALE 而不是 TRUNCAT。
以 R 组件为例,24 位 RGB 中的
0xDE(222)
将变为0x1A = (222.0/0xFF)* 16 位 RGB 中的 0x1F。
You might to want to SCALE rather than TRUNCAT.
Take R component for example ,
0xDE(222)
in 24bit RGB will become0x1A = (222.0/0xFF)*0x1F
in 16bit RGB.我有下面的代码:
我正在读取红色图像(255 0 0),并且我正在使用上面定义的函数(#define ARGB16(a, r, g, b) ( ((a) << ; 15) | (r>>3)|((g>>3)<<5)|((b>>3)<<10))
),但是当我使用十六进制编辑器而不是 7C00 打开文件时,输出文件显示: 1F 1F 1F ..
i have the code bellow :
and i am reading a red image(255 0 0), and i am using the function defined by you above(#define ARGB16(a, r, g, b) ( ((a) << 15) | (r>>3)|((g>>3)<<5)|((b>>3)<<10))
), but the output file shows me : 1F 1F 1F when i am opening the file with a hexaeditor instead of 7C00 ..