这是关于从像素值中提取RGB。下面是代码片段:
Byte a = (Byte)(myColor >> 24);
// Prevent division by zero
UInt32 ai = a;
if (ai == 0)
{
ai = 1;
}
ai = ((255 << 8) / ai);
Byte bA = a;
Byte bR = (Byte)((((myColor >> 16) & 0xFF) * ai) >> 8);
Byte bG = (Byte)((((myColor >> 8) & 0xFF) * ai) >> 8);
Byte bB = (Byte)((((myColor & 0xFF) * ai) >> 8));
我从技术上理解,也就是在位的层次上,和二进制正在发生的事情。我特别理解'Byte b# =(Byte)(myColor >> n) &0xFF)“部分)。我不明白的是乘法(我指的是这里的实现,而不是理论)。我特别想理解--所以我的问题是:
发布于 2010-12-16 18:02:32
这样做是为了提高精度,同时使用整数除法。使用整数操作的原因可能是为了速度。
If MyColor = 0xAABBCCDD
AA = 170
BB = 187
CC = 204
DD = 221
Expected values:
bA = 170
bR = 187 * (255 / 170) = 280.5
bG = 204 * (255 / 170) = 306
bB = 221 * (255 / 170) = 331.5
with integer division 255/170, 255/204, 255/221 will all evaluate to 1 and premultiplication becomes ineffective.
By using this operation ai = ((255 << 8) / ai)
with integer division: ai = (255*256)/170 = 384
and subsequent multiplications and shifts gives you a more accurate result.
E.g.
bR = 187 * 384 / 256 = 280
bG = 204 * 384 / 256 = 306
bB = 221 * 384 / 256 = 331
话虽如此,但我不相信这是α乘积的一个好公式。
如果您感兴趣,请阅读有关不动点算法的更多信息。
https://stackoverflow.com/questions/4460886
复制相似问题