Results 1 to 20 of 20

Thread: QPixmap pixel by pixel ?

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QPixmap pixel by pixel ?

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPixmap pixel by pixel ?

    QImage provides direct access to pixel data. Convert the QPixmap to a QImage with QPixmap::toImage().
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    tommy (3rd December 2007)

  4. #3
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Hi,

    Please see this code from a project of mine:
    Qt Code:
    1. QImage scannedImage;
    2. ....
    3. hocr_pix->n_channels = scannedImage.depth() / 8;
    4. hocr_pix->height = scannedImage.size().height();
    5. hocr_pix->width = scannedImage.size().width();
    6. hocr_pix->rowstride = scannedImage.bytesPerLine();
    7. hocr_pix->pixels = scannedImage.bits();
    To copy to clipboard, switch view to plain text mode 

    Look in assistant for QImage::bits();

    See also this documentation:
    http://doc.trolltech.com/4.3/qimage....l-manipulation

  5. The following user says thank you to elcuco for this useful post:

    tommy (3rd December 2007)

  6. #4
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

  7. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by tommy View Post
    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 View Post
    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 View Post
    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.

  9. The following user says thank you to jacek for this useful post:

    tommy (3rd December 2007)

  10. #7
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?:

    Qt Code:
    1. QImage myImage;
    2. myImage.load("imagefile.jpg");
    3. for (int i = 0; i<myImage->width(); ++i) {
    4. for (int j = 0; j<myImage->height(); ++j){
    5. QRgb pixel = myImage->pixel(i,j);}}
    6. remove(myImage); //to free the memory?
    To copy to clipboard, switch view to plain text mode 
    Last edited by tommy; 3rd December 2007 at 19:46.

  11. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Yes, like that, but to free the memory occupied by a QImage all you have to do is:
    Qt Code:
    1. myImage = QImage();
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to marcel for this useful post:

    tommy (3rd December 2007)

  13. #9
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. pixmapLabel->setPixmap(mypixmap);
    To copy to clipboard, switch view to plain text mode 
    to set the label but setImage() function doesn't even exist? How to set QLabel to QImage?

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QPixmap pixel by pixel ?

    Use both. Pixmaps are used for display and images for data manipulation.

    BTW. Sorry Marcel, I was faster

  15. The following user says thank you to wysota for this useful post:

    tommy (3rd December 2007)

  16. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  17. The following user says thank you to marcel for this useful post:

    tommy (3rd December 2007)

  18. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by marcel View Post
    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).

  19. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by wysota View Post
    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:
    Qt Code:
    1. void QPaintEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr,
    2. Qt::ImageConversionFlags flags)
    3. {
    4. QRectF baseSize(0, 0, image.width(), image.height());
    5. QImage im = image;
    6. if (baseSize != sr)
    7. im = im.copy(qFloor(sr.x()), qFloor(sr.y()),
    8. qCeil(sr.width()), qCeil(sr.height()));
    9. if (im.depth() == 1)
    10. im = im.convertToFormat(QImage::Format_RGB32);
    11. QPixmap pm = QPixmap::fromImage(im, flags);
    12. drawPixmap(r, pm, QRectF(QPointF(0, 0), pm.size()));
    13. }
    To copy to clipboard, switch view to plain text mode 

  20. #14
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by tommy View Post
    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:
    1. 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 21:08. Reason: updated contents

  21. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by marcel View Post
    I just changed my keyboard .
    How could you? Mine is over ten years old.

  22. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by jacek View Post
    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.

  23. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QPixmap pixel by pixel ?

    Get a joystick or a driving wheel

  24. #18
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap pixel by pixel ?

    Quote Originally Posted by wysota View Post
    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.

  25. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QPixmap pixel by pixel ?

    Maybe you won't be breaking them as fast as you break keyboards.

  26. #20
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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!

Similar Threads

  1. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.