PDA

View Full Version : Question about QImage



paolom
9th November 2010, 13:47
Hi,

Is it possible to know how is handled the internal pixel data of a QImage ??

Ex. the pixel data must be deleted manually when the QImage is delete or the QImage delete itself the pixel data.

Thanks

high_flyer
9th November 2010, 14:02
It depends how QImage got the data.
If you are giving QImage a file name, than QImage is responsible for the data.
If you are using one of the constructors that use external data, than QImage does not delete the content.

In general, data that QImage allocated, it will delete, data it didn't allocate, it wont delete.
You can find more info here: QImage

paolom
9th November 2010, 14:07
My problem is that I create my QImage with external pixel data, but it's possible that I make many transformation with this image, ( scaled, mirrored ) , and any time I want to know if I need to delete the pixel data.

high_flyer
9th November 2010, 15:10
As I said, since you are the data creator, you have to delete it, QImage will not delete it when it gets destroyed.

wysota
9th November 2010, 15:30
Just be aware that most methods in the API of QImage do not modify the original data but instead return a copy of the image and modify that. The data the copy operates on is managed by QImage and has no relation to the original data.

high_flyer
9th November 2010, 15:58
@wysota:
You are right, which actually makes me wander why:

The buffer must remain valid throughout the life of the QImage.

Since QImage works on a copy and not on the original data, if the original data gets destroyed, why should it bother QImage?

wysota
9th November 2010, 21:16
@wysota:
You are right, which actually makes me wander why:

Since QImage works on a copy and not on the original data, if the original data gets destroyed, why should it bother QImage?

It works on a copy if you do a deep copy somewhere. This is shallow copy:

QImage img(... data ...);
QImage shallow = img;
But scaled() and family really create a new image and render into it, there is no actual "copy" of the original made. So this is perfectly valid:

char *data = ...;
QImg cpy;
{
QImage img(... data ...);
cpy = img.scaled(200,200);
}
delete [] data;
doSomething(cpy);

This is probably safe too:

char *data = ...;
QImage img(... data ...);
QImage cpy = img.scaled(200,200);
delete [] data; // don't refer to img after this line
doSomething(cpy);

high_flyer
10th November 2010, 09:03
// don't refer to img after this line
This was my question - if scaled() created a deep copy (internally in 'img') then why 'data' must be kept alive from this point on?
As far as I can see in the docs, QImage is using the external buffer as slong as you don't do to it anything (for example when just drawing it).
But any operation on the buffer, will make QImage create a deep copy internally, and work on that.
From that point on, why is it important to keep the original buffer alive?

Actually I'll try it once I have a bit time, and follow it in the debugger.

EDIT:
I think I know why.
Its so that the 'const' methods will still work.

wysota
10th November 2010, 10:12
This was my question - if scaled() created a deep copy (internally in 'img') then why 'data' must be kept alive from this point on?
Because you can still refer to the original object.


QImage orig(... data ...);
QImage s1 = orig.scaled(200,200);
QImage s2 = orig.scaled(300,300);
orig.setPixel(...);

high_flyer
10th November 2010, 11:16
Because you can still refer to the original object.
Yes, but the original object is not "using" the external buffer, except for initializing its internal copy of the buffer, and that copy is a deep copy.
If QImage would not have the const methods, that return the original external buffer, then you would not have to worry about the external buffer stying valid.