C#可写位图像素预乘位移位
这是关于从像素值中提取RGB。以下是代码片段:C#可写位图像素预乘位移位,c#,pixels,writeablebitmapex,C#,Pixels,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 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 a=(Byte)(myColor>>24);
//防止被零除
UInt32 ai=a;
如果(ai==0)
{
ai=1;
}
ai=((255>16)&0xFF)*ai>8);
字节bG=(字节)((myColor>>8)和0xFF)*ai>>8);
字节bB=(字节)(((myColor&0xFF)*ai)>>8));
我在技术上理解,即在比特和二进制的层次上理解正在发生的事情。我特别了解“Byte b#=(Byte)(((myColor>>n)和0xFF)”部分。我不了解的是预乘(我这里指的是实现,而不是理论)。我特别想了解-我的问题如下:
这样做是为了在使用整数除法时提高精度。使用整数运算的原因可能是为了速度
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
如果MyColor=0xAABBCCDD
AA=170
BB=187
CC=204
DD=221
预期值:
bA=170
bR=187*(255/170)=280.5
bG=204*(255/170)=306
bB=221*(255/170)=331.5
使用整数除法255/170、255/204、255/221时,所有值都将计算为1,预乘法将无效。
通过使用此运算ai=((255这样做是为了在使用整数除法时提高精度。使用整数运算的原因可能是为了速度
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
如果MyColor=0xAABBCCDD
AA=170
BB=187
CC=204
DD=221
预期值:
bA=170
bR=187*(255/170)=280.5
bG=204*(255/170)=306
bB=221*(255/170)=331.5
使用整数除法255/170、255/204、255/221时,所有值都将计算为1,预乘法将无效。
通过使用此操作ai=((255)您在这里试图做什么?根据我的问题理解代码?您在这里试图做什么?根据我的问题理解代码?