c++ - 垂直翻转Char数组: is there a more efficient way?

标签 c++ image performance opengl

让我们从一些代码开始:

QByteArray OpenGLWidget::modifyImage(QByteArray imageArray, const int width, const int height){
    if (vertFlip){
        /* Each pixel constist of four unisgned chars: Red Green Blue Alpha.
         * The field is normally 640*480, this means that the whole picture is in fact 640*4 uChars wide.
         * The whole ByteArray is onedimensional, this means that 640*4 is the red of the first pixel of the second row
         * This function is EXTREMELY SLOW
         */
        QByteArray tempArray = imageArray;
        for (int h = 0; h < height; ++h){
            for (int w = 0; w < width/2; ++w){
                for (int i = 0; i < 4; ++i){
                    imageArray.data()[h*width*4 + 4*w + i] = tempArray.data()[h*width*4 + (4*width - 4*w) + i ];
                    imageArray.data()[h*width*4 + (4*width - 4*w) + i] = tempArray.data()[h*width*4 + 4*w + i];
                }
            }
        }
    }
    return imageArray;
}

这是我现在用来垂直翻转 640*480 图像的代码(该图像实际上不能保证是 640*480,但大部分是)。颜色编码为RGBA,这意味着数组总大小为640*480*4。我获得了 30 FPS 的图像,我想以相同的 FPS 在屏幕上显示它们。

在较旧的 CPU (Athlon x2) 上,此代码太多:CPU 正在竞速跟上 30 FPS,所以问题是:我可以更高效地执行此操作吗?

我也在使用 OpenGL,它是否有一个我不知道的技巧,可以以相对较低的 CPU/GPU 使用率翻转图像?

最佳答案

根据this question ,您可以通过按 (1,-1,1) 缩放图像来翻转 OpenGL 中的图像。 This question解释如何进行转换和缩放。

关于c++ - 垂直翻转Char数组: is there a more efficient way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925818/

相关文章:

c++ - 在复制构造函数内调用动态分配的类的复制构造函数?

c++ - Qt/CMake : missing ':' before identifier 'slots' `

c++ - 这是内存泄漏吗,如果不是,是否由标准保证?

search - 谷歌图片搜索: search similar images

c - 在 C 中实现 imagesc

image - Docker Pull 命令与 Docker Build 命令有何不同

r - 如何调整R中Bagging的参数?

oop - 为什么是 "Properties that return arrays are prone to code inefficiencies"?

c++ - 从控制台删除最后打印的元素

performance - 如何以空间和时间高效的方式填充 Data.Map