]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
015b00e73249610b483b5482b14dac586d3e0587
[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 and Markus Holzem
9 // Licence: wxWindows license
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 #include "wx/msw/dib.h"
47 #include "wx/image.h"
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
54 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
55
56 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // wxBitmapRefData
64 // ----------------------------------------------------------------------------
65
66 wxBitmapRefData::wxBitmapRefData()
67 {
68 m_quality = 0;
69 m_selectedInto = NULL;
70 m_numColors = 0;
71 m_bitmapMask = NULL;
72 m_hBitmap = (WXHBITMAP) NULL;
73 }
74
75 void wxBitmapRefData::Free()
76 {
77 wxASSERT_MSG( !m_selectedInto,
78 wxT("deleting bitmap still selected into wxMemoryDC") );
79
80 if ( m_hBitmap)
81 {
82 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
83 {
84 wxLogLastError("DeleteObject(hbitmap)");
85 }
86 }
87
88 delete m_bitmapMask;
89 m_bitmapMask = NULL;
90 }
91
92 // ----------------------------------------------------------------------------
93 // wxBitmap creation
94 // ----------------------------------------------------------------------------
95
96 // this function should be called from all wxBitmap ctors
97 void wxBitmap::Init()
98 {
99 // m_refData = NULL; done in the base class ctor
100
101 if ( wxTheBitmapList )
102 wxTheBitmapList->AddBitmap(this);
103 }
104
105 #ifdef __WIN32__
106
107 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
108 {
109 // it may be either HICON or HCURSOR
110 HICON hicon = (HICON)icon.GetHandle();
111
112 ICONINFO iconInfo;
113 if ( !::GetIconInfo(hicon, &iconInfo) )
114 {
115 wxLogLastError("GetIconInfo");
116
117 return FALSE;
118 }
119
120 wxBitmapRefData *refData = new wxBitmapRefData;
121 m_refData = refData;
122
123 int w = icon.GetWidth(),
124 h = icon.GetHeight();
125
126 refData->m_width = w;
127 refData->m_height = h;
128 refData->m_depth = wxDisplayDepth();
129
130 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
131
132 // the mask returned by GetIconInfo() is inversed compared to the usual
133 // wxWin convention
134 refData->m_bitmapMask = new wxMask((WXHBITMAP)
135 wxInvertMask(iconInfo.hbmMask, w, h));
136
137 #if WXWIN_COMPATIBILITY_2
138 refData->m_ok = TRUE;
139 #endif // WXWIN_COMPATIBILITY_2
140
141 return TRUE;
142 }
143
144 #endif // Win32
145
146 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
147 {
148 UnRef();
149
150 if ( !cursor.Ok() )
151 return FALSE;
152
153 #ifdef __WIN16__
154 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
155
156 return FALSE;
157 #else
158 return CopyFromIconOrCursor(cursor);
159 #endif // Win16
160 }
161
162 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
163 {
164 UnRef();
165
166 if ( !icon.Ok() )
167 return FALSE;
168
169 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
170 // to create a bitmap from icon there - but using this way we won't have
171 // the mask (FIXME)
172 #ifdef __WIN16__
173 int width = icon.GetWidth(),
174 height = icon.GetHeight();
175
176 // copy the icon to the bitmap
177 ScreenHDC hdcScreen;
178 HDC hdc = ::CreateCompatibleDC(hdcScreen);
179 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
180 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
181
182 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
183
184 ::SelectObject(hdc, hbmpOld);
185 ::DeleteDC(hdc);
186
187 wxBitmapRefData *refData = new wxBitmapRefData;
188 m_refData = refData;
189
190 refData->m_width = width;
191 refData->m_height = height;
192 refData->m_depth = wxDisplayDepth();
193
194 refData->m_hBitmap = (WXHBITMAP)hbitmap;
195
196 #if WXWIN_COMPATIBILITY_2
197 refData->m_ok = TRUE;
198 #endif // WXWIN_COMPATIBILITY_2
199
200 return TRUE;
201 #else // Win32
202 return CopyFromIconOrCursor(icon);
203 #endif // Win16/Win32
204 }
205
206 wxBitmap::~wxBitmap()
207 {
208 if (wxTheBitmapList)
209 wxTheBitmapList->DeleteObject(this);
210 }
211
212 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
213 {
214 Init();
215
216 wxBitmapRefData *refData = new wxBitmapRefData;
217 m_refData = refData;
218
219 refData->m_width = width;
220 refData->m_height = height;
221 refData->m_depth = depth;
222 refData->m_numColors = 0;
223 refData->m_selectedInto = NULL;
224
225 char *data;
226 if ( depth == 1 )
227 {
228 // we assume that it is in XBM format which is not quite the same as
229 // the format CreateBitmap() wants because the order of bytes in the
230 // line is inversed!
231 static const size_t bytesPerLine = (width + 7) / 8;
232 static const size_t padding = bytesPerLine % 2;
233 static const size_t len = height * ( padding + bytesPerLine );
234 data = (char *)malloc(len);
235 const char *src = bits;
236 char *dst = data;
237
238 for ( int rows = 0; rows < height; rows++ )
239 {
240 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
241 {
242 unsigned char val = *src++;
243 unsigned char reversed = 0;
244
245 for ( int bits = 0; bits < 8; bits++)
246 {
247 reversed <<= 1;
248 reversed |= (val & 0x01);
249 val >>= 1;
250 }
251 *dst++ = reversed;
252 }
253
254 if ( padding )
255 *dst++ = 0;
256 }
257 }
258 else
259 {
260 // bits should already be in Windows standard format
261 data = (char *)bits; // const_cast is harmless
262 }
263
264 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
265 if ( !hbmp )
266 {
267 wxLogLastError("CreateBitmap");
268 }
269
270 if ( data != bits )
271 {
272 free(data);
273 }
274
275 SetHBITMAP((WXHBITMAP)hbmp);
276 }
277
278 // Create from XPM data
279 bool wxBitmap::CreateFromXpm(const char **data)
280 {
281 Init();
282
283 return Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
284 }
285
286 wxBitmap::wxBitmap(int w, int h, int d)
287 {
288 Init();
289
290 (void)Create(w, h, d);
291 }
292
293 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
294 {
295 Init();
296
297 (void)Create(data, type, width, height, depth);
298 }
299
300 wxBitmap::wxBitmap(const wxString& filename, long type)
301 {
302 Init();
303
304 LoadFile(filename, (int)type);
305 }
306
307 bool wxBitmap::Create(int w, int h, int d)
308 {
309 UnRef();
310
311 m_refData = new wxBitmapRefData;
312
313 GetBitmapData()->m_width = w;
314 GetBitmapData()->m_height = h;
315 GetBitmapData()->m_depth = d;
316
317 HBITMAP hbmp;
318
319 if ( d > 0 )
320 {
321 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
322 if ( !hbmp )
323 {
324 wxLogLastError("CreateBitmap");
325 }
326 }
327 else
328 {
329 ScreenHDC dc;
330 hbmp = ::CreateCompatibleBitmap(dc, w, h);
331 if ( !hbmp )
332 {
333 wxLogLastError("CreateCompatibleBitmap");
334 }
335
336 GetBitmapData()->m_depth = wxDisplayDepth();
337 }
338
339 SetHBITMAP((WXHBITMAP)hbmp);
340
341 #if WXWIN_COMPATIBILITY_2
342 GetBitmapData()->m_ok = hbmp != 0;
343 #endif // WXWIN_COMPATIBILITY_2
344
345 return Ok();
346 }
347
348 bool wxBitmap::LoadFile(const wxString& filename, long type)
349 {
350 UnRef();
351
352 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
353
354 if ( handler )
355 {
356 m_refData = new wxBitmapRefData;
357
358 return handler->LoadFile(this, filename, type, -1, -1);
359 }
360 else
361 {
362 wxImage image;
363 if ( !image.LoadFile( filename, type ) || !image.Ok() )
364 return FALSE;
365
366 *this = image.ConvertToBitmap();
367
368 return TRUE;
369 }
370 }
371
372 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
373 {
374 UnRef();
375
376 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
377
378 if ( !handler )
379 {
380 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
381 "type %d defined."), type);
382
383 return FALSE;
384 }
385
386 m_refData = new wxBitmapRefData;
387
388 return handler->Create(this, data, type, width, height, depth);
389 }
390
391 bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
392 {
393 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
394
395 if ( handler )
396 {
397 return handler->SaveFile(this, filename, type, palette);
398 }
399 else
400 {
401 // FIXME what about palette? shouldn't we use it?
402 wxImage image( *this );
403 if (!image.Ok())
404 return FALSE;
405
406 return image.SaveFile( filename, type );
407 }
408 }
409
410 // ----------------------------------------------------------------------------
411 // sub bitmap extraction
412 // ----------------------------------------------------------------------------
413
414 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
415 {
416 wxCHECK_MSG( Ok() &&
417 (rect.x >= 0) && (rect.y >= 0) &&
418 (rect.x+rect.width <= GetWidth()) &&
419 (rect.y+rect.height <= GetHeight()),
420 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
421
422 wxBitmap ret( rect.width, rect.height, GetDepth() );
423 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
424
425 // copy bitmap data
426 HDC dcSrc = ::CreateCompatibleDC(NULL);
427 HDC dcDst = ::CreateCompatibleDC(NULL);
428 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
429 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
430 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
431
432 // copy mask if there is one
433 if (GetMask())
434 {
435 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
436
437 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
438 SelectObject(dcDst, (HBITMAP) hbmpMask);
439 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
440
441 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
442 ret.SetMask(mask);
443 }
444
445 SelectObject(dcDst, NULL);
446 SelectObject(dcSrc, NULL);
447 DeleteDC(dcDst);
448 DeleteDC(dcSrc);
449
450 return ret;
451 }
452
453 // ----------------------------------------------------------------------------
454 // wxBitmap accessors
455 // ----------------------------------------------------------------------------
456
457 void wxBitmap::SetQuality(int q)
458 {
459 EnsureHasData();
460
461 GetBitmapData()->m_quality = q;
462 }
463
464 #if WXWIN_COMPATIBILITY_2
465 void wxBitmap::SetOk(bool isOk)
466 {
467 EnsureHasData();
468
469 GetBitmapData()->m_ok = isOk;
470 }
471 #endif // WXWIN_COMPATIBILITY_2
472
473 void wxBitmap::SetPalette(const wxPalette& palette)
474 {
475 EnsureHasData();
476
477 GetBitmapData()->m_bitmapPalette = palette;
478 }
479
480 void wxBitmap::SetMask(wxMask *mask)
481 {
482 EnsureHasData();
483
484 GetBitmapData()->m_bitmapMask = mask;
485 }
486
487 // Creates a bitmap that matches the device context, from
488 // an arbitray bitmap. At present, the original bitmap must have an
489 // associated palette. TODO: use a default palette if no palette exists.
490 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
491 wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
492 {
493 wxMemoryDC memDC;
494 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
495 HPALETTE hPal = (HPALETTE) NULL;
496 LPBITMAPINFO lpDib;
497 void *lpBits = (void*) NULL;
498
499 if( GetPalette() && GetPalette()->Ok() )
500 {
501 tmpBitmap.SetPalette(*GetPalette());
502 memDC.SelectObject(tmpBitmap);
503 memDC.SetPalette(*GetPalette());
504 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
505 }
506 else
507 {
508 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
509 wxPalette palette;
510 palette.SetHPALETTE( (WXHPALETTE)hPal );
511 tmpBitmap.SetPalette( palette );
512 memDC.SelectObject(tmpBitmap);
513 memDC.SetPalette( palette );
514 }
515
516 // set the height negative because in a DIB the order of the lines is
517 // reversed
518 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
519 {
520 return wxNullBitmap;
521 }
522
523 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
524
525 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
526
527 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
528 GetWidth(), GetHeight(),
529 0, 0, 0, GetHeight(),
530 lpBits, lpDib, DIB_RGB_COLORS);
531
532 free(lpBits);
533
534 wxFreeDIB(lpDib);
535
536 return tmpBitmap;
537 }
538
539 // ----------------------------------------------------------------------------
540 // wxMask
541 // ----------------------------------------------------------------------------
542
543 wxMask::wxMask()
544 {
545 m_maskBitmap = 0;
546 }
547
548 // Construct a mask from a bitmap and a colour indicating
549 // the transparent area
550 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
551 {
552 m_maskBitmap = 0;
553 Create(bitmap, colour);
554 }
555
556 // Construct a mask from a bitmap and a palette index indicating
557 // the transparent area
558 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
559 {
560 m_maskBitmap = 0;
561 Create(bitmap, paletteIndex);
562 }
563
564 // Construct a mask from a mono bitmap (copies the bitmap).
565 wxMask::wxMask(const wxBitmap& bitmap)
566 {
567 m_maskBitmap = 0;
568 Create(bitmap);
569 }
570
571 wxMask::~wxMask()
572 {
573 if ( m_maskBitmap )
574 ::DeleteObject((HBITMAP) m_maskBitmap);
575 }
576
577 // Create a mask from a mono bitmap (copies the bitmap).
578 bool wxMask::Create(const wxBitmap& bitmap)
579 {
580 if ( m_maskBitmap )
581 {
582 ::DeleteObject((HBITMAP) m_maskBitmap);
583 m_maskBitmap = 0;
584 }
585 if (!bitmap.Ok() || bitmap.GetDepth() != 1)
586 {
587 return FALSE;
588 }
589 m_maskBitmap = (WXHBITMAP) CreateBitmap(
590 bitmap.GetWidth(),
591 bitmap.GetHeight(),
592 1, 1, 0
593 );
594 HDC srcDC = CreateCompatibleDC(0);
595 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
596 HDC destDC = CreateCompatibleDC(0);
597 SelectObject(destDC, (HBITMAP) m_maskBitmap);
598 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
599 SelectObject(srcDC, 0);
600 DeleteDC(srcDC);
601 SelectObject(destDC, 0);
602 DeleteDC(destDC);
603 return TRUE;
604 }
605
606 // Create a mask from a bitmap and a palette index indicating
607 // the transparent area
608 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
609 {
610 if ( m_maskBitmap )
611 {
612 ::DeleteObject((HBITMAP) m_maskBitmap);
613 m_maskBitmap = 0;
614 }
615 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
616 {
617 unsigned char red, green, blue;
618 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
619 {
620 wxColour transparentColour(red, green, blue);
621 return Create(bitmap, transparentColour);
622 }
623 }
624 return FALSE;
625 }
626
627 // Create a mask from a bitmap and a colour indicating
628 // the transparent area
629 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
630 {
631 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
632
633 if ( m_maskBitmap )
634 {
635 ::DeleteObject((HBITMAP) m_maskBitmap);
636 m_maskBitmap = 0;
637 }
638
639 int width = bitmap.GetWidth(),
640 height = bitmap.GetHeight();
641
642 // scan the bitmap for the transparent colour and set the corresponding
643 // pixels in the mask to BLACK and the rest to WHITE
644 COLORREF maskColour = wxColourToRGB(colour);
645 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
646
647 HDC srcDC = ::CreateCompatibleDC(NULL);
648 HDC destDC = ::CreateCompatibleDC(NULL);
649 if ( !srcDC || !destDC )
650 {
651 wxLogLastError("CreateCompatibleDC");
652 }
653
654 if ( !::SelectObject(srcDC, GetHbitmapOf(bitmap)) )
655 {
656 wxLogLastError("SelectObject");
657 }
658 if ( !::SelectObject(destDC, (HBITMAP)m_maskBitmap) )
659 {
660 wxLogLastError("SelectObject");
661 }
662
663 // this is not very efficient, but I can't think of a better way of doing
664 // it
665 for ( int w = 0; w < width; w++ )
666 {
667 for ( int h = 0; h < height; h++ )
668 {
669 COLORREF col = GetPixel(srcDC, w, h);
670 if ( col == CLR_INVALID )
671 {
672 wxLogLastError("GetPixel");
673
674 // doesn't make sense to continue
675 ::SelectObject(srcDC, 0);
676 ::DeleteDC(srcDC);
677 ::SelectObject(destDC, 0);
678 ::DeleteDC(destDC);
679
680 return FALSE;
681 }
682
683 if ( col == maskColour )
684 {
685 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
686 }
687 else
688 {
689 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
690 }
691 }
692 }
693
694 ::SelectObject(srcDC, 0);
695 ::DeleteDC(srcDC);
696 ::SelectObject(destDC, 0);
697 ::DeleteDC(destDC);
698
699 return TRUE;
700 }
701
702 // ----------------------------------------------------------------------------
703 // wxBitmapHandler
704 // ----------------------------------------------------------------------------
705
706 bool wxBitmapHandler::Create(wxGDIImage *image,
707 void *data,
708 long flags,
709 int width, int height, int depth)
710 {
711 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
712
713 return bitmap ? Create(bitmap, data, width, height, depth) : FALSE;
714 }
715
716 bool wxBitmapHandler::Load(wxGDIImage *image,
717 const wxString& name,
718 long flags,
719 int width, int height)
720 {
721 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
722
723 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
724 }
725
726 bool wxBitmapHandler::Save(wxGDIImage *image,
727 const wxString& name,
728 int type)
729 {
730 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
731
732 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
733 }
734
735 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
736 void *WXUNUSED(data),
737 long WXUNUSED(type),
738 int WXUNUSED(width),
739 int WXUNUSED(height),
740 int WXUNUSED(depth))
741 {
742 return FALSE;
743 }
744
745 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
746 const wxString& WXUNUSED(name),
747 long WXUNUSED(type),
748 int WXUNUSED(desiredWidth),
749 int WXUNUSED(desiredHeight))
750 {
751 return FALSE;
752 }
753
754 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
755 const wxString& WXUNUSED(name),
756 int WXUNUSED(type),
757 const wxPalette *WXUNUSED(palette))
758 {
759 return FALSE;
760 }
761
762 // ----------------------------------------------------------------------------
763 // DIB functions
764 // ----------------------------------------------------------------------------
765
766 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
767 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
768 {
769 unsigned long i, headerSize;
770 LPBITMAPINFO lpDIBheader = NULL;
771 LPPALETTEENTRY lpPe = NULL;
772
773
774 // Allocate space for a DIB header
775 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
776 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
777 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
778
779 GetPaletteEntries(hPal, 0, 256, lpPe);
780
781 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
782
783 // Fill in the static parts of the DIB header
784 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
785 lpDIBheader->bmiHeader.biWidth = xSize;
786 lpDIBheader->bmiHeader.biHeight = ySize;
787 lpDIBheader->bmiHeader.biPlanes = 1;
788
789 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
790 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
791 lpDIBheader->bmiHeader.biCompression = BI_RGB;
792 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
793 lpDIBheader->bmiHeader.biClrUsed = 256;
794
795
796 // Initialize the DIB palette
797 for (i = 0; i < 256; i++) {
798 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
799 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
800 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
801 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
802 }
803
804 *lpDIBHeader = lpDIBheader;
805
806 return TRUE;
807 }
808
809 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
810 {
811 free(lpDIBHeader);
812 }
813
814 // ----------------------------------------------------------------------------
815 // other helper functions
816 // ----------------------------------------------------------------------------
817
818 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
819 {
820 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
821
822 // get width/height from the bitmap if not given
823 if ( !w || !h )
824 {
825 BITMAP bm;
826 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
827 w = bm.bmWidth;
828 h = bm.bmHeight;
829 }
830
831 HDC hdcSrc = ::CreateCompatibleDC(NULL);
832 HDC hdcDst = ::CreateCompatibleDC(NULL);
833 if ( !hdcSrc || !hdcDst )
834 {
835 wxLogLastError("CreateCompatibleDC");
836 }
837
838 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
839 if ( !hbmpInvMask )
840 {
841 wxLogLastError("CreateBitmap");
842 }
843
844 ::SelectObject(hdcSrc, hbmpMask);
845 ::SelectObject(hdcDst, hbmpInvMask);
846 if ( !::BitBlt(hdcDst, 0, 0, w, h,
847 hdcSrc, 0, 0,
848 NOTSRCCOPY) )
849 {
850 wxLogLastError("BitBlt");
851 }
852
853 ::DeleteDC(hdcSrc);
854 ::DeleteDC(hdcDst);
855
856 return hbmpInvMask;
857 }