Andrei Vasilcoi
Andrei Vasilcoi

Reputation: 29

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. To take the first rgb value and put it at [0,0] in the image, next value at [0,1] and so on. Can anyone help me with this?

Upvotes: 2

Views: 1435

Answers (1)

Youw
Youw

Reputation: 879

This QImage constructor is what you need. The trick is only in choosing right Format. I guess in your case it would be 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.

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.

Upvotes: 5

Related Questions