[Qt-interest] Building an image from raw unsigned char array

Cole, Derek dcole at integrity-apps.com
Wed Jan 19 19:12:53 CET 2011


That last bit of code I posted was the working code, with the correct image output.

The library that outputs the image is visible to me, and I am able to see that it was simply outputting 3 chars per pixel, one for each Red Green and Blue

Im not sure why the other ways did not work, though I may try to fix that loop index problem that Jon pointed out earlier - I hadnt noticed that before.

Thanks for the help everyone

Derek

-----Original Message-----
From: mathias.malmqvist at nokia.com [mailto:mathias.malmqvist at nokia.com]
Sent: Wed 1/19/2011 12:53 PM
To: Cole, Derek; jmalacho at gmail.com
Cc: qt-interest at qt.nokia.com
Subject: RE: [Qt-interest] Building an image from raw unsigned char array
 

I've not been following the discussion in detail, but I think you said the 
picture came out distorted. Perhaps it's because the width of a scan-line
is not exactly W times 3 bytes. There's often padding at the end of each 
line to make it an even 32-bits (or 4 times 32-bit or whatever alignment 
the targeted hardware likes). Have you considered this?


Cheers
Mathias

________________________________________
From: qt-interest-bounces+mathias.malmqvist=nokia.com at qt.nokia.com [qt-interest-bounces+mathias.malmqvist=nokia.com at qt.nokia.com] on behalf of ext Cole, Derek [dcole at integrity-apps.com]
Sent: Wednesday, January 19, 2011 5:25 PM
To: Jon Malachowski
Cc: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Building an image from raw unsigned char array

I guess I didnt follow you there - if j is zero, i was reading in j=0, j=1 and j=2, then adding 3 to j for the next column - j=3, j=4, j=5 and so on...

In any event, I think I have figured the issue out with the help of a poster on stackoverflow.

This is what I ended up using:

    QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB888);
    for (int h = 0; h<imheight; h++){
        memcpy(qi->scanLine(h), imageData[h], imwidth*3);
    }
    QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
    QPixmap p1(p.scaled(ui->menuBar->width(),ui->menuBar->width(), Qt::KeepAspectRatio, Qt::SmoothTransformation ));

    ui->viewLabel->setPixmap(p1);
    ui->viewLabel->setFixedHeight(p1.height());
    ui->viewLabel->setFixedWidth(p1.width());

imageData was stored as an unsigned char**, and was declared like so:

unsigned char **imageData = new unsigned char *[H];
for(int i = 0; i < H; i++)
{

    imageData[i] = new unsigned char[W*3];
}

where H and W are height and width of image in pixels.



-----Original Message-----
From: Jon Malachowski [mailto:jmalacho at gmail.com]
Sent: Wed 1/19/2011 12:06 PM
To: Cole, Derek
Cc: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Building an image from raw unsigned char array

I'm not sure if anyone has pointed this out, but your inter loop iteration
across imageData is strange.  I wouldn't normally consider it but you (1)
said the image was garbled, and (2) your 'j' iterator is non standard and
non-descriptive, and (3) you havn't given us a full definition of how the
data is stored in imageData.

"
        for (int j = 0 ; j < imwidth ; j++)
                {

qi->setPixel(j,i,qRgb(imageData[i][j],imageData[i][j+1],imageData[i][j+2]));
                       j+=3;

                   }
"
In this code, you are skipping through 4 X sizeof(char) each time.  On my
machine that would be 4 bytes and not 3.  Said another way, you are reading
byte X,X+1, X+2 into a pixel, and then skipping X+3, and then setting
"x+=3."
 Is that what you mean to do? Is each pixel padded with useless data or an
alpha byte you are intentionally skipping?
JPM

On Tue, Jan 18, 2011 at 7:52 PM, Cole, Derek <dcole at integrity-apps.com>wrote:

>
> Hello,
>
> I have image data coming in from a C++ function as an unsigned char**
>
> It was defined to he H*W*3 in size, so there are 3 values for each pixel -
> a Red Green and Blue
>
> How is the best way to pull this data in to Qt so taht it can be displayed
> in a label?
>
> I thought I could use
>
>         QImage *qi = new QImage(imwidth, imheight, QImage::Format_ARGB32);
>             for (int i = 0 ; i < imheight ; i++)
>                 for (int j = 0 ; j < imwidth ; j++)
>                 {
>
> qi->setPixel(j,i,qRgb(imageData[i][j],imageData[i][j+1],imageData[i][j+2]));
>                        j+=3;
>
>                    }
>
>        QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
>        QPixmap p1(p.scaled(ui->menuBar->width(),ui->menuBar->width(),
> Qt::KeepAspectRatio, Qt::SmoothTransformation ));
>        ui->viewLabel->setPixmap(p1);
>        ui->viewLabel->setFixedHeight(p1.height());
>        ui->viewLabel->setFixedWidth(p1.width());
>
> But this gives me distorted results. imageData is what holds the unsigned
> char** passed back from the other function.
>
> Thanks in advance
>
> derek
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110119/1abd202c/attachment.html 


More information about the Qt-interest-old mailing list