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
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
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
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
As I said, since you are the data creator, you have to delete it, QImage will not delete it when it gets destroyed.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
@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?The buffer must remain valid throughout the life of the QImage.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
It works on a copy if you do a deep copy somewhere. This is shallow copy:
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:
Qt Code:
char *data = ...; QImg cpy; { cpy = img.scaled(200,200); } delete [] data; doSomething(cpy);To copy to clipboard, switch view to plain text mode
This is probably safe too:
Qt Code:
char *data = ...; delete [] data; // don't refer to img after this line doSomething(cpy);To copy to clipboard, switch view to plain text mode
This was my question - if scaled() created a deep copy (internally in 'img') then why 'data' must be kept alive from this point on?// don't refer to img after this line
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Because you can still refer to the original object.
Qt Code:
orig.setPixel(...);To copy to clipboard, switch view to plain text mode
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.Because you can still refer to the original object.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Bookmarks