]> git.saurik.com Git - wxWidgets.git/commitdiff
added XYToIndex() to avoid duplicating the checks for valid coordinates in many diffe...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 2 Apr 2005 21:16:04 +0000 (21:16 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 2 Apr 2005 21:16:04 +0000 (21:16 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33281 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/image.h
src/common/image.cpp

index a07b2d47d43a03c712db535ede6a75ec36eac88a..c4737d727342b82a4b1fc3c4d82180389513d7c6 100644 (file)
@@ -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;
 
index bbd82b9eec0c2746f10a9bccc710c448328664dd..c8d8a280288ed22a39cb8a035c937ce6f316ea16 100644 (file)
@@ -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) && (x<w) && (y<h), wxT("invalid image index") );
+void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
+{
+    long pos = XYToIndex(x, y);
+    wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
 
-    long pos = (y * w + x) * 3;
+    pos *= 3;
 
     M_IMGDATA->m_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) && (x<w) && (y<h), 0, wxT("invalid image index") );
-
-    long pos = (y * w + x) * 3;
+    pos *= 3;
 
     return M_IMGDATA->m_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) && (x<w) && (y<h), 0, wxT("invalid image index") );
-
-    long pos = (y * w + x) * 3;
+    pos *= 3;
 
     return M_IMGDATA->m_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) && (x<w) && (y<h), 0, wxT("invalid image index") );
-
-    long pos = (y * w + x) * 3;
+    pos *= 3;
 
     return M_IMGDATA->m_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