PDA

View Full Version : Qt crash at drawImage



Chaitra Sridhar
29th April 2016, 04:56
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 -


QPainter painter;
QImage rcptImage(216, 560, QImage::Format_Mono);

painter.begin(&rcptImage);

QImage img("./test1.bmp");
QImage::Format f = img.format();
printf("%d\n", f);
uchar* tempData = img.bits();

QImage img1(img.bits(), 320, 240, QImage::Format_Mono);
QImage::Format f1 = img1.format();

painter.drawImage(QPoint(10,10), img1);

painter.end();

rcptImage.save("/home/chaitras/output1.bmp");

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.

d_stranz
29th April 2016, 05:10
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?

Chaitra Sridhar
29th April 2016, 05:27
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.

d_stranz
29th April 2016, 05:29
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??

Chaitra Sridhar
29th April 2016, 06:30
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..

ChrisW67
30th April 2016, 00:10
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?


QImage img1(img);

Should achieve the goal.

Chaitra Sridhar
1st May 2016, 23:53
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"?