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