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