什么是掩码值?
我正在学习 SDL 库,创建表面的函数之一是 SDL_CreateRGBSurface。我想知道的四个参数是 R、G、B 和 A 掩模。在这种情况下,面具到底是什么?
我还在关注一本关于 SDL 的书,作者传入了 5 位红色、5 位蓝色和 6 位绿色作为掩码值。但这意味着什么?掩码值有什么作用?
我使用随机数(Uint32 颜色)在屏幕上填充了一个矩形,结果变为绿色。当我更改蒙版值时,我注意到即使具有相同的颜色值,颜色也会发生变化。
I'm learning the SDL library and one of the functions to create a surface is SDL_CreateRGBSurface. The four parameters I'm wondering about are R, G, B, and A masks. What exactly is a mask in this context?
I'm also following a book about SDL and the author passed in 5 bits of red, 5 bits of blue, and 6 bits of green for the mask values. What does that mean though? What do the mask values do?
I filled in a rectangle onto the screen using a random number (Uint32 color) and got green. When I changed the mask values I noticed that the color changed even with the same color value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上发生的情况是,当 SDL 确定每种颜色要显示多少时,它会查看掩码中设置了哪些位,以确定要注意颜色中的哪些位。它并不像按位
AND
那么简单,因为值会移位。例如,如果您将蒙版设置为
,则颜色将为绿色 {R=0, G=128, B=0}。
Rmask
指定的color
位为0,Gmask
指定的位为0x80 == 128
,Bmask
指定的位为 0。如果要反转相同颜色的遮罩:现在颜色将为蓝色 {R=0, G=0, B=128}。您正在查看的示例似乎使用了 16 位颜色,没有 Alpha 通道。由于 16 位不能平均划分为 3 个颜色通道,因此绿色会获得额外的位(因为人们认为人眼对绿色更敏感)。
示例:
颜色为 {R=2, G=17/2=8.5, B=20}。 (绿色的额外位意味着该值需要减半才能标准化)。
我不确定 SDL 到底是如何做到的,或者您可以在 SDL 中使用什么类型的疯狂掩码,但我想实际的算法是沿着按位
AND
的路线,然后右 -移位与掩码最低有效部分中清除的位数一样多的位?或者从最高有效位到最低有效位工作,对于掩码中设置的每个位,将总数左移并在设置了颜色的相应位时加 1。Basically what's happening is that when the SDL determines how much of each color to display, it looks at which bits are set in the mask to determine which bits to pay attention to in the color. It's not quite as simple as a bitwise
AND
because the values are shifted over. For example,and you've set your masks to be
then the color will be green {R=0, G=128, B=0}. The bits of
color
specified byRmask
are 0, the bits specified byGmask
are0x80 == 128
, and the bits specified byBmask
are 0. If you were to reverse the masks for the same color:now the color will be blue {R=0, G=0, B=128}. It appears that the example you're looking at uses 16 bit color without an alpha channel. Since 16 bits do not divide evenly by 3 color channels, green gets an extra bit (as the human eye is believed to be more sensitive to green).
Example:
The color will be {R=2, G=17/2=8.5, B=20}. (the extra bit for green means the value needs to be halved to normalize it).
I'm not sure of how exactly SDL does it, or what types of crazy masks you can use with SDL, but I would imagine the actual algorithm is along the lines of a bitwise
AND
, then right-shift as many bits as are cleared in the least significant portion of the mask? Or else working from most significant to least significant bit, for each bit that's set in the mask, shift the total left and add 1 if the corresponding bit of the color is set.