]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/bitmap.cpp
update to make digitalmars compile/link make clean
[wxWidgets.git] / src / msw / bitmap.cpp
... / ...
CommitLineData
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
64class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
65{
66public:
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
88public:
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
115private:
116 // optional mask for transparent drawing
117 wxMask *m_bitmapMask;
118
119 DECLARE_NO_COPY_CLASS(wxBitmapRefData)
120};
121
122// ----------------------------------------------------------------------------
123// macros
124// ----------------------------------------------------------------------------
125
126IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
127IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
128
129IMPLEMENT_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
140static 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
167wxBitmapRefData::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
181void 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
205void wxBitmap::Init()
206{
207 // m_refData = NULL; done in the base class ctor
208}
209
210wxGDIImageRefData *wxBitmap::CreateData() const
211{
212 return new wxBitmapRefData;
213}
214
215#ifdef __WIN32__
216
217bool 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
263bool 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
279bool 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
323bool 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
355wxBitmap::~wxBitmap()
356{
357}
358
359wxBitmap::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
426bool 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
444wxBitmap::wxBitmap(int w, int h, int d)
445{
446 Init();
447
448 (void)Create(w, h, d);
449}
450
451wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
452{
453 Init();
454
455 (void)Create(w, h, dc);
456}
457
458wxBitmap::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
465wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
466{
467 Init();
468
469 LoadFile(filename, (int)type);
470}
471
472bool wxBitmap::Create(int width, int height, int depth)
473{
474 return DoCreate(width, height, depth, 0);
475}
476
477bool 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
484bool 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
562bool 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
681wxImage 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
750bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
751{
752 return CreateFromImage(image, depth, 0);
753}
754
755bool 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
763bool 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
824wxImage 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
958bool 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
986bool 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
1004bool 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
1033wxBitmap 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
1093wxPalette* wxBitmap::GetPalette() const
1094{
1095 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1096 : (wxPalette *) NULL;
1097}
1098
1099wxMask *wxBitmap::GetMask() const
1100{
1101 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1102}
1103
1104#ifdef __WXDEBUG__
1105
1106wxDC *wxBitmap::GetSelectedInto() const
1107{
1108 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
1109}
1110
1111#endif
1112
1113#if WXWIN_COMPATIBILITY_2_4
1114
1115int wxBitmap::GetQuality() const
1116{
1117 return 0;
1118}
1119
1120#endif // WXWIN_COMPATIBILITY_2_4
1121
1122void wxBitmap::UseAlpha()
1123{
1124 if ( GetBitmapData() )
1125 GetBitmapData()->m_hasAlpha = true;
1126}
1127
1128bool wxBitmap::HasAlpha() const
1129{
1130 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1131}
1132
1133// ----------------------------------------------------------------------------
1134// wxBitmap setters
1135// ----------------------------------------------------------------------------
1136
1137#ifdef __WXDEBUG__
1138
1139void wxBitmap::SetSelectedInto(wxDC *dc)
1140{
1141 if ( GetBitmapData() )
1142 GetBitmapData()->m_selectedInto = dc;
1143}
1144
1145#endif
1146
1147#if wxUSE_PALETTE
1148
1149void wxBitmap::SetPalette(const wxPalette& palette)
1150{
1151 EnsureHasData();
1152
1153 GetBitmapData()->m_bitmapPalette = palette;
1154}
1155
1156#endif // wxUSE_PALETTE
1157
1158void wxBitmap::SetMask(wxMask *mask)
1159{
1160 EnsureHasData();
1161
1162 GetBitmapData()->SetMask(mask);
1163}
1164
1165#if WXWIN_COMPATIBILITY_2
1166
1167void 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
1178void wxBitmap::SetQuality(int WXUNUSED(quality))
1179{
1180}
1181
1182#endif // WXWIN_COMPATIBILITY_2_4
1183
1184// ----------------------------------------------------------------------------
1185// raw bitmap access support
1186// ----------------------------------------------------------------------------
1187
1188bool wxBitmap::GetRawData(wxRawBitmapData *data)
1189{
1190 wxCHECK_MSG( data, FALSE, _T("NULL pointer in wxBitmap::GetRawData") );
1191
1192 if ( !Ok() )
1193 {
1194 // no bitmap, no data (raw or otherwise)
1195 return FALSE;
1196 }
1197
1198 // if we're already a DIB we can access our data directly, but if not we
1199 // need to convert this DDB to a DIB section and use it for raw access and
1200 // then convert it back
1201 HBITMAP hDIB;
1202 if ( !GetBitmapData()->m_isDIB )
1203 {
1204 wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE,
1205 _T("GetRawData() may be called only once") );
1206
1207 wxDIB *dib = new wxDIB(*this);
1208 if ( !dib->IsOk() )
1209 {
1210 delete dib;
1211
1212 return FALSE;
1213 }
1214
1215 // we'll free it in UngetRawData()
1216 GetBitmapData()->m_dib = dib;
1217
1218 hDIB = dib->GetHandle();
1219 }
1220 else // we're a DIB
1221 {
1222 hDIB = GetHbitmap();
1223 }
1224
1225 DIBSECTION ds;
1226 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
1227 {
1228 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1229
1230 return FALSE;
1231 }
1232
1233 // ok, store the relevant info in wxRawBitmapData
1234 const LONG h = ds.dsBm.bmHeight;
1235
1236 data->m_width = ds.dsBm.bmWidth;
1237 data->m_height = h;
1238 data->m_bypp = ds.dsBm.bmBitsPixel / 8;
1239
1240 // remember that DIBs are stored in top to bottom order!
1241 const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
1242 data->m_stride = -bytesPerRow;
1243 data->m_pixels = (unsigned char *)ds.dsBm.bmBits;
1244 if ( h > 1 )
1245 {
1246 data->m_pixels += (h - 1)*bytesPerRow;
1247 }
1248
1249 return TRUE;
1250}
1251
1252void wxBitmap::UngetRawData(wxRawBitmapData *data)
1253{
1254 wxCHECK_RET( data, _T("NULL pointer in wxBitmap::UngetRawData()") );
1255
1256 if ( !Ok() )
1257 return;
1258
1259 if ( !*data )
1260 {
1261 // invalid data, don't crash -- but don't assert neither as we're
1262 // called automatically from wxRawBitmapData dtor and so there is no
1263 // way to prevent this from happening
1264 return;
1265 }
1266
1267 // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap
1268 // API uses normal, not premultiplied, colours, so adjust them here now
1269 wxRawBitmapIterator p(*data);
1270
1271 const int w = data->GetWidth();
1272 const int h = data->GetHeight();
1273
1274 for ( int y = 0; y < h; y++ )
1275 {
1276 wxRawBitmapIterator rowStart = p;
1277
1278 for ( int x = 0; x < w; x++ )
1279 {
1280 const unsigned alpha = p.Alpha();
1281
1282 p.Red() = (p.Red() * alpha + 127) / 255;
1283 p.Blue() = (p.Blue() * alpha + 127) / 255;
1284 p.Green() = (p.Green() * alpha + 127) / 255;
1285
1286 ++p;
1287 }
1288
1289 p = rowStart;
1290 p.OffsetY(1);
1291 }
1292
1293 // if we're a DDB we need to convert DIB back to DDB now to make the
1294 // changes made via wxRawBitmapData effective
1295 if ( !GetBitmapData()->m_isDIB )
1296 {
1297 wxDIB *dib = GetBitmapData()->m_dib;
1298 GetBitmapData()->m_dib = NULL;
1299
1300 // TODO: convert
1301
1302 delete dib;
1303 }
1304}
1305
1306// ----------------------------------------------------------------------------
1307// wxMask
1308// ----------------------------------------------------------------------------
1309
1310wxMask::wxMask()
1311{
1312 m_maskBitmap = 0;
1313}
1314
1315// Construct a mask from a bitmap and a colour indicating
1316// the transparent area
1317wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1318{
1319 m_maskBitmap = 0;
1320 Create(bitmap, colour);
1321}
1322
1323// Construct a mask from a bitmap and a palette index indicating
1324// the transparent area
1325wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1326{
1327 m_maskBitmap = 0;
1328 Create(bitmap, paletteIndex);
1329}
1330
1331// Construct a mask from a mono bitmap (copies the bitmap).
1332wxMask::wxMask(const wxBitmap& bitmap)
1333{
1334 m_maskBitmap = 0;
1335 Create(bitmap);
1336}
1337
1338wxMask::~wxMask()
1339{
1340 if ( m_maskBitmap )
1341 ::DeleteObject((HBITMAP) m_maskBitmap);
1342}
1343
1344// Create a mask from a mono bitmap (copies the bitmap).
1345bool wxMask::Create(const wxBitmap& bitmap)
1346{
1347#ifndef __WXMICROWIN__
1348 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1349 _T("can't create mask from invalid or not monochrome bitmap") );
1350
1351 if ( m_maskBitmap )
1352 {
1353 ::DeleteObject((HBITMAP) m_maskBitmap);
1354 m_maskBitmap = 0;
1355 }
1356
1357 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1358 bitmap.GetWidth(),
1359 bitmap.GetHeight(),
1360 1, 1, 0
1361 );
1362 HDC srcDC = CreateCompatibleDC(0);
1363 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1364 HDC destDC = CreateCompatibleDC(0);
1365 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1366 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1367 SelectObject(srcDC, 0);
1368 DeleteDC(srcDC);
1369 SelectObject(destDC, 0);
1370 DeleteDC(destDC);
1371 return TRUE;
1372#else
1373 return FALSE;
1374#endif
1375}
1376
1377// Create a mask from a bitmap and a palette index indicating
1378// the transparent area
1379bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1380{
1381 if ( m_maskBitmap )
1382 {
1383 ::DeleteObject((HBITMAP) m_maskBitmap);
1384 m_maskBitmap = 0;
1385 }
1386
1387#if wxUSE_PALETTE
1388 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1389 {
1390 unsigned char red, green, blue;
1391 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1392 {
1393 wxColour transparentColour(red, green, blue);
1394 return Create(bitmap, transparentColour);
1395 }
1396 }
1397#endif // wxUSE_PALETTE
1398
1399 return FALSE;
1400}
1401
1402// Create a mask from a bitmap and a colour indicating
1403// the transparent area
1404bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1405{
1406#ifndef __WXMICROWIN__
1407 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1408
1409 if ( m_maskBitmap )
1410 {
1411 ::DeleteObject((HBITMAP) m_maskBitmap);
1412 m_maskBitmap = 0;
1413 }
1414
1415 int width = bitmap.GetWidth(),
1416 height = bitmap.GetHeight();
1417
1418 // scan the bitmap for the transparent colour and set the corresponding
1419 // pixels in the mask to BLACK and the rest to WHITE
1420 COLORREF maskColour = wxColourToPalRGB(colour);
1421 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1422
1423 HDC srcDC = ::CreateCompatibleDC(NULL);
1424 HDC destDC = ::CreateCompatibleDC(NULL);
1425 if ( !srcDC || !destDC )
1426 {
1427 wxLogLastError(wxT("CreateCompatibleDC"));
1428 }
1429
1430 bool ok = TRUE;
1431
1432 // SelectObject() will fail
1433 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1434 _T("bitmap can't be selected in another DC") );
1435
1436 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1437 if ( !hbmpSrcOld )
1438 {
1439 wxLogLastError(wxT("SelectObject"));
1440
1441 ok = FALSE;
1442 }
1443
1444 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1445 if ( !hbmpDstOld )
1446 {
1447 wxLogLastError(wxT("SelectObject"));
1448
1449 ok = FALSE;
1450 }
1451
1452 if ( ok )
1453 {
1454 // this will create a monochrome bitmap with 0 points for the pixels
1455 // which have the same value as the background colour and 1 for the
1456 // others
1457 ::SetBkColor(srcDC, maskColour);
1458 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
1459 }
1460
1461 ::SelectObject(srcDC, hbmpSrcOld);
1462 ::DeleteDC(srcDC);
1463 ::SelectObject(destDC, hbmpDstOld);
1464 ::DeleteDC(destDC);
1465
1466 return ok;
1467#else // __WXMICROWIN__
1468 return FALSE;
1469#endif // __WXMICROWIN__/!__WXMICROWIN__
1470}
1471
1472// ----------------------------------------------------------------------------
1473// wxBitmapHandler
1474// ----------------------------------------------------------------------------
1475
1476bool wxBitmapHandler::Create(wxGDIImage *image,
1477 void *data,
1478 long flags,
1479 int width, int height, int depth)
1480{
1481 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1482
1483 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1484}
1485
1486bool wxBitmapHandler::Load(wxGDIImage *image,
1487 const wxString& name,
1488 long flags,
1489 int width, int height)
1490{
1491 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1492
1493 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1494}
1495
1496bool wxBitmapHandler::Save(wxGDIImage *image,
1497 const wxString& name,
1498 int type)
1499{
1500 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1501
1502 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1503}
1504
1505bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1506 void *WXUNUSED(data),
1507 long WXUNUSED(type),
1508 int WXUNUSED(width),
1509 int WXUNUSED(height),
1510 int WXUNUSED(depth))
1511{
1512 return FALSE;
1513}
1514
1515bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1516 const wxString& WXUNUSED(name),
1517 long WXUNUSED(type),
1518 int WXUNUSED(desiredWidth),
1519 int WXUNUSED(desiredHeight))
1520{
1521 return FALSE;
1522}
1523
1524bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1525 const wxString& WXUNUSED(name),
1526 int WXUNUSED(type),
1527 const wxPalette *WXUNUSED(palette))
1528{
1529 return FALSE;
1530}
1531
1532// ----------------------------------------------------------------------------
1533// DIB functions
1534// ----------------------------------------------------------------------------
1535
1536#ifndef __WXMICROWIN__
1537bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1538 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1539{
1540 unsigned long i, headerSize;
1541 LPBITMAPINFO lpDIBheader = NULL;
1542 LPPALETTEENTRY lpPe = NULL;
1543
1544
1545 // Allocate space for a DIB header
1546 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1547 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1548 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1549
1550 GetPaletteEntries(hPal, 0, 256, lpPe);
1551
1552 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1553
1554 // Fill in the static parts of the DIB header
1555 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1556 lpDIBheader->bmiHeader.biWidth = xSize;
1557 lpDIBheader->bmiHeader.biHeight = ySize;
1558 lpDIBheader->bmiHeader.biPlanes = 1;
1559
1560 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1561 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1562 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1563 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1564 lpDIBheader->bmiHeader.biClrUsed = 256;
1565
1566
1567 // Initialize the DIB palette
1568 for (i = 0; i < 256; i++) {
1569 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1570 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1571 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1572 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1573 }
1574
1575 *lpDIBHeader = lpDIBheader;
1576
1577 return TRUE;
1578}
1579
1580void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1581{
1582 free(lpDIBHeader);
1583}
1584#endif
1585
1586// ----------------------------------------------------------------------------
1587// global helper functions implemented here
1588// ----------------------------------------------------------------------------
1589
1590// helper of wxBitmapToHICON/HCURSOR
1591static
1592HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1593 bool iconWanted,
1594 int hotSpotX,
1595 int hotSpotY)
1596{
1597 if ( !bmp.Ok() )
1598 {
1599 // we can't create an icon/cursor form nothing
1600 return 0;
1601 }
1602
1603 wxMask *mask = bmp.GetMask();
1604 if ( !mask )
1605 {
1606 // we must have a mask for an icon, so even if it's probably incorrect,
1607 // do create it (grey is the "standard" transparent colour)
1608 mask = new wxMask(bmp, *wxLIGHT_GREY);
1609 }
1610
1611 ICONINFO iconInfo;
1612 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1613 if ( !iconWanted )
1614 {
1615 iconInfo.xHotspot = hotSpotX;
1616 iconInfo.yHotspot = hotSpotY;
1617 }
1618
1619 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1620 iconInfo.hbmColor = GetHbitmapOf(bmp);
1621
1622 // black out the transparent area to preserve background colour, because
1623 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1624 // the mask to the dest rect.
1625 {
1626 MemoryHDC dcSrc, dcDst;
1627 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1628 selectBitmap(dcDst, iconInfo.hbmColor);
1629
1630 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1631 dcSrc, 0, 0, SRCAND) )
1632 {
1633 wxLogLastError(_T("BitBlt"));
1634 }
1635 }
1636
1637 HICON hicon = ::CreateIconIndirect(&iconInfo);
1638
1639 if ( !bmp.GetMask() )
1640 {
1641 // we created the mask, now delete it
1642 delete mask;
1643 }
1644
1645 // delete the inverted mask bitmap we created as well
1646 ::DeleteObject(iconInfo.hbmMask);
1647
1648 return hicon;
1649}
1650
1651HICON wxBitmapToHICON(const wxBitmap& bmp)
1652{
1653 return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
1654}
1655
1656HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1657{
1658 return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
1659}
1660
1661HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1662{
1663#ifndef __WXMICROWIN__
1664 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1665
1666 // get width/height from the bitmap if not given
1667 if ( !w || !h )
1668 {
1669 BITMAP bm;
1670 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1671 w = bm.bmWidth;
1672 h = bm.bmHeight;
1673 }
1674
1675 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1676 HDC hdcDst = ::CreateCompatibleDC(NULL);
1677 if ( !hdcSrc || !hdcDst )
1678 {
1679 wxLogLastError(wxT("CreateCompatibleDC"));
1680 }
1681
1682 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1683 if ( !hbmpInvMask )
1684 {
1685 wxLogLastError(wxT("CreateBitmap"));
1686 }
1687
1688 ::SelectObject(hdcSrc, hbmpMask);
1689 ::SelectObject(hdcDst, hbmpInvMask);
1690 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1691 hdcSrc, 0, 0,
1692 NOTSRCCOPY) )
1693 {
1694 wxLogLastError(wxT("BitBlt"));
1695 }
1696
1697 ::DeleteDC(hdcSrc);
1698 ::DeleteDC(hdcDst);
1699
1700 return hbmpInvMask;
1701#else
1702 return 0;
1703#endif
1704}