Raw Data for QImage

I am new to graphic programming (pixels, images, etc.). I am trying to convert Raw data to QImage and display it on QLabel. The problem is that the raw data can be any data (in fact, this is not an image of the raw data, it is a binary file). The reason is because in order to understand how pixels work and something like that, I know that I am getting a random image with unusual results, but it will work. I am doing something like this, but I think I am 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 right? By the way, can you tell me where I should study this? Thank!

+5
source share
3 answers

. QImage - , , .

:

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());
}

rawData - .

+7

BGRA :

QImage image((const unsigned char*)pixels, width, height, QImage::Format_RGB32);
image.save("out.jpg");
+4

Syntactically, your code looks correct.

By reading the class signature, you can call setPixel as follows:

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

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

In addition, declaring a bare pointer is dangerous. The following code is equivalent:

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

And it will be freed after the function is completed.

Qt Examples are a great place to look for functionality. Also, study class documentation because they are littered with examples.

0
source

All Articles