]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
Changed test to reflect the real current state of dynamic loading
[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 "device.h"
44
45 #include "wx/msw/private.h"
46 #include "wx/log.h"
47
48 #if !defined(__WXMICROWIN__)
49 #include "wx/msw/dib.h"
50 #endif
51
52 #include "wx/image.h"
53 #include "wx/xpmdecod.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 // macros
62 // ----------------------------------------------------------------------------
63
64 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
65 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
66
67 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73 // ----------------------------------------------------------------------------
74 // wxBitmapRefData
75 // ----------------------------------------------------------------------------
76
77 wxBitmapRefData::wxBitmapRefData()
78 {
79 m_quality = 0;
80 m_selectedInto = NULL;
81 m_numColors = 0;
82 m_bitmapMask = NULL;
83 m_hBitmap = (WXHBITMAP) NULL;
84 }
85
86 void wxBitmapRefData::Free()
87 {
88 wxASSERT_MSG( !m_selectedInto,
89 wxT("deleting bitmap still selected into wxMemoryDC") );
90
91 if ( m_hBitmap)
92 {
93 // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap);
94 #if 1
95 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
96 {
97 wxLogLastError(wxT("DeleteObject(hbitmap)"));
98 }
99 #endif
100 }
101
102 delete m_bitmapMask;
103 m_bitmapMask = NULL;
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxBitmap creation
108 // ----------------------------------------------------------------------------
109
110 // this function should be called from all wxBitmap ctors
111 void wxBitmap::Init()
112 {
113 // m_refData = NULL; done in the base class ctor
114
115 }
116
117 #ifdef __WIN32__
118
119 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
120 {
121 #ifndef __WXMICROWIN__
122 // it may be either HICON or HCURSOR
123 HICON hicon = (HICON)icon.GetHandle();
124
125 ICONINFO iconInfo;
126 if ( !::GetIconInfo(hicon, &iconInfo) )
127 {
128 wxLogLastError(wxT("GetIconInfo"));
129
130 return FALSE;
131 }
132
133 wxBitmapRefData *refData = new wxBitmapRefData;
134 m_refData = refData;
135
136 int w = icon.GetWidth(),
137 h = icon.GetHeight();
138
139 refData->m_width = w;
140 refData->m_height = h;
141 refData->m_depth = wxDisplayDepth();
142
143 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
144
145 // the mask returned by GetIconInfo() is inversed compared to the usual
146 // wxWin convention
147 refData->m_bitmapMask = new wxMask((WXHBITMAP)
148 wxInvertMask(iconInfo.hbmMask, w, h));
149
150
151 // delete the old one now as we don't need it any more
152 ::DeleteObject(iconInfo.hbmMask);
153
154 #if WXWIN_COMPATIBILITY_2
155 refData->m_ok = TRUE;
156 #endif // WXWIN_COMPATIBILITY_2
157
158 return TRUE;
159 #else
160 return FALSE;
161 #endif
162 }
163
164 #endif // Win32
165
166 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
167 {
168 UnRef();
169
170 if ( !cursor.Ok() )
171 return FALSE;
172
173 #ifdef __WIN16__
174 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
175
176 return FALSE;
177 #else
178 return CopyFromIconOrCursor(cursor);
179 #endif // Win16
180 }
181
182 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
183 {
184 UnRef();
185
186 if ( !icon.Ok() )
187 return FALSE;
188
189 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
190 // to create a bitmap from icon there - but using this way we won't have
191 // the mask (FIXME)
192 #ifdef __WIN16__
193 int width = icon.GetWidth(),
194 height = icon.GetHeight();
195
196 // copy the icon to the bitmap
197 ScreenHDC hdcScreen;
198 HDC hdc = ::CreateCompatibleDC(hdcScreen);
199 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
200 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
201
202 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
203
204 ::SelectObject(hdc, hbmpOld);
205 ::DeleteDC(hdc);
206
207 wxBitmapRefData *refData = new wxBitmapRefData;
208 m_refData = refData;
209
210 refData->m_width = width;
211 refData->m_height = height;
212 refData->m_depth = wxDisplayDepth();
213
214 refData->m_hBitmap = (WXHBITMAP)hbitmap;
215
216 #if WXWIN_COMPATIBILITY_2
217 refData->m_ok = TRUE;
218 #endif // WXWIN_COMPATIBILITY_2
219
220 return TRUE;
221 #else // Win32
222 return CopyFromIconOrCursor(icon);
223 #endif // Win16/Win32
224 }
225
226 wxBitmap::~wxBitmap()
227 {
228 }
229
230 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
231 {
232 Init();
233
234 #ifndef __WXMICROWIN__
235 wxBitmapRefData *refData = new wxBitmapRefData;
236 m_refData = refData;
237
238 refData->m_width = width;
239 refData->m_height = height;
240 refData->m_depth = depth;
241 refData->m_numColors = 0;
242 refData->m_selectedInto = NULL;
243
244 char *data;
245 if ( depth == 1 )
246 {
247 // we assume that it is in XBM format which is not quite the same as
248 // the format CreateBitmap() wants because the order of bytes in the
249 // line is inversed!
250 const size_t bytesPerLine = (width + 7) / 8;
251 const size_t padding = bytesPerLine % 2;
252 const size_t len = height * ( padding + bytesPerLine );
253 data = (char *)malloc(len);
254 const char *src = bits;
255 char *dst = data;
256
257 for ( int rows = 0; rows < height; rows++ )
258 {
259 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
260 {
261 unsigned char val = *src++;
262 unsigned char reversed = 0;
263
264 for ( int bits = 0; bits < 8; bits++)
265 {
266 reversed <<= 1;
267 reversed |= (val & 0x01);
268 val >>= 1;
269 }
270 *dst++ = reversed;
271 }
272
273 if ( padding )
274 *dst++ = 0;
275 }
276 }
277 else
278 {
279 // bits should already be in Windows standard format
280 data = (char *)bits; // const_cast is harmless
281 }
282
283 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
284 if ( !hbmp )
285 {
286 wxLogLastError(wxT("CreateBitmap"));
287 }
288
289 if ( data != bits )
290 {
291 free(data);
292 }
293
294 SetHBITMAP((WXHBITMAP)hbmp);
295 #endif
296 }
297
298 // Create from XPM data
299 bool wxBitmap::CreateFromXpm(const char **data)
300 {
301 #if wxUSE_IMAGE && wxUSE_XPM
302 Init();
303
304 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
305
306 wxXPMDecoder decoder;
307 wxImage img = decoder.ReadData(data);
308 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
309
310 *this = wxBitmap(img);
311 return TRUE;
312 #else
313 return FALSE;
314 #endif
315 }
316
317 wxBitmap::wxBitmap(int w, int h, int d)
318 {
319 Init();
320
321 (void)Create(w, h, d);
322 }
323
324 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
325 {
326 Init();
327
328 (void)Create(data, type, width, height, depth);
329 }
330
331 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
332 {
333 Init();
334
335 LoadFile(filename, (int)type);
336 }
337
338 bool wxBitmap::Create(int w, int h, int d)
339 {
340 UnRef();
341
342 m_refData = new wxBitmapRefData;
343
344 GetBitmapData()->m_width = w;
345 GetBitmapData()->m_height = h;
346 GetBitmapData()->m_depth = d;
347
348 HBITMAP hbmp;
349 #ifndef __WXMICROWIN__
350 if ( d > 0 )
351 {
352 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
353 if ( !hbmp )
354 {
355 wxLogLastError(wxT("CreateBitmap"));
356 }
357 }
358 else
359 #endif
360 {
361 ScreenHDC dc;
362 hbmp = ::CreateCompatibleBitmap(dc, w, h);
363 if ( !hbmp )
364 {
365 wxLogLastError(wxT("CreateCompatibleBitmap"));
366 }
367
368 GetBitmapData()->m_depth = wxDisplayDepth();
369 }
370
371 SetHBITMAP((WXHBITMAP)hbmp);
372
373 #if WXWIN_COMPATIBILITY_2
374 GetBitmapData()->m_ok = hbmp != 0;
375 #endif // WXWIN_COMPATIBILITY_2
376 return Ok();
377 }
378
379 // ----------------------------------------------------------------------------
380 // wxImage to/from conversions
381 // ----------------------------------------------------------------------------
382
383 #if wxUSE_IMAGE
384
385 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
386 {
387 #ifdef __WXMICROWIN__
388
389 // Set this to 1 to experiment with mask code,
390 // which currently doesn't work
391 #define USE_MASKS 0
392
393 m_refData = new wxBitmapRefData();
394
395 // Initial attempt at a simple-minded implementation.
396 // The bitmap will always be created at the screen depth,
397 // so the 'depth' argument is ignored.
398
399 HDC hScreenDC = ::GetDC(NULL);
400 // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp);
401 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
402
403 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
404 HBITMAP hMaskBitmap = NULL;
405 HBITMAP hOldMaskBitmap = NULL;
406 HDC hMaskDC = NULL;
407 unsigned char maskR = 0;
408 unsigned char maskG = 0;
409 unsigned char maskB = 0;
410
411 // printf("Created bitmap %d\n", (int) hBitmap);
412 if (hBitmap == NULL)
413 {
414 ::ReleaseDC(NULL, hScreenDC);
415 return FALSE;
416 }
417 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
418
419 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
420 ::ReleaseDC(NULL, hScreenDC);
421
422 // created an mono-bitmap for the possible mask
423 bool hasMask = image.HasMask();
424
425 if ( hasMask )
426 {
427 #if USE_MASKS
428 // FIXME: we should be able to pass bpp = 1, but
429 // GdBlit can't handle a different depth
430 #if 0
431 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
432 #else
433 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
434 #endif
435 maskR = image.GetMaskRed();
436 maskG = image.GetMaskGreen();
437 maskB = image.GetMaskBlue();
438
439 if (!hMaskBitmap)
440 {
441 hasMask = FALSE;
442 }
443 else
444 {
445 hScreenDC = ::GetDC(NULL);
446 hMaskDC = ::CreateCompatibleDC(hScreenDC);
447 ::ReleaseDC(NULL, hScreenDC);
448
449 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
450 }
451 #else
452 hasMask = FALSE;
453 #endif
454 }
455
456 int i, j;
457 for (i = 0; i < image.GetWidth(); i++)
458 {
459 for (j = 0; j < image.GetHeight(); j++)
460 {
461 unsigned char red = image.GetRed(i, j);
462 unsigned char green = image.GetGreen(i, j);
463 unsigned char blue = image.GetBlue(i, j);
464
465 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
466
467 if (hasMask)
468 {
469 // scan the bitmap for the transparent colour and set the corresponding
470 // pixels in the mask to BLACK and the rest to WHITE
471 if (maskR == red && maskG == green && maskB == blue)
472 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
473 else
474 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
475 }
476 }
477 }
478
479 ::SelectObject(hMemDC, hOldBitmap);
480 ::DeleteDC(hMemDC);
481 if (hasMask)
482 {
483 ::SelectObject(hMaskDC, hOldMaskBitmap);
484 ::DeleteDC(hMaskDC);
485
486 ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap);
487 }
488
489 SetWidth(image.GetWidth());
490 SetHeight(image.GetHeight());
491 SetDepth(screenDepth);
492 SetHBITMAP( (WXHBITMAP) hBitmap );
493
494 #if wxUSE_PALETTE
495 // Copy the palette from the source image
496 SetPalette(image.GetPalette());
497 #endif // wxUSE_PALETTE
498
499 #if WXWIN_COMPATIBILITY_2
500 // check the wxBitmap object
501 GetBitmapData()->SetOk();
502 #endif // WXWIN_COMPATIBILITY_2
503
504 return TRUE;
505
506 #else
507 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
508
509 m_refData = new wxBitmapRefData();
510
511 // sizeLimit is the MS upper limit for the DIB size
512 #ifdef WIN32
513 int sizeLimit = 1024*768*3;
514 #else
515 int sizeLimit = 0x7fff ;
516 #endif
517
518 // width and height of the device-dependent bitmap
519 int width = image.GetWidth();
520 int bmpHeight = image.GetHeight();
521
522 // calc the number of bytes per scanline and padding
523 int bytePerLine = width*3;
524 int sizeDWORD = sizeof( DWORD );
525 int lineBoundary = bytePerLine % sizeDWORD;
526 int padding = 0;
527 if( lineBoundary > 0 )
528 {
529 padding = sizeDWORD - lineBoundary;
530 bytePerLine += padding;
531 }
532 // calc the number of DIBs and heights of DIBs
533 int numDIB = 1;
534 int hRemain = 0;
535 int height = sizeLimit/bytePerLine;
536 if( height >= bmpHeight )
537 height = bmpHeight;
538 else
539 {
540 numDIB = bmpHeight / height;
541 hRemain = bmpHeight % height;
542 if( hRemain >0 ) numDIB++;
543 }
544
545 // set bitmap parameters
546 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
547 SetWidth( width );
548 SetHeight( bmpHeight );
549 if (depth == -1) depth = wxDisplayDepth();
550 SetDepth( depth );
551
552 #if wxUSE_PALETTE
553 // Copy the palette from the source image
554 SetPalette(image.GetPalette());
555 #endif // wxUSE_PALETTE
556
557 // create a DIB header
558 int headersize = sizeof(BITMAPINFOHEADER);
559 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
560 wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
561 // Fill in the DIB header
562 lpDIBh->bmiHeader.biSize = headersize;
563 lpDIBh->bmiHeader.biWidth = (DWORD)width;
564 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
565 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
566 // the general formula for biSizeImage:
567 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
568 lpDIBh->bmiHeader.biPlanes = 1;
569 lpDIBh->bmiHeader.biBitCount = 24;
570 lpDIBh->bmiHeader.biCompression = BI_RGB;
571 lpDIBh->bmiHeader.biClrUsed = 0;
572 // These seem not really needed for our purpose here.
573 lpDIBh->bmiHeader.biClrImportant = 0;
574 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
575 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
576 // memory for DIB data
577 unsigned char *lpBits;
578 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
579 if( !lpBits )
580 {
581 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
582 free( lpDIBh );
583 return FALSE;
584 }
585
586 // create and set the device-dependent bitmap
587 HDC hdc = ::GetDC(NULL);
588 HDC memdc = ::CreateCompatibleDC( hdc );
589 HBITMAP hbitmap;
590 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
591 ::SelectObject( memdc, hbitmap);
592
593 #if wxUSE_PALETTE
594 HPALETTE hOldPalette = 0;
595 if (image.GetPalette().Ok())
596 {
597 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
598 ::RealizePalette(memdc);
599 }
600 #endif // wxUSE_PALETTE
601
602 // copy image data into DIB data and then into DDB (in a loop)
603 unsigned char *data = image.GetData();
604 int i, j, n;
605 int origin = 0;
606 unsigned char *ptdata = data;
607 unsigned char *ptbits;
608
609 for( n=0; n<numDIB; n++ )
610 {
611 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
612 {
613 // redefine height and size of the (possibly) last smaller DIB
614 // memory is not reallocated
615 height = hRemain;
616 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
617 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
618 }
619 ptbits = lpBits;
620
621 for( j=0; j<height; j++ )
622 {
623 for( i=0; i<width; i++ )
624 {
625 *(ptbits++) = *(ptdata+2);
626 *(ptbits++) = *(ptdata+1);
627 *(ptbits++) = *(ptdata );
628 ptdata += 3;
629 }
630 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
631 }
632 ::StretchDIBits( memdc, 0, origin, width, height,\
633 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
634 origin += height;
635 // if numDIB = 1, lines below can also be used
636 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
637 // The above line is equivalent to the following two lines.
638 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
639 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
640 // or the following lines
641 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
642 // HDC memdc = ::CreateCompatibleDC( hdc );
643 // ::SelectObject( memdc, hbitmap);
644 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
645 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
646 // ::SelectObject( memdc, 0 );
647 // ::DeleteDC( memdc );
648 }
649 SetHBITMAP( (WXHBITMAP) hbitmap );
650
651 #if wxUSE_PALETTE
652 if (hOldPalette)
653 SelectPalette(memdc, hOldPalette, FALSE);
654 #endif // wxUSE_PALETTE
655
656 // similarly, created an mono-bitmap for the possible mask
657 if( image.HasMask() )
658 {
659 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
660 HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
661 if( numDIB == 1 ) height = bmpHeight;
662 else height = sizeLimit/bytePerLine;
663 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
664 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
665 origin = 0;
666 unsigned char r = image.GetMaskRed();
667 unsigned char g = image.GetMaskGreen();
668 unsigned char b = image.GetMaskBlue();
669 unsigned char zero = 0, one = 255;
670 ptdata = data;
671 for( n=0; n<numDIB; n++ )
672 {
673 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
674 {
675 // redefine height and size of the (possibly) last smaller DIB
676 // memory is not reallocated
677 height = hRemain;
678 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
679 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
680 }
681 ptbits = lpBits;
682 for( int j=0; j<height; j++ )
683 {
684 for(i=0; i<width; i++ )
685 {
686 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
687 unsigned char cr = (*(ptdata++)) ;
688 unsigned char cg = (*(ptdata++)) ;
689 unsigned char cb = (*(ptdata++)) ;
690
691 if( ( cr !=r) || (cg!=g) || (cb!=b) )
692 {
693 *(ptbits++) = one;
694 *(ptbits++) = one;
695 *(ptbits++) = one;
696 }
697 else
698 {
699 *(ptbits++) = zero;
700 *(ptbits++) = zero;
701 *(ptbits++) = zero;
702 }
703 }
704 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
705 }
706 ::StretchDIBits( memdc, 0, origin, width, height,\
707 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
708 origin += height;
709 }
710 // create a wxMask object
711 wxMask *mask = new wxMask();
712 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
713 SetMask( mask );
714 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
715 /* The following can also be used but is slow to run
716 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
717 wxMask *mask = new wxMask( *this, colour );
718 SetMask( mask );
719 */
720
721 ::SelectObject( memdc, hbmpOld );
722 }
723
724 // free allocated resources
725 ::DeleteDC( memdc );
726 ::ReleaseDC(NULL, hdc);
727 free(lpDIBh);
728 free(lpBits);
729
730 #if WXWIN_COMPATIBILITY_2
731 // check the wxBitmap object
732 GetBitmapData()->SetOk();
733 #endif // WXWIN_COMPATIBILITY_2
734
735 return TRUE;
736 #endif
737 }
738
739 wxImage wxBitmap::ConvertToImage() const
740 {
741 #ifdef __WXMICROWIN__
742 // Initial attempt at a simple-minded implementation.
743 // The bitmap will always be created at the screen depth,
744 // so the 'depth' argument is ignored.
745 // TODO: transparency (create a mask image)
746
747 if (!Ok())
748 {
749 wxFAIL_MSG( wxT("bitmap is invalid") );
750 return wxNullImage;
751 }
752
753 wxImage image;
754
755 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
756
757 // create an wxImage object
758 int width = GetWidth();
759 int height = GetHeight();
760 image.Create( width, height );
761 unsigned char *data = image.GetData();
762 if( !data )
763 {
764 wxFAIL_MSG( wxT("could not allocate data for image") );
765 return wxNullImage;
766 }
767
768 HDC hScreenDC = ::GetDC(NULL);
769
770 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
771 ::ReleaseDC(NULL, hScreenDC);
772
773 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
774
775 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
776
777 int i, j;
778 for (i = 0; i < GetWidth(); i++)
779 {
780 for (j = 0; j < GetHeight(); j++)
781 {
782 COLORREF color = ::GetPixel(hMemDC, i, j);
783 unsigned char red = GetRValue(color);
784 unsigned char green = GetGValue(color);
785 unsigned char blue = GetBValue(color);
786
787 image.SetRGB(i, j, red, green, blue);
788 }
789 }
790
791 ::SelectObject(hMemDC, hOldBitmap);
792 ::DeleteDC(hMemDC);
793
794 #if wxUSE_PALETTE
795 // Copy the palette from the source image
796 if (GetPalette())
797 image.SetPalette(* GetPalette());
798 #endif // wxUSE_PALETTE
799
800 return image;
801
802 #else // __MICROWIN__
803
804 wxImage image;
805
806 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
807
808 // create an wxImage object
809 int width = GetWidth();
810 int height = GetHeight();
811 image.Create( width, height );
812 unsigned char *data = image.GetData();
813 if( !data )
814 {
815 wxFAIL_MSG( wxT("could not allocate data for image") );
816 return wxNullImage;
817 }
818
819 // calc the number of bytes per scanline and padding in the DIB
820 int bytePerLine = width*3;
821 int sizeDWORD = sizeof( DWORD );
822 int lineBoundary = bytePerLine % sizeDWORD;
823 int padding = 0;
824 if( lineBoundary > 0 )
825 {
826 padding = sizeDWORD - lineBoundary;
827 bytePerLine += padding;
828 }
829
830 // create a DIB header
831 int headersize = sizeof(BITMAPINFOHEADER);
832 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
833 if( !lpDIBh )
834 {
835 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
836 free( data );
837 return wxNullImage;
838 }
839 // Fill in the DIB header
840 lpDIBh->bmiHeader.biSize = headersize;
841 lpDIBh->bmiHeader.biWidth = width;
842 lpDIBh->bmiHeader.biHeight = -height;
843 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
844 lpDIBh->bmiHeader.biPlanes = 1;
845 lpDIBh->bmiHeader.biBitCount = 24;
846 lpDIBh->bmiHeader.biCompression = BI_RGB;
847 lpDIBh->bmiHeader.biClrUsed = 0;
848 // These seem not really needed for our purpose here.
849 lpDIBh->bmiHeader.biClrImportant = 0;
850 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
851 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
852 // memory for DIB data
853 unsigned char *lpBits;
854 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
855 if( !lpBits )
856 {
857 wxFAIL_MSG( wxT("could not allocate data for DIB") );
858 free( data );
859 free( lpDIBh );
860 return wxNullImage;
861 }
862
863 // copy data from the device-dependent bitmap to the DIB
864 HDC hdc = ::GetDC(NULL);
865 HBITMAP hbitmap;
866 hbitmap = (HBITMAP) GetHBITMAP();
867 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
868
869 // copy DIB data into the wxImage object
870 int i, j;
871 unsigned char *ptdata = data;
872 unsigned char *ptbits = lpBits;
873 for( i=0; i<height; i++ )
874 {
875 for( j=0; j<width; j++ )
876 {
877 *(ptdata++) = *(ptbits+2);
878 *(ptdata++) = *(ptbits+1);
879 *(ptdata++) = *(ptbits );
880 ptbits += 3;
881 }
882 ptbits += padding;
883 }
884
885 // similarly, set data according to the possible mask bitmap
886 if( GetMask() && GetMask()->GetMaskBitmap() )
887 {
888 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
889 // memory DC created, color set, data copied, and memory DC deleted
890 HDC memdc = ::CreateCompatibleDC( hdc );
891 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
892 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
893 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
894 ::DeleteDC( memdc );
895 // background color set to RGB(16,16,16) in consistent with wxGTK
896 unsigned char r=16, g=16, b=16;
897 ptdata = data;
898 ptbits = lpBits;
899 for( i=0; i<height; i++ )
900 {
901 for( j=0; j<width; j++ )
902 {
903 if( *ptbits != 0 )
904 ptdata += 3;
905 else
906 {
907 *(ptdata++) = r;
908 *(ptdata++) = g;
909 *(ptdata++) = b;
910 }
911 ptbits += 3;
912 }
913 ptbits += padding;
914 }
915 image.SetMaskColour( r, g, b );
916 image.SetMask( TRUE );
917 }
918 else
919 {
920 image.SetMask( FALSE );
921 }
922 // free allocated resources
923 ::ReleaseDC(NULL, hdc);
924 free(lpDIBh);
925 free(lpBits);
926
927 return image;
928 #endif
929 }
930
931 #endif // wxUSE_IMAGE
932
933 bool wxBitmap::LoadFile(const wxString& filename, long type)
934 {
935 UnRef();
936
937 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
938
939 if ( handler )
940 {
941 m_refData = new wxBitmapRefData;
942
943 return handler->LoadFile(this, filename, type, -1, -1);
944 }
945 #if wxUSE_IMAGE
946 else
947 {
948 wxImage image;
949 if ( image.LoadFile( filename, type ) && image.Ok() )
950 {
951 *this = image.ConvertToBitmap();
952
953 return TRUE;
954 }
955 }
956 #endif // wxUSE_IMAGE
957
958 return FALSE;
959 }
960
961 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
962 {
963 UnRef();
964
965 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
966
967 if ( !handler )
968 {
969 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
970
971 return FALSE;
972 }
973
974 m_refData = new wxBitmapRefData;
975
976 return handler->Create(this, data, type, width, height, depth);
977 }
978
979 bool wxBitmap::SaveFile(const wxString& filename,
980 int type,
981 const wxPalette *palette)
982 {
983 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
984
985 if ( handler )
986 {
987 return handler->SaveFile(this, filename, type, palette);
988 }
989 #if wxUSE_IMAGE
990 else
991 {
992 // FIXME what about palette? shouldn't we use it?
993 wxImage image( *this );
994 if ( image.Ok() )
995 {
996 return image.SaveFile(filename, type);
997 }
998 }
999 #endif // wxUSE_IMAGE
1000
1001 return FALSE;
1002 }
1003
1004 // ----------------------------------------------------------------------------
1005 // sub bitmap extraction
1006 // ----------------------------------------------------------------------------
1007
1008 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1009 {
1010 #ifndef __WXMICROWIN__
1011 wxCHECK_MSG( Ok() &&
1012 (rect.x >= 0) && (rect.y >= 0) &&
1013 (rect.x+rect.width <= GetWidth()) &&
1014 (rect.y+rect.height <= GetHeight()),
1015 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1016
1017 wxBitmap ret( rect.width, rect.height, GetDepth() );
1018 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1019
1020 // copy bitmap data
1021 HDC dcSrc = ::CreateCompatibleDC(NULL);
1022 HDC dcDst = ::CreateCompatibleDC(NULL);
1023 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
1024 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
1025 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
1026
1027 // copy mask if there is one
1028 if (GetMask())
1029 {
1030 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1031
1032 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
1033 SelectObject(dcDst, (HBITMAP) hbmpMask);
1034 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
1035
1036 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1037 ret.SetMask(mask);
1038 }
1039
1040 SelectObject(dcDst, NULL);
1041 SelectObject(dcSrc, NULL);
1042 DeleteDC(dcDst);
1043 DeleteDC(dcSrc);
1044
1045 return ret;
1046 #else
1047 return wxBitmap();
1048 #endif
1049 }
1050
1051 // ----------------------------------------------------------------------------
1052 // wxBitmap accessors
1053 // ----------------------------------------------------------------------------
1054
1055 void wxBitmap::SetQuality(int q)
1056 {
1057 EnsureHasData();
1058
1059 GetBitmapData()->m_quality = q;
1060 }
1061
1062 #if WXWIN_COMPATIBILITY_2
1063 void wxBitmap::SetOk(bool isOk)
1064 {
1065 EnsureHasData();
1066
1067 GetBitmapData()->m_ok = isOk;
1068 }
1069 #endif // WXWIN_COMPATIBILITY_2
1070
1071 #if wxUSE_PALETTE
1072
1073 void wxBitmap::SetPalette(const wxPalette& palette)
1074 {
1075 EnsureHasData();
1076
1077 GetBitmapData()->m_bitmapPalette = palette;
1078 }
1079
1080 #endif // wxUSE_PALETTE
1081
1082 void wxBitmap::SetMask(wxMask *mask)
1083 {
1084 EnsureHasData();
1085
1086 GetBitmapData()->m_bitmapMask = mask;
1087 }
1088
1089 // Creates a bitmap that matches the device context, from
1090 // an arbitray bitmap. At present, the original bitmap must have an
1091 // associated palette. TODO: use a default palette if no palette exists.
1092 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1093 wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
1094 {
1095 #ifdef __WXMICROWIN__
1096 return *this;
1097 #else
1098 wxMemoryDC memDC;
1099 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
1100 HPALETTE hPal = (HPALETTE) NULL;
1101 LPBITMAPINFO lpDib;
1102 void *lpBits = (void*) NULL;
1103
1104 #if wxUSE_PALETTE
1105 if( GetPalette() && GetPalette()->Ok() )
1106 {
1107 tmpBitmap.SetPalette(*GetPalette());
1108 memDC.SelectObject(tmpBitmap);
1109 memDC.SetPalette(*GetPalette());
1110 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
1111 }
1112 else
1113 {
1114 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1115 wxPalette palette;
1116 palette.SetHPALETTE( (WXHPALETTE)hPal );
1117 tmpBitmap.SetPalette( palette );
1118 memDC.SelectObject(tmpBitmap);
1119 memDC.SetPalette( palette );
1120 }
1121 #else // !wxUSE_PALETTE
1122 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1123 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1124
1125 // set the height negative because in a DIB the order of the lines is
1126 // reversed
1127 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
1128 {
1129 return wxNullBitmap;
1130 }
1131
1132 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
1133
1134 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
1135
1136 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
1137 GetWidth(), GetHeight(),
1138 0, 0, 0, GetHeight(),
1139 lpBits, lpDib, DIB_RGB_COLORS);
1140
1141 free(lpBits);
1142
1143 wxFreeDIB(lpDib);
1144
1145 return tmpBitmap;
1146 #endif
1147 }
1148
1149 // ----------------------------------------------------------------------------
1150 // wxMask
1151 // ----------------------------------------------------------------------------
1152
1153 wxMask::wxMask()
1154 {
1155 m_maskBitmap = 0;
1156 }
1157
1158 // Construct a mask from a bitmap and a colour indicating
1159 // the transparent area
1160 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1161 {
1162 m_maskBitmap = 0;
1163 Create(bitmap, colour);
1164 }
1165
1166 // Construct a mask from a bitmap and a palette index indicating
1167 // the transparent area
1168 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1169 {
1170 m_maskBitmap = 0;
1171 Create(bitmap, paletteIndex);
1172 }
1173
1174 // Construct a mask from a mono bitmap (copies the bitmap).
1175 wxMask::wxMask(const wxBitmap& bitmap)
1176 {
1177 m_maskBitmap = 0;
1178 Create(bitmap);
1179 }
1180
1181 wxMask::~wxMask()
1182 {
1183 if ( m_maskBitmap )
1184 ::DeleteObject((HBITMAP) m_maskBitmap);
1185 }
1186
1187 // Create a mask from a mono bitmap (copies the bitmap).
1188 bool wxMask::Create(const wxBitmap& bitmap)
1189 {
1190 #ifndef __WXMICROWIN__
1191 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1192 _T("can't create mask from invalid or not monochrome bitmap") );
1193
1194 if ( m_maskBitmap )
1195 {
1196 ::DeleteObject((HBITMAP) m_maskBitmap);
1197 m_maskBitmap = 0;
1198 }
1199
1200 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1201 bitmap.GetWidth(),
1202 bitmap.GetHeight(),
1203 1, 1, 0
1204 );
1205 HDC srcDC = CreateCompatibleDC(0);
1206 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1207 HDC destDC = CreateCompatibleDC(0);
1208 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1209 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1210 SelectObject(srcDC, 0);
1211 DeleteDC(srcDC);
1212 SelectObject(destDC, 0);
1213 DeleteDC(destDC);
1214 return TRUE;
1215 #else
1216 return FALSE;
1217 #endif
1218 }
1219
1220 // Create a mask from a bitmap and a palette index indicating
1221 // the transparent area
1222 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1223 {
1224 if ( m_maskBitmap )
1225 {
1226 ::DeleteObject((HBITMAP) m_maskBitmap);
1227 m_maskBitmap = 0;
1228 }
1229
1230 #if wxUSE_PALETTE
1231 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1232 {
1233 unsigned char red, green, blue;
1234 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1235 {
1236 wxColour transparentColour(red, green, blue);
1237 return Create(bitmap, transparentColour);
1238 }
1239 }
1240 #endif // wxUSE_PALETTE
1241
1242 return FALSE;
1243 }
1244
1245 // Create a mask from a bitmap and a colour indicating
1246 // the transparent area
1247 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1248 {
1249 #ifndef __WXMICROWIN__
1250 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1251
1252 if ( m_maskBitmap )
1253 {
1254 ::DeleteObject((HBITMAP) m_maskBitmap);
1255 m_maskBitmap = 0;
1256 }
1257
1258 int width = bitmap.GetWidth(),
1259 height = bitmap.GetHeight();
1260
1261 // scan the bitmap for the transparent colour and set the corresponding
1262 // pixels in the mask to BLACK and the rest to WHITE
1263 COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue());
1264 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1265
1266 HDC srcDC = ::CreateCompatibleDC(NULL);
1267 HDC destDC = ::CreateCompatibleDC(NULL);
1268 if ( !srcDC || !destDC )
1269 {
1270 wxLogLastError(wxT("CreateCompatibleDC"));
1271 }
1272
1273 bool ok = TRUE;
1274
1275 // SelectObject() will fail
1276 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1277 _T("bitmap can't be selected in another DC") );
1278
1279 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1280 if ( !hbmpSrcOld )
1281 {
1282 wxLogLastError(wxT("SelectObject"));
1283
1284 ok = FALSE;
1285 }
1286
1287 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1288 if ( !hbmpDstOld )
1289 {
1290 wxLogLastError(wxT("SelectObject"));
1291
1292 ok = FALSE;
1293 }
1294
1295 if ( ok )
1296 {
1297 // this will create a monochrome bitmap with 0 points for the pixels
1298 // which have the same value as the background colour and 1 for the
1299 // others
1300 ::SetBkColor(srcDC, maskColour);
1301 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
1302 }
1303
1304 ::SelectObject(srcDC, hbmpSrcOld);
1305 ::DeleteDC(srcDC);
1306 ::SelectObject(destDC, hbmpDstOld);
1307 ::DeleteDC(destDC);
1308
1309 return ok;
1310 #else // __WXMICROWIN__
1311 return FALSE;
1312 #endif // __WXMICROWIN__/!__WXMICROWIN__
1313 }
1314
1315 // ----------------------------------------------------------------------------
1316 // wxBitmapHandler
1317 // ----------------------------------------------------------------------------
1318
1319 bool wxBitmapHandler::Create(wxGDIImage *image,
1320 void *data,
1321 long flags,
1322 int width, int height, int depth)
1323 {
1324 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1325
1326 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1327 }
1328
1329 bool wxBitmapHandler::Load(wxGDIImage *image,
1330 const wxString& name,
1331 long flags,
1332 int width, int height)
1333 {
1334 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1335
1336 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1337 }
1338
1339 bool wxBitmapHandler::Save(wxGDIImage *image,
1340 const wxString& name,
1341 int type)
1342 {
1343 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1344
1345 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1346 }
1347
1348 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1349 void *WXUNUSED(data),
1350 long WXUNUSED(type),
1351 int WXUNUSED(width),
1352 int WXUNUSED(height),
1353 int WXUNUSED(depth))
1354 {
1355 return FALSE;
1356 }
1357
1358 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1359 const wxString& WXUNUSED(name),
1360 long WXUNUSED(type),
1361 int WXUNUSED(desiredWidth),
1362 int WXUNUSED(desiredHeight))
1363 {
1364 return FALSE;
1365 }
1366
1367 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1368 const wxString& WXUNUSED(name),
1369 int WXUNUSED(type),
1370 const wxPalette *WXUNUSED(palette))
1371 {
1372 return FALSE;
1373 }
1374
1375 // ----------------------------------------------------------------------------
1376 // DIB functions
1377 // ----------------------------------------------------------------------------
1378
1379 #ifndef __WXMICROWIN__
1380 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1381 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1382 {
1383 unsigned long i, headerSize;
1384 LPBITMAPINFO lpDIBheader = NULL;
1385 LPPALETTEENTRY lpPe = NULL;
1386
1387
1388 // Allocate space for a DIB header
1389 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1390 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1391 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1392
1393 GetPaletteEntries(hPal, 0, 256, lpPe);
1394
1395 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1396
1397 // Fill in the static parts of the DIB header
1398 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1399 lpDIBheader->bmiHeader.biWidth = xSize;
1400 lpDIBheader->bmiHeader.biHeight = ySize;
1401 lpDIBheader->bmiHeader.biPlanes = 1;
1402
1403 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1404 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1405 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1406 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1407 lpDIBheader->bmiHeader.biClrUsed = 256;
1408
1409
1410 // Initialize the DIB palette
1411 for (i = 0; i < 256; i++) {
1412 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1413 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1414 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1415 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1416 }
1417
1418 *lpDIBHeader = lpDIBheader;
1419
1420 return TRUE;
1421 }
1422
1423 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1424 {
1425 free(lpDIBHeader);
1426 }
1427 #endif
1428
1429 // ----------------------------------------------------------------------------
1430 // other helper functions
1431 // ----------------------------------------------------------------------------
1432
1433 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1434 {
1435 #ifndef __WXMICROWIN__
1436 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1437
1438 // get width/height from the bitmap if not given
1439 if ( !w || !h )
1440 {
1441 BITMAP bm;
1442 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1443 w = bm.bmWidth;
1444 h = bm.bmHeight;
1445 }
1446
1447 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1448 HDC hdcDst = ::CreateCompatibleDC(NULL);
1449 if ( !hdcSrc || !hdcDst )
1450 {
1451 wxLogLastError(wxT("CreateCompatibleDC"));
1452 }
1453
1454 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1455 if ( !hbmpInvMask )
1456 {
1457 wxLogLastError(wxT("CreateBitmap"));
1458 }
1459
1460 ::SelectObject(hdcSrc, hbmpMask);
1461 ::SelectObject(hdcDst, hbmpInvMask);
1462 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1463 hdcSrc, 0, 0,
1464 NOTSRCCOPY) )
1465 {
1466 wxLogLastError(wxT("BitBlt"));
1467 }
1468
1469 ::DeleteDC(hdcSrc);
1470 ::DeleteDC(hdcDst);
1471
1472 return hbmpInvMask;
1473 #else
1474 return 0;
1475 #endif
1476 }