如何翻转像素数据的Y轴
How to flip Y axis of pixel data
我正在学习C++,在收集从glMapBuffer到数组的数据后,我想翻转y轴中的数据
unsigned char * Data = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER,
GL_READ_ONLY);
char firstarray[ length * width * 4] ;
memcpy( firstarray , Data , sizeof( firstarray ));
现在我想在y轴上翻转第一个数组的数据。
我确实尝试过,但我无法正确计算。
好吧,最好是让正确。你实际上只是产生了一个XY问题。。。
适当的访问器函数可能如下所示:
unsigned char* getPixel(unsigned int row, unsigend int column)
{
return array + (row * width + column) * 4;
}
unsigned char* getSubPixel(unsigned int row, unsigend int column, unsigned int color)
{
return getPixel(row, column) + color;
}
我想翻转y轴中的缓冲区
假设你想生成一个在x轴镜像的新图像,你可以简单地进行
std::swap(*getSubPixel(x, y, 0), *getSubPixel(x, width - y, 0))
// same for the other three sub-pixels
// if you decide to return references instead of pointers, you don't need
// to dereference (can skip the asterisks)
对于行的half中的每个像素(必须是一半,否则您将两次交换所有值,从而再次生成相同的图像(和每行。
在堆栈上分配的这个大小的数组很可能会被分配到stack-overflow
(双关语(,所以请使用向量。
此外,从* 4
(和维度(来看,我认为您正在使用数组来存储图像,因此您可以创建一个结构来存储最内部的维度,如:
struct rgba //or color
{
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
然后创建一个std::vector,它包含颜色结构(在我的示例中为rgba
(:std::vector<rgba> img;
你用它就像:
int length = 1080;
int width = 1920;
std::vector<rgba> img(length * width);
for (int i = 0; i != length; i++)
for (int j = 0; j != width; j++)
img[i * width + j] = { 255, 255, 255, 255 };
或者你可以看看一些做图像处理的库,例如OpenCV
相关文章:
- OpenGL - 每个三角形的像素数不正确
- C++ 在给定像素数据数组的表单上绘制矩形
- 从位图提取像素数据的问题
- 如何在 sfml 中使用图像中的像素数组
- 如何将 QImage 中的像素数据转换为其他格式
- 如何将CImg函数与像素数据一起使用?
- 将原始像素数据转换为qpixmap
- qimage:如何正确破坏指向像素数据的指针
- 如何通过 c++ 获取图像的所有像素数据 (RGB)
- 如何从窗口中获取像素数据像素缓冲区并提取RGB
- 设备定位的像素数组(从 C++ AMP)到 使用 D3D / D2D 的窗口
- SetPixel在像素数量后崩溃
- I'm试图将像素数据转换为OpenCV Mat对象
- 使用opencv将像素数据分配到向量中
- 如何在 c++ 中最有效地将像素数组呈现到窗口
- 将浮点像素数据从文件中读回 C#
- 在窗口中的屏幕上获取坐标像素数据的简单方法
- 有没有办法在不复制所有像素的情况下将现有的像素数组转换为位图
- 在 C 或 C++ 中将 JPEG 或 PNG 图像作为像素数据打开
- 如何写入原始像素数据并另存为位图