什么是掩码值?

发布于 2024-09-29 15:25:44 字数 241 浏览 7 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-10-06 15:25:44

基本上发生的情况是,当 SDL 确定每种颜色要显示多少时,它会查看掩码中设置了哪些位,以确定要注意颜色中的哪些位。它并不像按位 AND 那么简单,因为值会移位。例如,

color = 0x00800000 = 00000000 10000000 00000000 00000000

如果您将蒙版设置为

Rmask = 0xFF000000 = 11111111 00000000 00000000 00000000
Gmask = 0x00FF0000 = 00000000 11111111 00000000 00000000
Bmask = 0x0000FF00 = 00000000 00000000 11111111 00000000
Amask = 0x000000FF = 00000000 00000000 00000000 11111111

,则颜色将为绿色 {R=0, G=128, B=0}。 Rmask指定的color位为0,Gmask指定的位为0x80 == 128Bmask 指定的位为 0。如果要反转相同颜色的遮罩:

Rmask = 0x000000FF
Gmask = 0x0000FF00
Bmask = 0x00FF0000
Amask = 0xFF000000

现在颜色将为蓝色 {R=0, G=0, B=128}。您正在查看的示例似乎使用了 16 位颜色,没有 Alpha 通道。由于 16 位不能平均划分为 3 个颜色通道,因此绿色会获得额外的位(因为人们认为人眼对绿色更敏感)。

示例:

color = 0x1234 = 00010 010001 10100

Rmask = 0xF800 = 11111 000000 00000
Gmask = 0x07E0 = 00000 111111 00000
Bmask = 0x001F = 00000 000000 11111
Amask = 0

颜色为 {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,

color = 0x00800000 = 00000000 10000000 00000000 00000000

and you've set your masks to be

Rmask = 0xFF000000 = 11111111 00000000 00000000 00000000
Gmask = 0x00FF0000 = 00000000 11111111 00000000 00000000
Bmask = 0x0000FF00 = 00000000 00000000 11111111 00000000
Amask = 0x000000FF = 00000000 00000000 00000000 11111111

then the color will be green {R=0, G=128, B=0}. The bits of color specified by Rmask are 0, the bits specified by Gmask are 0x80 == 128, and the bits specified by Bmask are 0. If you were to reverse the masks for the same color:

Rmask = 0x000000FF
Gmask = 0x0000FF00
Bmask = 0x00FF0000
Amask = 0xFF000000

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:

color = 0x1234 = 00010 010001 10100

Rmask = 0xF800 = 11111 000000 00000
Gmask = 0x07E0 = 00000 111111 00000
Bmask = 0x001F = 00000 000000 11111
Amask = 0

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文