C# WriteableBitmapEx 像素预乘位移位
这是关于从像素值中提取 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)' 部分。我不理解的是预乘法(我指的是这里的实现,而不是理论)。我特别想理解 - 所以我的问题是:
- 为什么 255 向右移动 8 位,然后除以 alpha?
- 为什么每个值都乘以该结果,然后向左移动 8 位?
This is about extracting the RGB from a pixel value. Here is the code snippet:
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));
I understand technically, i.e. at the level of bits, and binary what is going on. I understand in particular the 'Byte b# = (Byte)((((myColor >> n) & 0xFF)' parts. What I do not understand is the premultiplication (I mean the implementation here, not the theory). In particular I want to understand - and so to my questions:
- Why is 255 being shifted by 8 bits to the right and then devided by the alpha?
- Why is each value being multiplied by the result of this and then shifted 8 bits to the left?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做是为了提高使用整数除法时的精度。使用整数运算的原因可能是为了速度。
话虽如此,我不相信这是一个很好的 alpha 预乘公式。
如果您有兴趣,请阅读定点算术了解更多信息
This is done to increase the precision while using integer division. Reason to use integer operations could be for speed.
Having said that I'm not convinced that this is a good formula for alpha premultiply.
If you are interested, and for more information read up on Fixed Point Arithmetic