Results 1 to 7 of 7

Thread: Creating a QImage from uchar* data

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Creating a QImage from uchar* data

    I have a pointer to the start of a Format_Indexed8 bmp image and need to store it into a QImage. I have not been able to successfully store the image. I was hoping there would be a way to pass the start of the data and the Format_Indexed8, so that the function would locate where the Color Table is. Is this possible or do I need to pass the exact location of where the image pixel data is stored and where the color table is located?

    Any help would be appreciated.

    I was hoping there would be something like this:
    unsigned int size = /* Size of header + Size of Color Table + Size of Pixel Data */
    QImage tmp(dataPointer->GetNumCols(), dataPointer->GetNumRows(), QImage::Format_Indexed8);
    tmp.loadFromData((const uchar*)dataPointer->GetImageData(), size);

    Any help is appreciated...

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a QImage from uchar* data

    Hi,

    Are you loading an image from disk? If you are loading an image from a file you can load the image with the operator
    "bool load ( const QString & fileName, const char * format = 0 )".

    If you have the image into memory and want to put it into a QImage you have to create a QImage, create the palette for this image and finally load the data using the memory pointer with the operator
    "bool loadFromData ( const uchar * data, int len, const char * format = 0 )".

    I think that you can't let it know where the palette is. If the image is a Black and White image you have to create a palette with "Red = Green = Blue" and 256 colors.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QImage from uchar* data

    I am loading the image from memory...and what is 'data' supposed to point to? Should it point to the beginning of the files data or to where the actual pixel data is? also what is 'len'? and format supposed to be sent to?

    "bool loadFromData ( const uchar * data, int len, const char * format = 0 )".

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a QImage from uchar* data

    Hi,
    "*data" is a pointer to the image in memory. It has to be the pointer to the first pixel.
    "len" is the lenght of the image in bytes, so it will be width*height if it is a Black/White image or widht*height*3 if it is a RGB image.

    Have you read the documentation? It is explained there.

    The image is in memory, so you will have an image structure or an image class? Or you have the data of the image into an array? Could you post wich image structure are you using please.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QImage from uchar* data

    Below is the structures which defines the image. I also have an image class which returns the pixel data of the image. The issue is that I need to somehow set the ColorMap, which "bool loadFromData ( const uchar * data, int len, const char * format = 0 )" will not do if I'm only passing the pixel data. How do I create the QVector of QRgb values to pass to SetColorMap and when am I supposed to call that function? Am I supposed to call SetColorMap after setting the width and height of the image, but before setting the pixel data...or does the order not matter?

    Another note: Because this bmp uses a color map it uses one byte per pixel.

    Qt Code:
    1. // BMP File header
    2. typedef struct
    3. {
    4. unsigned short bfType;
    5. unsigned int bfSize;
    6. unsigned short bfReserved1;
    7. unsigned short bfReserved2;
    8. unsigned int bfOffBits;
    9. }
    10.  
    11. // BMP file info
    12. typedef struct
    13. {
    14. unsigned int biSize;
    15. int biWidth;
    16. int biHeight;
    17. unsigned short biPlanes;
    18. unsigned short biBitCount;
    19. unsigned int biCompression;
    20. unsigned int biSizeImage;
    21. int biXPelsPerMeter;
    22. int biYPelsPerMeter;
    23. unsigned int biClrUsed;
    24. unsigned int biClrImportant;
    25. } BITMAPINFOHEADER;
    26.  
    27. // Colormap entry
    28. typedef struct
    29. {
    30. unsigned char rgbBlue;
    31. unsigned char rgbGreen;
    32. unsigned char rgbRed;
    33. unsigned char rgbReserved;
    34. } RGBQUAD;
    35.  
    36. // Bitmap Info
    37. typedef struct
    38. {
    39. BITMAPINFOHEADER bmiHeader;
    40. RGBQUAD bmiColors[256];
    41. } BITMAPINFO;
    To copy to clipboard, switch view to plain text mode 
    Last edited by forrestfsu; 29th January 2007 at 19:12.

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a QImage from uchar* data

    Hi
    OK, so "bmiColors" is an array that define the palette. You can do something like this:

    [/CODE]
    image.setNumColors(256);
    for (int i=0; i<255; i++)
    {
    image.setColor(i, qRgb(bmiColors.rgbRed,bmiColors.rgbGreen,bmiColors .rgbBlue));
    }
    QVector<QRgb> m_qPalette; //If you want to save the palette and use it another time
    image.loadFromData((const uchar*)dataPointer->GetImageData());
    [/CODE]

    It will take the color "i" from the "bmiColors" array and put them into the color table of the image, then load the image into the QImage.

    You can set the color table before or after of setting the pixel data. It will be used when the image will be used.
    Òscar Llarch i Galán

  7. #7
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QImage from uchar* data

    Thanks, I got something similar to what you explained working.

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 19:58
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.