"& 是什么意思? 0xff”做?

发布于 2024-11-09 16:02:32 字数 335 浏览 6 评论 0 原文

我试图理解下面的代码,其中 b 是给定的整数,image 是图像。

据我所知,如果给定点 i,j 的 RGB 值大于 b,则将该像素设置为白色,否则设置为黑色。所以会将图像转换为黑白图像。

然而我不知道 (& 0xff) 实际做了什么,我猜它是一种二进制移位?

if ((image.getRGB(i, j) & 0xff) > b) {
    image.setRGB(i, j, 0xffffff) ;
} else {
    image.setRGB(i, j, 0x000000);
}

I am trying to understand the code below where b is a given integer and image is an image.

I understand that if the RGB value at given point i,j is greater than b then set that pixel to white else set to black. so would convert the image into black and white.

However I am lost to what (& 0xff) actually does, I am guessing its a kind of binary shift?

if ((image.getRGB(i, j) & 0xff) > b) {
    image.setRGB(i, j, 0xffffff) ;
} else {
    image.setRGB(i, j, 0x000000);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

残龙傲雪 2024-11-16 16:02:32

这就是所谓的面具。问题是,您可以将 RGB 值全部存储在一个整数中,每个分量对应一个字节。类似于 0xAARRGGBB(阿尔法、红色、绿色、蓝色)。通过使用 0xFF 执行按位与,您只保留最后一部分,即蓝色。对于其他通道,您可以使用:

int alpha = (rgb >>> 24) & 0xFF;
int red   = (rgb >>> 16) & 0xFF;
int green = (rgb >>>  8) & 0xFF;
int blue  = (rgb >>>  0) & 0xFF;

在 alpha 情况下,您可以跳过 & 0xFF,因为它不执行任何操作;蓝色情况下移位 0 也是如此。

It's a so-called mask. The thing is, you get the RGB value all in one integer, with one byte for each component. Something like 0xAARRGGBB (alpha, red, green, blue). By performing a bitwise-and with 0xFF, you keep just the last part, which is blue. For other channels, you'd use:

int alpha = (rgb >>> 24) & 0xFF;
int red   = (rgb >>> 16) & 0xFF;
int green = (rgb >>>  8) & 0xFF;
int blue  = (rgb >>>  0) & 0xFF;

In the alpha case, you can skip & 0xFF, because it doesn't do anything; same for shifting by 0 in the blue case.

你没皮卡萌 2024-11-16 16:02:32

颜色表示

RGB 值是一个整数,因此它在内存中用 4 个字节(或等效的 32 位)表示。

示例:

00000001 00000010 00000011 00000100

每个字节代表颜色的一个组成部分:

  • 第一个字节:对应于不透明度的 alpha 值(示例中为 00000001)
  • 第二个字节:红色值(示例中为 00000010)
  • 字节:绿色值(示例中为 00000011)
  • 第三个字节:绿色值(示例中为 00000011)第四个 byte:蓝色值(示例中为 00000100)

0xff 和 0xffffff 符号

0xff 表示十六进制值 FF,等于整数 255。其二进制表示为:

00000000 00000000 00000000 11111111

同样 0xffffff 表示为:

00000000 11111111 11111111 11111111

它对应于颜色 白色(红、绿、蓝等于 255)。

&运算符

二元运算符和“&”应用于两个整数 i1 和 i2 (i1 & i2)。它返回一个整数,除了 i1 和 i2 中都等于 1 的位外,所有位都等于 0。
例如,如果我们应用 &在我的第一个示例和 0xff 上,我们得到:

00000000 00000000 00000000 00000100

因此, (& 0xff) 只能保留最后一个字节的值(即颜色的蓝色分量的值)。

// If the blue component of image.getRGB(i, j) is greater than b
if ((image.getRGB(i, j) & 0xff) > b) {
    // Set the image to white
    image.setRGB(i, j, 0xffffff) ;
} else {
    // Set the image to black
    image.setRGB(i, j, 0x000000);
}

Color representation

The RGB value is an integer so it is represented in memory by 4 bytes (or equivalently 32 bits).

Example:

00000001 00000010 00000011 00000100

Each byte represents one component of the color :

  • 1st byte: alpha value (00000001 in the example) which corresponds to the opacity
  • 2nd byte: red value (00000010 in the example)
  • 3rd byte: green value (00000011 in the example)
  • 4th byte: blue value (00000100 in the example)

0xff and 0xffffff symbols

0xff represents the hexadecimal value FF which is equal to the integer 255. Its binary representation is:

00000000 00000000 00000000 11111111

Similarly 0xffffff is represented by:

00000000 11111111 11111111 11111111

It corresponds to the color white (red, green and blue equal to 255).

& operator

The binary operator and "&" is applied on two integers i1 and i2 (i1 & i2). It returns an integer with all its bits equal to 0 except the ones which are equal to 1 in both i1 and i2.
For example, if we apply & on my first example and on 0xff, we obtain:

00000000 00000000 00000000 00000100

As a consequence, (& 0xff) enables to only keep the values of the last byte (i.e., the value of the blue component of the color).

// If the blue component of image.getRGB(i, j) is greater than b
if ((image.getRGB(i, j) & 0xff) > b) {
    // Set the image to white
    image.setRGB(i, j, 0xffffff) ;
} else {
    // Set the image to black
    image.setRGB(i, j, 0x000000);
}
空城缀染半城烟沙 2024-11-16 16:02:32

正在

& 0xFF

获取颜色分量之一(红色或蓝色,我忘了是哪一个)。

如果不执行颜色遮罩,请考虑 RGB (0, 127, 0) 和阈值 63。 getRGB(...) 调用将返回

(0 * 256 * 256) + (127 * 256) + 0 = 32512

Which 明显大于阈值 63。但其目的是忽略其他两个颜色通道。位掩码仅获取最低 8 位,其中为零。

检查

> b

颜色是否比特定阈值“b”更亮。

如果超过阈值,则将像素着色为白色,使用

image.setRGB(i,j,0xffffff)

...否则将其着色为黑色,使用...

image.setRGB(i,j,0x000000)

因此,它是基于单个颜色通道上的简单逐像素阈值的黑白转换。

The

& 0xFF

is getting one of the color components (either red or blue, I forget which).

If the color mask is not performed, consider RGB (0, 127, 0), and the threshold 63. The getRGB(...) call would return

(0 * 256 * 256) + (127 * 256) + 0 = 32512

Which is clearly more than the threshold 63. But the intent was to ignore the other two color channels. The bitmask gets only the lowest 8 bits, with is zero.

The

> b

is checking if the color is brighter than a particular threshold, 'b'.

If the threshold is exceeded, the pixel is colored white, using

image.setRGB(i,j,0xffffff)

... otherwise it is colored black, using

image.setRGB(i,j,0x000000)

So it is a conversion to black and white based on a simple pixel-by-pixel threshold on a single color channel.

负佳期 2024-11-16 16:02:32

It is probably because there is some conversion to or from ARGB. This is a really good blog post about why to do bit-wise operations for colors.

北方的韩爷 2024-11-16 16:02:32

<代码>& 0xff 是按位 AND

(image.getRGB(i,j)&0xff) 获取 getRGB 返回的 rgb 编码 int 的蓝色值 <代码>> b 部分检查是否大于某个阈值

& 0xff is a bitwise AND

(image.getRGB(i,j)&0xff) gets the blue value of the rgb encoded int returned by getRGB the > b part check whether it's larger than some threshold

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