Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raw data to QImage

Tags:

qt

qimage

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!

like image 833
Nika Avatar asked Jan 28 '13 13:01

Nika


1 Answers

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.

like image 60
york.beta Avatar answered Sep 22 '22 12:09

york.beta