Page 1 of 1

Standalone copy of wxImage

Posted: Wed Sep 28, 2005 2:13 pm
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 );

Posted: Thu Sep 29, 2005 5:49 am
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);

Posted: Wed Oct 05, 2005 11:44 pm
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.

Posted: Sat Oct 15, 2005 11:15 am
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.