简体   繁体   English

rgb值到QImage的向量

[英]Vector of rgb values to QImage

Hi the context in which I need help is as follows. 嗨,我需要帮助的上下文如下。 I construct a vector of rgb values (one rgb value is an UInt32) and i want to put them as fast as possible in a QImage preferably without setPixel as it is very slow. 我构造了一个rgb值的向量(一个rgb值是一个UInt32),我想将它们尽可能快地放入QImage中,最好不要使用setPixel,因为它非常慢。 To take the first rgb value and put it at [0,0] in the image, next value at [0,1] and so on. 要获取第一个rgb值并将其置于图像中的[0,0],将下一个值置于[0,1],依此类推。 Can anyone help me with this? 谁能帮我这个?

This QImage constructor is what you need. 您需要 QImage构造函数。 The trick is only in choosing right Format . 诀窍仅在于选择正确的Format I guess in your case it would be QImage::Format_RGB32. 我想在您的情况下,它将是QImage :: Format_RGB32。

So try: 因此,请尝试:

std::vector<int32_t> pixels{/*init or fill-up your data*/};
int width = 42, height = 42; // your valid values here

// what you're interesting in
auto image(Qimage((uchar *)pixels.data(), width, height, QImage::Format_RGB32));

This approach is the fastest possible way - the pixels will not be copied, but original buffer will be used internally by QImage. 这种方法是最快的方法-不会复制像素,但是QImage将在内部使用原始缓冲区。

If you're going to render your image later with QPainter::drawImage , don't forget to pass Qt::NoFormatConversion as a flag - this will improve your app performance. 如果以后要使用QPainter :: drawImage渲染图像,请不要忘记将Qt :: NoFormatConversion作为标志传递-这将提高应用程序性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM