Results 1 to 14 of 14

Thread: QImage from 8 bit char* RGB buffer

  1. #1
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default QImage from 8 bit char* RGB buffer

    This is my first post so hello to everybody
    I't trying to create qt app which will display images from 8 bit RGB buffer.
    I tried warious ways to achiewe it, but so far without success. Here is what I've tried :

    ...
    QImage img;
    img.loadFromData( (uchar*)myRBGBuffer );
    QPixmap pixmap;
    pixmap.convertFromImage(img);
    ...

    but this gives me incorrect results - whole pixmap is in one color.
    The I tried adding PPM header - "P6 width height nColors", but that did't help also.

    My question is - how can I display images from RGB buffer, as fast as possible, avoiding unnecessary conversions - I'm trying to create mobile robot driver ap, so time is crucial.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage from 8 bit char* RGB buffer

    You need to set a palette (color mapping table) for the image.

    From QImage docs:
    8-bpp images are much easier to work with than 1-bpp images because they have a single byte per pixel:
    QImage image;
    // set entry 19 in the color table to yellow
    image.setColor( 19, qRgb(255,255,0) );
    // set 8 bit pixel at (x,y) to value yellow (in color table)
    *(image.scanLine(y) + x) = 19;

  3. #3
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    Thanks for reply. Unfortunatelly it did't help.Here is my code:
    ...
    imgData = cvLoadImage( s, -1 ); //function that loads image
    QImage img;
    img.create( imgData->width, imgData->height, 8, 255 );
    for ( int i=0; i<255; i++ ) // build color table
    img.setColor( i, qRgb(i,0,0) );
    img.loadFromData( (uchar*)imgData->imageData, imgData->widthStep*imgData->height, 0 );
    QPixmap pixmap;
    pixmap.convertFromImage(img);
    m_pixmap->setPixmap(pixmap);
    ...
    but the displayed pixmap is black

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

    Default Re: QImage from 8 bit char* RGB buffer

    Check if the QImage object is ok (for example by saving it to file and viewing with an image viewer).

  5. #5
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    Quote Originally Posted by wysota
    Check if the QImage object is ok (for example by saving it to file and viewing with an image viewer).
    Unfortunatelly it isn't
    The rgb data is ok, since I can save using opencv library (which I'm using to do some image processing), but I can't construct proper qimage from it ...

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

    Default Re: QImage from 8 bit char* RGB buffer

    QImage::fromData() expects data in a format like BMP or PNG, not just an array of colours.

    Try:
    Qt Code:
    1. QImage img;
    2. img.create( imgData->width, imgData->height, 8 );
    3. for ( int i = 0; i < imgData->width; ++i ) {
    4. for ( int j = 0; j < imgData->height; ++j ) {
    5. img.setPixel( i, j, ... );
    6. }
    7. }
    8. m_pixmap->setPixmap( img );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    Quote Originally Posted by jacek
    QImage::fromData() expects data in a format like BMP or PNG, not just an array of colours.

    Try:
    Qt Code:
    1. QImage img;
    2. img.create( imgData->width, imgData->height, 8 );
    3. for ( int i = 0; i < imgData->width; ++i ) {
    4. for ( int j = 0; j < imgData->height; ++j ) {
    5. img.setPixel( i, j, ... );
    6. }
    7. }
    8. m_pixmap->setPixmap( img );
    To copy to clipboard, switch view to plain text mode 
    I know about it, but I have to do some processing and then show images as fast as possible therefore coping image data pixel by pixel isn't something that I want. I'm hoping to achieve ~25fps (with some processing included).Surely it is possible with qt, isn't it ?

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

    Default Re: QImage from 8 bit char* RGB buffer

    Quote Originally Posted by tboloo
    therefore coping image data pixel by pixel isn't something that I want. I'm hoping to achieve ~25fps (with some processing included).
    It depends how on the way you store that image data. If all pixels are in one block and their representation is compatible with QRgb, you could use qmemmove instead.

    http://www.qtcentre.org/forum/showthread.php?t=164

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage from 8 bit char* RGB buffer

    If you want to do 25fps, avoid converting between a QImage and QPixmap (which is said to be slow). It's better to blit the image directly without conversion.

  10. #10
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    I've updated code to
    Qt Code:
    1. imgData = cvLoadImage( s, -1 );
    2. cvSaveImage("cvImage.png",imgData );
    3. QImage img;
    4. img.create( imgData->width, imgData->height, 8, 255 );
    5. unsigned char *buff = img.bits();
    6. memcpy(buff,(uchar*)imgData->imageData,
    7. imgData->widthStep*imgData->height);
    8. for ( int i=0; i<255; i++ ) // build color table
    9. img.setColor( i, qRgb(i,0,0) );
    10. img.save( "qtimage", "PNG", 0 );
    11. QPixmap pixmap;
    12. pixmap.convertFromImage(img);
    13. m_pixmap->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 
    but result is somehow far from what I expect
    This is the orginal that I try to load cvimage.png
    and this is the result qtimage.png

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage from 8 bit char* RGB buffer

    If you'd set appropriate colours, it would be easier to see what's wrong. At least make them gray ( img.setColor( i, qRgb(i,i,i) ); ).

  12. #12
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    Quote Originally Posted by wysota
    If you'd set appropriate colours, it would be easier to see what's wrong. At least make them gray ( img.setColor( i, qRgb(i,i,i) ); ).
    I doesn't change much , it just becomes gray instead of red, see qtimage_gray.png
    Conserning your previous post I'll try another approaches as soon as I'll get images displayed correctly.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage from 8 bit char* RGB buffer

    Well, that's much better

    My guess is that this cv holds data in some other order than QImage expects. The image is interlaced and each second scanline is translated to the right.

  14. #14
    Join Date
    Apr 2006
    Posts
    7
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QImage from 8 bit char* RGB buffer

    Quote Originally Posted by wysota
    Well, that's much better

    My guess is that this cv holds data in some other order than QImage expects. The image is interlaced and each second scanline is translated to the right.
    I guess you're right. I think I'll have to play with scanLine() function, althoug I'm not sure whether I'll be able to do all necessary stuff in less then 40ms .
    Anyway thanks for your help and quick response.

Similar Threads

  1. get QImage object from buffer
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2012, 02:03
  2. Replies: 12
    Last Post: 7th September 2011, 16:37
  3. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 22:05
  4. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 13:10
  5. QImage buffer
    By rafaelsegundo in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2008, 06:48

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.