Re: QPixmap pixel by pixel ?
QImage provides direct access to pixel data. Convert the QPixmap to a QImage with QPixmap::toImage().
Re: QPixmap pixel by pixel ?
Hi,
Please see this code from a project of mine:
Code:
....
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....l-manipulation
Re: QPixmap pixel by pixel ?
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).
Re: QPixmap pixel by pixel ?
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?
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
tommy
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.
Quote:
Originally Posted by
tommy
Do you know if operating with Qimages (converting and reading them etc) is significantly faster that bitmaps?
QImages are bitmaps.
Quote:
Originally Posted by
tommy
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.
Re: QPixmap pixel by pixel ?
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?:
Code:
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?
Re: QPixmap pixel by pixel ?
Yes, like that, but to free the memory occupied by a QImage all you have to do is:
Re: QPixmap pixel by pixel ?
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:
Code:
pixmapLabel->setPixmap(mypixmap);
to set the label but setImage() function doesn't even exist? How to set QLabel to QImage?
Re: QPixmap pixel by pixel ?
Use both. Pixmaps are used for display and images for data manipulation.
BTW. Sorry Marcel, I was faster ;)
Re: QPixmap pixel by pixel ?
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().
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
marcel
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).
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
wysota
BTW. Sorry Marcel, I was faster ;)
I just changed my keyboard :).
Quote:
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:
Code:
Qt::ImageConversionFlags flags)
{
QRectF baseSize
(0,
0, image.
width(), image.
height());
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);
}
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
tommy
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:
Code:
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/
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
marcel
I just changed my keyboard :).
How could you? Mine is over ten years old. ;)
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
jacek
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.:)
Re: QPixmap pixel by pixel ?
Get a joystick or a driving wheel :)
Re: QPixmap pixel by pixel ?
Quote:
Originally Posted by
wysota
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.
Re: QPixmap pixel by pixel ?
Maybe you won't be breaking them as fast as you break keyboards.
Re: QPixmap pixel by pixel ?
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!