]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
0118ff56bdeac342f0f13becc8329a51aed279cd
[wxWidgets.git] / src / msw / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "bitmap.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include <stdio.h>
33
34 #include "wx/list.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
40 #include "wx/icon.h"
41 #endif
42
43 #include "wx/msw/private.h"
44 #include "wx/log.h"
45
46 #if !defined(__WXMICROWIN__)
47 #include "wx/msw/dib.h"
48 #endif
49
50 #include "wx/image.h"
51 #include "wx/xpmdecod.h"
52
53 // missing from mingw32 header
54 #ifndef CLR_INVALID
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
57
58 // ----------------------------------------------------------------------------
59 // macros
60 // ----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
63 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
64
65 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxBitmapRefData
73 // ----------------------------------------------------------------------------
74
75 wxBitmapRefData::wxBitmapRefData()
76 {
77 m_quality = 0;
78 m_selectedInto = NULL;
79 m_numColors = 0;
80 m_bitmapMask = NULL;
81 m_hBitmap = (WXHBITMAP) NULL;
82 }
83
84 void wxBitmapRefData::Free()
85 {
86 wxASSERT_MSG( !m_selectedInto,
87 wxT("deleting bitmap still selected into wxMemoryDC") );
88
89 if ( m_hBitmap)
90 {
91 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
92 {
93 wxLogLastError(wxT("DeleteObject(hbitmap)"));
94 }
95 }
96
97 delete m_bitmapMask;
98 m_bitmapMask = NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // wxBitmap creation
103 // ----------------------------------------------------------------------------
104
105 // this function should be called from all wxBitmap ctors
106 void wxBitmap::Init()
107 {
108 // m_refData = NULL; done in the base class ctor
109
110 }
111
112 #ifdef __WIN32__
113
114 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
115 {
116 #ifndef __WXMICROWIN__
117 // it may be either HICON or HCURSOR
118 HICON hicon = (HICON)icon.GetHandle();
119
120 ICONINFO iconInfo;
121 if ( !::GetIconInfo(hicon, &iconInfo) )
122 {
123 wxLogLastError(wxT("GetIconInfo"));
124
125 return FALSE;
126 }
127
128 wxBitmapRefData *refData = new wxBitmapRefData;
129 m_refData = refData;
130
131 int w = icon.GetWidth(),
132 h = icon.GetHeight();
133
134 refData->m_width = w;
135 refData->m_height = h;
136 refData->m_depth = wxDisplayDepth();
137
138 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
139
140 // the mask returned by GetIconInfo() is inversed compared to the usual
141 // wxWin convention
142 refData->m_bitmapMask = new wxMask((WXHBITMAP)
143 wxInvertMask(iconInfo.hbmMask, w, h));
144
145
146 // delete the old one now as we don't need it any more
147 ::DeleteObject(iconInfo.hbmMask);
148
149 #if WXWIN_COMPATIBILITY_2
150 refData->m_ok = TRUE;
151 #endif // WXWIN_COMPATIBILITY_2
152
153 return TRUE;
154 #else
155 return FALSE;
156 #endif
157 }
158
159 #endif // Win32
160
161 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
162 {
163 UnRef();
164
165 if ( !cursor.Ok() )
166 return FALSE;
167
168 #ifdef __WIN16__
169 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
170
171 return FALSE;
172 #else
173 return CopyFromIconOrCursor(cursor);
174 #endif // Win16
175 }
176
177 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
178 {
179 UnRef();
180
181 if ( !icon.Ok() )
182 return FALSE;
183
184 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
185 // to create a bitmap from icon there - but using this way we won't have
186 // the mask (FIXME)
187 #ifdef __WIN16__
188 int width = icon.GetWidth(),
189 height = icon.GetHeight();
190
191 // copy the icon to the bitmap
192 ScreenHDC hdcScreen;
193 HDC hdc = ::CreateCompatibleDC(hdcScreen);
194 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
195 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
196
197 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
198
199 ::SelectObject(hdc, hbmpOld);
200 ::DeleteDC(hdc);
201
202 wxBitmapRefData *refData = new wxBitmapRefData;
203 m_refData = refData;
204
205 refData->m_width = width;
206 refData->m_height = height;
207 refData->m_depth = wxDisplayDepth();
208
209 refData->m_hBitmap = (WXHBITMAP)hbitmap;
210
211 #if WXWIN_COMPATIBILITY_2
212 refData->m_ok = TRUE;
213 #endif // WXWIN_COMPATIBILITY_2
214
215 return TRUE;
216 #else // Win32
217 return CopyFromIconOrCursor(icon);
218 #endif // Win16/Win32
219 }
220
221 wxBitmap::~wxBitmap()
222 {
223 }
224
225 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
226 {
227 Init();
228
229 #ifndef __WXMICROWIN__
230 wxBitmapRefData *refData = new wxBitmapRefData;
231 m_refData = refData;
232
233 refData->m_width = width;
234 refData->m_height = height;
235 refData->m_depth = depth;
236 refData->m_numColors = 0;
237 refData->m_selectedInto = NULL;
238
239 char *data;
240 if ( depth == 1 )
241 {
242 // we assume that it is in XBM format which is not quite the same as
243 // the format CreateBitmap() wants because the order of bytes in the
244 // line is inversed!
245 const size_t bytesPerLine = (width + 7) / 8;
246 const size_t padding = bytesPerLine % 2;
247 const size_t len = height * ( padding + bytesPerLine );
248 data = (char *)malloc(len);
249 const char *src = bits;
250 char *dst = data;
251
252 for ( int rows = 0; rows < height; rows++ )
253 {
254 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
255 {
256 unsigned char val = *src++;
257 unsigned char reversed = 0;
258
259 for ( int bits = 0; bits < 8; bits++)
260 {
261 reversed <<= 1;
262 reversed |= (val & 0x01);
263 val >>= 1;
264 }
265 *dst++ = reversed;
266 }
267
268 if ( padding )
269 *dst++ = 0;
270 }
271 }
272 else
273 {
274 // bits should already be in Windows standard format
275 data = (char *)bits; // const_cast is harmless
276 }
277
278 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
279 if ( !hbmp )
280 {
281 wxLogLastError(wxT("CreateBitmap"));
282 }
283
284 if ( data != bits )
285 {
286 free(data);
287 }
288
289 SetHBITMAP((WXHBITMAP)hbmp);
290 #endif
291 }
292
293 // Create from XPM data
294 bool wxBitmap::CreateFromXpm(const char **data)
295 {
296 #if wxUSE_IMAGE && wxUSE_XPM
297 Init();
298
299 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
300
301 wxXPMDecoder decoder;
302 wxImage img = decoder.ReadData(data);
303 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
304
305 *this = wxBitmap(img);
306 return TRUE;
307 #else
308 return FALSE;
309 #endif
310 }
311
312 wxBitmap::wxBitmap(int w, int h, int d)
313 {
314 Init();
315
316 (void)Create(w, h, d);
317 }
318
319 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
320 {
321 Init();
322
323 (void)Create(data, type, width, height, depth);
324 }
325
326 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
327 {
328 Init();
329
330 LoadFile(filename, (int)type);
331 }
332
333 bool wxBitmap::Create(int w, int h, int d)
334 {
335 UnRef();
336
337 m_refData = new wxBitmapRefData;
338
339 GetBitmapData()->m_width = w;
340 GetBitmapData()->m_height = h;
341 GetBitmapData()->m_depth = d;
342
343 HBITMAP hbmp;
344 #ifndef __WXMICROWIN__
345 if ( d > 0 )
346 {
347 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
348 if ( !hbmp )
349 {
350 wxLogLastError(wxT("CreateBitmap"));
351 }
352 }
353 else
354 #endif
355 {
356 ScreenHDC dc;
357 hbmp = ::CreateCompatibleBitmap(dc, w, h);
358 if ( !hbmp )
359 {
360 wxLogLastError(wxT("CreateCompatibleBitmap"));
361 }
362
363 GetBitmapData()->m_depth = wxDisplayDepth();
364 }
365
366 SetHBITMAP((WXHBITMAP)hbmp);
367
368 #if WXWIN_COMPATIBILITY_2
369 GetBitmapData()->m_ok = hbmp != 0;
370 #endif // WXWIN_COMPATIBILITY_2
371 return Ok();
372 }
373
374 // ----------------------------------------------------------------------------
375 // wxImage to/from conversions
376 // ----------------------------------------------------------------------------
377
378 #if wxUSE_IMAGE
379
380 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
381 {
382 #ifdef __WXMICROWIN__
383
384 // Initial attempt at a simple-minded implementation.
385 // The bitmap will always be created at the screen depth,
386 // so the 'depth' argument is ignored.
387 // TODO: transparency (create a mask image)
388
389 HDC hScreenDC = ::GetDC(NULL);
390 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
391
392 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
393 if (hBitmap == NULL)
394 {
395 ::ReleaseDC(NULL, hScreenDC);
396 return FALSE;
397 }
398 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
399 ::ReleaseDC(NULL, hScreenDC);
400
401 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
402
403 int i, j;
404 for (i = 0; i < image.GetWidth(); i++)
405 {
406 for (j = 0; j < image.GetHeight(); j++)
407 {
408 unsigned char red = image.GetRed(i, j);
409 unsigned char green = image.GetGreen(i, j);
410 unsigned char blue = image.GetBlue(i, j);
411
412 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
413 }
414 }
415
416 ::SelectObject(hMemDC, hOldBitmap);
417 ::DeleteDC(hMemDC);
418
419 m_refData = new wxBitmapRefData();
420
421 SetWidth(image.GetWidth());
422 SetHeight(image.GetHeight());
423 SetDepth(screenDepth);
424 SetHBITMAP( (WXHBITMAP) hBitmap );
425
426 #if wxUSE_PALETTE
427 // Copy the palette from the source image
428 SetPalette(image.GetPalette());
429 #endif // wxUSE_PALETTE
430
431 return TRUE;
432
433 #else
434 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
435
436 m_refData = new wxBitmapRefData();
437
438 // sizeLimit is the MS upper limit for the DIB size
439 #ifdef WIN32
440 int sizeLimit = 1024*768*3;
441 #else
442 int sizeLimit = 0x7fff ;
443 #endif
444
445 // width and height of the device-dependent bitmap
446 int width = image.GetWidth();
447 int bmpHeight = image.GetHeight();
448
449 // calc the number of bytes per scanline and padding
450 int bytePerLine = width*3;
451 int sizeDWORD = sizeof( DWORD );
452 int lineBoundary = bytePerLine % sizeDWORD;
453 int padding = 0;
454 if( lineBoundary > 0 )
455 {
456 padding = sizeDWORD - lineBoundary;
457 bytePerLine += padding;
458 }
459 // calc the number of DIBs and heights of DIBs
460 int numDIB = 1;
461 int hRemain = 0;
462 int height = sizeLimit/bytePerLine;
463 if( height >= bmpHeight )
464 height = bmpHeight;
465 else
466 {
467 numDIB = bmpHeight / height;
468 hRemain = bmpHeight % height;
469 if( hRemain >0 ) numDIB++;
470 }
471
472 // set bitmap parameters
473 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
474 SetWidth( width );
475 SetHeight( bmpHeight );
476 if (depth == -1) depth = wxDisplayDepth();
477 SetDepth( depth );
478
479 #if wxUSE_PALETTE
480 // Copy the palette from the source image
481 SetPalette(image.GetPalette());
482 #endif // wxUSE_PALETTE
483
484 // create a DIB header
485 int headersize = sizeof(BITMAPINFOHEADER);
486 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
487 wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
488 // Fill in the DIB header
489 lpDIBh->bmiHeader.biSize = headersize;
490 lpDIBh->bmiHeader.biWidth = (DWORD)width;
491 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
492 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
493 // the general formula for biSizeImage:
494 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
495 lpDIBh->bmiHeader.biPlanes = 1;
496 lpDIBh->bmiHeader.biBitCount = 24;
497 lpDIBh->bmiHeader.biCompression = BI_RGB;
498 lpDIBh->bmiHeader.biClrUsed = 0;
499 // These seem not really needed for our purpose here.
500 lpDIBh->bmiHeader.biClrImportant = 0;
501 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
502 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
503 // memory for DIB data
504 unsigned char *lpBits;
505 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
506 if( !lpBits )
507 {
508 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
509 free( lpDIBh );
510 return FALSE;
511 }
512
513 // create and set the device-dependent bitmap
514 HDC hdc = ::GetDC(NULL);
515 HDC memdc = ::CreateCompatibleDC( hdc );
516 HBITMAP hbitmap;
517 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
518 ::SelectObject( memdc, hbitmap);
519
520 #if wxUSE_PALETTE
521 HPALETTE hOldPalette = 0;
522 if (image.GetPalette().Ok())
523 {
524 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
525 ::RealizePalette(memdc);
526 }
527 #endif // wxUSE_PALETTE
528
529 // copy image data into DIB data and then into DDB (in a loop)
530 unsigned char *data = image.GetData();
531 int i, j, n;
532 int origin = 0;
533 unsigned char *ptdata = data;
534 unsigned char *ptbits;
535
536 for( n=0; n<numDIB; n++ )
537 {
538 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
539 {
540 // redefine height and size of the (possibly) last smaller DIB
541 // memory is not reallocated
542 height = hRemain;
543 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
544 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
545 }
546 ptbits = lpBits;
547
548 for( j=0; j<height; j++ )
549 {
550 for( i=0; i<width; i++ )
551 {
552 *(ptbits++) = *(ptdata+2);
553 *(ptbits++) = *(ptdata+1);
554 *(ptbits++) = *(ptdata );
555 ptdata += 3;
556 }
557 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
558 }
559 ::StretchDIBits( memdc, 0, origin, width, height,\
560 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
561 origin += height;
562 // if numDIB = 1, lines below can also be used
563 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
564 // The above line is equivalent to the following two lines.
565 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
566 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
567 // or the following lines
568 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
569 // HDC memdc = ::CreateCompatibleDC( hdc );
570 // ::SelectObject( memdc, hbitmap);
571 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
572 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
573 // ::SelectObject( memdc, 0 );
574 // ::DeleteDC( memdc );
575 }
576 SetHBITMAP( (WXHBITMAP) hbitmap );
577
578 #if wxUSE_PALETTE
579 if (hOldPalette)
580 SelectPalette(memdc, hOldPalette, FALSE);
581 #endif // wxUSE_PALETTE
582
583 // similarly, created an mono-bitmap for the possible mask
584 if( image.HasMask() )
585 {
586 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
587 HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
588 if( numDIB == 1 ) height = bmpHeight;
589 else height = sizeLimit/bytePerLine;
590 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
591 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
592 origin = 0;
593 unsigned char r = image.GetMaskRed();
594 unsigned char g = image.GetMaskGreen();
595 unsigned char b = image.GetMaskBlue();
596 unsigned char zero = 0, one = 255;
597 ptdata = data;
598 for( n=0; n<numDIB; n++ )
599 {
600 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
601 {
602 // redefine height and size of the (possibly) last smaller DIB
603 // memory is not reallocated
604 height = hRemain;
605 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
606 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
607 }
608 ptbits = lpBits;
609 for( int j=0; j<height; j++ )
610 {
611 for(i=0; i<width; i++ )
612 {
613 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
614 unsigned char cr = (*(ptdata++)) ;
615 unsigned char cg = (*(ptdata++)) ;
616 unsigned char cb = (*(ptdata++)) ;
617
618 if( ( cr !=r) || (cg!=g) || (cb!=b) )
619 {
620 *(ptbits++) = one;
621 *(ptbits++) = one;
622 *(ptbits++) = one;
623 }
624 else
625 {
626 *(ptbits++) = zero;
627 *(ptbits++) = zero;
628 *(ptbits++) = zero;
629 }
630 }
631 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
632 }
633 ::StretchDIBits( memdc, 0, origin, width, height,\
634 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
635 origin += height;
636 }
637 // create a wxMask object
638 wxMask *mask = new wxMask();
639 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
640 SetMask( mask );
641 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
642 /* The following can also be used but is slow to run
643 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
644 wxMask *mask = new wxMask( *this, colour );
645 SetMask( mask );
646 */
647
648 ::SelectObject( memdc, hbmpOld );
649 }
650
651 // free allocated resources
652 ::DeleteDC( memdc );
653 ::ReleaseDC(NULL, hdc);
654 free(lpDIBh);
655 free(lpBits);
656
657 #if WXWIN_COMPATIBILITY_2
658 // check the wxBitmap object
659 GetBitmapData()->SetOk();
660 #endif // WXWIN_COMPATIBILITY_2
661
662 return TRUE;
663 #endif
664 }
665
666 wxImage wxBitmap::ConvertToImage() const
667 {
668 #ifdef __WXMICROWIN__
669 // Initial attempt at a simple-minded implementation.
670 // The bitmap will always be created at the screen depth,
671 // so the 'depth' argument is ignored.
672 // TODO: transparency (create a mask image)
673
674 if (!Ok())
675 {
676 wxFAIL_MSG( wxT("bitmap is invalid") );
677 return wxNullImage;
678 }
679
680 wxImage image;
681
682 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
683
684 // create an wxImage object
685 int width = GetWidth();
686 int height = GetHeight();
687 image.Create( width, height );
688 unsigned char *data = image.GetData();
689 if( !data )
690 {
691 wxFAIL_MSG( wxT("could not allocate data for image") );
692 return wxNullImage;
693 }
694
695 HDC hScreenDC = ::GetDC(NULL);
696
697 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
698 ::ReleaseDC(NULL, hScreenDC);
699
700 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
701
702 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
703
704 int i, j;
705 for (i = 0; i < GetWidth(); i++)
706 {
707 for (j = 0; j < GetHeight(); j++)
708 {
709 COLORREF color = ::GetPixel(hMemDC, i, j);
710 unsigned char red = GetRValue(color);
711 unsigned char green = GetGValue(color);
712 unsigned char blue = GetBValue(color);
713
714 image.SetRGB(i, j, red, green, blue);
715 }
716 }
717
718 ::SelectObject(hMemDC, hOldBitmap);
719 ::DeleteDC(hMemDC);
720
721 #if wxUSE_PALETTE
722 // Copy the palette from the source image
723 if (GetPalette())
724 image.SetPalette(* GetPalette());
725 #endif // wxUSE_PALETTE
726
727 return image;
728
729 #else // __MICROWIN__
730
731 wxImage image;
732
733 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
734
735 // create an wxImage object
736 int width = GetWidth();
737 int height = GetHeight();
738 image.Create( width, height );
739 unsigned char *data = image.GetData();
740 if( !data )
741 {
742 wxFAIL_MSG( wxT("could not allocate data for image") );
743 return wxNullImage;
744 }
745
746 // calc the number of bytes per scanline and padding in the DIB
747 int bytePerLine = width*3;
748 int sizeDWORD = sizeof( DWORD );
749 int lineBoundary = bytePerLine % sizeDWORD;
750 int padding = 0;
751 if( lineBoundary > 0 )
752 {
753 padding = sizeDWORD - lineBoundary;
754 bytePerLine += padding;
755 }
756
757 // create a DIB header
758 int headersize = sizeof(BITMAPINFOHEADER);
759 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
760 if( !lpDIBh )
761 {
762 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
763 free( data );
764 return wxNullImage;
765 }
766 // Fill in the DIB header
767 lpDIBh->bmiHeader.biSize = headersize;
768 lpDIBh->bmiHeader.biWidth = width;
769 lpDIBh->bmiHeader.biHeight = -height;
770 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
771 lpDIBh->bmiHeader.biPlanes = 1;
772 lpDIBh->bmiHeader.biBitCount = 24;
773 lpDIBh->bmiHeader.biCompression = BI_RGB;
774 lpDIBh->bmiHeader.biClrUsed = 0;
775 // These seem not really needed for our purpose here.
776 lpDIBh->bmiHeader.biClrImportant = 0;
777 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
778 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
779 // memory for DIB data
780 unsigned char *lpBits;
781 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
782 if( !lpBits )
783 {
784 wxFAIL_MSG( wxT("could not allocate data for DIB") );
785 free( data );
786 free( lpDIBh );
787 return wxNullImage;
788 }
789
790 // copy data from the device-dependent bitmap to the DIB
791 HDC hdc = ::GetDC(NULL);
792 HBITMAP hbitmap;
793 hbitmap = (HBITMAP) GetHBITMAP();
794 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
795
796 // copy DIB data into the wxImage object
797 int i, j;
798 unsigned char *ptdata = data;
799 unsigned char *ptbits = lpBits;
800 for( i=0; i<height; i++ )
801 {
802 for( j=0; j<width; j++ )
803 {
804 *(ptdata++) = *(ptbits+2);
805 *(ptdata++) = *(ptbits+1);
806 *(ptdata++) = *(ptbits );
807 ptbits += 3;
808 }
809 ptbits += padding;
810 }
811
812 // similarly, set data according to the possible mask bitmap
813 if( GetMask() && GetMask()->GetMaskBitmap() )
814 {
815 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
816 // memory DC created, color set, data copied, and memory DC deleted
817 HDC memdc = ::CreateCompatibleDC( hdc );
818 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
819 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
820 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
821 ::DeleteDC( memdc );
822 // background color set to RGB(16,16,16) in consistent with wxGTK
823 unsigned char r=16, g=16, b=16;
824 ptdata = data;
825 ptbits = lpBits;
826 for( i=0; i<height; i++ )
827 {
828 for( j=0; j<width; j++ )
829 {
830 if( *ptbits != 0 )
831 ptdata += 3;
832 else
833 {
834 *(ptdata++) = r;
835 *(ptdata++) = g;
836 *(ptdata++) = b;
837 }
838 ptbits += 3;
839 }
840 ptbits += padding;
841 }
842 image.SetMaskColour( r, g, b );
843 image.SetMask( TRUE );
844 }
845 else
846 {
847 image.SetMask( FALSE );
848 }
849 // free allocated resources
850 ::ReleaseDC(NULL, hdc);
851 free(lpDIBh);
852 free(lpBits);
853
854 return image;
855 #endif
856 }
857
858 #endif // wxUSE_IMAGE
859
860 bool wxBitmap::LoadFile(const wxString& filename, long type)
861 {
862 UnRef();
863
864 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
865
866 if ( handler )
867 {
868 m_refData = new wxBitmapRefData;
869
870 return handler->LoadFile(this, filename, type, -1, -1);
871 }
872 #if wxUSE_IMAGE
873 else
874 {
875 wxImage image;
876 if ( image.LoadFile( filename, type ) && image.Ok() )
877 {
878 *this = image.ConvertToBitmap();
879
880 return TRUE;
881 }
882 }
883 #endif // wxUSE_IMAGE
884
885 return FALSE;
886 }
887
888 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
889 {
890 UnRef();
891
892 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
893
894 if ( !handler )
895 {
896 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
897
898 return FALSE;
899 }
900
901 m_refData = new wxBitmapRefData;
902
903 return handler->Create(this, data, type, width, height, depth);
904 }
905
906 bool wxBitmap::SaveFile(const wxString& filename,
907 int type,
908 const wxPalette *palette)
909 {
910 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
911
912 if ( handler )
913 {
914 return handler->SaveFile(this, filename, type, palette);
915 }
916 #if wxUSE_IMAGE
917 else
918 {
919 // FIXME what about palette? shouldn't we use it?
920 wxImage image( *this );
921 if ( image.Ok() )
922 {
923 return image.SaveFile(filename, type);
924 }
925 }
926 #endif // wxUSE_IMAGE
927
928 return FALSE;
929 }
930
931 // ----------------------------------------------------------------------------
932 // sub bitmap extraction
933 // ----------------------------------------------------------------------------
934
935 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
936 {
937 #ifndef __WXMICROWIN__
938 wxCHECK_MSG( Ok() &&
939 (rect.x >= 0) && (rect.y >= 0) &&
940 (rect.x+rect.width <= GetWidth()) &&
941 (rect.y+rect.height <= GetHeight()),
942 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
943
944 wxBitmap ret( rect.width, rect.height, GetDepth() );
945 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
946
947 // copy bitmap data
948 HDC dcSrc = ::CreateCompatibleDC(NULL);
949 HDC dcDst = ::CreateCompatibleDC(NULL);
950 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
951 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
952 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
953
954 // copy mask if there is one
955 if (GetMask())
956 {
957 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
958
959 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
960 SelectObject(dcDst, (HBITMAP) hbmpMask);
961 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
962
963 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
964 ret.SetMask(mask);
965 }
966
967 SelectObject(dcDst, NULL);
968 SelectObject(dcSrc, NULL);
969 DeleteDC(dcDst);
970 DeleteDC(dcSrc);
971
972 return ret;
973 #else
974 return wxBitmap();
975 #endif
976 }
977
978 // ----------------------------------------------------------------------------
979 // wxBitmap accessors
980 // ----------------------------------------------------------------------------
981
982 void wxBitmap::SetQuality(int q)
983 {
984 EnsureHasData();
985
986 GetBitmapData()->m_quality = q;
987 }
988
989 #if WXWIN_COMPATIBILITY_2
990 void wxBitmap::SetOk(bool isOk)
991 {
992 EnsureHasData();
993
994 GetBitmapData()->m_ok = isOk;
995 }
996 #endif // WXWIN_COMPATIBILITY_2
997
998 #if wxUSE_PALETTE
999
1000 void wxBitmap::SetPalette(const wxPalette& palette)
1001 {
1002 EnsureHasData();
1003
1004 GetBitmapData()->m_bitmapPalette = palette;
1005 }
1006
1007 #endif // wxUSE_PALETTE
1008
1009 void wxBitmap::SetMask(wxMask *mask)
1010 {
1011 EnsureHasData();
1012
1013 GetBitmapData()->m_bitmapMask = mask;
1014 }
1015
1016 // Creates a bitmap that matches the device context, from
1017 // an arbitray bitmap. At present, the original bitmap must have an
1018 // associated palette. TODO: use a default palette if no palette exists.
1019 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1020 wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
1021 {
1022 #ifdef __WXMICROWIN__
1023 return wxBitmap();
1024 #else
1025 wxMemoryDC memDC;
1026 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
1027 HPALETTE hPal = (HPALETTE) NULL;
1028 LPBITMAPINFO lpDib;
1029 void *lpBits = (void*) NULL;
1030
1031 #if wxUSE_PALETTE
1032 if( GetPalette() && GetPalette()->Ok() )
1033 {
1034 tmpBitmap.SetPalette(*GetPalette());
1035 memDC.SelectObject(tmpBitmap);
1036 memDC.SetPalette(*GetPalette());
1037 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
1038 }
1039 else
1040 {
1041 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1042 wxPalette palette;
1043 palette.SetHPALETTE( (WXHPALETTE)hPal );
1044 tmpBitmap.SetPalette( palette );
1045 memDC.SelectObject(tmpBitmap);
1046 memDC.SetPalette( palette );
1047 }
1048 #else // !wxUSE_PALETTE
1049 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1050 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1051
1052 // set the height negative because in a DIB the order of the lines is
1053 // reversed
1054 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
1055 {
1056 return wxNullBitmap;
1057 }
1058
1059 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
1060
1061 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
1062
1063 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
1064 GetWidth(), GetHeight(),
1065 0, 0, 0, GetHeight(),
1066 lpBits, lpDib, DIB_RGB_COLORS);
1067
1068 free(lpBits);
1069
1070 wxFreeDIB(lpDib);
1071
1072 return tmpBitmap;
1073 #endif
1074 }
1075
1076 // ----------------------------------------------------------------------------
1077 // wxMask
1078 // ----------------------------------------------------------------------------
1079
1080 wxMask::wxMask()
1081 {
1082 m_maskBitmap = 0;
1083 }
1084
1085 // Construct a mask from a bitmap and a colour indicating
1086 // the transparent area
1087 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1088 {
1089 m_maskBitmap = 0;
1090 Create(bitmap, colour);
1091 }
1092
1093 // Construct a mask from a bitmap and a palette index indicating
1094 // the transparent area
1095 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1096 {
1097 m_maskBitmap = 0;
1098 Create(bitmap, paletteIndex);
1099 }
1100
1101 // Construct a mask from a mono bitmap (copies the bitmap).
1102 wxMask::wxMask(const wxBitmap& bitmap)
1103 {
1104 m_maskBitmap = 0;
1105 Create(bitmap);
1106 }
1107
1108 wxMask::~wxMask()
1109 {
1110 if ( m_maskBitmap )
1111 ::DeleteObject((HBITMAP) m_maskBitmap);
1112 }
1113
1114 // Create a mask from a mono bitmap (copies the bitmap).
1115 bool wxMask::Create(const wxBitmap& bitmap)
1116 {
1117 #ifndef __WXMICROWIN__
1118 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1119 _T("can't create mask from invalid or not monochrome bitmap") );
1120
1121 if ( m_maskBitmap )
1122 {
1123 ::DeleteObject((HBITMAP) m_maskBitmap);
1124 m_maskBitmap = 0;
1125 }
1126
1127 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1128 bitmap.GetWidth(),
1129 bitmap.GetHeight(),
1130 1, 1, 0
1131 );
1132 HDC srcDC = CreateCompatibleDC(0);
1133 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1134 HDC destDC = CreateCompatibleDC(0);
1135 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1136 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1137 SelectObject(srcDC, 0);
1138 DeleteDC(srcDC);
1139 SelectObject(destDC, 0);
1140 DeleteDC(destDC);
1141 return TRUE;
1142 #else
1143 return FALSE;
1144 #endif
1145 }
1146
1147 // Create a mask from a bitmap and a palette index indicating
1148 // the transparent area
1149 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1150 {
1151 if ( m_maskBitmap )
1152 {
1153 ::DeleteObject((HBITMAP) m_maskBitmap);
1154 m_maskBitmap = 0;
1155 }
1156
1157 #if wxUSE_PALETTE
1158 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1159 {
1160 unsigned char red, green, blue;
1161 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1162 {
1163 wxColour transparentColour(red, green, blue);
1164 return Create(bitmap, transparentColour);
1165 }
1166 }
1167 #endif // wxUSE_PALETTE
1168
1169 return FALSE;
1170 }
1171
1172 // Create a mask from a bitmap and a colour indicating
1173 // the transparent area
1174 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1175 {
1176 #ifndef __WXMICROWIN__
1177 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1178
1179 if ( m_maskBitmap )
1180 {
1181 ::DeleteObject((HBITMAP) m_maskBitmap);
1182 m_maskBitmap = 0;
1183 }
1184
1185 int width = bitmap.GetWidth(),
1186 height = bitmap.GetHeight();
1187
1188 // scan the bitmap for the transparent colour and set the corresponding
1189 // pixels in the mask to BLACK and the rest to WHITE
1190 COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue());
1191 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1192
1193 HDC srcDC = ::CreateCompatibleDC(NULL);
1194 HDC destDC = ::CreateCompatibleDC(NULL);
1195 if ( !srcDC || !destDC )
1196 {
1197 wxLogLastError(wxT("CreateCompatibleDC"));
1198 }
1199
1200 bool ok = TRUE;
1201
1202 // SelectObject() will fail
1203 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1204 _T("bitmap can't be selected in another DC") );
1205
1206 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1207 if ( !hbmpSrcOld )
1208 {
1209 wxLogLastError(wxT("SelectObject"));
1210
1211 ok = FALSE;
1212 }
1213
1214 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1215 if ( !hbmpDstOld )
1216 {
1217 wxLogLastError(wxT("SelectObject"));
1218
1219 ok = FALSE;
1220 }
1221
1222 // this is not very efficient, but I can't think of a better way of doing
1223 // it
1224 for ( int w = 0; ok && (w < width); w++ )
1225 {
1226 for ( int h = 0; ok && (h < height); h++ )
1227 {
1228 COLORREF col = GetPixel(srcDC, w, h);
1229 if ( col == CLR_INVALID )
1230 {
1231 wxLogLastError(wxT("GetPixel"));
1232
1233 // doesn't make sense to continue
1234 ok = FALSE;
1235
1236 break;
1237 }
1238
1239 if ( col == maskColour )
1240 {
1241 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
1242 }
1243 else
1244 {
1245 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
1246 }
1247 }
1248 }
1249
1250 ::SelectObject(srcDC, hbmpSrcOld);
1251 ::DeleteDC(srcDC);
1252 ::SelectObject(destDC, hbmpDstOld);
1253 ::DeleteDC(destDC);
1254
1255 return ok;
1256 #else
1257 return FALSE;
1258 #endif
1259 }
1260
1261 // ----------------------------------------------------------------------------
1262 // wxBitmapHandler
1263 // ----------------------------------------------------------------------------
1264
1265 bool wxBitmapHandler::Create(wxGDIImage *image,
1266 void *data,
1267 long flags,
1268 int width, int height, int depth)
1269 {
1270 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1271
1272 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1273 }
1274
1275 bool wxBitmapHandler::Load(wxGDIImage *image,
1276 const wxString& name,
1277 long flags,
1278 int width, int height)
1279 {
1280 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1281
1282 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1283 }
1284
1285 bool wxBitmapHandler::Save(wxGDIImage *image,
1286 const wxString& name,
1287 int type)
1288 {
1289 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1290
1291 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1292 }
1293
1294 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1295 void *WXUNUSED(data),
1296 long WXUNUSED(type),
1297 int WXUNUSED(width),
1298 int WXUNUSED(height),
1299 int WXUNUSED(depth))
1300 {
1301 return FALSE;
1302 }
1303
1304 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1305 const wxString& WXUNUSED(name),
1306 long WXUNUSED(type),
1307 int WXUNUSED(desiredWidth),
1308 int WXUNUSED(desiredHeight))
1309 {
1310 return FALSE;
1311 }
1312
1313 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1314 const wxString& WXUNUSED(name),
1315 int WXUNUSED(type),
1316 const wxPalette *WXUNUSED(palette))
1317 {
1318 return FALSE;
1319 }
1320
1321 // ----------------------------------------------------------------------------
1322 // DIB functions
1323 // ----------------------------------------------------------------------------
1324
1325 #ifndef __WXMICROWIN__
1326 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1327 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1328 {
1329 unsigned long i, headerSize;
1330 LPBITMAPINFO lpDIBheader = NULL;
1331 LPPALETTEENTRY lpPe = NULL;
1332
1333
1334 // Allocate space for a DIB header
1335 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1336 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1337 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1338
1339 GetPaletteEntries(hPal, 0, 256, lpPe);
1340
1341 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1342
1343 // Fill in the static parts of the DIB header
1344 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1345 lpDIBheader->bmiHeader.biWidth = xSize;
1346 lpDIBheader->bmiHeader.biHeight = ySize;
1347 lpDIBheader->bmiHeader.biPlanes = 1;
1348
1349 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1350 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1351 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1352 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1353 lpDIBheader->bmiHeader.biClrUsed = 256;
1354
1355
1356 // Initialize the DIB palette
1357 for (i = 0; i < 256; i++) {
1358 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1359 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1360 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1361 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1362 }
1363
1364 *lpDIBHeader = lpDIBheader;
1365
1366 return TRUE;
1367 }
1368
1369 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1370 {
1371 free(lpDIBHeader);
1372 }
1373 #endif
1374
1375 // ----------------------------------------------------------------------------
1376 // other helper functions
1377 // ----------------------------------------------------------------------------
1378
1379 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1380 {
1381 #ifndef __WXMICROWIN__
1382 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1383
1384 // get width/height from the bitmap if not given
1385 if ( !w || !h )
1386 {
1387 BITMAP bm;
1388 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1389 w = bm.bmWidth;
1390 h = bm.bmHeight;
1391 }
1392
1393 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1394 HDC hdcDst = ::CreateCompatibleDC(NULL);
1395 if ( !hdcSrc || !hdcDst )
1396 {
1397 wxLogLastError(wxT("CreateCompatibleDC"));
1398 }
1399
1400 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1401 if ( !hbmpInvMask )
1402 {
1403 wxLogLastError(wxT("CreateBitmap"));
1404 }
1405
1406 ::SelectObject(hdcSrc, hbmpMask);
1407 ::SelectObject(hdcDst, hbmpInvMask);
1408 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1409 hdcSrc, 0, 0,
1410 NOTSRCCOPY) )
1411 {
1412 wxLogLastError(wxT("BitBlt"));
1413 }
1414
1415 ::DeleteDC(hdcSrc);
1416 ::DeleteDC(hdcDst);
1417
1418 return hbmpInvMask;
1419 #else
1420 return 0;
1421 #endif
1422 }