Results 1 to 6 of 6

Thread: Trying to make a Qimage from a char *

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Trying to make a Qimage from a char *

    Hello,
    I have Qt 5.1 and I am trying to display pictures I get from a scanner into a window.
    My 8 bit grey scale image comes as an array called m_raw_data of type unsigned char *, and I know that its dimensions are 320x480.

    I want to create a QImage structure to wrap it up so I can paint my widget.
    I have the problem that the following code does not compile
    Qt Code:
    1. QVector<QRgb> colorTable;
    2. for(int i = 0; i < 256; i++){
    3. colorTable.append(qRgb(i,i,i));
    4. }
    5. memset(m_raw_data, 0, DEFAULT_IMAGE_WIDTH*DEFAULT_IMAGE_HEIGHT);
    6. m_image = new QImage(m_raw_data, DEFAULT_IMAGE_WIDTH,DEFAULT_IMAGE_HEIGHT,QImage::Format_Indexed8);
    7. m_image->setColorTable(colorTable);
    To copy to clipboard, switch view to plain text mode 
    The compiler says that the constructor QImage is ambiguous, and I am willing to supply the extra arguments, but I dont really dont know what they mean, as the documentation just says
    typedef QImageCleanupFunction
    A function with the following signature that can be used to implement basic image memory management:

    void myImageCleanupHandler(void *info);
    This typedef was introduced in QtGui 5.0.

    This description is so short as to leave me speechless. There is no example either.
    Thankyou for your attention
    Henri

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trying to make a Qimage from a char *

    I don't think passing a cleanup function will help to disambiguate, all those "raw data" constructors have it as the second to last argument.

    You could try casting your array to one of the the two types used for the constructors' first argument.

    Btw, your code doesn't make sense, you set your image data to 0 before creating the image.

    Oh, and you most likely don't need to create QImage on the heap.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to make a Qimage from a char *

    Thankyou for your reply.
    I used memset because I was planning on changing the contents of the raw data and hoping that I could do this after the call to the QImage constructor.
    Apparently this is not possible.

    Still answer to what the cleanup function is supposed to do. It did disambiguate my function call by the way. I just passed one that did nothing.

  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: Trying to make a Qimage from a char *

    Your code compiles just fine here (it may not do what you expect but that's another problem). What is your actual error message and/or your actual code?
    Qt Code:
    1. #include <QApplication>
    2. #include <QImage>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7. const int DEFAULT_IMAGE_WIDTH = 320;
    8. const int DEFAULT_IMAGE_HEIGHT = 480;
    9. unsigned char m_raw_data[DEFAULT_IMAGE_WIDTH * DEFAULT_IMAGE_HEIGHT];
    10. QImage *m_image;
    11.  
    12. // YOUR CODE
    13. QVector<QRgb> colorTable;
    14. for(int i = 0; i < 256; i++){
    15. colorTable.append(qRgb(i,i,i));
    16. }
    17. memset(m_raw_data, 0, DEFAULT_IMAGE_WIDTH*DEFAULT_IMAGE_HEIGHT);
    18. m_image = new QImage(m_raw_data, DEFAULT_IMAGE_WIDTH,DEFAULT_IMAGE_HEIGHT,QImage::Format_Indexed8);
    19. m_image->setColorTable(colorTable);
    20. // END YOUR CODE
    21.  
    22. delete m_image;
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. $ ~/Qt5.1.1/5.1.1/gcc_64/bin/qmake
    2. $ make
    3. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/home/chrisw/Qt5.1.1/5.1.1/gcc_64/mkspecs/linux-g++ -I. -I. -I/home/chrisw/Qt5.1.1/5.1.1/gcc_64/include -I/home/chrisw/Qt5.1.1/5.1.1/gcc_64/include/QtWidgets -I/home/chrisw/Qt5.1.1/5.1.1/gcc_64/include/QtGui -I/home/chrisw/Qt5.1.1/5.1.1/gcc_64/include/QtCore -I. -o main.o main.cpp
    4. g++ -Wl,-O1 -Wl,-rpath,/home/chrisw/Qt5.1.1/5.1.1/gcc_64 -Wl,-rpath,/home/chrisw/Qt5.1.1/5.1.1/gcc_64/lib -o tt main.o -L/home/chrisw/Qt5.1.1/5.1.1/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
    5. $
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trying to make a Qimage from a char *

    Quote Originally Posted by feraudyh View Post
    I used memset because I was planning on changing the contents of the raw data and hoping that I could do this after the call to the QImage constructor.
    Apparently this is not possible.
    Ah, no, that is indeed possible. Passing memory to the QImage constructor makes it use that memory, not a copy.

    Alternatively you could let QImage create the memory and then access it via QImage::bits() or QImage::scanline().

    Cheers,
    _

  6. #6
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trying to make a Qimage from a char *

    I'm not sure about your problem, but I also wrote a RAW-Imageviewer. I pass the Data from a QFile to a Qbytearray to a QImage and then display it as a Pixmap on a Label. It works, but there was a problem, the image was not displayed correctly because I used the Format_Indexed8. Guess you also have to use rgb32 even if your images are 8bit grayscale. If you do so, you also will not need your colortable anymore. Here is a example:


    void MainWindow:n_pushButton_clicked()
    {
    QFile file(filename[0]);
    if (file.open(QIODevice::ReadOnly))
    {
    QByteArray ba=file.readAll();
    QImage image(512,256,QImage::Format_RGB32);
    int j=0, color;
    for (int y=0; y<image.height(); y++)
    {
    for (int x=0; x<image.width(); x++)
    {
    color = ba.at(j);
    image.setPixel(x,y,color+(color<<8)+(color<<16)); // here is the hex shift for rgb32 0xffRRGGBB
    j++;
    }
    }
    QPixmap pix2(QPixmap::fromImage(image));
    ui->lb1->setPixmap(pix2);
    file.close();
    }
    else{...}
    }

    Hope this is helpful

    Best regards,

    Olli

Similar Threads

  1. Creating QImage from char*
    By emaborsa in forum Newbie
    Replies: 5
    Last Post: 8th May 2011, 20:47
  2. How to make a mask with QImage?
    By MeteorAndSusan in forum Qt Programming
    Replies: 6
    Last Post: 10th August 2010, 11:06
  3. const char* QImage Format?
    By grabalon in forum Newbie
    Replies: 2
    Last Post: 13th May 2010, 00:44
  4. QImage and 8 bit unsigner char **
    By fyanardi in forum Newbie
    Replies: 4
    Last Post: 6th June 2006, 14:13
  5. QImage from 8 bit char* RGB buffer
    By tboloo in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2006, 19:56

Tags for this Thread

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.