[英]Get RGB of a pixel in stb_image
I'm created and loaded this image:我创建并加载了这个图像:
int x, y, comps;
unsigned char* data = stbi_load(".//textures//heightMapTexture.png", &x, &y, &comps, 1);
Now, how i'm get a RGB of a certain pixel of this image?现在,我如何获得该图像的某个像素的 RGB?
You are using the 8-bits-per-channel interface.您正在使用 8-bits-per-channel 接口。 Also, you are requesting only one channel (the last argument given to stbi_load
).此外,您只请求一个通道(给stbi_load
的最后一个参数)。 You won't obtain RGB data with only one channel requested.仅请求一个通道不会获得 RGB 数据。
If you work with rgb images, you will probably get 3 or 4 in comps
and you want to have at least 3 in the last argument.如果您使用 rgb 图像,您可能会在comps
中得到 3 或 4,并且您希望在最后一个参数中至少有 3。
The data
buffer returned by stbi_load will containt 8bits * x * y * channelRequested , or x * y * channelCount bytes. stbi_load 返回的data
缓冲区将包含 8bits * x * y * channelRequested 或 x * y * channelCount 个字节。 you can access the (i, j) pixel info as such:您可以这样访问 (i, j) 像素信息:
unsigned bytePerPixel = channelCount;
unsigned char* pixelOffset = data + (i + x * j) * bytePerPixel;
unsigned char r = pixelOffset[0];
unsigned char g = pixelOffset[1];
unsigned char b = pixelOffset[2];
unsigned char a = channelCount >= 4 ? pixelOffset[3] : 0xff;
That way you can have your RGB(A) per-pixel data.这样您就可以获得每像素的 RGB(A) 数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.