QImage provides direct access to pixel data. Convert the QPixmap to a QImage with QPixmap::toImage().
QImage provides direct access to pixel data. Convert the QPixmap to a QImage with QPixmap::toImage().
J-P Nurmi
tommy (3rd December 2007)
Thanks jpn.
It looks like toImage() converts pixmaps to images of different bit depths depending on what the original image was. What I hope to do is to crate a bottleneck in such a way that however the original pixmap was made, the created QImage has exact same file format.
I can actually already achieve what I want by first saving pixmap as a bitmap image (all saved bitmaps have the same bit depths with Qt), and then reopening the bitmap image for data input. The reason I asked my question was to find out if there was a more elegant way that did not involve saving and reopening bitmaps. But I guess there isn't if its true that diffent QImage can have a different file structures ( I'd have to write a separate function for opening each type of QImage). All the bitmaps Qt makes are classical 24bpp rgb images - very convenient for me because I only have to write one function for reading data from any image (they are all the same type of files).
Thanks Elcuco,
Do you know if operating with Qimages (converting and reading them etc) is significantly faster that bitmaps?
How do you open a QImage for data input? It's not really a file that you can open like you open a binary in C/C++, is it?
Hi Tommy,
I loaded the image using QImage::load(), and then I pass it to a ansi C library for manipulation. I know that in most cases, you can treat the bits() function as something similar to this:Qt Code:
dword picture[SIZE_Y][SIZE_Y] ;To copy to clipboard, switch view to plain text mode
It will fail sometimes. Be warned (at least in MACs). If you want to review my code, here it is: http://code.google.com/p/qhocr/
Last edited by elcuco; 3rd December 2007 at 22:08. Reason: updated contents
tommy (3rd December 2007)
Thanks!
So, if my goal is to read all the RGB colors of any image format that Qt supports (without prior knowing which one it's going to be) for read function with one simple function, I just have to convert the file to a QImage and access pixels directly with some pixel retrieving function??
As in?:
Qt Code:
QImage myImage; myImage.load("imagefile.jpg"); for (int i = 0; i<myImage->width(); ++i) { for (int j = 0; j<myImage->height(); ++j){ QRgb pixel = myImage->pixel(i,j);}} remove(myImage); //to free the memory?To copy to clipboard, switch view to plain text mode
Last edited by tommy; 3rd December 2007 at 20:46.
Yes, like that, but to free the memory occupied by a QImage all you have to do is:
tommy (3rd December 2007)
Thanks!
One thing I'm confused about is the following:
If QPixmap is used for displaying and QImage for I/O, then which should I use if I want to do both. I first want to display the "myfile.jpg" as a pixmap using QLabel approach and then I want to open it and get the coordinates of all pixels that are brighter than certain value. I've been doing:
to set the label but setImage() function doesn't even exist? How to set QLabel to QImage?Qt Code:
pixmapLabel->setPixmap(mypixmap);To copy to clipboard, switch view to plain text mode
Use both. Pixmaps are used for display and images for data manipulation.
BTW. Sorry Marcel, I was faster![]()
tommy (3rd December 2007)
I just changed my keyboard.
Yes, it seems it's the same on all platforms:The painful fact is that as fat as I remember the image is going to be converted to a pixmap before being drawn anyway (at least on X11).
Qt Code:
Qt::ImageConversionFlags flags) { QImage im = image; if (baseSize != sr) im = im.copy(qFloor(sr.x()), qFloor(sr.y()), qCeil(sr.width()), qCeil(sr.height())); if (im.depth() == 1) }To copy to clipboard, switch view to plain text mode
Get a joystick or a driving wheel![]()
You can use QPixmap::fromImage( it is static in QPixmap) to obtain a QPixmap. But this approach is usually expensive.
It is better to subclass QLabel and override it's paint event and draw the QImage in there. via QPainter::drawImage().
tommy (3rd December 2007)
Bookmarks