]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/image.cpp
[1231183] 'cleanup: mismatched indentation' and other cleanings.
[wxWidgets.git] / src / common / image.cpp
index 7767fedb90baa41abb06bf930ff7768a3d0c8204..cf35689226e15f8141496be70a30af5869036b18 100644 (file)
@@ -498,25 +498,53 @@ wxImage wxImage::Rotate90( bool clockwise ) const
 
     wxCHECK_MSG( data, image, wxT("unable to create image") );
 
+    unsigned char *source_data = M_IMGDATA->m_data;
+    unsigned char *target_data;
+    unsigned char *alpha_data = 0 ;
+    unsigned char *source_alpha = 0 ;
+    unsigned char *target_alpha = 0 ;
+
     if (M_IMGDATA->m_hasMask)
+    {
         image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
+    }
+    else
+    {
+        source_alpha = M_IMGDATA->m_alpha ;
+        if ( source_alpha )
+        {
+            image.SetAlpha() ;
+            alpha_data = image.GetAlpha() ;
+        }
+    }
 
     long height = M_IMGDATA->m_height;
     long width  = M_IMGDATA->m_width;
 
-    unsigned char *source_data = M_IMGDATA->m_data;
-    unsigned char *target_data;
-
     for (long j = 0; j < height; j++)
     {
         for (long i = 0; i < width; i++)
         {
             if (clockwise)
+            {
                 target_data = data + (((i+1)*height) - j - 1)*3;
+                if(source_alpha)
+                    target_alpha = alpha_data + (((i+1)*height) - j - 1);
+            }
             else
+            {
                 target_data = data + ((height*(width-1)) + j - (i*height))*3;
+                if(source_alpha)
+                    target_alpha = alpha_data + ((height*(width-1)) + j - (i*height));
+            }
             memcpy( target_data, source_data, 3 );
             source_data += 3;
+
+            if(source_alpha)
+            {
+                memcpy( target_alpha, source_alpha, 1 );
+                source_alpha += 1;
+            }
         }
     }
 
@@ -787,6 +815,20 @@ wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char
     return image;
 }
 
+int wxImage::GetWidth() const
+{
+    wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
+
+    return M_IMGDATA->m_width;
+}
+
+int wxImage::GetHeight() const
+{
+    wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
+
+    return M_IMGDATA->m_height;
+}
+
 long wxImage::XYToIndex(int x, int y) const
 {
     if ( Ok() &&
@@ -1022,8 +1064,6 @@ void wxImage::InitAlpha()
     unsigned char *alpha = M_IMGDATA->m_alpha;
     const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
 
-    static const unsigned char ALPHA_TRANSPARENT = 0;
-    static const unsigned char ALPHA_OPAQUE = 0xff;
     if ( HasMask() )
     {
         // use the mask to initialize the alpha channel.
@@ -1037,8 +1077,8 @@ void wxImage::InitAlpha()
               src += 3, alpha++ )
         {
             *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
-                            ? ALPHA_TRANSPARENT
-                            : ALPHA_OPAQUE;
+                            ? wxIMAGE_ALPHA_TRANSPARENT
+                            : wxIMAGE_ALPHA_OPAQUE;
         }
 
         M_IMGDATA->m_hasMask = false;
@@ -1046,7 +1086,7 @@ void wxImage::InitAlpha()
     else // no mask
     {
         // make the image fully opaque
-        memset(alpha, ALPHA_OPAQUE, lenAlpha);
+        memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha);
     }
 }
 
@@ -1117,18 +1157,35 @@ bool wxImage::HasMask() const
     return M_IMGDATA->m_hasMask;
 }
 
-int wxImage::GetWidth() const
+bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const
 {
-    wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
+    long pos = XYToIndex(x, y);
+    wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") );
 
-    return M_IMGDATA->m_width;
-}
+    // check mask
+    if ( M_IMGDATA->m_hasMask )
+    {
+        const unsigned char *p = M_IMGDATA->m_data + 3*pos;
+        if ( p[0] == M_IMGDATA->m_maskRed &&
+                p[1] == M_IMGDATA->m_maskGreen &&
+                    p[2] == M_IMGDATA->m_maskBlue )
+        {
+            return true;
+        }
+    }
 
-int wxImage::GetHeight() const
-{
-    wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
+    // then check alpha
+    if ( M_IMGDATA->m_alpha )
+    {
+        if ( M_IMGDATA->m_alpha[pos] < threshold )
+        {
+            // transparent enough
+            return true;
+        }
+    }
 
-    return M_IMGDATA->m_height;
+    // not transparent
+    return false;
 }
 
 bool wxImage::SetMaskFromImage(const wxImage& mask,
@@ -1216,9 +1273,11 @@ bool wxImage::ConvertAlphaToMask(unsigned char threshold)
     return true;
 }
 
-#if wxUSE_PALETTE
-
+// ----------------------------------------------------------------------------
 // Palette functions
+// ----------------------------------------------------------------------------
+
+#if wxUSE_PALETTE
 
 bool wxImage::HasPalette() const
 {
@@ -1244,7 +1303,10 @@ void wxImage::SetPalette(const wxPalette& palette)
 
 #endif // wxUSE_PALETTE
 
+// ----------------------------------------------------------------------------
 // Option functions (arbitrary name/value mapping)
+// ----------------------------------------------------------------------------
+
 void wxImage::SetOption(const wxString& name, const wxString& value)
 {
     wxCHECK_RET( Ok(), wxT("invalid image") );
@@ -1292,6 +1354,10 @@ bool wxImage::HasOption(const wxString& name) const
     return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND);
 }
 
+// ----------------------------------------------------------------------------
+// image I/O
+// ----------------------------------------------------------------------------
+
 bool wxImage::LoadFile( const wxString& filename, long type, int index )
 {
 #if wxUSE_STREAMS
@@ -1549,6 +1615,10 @@ bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
 }
 #endif // wxUSE_STREAMS
 
+// ----------------------------------------------------------------------------
+// image I/O handlers
+// ----------------------------------------------------------------------------
+
 void wxImage::AddHandler( wxImageHandler *handler )
 {
     // Check for an existing handler of the type being added.