如何在c#中从RGB555转换为RGB888?
我需要将 16 位 XRGB1555 转换为 24 位 RGB888。我的函数如下,但它并不完美,即 0b11111 的值将给出 248 作为像素值,而不是 255。该函数适用于小端,但可以轻松修改为大端。
public static Color XRGB1555(byte b0, byte b1)
{
return Color.FromArgb(0xFF, (b1 & 0x7C) << 1, ((b1 & 0x03) << 6) | ((b0 & 0xE0) >> 2), (b0 & 0x1F) << 3);
}
有什么想法如何让它发挥作用吗?
I need to convert 16-bit XRGB1555 into 24-bit RGB888. My function for this is below, but it's not perfect, i.e. a value of 0b11111 wil give 248 as the pixel value, not 255. This function is for little-endian, but can easily be modified for big-endian.
public static Color XRGB1555(byte b0, byte b1)
{
return Color.FromArgb(0xFF, (b1 & 0x7C) << 1, ((b1 & 0x03) << 6) | ((b0 & 0xE0) >> 2), (b0 & 0x1F) << 3);
}
Any ideas how to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常会将最高位复制到最低位,因此,如果您有如下所示的 5 位:
您可以将其扩展为 8 位,如下所示:
这样,所有零仍为全零,所有 1 变为全 1,并且值介于两者之间适当缩放。
(请注意,A、B、C 等不应是十六进制数字 - 它们是代表单个位的变量)。
You would normally copy the highest bits down to the bottom bits, so if you had five bits as follows:
You would extend that to eight bits as:
That way, all zeros remains all zeros, all ones becomes all ones, and values in between scale appropriately.
(Note that A,B,C etc aren't supposed to be hex digits - they are variables representing a single bit).
我会用一个查找表。由于只有 32 个不同的值,它甚至适合缓存行。
您可以通过以下方式从 5 位值获取 8 位值:
return (x<<3)||(x>>2);
不过,舍入可能并不完美。即,结果并不总是最接近输入,但绝不会远离 1/255。
I'd go with a lookup table. Since there are only 32 different values it even fits in a cache-line.
You can get the 8 bit value from the 5 bit value with:
return (x<<3)||(x>>2);
The rounding might not be perfect though. I.e. the result isn't always closest to the input, but it never is further away that 1/255.