Raw data to QImage
Asked Answered
K

3

7

I'm new to graphics programming (pixels, images, etc..) I'm trying to convert Raw data to QImage and display it on QLabel. The problem is that, the raw data can be any data (it's not actually image raw data, it's binary file.) The reason if this is that, to understand deeply how pixels and things like that work, I know I'll get random image with weird results, but it will work. I'm doing something like this, but I think I'm doing it wrong!

QImage *img = new QImage(640, 480, QImage::Format_RGB16); //640,480 size picture.
//here I'm trying to fill newly created QImage with random pixels and display it.
for(int i = 0; i < 640; i++)
{
    for(int u = 0; u < 480; u++)
    {
        img->setPixel(i, u, rawData[i]);
    }
}
ui->label->setPixmap(QPixmap::fromImage(*img));

am I doing it correctly? By the way, can you point me where should I learn these things? Thank you!

Kealey answered 28/1, 2013 at 13:32 Comment(1)
no need to create img dynamically if you are going to use QPixmap::fromImage as it allocate heap memory for the pixmap. And QImage use copy on write.Farl
A
7

Overall it's correct. QImage is a class that allows to manipulate its own data directly, but you should use correct pixel format.

A bit more efficient example:

QImage* img = new QImage(640, 480, QImage::Format_RGB16);
for (int y = 0; y < img->height(); y++)
{
    memcpy(img->scanLine(y), rawData[y], img->bytesPerLine());
}

Where rawData is a two-dimension array.

Arboreous answered 28/1, 2013 at 13:53 Comment(5)
Hi, when I tried your example, all the raw data shows same result. rawData is QByteArray and I'm using it like &rawData[y], I'm wrong right?Kealey
No, &rawData[y] isn't correct, because you will copy the same pixels many times. You should use: rawData.constData() + y * img->bytesPerLine(). In this case on the each iteration new peace of data will be copied.Arboreous
I faced another problem here. I don't know how long picture should be, I mean width and height and sometimes it crashes when I pass less than 2kb data.. What is the best way to calculate it?Kealey
How about creating new question? Where from are you reading data? Why don't you know the size of picture? Could you describe in detail what you are doing?Arboreous
Okay, will write new question :)Kealey
G
4

This is how I saved a raw BGRA frame to the disk:

QImage image((const unsigned char*)pixels, width, height, QImage::Format_RGB32);
image.save("out.jpg");
Gyrostatics answered 14/11, 2014 at 15:39 Comment(1)
Should work for RGB32 data, but one have to keep in mind that for smaller pixel types (for example Format_Grayscale8), lines have to be 32-bytes aligned.Hershel
G
0

Syntactically, your code appears to be correct.

Reading the class signature, you may want to call setPixel in the following manner:

img->setPixel(i, u, QRbg(##FFRRGGBB));

Where ##FFRRGGBB is a color quadruplet, unless, of course, you want monochrome 8 bit support.

Additionally, declaring a naked pointer is dangerous. The following code is equivalent:

QImage image(640, 480, QImage::Format_something);
QPixmap::fromImage(image);

And will deallocate appropriately upon function completion.

Qt Examples directory is a great place to search for functionality. Also, peruse the class documentation because they're littered with examples.

Goodish answered 28/1, 2013 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.