From 5644ac46408ac3e68894b23a7b89e041ddcda89d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 Apr 2005 21:16:04 +0000 Subject: [PATCH] added XYToIndex() to avoid duplicating the checks for valid coordinates in many different places git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33281 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/image.h | 6 +++ src/common/image.cpp | 103 +++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index a07b2d47d4..c4737d7273 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -354,6 +354,12 @@ public: protected: static wxList sm_handlers; + // return the index of the point with the given coordinates or -1 if the + // image is invalid of the coordinates are out of range + // + // note that index must be multiplied by 3 when using it with RGB array + long XYToIndex(int x, int y) const; + private: friend class WXDLLEXPORT wxImageHandler; diff --git a/src/common/image.cpp b/src/common/image.cpp index bbd82b9eec..c8d8a28028 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -782,16 +782,24 @@ wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char return image; } -void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) +long wxImage::XYToIndex(int x, int y) const { - wxCHECK_RET( Ok(), wxT("invalid image") ); + if ( Ok() && + x >= 0 && y >= 0 && + x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) + { + return y*M_IMGDATA->m_width + x; + } - int w = M_IMGDATA->m_width; - int h = M_IMGDATA->m_height; + return -1; +} - wxCHECK_RET( (x>=0) && (y>=0) && (xm_data[ pos ] = r; M_IMGDATA->m_data[ pos+1 ] = g; @@ -836,42 +844,30 @@ void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, uns unsigned char wxImage::GetRed( int x, int y ) const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + long pos = XYToIndex(x, y); + wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); - int w = M_IMGDATA->m_width; - int h = M_IMGDATA->m_height; - - wxCHECK_MSG( (x>=0) && (y>=0) && (xm_data[pos]; } unsigned char wxImage::GetGreen( int x, int y ) const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); - - int w = M_IMGDATA->m_width; - int h = M_IMGDATA->m_height; + long pos = XYToIndex(x, y); + wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); - wxCHECK_MSG( (x>=0) && (y>=0) && (xm_data[pos+1]; } unsigned char wxImage::GetBlue( int x, int y ) const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); - - int w = M_IMGDATA->m_width; - int h = M_IMGDATA->m_height; + long pos = XYToIndex(x, y); + wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); - wxCHECK_MSG( (x>=0) && (y>=0) && (xm_data[pos+2]; } @@ -947,51 +943,45 @@ void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool void wxImage::SetAlpha(int x, int y, unsigned char alpha) { - wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") ); - - int w = M_IMGDATA->m_width, - h = M_IMGDATA->m_height; + wxCHECK_RET( HasAlpha(), wxT("no alpha channel") ); - wxCHECK_RET( x >=0 && y >= 0 && x < w && y < h, wxT("invalid image index") ); + long pos = XYToIndex(x, y); + wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); - M_IMGDATA->m_alpha[y*w + x] = alpha; + M_IMGDATA->m_alpha[pos] = alpha; } unsigned char wxImage::GetAlpha(int x, int y) const { - wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") ); + wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") ); - int w = M_IMGDATA->m_width, - h = M_IMGDATA->m_height; + long pos = XYToIndex(x, y); + wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); - wxCHECK_MSG( x >=0 && y >= 0 && x < w && y < h, 0, wxT("invalid image index") ); - - return M_IMGDATA->m_alpha[y*w + x]; + return M_IMGDATA->m_alpha[pos]; } -bool wxImage::ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b ) +bool +wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) { - SetAlpha( NULL ); + SetAlpha(NULL); - int w = M_IMGDATA->m_width, - h = M_IMGDATA->m_height; + const int w = M_IMGDATA->m_width; + const int h = M_IMGDATA->m_height; unsigned char *alpha = GetAlpha(); unsigned char *data = GetData(); - int x,y; - for (y = 0; y < h; y++) - for (x = 0; x < w; x++) - { - *alpha = *data; - alpha++; - *data = r; - data++; - *data = g; - data++; - *data = b; - data++; - } + for ( int y = 0; y < h; y++ ) + { + for ( int x = 0; x < w; x++ ) + { + *alpha++ = *data; + *data++ = r; + *data++ = g; + *data++ = b; + } + } return true; } @@ -1008,7 +998,6 @@ void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) free(M_IMGDATA->m_alpha); M_IMGDATA->m_alpha = alpha; M_IMGDATA->m_static = static_data; - } unsigned char *wxImage::GetAlpha() const -- 2.45.2