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