PDA

View Full Version : QPixmap pixel by pixel ?



tommy
3rd December 2007, 18:03
Hi,

Is it possible to read QPixmap info pixel by pixel? I know how to read bitmap info pixel by pixel (open as binary, find the beginnning of pixel info and start reading) but can I do the same with QPixmaps?
So, if I wanted to get the RGB colors of the second pixel of the first row of my QPixmap, how would I do that?

jpn
3rd December 2007, 18:44
QImage provides direct access to pixel data. Convert the QPixmap to a QImage with QPixmap::toImage().

elcuco
3rd December 2007, 18:48
Hi,

Please see this code from a project of mine:


QImage scannedImage;
....
hocr_pix->n_channels = scannedImage.depth() / 8;
hocr_pix->height = scannedImage.size().height();
hocr_pix->width = scannedImage.size().width();
hocr_pix->rowstride = scannedImage.bytesPerLine();
hocr_pix->pixels = scannedImage.bits();


Look in assistant for QImage::bits();

See also this documentation:
http://doc.trolltech.com/4.3/qimage.html#pixel-manipulation

tommy
3rd December 2007, 18:58
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).

tommy
3rd December 2007, 19:03
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?

jacek
3rd December 2007, 19:14
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).
You can convert QImage between different formats.


Do you know if operating with Qimages (converting and reading them etc) is significantly faster that bitmaps?
QImages are bitmaps.


How do you open a QImage for data input?
QImage is stored in memory, so you don't have to "open" it. You can change it whenever you want, you just have to create it first.

tommy
3rd December 2007, 19:38
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?:



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?

marcel
3rd December 2007, 19:52
Yes, like that, but to free the memory occupied by a QImage all you have to do is:


myImage = QImage();

tommy
3rd December 2007, 20:00
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:


pixmapLabel->setPixmap(mypixmap);

to set the label but setImage() function doesn't even exist? How to set QLabel to QImage?

wysota
3rd December 2007, 20:17
Use both. Pixmaps are used for display and images for data manipulation.

BTW. Sorry Marcel, I was faster ;)

marcel
3rd December 2007, 20:17
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().

wysota
3rd December 2007, 20:18
It is better to subclass QLabel and override it's paint event and draw the QImage in there. via QPainter::drawImage().

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).

marcel
3rd December 2007, 20:54
BTW. Sorry Marcel, I was faster ;)
I just changed my keyboard :).



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).

Yes, it seems it's the same on all platforms:


void QPaintEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr,
Qt::ImageConversionFlags flags)
{
QRectF baseSize(0, 0, image.width(), image.height());
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)
im = im.convertToFormat(QImage::Format_RGB32);
QPixmap pm = QPixmap::fromImage(im, flags);
drawPixmap(r, pm, QRectF(QPointF(0, 0), pm.size()));
}

elcuco
3rd December 2007, 21:07
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:
dword picture[SIZE_Y][SIZE_Y] ;

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/

jacek
3rd December 2007, 21:34
I just changed my keyboard :).
How could you? Mine is over ten years old. ;)

marcel
3rd December 2007, 21:45
How could you? Mine is over ten years old. ;)
I changed about 5 keyboards in the last year. The arrow keys get broken from too much aggressive driving in Need for Speed.:)

wysota
3rd December 2007, 22:33
Get a joystick or a driving wheel :)

marcel
3rd December 2007, 22:35
Get a joystick or a driving wheel :)
Hey, I don't afford breaking 5 wheels in a year! :) Maybe 5 joysticks(cheap ones), but still it's cheaper with keyboards.

wysota
3rd December 2007, 22:41
Maybe you won't be breaking them as fast as you break keyboards.

marcel
3rd December 2007, 22:52
I think I'm gonna buy one of those things tomorrow :). I don't know how fast it's going to break, but I just remembered how cool they are, I played with one some time ago, and it was pretty cool.

Look what you made me do!