]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/image.cpp
wxSocketClient::Connect() now honours the 'wait' parameter.
[wxWidgets.git] / src / common / image.cpp
index 91fc0fb9be8a39df1360fcaf03116736fc69764f..ab36e52be6d04b5260e104fd2823efe0c9cd274f 100644 (file)
@@ -15,7 +15,7 @@
 #include "wx/wxprec.h"
 
 #ifdef __BORLANDC__
-#pragma hdrstop
+    #pragma hdrstop
 #endif
 
 #include "wx/image.h"
 #include <string.h>
 
 #ifdef __SALFORDC__
-#ifdef FAR
-#undef FAR
-#endif
+    #undef FAR
 #endif
 
 #ifdef __WXMSW__
-#include <windows.h>
+    #include "wx/msw/private.h"
 #endif
 
 //-----------------------------------------------------------------------------
@@ -49,8 +47,8 @@ class wxImageRefData: public wxObjectRefData
 {
 
 public:
-    wxImageRefData(void);
-    ~wxImageRefData(void);
+    wxImageRefData();
+    ~wxImageRefData();
 
     int             m_width;
     int             m_height;
@@ -60,7 +58,7 @@ public:
     bool            m_ok;
 };
 
-wxImageRefData::wxImageRefData(void)
+wxImageRefData::wxImageRefData()
 {
     m_width = 0;
     m_height = 0;
@@ -72,7 +70,7 @@ wxImageRefData::wxImageRefData(void)
     m_hasMask = FALSE;
 }
 
-wxImageRefData::~wxImageRefData(void)
+wxImageRefData::~wxImageRefData()
 {
     if (m_data) free( m_data );
 }
@@ -84,7 +82,7 @@ wxList wxImage::sm_handlers;
 #define M_IMGDATA ((wxImageRefData *)m_refData)
 
 #if !USE_SHARED_LIBRARIES
-IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
+    IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
 #endif
 
 wxImage::wxImage()
@@ -152,7 +150,7 @@ void wxImage::Destroy()
     UnRef();
 }
 
-wxImage wxImage::Scale( int width, int height )
+wxImage wxImage::Scale( int width, int height ) const
 {
     wxImage image;
 
@@ -191,6 +189,43 @@ wxImage wxImage::Scale( int width, int height )
     return image;
 }
 
+wxImage wxImage::GetSubImage( const wxRect &rect ) const
+{
+    wxImage image;
+
+    wxCHECK_MSG( Ok(), image, _T("invalid image") );
+
+    wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight())
+                , image, _T("invalid subimage size") );
+
+    int subwidth=rect.GetWidth();
+    const int subheight=rect.GetHeight();
+
+    image.Create( subwidth, subheight );
+
+    char unsigned *subdata = image.GetData(), *data=GetData();
+
+    wxCHECK_MSG( subdata, image, _T("unable to create image") );
+
+    if (M_IMGDATA->m_hasMask)
+        image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
+
+    const int subleft=3*rect.GetLeft();
+    const int width=3*GetWidth();
+    subwidth*=3;
+    
+    data+=rect.GetTop()*width+subleft;
+
+    for (long j = 0; j < subheight; ++j)
+    {
+        memcpy( subdata, data, subwidth);
+        subdata+=subwidth;
+       data+=width;
+    }
+
+    return image;
+}
+
 void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
 {
     wxCHECK_RET( Ok(), _T("invalid image") );
@@ -265,7 +300,20 @@ void wxImage::SetData( char unsigned *data )
 {
     wxCHECK_RET( Ok(), _T("invalid image") );
 
-    memcpy(M_IMGDATA->m_data, data, M_IMGDATA->m_width * M_IMGDATA->m_height * 3);
+    wxImageRefData *newRefData = new wxImageRefData();
+
+    newRefData->m_width = M_IMGDATA->m_width;
+    newRefData->m_height = M_IMGDATA->m_height;
+    newRefData->m_data = data;
+    newRefData->m_ok = TRUE;
+    newRefData->m_maskRed = M_IMGDATA->m_maskRed;
+    newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
+    newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
+    newRefData->m_hasMask = M_IMGDATA->m_hasMask;
+    
+    UnRef();
+    
+    m_refData = newRefData;
 }
 
 void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
@@ -390,13 +438,32 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
 }
 
 #if wxUSE_STREAMS
+
 bool wxImage::LoadFile( wxInputStream& stream, long type )
 {
     UnRef();
 
     m_refData = new wxImageRefData;
 
-    wxImageHandler *handler = FindHandler(type);
+    wxImageHandler *handler;
+
+    if (type==wxBITMAP_TYPE_ANY)
+    {
+       wxList &list=GetHandlers();
+
+       for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
+       {  
+           handler=(wxImageHandler*)node->GetData();
+        if (handler->CanRead( stream ))
+            return handler->LoadFile( this, stream );
+
+       }
+
+       wxLogWarning( _T("No handler found for this image.") );
+       return FALSE;
+    }
+
+    handler = FindHandler(type);
 
     if (handler == NULL)
     {
@@ -540,7 +607,7 @@ wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
 
 void wxImage::InitStandardHandlers()
 {
-    AddHandler( new wxBMPHandler );
+  AddHandler( new wxBMPHandler );
 }
 
 void wxImage::CleanUpHandlers()
@@ -565,15 +632,42 @@ IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject)
 #endif
 
 #if wxUSE_STREAMS
-bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream) )
+bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
+{
+    return FALSE;
+}
+
+bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
 {
     return FALSE;
 }
 
-bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream) )
+bool wxImageHandler::CanRead( wxInputStream& stream )
 {
     return FALSE;
 }
+
+bool wxImageHandler::CanRead( const wxString& name )
+{
+#if wxUSE_STREAMS
+    if (wxFileExists(name))
+    {
+        wxFileInputStream stream(name);
+        return CanRead(stream);
+    }
+
+    else {
+        wxLogError( _T("Can't check image format of file '%s': file does not exist."), name.c_str() );
+
+        return FALSE;
+    }
+#else // !wxUSE_STREAMS
+    return FALSE;
+#endif // wxUSE_STREAMS
+}
+
+
+
 #endif // wxUSE_STREAMS
 
 //-----------------------------------------------------------------------------
@@ -584,8 +678,15 @@ bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSE
 
 wxBitmap wxImage::ConvertToBitmap() const
 {
+    if ( !Ok() )
+        return wxNullBitmap;
+
     // sizeLimit is the MS upper limit for the DIB size
+#ifdef  WIN32
     int sizeLimit = 1024*768*3;
+#else
+    int sizeLimit = 0x7fff ;
+#endif
 
     // width and height of the device-dependent bitmap
     int width = GetWidth();
@@ -594,11 +695,11 @@ wxBitmap wxImage::ConvertToBitmap() const
     // calc the number of bytes per scanline and padding
     int bytePerLine = width*3;
     int sizeDWORD = sizeof( DWORD );
-    div_t lineBoundary = div( bytePerLine, sizeDWORD );
+    int lineBoundary = bytePerLine % sizeDWORD;
     int padding = 0;
-    if( lineBoundary.rem > 0 )
+    if( lineBoundary > 0 )
     {
-        padding = sizeDWORD - lineBoundary.rem;
+        padding = sizeDWORD - lineBoundary;
         bytePerLine += padding;
     }
     // calc the number of DIBs and heights of DIBs
@@ -609,9 +710,8 @@ wxBitmap wxImage::ConvertToBitmap() const
         height = bmpHeight;
     else
     {
-        div_t result = div( bmpHeight, height );
-        numDIB = result.quot;
-        hRemain = result.rem;
+        numDIB =  bmpHeight / height;
+        hRemain = bmpHeight % height;
         if( hRemain >0 )  numDIB++;
     }
 
@@ -807,11 +907,11 @@ wxImage::wxImage( const wxBitmap &bitmap )
     // calc the number of bytes per scanline and padding in the DIB
     int bytePerLine = width*3;
     int sizeDWORD = sizeof( DWORD );
-    div_t lineBoundary = div( bytePerLine, sizeDWORD );
+    int lineBoundary =  bytePerLine % sizeDWORD;
     int padding = 0;
-    if( lineBoundary.rem > 0 )
+    if( lineBoundary > 0 )
     {
-        padding = sizeDWORD - lineBoundary.rem;
+        padding = sizeDWORD - lineBoundary;
         bytePerLine += padding;
     }
 
@@ -946,7 +1046,7 @@ wxBitmap wxImage::ConvertToBitmap() const
      // Retrieve depth
 
     GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
-    if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
+    if (visual == NULL) visual = gdk_visual_get_system();
     int bpp = visual->depth;
 
     bitmap.SetDepth( bpp );
@@ -1181,7 +1281,8 @@ wxImage::wxImage( const wxBitmap &bitmap )
     {
         for (int i = 0; i < bitmap.GetWidth(); i++)
         {
-            int pixel = gdk_image_get_pixel( gdk_image, i, j );
+            wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j );
+           pixel = wxINT32_SWAP_ON_BE( pixel );
             if (bpp <= 8)
             {
                 data[pos] = cmap->colors[pixel].red >> 8;
@@ -1487,7 +1588,7 @@ wxImage::wxImage( const wxBitmap &bitmap )
 
     vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
 
-    wxCHECK_MSG( vi, wxNullBitmap, _T("no visual") );
+    wxCHECK_RET( vi, _T("no visual") );
 
     if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;