[Qt-interest] Get raw data from QImag
Till Oliver Knoll
till.oliver.knoll at gmail.com
Thu Apr 28 08:31:59 CEST 2011
First off, you really need to extract the raw pixel data from the QImage, not the QPixmap. I think you do that, but just to make sure. (Just for the record: the reason is off course that the pixel format is totally internal inside QPixmap, since very dependent on the underlying hardware/OS).
The fact that your data is "grayscale" doesn't really matter here - what matters is the fact that you get your QImage as a result from "convertToImage", and I strongly assume that you then get a 32 bit RGBA pixel format (check Qt docs)! Or query the pixel format dynamically (isn't there a QImage getter method for that?), but I think the resulting pixel format is always something with 32 bit per pixel.
That said, you also have to take byte alignment into account: IIRC the "end of a scanline" is aligned (padded with dummy data) to a multiple of N bytes (not sure what N is: 4? 8? Again, refer to Qt docs).
In other words: in the general case you can't simply take the first pixels() and then keep going "width * height" times over all successive and adjacent pixel data!
What is save to do is to iterate over a single scanline(), then take the next one etc. Note that in your case you still get RGB data (all values the same I guess), so simply take the highest or lowest byte (again, refer to Qt docs about byte alignment - otherwise you might catch the alpha value, i.e. always 255 ;)
The "cheapest" and least error-prone way (but not the efficient one) is to use pixel(x, y). I think that even gives you a QColor value, no? Or a QRgb value.
Cheers (and sorry for top-posting this time - iEditing is really a pain ;)
Oliver
Am 28.04.2011 um 01:37 schrieb "Cole, Derek" <dcole at integrity-apps.com>:
> Sorry - accidentallly sent last mail.
>
> As I was saying, I want to get the raw data back out of a section of the QGraphicsItem, so I used code like this:
>
> QPixmap fullPmap = topItem->pixmap();
> fullPmap = fullPmap.copy(isec.toRect());
> QImage chip = fullPmap.toImage();
>
>
> so I know that chip has the right section in it, because I can use "display code" like I just posted with chip as the image instead of qi, and it works.
>
> However, I need to get the raw data back from chip, so that I can send it off to another 3rd party library that is expecting a single unsigned char *
>
> In my case, as you can see from the way I created the image in the display code below, I just want to send it a single band, since it has been displayed as grayscale anyway.
>
> I tried to use chip.bits() but that is giving me some data that is unusable, apparently, or I am using it wrong.
>
> So what is the proper way to do this?
>
>
> From: Cole, Derek
> Sent: Wednesday, April 27, 2011 7:33 PM
> To: qt-interest at qt.nokia.com
> Subject: Get raw data from QImage
>
> Hello,
>
> I have populated a QGraphicsPixmapItem with an image using code similar to the following:
>
> QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
> for (int i = 0 ; i < imheight ; i++)
> {
> for (int j = 0 ; j < imwidth ; j++)
> {
> qi->setPixel(j,i,qRgb(imageData[i*imwidth+j],imageData[i*imwidth+j],imageData[i*imwidth+j]));
>
> }
> }
>
> QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));
> if(scene.items().contains(item)){
> scene.removeItem(item);
> item->~QGraphicsItem();
> }
> item = new ImagePixmapItem(p);
> scene.addItem(item);
>
> Now I want to get the data back from one of those QGraphicsItems and I am using some code like this to get the QImage of a particular part of the image:
>
>
>
> _______________________________________________
> 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/20110428/40bed868/attachment.html
More information about the Qt-interest-old
mailing list