don't return garbage from GetRawBitmap
[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
9 // Licence: wxWindows licence
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 #include "wx/rawbmp.h"
54
55 // missing from mingw32 header
56 #ifndef CLR_INVALID
57 #define CLR_INVALID ((COLORREF)-1)
58 #endif // no CLR_INVALID
59
60 // ----------------------------------------------------------------------------
61 // Bitmap data
62 // ----------------------------------------------------------------------------
63
64 class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
65 {
66 public:
67 wxBitmapRefData();
68 virtual ~wxBitmapRefData() { Free(); }
69
70 virtual void Free();
71
72 // set the mask object to use as the mask, we take ownership of it
73 void SetMask(wxMask *mask)
74 {
75 delete m_bitmapMask;
76 m_bitmapMask = mask;
77 }
78
79 // set the HBITMAP to use as the mask
80 void SetMask(HBITMAP hbmpMask)
81 {
82 SetMask(new wxMask((WXHBITMAP)hbmpMask));
83 }
84
85 // return the mask
86 wxMask *GetMask() const { return m_bitmapMask; }
87
88 public:
89 #if wxUSE_PALETTE
90 wxPalette m_bitmapPalette;
91 #endif // wxUSE_PALETTE
92
93 // MSW-specific
94 // ------------
95
96 #ifdef __WXDEBUG__
97 // this field is solely for error checking: we detect selecting a bitmap
98 // into more than one DC at once or deleting a bitmap still selected into a
99 // DC (both are serious programming errors under Windows)
100 wxDC *m_selectedInto;
101 #endif // __WXDEBUG__
102
103 // true if we have alpha transparency info and can be drawn using
104 // AlphaBlend()
105 bool m_hasAlpha;
106
107 private:
108 // optional mask for transparent drawing
109 wxMask *m_bitmapMask;
110
111 DECLARE_NO_COPY_CLASS(wxBitmapRefData)
112 };
113
114 // ----------------------------------------------------------------------------
115 // macros
116 // ----------------------------------------------------------------------------
117
118 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
119 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
120
121 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
122
123 // ============================================================================
124 // implementation
125 // ============================================================================
126
127 // ----------------------------------------------------------------------------
128 // helper functions
129 // ----------------------------------------------------------------------------
130
131 // decide whether we should create a DIB or a DDB for the given parameters
132 static bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
133 {
134 // here is the logic:
135 //
136 // (a) if hdc is specified, the caller explicitly wants DDB
137 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
138 // less DIBs anyhow)
139 // (c) finally, create DIBs under Win9x even if the depth hasn't been
140 // explicitly specified but the current display depth is 24 or more
141 // and the image is "big", i.e. > 16Mb which is the theoretical limit
142 // for DDBs under Win9x
143 //
144 // consequences (all of which seem to make sense):
145 //
146 // (i) by default, DDBs are created (depth == -1 usually)
147 // (ii) DIBs can be created by explicitly specifying the depth
148 // (iii) using a DC always forces creating a DDB
149 return !hdc &&
150 (d >= 24 ||
151 (d == -1 &&
152 wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
153 }
154
155 // ----------------------------------------------------------------------------
156 // wxBitmapRefData
157 // ----------------------------------------------------------------------------
158
159 wxBitmapRefData::wxBitmapRefData()
160 {
161 #ifdef __WXDEBUG__
162 m_selectedInto = NULL;
163 #endif
164 m_bitmapMask = NULL;
165 m_hBitmap = (WXHBITMAP) NULL;
166 m_hasAlpha = FALSE;
167 }
168
169 void wxBitmapRefData::Free()
170 {
171 wxASSERT_MSG( !m_selectedInto,
172 wxT("deleting bitmap still selected into wxMemoryDC") );
173
174 if ( m_hBitmap)
175 {
176 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
177 {
178 wxLogLastError(wxT("DeleteObject(hbitmap)"));
179 }
180 }
181
182 delete m_bitmapMask;
183 m_bitmapMask = NULL;
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxBitmap creation
188 // ----------------------------------------------------------------------------
189
190 // this function should be called from all wxBitmap ctors
191 void wxBitmap::Init()
192 {
193 // m_refData = NULL; done in the base class ctor
194 }
195
196 wxGDIImageRefData *wxBitmap::CreateData() const
197 {
198 return new wxBitmapRefData;
199 }
200
201 #ifdef __WIN32__
202
203 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
204 {
205 #ifndef __WXMICROWIN__
206 // it may be either HICON or HCURSOR
207 HICON hicon = (HICON)icon.GetHandle();
208
209 ICONINFO iconInfo;
210 if ( !::GetIconInfo(hicon, &iconInfo) )
211 {
212 wxLogLastError(wxT("GetIconInfo"));
213
214 return FALSE;
215 }
216
217 wxBitmapRefData *refData = new wxBitmapRefData;
218 m_refData = refData;
219
220 int w = icon.GetWidth(),
221 h = icon.GetHeight();
222
223 refData->m_width = w;
224 refData->m_height = h;
225 refData->m_depth = wxDisplayDepth();
226
227 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
228
229 // the mask returned by GetIconInfo() is inversed compared to the usual
230 // wxWin convention
231 refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
232
233
234 // delete the old one now as we don't need it any more
235 ::DeleteObject(iconInfo.hbmMask);
236
237 #if WXWIN_COMPATIBILITY_2
238 refData->m_ok = TRUE;
239 #endif // WXWIN_COMPATIBILITY_2
240
241 return TRUE;
242 #else
243 return FALSE;
244 #endif
245 }
246
247 #endif // Win32
248
249 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
250 {
251 UnRef();
252
253 if ( !cursor.Ok() )
254 return FALSE;
255
256 #ifdef __WIN16__
257 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
258
259 return FALSE;
260 #else
261 return CopyFromIconOrCursor(cursor);
262 #endif // Win16
263 }
264
265 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
266 {
267 UnRef();
268
269 if ( !icon.Ok() )
270 return FALSE;
271
272 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
273 // to create a bitmap from icon there - but using this way we won't have
274 // the mask (FIXME)
275 #ifdef __WIN16__
276 int width = icon.GetWidth(),
277 height = icon.GetHeight();
278
279 // copy the icon to the bitmap
280 ScreenHDC hdcScreen;
281 HDC hdc = ::CreateCompatibleDC(hdcScreen);
282 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
283 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
284
285 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
286
287 ::SelectObject(hdc, hbmpOld);
288 ::DeleteDC(hdc);
289
290 wxBitmapRefData *refData = new wxBitmapRefData;
291 m_refData = refData;
292
293 refData->m_width = width;
294 refData->m_height = height;
295 refData->m_depth = wxDisplayDepth();
296
297 refData->m_hBitmap = (WXHBITMAP)hbitmap;
298
299 #if WXWIN_COMPATIBILITY_2
300 refData->m_ok = TRUE;
301 #endif // WXWIN_COMPATIBILITY_2
302
303 return TRUE;
304 #else // Win32
305 return CopyFromIconOrCursor(icon);
306 #endif // Win16/Win32
307 }
308
309 bool wxBitmap::CopyFromDIB(const wxDIB& dib)
310 {
311 wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
312
313 HBITMAP hbitmap = dib.CreateDDB();
314 if ( !hbitmap )
315 return FALSE;
316
317 UnRef();
318
319 wxBitmapRefData *refData = new wxBitmapRefData;
320 m_refData = refData;
321
322 refData->m_width = dib.GetWidth();
323 refData->m_height = dib.GetHeight();
324 refData->m_depth = dib.GetDepth();
325
326 refData->m_hBitmap = (WXHBITMAP)hbitmap;
327
328 #if wxUSE_PALETTE
329 wxPalette *palette = dib.CreatePalette();
330 if ( palette )
331 {
332 refData->m_bitmapPalette = *palette;
333 }
334
335 delete palette;
336 #endif // wxUSE_PALETTE
337
338 return TRUE;
339 }
340
341 wxBitmap::~wxBitmap()
342 {
343 }
344
345 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
346 {
347 Init();
348
349 #ifndef __WXMICROWIN__
350 wxBitmapRefData *refData = new wxBitmapRefData;
351 m_refData = refData;
352
353 refData->m_width = width;
354 refData->m_height = height;
355 refData->m_depth = depth;
356
357 char *data;
358 if ( depth == 1 )
359 {
360 // we assume that it is in XBM format which is not quite the same as
361 // the format CreateBitmap() wants because the order of bytes in the
362 // line is inversed!
363 const size_t bytesPerLine = (width + 7) / 8;
364 const size_t padding = bytesPerLine % 2;
365 const size_t len = height * ( padding + bytesPerLine );
366 data = (char *)malloc(len);
367 const char *src = bits;
368 char *dst = data;
369
370 for ( int rows = 0; rows < height; rows++ )
371 {
372 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
373 {
374 unsigned char val = *src++;
375 unsigned char reversed = 0;
376
377 for ( int bits = 0; bits < 8; bits++)
378 {
379 reversed <<= 1;
380 reversed |= (val & 0x01);
381 val >>= 1;
382 }
383 *dst++ = reversed;
384 }
385
386 if ( padding )
387 *dst++ = 0;
388 }
389 }
390 else
391 {
392 // bits should already be in Windows standard format
393 data = (char *)bits; // const_cast is harmless
394 }
395
396 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
397 if ( !hbmp )
398 {
399 wxLogLastError(wxT("CreateBitmap"));
400 }
401
402 if ( data != bits )
403 {
404 free(data);
405 }
406
407 SetHBITMAP((WXHBITMAP)hbmp);
408 #endif
409 }
410
411 // Create from XPM data
412 bool wxBitmap::CreateFromXpm(const char **data)
413 {
414 #if wxUSE_IMAGE && wxUSE_XPM
415 Init();
416
417 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
418
419 wxXPMDecoder decoder;
420 wxImage img = decoder.ReadData(data);
421 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
422
423 *this = wxBitmap(img);
424 return TRUE;
425 #else
426 return FALSE;
427 #endif
428 }
429
430 wxBitmap::wxBitmap(int w, int h, int d)
431 {
432 Init();
433
434 (void)Create(w, h, d);
435 }
436
437 wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
438 {
439 Init();
440
441 (void)Create(w, h, dc);
442 }
443
444 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
445 {
446 Init();
447
448 (void)Create(data, type, width, height, depth);
449 }
450
451 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
452 {
453 Init();
454
455 LoadFile(filename, (int)type);
456 }
457
458 bool wxBitmap::Create(int width, int height, int depth)
459 {
460 return DoCreate(width, height, depth, 0);
461 }
462
463 bool wxBitmap::Create(int width, int height, const wxDC& dc)
464 {
465 wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") );
466
467 return DoCreate(width, height, -1, dc.GetHDC());
468 }
469
470 bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
471 {
472 UnRef();
473
474 m_refData = new wxBitmapRefData;
475
476 GetBitmapData()->m_width = w;
477 GetBitmapData()->m_height = h;
478
479 HBITMAP hbmp;
480
481 if ( wxShouldCreateDIB(w, h, d, hdc) )
482 {
483 if ( d == -1 )
484 {
485 // create DIBs without alpha channel by default
486 d = 24;
487 }
488
489 wxDIB dib(w, h, d);
490 if ( !dib.IsOk() )
491 return FALSE;
492
493 // don't delete the DIB section in dib object dtor
494 hbmp = dib.Detach();
495
496 GetBitmapData()->m_depth = d;
497 }
498 else // create a DDB
499 {
500 if ( d == -1 )
501 d = wxDisplayDepth();
502
503 GetBitmapData()->m_depth = d;
504
505 #ifndef __WXMICROWIN__
506 if ( d > 0 )
507 {
508 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
509 if ( !hbmp )
510 {
511 wxLogLastError(wxT("CreateBitmap"));
512 }
513 }
514 else
515 #endif // !__WXMICROWIN__
516 {
517 ScreenHDC dc;
518 hbmp = ::CreateCompatibleBitmap(dc, w, h);
519 if ( !hbmp )
520 {
521 wxLogLastError(wxT("CreateCompatibleBitmap"));
522 }
523
524 GetBitmapData()->m_depth = wxDisplayDepth();
525 }
526 }
527
528 SetHBITMAP((WXHBITMAP)hbmp);
529
530 #if WXWIN_COMPATIBILITY_2
531 GetBitmapData()->m_ok = hbmp != 0;
532 #endif // WXWIN_COMPATIBILITY_2
533
534 return Ok();
535 }
536
537 #if wxUSE_IMAGE
538
539 // ----------------------------------------------------------------------------
540 // wxImage to/from conversions for Microwin
541 // ----------------------------------------------------------------------------
542
543 // Microwin versions are so different from normal ones that it really doesn't
544 // make sense to use #ifdefs inside the function bodies
545 #ifdef __WXMICROWIN__
546
547 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
548 {
549 // Set this to 1 to experiment with mask code,
550 // which currently doesn't work
551 #define USE_MASKS 0
552
553 m_refData = new wxBitmapRefData();
554
555 // Initial attempt at a simple-minded implementation.
556 // The bitmap will always be created at the screen depth,
557 // so the 'depth' argument is ignored.
558
559 HDC hScreenDC = ::GetDC(NULL);
560 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
561
562 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
563 HBITMAP hMaskBitmap = NULL;
564 HBITMAP hOldMaskBitmap = NULL;
565 HDC hMaskDC = NULL;
566 unsigned char maskR = 0;
567 unsigned char maskG = 0;
568 unsigned char maskB = 0;
569
570 // printf("Created bitmap %d\n", (int) hBitmap);
571 if (hBitmap == NULL)
572 {
573 ::ReleaseDC(NULL, hScreenDC);
574 return FALSE;
575 }
576 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
577
578 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
579 ::ReleaseDC(NULL, hScreenDC);
580
581 // created an mono-bitmap for the possible mask
582 bool hasMask = image.HasMask();
583
584 if ( hasMask )
585 {
586 #if USE_MASKS
587 // FIXME: we should be able to pass bpp = 1, but
588 // GdBlit can't handle a different depth
589 #if 0
590 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
591 #else
592 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
593 #endif
594 maskR = image.GetMaskRed();
595 maskG = image.GetMaskGreen();
596 maskB = image.GetMaskBlue();
597
598 if (!hMaskBitmap)
599 {
600 hasMask = FALSE;
601 }
602 else
603 {
604 hScreenDC = ::GetDC(NULL);
605 hMaskDC = ::CreateCompatibleDC(hScreenDC);
606 ::ReleaseDC(NULL, hScreenDC);
607
608 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
609 }
610 #else
611 hasMask = FALSE;
612 #endif
613 }
614
615 int i, j;
616 for (i = 0; i < image.GetWidth(); i++)
617 {
618 for (j = 0; j < image.GetHeight(); j++)
619 {
620 unsigned char red = image.GetRed(i, j);
621 unsigned char green = image.GetGreen(i, j);
622 unsigned char blue = image.GetBlue(i, j);
623
624 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
625
626 if (hasMask)
627 {
628 // scan the bitmap for the transparent colour and set the corresponding
629 // pixels in the mask to BLACK and the rest to WHITE
630 if (maskR == red && maskG == green && maskB == blue)
631 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
632 else
633 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
634 }
635 }
636 }
637
638 ::SelectObject(hMemDC, hOldBitmap);
639 ::DeleteDC(hMemDC);
640 if (hasMask)
641 {
642 ::SelectObject(hMaskDC, hOldMaskBitmap);
643 ::DeleteDC(hMaskDC);
644
645 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
646 }
647
648 SetWidth(image.GetWidth());
649 SetHeight(image.GetHeight());
650 SetDepth(screenDepth);
651 SetHBITMAP( (WXHBITMAP) hBitmap );
652
653 #if wxUSE_PALETTE
654 // Copy the palette from the source image
655 SetPalette(image.GetPalette());
656 #endif // wxUSE_PALETTE
657
658 #if WXWIN_COMPATIBILITY_2
659 // check the wxBitmap object
660 GetBitmapData()->SetOk();
661 #endif // WXWIN_COMPATIBILITY_2
662
663 return TRUE;
664 }
665
666 wxImage wxBitmap::ConvertToImage() const
667 {
668 // Initial attempt at a simple-minded implementation.
669 // The bitmap will always be created at the screen depth,
670 // so the 'depth' argument is ignored.
671 // TODO: transparency (create a mask image)
672
673 if (!Ok())
674 {
675 wxFAIL_MSG( wxT("bitmap is invalid") );
676 return wxNullImage;
677 }
678
679 wxImage image;
680
681 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
682
683 // create an wxImage object
684 int width = GetWidth();
685 int height = GetHeight();
686 image.Create( width, height );
687 unsigned char *data = image.GetData();
688 if( !data )
689 {
690 wxFAIL_MSG( wxT("could not allocate data for image") );
691 return wxNullImage;
692 }
693
694 HDC hScreenDC = ::GetDC(NULL);
695
696 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
697 ::ReleaseDC(NULL, hScreenDC);
698
699 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
700
701 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
702
703 int i, j;
704 for (i = 0; i < GetWidth(); i++)
705 {
706 for (j = 0; j < GetHeight(); j++)
707 {
708 COLORREF color = ::GetPixel(hMemDC, i, j);
709 unsigned char red = GetRValue(color);
710 unsigned char green = GetGValue(color);
711 unsigned char blue = GetBValue(color);
712
713 image.SetRGB(i, j, red, green, blue);
714 }
715 }
716
717 ::SelectObject(hMemDC, hOldBitmap);
718 ::DeleteDC(hMemDC);
719
720 #if wxUSE_PALETTE
721 // Copy the palette from the source image
722 if (GetPalette())
723 image.SetPalette(* GetPalette());
724 #endif // wxUSE_PALETTE
725
726 return image;
727 }
728
729 #endif // __WXMICROWIN__
730
731 // ----------------------------------------------------------------------------
732 // wxImage to/from conversions
733 // ----------------------------------------------------------------------------
734
735 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
736 {
737 return CreateFromImage(image, depth, 0);
738 }
739
740 bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
741 {
742 wxCHECK_MSG( dc.Ok(), FALSE,
743 _T("invalid HDC in wxBitmap::CreateFromImage()") );
744
745 return CreateFromImage(image, -1, dc.GetHDC());
746 }
747
748 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
749 {
750 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
751
752 UnRef();
753
754 // first convert the image to DIB
755 const int h = image.GetHeight();
756 const int w = image.GetWidth();
757
758 wxDIB dib(image);
759 if ( !dib.IsOk() )
760 return FALSE;
761
762
763 // store the bitmap parameters
764 wxBitmapRefData *refData = new wxBitmapRefData;
765 refData->m_width = w;
766 refData->m_height = h;
767 refData->m_hasAlpha = image.HasAlpha();
768
769 m_refData = refData;
770
771
772 // next either store DIB as is or create a DDB from it
773 HBITMAP hbitmap;
774
775 // are we going to use DIB?
776 if ( wxShouldCreateDIB(w, h, depth, hdc) )
777 {
778 // don't delete the DIB section in dib object dtor
779 hbitmap = dib.Detach();
780
781 refData->m_depth = dib.GetDepth();
782 }
783 else // we need to convert DIB to DDB
784 {
785 hbitmap = dib.CreateDDB((HDC)hdc);
786
787 refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
788 }
789
790 // validate this object
791 SetHBITMAP((WXHBITMAP)hbitmap);
792
793 #if WXWIN_COMPATIBILITY_2
794 m_refData->m_ok = TRUE;
795 #endif // WXWIN_COMPATIBILITY_2
796
797 // finally also set the mask if we have one
798 if ( image.HasMask() )
799 {
800 SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
801 image.GetMaskGreen(),
802 image.GetMaskBlue())));
803 }
804
805 return TRUE;
806 }
807
808 wxImage wxBitmap::ConvertToImage() const
809 {
810 wxImage image;
811
812 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
813
814 // create an wxImage object
815 int width = GetWidth();
816 int height = GetHeight();
817 image.Create( width, height );
818 unsigned char *data = image.GetData();
819 if( !data )
820 {
821 wxFAIL_MSG( wxT("could not allocate data for image") );
822 return wxNullImage;
823 }
824
825 // calc the number of bytes per scanline and padding in the DIB
826 int bytePerLine = width*3;
827 int sizeDWORD = sizeof( DWORD );
828 int lineBoundary = bytePerLine % sizeDWORD;
829 int padding = 0;
830 if( lineBoundary > 0 )
831 {
832 padding = sizeDWORD - lineBoundary;
833 bytePerLine += padding;
834 }
835
836 // create a DIB header
837 int headersize = sizeof(BITMAPINFOHEADER);
838 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
839 if( !lpDIBh )
840 {
841 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
842 free( data );
843 return wxNullImage;
844 }
845 // Fill in the DIB header
846 lpDIBh->bmiHeader.biSize = headersize;
847 lpDIBh->bmiHeader.biWidth = width;
848 lpDIBh->bmiHeader.biHeight = -height;
849 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
850 lpDIBh->bmiHeader.biPlanes = 1;
851 lpDIBh->bmiHeader.biBitCount = 24;
852 lpDIBh->bmiHeader.biCompression = BI_RGB;
853 lpDIBh->bmiHeader.biClrUsed = 0;
854 // These seem not really needed for our purpose here.
855 lpDIBh->bmiHeader.biClrImportant = 0;
856 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
857 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
858 // memory for DIB data
859 unsigned char *lpBits;
860 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
861 if( !lpBits )
862 {
863 wxFAIL_MSG( wxT("could not allocate data for DIB") );
864 free( data );
865 free( lpDIBh );
866 return wxNullImage;
867 }
868
869 // copy data from the device-dependent bitmap to the DIB
870 HDC hdc = ::GetDC(NULL);
871 HBITMAP hbitmap;
872 hbitmap = (HBITMAP) GetHBITMAP();
873 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
874
875 // copy DIB data into the wxImage object
876 int i, j;
877 unsigned char *ptdata = data;
878 unsigned char *ptbits = lpBits;
879 for( i=0; i<height; i++ )
880 {
881 for( j=0; j<width; j++ )
882 {
883 *(ptdata++) = *(ptbits+2);
884 *(ptdata++) = *(ptbits+1);
885 *(ptdata++) = *(ptbits );
886 ptbits += 3;
887 }
888 ptbits += padding;
889 }
890
891 // similarly, set data according to the possible mask bitmap
892 if( GetMask() && GetMask()->GetMaskBitmap() )
893 {
894 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
895 // memory DC created, color set, data copied, and memory DC deleted
896 HDC memdc = ::CreateCompatibleDC( hdc );
897 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
898 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
899 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
900 ::DeleteDC( memdc );
901 // background color set to RGB(16,16,16) in consistent with wxGTK
902 unsigned char r=16, g=16, b=16;
903 ptdata = data;
904 ptbits = lpBits;
905 for( i=0; i<height; i++ )
906 {
907 for( j=0; j<width; j++ )
908 {
909 if( *ptbits != 0 )
910 ptdata += 3;
911 else
912 {
913 *(ptdata++) = r;
914 *(ptdata++) = g;
915 *(ptdata++) = b;
916 }
917 ptbits += 3;
918 }
919 ptbits += padding;
920 }
921 image.SetMaskColour( r, g, b );
922 image.SetMask( TRUE );
923 }
924 else
925 {
926 image.SetMask( FALSE );
927 }
928 // free allocated resources
929 ::ReleaseDC(NULL, hdc);
930 free(lpDIBh);
931 free(lpBits);
932
933 return image;
934 }
935
936 #endif // wxUSE_IMAGE
937
938 // ----------------------------------------------------------------------------
939 // loading and saving bitmaps
940 // ----------------------------------------------------------------------------
941
942 bool wxBitmap::LoadFile(const wxString& filename, long type)
943 {
944 UnRef();
945
946 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
947
948 if ( handler )
949 {
950 m_refData = new wxBitmapRefData;
951
952 return handler->LoadFile(this, filename, type, -1, -1);
953 }
954 #if wxUSE_IMAGE
955 else
956 {
957 wxImage image;
958 if ( image.LoadFile( filename, type ) && image.Ok() )
959 {
960 *this = wxBitmap(image);
961
962 return TRUE;
963 }
964 }
965 #endif // wxUSE_IMAGE
966
967 return FALSE;
968 }
969
970 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
971 {
972 UnRef();
973
974 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
975
976 if ( !handler )
977 {
978 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
979
980 return FALSE;
981 }
982
983 m_refData = new wxBitmapRefData;
984
985 return handler->Create(this, data, type, width, height, depth);
986 }
987
988 bool wxBitmap::SaveFile(const wxString& filename,
989 int type,
990 const wxPalette *palette)
991 {
992 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
993
994 if ( handler )
995 {
996 return handler->SaveFile(this, filename, type, palette);
997 }
998 #if wxUSE_IMAGE
999 else
1000 {
1001 // FIXME what about palette? shouldn't we use it?
1002 wxImage image = ConvertToImage();
1003 if ( image.Ok() )
1004 {
1005 return image.SaveFile(filename, type);
1006 }
1007 }
1008 #endif // wxUSE_IMAGE
1009
1010 return FALSE;
1011 }
1012
1013 // ----------------------------------------------------------------------------
1014 // sub bitmap extraction
1015 // ----------------------------------------------------------------------------
1016
1017 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1018 {
1019 wxCHECK_MSG( Ok() &&
1020 (rect.x >= 0) && (rect.y >= 0) &&
1021 (rect.x+rect.width <= GetWidth()) &&
1022 (rect.y+rect.height <= GetHeight()),
1023 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1024
1025 wxBitmap ret( rect.width, rect.height );
1026 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1027
1028 #ifndef __WXMICROWIN__
1029 // TODO: copy alpha channel data if any
1030
1031 // copy bitmap data
1032 MemoryHDC dcSrc,
1033 dcDst;
1034
1035 {
1036 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
1037 selectDst(dcDst, GetHbitmapOf(ret));
1038
1039 if ( !selectSrc || !selectDst )
1040 {
1041 wxLogLastError(_T("SelectObjct(hBitmap)"));
1042 }
1043
1044 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1045 dcSrc, rect.x, rect.y, SRCCOPY) )
1046 {
1047 wxLogLastError(_T("BitBlt"));
1048 }
1049 }
1050
1051 // copy mask if there is one
1052 if ( GetMask() )
1053 {
1054 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1055
1056 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1057 selectDst(dcDst, hbmpMask);
1058
1059 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1060 dcSrc, rect.x, rect.y, SRCCOPY) )
1061 {
1062 wxLogLastError(_T("BitBlt"));
1063 }
1064
1065 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1066 ret.SetMask(mask);
1067 }
1068 #endif // !__WXMICROWIN__
1069
1070 return ret;
1071 }
1072
1073 // ----------------------------------------------------------------------------
1074 // wxBitmap accessors
1075 // ----------------------------------------------------------------------------
1076
1077 wxPalette* wxBitmap::GetPalette() const
1078 {
1079 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1080 : (wxPalette *) NULL;
1081 }
1082
1083 wxMask *wxBitmap::GetMask() const
1084 {
1085 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1086 }
1087
1088 #ifdef __WXDEBUG__
1089
1090 wxDC *wxBitmap::GetSelectedInto() const
1091 {
1092 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
1093 }
1094
1095 #endif
1096
1097 #if WXWIN_COMPATIBILITY_2_4
1098
1099 int wxBitmap::GetQuality() const
1100 {
1101 return 0;
1102 }
1103
1104 #endif // WXWIN_COMPATIBILITY_2_4
1105
1106 void wxBitmap::UseAlpha()
1107 {
1108 if ( GetBitmapData() )
1109 GetBitmapData()->m_hasAlpha = true;
1110 }
1111
1112 bool wxBitmap::HasAlpha() const
1113 {
1114 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1115 }
1116
1117 // ----------------------------------------------------------------------------
1118 // wxBitmap setters
1119 // ----------------------------------------------------------------------------
1120
1121 #ifdef __WXDEBUG__
1122
1123 void wxBitmap::SetSelectedInto(wxDC *dc)
1124 {
1125 if ( GetBitmapData() )
1126 GetBitmapData()->m_selectedInto = dc;
1127 }
1128
1129 #endif
1130
1131 #if wxUSE_PALETTE
1132
1133 void wxBitmap::SetPalette(const wxPalette& palette)
1134 {
1135 EnsureHasData();
1136
1137 GetBitmapData()->m_bitmapPalette = palette;
1138 }
1139
1140 #endif // wxUSE_PALETTE
1141
1142 void wxBitmap::SetMask(wxMask *mask)
1143 {
1144 EnsureHasData();
1145
1146 GetBitmapData()->SetMask(mask);
1147 }
1148
1149 #if WXWIN_COMPATIBILITY_2
1150
1151 void wxBitmap::SetOk(bool isOk)
1152 {
1153 EnsureHasData();
1154
1155 GetBitmapData()->m_ok = isOk;
1156 }
1157
1158 #endif // WXWIN_COMPATIBILITY_2
1159
1160 #if WXWIN_COMPATIBILITY_2_4
1161
1162 void wxBitmap::SetQuality(int WXUNUSED(quality))
1163 {
1164 }
1165
1166 #endif // WXWIN_COMPATIBILITY_2_4
1167
1168 // ----------------------------------------------------------------------------
1169 // raw bitmap access support
1170 // ----------------------------------------------------------------------------
1171
1172 bool wxBitmap::GetRawData(wxRawBitmapData *data)
1173 {
1174 wxCHECK_MSG( data, FALSE, _T("NULL pointer in wxBitmap::GetRawData") );
1175
1176 if ( !Ok() )
1177 {
1178 // no bitmap, no data (raw or otherwise)
1179 return FALSE;
1180 }
1181
1182 // we only support raw access to the DIBs, so check if we have one
1183 DIBSECTION ds;
1184 if ( ::GetObject(GetHbitmap(), sizeof(ds), &ds) != sizeof(DIBSECTION) )
1185 {
1186 return FALSE;
1187 }
1188
1189 // ok, store the relevant info in wxRawBitmapData
1190 const LONG h = ds.dsBm.bmHeight;
1191
1192 data->m_width = ds.dsBm.bmWidth;
1193 data->m_height = h;
1194 data->m_bypp = ds.dsBm.bmBitsPixel / 8;
1195
1196 // remember that DIBs are stored in top to bottom order!
1197 const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
1198 data->m_stride = -bytesPerRow;
1199 data->m_pixels = (unsigned char *)ds.dsBm.bmBits;
1200 if ( h > 1 )
1201 {
1202 data->m_pixels += (h - 1)*bytesPerRow;
1203 }
1204
1205 return TRUE;
1206 }
1207
1208 void wxBitmap::UngetRawData(wxRawBitmapData *data)
1209 {
1210 wxCHECK_RET( data, _T("NULL pointer in wxBitmap::UngetRawData()") );
1211
1212 // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap
1213 // API uses normal, not premultiplied, colours, so adjust them here now
1214 wxRawBitmapIterator p(*data);
1215
1216 const int w = data->GetWidth();
1217 const int h = data->GetHeight();
1218
1219 for ( int y = 0; y < h; y++ )
1220 {
1221 wxRawBitmapIterator rowStart = p;
1222
1223 for ( int x = 0; x < w; x++ )
1224 {
1225 const unsigned alpha = p.Alpha();
1226
1227 p.Red() = (p.Red() * alpha) / 255;
1228 p.Blue() = (p.Blue() * alpha) / 255;
1229 p.Green() = (p.Green() * alpha) / 255;
1230
1231 ++p;
1232 }
1233
1234 p = rowStart;
1235 p.OffsetY(1);
1236 }
1237 }
1238
1239 // ----------------------------------------------------------------------------
1240 // wxMask
1241 // ----------------------------------------------------------------------------
1242
1243 wxMask::wxMask()
1244 {
1245 m_maskBitmap = 0;
1246 }
1247
1248 // Construct a mask from a bitmap and a colour indicating
1249 // the transparent area
1250 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1251 {
1252 m_maskBitmap = 0;
1253 Create(bitmap, colour);
1254 }
1255
1256 // Construct a mask from a bitmap and a palette index indicating
1257 // the transparent area
1258 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1259 {
1260 m_maskBitmap = 0;
1261 Create(bitmap, paletteIndex);
1262 }
1263
1264 // Construct a mask from a mono bitmap (copies the bitmap).
1265 wxMask::wxMask(const wxBitmap& bitmap)
1266 {
1267 m_maskBitmap = 0;
1268 Create(bitmap);
1269 }
1270
1271 wxMask::~wxMask()
1272 {
1273 if ( m_maskBitmap )
1274 ::DeleteObject((HBITMAP) m_maskBitmap);
1275 }
1276
1277 // Create a mask from a mono bitmap (copies the bitmap).
1278 bool wxMask::Create(const wxBitmap& bitmap)
1279 {
1280 #ifndef __WXMICROWIN__
1281 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1282 _T("can't create mask from invalid or not monochrome bitmap") );
1283
1284 if ( m_maskBitmap )
1285 {
1286 ::DeleteObject((HBITMAP) m_maskBitmap);
1287 m_maskBitmap = 0;
1288 }
1289
1290 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1291 bitmap.GetWidth(),
1292 bitmap.GetHeight(),
1293 1, 1, 0
1294 );
1295 HDC srcDC = CreateCompatibleDC(0);
1296 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1297 HDC destDC = CreateCompatibleDC(0);
1298 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1299 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1300 SelectObject(srcDC, 0);
1301 DeleteDC(srcDC);
1302 SelectObject(destDC, 0);
1303 DeleteDC(destDC);
1304 return TRUE;
1305 #else
1306 return FALSE;
1307 #endif
1308 }
1309
1310 // Create a mask from a bitmap and a palette index indicating
1311 // the transparent area
1312 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1313 {
1314 if ( m_maskBitmap )
1315 {
1316 ::DeleteObject((HBITMAP) m_maskBitmap);
1317 m_maskBitmap = 0;
1318 }
1319
1320 #if wxUSE_PALETTE
1321 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1322 {
1323 unsigned char red, green, blue;
1324 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1325 {
1326 wxColour transparentColour(red, green, blue);
1327 return Create(bitmap, transparentColour);
1328 }
1329 }
1330 #endif // wxUSE_PALETTE
1331
1332 return FALSE;
1333 }
1334
1335 // Create a mask from a bitmap and a colour indicating
1336 // the transparent area
1337 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1338 {
1339 #ifndef __WXMICROWIN__
1340 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1341
1342 if ( m_maskBitmap )
1343 {
1344 ::DeleteObject((HBITMAP) m_maskBitmap);
1345 m_maskBitmap = 0;
1346 }
1347
1348 int width = bitmap.GetWidth(),
1349 height = bitmap.GetHeight();
1350
1351 // scan the bitmap for the transparent colour and set the corresponding
1352 // pixels in the mask to BLACK and the rest to WHITE
1353 COLORREF maskColour = wxColourToPalRGB(colour);
1354 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1355
1356 HDC srcDC = ::CreateCompatibleDC(NULL);
1357 HDC destDC = ::CreateCompatibleDC(NULL);
1358 if ( !srcDC || !destDC )
1359 {
1360 wxLogLastError(wxT("CreateCompatibleDC"));
1361 }
1362
1363 bool ok = TRUE;
1364
1365 // SelectObject() will fail
1366 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1367 _T("bitmap can't be selected in another DC") );
1368
1369 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1370 if ( !hbmpSrcOld )
1371 {
1372 wxLogLastError(wxT("SelectObject"));
1373
1374 ok = FALSE;
1375 }
1376
1377 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1378 if ( !hbmpDstOld )
1379 {
1380 wxLogLastError(wxT("SelectObject"));
1381
1382 ok = FALSE;
1383 }
1384
1385 if ( ok )
1386 {
1387 // this will create a monochrome bitmap with 0 points for the pixels
1388 // which have the same value as the background colour and 1 for the
1389 // others
1390 ::SetBkColor(srcDC, maskColour);
1391 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
1392 }
1393
1394 ::SelectObject(srcDC, hbmpSrcOld);
1395 ::DeleteDC(srcDC);
1396 ::SelectObject(destDC, hbmpDstOld);
1397 ::DeleteDC(destDC);
1398
1399 return ok;
1400 #else // __WXMICROWIN__
1401 return FALSE;
1402 #endif // __WXMICROWIN__/!__WXMICROWIN__
1403 }
1404
1405 // ----------------------------------------------------------------------------
1406 // wxBitmapHandler
1407 // ----------------------------------------------------------------------------
1408
1409 bool wxBitmapHandler::Create(wxGDIImage *image,
1410 void *data,
1411 long flags,
1412 int width, int height, int depth)
1413 {
1414 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1415
1416 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1417 }
1418
1419 bool wxBitmapHandler::Load(wxGDIImage *image,
1420 const wxString& name,
1421 long flags,
1422 int width, int height)
1423 {
1424 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1425
1426 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1427 }
1428
1429 bool wxBitmapHandler::Save(wxGDIImage *image,
1430 const wxString& name,
1431 int type)
1432 {
1433 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1434
1435 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1436 }
1437
1438 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1439 void *WXUNUSED(data),
1440 long WXUNUSED(type),
1441 int WXUNUSED(width),
1442 int WXUNUSED(height),
1443 int WXUNUSED(depth))
1444 {
1445 return FALSE;
1446 }
1447
1448 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1449 const wxString& WXUNUSED(name),
1450 long WXUNUSED(type),
1451 int WXUNUSED(desiredWidth),
1452 int WXUNUSED(desiredHeight))
1453 {
1454 return FALSE;
1455 }
1456
1457 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1458 const wxString& WXUNUSED(name),
1459 int WXUNUSED(type),
1460 const wxPalette *WXUNUSED(palette))
1461 {
1462 return FALSE;
1463 }
1464
1465 // ----------------------------------------------------------------------------
1466 // DIB functions
1467 // ----------------------------------------------------------------------------
1468
1469 #ifndef __WXMICROWIN__
1470 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1471 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1472 {
1473 unsigned long i, headerSize;
1474 LPBITMAPINFO lpDIBheader = NULL;
1475 LPPALETTEENTRY lpPe = NULL;
1476
1477
1478 // Allocate space for a DIB header
1479 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1480 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1481 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1482
1483 GetPaletteEntries(hPal, 0, 256, lpPe);
1484
1485 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1486
1487 // Fill in the static parts of the DIB header
1488 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1489 lpDIBheader->bmiHeader.biWidth = xSize;
1490 lpDIBheader->bmiHeader.biHeight = ySize;
1491 lpDIBheader->bmiHeader.biPlanes = 1;
1492
1493 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1494 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1495 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1496 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1497 lpDIBheader->bmiHeader.biClrUsed = 256;
1498
1499
1500 // Initialize the DIB palette
1501 for (i = 0; i < 256; i++) {
1502 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1503 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1504 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1505 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1506 }
1507
1508 *lpDIBHeader = lpDIBheader;
1509
1510 return TRUE;
1511 }
1512
1513 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1514 {
1515 free(lpDIBHeader);
1516 }
1517 #endif
1518
1519 // ----------------------------------------------------------------------------
1520 // global helper functions implemented here
1521 // ----------------------------------------------------------------------------
1522
1523 // helper of wxBitmapToHICON/HCURSOR
1524 static
1525 HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1526 bool iconWanted,
1527 int hotSpotX,
1528 int hotSpotY)
1529 {
1530 if ( !bmp.Ok() )
1531 {
1532 // we can't create an icon/cursor form nothing
1533 return 0;
1534 }
1535
1536 wxMask *mask = bmp.GetMask();
1537 if ( !mask )
1538 {
1539 // we must have a mask for an icon, so even if it's probably incorrect,
1540 // do create it (grey is the "standard" transparent colour)
1541 mask = new wxMask(bmp, *wxLIGHT_GREY);
1542 }
1543
1544 ICONINFO iconInfo;
1545 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1546 if ( !iconWanted )
1547 {
1548 iconInfo.xHotspot = hotSpotX;
1549 iconInfo.yHotspot = hotSpotY;
1550 }
1551
1552 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1553 iconInfo.hbmColor = GetHbitmapOf(bmp);
1554
1555 // black out the transparent area to preserve background colour, because
1556 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1557 // the mask to the dest rect.
1558 {
1559 MemoryHDC dcSrc, dcDst;
1560 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1561 selectBitmap(dcDst, iconInfo.hbmColor);
1562
1563 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1564 dcSrc, 0, 0, SRCAND) )
1565 {
1566 wxLogLastError(_T("BitBlt"));
1567 }
1568 }
1569
1570 HICON hicon = ::CreateIconIndirect(&iconInfo);
1571
1572 if ( !bmp.GetMask() )
1573 {
1574 // we created the mask, now delete it
1575 delete mask;
1576 }
1577
1578 // delete the inverted mask bitmap we created as well
1579 ::DeleteObject(iconInfo.hbmMask);
1580
1581 return hicon;
1582 }
1583
1584 HICON wxBitmapToHICON(const wxBitmap& bmp)
1585 {
1586 return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
1587 }
1588
1589 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1590 {
1591 return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
1592 }
1593
1594 HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1595 {
1596 #ifndef __WXMICROWIN__
1597 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1598
1599 // get width/height from the bitmap if not given
1600 if ( !w || !h )
1601 {
1602 BITMAP bm;
1603 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1604 w = bm.bmWidth;
1605 h = bm.bmHeight;
1606 }
1607
1608 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1609 HDC hdcDst = ::CreateCompatibleDC(NULL);
1610 if ( !hdcSrc || !hdcDst )
1611 {
1612 wxLogLastError(wxT("CreateCompatibleDC"));
1613 }
1614
1615 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1616 if ( !hbmpInvMask )
1617 {
1618 wxLogLastError(wxT("CreateBitmap"));
1619 }
1620
1621 ::SelectObject(hdcSrc, hbmpMask);
1622 ::SelectObject(hdcDst, hbmpInvMask);
1623 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1624 hdcSrc, 0, 0,
1625 NOTSRCCOPY) )
1626 {
1627 wxLogLastError(wxT("BitBlt"));
1628 }
1629
1630 ::DeleteDC(hdcSrc);
1631 ::DeleteDC(hdcDst);
1632
1633 return hbmpInvMask;
1634 #else
1635 return 0;
1636 #endif
1637 }