如何翻转像素数据的Y轴

How to flip Y axis of pixel data

本文关键字:数据 像素数 像素 何翻转 翻转      更新时间:2023-10-16

我正在学习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