Results 1 to 10 of 10

Thread: Creating a pixmap from unsigned char*

  1. #1
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating a pixmap from unsigned char*

    Helllo

    i want to dispaly an image in my app, which i manually constructed using a table of unsigned *char. The format is RGBRGBRGB...... and so on... (I don't need alpha)

    here is a sample code i used to write for a similar wxWidgets application:

    Qt Code:
    1. int size = m_Width * m_Height * 3;
    2. unsigned char *img;
    3. img = (unsigned char*) malloc (size);
    4. int j = 0;
    5.  
    6. for(int i=0; i<m_Width * m_Height; i++)
    7. {
    8. img[j] = m_Grey8Data[i];
    9. img[j+1] = m_Grey8Data[i];
    10. img[j+2] = m_Grey8Data[i];
    11. j += 3;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way i can create a pixmap out of this array? If not, what technique should i use to achieve the same thing?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating a pixmap from unsigned char*

    did you try QPixmap::loadFromData() ?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a pixmap from unsigned char*

    yes but this function requires the data to contain headers. Only supports formats like bmp, jpg.. etc

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating a pixmap from unsigned char*

    the functions does not require a header, it will resort to searching for one if the format is not given.
    I have used it many times with raw data with out a header.
    From what you have written in the first post, I would try "BMP" or "PPM" if the function did not guess correctly with out a specified format.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a pixmap from unsigned char*

    here is the code i ended up with:

    Qt Code:
    1. uint iWidth = 640;
    2. uint iHeight = 480;
    3. uint iSize = iWidth * iHeight;
    4. uchar *imgData = new uchar[iSize*4];
    5. uint j=0;
    6. for(uint i=0; i<iSize; i++)
    7. {
    8. imgData[j] = 255; // R
    9. imgData[j+1] = 128; // G
    10. imgData[j+2] = 128; // B
    11. imgData[j+3] = 255; // A
    12. j += 4;
    13. }
    14. QImage image(iWidth, iHeight, QImage::Format_ARGB32);
    15.  
    16. for (unsigned int y = 0; y < iHeight; y++)
    17. {
    18. for(unsigned int x = 0; x < iWidth; x++)
    19. {
    20. int s = y*iWidth + x*4;
    21. QColor color(imgData[s], imgData[s+1], imgData[s+2], imgData[s+3]);
    22. image.setPixel(x, y, color.rgba());
    23. }
    24. }
    25.  
    26. m_pm = new QPixmap();
    27. *m_pm = m_pm->fromImage(image);
    To copy to clipboard, switch view to plain text mode 

    It works as expected. I'm not sure though if everything is done correctly. Please comment on the above code

    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Creating a pixmap from unsigned char*

    Yes, for manipulating image data QImage is the way to go.
    However, this code generates a uniform image, for that you don't need all that code, unless this is only a test code.
    Tip: If you need to change an already existing image, you can use QImage::bits() to access the image data, so you don't need to allocate and delete and call setPixel() size times for each change.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a pixmap from unsigned char*

    I wish I knew a way to use your above code with QPixmap::loadFromData instead of the QImage version since it costs to much CPU time for me to convert back to the pixmap for display.

    For some reason the function always fails for me and returns a null QPixmap.

    I'm doing something like:

    Qt Code:
    1. char *the_image[ 64 * 64 ];
    2. for (int i = 0; i < (64 * 64); i++ ) {
    3. the_image[ i ] = qrand() % 256;
    4. }
    5. my_pixmap.loadFromData( the_image, 64 * 64 );
    To copy to clipboard, switch view to plain text mode 

    But loadFromData always returns 0 (fail).

    Any tips?

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

    Default Re: Creating a pixmap from unsigned char*

    I really would look at generating a PPM "file". It is very similar to your RGB bytes with a few simple headers specifying colour depth, height, width preceding it. Here's a basic example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class mainwindow: public QMainWindow {
    5. public:
    6. mainwindow(QWidget *p = 0): QMainWindow(p) {
    7. makeImage(256, 256, 255);
    8. QLabel *widget = new QLabel(this);
    9. widget->setPixmap(m_pixmap);
    10. setCentralWidget(widget);
    11. }
    12. private:
    13. void makeImage(int width, int height, int maxColour) {
    14. bool goodLoad;
    15.  
    16. // Header
    17. ba.append("P6 ");
    18. ba.append(QString::number(width).toAscii());
    19. ba.append(" ");
    20. ba.append(QString::number(height).toAscii());
    21. ba.append(" ");
    22. ba.append(QString::number(maxColour).toAscii());
    23. ba.append("\n");
    24.  
    25. // Raw binary data
    26. for (int r = 0; r < height; r++) {
    27. for (int c = 0; c < width; c++) {
    28. ba.append( char(r % maxColour) ); // red byte (use 16 bits if maxColour > 255)
    29. ba.append( char(r % maxColour) ); // green byte
    30. ba.append( char(r % maxColour) ); // blue byte
    31. }
    32. }
    33. goodLoad = m_pixmap.loadFromData(ba, "ppm");
    34. qDebug() << goodLoad;
    35. }
    36.  
    37. QPixmap m_pixmap;
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43. mainwindow m;
    44. m.show();
    45. return app.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

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

    mhoover (27th July 2010)

  10. #9
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating a pixmap from unsigned char*

    Thanks ChrisW67!

    This code worked great. I found that when I wanted to actually display my video data, it helped to change the file format a little bit to a PGM file (it's grayscale and doesn't expect an R a G and a B for every pixel). This involved basically just setting the P6 in the header to P5 and then appending my char* data.

    I had expected that replacing my QImage->QPixmap code to direct QPixmap code would reduce my %cpu substantially, but it is very similar. It actually uses 4%cpu more. This is after an optimization I did where I made the QByteArray a member variable and used QByteArray::replace() instead of QByteArrayAppend() (that optimization shaved 26%cpu off my application).

    Thanks for your help.

  11. #10
    Join Date
    Dec 2016
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Creating a pixmap from unsigned char*

    Sorry for posting in such an old theme - but you have an error in line 20.
    It should be
    Qt Code:
    1. int s = y*iWidth*4 + x*4;
    To copy to clipboard, switch view to plain text mode 
    or it won't work properly.

Similar Threads

  1. unsigned char * to QString
    By elina.du in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2009, 08:33
  2. convert unsigned char * to QString
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 20:31
  3. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  4. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.