Results 1 to 7 of 7

Thread: QImage from uchar buffer

  1. #1
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default QImage from uchar buffer

    I'm trying to create a QImage from a buffer, but it doesn't work. If I use setPixel() it works, but if I use a uchar buffer, the Image won't be drawn.

    Any idea why?

    Qt Code:
    1. int imwidth = 4;
    2. int imheight = 16;
    3.  
    4. uchar BUF[] = {
    5. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    6. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    7. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    8. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    9. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    10. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    11. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    12. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    13. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    14. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    15. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    16. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    17. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    18. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    19. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
    20. 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0
    21. };
    22.  
    23.  
    24. QImage qi(BUF, imwidth, imheight, QImage::Format_ARGB32); // doesn't work
    25. QRect s(0, 0, imwidth, imheight);
    26. painter->drawImage(s, qi, s, Qt::AutoColor);
    To copy to clipboard, switch view to plain text mode 

    http://developer.qt.nokia.com/doc/qt-4.8/qimage.html

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QImage from uchar buffer

    First, you need to figure out whether the image it is a) not drawn or b) not created properly... I'd suggest the latter, as you have 16 x 16 bytes in your buffer, but you need 16 x 16 x 4 bytes...

  3. #3
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage from uchar buffer

    Quote Originally Posted by mvuori View Post
    I'd suggest the latter, as you have 16 x 16 bytes in your buffer, but you need 16 x 16 x 4 bytes...
    The buffer is 256 bytes long, because it is a 4x16 pixel image!

    Never mind, I solved the problem.

    The image was drawn, but I couldn't see it, because the Alpha channel was at full transparency (0).

    It's a little bit weird, because the documentation says QImage::Format_ARGB32 is represented as 0xAARRGGBB.

    But in memory the uchar has to be in reverse order. That means, the first 4 bytes are actually BB, GG, RR, AA and not AA, RR, GG, BB as I thought.
    Last edited by StarShaper; 21st February 2012 at 00:56.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QImage from uchar buffer

    The behaviour is not weird: the documented 0xAARRGGBB describes a 32-bit unsigned int. Ints have a platform-dependent byte order (usually liitle-endian). You can do this:
    Qt Code:
    1. quint32 BUF[] = {
    2. 0xffff0000, 0xffff0000, 0xffff0000, 0xffff0000,
    3. ...
    4. // or this
    5. qRgb(255, 0, 0), qRgb(255, 0, 0), qRgb(255, 0, 0), qRgb(255, 0, 0),
    6. ...
    7. };
    8.  
    9. QImage qi(reinterpret_cast<unsigned char*>(BUF), imwidth, imheight, QImage::Format_ARGB32);
    To copy to clipboard, switch view to plain text mode 
    and let the compiler or Qt worry about byte ordering.

  5. #5
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage from uchar buffer

    Quote Originally Posted by ChrisW67 View Post
    The behaviour is not weird: the documented 0xAARRGGBB describes a 32-bit unsigned int. Ints have a platform-dependent byte order (usually liitle-endian).
    I always thought that ints are stored big-endian on most systems. Now it seems logical...

    Although the ubiquitous x86 of today use little-endian storage for all types of data (integer, floating point, BCD), there have been...

    http://en.wikipedia.org/wiki/Endianness
    Unfortunately I can't allocate the buffer as quint32. It has to be uchar.
    Last edited by StarShaper; 21st February 2012 at 00:59.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QImage from uchar buffer

    Unfortunately I can't allocate the buffer as quint32. It has to be uchar.
    Works fine for me in the example I posted. You have to pass a pointer to uchar, hence the reinterpret_cast, but the buffer can be structured as int (with care). Doing this should also ensure that the data buffer meets the 32-bit aligned requirement.

    Edit: As an aside the only big-endian CPUs I have worked with directly are the Motorola 68000 and GEC 4000 series.
    Last edited by ChrisW67; 21st February 2012 at 05:15.

  7. #7
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage from uchar buffer

    Quote Originally Posted by ChrisW67 View Post
    Works fine for me in the example I posted. You have to pass a pointer to uchar, hence the reinterpret_cast, but the buffer can be structured as int (with care). Doing this should also ensure that the data buffer meets the 32-bit aligned requirement.
    You misunderstood me. I know what you are doing with reinterpret_cast(uchar*). I meant that I can't allocate the buffer as quint32, because I already got the buffer as uchar.

    This is the correct buffer.

    Qt Code:
    1. // Image (ARGB) with 4x16 pixel and with a length of 256 bytes, full red and 0 % (255) transparency.
    2. // Little-endian byte order with Blue, Green, Red, Alpha, ...
    3. //uint8_t BUF[] = {
    4. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    5. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    6. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    7. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    8. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    9. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    10. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    11. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    12. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    13. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    14. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    15. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    16. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    17. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255,
    18. // 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255
    19. //};
    To copy to clipboard, switch view to plain text mode 
    Last edited by StarShaper; 21st February 2012 at 14:26.

Similar Threads

  1. Replies: 2
    Last Post: 19th July 2018, 06:38
  2. get QImage object from buffer
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2012, 02:03
  3. QImage buffer
    By rafaelsegundo in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2008, 06:48
  4. Creating a QImage from uchar* data
    By forrestfsu in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2007, 15:21
  5. QImage from 8 bit char* RGB buffer
    By tboloo in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2006, 19:56

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.