Results 1 to 4 of 4

Thread: Convert RAW 8 bit pixels into a QImage

  1. #1
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Thanks
    4
    Platforms
    MacOS X Unix/X11

    Exclamation Convert RAW 8 bit pixels into a QImage

    Hi, I would like to know how can I convert an array of unsigned chars which represents a grayscale image in raw format into a QImage object, since I have been trying with Qt's QImage::loadFromData() unsuccessfully. Also, if there is any source of information on Qt's native image processing API, please let me know because Trolltech's official C++ Qt GUI Programming with Qt has become not enough for me.

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

    Default Re: Convert RAW 8 bit pixels into a QImage

    QImage::loadFromData() expects the data to contain a header. Just construct a QImage object with suitable size and format and access its bits().
    J-P Nurmi

  3. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert RAW 8 bit pixels into a QImage

    Quote Originally Posted by danielperaza View Post
    Hi, I would like to know how can I convert an array of unsigned chars which represents a grayscale image in raw format into a QImage object, since I have been trying with Qt's QImage::loadFromData() unsuccessfully. Also, if there is any source of information on Qt's native image processing API, please let me know because Trolltech's official C++ Qt GUI Programming with Qt has become not enough for me.
    This is how I do it:

    Qt Code:
    1. uchar** currImageData = &(frameData.value(frameName)->imageData);
    2.  
    3. // check if the frame dimensions have changed
    4. if(!(*currImageData) || frame->width != frameData.value(frameName)->width || frame->height != frameData.value(frameName)->height) {
    5. if(*currImageData) {
    6. qDebug() << "deleting data";
    7. delete[] *currImageData;
    8. }
    9. qDebug() << "allocating" << frame->nChannels << " channel image of size" << frame->width << frame->height << "and depth" << frame->depth;
    10. imageHeight = frame->height;
    11. *currImageData = new unsigned char[4*imageWidth*imageHeight];
    12. for(int i = 0; i < imageWidth*imageHeight; i++) {
    13. (*currImageData)[i*4+3] = 0xFF;
    14. }
    15. }
    16.  
    17. int pixels =frame->width * frame->height;
    18.  
    19. uchar* src = (uchar*)(frame->imageData);
    20. uchar* srcEnd = src + (frame->nChannels*pixels);
    21. uchar* dest = *currImageData;
    22. if(frame->nChannels == 3) {
    23. do {
    24. memcpy(dest, src, 3);
    25. dest += 4;
    26. src += 3;
    27. } while(src < srcEnd);
    28. }
    29. else if(frame->nChannels == 1) {
    30. //qDebug() << "single channel";
    31. int index = 0;
    32. do {
    33. // RGB pixels are written with the same value
    34. memset(dest, *src, 3);
    35. // increment past alpha
    36. dest+=4;
    37. // increment source to next pixel
    38. src++;
    39. index++;
    40. } while(src < srcEnd);
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    The first part is just memory allocation for the data, then it copies it (depending on the number of channels) and when you want a QImage from that data you can use the QImage constructor that takes uchar* data and give it the correct dimensions.

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert RAW 8 bit pixels into a QImage

    I would follow JPN's suggestion, but using QImage::scanLine() like this:
    Qt Code:
    1. QImage image(width, height, QImage::Format_Indexed8);
    2. for (int y = 0;y<height; y++)
    3. memcpy(image.scanLine(y), rawData + y*width, width);
    To copy to clipboard, switch view to plain text mode 
    The problem with QImage::bits() is that Qt might add padding to each row to align the rows to 32 bits.

    Then you need to set the Color table like this:
    Qt Code:
    1. QVector<QRgb> colorTable(256);
    2. for(int i=0;i<256;i++)
    3. colorTable[i] = qRgb(i,i,i);
    4. image.setColorTable(colorTable);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 12:40

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.