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