]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
Updated to new PyCrust
[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
07cf98cb
VZ
110 if ( wxTheBitmapList )
111 wxTheBitmapList->AddBitmap(this);
4fe5383d
VZ
112}
113
6d167489
VZ
114#ifdef __WIN32__
115
116bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
117{
04ef50df 118#ifndef __WXMICROWIN__
6d167489
VZ
119 // it may be either HICON or HCURSOR
120 HICON hicon = (HICON)icon.GetHandle();
121
122 ICONINFO iconInfo;
123 if ( !::GetIconInfo(hicon, &iconInfo) )
124 {
f6bcfd97 125 wxLogLastError(wxT("GetIconInfo"));
6d167489
VZ
126
127 return FALSE;
128 }
129
130 wxBitmapRefData *refData = new wxBitmapRefData;
131 m_refData = refData;
132
d9c8e68e
VZ
133 int w = icon.GetWidth(),
134 h = icon.GetHeight();
135
136 refData->m_width = w;
137 refData->m_height = h;
6d167489
VZ
138 refData->m_depth = wxDisplayDepth();
139
140 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
d9c8e68e
VZ
141
142 // the mask returned by GetIconInfo() is inversed compared to the usual
143 // wxWin convention
4b7f2165
VZ
144 refData->m_bitmapMask = new wxMask((WXHBITMAP)
145 wxInvertMask(iconInfo.hbmMask, w, h));
6d167489
VZ
146
147#if WXWIN_COMPATIBILITY_2
148 refData->m_ok = TRUE;
149#endif // WXWIN_COMPATIBILITY_2
150
151 return TRUE;
04ef50df
JS
152#else
153 return FALSE;
154#endif
6d167489
VZ
155}
156
157#endif // Win32
158
159bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
4fe5383d
VZ
160{
161 UnRef();
07cf98cb 162
6d167489 163 if ( !cursor.Ok() )
4fe5383d 164 return FALSE;
07cf98cb 165
6d167489
VZ
166#ifdef __WIN16__
167 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
168
169 return FALSE;
8f177c8e 170#else
6d167489 171 return CopyFromIconOrCursor(cursor);
8f177c8e 172#endif // Win16
6d167489
VZ
173}
174
175bool wxBitmap::CopyFromIcon(const wxIcon& icon)
176{
177 UnRef();
07cf98cb 178
6d167489
VZ
179 if ( !icon.Ok() )
180 return FALSE;
4fe5383d
VZ
181
182 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
183 // to create a bitmap from icon there - but using this way we won't have
184 // the mask (FIXME)
185#ifdef __WIN16__
6d167489
VZ
186 int width = icon.GetWidth(),
187 height = icon.GetHeight();
188
4fe5383d 189 // copy the icon to the bitmap
6d167489 190 ScreenHDC hdcScreen;
4fe5383d
VZ
191 HDC hdc = ::CreateCompatibleDC(hdcScreen);
192 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
07cf98cb
VZ
193 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
194
6d167489 195 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
07cf98cb
VZ
196
197 ::SelectObject(hdc, hbmpOld);
198 ::DeleteDC(hdc);
4fe5383d 199
6d167489
VZ
200 wxBitmapRefData *refData = new wxBitmapRefData;
201 m_refData = refData;
4fe5383d 202
6d167489
VZ
203 refData->m_width = width;
204 refData->m_height = height;
205 refData->m_depth = wxDisplayDepth();
07cf98cb 206
6d167489 207 refData->m_hBitmap = (WXHBITMAP)hbitmap;
07cf98cb 208
6d167489
VZ
209#if WXWIN_COMPATIBILITY_2
210 refData->m_ok = TRUE;
211#endif // WXWIN_COMPATIBILITY_2
222594ea 212
4fe5383d 213 return TRUE;
6d167489
VZ
214#else // Win32
215 return CopyFromIconOrCursor(icon);
216#endif // Win16/Win32
10fcf31a
VZ
217}
218
219wxBitmap::~wxBitmap()
2bda0e17
KB
220{
221 if (wxTheBitmapList)
222 wxTheBitmapList->DeleteObject(this);
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
VS
615 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
616
617 return TRUE;
8cb172b4 618#endif
fec19ea9
VS
619}
620
621wxImage wxBitmap::ConvertToImage() const
622{
8cb172b4
JS
623#ifdef __WXMICROWIN__
624 // TODO
625 return wxImage();
626#else
fec19ea9 627 wxImage image;
6d51f220 628
fec19ea9
VS
629 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
630
631 // create an wxImage object
632 int width = GetWidth();
633 int height = GetHeight();
634 image.Create( width, height );
635 unsigned char *data = image.GetData();
636 if( !data )
637 {
638 wxFAIL_MSG( wxT("could not allocate data for image") );
639 return wxNullImage;
640 }
641
642 // calc the number of bytes per scanline and padding in the DIB
643 int bytePerLine = width*3;
644 int sizeDWORD = sizeof( DWORD );
645 int lineBoundary = bytePerLine % sizeDWORD;
646 int padding = 0;
647 if( lineBoundary > 0 )
648 {
649 padding = sizeDWORD - lineBoundary;
650 bytePerLine += padding;
651 }
652
653 // create a DIB header
654 int headersize = sizeof(BITMAPINFOHEADER);
655 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
656 if( !lpDIBh )
657 {
658 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
659 free( data );
660 return wxNullImage;
661 }
662 // Fill in the DIB header
663 lpDIBh->bmiHeader.biSize = headersize;
664 lpDIBh->bmiHeader.biWidth = width;
665 lpDIBh->bmiHeader.biHeight = -height;
666 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
667 lpDIBh->bmiHeader.biPlanes = 1;
668 lpDIBh->bmiHeader.biBitCount = 24;
669 lpDIBh->bmiHeader.biCompression = BI_RGB;
670 lpDIBh->bmiHeader.biClrUsed = 0;
671 // These seem not really needed for our purpose here.
672 lpDIBh->bmiHeader.biClrImportant = 0;
673 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
674 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
675 // memory for DIB data
676 unsigned char *lpBits;
677 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
678 if( !lpBits )
679 {
680 wxFAIL_MSG( wxT("could not allocate data for DIB") );
681 free( data );
682 free( lpDIBh );
683 return wxNullImage;
684 }
685
686 // copy data from the device-dependent bitmap to the DIB
687 HDC hdc = ::GetDC(NULL);
688 HBITMAP hbitmap;
689 hbitmap = (HBITMAP) GetHBITMAP();
690 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
691
692 // copy DIB data into the wxImage object
693 int i, j;
694 unsigned char *ptdata = data;
695 unsigned char *ptbits = lpBits;
696 for( i=0; i<height; i++ )
697 {
698 for( j=0; j<width; j++ )
699 {
700 *(ptdata++) = *(ptbits+2);
701 *(ptdata++) = *(ptbits+1);
702 *(ptdata++) = *(ptbits );
703 ptbits += 3;
704 }
705 ptbits += padding;
706 }
707
708 // similarly, set data according to the possible mask bitmap
709 if( GetMask() && GetMask()->GetMaskBitmap() )
710 {
711 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
712 // memory DC created, color set, data copied, and memory DC deleted
713 HDC memdc = ::CreateCompatibleDC( hdc );
714 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
715 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
716 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
717 ::DeleteDC( memdc );
718 // background color set to RGB(16,16,16) in consistent with wxGTK
719 unsigned char r=16, g=16, b=16;
720 ptdata = data;
721 ptbits = lpBits;
722 for( i=0; i<height; i++ )
723 {
724 for( j=0; j<width; j++ )
725 {
726 if( *ptbits != 0 )
727 ptdata += 3;
728 else
729 {
730 *(ptdata++) = r;
731 *(ptdata++) = g;
732 *(ptdata++) = b;
733 }
734 ptbits += 3;
735 }
736 ptbits += padding;
737 }
738 image.SetMaskColour( r, g, b );
739 image.SetMask( TRUE );
740 }
741 else
742 {
743 image.SetMask( FALSE );
744 }
745 // free allocated resources
746 ::ReleaseDC(NULL, hdc);
747 free(lpDIBh);
748 free(lpBits);
749
750 return image;
8cb172b4 751#endif
fec19ea9
VS
752}
753
6d51f220
VZ
754#endif // wxUSE_IMAGE
755
debe6624 756bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 757{
6d167489 758 UnRef();
2bda0e17 759
6d167489 760 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 761
6d167489
VZ
762 if ( handler )
763 {
764 m_refData = new wxBitmapRefData;
2bda0e17 765
6d167489
VZ
766 return handler->LoadFile(this, filename, type, -1, -1);
767 }
6d51f220 768#if wxUSE_IMAGE
6d167489 769 else
b75dd496 770 {
6d167489 771 wxImage image;
6d51f220
VZ
772 if ( image.LoadFile( filename, type ) && image.Ok() )
773 {
774 *this = image.ConvertToBitmap();
6d167489 775
6d51f220
VZ
776 return TRUE;
777 }
b75dd496 778 }
6d51f220
VZ
779#endif // wxUSE_IMAGE
780
781 return FALSE;
2bda0e17
KB
782}
783
debe6624 784bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 785{
6d167489 786 UnRef();
2bda0e17 787
6d167489 788 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 789
6d167489
VZ
790 if ( !handler )
791 {
f6bcfd97 792 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
2bda0e17 793
6d167489
VZ
794 return FALSE;
795 }
1d792928 796
6d167489 797 m_refData = new wxBitmapRefData;
1d792928 798
6d167489 799 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
800}
801
d275c7eb
VZ
802bool wxBitmap::SaveFile(const wxString& filename,
803 int type,
804 const wxPalette *palette)
2bda0e17 805{
6d167489 806 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 807
6d167489
VZ
808 if ( handler )
809 {
810 return handler->SaveFile(this, filename, type, palette);
811 }
6d51f220 812#if wxUSE_IMAGE
6d167489
VZ
813 else
814 {
815 // FIXME what about palette? shouldn't we use it?
816 wxImage image( *this );
6d51f220
VZ
817 if ( image.Ok() )
818 {
819 return image.SaveFile(filename, type);
820 }
6d167489 821 }
6d51f220
VZ
822#endif // wxUSE_IMAGE
823
824 return FALSE;
2bda0e17
KB
825}
826
4b7f2165
VZ
827// ----------------------------------------------------------------------------
828// sub bitmap extraction
829// ----------------------------------------------------------------------------
830
831wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
832{
04ef50df 833#ifndef __WXMICROWIN__
4b7f2165
VZ
834 wxCHECK_MSG( Ok() &&
835 (rect.x >= 0) && (rect.y >= 0) &&
836 (rect.x+rect.width <= GetWidth()) &&
837 (rect.y+rect.height <= GetHeight()),
838 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
839
840 wxBitmap ret( rect.width, rect.height, GetDepth() );
841 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
842
843 // copy bitmap data
844 HDC dcSrc = ::CreateCompatibleDC(NULL);
845 HDC dcDst = ::CreateCompatibleDC(NULL);
846 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
847 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
848 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
849
850 // copy mask if there is one
851 if (GetMask())
852 {
853 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
854
855 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
856 SelectObject(dcDst, (HBITMAP) hbmpMask);
857 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
858
859 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
860 ret.SetMask(mask);
861 }
862
863 SelectObject(dcDst, NULL);
864 SelectObject(dcSrc, NULL);
865 DeleteDC(dcDst);
866 DeleteDC(dcSrc);
867
868 return ret;
04ef50df
JS
869#else
870 return wxBitmap();
871#endif
4b7f2165
VZ
872}
873
6d167489
VZ
874// ----------------------------------------------------------------------------
875// wxBitmap accessors
876// ----------------------------------------------------------------------------
2bda0e17
KB
877
878void wxBitmap::SetQuality(int q)
879{
6d167489 880 EnsureHasData();
2bda0e17 881
6d167489 882 GetBitmapData()->m_quality = q;
2bda0e17
KB
883}
884
6d167489 885#if WXWIN_COMPATIBILITY_2
2bda0e17
KB
886void wxBitmap::SetOk(bool isOk)
887{
6d167489 888 EnsureHasData();
2bda0e17 889
6d167489 890 GetBitmapData()->m_ok = isOk;
2bda0e17 891}
6d167489 892#endif // WXWIN_COMPATIBILITY_2
2bda0e17 893
d275c7eb
VZ
894#if wxUSE_PALETTE
895
2bda0e17
KB
896void wxBitmap::SetPalette(const wxPalette& palette)
897{
6d167489 898 EnsureHasData();
2bda0e17 899
6d167489 900 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
901}
902
d275c7eb
VZ
903#endif // wxUSE_PALETTE
904
2bda0e17
KB
905void wxBitmap::SetMask(wxMask *mask)
906{
6d167489 907 EnsureHasData();
2bda0e17 908
6d167489 909 GetBitmapData()->m_bitmapMask = mask;
2bda0e17
KB
910}
911
7b46ecac
JS
912// Creates a bitmap that matches the device context, from
913// an arbitray bitmap. At present, the original bitmap must have an
914// associated palette. TODO: use a default palette if no palette exists.
915// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
916wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
917{
04ef50df
JS
918#ifdef __WXMICROWIN__
919 return wxBitmap();
920#else
7b46ecac 921 wxMemoryDC memDC;
4b7f2165 922 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
57c208c5 923 HPALETTE hPal = (HPALETTE) NULL;
7b46ecac 924 LPBITMAPINFO lpDib;
57c208c5 925 void *lpBits = (void*) NULL;
7b46ecac 926
d275c7eb 927#if wxUSE_PALETTE
6d167489 928 if( GetPalette() && GetPalette()->Ok() )
a367b9b3 929 {
6d167489 930 tmpBitmap.SetPalette(*GetPalette());
a367b9b3 931 memDC.SelectObject(tmpBitmap);
6d167489
VZ
932 memDC.SetPalette(*GetPalette());
933 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
a367b9b3
JS
934 }
935 else
936 {
937 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
938 wxPalette palette;
939 palette.SetHPALETTE( (WXHPALETTE)hPal );
940 tmpBitmap.SetPalette( palette );
941 memDC.SelectObject(tmpBitmap);
942 memDC.SetPalette( palette );
943 }
d275c7eb
VZ
944#else // !wxUSE_PALETTE
945 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
946#endif // wxUSE_PALETTE/!wxUSE_PALETTE
7b46ecac 947
6d167489
VZ
948 // set the height negative because in a DIB the order of the lines is
949 // reversed
950 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
951 {
952 return wxNullBitmap;
953 }
7b46ecac
JS
954
955 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
956
6d167489 957 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
7b46ecac 958
6d167489
VZ
959 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
960 GetWidth(), GetHeight(),
961 0, 0, 0, GetHeight(),
962 lpBits, lpDib, DIB_RGB_COLORS);
7b46ecac
JS
963
964 free(lpBits);
965
6d167489
VZ
966 wxFreeDIB(lpDib);
967
968 return tmpBitmap;
04ef50df 969#endif
7b46ecac
JS
970}
971
6d167489
VZ
972// ----------------------------------------------------------------------------
973// wxMask
974// ----------------------------------------------------------------------------
2bda0e17 975
10fcf31a 976wxMask::wxMask()
2bda0e17
KB
977{
978 m_maskBitmap = 0;
979}
980
981// Construct a mask from a bitmap and a colour indicating
982// the transparent area
983wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
984{
985 m_maskBitmap = 0;
6d167489 986 Create(bitmap, colour);
2bda0e17
KB
987}
988
989// Construct a mask from a bitmap and a palette index indicating
990// the transparent area
debe6624 991wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
992{
993 m_maskBitmap = 0;
6d167489 994 Create(bitmap, paletteIndex);
2bda0e17
KB
995}
996
997// Construct a mask from a mono bitmap (copies the bitmap).
998wxMask::wxMask(const wxBitmap& bitmap)
999{
1000 m_maskBitmap = 0;
6d167489 1001 Create(bitmap);
2bda0e17
KB
1002}
1003
10fcf31a 1004wxMask::~wxMask()
2bda0e17
KB
1005{
1006 if ( m_maskBitmap )
1007 ::DeleteObject((HBITMAP) m_maskBitmap);
1008}
1009
1010// Create a mask from a mono bitmap (copies the bitmap).
1011bool wxMask::Create(const wxBitmap& bitmap)
1012{
04ef50df 1013#ifndef __WXMICROWIN__
a58a12e9
VZ
1014 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1015 _T("can't create mask from invalid or not monochrome bitmap") );
1016
2bda0e17 1017 if ( m_maskBitmap )
6d167489
VZ
1018 {
1019 ::DeleteObject((HBITMAP) m_maskBitmap);
1020 m_maskBitmap = 0;
1021 }
a58a12e9 1022
6d167489
VZ
1023 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1024 bitmap.GetWidth(),
1025 bitmap.GetHeight(),
1026 1, 1, 0
1027 );
1028 HDC srcDC = CreateCompatibleDC(0);
1029 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1030 HDC destDC = CreateCompatibleDC(0);
1031 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1032 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1033 SelectObject(srcDC, 0);
1034 DeleteDC(srcDC);
1035 SelectObject(destDC, 0);
1036 DeleteDC(destDC);
1037 return TRUE;
04ef50df
JS
1038#else
1039 return FALSE;
1040#endif
2bda0e17
KB
1041}
1042
1043// Create a mask from a bitmap and a palette index indicating
1044// the transparent area
debe6624 1045bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1046{
1047 if ( m_maskBitmap )
1d792928 1048 {
6d167489
VZ
1049 ::DeleteObject((HBITMAP) m_maskBitmap);
1050 m_maskBitmap = 0;
1d792928 1051 }
d275c7eb
VZ
1052
1053#if wxUSE_PALETTE
6d167489
VZ
1054 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1055 {
1056 unsigned char red, green, blue;
1057 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1058 {
1059 wxColour transparentColour(red, green, blue);
1060 return Create(bitmap, transparentColour);
1061 }
1062 }
d275c7eb
VZ
1063#endif // wxUSE_PALETTE
1064
6d167489 1065 return FALSE;
2bda0e17
KB
1066}
1067
1068// Create a mask from a bitmap and a colour indicating
1069// the transparent area
1070bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1071{
04ef50df 1072#ifndef __WXMICROWIN__
4b7f2165
VZ
1073 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1074
2bda0e17 1075 if ( m_maskBitmap )
1d792928 1076 {
6d167489
VZ
1077 ::DeleteObject((HBITMAP) m_maskBitmap);
1078 m_maskBitmap = 0;
1079 }
4b7f2165
VZ
1080
1081 int width = bitmap.GetWidth(),
1082 height = bitmap.GetHeight();
1083
1084 // scan the bitmap for the transparent colour and set the corresponding
1085 // pixels in the mask to BLACK and the rest to WHITE
1086 COLORREF maskColour = wxColourToRGB(colour);
1087 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1088
1089 HDC srcDC = ::CreateCompatibleDC(NULL);
1090 HDC destDC = ::CreateCompatibleDC(NULL);
1091 if ( !srcDC || !destDC )
1092 {
f6bcfd97 1093 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1094 }
1095
0bafad0c
VZ
1096 bool ok = TRUE;
1097
1e6feb95
VZ
1098 // SelectObject() will fail
1099 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1100 _T("bitmap can't be selected in another DC") );
1101
0bafad0c
VZ
1102 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1103 if ( !hbmpSrcOld )
6d167489 1104 {
f6bcfd97 1105 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1106
1107 ok = FALSE;
4b7f2165 1108 }
0bafad0c
VZ
1109
1110 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1111 if ( !hbmpDstOld )
4b7f2165 1112 {
f6bcfd97 1113 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1114
1115 ok = FALSE;
1d792928 1116 }
2bda0e17 1117
4b7f2165
VZ
1118 // this is not very efficient, but I can't think of a better way of doing
1119 // it
0bafad0c 1120 for ( int w = 0; ok && (w < width); w++ )
2bda0e17 1121 {
0bafad0c 1122 for ( int h = 0; ok && (h < height); h++ )
c793fa87 1123 {
6d167489 1124 COLORREF col = GetPixel(srcDC, w, h);
4b7f2165
VZ
1125 if ( col == CLR_INVALID )
1126 {
f6bcfd97 1127 wxLogLastError(wxT("GetPixel"));
4b7f2165
VZ
1128
1129 // doesn't make sense to continue
0bafad0c 1130 ok = FALSE;
4b7f2165 1131
0bafad0c 1132 break;
4b7f2165
VZ
1133 }
1134
1135 if ( col == maskColour )
6d167489
VZ
1136 {
1137 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
1138 }
1139 else
1140 {
1141 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
1142 }
c793fa87 1143 }
2bda0e17 1144 }
4b7f2165 1145
0bafad0c 1146 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1147 ::DeleteDC(srcDC);
0bafad0c 1148 ::SelectObject(destDC, hbmpDstOld);
6d167489 1149 ::DeleteDC(destDC);
4b7f2165 1150
0bafad0c 1151 return ok;
04ef50df
JS
1152#else
1153 return FALSE;
1154#endif
6d167489
VZ
1155}
1156
1157// ----------------------------------------------------------------------------
1158// wxBitmapHandler
1159// ----------------------------------------------------------------------------
1d792928 1160
6d167489
VZ
1161bool wxBitmapHandler::Create(wxGDIImage *image,
1162 void *data,
1163 long flags,
1164 int width, int height, int depth)
1165{
1166 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1167
8c1375b9 1168 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
2bda0e17
KB
1169}
1170
6d167489
VZ
1171bool wxBitmapHandler::Load(wxGDIImage *image,
1172 const wxString& name,
1173 long flags,
1174 int width, int height)
2bda0e17 1175{
6d167489 1176 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1177
6d167489
VZ
1178 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1179}
2bda0e17 1180
6d167489
VZ
1181bool wxBitmapHandler::Save(wxGDIImage *image,
1182 const wxString& name,
1183 int type)
2bda0e17 1184{
6d167489
VZ
1185 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1186
1187 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
2bda0e17
KB
1188}
1189
6d167489
VZ
1190bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1191 void *WXUNUSED(data),
1192 long WXUNUSED(type),
1193 int WXUNUSED(width),
1194 int WXUNUSED(height),
1195 int WXUNUSED(depth))
2bda0e17 1196{
6d167489 1197 return FALSE;
2bda0e17
KB
1198}
1199
6d167489
VZ
1200bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1201 const wxString& WXUNUSED(name),
1202 long WXUNUSED(type),
1203 int WXUNUSED(desiredWidth),
1204 int WXUNUSED(desiredHeight))
2bda0e17 1205{
6d167489 1206 return FALSE;
2bda0e17
KB
1207}
1208
6d167489
VZ
1209bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1210 const wxString& WXUNUSED(name),
1211 int WXUNUSED(type),
1212 const wxPalette *WXUNUSED(palette))
2bda0e17 1213{
6d167489 1214 return FALSE;
7b46ecac
JS
1215}
1216
6d167489
VZ
1217// ----------------------------------------------------------------------------
1218// DIB functions
1219// ----------------------------------------------------------------------------
1220
04ef50df 1221#ifndef __WXMICROWIN__
6d167489
VZ
1222bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1223 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1224{
1225 unsigned long i, headerSize;
1226 LPBITMAPINFO lpDIBheader = NULL;
1227 LPPALETTEENTRY lpPe = NULL;
1228
1229
1230 // Allocate space for a DIB header
1231 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1232 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1233 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1234
1235 GetPaletteEntries(hPal, 0, 256, lpPe);
1236
7b46ecac
JS
1237 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1238
7b46ecac
JS
1239 // Fill in the static parts of the DIB header
1240 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1241 lpDIBheader->bmiHeader.biWidth = xSize;
1242 lpDIBheader->bmiHeader.biHeight = ySize;
1243 lpDIBheader->bmiHeader.biPlanes = 1;
1244
1245 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1246 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1247 lpDIBheader->bmiHeader.biCompression = BI_RGB;
6d167489 1248 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
7b46ecac
JS
1249 lpDIBheader->bmiHeader.biClrUsed = 256;
1250
1251
1252 // Initialize the DIB palette
1253 for (i = 0; i < 256; i++) {
1254 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1255 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1256 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1257 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1258 }
1259
1260 *lpDIBHeader = lpDIBheader;
1261
6d167489 1262 return TRUE;
7b46ecac
JS
1263}
1264
6d167489 1265void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1266{
6d167489 1267 free(lpDIBHeader);
7b46ecac 1268}
04ef50df 1269#endif
7b46ecac 1270
4b7f2165
VZ
1271// ----------------------------------------------------------------------------
1272// other helper functions
1273// ----------------------------------------------------------------------------
1274
1275extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1276{
04ef50df 1277#ifndef __WXMICROWIN__
4b7f2165
VZ
1278 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1279
1280 // get width/height from the bitmap if not given
1281 if ( !w || !h )
1282 {
1283 BITMAP bm;
1284 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1285 w = bm.bmWidth;
1286 h = bm.bmHeight;
1287 }
1288
1289 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1290 HDC hdcDst = ::CreateCompatibleDC(NULL);
1291 if ( !hdcSrc || !hdcDst )
1292 {
f6bcfd97 1293 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1294 }
1295
1296 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1297 if ( !hbmpInvMask )
1298 {
f6bcfd97 1299 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1300 }
1301
1302 ::SelectObject(hdcSrc, hbmpMask);
1303 ::SelectObject(hdcDst, hbmpInvMask);
1304 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1305 hdcSrc, 0, 0,
1306 NOTSRCCOPY) )
1307 {
f6bcfd97 1308 wxLogLastError(wxT("BitBlt"));
4b7f2165 1309 }
7b46ecac 1310
4b7f2165
VZ
1311 ::DeleteDC(hdcSrc);
1312 ::DeleteDC(hdcDst);
1313
1314 return hbmpInvMask;
04ef50df
JS
1315#else
1316 return 0;
1317#endif
4b7f2165 1318}