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