让我们从一些代码开始:
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 使用率相对较低的图像?