Results 1 to 6 of 6

Thread: Construct QPixmap or QImage from raw data.

  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Construct QPixmap or QImage from raw data.

    Hi all,

    I am writing a plugin for an application. This application have a callback interface that I inherit and when an image arrive the following method is called:

    Qt Code:
    1. void IListener::onImage(int height, int width, int *data);
    To copy to clipboard, switch view to plain text mode 

    In that method I have to create a valid QPixmap or QImage from those height, weight and integer data pointer. If someone know how to do that, please share with me.

    Regards,
    The Storm
    Last edited by The Storm; 28th November 2008 at 15:18.

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Construct QPixmap or QImage from raw data.

    Is that "data" raw pixels with no header?

    QImage has a constructor for width and height where you can supply a format argument indicating how many bits per pixel you have and how they are arranged.
    Then you can copy the data into the QImage line by line using the pointer provided by
    QImage::scanLine (which needs to be properly casted first).
    Last edited by drhex; 28th November 2008 at 14:36.

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

    The Storm (1st December 2008)

  4. #3
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Construct QPixmap or QImage from raw data.

    So basicly you mean that I have to do something like this:

    Qt Code:
    1. QImage image( width, height, format );
    To copy to clipboard, switch view to plain text mode 

    But then how to copy the data ? Could you please be more specific. Is QImage::Format_RGB32 good format, the image have transparent parts btw.

  5. #4
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Construct QPixmap or QImage from raw data.

    Anyone, please. I forgot to mentoin that the data are raw pixels.

  6. #5
    Join Date
    May 2007
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Construct QPixmap or QImage from raw data.

    Tha main problem is what format of data is inside in this integer data pointer. Is it rgba (0xAARRGGBB) or rgb( (0xffRRGGBB) or what. In case of rgba you can do something like this:

    Qt Code:
    1. void onImage(int height, int width, int* data)
    2. {
    3. QImage image(width, height, QImage::Format_ARGB32);
    4. for(int y(0);y < height;++y)
    5. for(int x(0);x < width;++x)
    6. {
    7. int index(y*width + x);
    8. image.setPixel(x, y, data[index]);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Here you have the link to available format of image:
    http://doc.trolltech.com/4.4/qimage.html#Format-enum

  7. The following user says thank you to pgorszkowski for this useful post:

    The Storm (1st December 2008)

  8. #6
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Construct QPixmap or QImage from raw data.

    Thanks a lot. The image is PNG and the format QImage::Format_ARGB32 have done the work. My end realisation actually is a bit differend and I hope more fast:

    Qt Code:
    1. void onImage(int height, int width, int* data)
    2. {
    3. QImage image = QImage( width, height, QImage::Format_ARGB32 );
    4.  
    5. for (int i = 0; i < height; ++i)
    6. memcpy(image.scanLine(i), data + i * width, width * 4);
    7. }
    To copy to clipboard, switch view to plain text mode 

    I hope that this will be useful for others that have my issue.
    Last edited by The Storm; 1st December 2008 at 09:54.

Similar Threads

  1. Replies: 2
    Last Post: 29th September 2008, 00:08
  2. Loading a QPixmap from raw data
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2008, 10:12
  3. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 12:40
  4. QImage loadFromData not run... XPDF data
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2007, 11:51
  5. Creating a QImage from uchar* data
    By forrestfsu in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2007, 15:21

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.