如何翻转像素数据的Y轴

我正在学习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轴上翻转firstarray的数据。

xutuzi 回答:如何翻转像素数据的Y轴

  

我确实尝试过,但是我无法正确计算数学。

好吧,改而使那个正确得多。您实际上刚刚制作了XY-problem ...

适当的访问器函数可能如下所示:

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)
对于该行一半中的每个像素

(必须为一半,否则您将交换所有值两次,从而再次得到相同的图像)。

,

在堆栈上分配的该大小的数组最有可能进入stack-overflow(双关语),因此请使用向量。

我还从* 4(和尺寸)开始,我认为您正在使用数组来存储图像,因此您可以创建一个结构来存储最里面的尺寸,例如:

struct rgba //or color
{ 
    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
};

然后创建一个包含颜色结构(在我的示例中为rgba)的std::vectorstd::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 };

或者您可以看看一些进行图像处理的库,例如OpenCV

本文链接:https://www.f2er.com/3161245.html

大家都在问