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