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