Results 1 to 7 of 7

Thread: Qt crash at drawImage

  1. #1
    Join Date
    Apr 2016
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Qt crash at drawImage

    Hi,
    I am relatively new to Qt programming. I have hit a dead end and seek help.
    I am running Qt in Linux.
    Following is my code -
    Qt Code:
    1. QPainter painter;
    2. QImage rcptImage(216, 560, QImage::Format_Mono);
    3.  
    4. painter.begin(&rcptImage);
    5.  
    6. QImage img("./test1.bmp");
    7. QImage::Format f = img.format();
    8. printf("%d\n", f);
    9. uchar* tempData = img.bits();
    10.  
    11. QImage img1(img.bits(), 320, 240, QImage::Format_Mono);
    12. QImage::Format f1 = img1.format();
    13.  
    14. painter.drawImage(QPoint(10,10), img1);
    15.  
    16. painter.end();
    17.  
    18. rcptImage.save("/home/chaitras/output1.bmp");
    To copy to clipboard, switch view to plain text mode 
    Program is crashing at "painter.drawImage(QPoint(10,10), img1);"

    test1.bmp is a 1bpp image - 360 x 260 x 1. Attached the image.

    I am trying to reading the bits from "img" and create a new image img1 using these bits.
    And using img1, which results in a crash.

    Stacktrace is pointing to a function "convertIndexedToARGB32PM Line 95"


    convertIndexedToARGB32PM Line 95
    blend_untransformed_generic Line 4264
    fillRect_normalized Line 1491
    QRasterPaintEngine::drawImage Line 2187
    QPainter::drawImage Line 5356
    QPainter::drawImage qpainter.h Line 852
    main


    Any help on this would be appreciated.
    Attached Images Attached Images
    Last edited by anda_skoa; 29th April 2016 at 09:39. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt crash at drawImage

    So none of the sizes of your images match. One is 216 x 560, one is 320 x 240, another is 360 x 260. What do you think will happen if you try to load one image of 360 x 260 into another image of 320 x 240, and then try to paint that into an image of 216 x 560? Do you expect QImage and QPainter to try to understand what you really want to do and just do it for you despite the fact that nothing matches?

  3. #3
    Join Date
    Apr 2016
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qt crash at drawImage

    The "rcptImage" is a sort of canvas I am using to use to either drawImage or drawText.

    Sorry, that was a typo.
    My image is 360 x 240, I mean the one I uploaded.

    Anyways even after changing all sizes to 320x240, the crash is still happening...


    Added after 8 minutes:


    Also, the surprising thing is that, if use "img" with "drawImage", it works absolutely fine.
    When I use "img1", it is crashing.
    Last edited by Chaitra Sridhar; 29th April 2016 at 04:27.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt crash at drawImage

    If your input image is 360 x 240 and the image you are trying to paint into is smaller (320 x 240), then it is still probably going to blow up.

    Do you know that the image you say you loaded has actually been loaded (QImage::isNull()) or that the pointer returned by QImage::bits() is not NULL or that the image you create from the bits is not null??

  5. #5
    Join Date
    Apr 2016
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qt crash at drawImage

    I changed the canvas size to 500x500(rcptImage),
    img.isNull() is false. I printed out img.Bits(), it has data and it is not Null

    img1.isNull() is false. and img1.Bits() is not null.

    I also looped through the bits and subtracted the pixel values of img and img1. The differences for all pixels are all zeros.

    Also, img and img1 are both 320 x 240.. a total of 9600 bytes of data.

    Another observation is that when I use loadFromData function with img1, i.e., tried the following
    const uchar* temp = img.constBits();
    QImage img1;
    bool result = img1.loadFromData(temp, 9600, "BMP");

    The result is false.


    Added after 25 minutes:


    Hi d_stranz,

    I just tried couple of things, and when I changed the image format as follows, the program does not crash

    QImage img1(tempData, 320, 240, QImage::Format_ARGB32);

    When I use
    QImage img1(tempData, 320, 240, QImage::Format_Mono);

    there is a crash.

    Why is this happening. As per my understanding, "QImage img("/home/chaitras/work/Printer/NipponPrinter/Printer_Test/test1.bmp");" is a QImage::Format_Mono. I confirmed this by printing "QImage::Format f = img.format();"
    But when I read the get the img.bits() and create QImage img1(img.bits(), 320, 240, QImage::Format_Mono); it does not work..

    Why is it like that? Do u have any idea on this??
    Let me know your thoughts..
    Last edited by Chaitra Sridhar; 29th April 2016 at 05:30.

  6. #6
    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: Qt crash at drawImage

    You have a mono input image and you appear to want an exact copy of that image. Why are you playing with the internal data buffer at all?
    Qt Code:
    1. QImage img1(img);
    To copy to clipboard, switch view to plain text mode 
    Should achieve the goal.

  7. #7
    Join Date
    Apr 2016
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Qt crash at drawImage

    Thanks for your reply.

    In my project, I am expecting the image as a byte stream. And I need to use this byte stream in to QImage and then print this image out.
    So, I happened to write a small test program that intends to do this.
    But, it is crashing at the mentioned point.

    Could you please give an example as to how to load a byte stream into QImage and do a "drawImage"?

Similar Threads

  1. QImage and drawImage method
    By ally in forum Newbie
    Replies: 3
    Last Post: 6th July 2015, 10:56
  2. QPainter::drawImage randomly(?) omits some images
    By Al_ in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2014, 08:14
  3. Replies: 5
    Last Post: 13th November 2013, 01:54
  4. Factoring drawImage
    By Alundra in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2013, 11:39
  5. QPainter.drawImage() not working with resource image file
    By thiagoalencar22 in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2010, 21:07

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.