Results 1 to 10 of 10

Thread: Question about QImage

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default Question about QImage

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question about QImage

    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.

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Question about QImage

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question about QImage

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question about QImage

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question about QImage

    @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?
    ==========================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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question about QImage

    Quote Originally Posted by high_flyer View Post
    @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:
    Qt Code:
    1. QImage img(... data ...);
    2. QImage shallow = img;
    To copy to clipboard, switch view to plain text mode 
    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:
    1. char *data = ...;
    2. QImg cpy;
    3. {
    4. QImage img(... data ...);
    5. cpy = img.scaled(200,200);
    6. }
    7. delete [] data;
    8. doSomething(cpy);
    To copy to clipboard, switch view to plain text mode 

    This is probably safe too:
    Qt Code:
    1. char *data = ...;
    2. QImage img(... data ...);
    3. QImage cpy = img.scaled(200,200);
    4. delete [] data; // don't refer to img after this line
    5. doSomething(cpy);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question about QImage

    // 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.
    ==========================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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question about QImage

    Quote Originally Posted by high_flyer View Post
    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.

    Qt Code:
    1. QImage orig(... data ...);
    2. QImage s1 = orig.scaled(200,200);
    3. QImage s2 = orig.scaled(300,300);
    4. orig.setPixel(...);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question about QImage

    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.
    ==========================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.

Similar Threads

  1. Question about QImage
    By paolom in forum Qt Programming
    Replies: 9
    Last Post: 9th September 2010, 18:47
  2. QImage
    By andreahmed in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2010, 07:11
  3. QImage
    By Maluko_Da_Tola in forum Newbie
    Replies: 1
    Last Post: 31st July 2010, 06:28
  4. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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
  •  
Qt is a trademark of The Qt Company.