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