]> git.saurik.com Git - wxWidgets.git/commitdiff
enable wxDIB for Windows CE
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 4 Apr 2004 12:57:36 +0000 (12:57 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 4 Apr 2004 12:57:36 +0000 (12:57 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26603 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/bitmap.h
include/wx/msw/dib.h
include/wx/msw/wince/setup.h
src/msw/bitmap.cpp
src/msw/dib.cpp
src/msw/ole/dataobj.cpp

index d31371c89185dcda7e0c5e037db002cfee08483a..0af92d4ea2c463f827b5c40a8f80d7bfd0653f98 100644 (file)
@@ -71,7 +71,7 @@ public:
     // Create a bitmap compatible with the given DC
     wxBitmap(int width, int height, const wxDC& dc);
 
-#if wxUSE_IMAGE
+#if wxUSE_IMAGE && wxUSE_WXDIB
     // Convert from wxImage
     wxBitmap(const wxImage& image, int depth = -1)
         { (void)CreateFromImage(image, depth); }
@@ -108,7 +108,7 @@ public:
 
     virtual ~wxBitmap();
 
-#if wxUSE_IMAGE
+#if wxUSE_IMAGE && wxUSE_WXDIB
     wxImage ConvertToImage() const;
 #endif // wxUSE_IMAGE
 
@@ -186,7 +186,7 @@ protected:
     // creates an uninitialized bitmap, called from Create()s above
     bool DoCreate(int w, int h, int depth, WXHDC hdc);
 
-#if wxUSE_IMAGE
+#if wxUSE_IMAGE && wxUSE_WXDIB
     // creates the bitmap from wxImage, supposed to be called from ctor
     bool CreateFromImage(const wxImage& image, int depth);
 
index 7f0d2ecb07a7de170be5e0bdea11482b950182f7..011de9e1a28973105203af6db620a4df9eb931d6 100644 (file)
@@ -59,10 +59,12 @@ public:
     // operations
     // ----------
 
+#ifndef __WXWINCE__
     // create a bitmap compatiblr with the given HDC (or screen by default) and
     // return its handle, the caller is responsible for freeing it (using
     // DeleteObject())
     HBITMAP CreateDDB(HDC hdc = 0) const;
+#endif // !__WXWINCE__
 
     // get the handle from the DIB and reset it, i.e. this object won't destroy
     // the DIB after this (but the caller should do it)
@@ -105,6 +107,7 @@ public:
     // these functions are only used by wxWindows internally right now, please
     // don't use them directly if possible as they're subject to change
 
+#ifndef __WXWINCE__
     // creates a DDB compatible with the given (or screen) DC from either
     // a plain DIB or a DIB section (in which case the last parameter must be
     // non NULL)
@@ -122,6 +125,7 @@ public:
     // function (this overload is needed for wxBitmapDataObject code in
     // src/msw/ole/dataobj.cpp)
     static size_t ConvertFromBitmap(BITMAPINFO *pbi, HBITMAP hbmp);
+#endif // __WXWINCE__
 
 
     // wxImage conversion
@@ -158,6 +162,11 @@ private:
     // free resources
     void Free();
 
+    // initialize the contents from the provided DDB (Create() must have been
+    // already called)
+    bool CopyFromDDB(HBITMAP hbmp);
+
+
     // the DIB section handle, 0 if invalid
     HBITMAP m_handle;
 
index a5cd466b5144200c506fc5987193be5951f57823..6955ed0d2496877a3d9b5e3bc92a860f3975c9c0 100644 (file)
 #define wxUSE_DIB_FOR_BITMAP 0
 
 // Set this to 1 to enable wxDIB
-#define wxUSE_WXDIB 0
+#define wxUSE_WXDIB 1
 
 // ----------------------------------------------------------------------------
 // common dialogs
index 37e69a0e5c995feb00b1d272cb520a75011d1d38..86bddb84ca562a07824fd881a0e0effefdda93f5 100644 (file)
@@ -141,32 +141,44 @@ IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
 // ----------------------------------------------------------------------------
 
 // decide whether we should create a DIB or a DDB for the given parameters
-static bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
-{
-#if wxUSE_WXDIB
-    // here is the logic:
-    //
-    //  (a) if hdc is specified, the caller explicitly wants DDB
-    //  (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
-    //      less DIBs anyhow)
-    //  (c) finally, create DIBs under Win9x even if the depth hasn't been
-    //      explicitly specified but the current display depth is 24 or more
-    //      and the image is "big", i.e. > 16Mb which is the theoretical limit
-    //      for DDBs under Win9x
-    //
-    // consequences (all of which seem to make sense):
-    //
-    //  (i)     by default, DDBs are created (depth == -1 usually)
-    //  (ii)    DIBs can be created by explicitly specifying the depth
-    //  (iii)   using a DC always forces creating a DDB
-    return !hdc &&
-            (d >= 24 ||
-                (d == -1 &&
-                    wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
-#else
-    return FALSE;
-#endif
-}
+//
+// NB: we always use DIBs under Windows CE as this is much simpler (even if
+//     also less efficient...) and we obviously can't use them if there is no
+//     DIB support compiled in at all
+#ifdef __WXWINCE__
+    static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; }
+
+    #define ALWAYS_USE_DIB
+#elif !wxUSE_WXDIB
+    // no sense in defining wxShouldCreateDIB() as we can't compile code
+    // executed if it is true, so we have to use #if's anyhow
+    #define NEVER_USE_DIB
+#else // wxUSE_WXDIB && !__WXWINCE__
+    static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
+    {
+        // here is the logic:
+        //
+        //  (a) if hdc is specified, the caller explicitly wants DDB
+        //  (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
+        //      or less DIBs anyhow)
+        //  (c) finally, create DIBs under Win9x even if the depth hasn't been
+        //      explicitly specified but the current display depth is 24 or
+        //      more and the image is "big", i.e. > 16Mb which is the
+        //      theoretical limit for DDBs under Win9x
+        //
+        // consequences (all of which seem to make sense):
+        //
+        //  (i)     by default, DDBs are created (depth == -1 usually)
+        //  (ii)    DIBs can be created by explicitly specifying the depth
+        //  (iii)   using a DC always forces creating a DDB
+        return !hdc &&
+                (d >= 24 ||
+                    (d == -1 &&
+                        wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
+    }
+
+    #define SOMETIMES_USE_DIB
+#endif // different DIB usage scenarious
 
 // ----------------------------------------------------------------------------
 // wxBitmapRefData
@@ -288,14 +300,19 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon)
     return CopyFromIconOrCursor(icon);
 }
 
-#if wxUSE_WXDIB
+#ifndef NEVER_USE_DIB
+
 bool wxBitmap::CopyFromDIB(const wxDIB& dib)
 {
     wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
 
+#ifdef SOMETIMES_USE_DIB
     HBITMAP hbitmap = dib.CreateDDB();
     if ( !hbitmap )
         return FALSE;
+#else // ALWAYS_USE_DIB
+    HBITMAP hbitmap = ((wxDIB &)dib).Detach();  // const_cast
+#endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
 
     UnRef();
 
@@ -320,7 +337,8 @@ bool wxBitmap::CopyFromDIB(const wxDIB& dib)
 
     return TRUE;
 }
-#endif
+
+#endif // NEVER_USE_DIB
 
 wxBitmap::~wxBitmap()
 {
@@ -464,9 +482,9 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
     GetBitmapData()->m_width = w;
     GetBitmapData()->m_height = h;
 
-    HBITMAP hbmp;
+    HBITMAP hbmp    wxDUMMY_INITIALIZE(0);
 
-#if wxUSE_WXDIB
+#ifndef NEVER_USE_DIB
     if ( wxShouldCreateDIB(w, h, d, hdc) )
     {
         if ( d == -1 )
@@ -486,9 +504,9 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
         GetBitmapData()->m_depth = d;
     }
     else // create a DDB
-#endif
+#endif // NEVER_USE_DIB
     {
-
+#ifndef ALWAYS_USE_DIB
 #ifndef __WXMICROWIN__
         if ( d > 0 )
         {
@@ -497,8 +515,10 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
             {
                 wxLogLastError(wxT("CreateBitmap"));
             }
+
+            GetBitmapData()->m_depth = d;
         }
-        else
+        else // d == 0, create bitmap compatible with the screen
 #endif // !__WXMICROWIN__
         {
             ScreenHDC dc;
@@ -510,6 +530,7 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
 
             GetBitmapData()->m_depth = wxDisplayDepth();
         }
+#endif // !ALWAYS_USE_DIB
     }
 
     SetHBITMAP((WXHBITMAP)hbmp);
@@ -710,6 +731,8 @@ wxImage wxBitmap::ConvertToImage() const
 // wxImage to/from conversions
 // ----------------------------------------------------------------------------
 
+#if wxUSE_WXDIB
+
 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
 {
     return CreateFromImage(image, depth, 0);
@@ -723,9 +746,8 @@ bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
     return CreateFromImage(image, -1, dc.GetHDC());
 }
 
-bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
 {
-#if wxUSE_WXDIB
     wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
 
     UnRef();
@@ -749,7 +771,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
 
 
     // next either store DIB as is or create a DDB from it
-    HBITMAP hbitmap;
+    HBITMAP hbitmap     wxDUMMY_INITIALIZE(0);
 
     // are we going to use DIB?
     //
@@ -762,12 +784,14 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
         refData->m_isDIB = TRUE;
         refData->m_depth = dib.GetDepth();
     }
+#ifndef ALWAYS_USE_DIB
     else // we need to convert DIB to DDB
     {
         hbitmap = dib.CreateDDB((HDC)hdc);
 
         refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
     }
+#endif // !ALWAYS_USE_DIB
 
     // validate this object
     SetHBITMAP((WXHBITMAP)hbitmap);
@@ -781,350 +805,27 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
     }
 
     return TRUE;
-#else
-    // FIXME: wxWinCE doesn't support wxDIB yet
-    return FALSE;
-#endif
 }
 
 wxImage wxBitmap::ConvertToImage() const
 {
-    // FIXME: this is untested code for WinCE, and
-    // the mask is not yet handled.
-    // For tips, see:
-    // http://www.codeproject.com/bitmap/dibsection.asp?print=true
-
-#ifdef __WXWINCE__
-    // the colour used as transparent one in wxImage and the one it is replaced
-    // with when it really occurs in the bitmap
-    static const int MASK_RED = 1;
-    static const int MASK_GREEN = 2;
-    static const int MASK_BLUE = 3;
-    static const int MASK_BLUE_REPLACEMENT = 2;
+    wxDIB dib(*this);
 
     wxImage image;
-
-    wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
-
-    // create an wxImage object
-    int width = GetWidth();
-    int height = GetHeight();
-    image.Create( width, height );
-    unsigned char *data = image.GetData();
-    if( !data )
-    {
-        wxFAIL_MSG( wxT("could not allocate data for image") );
-        return wxNullImage;
-    }
-
-    // calc the number of bytes per scanline and padding in the DIB
-    int bytePerLine = width*3;
-    int sizeDWORD = sizeof( DWORD );
-    int lineBoundary =  bytePerLine % sizeDWORD;
-    int padding = 0;
-    if( lineBoundary > 0 )
-    {
-        padding = sizeDWORD - lineBoundary;
-        bytePerLine += padding;
-    }
-
-    // create a DIB header
-    int headersize = sizeof(BITMAPINFOHEADER);
-    BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
-    if( !lpDIBh )
+    if ( dib.IsOk() )
     {
-        wxFAIL_MSG( wxT("could not allocate data for DIB header") );
-        free( data );
-        return wxNullImage;
-    }
-    // Fill in the DIB header
-    lpDIBh->bmiHeader.biSize = headersize;
-    lpDIBh->bmiHeader.biWidth = width;
-    lpDIBh->bmiHeader.biHeight = -height;
-    lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
-    lpDIBh->bmiHeader.biPlanes = 1;
-    lpDIBh->bmiHeader.biBitCount = 24;
-    lpDIBh->bmiHeader.biCompression = BI_RGB;
-    lpDIBh->bmiHeader.biClrUsed = 0;
-    // These seem not really needed for our purpose here.
-    lpDIBh->bmiHeader.biClrImportant = 0;
-    lpDIBh->bmiHeader.biXPelsPerMeter = 0;
-    lpDIBh->bmiHeader.biYPelsPerMeter = 0;
-
-    // memory for DIB data is allocated by CreateDIBSection
-    void *lpBits = NULL;
-
-    // copy data from the device-dependent bitmap to the DIB
-    HDC hdc = ::GetDC(NULL);
-    HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
-
-    HBITMAP hBitmapSection = ::CreateDIBSection( hdc, lpDIBh, DIB_RGB_COLORS, & lpBits, NULL, 0 );
-    if (!hBitmapSection)
-    {
-        wxFAIL_MSG( wxT("could not create a DIB section") );
-        return wxNullImage;
-    }
-
-    // Copy the image from the DDB to the DIBSection
-    // Need to copy the supplied bitmap onto the newly created DIBsection
-    HDC hMemDC = CreateCompatibleDC(hdc);
-       HDC hCopyDC = CreateCompatibleDC(hdc);
-       
-    if (! hMemDC || ! hCopyDC) 
-    {
-        wxFAIL_MSG( wxT("unable to create compatible DCs") );
-        return wxNullImage;
-    }
-
-#if 0
-    if (m_hPal)
-    {
-        SelectPalette(hMemDC, m_hPal, FALSE);   RealizePalette(hMemDC);
-        SelectPalette(hCopyDC, m_hPal, FALSE);  RealizePalette(hCopyDC);
-    }
-#endif
-
-    HBITMAP hOldMemBitmap  = (HBITMAP) SelectObject(hMemDC,  hBitmap);
-    HBITMAP hOldCopyBitmap = (HBITMAP) SelectObject(hCopyDC, hBitmapSection);
-
-    BitBlt(hCopyDC, 0, 0, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);
-
-    SelectObject(hMemDC,  hOldMemBitmap);
-    SelectObject(hCopyDC, hOldCopyBitmap);
-    DeleteDC(hMemDC);
-    DeleteDC(hCopyDC);
-
-#if 0
-    if (m_hPal)
-    {
-               HGDIOBJ hObj = ::GetStockObject(DEFAULT_PALETTE);
-        SelectObject(hMemDC, hObj);
-        SelectObject(hCopyDC, hObj);
-    }
-#endif
-
-       ReleaseDC(NULL, hdc);
-
-    // copy DIB data into the wxImage object
-    int i, j;
-    unsigned char *ptdata = data;
-    unsigned char *ptbits = (unsigned char*) lpBits;
-    for( i=0; i<height; i++ )
-    {
-        for( j=0; j<width; j++ )
-        {
-            *(ptdata++) = *(ptbits+2);
-            *(ptdata++) = *(ptbits+1);
-            *(ptdata++) = *(ptbits  );
-            ptbits += 3;
-        }
-        ptbits += padding;
-    }
-
-    // TODO
-#if 0
-    // similarly, set data according to the possible mask bitmap
-    if( GetMask() && GetMask()->GetMaskBitmap() )
-    {
-        hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
-        // memory DC created, color set, data copied, and memory DC deleted
-        HDC memdc = ::CreateCompatibleDC( hdc );
-        ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
-        ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
-        ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
-        ::DeleteDC( memdc );
-        ptdata = data;
-        ptbits = lpBits;
-        for( i=0; i<height; i++ )
-        {
-            for( j=0; j<width; j++ )
-            {
-                // is this pixel transparent?
-                if ( *ptbits != 0 )
-                {
-                    if ( (ptdata[0] == MASK_RED) &&
-                            (ptdata[1] == MASK_GREEN) &&
-                                (ptdata[2] == MASK_BLUE) )
-                    {
-                        // we have to fudge the colour a bit to prevent this
-                        // pixel from appearing transparent
-                        ptdata[2] = MASK_BLUE_REPLACEMENT;
-                    }
-                    ptdata += 3;
-                }
-                else // masked pixel
-                {
-                    *(ptdata++)  = MASK_RED;
-                    *(ptdata++)  = MASK_GREEN;
-                    *(ptdata++)  = MASK_BLUE;
-                }
-                ptbits += 3;
-            }
-            ptbits += padding;
-        }
-
-        image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
-        image.SetMask( TRUE );
-    }
-    else
-#endif
-    {
-        image.SetMask( FALSE );
-    }
-
-    // free allocated resources
-    ::ReleaseDC(NULL, hdc);
-    free(lpDIBh);
-
-    // Delete the DIB section
-    ::DeleteObject(hBitmapSection);
-
-    return image;
-#else
-    // the colour used as transparent one in wxImage and the one it is replaced
-    // with when it really occurs in the bitmap
-    static const int MASK_RED = 1;
-    static const int MASK_GREEN = 2;
-    static const int MASK_BLUE = 3;
-    static const int MASK_BLUE_REPLACEMENT = 2;
-
-    wxImage image;
-
-    wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
-
-    // create an wxImage object
-    int width = GetWidth();
-    int height = GetHeight();
-    image.Create( width, height );
-    unsigned char *data = image.GetData();
-    if( !data )
-    {
-        wxFAIL_MSG( wxT("could not allocate data for image") );
-        return wxNullImage;
-    }
-
-    // calc the number of bytes per scanline and padding in the DIB
-    int bytePerLine = width*3;
-    int sizeDWORD = sizeof( DWORD );
-    int lineBoundary =  bytePerLine % sizeDWORD;
-    int padding = 0;
-    if( lineBoundary > 0 )
-    {
-        padding = sizeDWORD - lineBoundary;
-        bytePerLine += padding;
-    }
-
-    // create a DIB header
-    int headersize = sizeof(BITMAPINFOHEADER);
-    BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
-    if( !lpDIBh )
-    {
-        wxFAIL_MSG( wxT("could not allocate data for DIB header") );
-        free( data );
-        return wxNullImage;
-    }
-    // Fill in the DIB header
-    lpDIBh->bmiHeader.biSize = headersize;
-    lpDIBh->bmiHeader.biWidth = width;
-    lpDIBh->bmiHeader.biHeight = -height;
-    lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
-    lpDIBh->bmiHeader.biPlanes = 1;
-    lpDIBh->bmiHeader.biBitCount = 24;
-    lpDIBh->bmiHeader.biCompression = BI_RGB;
-    lpDIBh->bmiHeader.biClrUsed = 0;
-    // These seem not really needed for our purpose here.
-    lpDIBh->bmiHeader.biClrImportant = 0;
-    lpDIBh->bmiHeader.biXPelsPerMeter = 0;
-    lpDIBh->bmiHeader.biYPelsPerMeter = 0;
-    // memory for DIB data
-    unsigned char *lpBits;
-    lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
-    if( !lpBits )
-    {
-        wxFAIL_MSG( wxT("could not allocate data for DIB") );
-        free( data );
-        free( lpDIBh );
-        return wxNullImage;
-    }
-
-    // copy data from the device-dependent bitmap to the DIB
-    HDC hdc = ::GetDC(NULL);
-    HBITMAP hbitmap;
-    hbitmap = (HBITMAP) GetHBITMAP();
-    ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
-
-    // copy DIB data into the wxImage object
-    int i, j;
-    unsigned char *ptdata = data;
-    unsigned char *ptbits = lpBits;
-    for( i=0; i<height; i++ )
-    {
-        for( j=0; j<width; j++ )
-        {
-            *(ptdata++) = *(ptbits+2);
-            *(ptdata++) = *(ptbits+1);
-            *(ptdata++) = *(ptbits  );
-            ptbits += 3;
-        }
-        ptbits += padding;
-    }
-
-    // similarly, set data according to the possible mask bitmap
-    if( GetMask() && GetMask()->GetMaskBitmap() )
-    {
-        hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
-        // memory DC created, color set, data copied, and memory DC deleted
-        HDC memdc = ::CreateCompatibleDC( hdc );
-        ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
-        ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
-        ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
-        ::DeleteDC( memdc );
-        ptdata = data;
-        ptbits = lpBits;
-        for( i=0; i<height; i++ )
+        image = dib.ConvertToImage();
+        if ( image.Ok() )
         {
-            for( j=0; j<width; j++ )
-            {
-                // is this pixel transparent?
-                if ( *ptbits != 0 )
-                {
-                    if ( (ptdata[0] == MASK_RED) &&
-                            (ptdata[1] == MASK_GREEN) &&
-                                (ptdata[2] == MASK_BLUE) )
-                    {
-                        // we have to fudge the colour a bit to prevent this
-                        // pixel from appearing transparent
-                        ptdata[2] = MASK_BLUE_REPLACEMENT;
-                    }
-                    ptdata += 3;
-                }
-                else // masked pixel
-                {
-                    *(ptdata++)  = MASK_RED;
-                    *(ptdata++)  = MASK_GREEN;
-                    *(ptdata++)  = MASK_BLUE;
-                }
-                ptbits += 3;
-            }
-            ptbits += padding;
+            // TODO: set mask
         }
-
-        image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
-        image.SetMask( TRUE );
     }
-    else
-    {
-        image.SetMask( FALSE );
-    }
-    // free allocated resources
-    ::ReleaseDC(NULL, hdc);
-    free(lpDIBh);
-    free(lpBits);
 
     return image;
-#endif
 }
 
+#endif // wxUSE_WXDIB
+
 #endif // wxUSE_IMAGE
 
 // ----------------------------------------------------------------------------
@@ -1144,7 +845,7 @@ bool wxBitmap::LoadFile(const wxString& filename, long type)
         return handler->LoadFile(this, filename, type, -1, -1);
     }
 #if wxUSE_IMAGE
-    else
+    else // no bitmap handler found
     {
         wxImage image;
         if ( image.LoadFile( filename, type ) && image.Ok() )
@@ -1188,7 +889,7 @@ bool wxBitmap::SaveFile(const wxString& filename,
         return handler->SaveFile(this, filename, type, palette);
     }
 #if wxUSE_IMAGE
-    else
+    else // no bitmap handler found
     {
         // FIXME what about palette? shouldn't we use it?
         wxImage image = ConvertToImage();
index 3b42e9c0f7089b16150897835572c5fca7adf54c..8da2f37757c6f6934244972b3d857f63b8e7d902 100644 (file)
@@ -45,7 +45,7 @@
 #include <stdlib.h>
 
 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
-#include <memory.h>
+    #include <memory.h>
 #endif
 
 #ifdef __GNUWIN32_OLD__
 #include "wx/image.h"
 #include "wx/msw/dib.h"
 
+#ifdef __WXWINCE__
+    #include <shellapi.h>       // for SHLoadDIBitmap()
+#endif
+
 // ----------------------------------------------------------------------------
 // private functions
 // ----------------------------------------------------------------------------
 
 // calculate the number of palette entries needed for the bitmap with this
 // number of bits per pixel
-static inline WORD wxGetNumOfBitmapColors(WORD bitsPerPixel)
+static inline WORD GetNumberOfColours(WORD bitsPerPixel)
 {
     // only 1, 4 and 8bpp bitmaps use palettes (well, they could be used with
     // 24bpp ones too but we don't support this as I think it's quite uncommon)
@@ -89,18 +93,21 @@ static inline bool GetDIBSection(HBITMAP hbmp, DIBSECTION *ds)
 
 bool wxDIB::Create(int width, int height, int depth)
 {
-    // we don't handle the palette yet
-    wxASSERT_MSG( depth == 24 || depth == 32,
-                    _T("unsupported image depth in wxDIB::Create()") );
+    // we don't support formats using palettes right now so we only create
+    // either 24bpp (RGB) or 32bpp (RGBA) bitmaps
+    wxASSERT_MSG( depth, _T("invalid image depth in wxDIB::Create()") );
+    if ( depth < 24 )
+        depth = 24;
 
-    static const int infosize = sizeof(BITMAPINFOHEADER);
+    // allocate memory for bitmap structures
+    static const int sizeHeader = sizeof(BITMAPINFOHEADER);
 
-    BITMAPINFO *info = (BITMAPINFO *)malloc(infosize);
+    BITMAPINFO *info = (BITMAPINFO *)malloc(sizeHeader);
     wxCHECK_MSG( info, false, _T("malloc(BITMAPINFO) failed") );
 
-    memset(info, 0, infosize);
+    memset(info, 0, sizeHeader);
 
-    info->bmiHeader.biSize = infosize;
+    info->bmiHeader.biSize = sizeHeader;
     info->bmiHeader.biWidth = width;
 
     // we use positive height here which corresponds to a DIB with normal, i.e.
@@ -168,46 +175,103 @@ bool wxDIB::Create(const wxBitmap& bmp)
         const int w = bmp.GetWidth();
         const int h = bmp.GetHeight();
         int d = bmp.GetDepth();
-        if ( d == -1 )
+        if ( d <= 0 )
             d = wxDisplayDepth();
 
-        if ( !Create(w, h, d) )
+        if ( !Create(w, h, d) || !CopyFromDDB(hbmp) )
             return false;
+    }
 
-        if ( !GetDIBSection(m_handle, &ds) )
-        {
-            // we've just created a new DIB section, why should this fail?
-            wxFAIL_MSG( _T("GetObject(DIBSECTION) unexpectedly failed") );
+    return true;
+}
 
-            return false;
-        }
+// Windows CE doesn't have GetDIBits() so use an alternative implementation
+// for it
+//
+// in fact I'm not sure if GetDIBits() is really much better than using
+// BitBlt() like this -- it should be faster but I didn't do any tests, if
+// anybody has time to do them and by chance finds that GetDIBits() is not
+// much faster than BitBlt(), we could always use the Win CE version here
+#ifdef __WXWINCE__
 
-        if ( !::GetDIBits
-                (
-                    ScreenHDC(),                // the DC to use
-                    hbmp,                       // the source DDB
-                    0,                          // first scan line
-                    h,                          // number of lines to copy
-                    ds.dsBm.bmBits,             // pointer to the buffer
-                    (BITMAPINFO *)&ds.dsBmih,   // bitmap header
-                    DIB_RGB_COLORS              // and not DIB_PAL_COLORS
-                ) )
-        {
-            wxLogLastError(wxT("GetDIBits()"));
+bool wxDIB::CopyFromDDB(HBITMAP hbmp)
+{
+    MemoryHDC hdcSrc;
+    if ( !hdcSrc )
+        return false;
 
-            return 0;
-        }
+    SelectInHDC selectSrc(hdcSrc, hbmp);
+    if ( !selectSrc )
+        return false;
+
+    MemoryHDC hdcDst;
+    if ( !hdcDst )
+        return false;
+
+    SelectInHDC selectDst(hdcDst, m_handle);
+    if ( !selectDst )
+        return false;
+
+
+    if ( !::BitBlt(
+                    hdcDst,
+                    0, 0, m_width, m_height,
+                    hdcSrc,
+                    0, 0,
+                    SRCCOPY
+                  ) )
+    {
+        wxLogLastError(_T("BitBlt(DDB -> DIB)"));
+
+        return false;
+    }
+
+    return true;
+}
+
+#else // !__WXWINCE__
+
+bool wxDIB::CopyFromDDB(HBITMAP hbmp)
+{
+    DIBSECTION ds;
+    if ( !GetDIBSection(m_handle, &ds) )
+    {
+        // we're sure that our handle is a DIB section, so this should work
+        wxFAIL_MSG( _T("GetObject(DIBSECTION) unexpectedly failed") );
+
+        return false;
+    }
+
+    if ( !::GetDIBits
+            (
+                ScreenHDC(),                // the DC to use
+                hbmp,                       // the source DDB
+                0,                          // first scan line
+                m_height,                   // number of lines to copy
+                ds.dsBm.bmBits,             // pointer to the buffer
+                (BITMAPINFO *)&ds.dsBmih,   // bitmap header
+                DIB_RGB_COLORS              // and not DIB_PAL_COLORS
+            ) )
+    {
+        wxLogLastError(wxT("GetDIBits()"));
+
+        return false;
     }
 
     return true;
 }
 
+#endif // __WXWINCE__/!__WXWINCE__
+
 // ----------------------------------------------------------------------------
 // Loading/saving the DIBs
 // ----------------------------------------------------------------------------
 
 bool wxDIB::Load(const wxString& filename)
 {
+#ifdef __WXWINCE__
+    m_handle = SHLoadDIBitmap(filename);
+#else // !__WXWINCE__
     m_handle = (HBITMAP)::LoadImage
                          (
                             wxGetInstance(),
@@ -216,9 +280,11 @@ bool wxDIB::Load(const wxString& filename)
                             0, 0, // don't specify the size
                             LR_CREATEDIBSECTION | LR_LOADFROMFILE
                          );
+#endif // __WXWINCE__
+
     if ( !m_handle )
     {
-        wxLogLastError(_T("LoadImage(LR_CREATEDIBSECTION | LR_LOADFROMFILE)"));
+        wxLogLastError(_T("Loading DIB from file"));
 
         return false;
     }
@@ -303,6 +369,8 @@ void wxDIB::DoGetObject() const
 // DDB <-> DIB conversions
 // ----------------------------------------------------------------------------
 
+#ifndef __WXWINCE__
+
 HBITMAP wxDIB::CreateDDB(HDC hdc) const
 {
     wxCHECK_MSG( m_handle, 0, _T("wxDIB::CreateDDB(): invalid object") );
@@ -351,7 +419,7 @@ HBITMAP wxDIB::ConvertToBitmap(const BITMAPINFO *pbmi, HDC hdc, void *bits)
                 numColors = pbmih->biClrUsed;
                 if ( !numColors )
                 {
-                    numColors = wxGetNumOfBitmapColors(pbmih->biBitCount);
+                    numColors = GetNumberOfColours(pbmih->biBitCount);
                 }
                 break;
 
@@ -417,7 +485,7 @@ size_t wxDIB::ConvertFromBitmap(BITMAPINFO *pbi, HBITMAP hbmp)
     bi.biBitCount = bm.bmBitsPixel;
 
     // memory we need for BITMAPINFO only
-    DWORD dwLen = bi.biSize + wxGetNumOfBitmapColors(bm.bmBitsPixel) * sizeof(RGBQUAD);
+    DWORD dwLen = bi.biSize + GetNumberOfColours(bm.bmBitsPixel) * sizeof(RGBQUAD);
 
     // get either just the image size or the image bits
     if ( !::GetDIBits
@@ -475,6 +543,8 @@ HGLOBAL wxDIB::ConvertFromBitmap(HBITMAP hbmp)
     return hDIB;
 }
 
+#endif // __WXWINCE__
+
 // ----------------------------------------------------------------------------
 // palette support
 // ----------------------------------------------------------------------------
@@ -498,7 +568,7 @@ wxPalette *wxDIB::CreatePalette() const
     if ( !biClrUsed )
     {
         // biClrUsed field might not be set
-        biClrUsed = wxGetNumOfBitmapColors(ds.dsBmih.biBitCount);
+        biClrUsed = GetNumberOfColours(ds.dsBmih.biBitCount);
     }
 
     if ( !biClrUsed )
@@ -606,8 +676,64 @@ bool wxDIB::Create(const wxImage& image)
     return true;
 }
 
+wxImage wxDIB::ConvertToImage() const
+{
+    wxCHECK_MSG( IsOk(), wxNullImage,
+                    wxT("can't convert invalid DIB to wxImage") );
+
+    // create the wxImage object
+    const int w = GetWidth();
+    const int h = GetHeight();
+    wxImage image(w, h, false /* don't bother clearing memory */);
+    if ( !image.Ok() )
+    {
+        wxFAIL_MSG( wxT("could not allocate data for image") );
+        return wxNullImage;
+    }
+
+    const int bpp = GetDepth();
+    if ( bpp == 32 )
+    {
+        image.SetAlpha();
+    }
+
+    // this is the same loop as in Create() just above but with copy direction
+    // reversed
+    const int dstBytesPerLine = w * 3;
+    const int srcBytesPerLine = GetLineSize(w, bpp);
+    unsigned char *dst = image.GetData() + ((h - 1) * dstBytesPerLine);
+    unsigned char *alpha = image.HasAlpha() ? image.GetAlpha() + (h - 1)*w
+                                            : NULL;
+    const unsigned char *srcLineStart = (unsigned char *)GetData();
+    for ( int y = 0; y < h; y++ )
+    {
+        // copy one DIB line
+        const unsigned char *src = srcLineStart;
+        for ( int x = 0; x < w; x++ )
+        {
+            dst[2] = *src++;
+            dst[1] = *src++;
+            dst[0] = *src++;
+
+            dst += 3;
+
+            if ( alpha )
+                *alpha++ = *src++;
+        }
+
+        // pass to the previous line in the image
+        dst -= 2*dstBytesPerLine;
+        if ( alpha )
+            alpha -= 2*w;
+
+        // and to the next one in the DIB
+        srcLineStart += srcBytesPerLine;
+    }
+
+    return image;
+}
+
 #endif // wxUSE_IMAGE
 
-#endif
-    // wxUSE_WXDIB
-    
+#endif // wxUSE_WXDIB
+
index fba8eb1427db6341aa19a33039728b780e39d2fe..9c70b15cb0d3e4967ecf75afd203b2e52a758625 100644 (file)
@@ -761,9 +761,11 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
 // wxBitmapDataObject supports CF_DIB format
 // ----------------------------------------------------------------------------
 
+// TODO: support CF_DIB under Windows CE as well
+
 size_t wxBitmapDataObject::GetDataSize() const
 {
-#if wxUSE_WXDIB
+#if wxUSE_WXDIB && !defined(__WXWINCE__)
     return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
 #else
     return 0;
@@ -772,7 +774,7 @@ size_t wxBitmapDataObject::GetDataSize() const
 
 bool wxBitmapDataObject::GetDataHere(void *buf) const
 {
-#if wxUSE_WXDIB
+#if wxUSE_WXDIB && !defined(__WXWINCE__)
     BITMAPINFO * const pbi = (BITMAPINFO *)buf;
 
     return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
@@ -783,7 +785,7 @@ bool wxBitmapDataObject::GetDataHere(void *buf) const
 
 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
 {
-#if wxUSE_WXDIB
+#if wxUSE_WXDIB && !defined(__WXWINCE__)
     const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
 
     HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);