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