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