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