在数组中设置像素颜色
Set Pixel colour in an array
我有一个像素数组存储在一个向量中,如下所示:
typedef union RGBA
{
std::uint32_t Colour;
struct
{
std::uint8_t R, G, B, A;
};
} *PRGB;
std::vector<RGBA> Pixels; //My pixels are read into this vector.
我使用以下两个函数处理它。一个是用于阅读,另一个用于写作。read 函数获取一个字节数组并翻转它们并将它们存储到上面的结构中。它考虑了填充,因此它适用于 24 位和 32 位位图。write 函数将其翻转回来并将其写入字节数组。
void ReadPixels(const std::uint8_t* In, RGBA* Out)
{
for (std::size_t I = 0; I < height; ++I)
{
for (std::size_t J = 0; J < width; ++J)
{
Out[(height - 1 - I) * width + J].B = *(In++);
Out[(height - 1 - I) * width + J].G = *(In++);
Out[(height - 1 - I) * width + J].R = *(In++);
Out[(height - 1 - I) * width + J].A = (BitsPerPixel > 24 ? * (In++) : 0xFF);
}
if(BitsPerPixel == 24)
In += (-width * 3) & 3;
}
}
void WritePixels(const RGBA* In, std::uint8_t* Out)
{
for (std::size_t I = 0; I < height; ++I)
{
for (std::size_t J = 0; J < width; ++J)
{
*(Out++) = In[(height - 1 - I) * width + J].B;
*(Out++) = In[(height - 1 - I) * width + J].G;
*(Out++) = In[(height - 1 - I) * width + J].R;
if (BitsPerPixel > 24)
*(Out++) = In[(height - 1 - I) * width + J].A;
}
if(BitsPerPixel == 24)
Out += (-width * 3) & 3;
}
}
问题是,如果我只想更改数组中的一个像素,我必须翻转并将整个图像复制到矢量中,使用以下方法更改像素:
inline void SetPixel(int X, int Y, std::uint32_t Color)
{
Pixels[Y * width + X].Colour = Color;
}
然后将其翻转回数组中。有没有更好的方法来更改数组中的单个像素而不必每次都这样做?
我尝试了这个公式(以便考虑填充):
ByteArray[((height - 1 - Y) * width + X) + (Y * ((-width * 3) & 3))] = Color;
但它不起作用。有什么想法吗?
你的下标>索引公式看起来全错了。
也许:
int stride = width * BitsPerPixel/8;
stride = ((stride - 1) & ~3) + 4; // round up to multiple of 4 bytes
RGBQUAD& selected_pixel = *reinterpret_cast<RGBQUAD*>(array + stride * (height - 1 - Y)) + X * BitsPerPixel/8);
selected_pixel.R = ...
...
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 无法获取窗口 C++ 的像素颜色
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 从SDL_Texture访问像素颜色
- 结构像素颜色值
- 将OpenCV中的像素颜色映射到预定的颜色列表中
- magick 获得像素颜色.PixelPacket似乎不再存在了
- 在数组中设置像素颜色
- 在 openCV 中更改单个像素颜色
- 获取图像的像素颜色
- 使用 BMP 作为高度图.如何访问像素颜色以设置高度
- 以最快的方式获取像素颜色
- glsl-获取像素颜色[像素着色器]
- 如何访问位图中的像素颜色
- 快板定义像素颜色
- 使用像素颜色的人工神经网络
- 使用magick++获取像素颜色
- FreeImage:获取像素颜色
- 从指定坐标[x, y]处的位图数据获取像素颜色