Standalone copy of wxImage

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Standalone copy of wxImage

Post by Ravilj »

The wxImage copy constructor doesn't seem to create a new copy of the image object. I used this code to do so:

Code: Select all

        wxImage * m_pOldImg = new wxImage( "image.bmp", wxBITMAP_TYPE_BMP, -1 );

        int dataSize = m_pOldImg->GetWidth( )*m_pOldImg->GetHeight( )*3;
        unsigned char * NewImgData = ( unsigned char * ) malloc( dataSize );
        unsigned char * OldImgData = m_pOldImg->GetData( );

        memcpy( NewImgData, OldImgData, dataSize );

        wxImage * m_pNewImg = new wxImage( m_pOldImg->GetWidth( ), m_pOldImg->GetHeight( ), NewImgData );
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

From wxImage Documentation
Copy constructor.

wxImage(const wxImage& image)

Code: Select all

 wxImage * m_pOldImg = new wxImage( "image.bmp", wxBITMAP_TYPE_BMP, -1 );
wxImage * m_pNewImg = new wxImage(*m_pOldImg);
KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL »

I don't know why you make it so difficult??

Code: Select all

    // creates an identical copy of the image (the = operator
    // just raises the ref count)
    wxImage Copy() const;
Where the code would become:

Code: Select all

wxImage * m_pOldImg = new wxImage( "image.bmp", wxBITMAP_TYPE_BMP, -1 );
wxImage * m_pNewImg = &(m_pOldImg->Copy());
BTW, the copy constructor only References the image object. It does not make a new copy...

Also it's rather strange to create a new copy of the image object as every action which modifies the image will do that in place (non-const members) or return a new image (const-members). You should always check upon include/wx/image.h to see which one applies in your situation.
Last edited by KaReL on Wed Oct 19, 2005 6:03 am, edited 1 time in total.
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj »

phlox81 wrote:
From wxImage Documentation
Copy constructor.

wxImage(const wxImage& image)

Code: Select all

 wxImage * m_pOldImg = new wxImage( "image.bmp", wxBITMAP_TYPE_BMP, -1 );
wxImage * m_pNewImg = new wxImage(*m_pOldImg);
Yeah thats all very well but if you change the image data of old image it also affects the new image. Since the copy constructor does not create a new instance in memory of the image data.

KaRel, didnt see the copy function :P Oops yes that makes life easier hehe.
Post Reply