]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
reset virtual root to NULL in DeleteAllItems()
[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
62e1ba75
JS
43//#include "device.h"
44
2bda0e17 45#include "wx/msw/private.h"
1d792928
VZ
46#include "wx/log.h"
47
04ef50df 48#if !defined(__WXMICROWIN__)
2bda0e17 49#include "wx/msw/dib.h"
04ef50df
JS
50#endif
51
b75dd496 52#include "wx/image.h"
66e23ad2 53#include "wx/xpmdecod.h"
2bda0e17 54
3c1a88d8
VZ
55// missing from mingw32 header
56#ifndef CLR_INVALID
57 #define CLR_INVALID ((COLORREF)-1)
58#endif // no CLR_INVALID
59
10fcf31a
VZ
60// ----------------------------------------------------------------------------
61// macros
62// ----------------------------------------------------------------------------
63
4b7f2165
VZ
64IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
65IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
6d167489 66
4b7f2165 67IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
2bda0e17 68
10fcf31a
VZ
69// ============================================================================
70// implementation
71// ============================================================================
72
73// ----------------------------------------------------------------------------
74// wxBitmapRefData
75// ----------------------------------------------------------------------------
76
77wxBitmapRefData::wxBitmapRefData()
2bda0e17 78{
6d167489
VZ
79 m_quality = 0;
80 m_selectedInto = NULL;
81 m_numColors = 0;
82 m_bitmapMask = NULL;
340196c0 83 m_hBitmap = (WXHBITMAP) NULL;
2bda0e17
KB
84}
85
6d167489 86void wxBitmapRefData::Free()
2bda0e17 87{
d59ceba5
VZ
88 wxASSERT_MSG( !m_selectedInto,
89 wxT("deleting bitmap still selected into wxMemoryDC") );
2bda0e17 90
d59ceba5 91 if ( m_hBitmap)
6d167489 92 {
8e9ff815 93 // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap);
62e1ba75 94#if 1
6d167489
VZ
95 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
96 {
f6bcfd97 97 wxLogLastError(wxT("DeleteObject(hbitmap)"));
6d167489 98 }
62e1ba75 99#endif
6d167489 100 }
e7003166 101
6d167489
VZ
102 delete m_bitmapMask;
103 m_bitmapMask = NULL;
2bda0e17
KB
104}
105
10fcf31a 106// ----------------------------------------------------------------------------
6d167489 107// wxBitmap creation
10fcf31a
VZ
108// ----------------------------------------------------------------------------
109
4fe5383d
VZ
110// this function should be called from all wxBitmap ctors
111void wxBitmap::Init()
2bda0e17 112{
4fe5383d 113 // m_refData = NULL; done in the base class ctor
2bda0e17 114
4fe5383d
VZ
115}
116
6d167489
VZ
117#ifdef __WIN32__
118
119bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
120{
04ef50df 121#ifndef __WXMICROWIN__
6d167489
VZ
122 // it may be either HICON or HCURSOR
123 HICON hicon = (HICON)icon.GetHandle();
124
125 ICONINFO iconInfo;
126 if ( !::GetIconInfo(hicon, &iconInfo) )
127 {
f6bcfd97 128 wxLogLastError(wxT("GetIconInfo"));
6d167489
VZ
129
130 return FALSE;
131 }
132
133 wxBitmapRefData *refData = new wxBitmapRefData;
134 m_refData = refData;
135
d9c8e68e
VZ
136 int w = icon.GetWidth(),
137 h = icon.GetHeight();
138
139 refData->m_width = w;
140 refData->m_height = h;
6d167489
VZ
141 refData->m_depth = wxDisplayDepth();
142
143 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
d9c8e68e
VZ
144
145 // the mask returned by GetIconInfo() is inversed compared to the usual
146 // wxWin convention
4b7f2165
VZ
147 refData->m_bitmapMask = new wxMask((WXHBITMAP)
148 wxInvertMask(iconInfo.hbmMask, w, h));
6d167489 149
68f36a2c
VZ
150
151 // delete the old one now as we don't need it any more
152 ::DeleteObject(iconInfo.hbmMask);
153
6d167489
VZ
154#if WXWIN_COMPATIBILITY_2
155 refData->m_ok = TRUE;
156#endif // WXWIN_COMPATIBILITY_2
157
158 return TRUE;
04ef50df
JS
159#else
160 return FALSE;
161#endif
6d167489
VZ
162}
163
164#endif // Win32
165
166bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
4fe5383d
VZ
167{
168 UnRef();
07cf98cb 169
6d167489 170 if ( !cursor.Ok() )
4fe5383d 171 return FALSE;
07cf98cb 172
6d167489
VZ
173#ifdef __WIN16__
174 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
175
176 return FALSE;
8f177c8e 177#else
6d167489 178 return CopyFromIconOrCursor(cursor);
8f177c8e 179#endif // Win16
6d167489
VZ
180}
181
182bool wxBitmap::CopyFromIcon(const wxIcon& icon)
183{
184 UnRef();
07cf98cb 185
6d167489
VZ
186 if ( !icon.Ok() )
187 return FALSE;
4fe5383d
VZ
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__
6d167489
VZ
193 int width = icon.GetWidth(),
194 height = icon.GetHeight();
195
4fe5383d 196 // copy the icon to the bitmap
6d167489 197 ScreenHDC hdcScreen;
4fe5383d
VZ
198 HDC hdc = ::CreateCompatibleDC(hdcScreen);
199 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
07cf98cb
VZ
200 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
201
6d167489 202 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
07cf98cb
VZ
203
204 ::SelectObject(hdc, hbmpOld);
205 ::DeleteDC(hdc);
4fe5383d 206
6d167489
VZ
207 wxBitmapRefData *refData = new wxBitmapRefData;
208 m_refData = refData;
4fe5383d 209
6d167489
VZ
210 refData->m_width = width;
211 refData->m_height = height;
212 refData->m_depth = wxDisplayDepth();
07cf98cb 213
6d167489 214 refData->m_hBitmap = (WXHBITMAP)hbitmap;
07cf98cb 215
6d167489
VZ
216#if WXWIN_COMPATIBILITY_2
217 refData->m_ok = TRUE;
218#endif // WXWIN_COMPATIBILITY_2
222594ea 219
4fe5383d 220 return TRUE;
6d167489
VZ
221#else // Win32
222 return CopyFromIconOrCursor(icon);
223#endif // Win16/Win32
10fcf31a
VZ
224}
225
226wxBitmap::~wxBitmap()
2bda0e17 227{
2bda0e17
KB
228}
229
5bd3a2da 230wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
2bda0e17 231{
4fe5383d 232 Init();
2bda0e17 233
04ef50df 234#ifndef __WXMICROWIN__
6d167489
VZ
235 wxBitmapRefData *refData = new wxBitmapRefData;
236 m_refData = refData;
2bda0e17 237
5bd3a2da
VZ
238 refData->m_width = width;
239 refData->m_height = height;
240 refData->m_depth = depth;
6d167489
VZ
241 refData->m_numColors = 0;
242 refData->m_selectedInto = NULL;
2bda0e17 243
5bd3a2da
VZ
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!
4d24ece7
VZ
250 const size_t bytesPerLine = (width + 7) / 8;
251 const size_t padding = bytesPerLine % 2;
252 const size_t len = height * ( padding + bytesPerLine );
5bd3a2da
VZ
253 data = (char *)malloc(len);
254 const char *src = bits;
255 char *dst = data;
256
257 for ( int rows = 0; rows < height; rows++ )
258 {
0765adca 259 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
5bd3a2da 260 {
0765adca
VZ
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;
5bd3a2da
VZ
271 }
272
273 if ( padding )
274 *dst++ = 0;
5bd3a2da
VZ
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);
6d167489
VZ
284 if ( !hbmp )
285 {
f6bcfd97 286 wxLogLastError(wxT("CreateBitmap"));
6d167489 287 }
2bda0e17 288
5bd3a2da
VZ
289 if ( data != bits )
290 {
291 free(data);
292 }
293
6d167489 294 SetHBITMAP((WXHBITMAP)hbmp);
04ef50df 295#endif
2bda0e17
KB
296}
297
2fd284a4 298// Create from XPM data
4b7f2165 299bool wxBitmap::CreateFromXpm(const char **data)
2fd284a4 300{
66e23ad2 301#if wxUSE_IMAGE && wxUSE_XPM
4fe5383d
VZ
302 Init();
303
66e23ad2 304 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
4d24ece7 305
66e23ad2
VS
306 wxXPMDecoder decoder;
307 wxImage img = decoder.ReadData(data);
308 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
4d24ece7 309
c59b4b01 310 *this = wxBitmap(img);
66e23ad2
VS
311 return TRUE;
312#else
4d24ece7 313 return FALSE;
66e23ad2 314#endif
2fd284a4
JS
315}
316
debe6624 317wxBitmap::wxBitmap(int w, int h, int d)
2bda0e17 318{
4fe5383d 319 Init();
2bda0e17 320
4fe5383d 321 (void)Create(w, h, d);
2bda0e17
KB
322}
323
debe6624 324wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
2bda0e17 325{
4fe5383d 326 Init();
2bda0e17 327
6d167489 328 (void)Create(data, type, width, height, depth);
2bda0e17
KB
329}
330
2aeec9ec 331wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
2bda0e17 332{
4fe5383d 333 Init();
2bda0e17 334
4fe5383d 335 LoadFile(filename, (int)type);
2bda0e17
KB
336}
337
debe6624 338bool wxBitmap::Create(int w, int h, int d)
2bda0e17 339{
6d167489
VZ
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;
e640f823 349#ifndef __WXMICROWIN__
6d167489
VZ
350 if ( d > 0 )
351 {
352 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
353 if ( !hbmp )
354 {
f6bcfd97 355 wxLogLastError(wxT("CreateBitmap"));
6d167489
VZ
356 }
357 }
358 else
e640f823 359#endif
6d167489
VZ
360 {
361 ScreenHDC dc;
362 hbmp = ::CreateCompatibleBitmap(dc, w, h);
363 if ( !hbmp )
364 {
f6bcfd97 365 wxLogLastError(wxT("CreateCompatibleBitmap"));
6d167489
VZ
366 }
367
368 GetBitmapData()->m_depth = wxDisplayDepth();
369 }
2bda0e17 370
6d167489 371 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17 372
6d167489
VZ
373#if WXWIN_COMPATIBILITY_2
374 GetBitmapData()->m_ok = hbmp != 0;
375#endif // WXWIN_COMPATIBILITY_2
6d167489 376 return Ok();
2bda0e17
KB
377}
378
6d51f220
VZ
379// ----------------------------------------------------------------------------
380// wxImage to/from conversions
381// ----------------------------------------------------------------------------
382
383#if wxUSE_IMAGE
384
fec19ea9
VS
385bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
386{
8cb172b4 387#ifdef __WXMICROWIN__
62e1ba75
JS
388
389 // Set this to 1 to experiment with mask code,
390 // which currently doesn't work
391#define USE_MASKS 0
392
54a96d02 393 m_refData = new wxBitmapRefData();
e640f823
JS
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.
8e9ff815 398
e640f823 399 HDC hScreenDC = ::GetDC(NULL);
62e1ba75 400 // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp);
e640f823
JS
401 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
402
403 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
62e1ba75
JS
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
54a96d02 411 // printf("Created bitmap %d\n", (int) hBitmap);
e640f823
JS
412 if (hBitmap == NULL)
413 {
414 ::ReleaseDC(NULL, hScreenDC);
8e9ff815 415 return FALSE;
e640f823
JS
416 }
417 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
e640f823
JS
418
419 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
62e1ba75
JS
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;
8e9ff815 442 }
62e1ba75
JS
443 else
444 {
445 hScreenDC = ::GetDC(NULL);
446 hMaskDC = ::CreateCompatibleDC(hScreenDC);
447 ::ReleaseDC(NULL, hScreenDC);
448
449 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
8e9ff815 450 }
62e1ba75
JS
451#else
452 hasMask = FALSE;
453#endif
454 }
e640f823
JS
455
456 int i, j;
457 for (i = 0; i < image.GetWidth(); i++)
458 {
8e9ff815
VZ
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);
e640f823 464
8e9ff815 465 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
62e1ba75
JS
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));
8e9ff815
VZ
475 }
476 }
e640f823
JS
477 }
478
479 ::SelectObject(hMemDC, hOldBitmap);
480 ::DeleteDC(hMemDC);
62e1ba75
JS
481 if (hasMask)
482 {
483 ::SelectObject(hMaskDC, hOldMaskBitmap);
8e9ff815 484 ::DeleteDC(hMaskDC);
62e1ba75
JS
485
486 ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap);
487 }
8e9ff815 488
e640f823
JS
489 SetWidth(image.GetWidth());
490 SetHeight(image.GetHeight());
491 SetDepth(screenDepth);
492 SetHBITMAP( (WXHBITMAP) hBitmap );
8e9ff815 493
e640f823
JS
494#if wxUSE_PALETTE
495 // Copy the palette from the source image
496 SetPalette(image.GetPalette());
497#endif // wxUSE_PALETTE
498
54a96d02
JS
499#if WXWIN_COMPATIBILITY_2
500 // check the wxBitmap object
501 GetBitmapData()->SetOk();
502#endif // WXWIN_COMPATIBILITY_2
503
e640f823
JS
504 return TRUE;
505
8cb172b4 506#else
fec19ea9
VS
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
c447ce17 546 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
fec19ea9
VS
547 SetWidth( width );
548 SetHeight( bmpHeight );
549 if (depth == -1) depth = wxDisplayDepth();
550 SetDepth( depth );
551
e22c13fe 552#if wxUSE_PALETTE
19193a2c
KB
553 // Copy the palette from the source image
554 SetPalette(image.GetPalette());
e22c13fe 555#endif // wxUSE_PALETTE
19193a2c 556
fec19ea9
VS
557 // create a DIB header
558 int headersize = sizeof(BITMAPINFOHEADER);
559 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
097aeb99 560 wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
fec19ea9
VS
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
d275c7eb 593#if wxUSE_PALETTE
fec19ea9
VS
594 HPALETTE hOldPalette = 0;
595 if (image.GetPalette().Ok())
596 {
597 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
598 ::RealizePalette(memdc);
599 }
d275c7eb 600#endif // wxUSE_PALETTE
fec19ea9
VS
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
d275c7eb 651#if wxUSE_PALETTE
fec19ea9
VS
652 if (hOldPalette)
653 SelectPalette(memdc, hOldPalette, FALSE);
d275c7eb 654#endif // wxUSE_PALETTE
fec19ea9
VS
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
6d51f220 734
fec19ea9 735 return TRUE;
8cb172b4 736#endif
fec19ea9
VS
737}
738
739wxImage wxBitmap::ConvertToImage() const
740{
8cb172b4 741#ifdef __WXMICROWIN__
e640f823
JS
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") );
8e9ff815 750 return wxNullImage;
e640f823
JS
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 }
8e9ff815 767
e640f823
JS
768 HDC hScreenDC = ::GetDC(NULL);
769
770 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
771 ::ReleaseDC(NULL, hScreenDC);
772
773 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
8e9ff815 774
e640f823
JS
775 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
776
777 int i, j;
778 for (i = 0; i < GetWidth(); i++)
779 {
8e9ff815
VZ
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 }
e640f823
JS
789 }
790
791 ::SelectObject(hMemDC, hOldBitmap);
792 ::DeleteDC(hMemDC);
8e9ff815 793
e640f823
JS
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
fec19ea9 804 wxImage image;
6d51f220 805
fec19ea9
VS
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;
8cb172b4 928#endif
fec19ea9
VS
929}
930
6d51f220
VZ
931#endif // wxUSE_IMAGE
932
debe6624 933bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 934{
6d167489 935 UnRef();
2bda0e17 936
6d167489 937 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 938
6d167489
VZ
939 if ( handler )
940 {
941 m_refData = new wxBitmapRefData;
2bda0e17 942
6d167489
VZ
943 return handler->LoadFile(this, filename, type, -1, -1);
944 }
6d51f220 945#if wxUSE_IMAGE
6d167489 946 else
b75dd496 947 {
6d167489 948 wxImage image;
6d51f220
VZ
949 if ( image.LoadFile( filename, type ) && image.Ok() )
950 {
368d59f0 951 *this = wxBitmap(image);
6d167489 952
6d51f220
VZ
953 return TRUE;
954 }
b75dd496 955 }
6d51f220
VZ
956#endif // wxUSE_IMAGE
957
958 return FALSE;
2bda0e17
KB
959}
960
debe6624 961bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 962{
6d167489 963 UnRef();
2bda0e17 964
6d167489 965 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 966
6d167489
VZ
967 if ( !handler )
968 {
9b601c24 969 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
2bda0e17 970
6d167489
VZ
971 return FALSE;
972 }
1d792928 973
6d167489 974 m_refData = new wxBitmapRefData;
1d792928 975
6d167489 976 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
977}
978
d275c7eb
VZ
979bool wxBitmap::SaveFile(const wxString& filename,
980 int type,
981 const wxPalette *palette)
2bda0e17 982{
6d167489 983 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 984
6d167489
VZ
985 if ( handler )
986 {
987 return handler->SaveFile(this, filename, type, palette);
988 }
6d51f220 989#if wxUSE_IMAGE
6d167489
VZ
990 else
991 {
992 // FIXME what about palette? shouldn't we use it?
368d59f0 993 wxImage image = ConvertToImage();
6d51f220
VZ
994 if ( image.Ok() )
995 {
996 return image.SaveFile(filename, type);
997 }
6d167489 998 }
6d51f220
VZ
999#endif // wxUSE_IMAGE
1000
1001 return FALSE;
2bda0e17
KB
1002}
1003
4b7f2165
VZ
1004// ----------------------------------------------------------------------------
1005// sub bitmap extraction
1006// ----------------------------------------------------------------------------
1007
1008wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1009{
1010 wxCHECK_MSG( Ok() &&
1011 (rect.x >= 0) && (rect.y >= 0) &&
1012 (rect.x+rect.width <= GetWidth()) &&
1013 (rect.y+rect.height <= GetHeight()),
1014 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1015
1016 wxBitmap ret( rect.width, rect.height, GetDepth() );
1017 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1018
8e9ff815 1019#ifndef __WXMICROWIN__
4b7f2165 1020 // copy bitmap data
8e9ff815
VZ
1021 MemoryHDC dcSrc, dcDst;
1022
1023 {
1024 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
1025 selectDst(dcDst, GetHbitmapOf(ret));
1026
1027 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1028 dcSrc, rect.x, rect.y, SRCCOPY) )
1029 {
1030 wxLogLastError(_T("BitBlt"));
1031 }
1032 }
4b7f2165
VZ
1033
1034 // copy mask if there is one
8e9ff815 1035 if ( GetMask() )
4b7f2165
VZ
1036 {
1037 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1038
8e9ff815
VZ
1039 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1040 selectDst(dcDst, hbmpMask);
1041
1042 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1043 dcSrc, rect.x, rect.y, SRCCOPY) )
1044 {
1045 wxLogLastError(_T("BitBlt"));
1046 }
4b7f2165
VZ
1047
1048 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1049 ret.SetMask(mask);
1050 }
8e9ff815 1051#endif // !__WXMICROWIN__
4b7f2165
VZ
1052
1053 return ret;
1054}
1055
6d167489
VZ
1056// ----------------------------------------------------------------------------
1057// wxBitmap accessors
1058// ----------------------------------------------------------------------------
2bda0e17
KB
1059
1060void wxBitmap::SetQuality(int q)
1061{
6d167489 1062 EnsureHasData();
2bda0e17 1063
6d167489 1064 GetBitmapData()->m_quality = q;
2bda0e17
KB
1065}
1066
6d167489 1067#if WXWIN_COMPATIBILITY_2
2bda0e17
KB
1068void wxBitmap::SetOk(bool isOk)
1069{
6d167489 1070 EnsureHasData();
2bda0e17 1071
6d167489 1072 GetBitmapData()->m_ok = isOk;
2bda0e17 1073}
6d167489 1074#endif // WXWIN_COMPATIBILITY_2
2bda0e17 1075
d275c7eb
VZ
1076#if wxUSE_PALETTE
1077
2bda0e17
KB
1078void wxBitmap::SetPalette(const wxPalette& palette)
1079{
6d167489 1080 EnsureHasData();
2bda0e17 1081
6d167489 1082 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
1083}
1084
d275c7eb
VZ
1085#endif // wxUSE_PALETTE
1086
2bda0e17
KB
1087void wxBitmap::SetMask(wxMask *mask)
1088{
6d167489 1089 EnsureHasData();
2bda0e17 1090
6d167489 1091 GetBitmapData()->m_bitmapMask = mask;
2bda0e17
KB
1092}
1093
7b46ecac
JS
1094// Creates a bitmap that matches the device context, from
1095// an arbitray bitmap. At present, the original bitmap must have an
1096// associated palette. TODO: use a default palette if no palette exists.
1097// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1098wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
1099{
04ef50df 1100#ifdef __WXMICROWIN__
54a96d02 1101 return *this;
04ef50df 1102#else
7b46ecac 1103 wxMemoryDC memDC;
4b7f2165 1104 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
57c208c5 1105 HPALETTE hPal = (HPALETTE) NULL;
7b46ecac 1106 LPBITMAPINFO lpDib;
57c208c5 1107 void *lpBits = (void*) NULL;
7b46ecac 1108
d275c7eb 1109#if wxUSE_PALETTE
6d167489 1110 if( GetPalette() && GetPalette()->Ok() )
a367b9b3 1111 {
6d167489 1112 tmpBitmap.SetPalette(*GetPalette());
a367b9b3 1113 memDC.SelectObject(tmpBitmap);
6d167489
VZ
1114 memDC.SetPalette(*GetPalette());
1115 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
a367b9b3
JS
1116 }
1117 else
1118 {
1119 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1120 wxPalette palette;
1121 palette.SetHPALETTE( (WXHPALETTE)hPal );
1122 tmpBitmap.SetPalette( palette );
1123 memDC.SelectObject(tmpBitmap);
1124 memDC.SetPalette( palette );
1125 }
d275c7eb
VZ
1126#else // !wxUSE_PALETTE
1127 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
1128#endif // wxUSE_PALETTE/!wxUSE_PALETTE
7b46ecac 1129
6d167489
VZ
1130 // set the height negative because in a DIB the order of the lines is
1131 // reversed
1132 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
1133 {
1134 return wxNullBitmap;
1135 }
7b46ecac
JS
1136
1137 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
1138
6d167489 1139 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
7b46ecac 1140
6d167489
VZ
1141 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
1142 GetWidth(), GetHeight(),
1143 0, 0, 0, GetHeight(),
1144 lpBits, lpDib, DIB_RGB_COLORS);
7b46ecac
JS
1145
1146 free(lpBits);
1147
6d167489
VZ
1148 wxFreeDIB(lpDib);
1149
1150 return tmpBitmap;
04ef50df 1151#endif
7b46ecac
JS
1152}
1153
6d167489
VZ
1154// ----------------------------------------------------------------------------
1155// wxMask
1156// ----------------------------------------------------------------------------
2bda0e17 1157
10fcf31a 1158wxMask::wxMask()
2bda0e17
KB
1159{
1160 m_maskBitmap = 0;
1161}
1162
1163// Construct a mask from a bitmap and a colour indicating
1164// the transparent area
1165wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1166{
1167 m_maskBitmap = 0;
6d167489 1168 Create(bitmap, colour);
2bda0e17
KB
1169}
1170
1171// Construct a mask from a bitmap and a palette index indicating
1172// the transparent area
debe6624 1173wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1174{
1175 m_maskBitmap = 0;
6d167489 1176 Create(bitmap, paletteIndex);
2bda0e17
KB
1177}
1178
1179// Construct a mask from a mono bitmap (copies the bitmap).
1180wxMask::wxMask(const wxBitmap& bitmap)
1181{
1182 m_maskBitmap = 0;
6d167489 1183 Create(bitmap);
2bda0e17
KB
1184}
1185
10fcf31a 1186wxMask::~wxMask()
2bda0e17
KB
1187{
1188 if ( m_maskBitmap )
1189 ::DeleteObject((HBITMAP) m_maskBitmap);
1190}
1191
1192// Create a mask from a mono bitmap (copies the bitmap).
1193bool wxMask::Create(const wxBitmap& bitmap)
1194{
04ef50df 1195#ifndef __WXMICROWIN__
a58a12e9
VZ
1196 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1197 _T("can't create mask from invalid or not monochrome bitmap") );
1198
2bda0e17 1199 if ( m_maskBitmap )
6d167489
VZ
1200 {
1201 ::DeleteObject((HBITMAP) m_maskBitmap);
1202 m_maskBitmap = 0;
1203 }
a58a12e9 1204
6d167489
VZ
1205 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1206 bitmap.GetWidth(),
1207 bitmap.GetHeight(),
1208 1, 1, 0
1209 );
1210 HDC srcDC = CreateCompatibleDC(0);
1211 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1212 HDC destDC = CreateCompatibleDC(0);
1213 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1214 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1215 SelectObject(srcDC, 0);
1216 DeleteDC(srcDC);
1217 SelectObject(destDC, 0);
1218 DeleteDC(destDC);
1219 return TRUE;
04ef50df
JS
1220#else
1221 return FALSE;
1222#endif
2bda0e17
KB
1223}
1224
1225// Create a mask from a bitmap and a palette index indicating
1226// the transparent area
debe6624 1227bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1228{
1229 if ( m_maskBitmap )
1d792928 1230 {
6d167489
VZ
1231 ::DeleteObject((HBITMAP) m_maskBitmap);
1232 m_maskBitmap = 0;
1d792928 1233 }
d275c7eb
VZ
1234
1235#if wxUSE_PALETTE
6d167489
VZ
1236 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1237 {
1238 unsigned char red, green, blue;
1239 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1240 {
1241 wxColour transparentColour(red, green, blue);
1242 return Create(bitmap, transparentColour);
1243 }
1244 }
d275c7eb
VZ
1245#endif // wxUSE_PALETTE
1246
6d167489 1247 return FALSE;
2bda0e17
KB
1248}
1249
1250// Create a mask from a bitmap and a colour indicating
1251// the transparent area
1252bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1253{
04ef50df 1254#ifndef __WXMICROWIN__
4b7f2165
VZ
1255 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1256
2bda0e17 1257 if ( m_maskBitmap )
1d792928 1258 {
6d167489
VZ
1259 ::DeleteObject((HBITMAP) m_maskBitmap);
1260 m_maskBitmap = 0;
1261 }
4b7f2165
VZ
1262
1263 int width = bitmap.GetWidth(),
1264 height = bitmap.GetHeight();
1265
1266 // scan the bitmap for the transparent colour and set the corresponding
1267 // pixels in the mask to BLACK and the rest to WHITE
f423f4db 1268 COLORREF maskColour = wxColourToPalRGB(colour);
4b7f2165
VZ
1269 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1270
1271 HDC srcDC = ::CreateCompatibleDC(NULL);
1272 HDC destDC = ::CreateCompatibleDC(NULL);
1273 if ( !srcDC || !destDC )
1274 {
f6bcfd97 1275 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1276 }
1277
0bafad0c
VZ
1278 bool ok = TRUE;
1279
1e6feb95
VZ
1280 // SelectObject() will fail
1281 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1282 _T("bitmap can't be selected in another DC") );
1283
0bafad0c
VZ
1284 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1285 if ( !hbmpSrcOld )
6d167489 1286 {
f6bcfd97 1287 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1288
1289 ok = FALSE;
4b7f2165 1290 }
0bafad0c
VZ
1291
1292 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1293 if ( !hbmpDstOld )
4b7f2165 1294 {
f6bcfd97 1295 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1296
1297 ok = FALSE;
1d792928 1298 }
2bda0e17 1299
59ff46cb 1300 if ( ok )
2bda0e17 1301 {
59ff46cb
VZ
1302 // this will create a monochrome bitmap with 0 points for the pixels
1303 // which have the same value as the background colour and 1 for the
1304 // others
1305 ::SetBkColor(srcDC, maskColour);
1306 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
2bda0e17 1307 }
4b7f2165 1308
0bafad0c 1309 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1310 ::DeleteDC(srcDC);
0bafad0c 1311 ::SelectObject(destDC, hbmpDstOld);
6d167489 1312 ::DeleteDC(destDC);
4b7f2165 1313
0bafad0c 1314 return ok;
59ff46cb 1315#else // __WXMICROWIN__
04ef50df 1316 return FALSE;
59ff46cb 1317#endif // __WXMICROWIN__/!__WXMICROWIN__
6d167489
VZ
1318}
1319
1320// ----------------------------------------------------------------------------
1321// wxBitmapHandler
1322// ----------------------------------------------------------------------------
1d792928 1323
6d167489
VZ
1324bool wxBitmapHandler::Create(wxGDIImage *image,
1325 void *data,
1326 long flags,
1327 int width, int height, int depth)
1328{
1329 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1330
8c1375b9 1331 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
2bda0e17
KB
1332}
1333
6d167489
VZ
1334bool wxBitmapHandler::Load(wxGDIImage *image,
1335 const wxString& name,
1336 long flags,
1337 int width, int height)
2bda0e17 1338{
6d167489 1339 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1340
6d167489
VZ
1341 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1342}
2bda0e17 1343
6d167489
VZ
1344bool wxBitmapHandler::Save(wxGDIImage *image,
1345 const wxString& name,
1346 int type)
2bda0e17 1347{
6d167489
VZ
1348 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1349
1350 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
2bda0e17
KB
1351}
1352
6d167489
VZ
1353bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1354 void *WXUNUSED(data),
1355 long WXUNUSED(type),
1356 int WXUNUSED(width),
1357 int WXUNUSED(height),
1358 int WXUNUSED(depth))
2bda0e17 1359{
6d167489 1360 return FALSE;
2bda0e17
KB
1361}
1362
6d167489
VZ
1363bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1364 const wxString& WXUNUSED(name),
1365 long WXUNUSED(type),
1366 int WXUNUSED(desiredWidth),
1367 int WXUNUSED(desiredHeight))
2bda0e17 1368{
6d167489 1369 return FALSE;
2bda0e17
KB
1370}
1371
6d167489
VZ
1372bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1373 const wxString& WXUNUSED(name),
1374 int WXUNUSED(type),
1375 const wxPalette *WXUNUSED(palette))
2bda0e17 1376{
6d167489 1377 return FALSE;
7b46ecac
JS
1378}
1379
6d167489
VZ
1380// ----------------------------------------------------------------------------
1381// DIB functions
1382// ----------------------------------------------------------------------------
1383
04ef50df 1384#ifndef __WXMICROWIN__
6d167489
VZ
1385bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1386 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1387{
1388 unsigned long i, headerSize;
1389 LPBITMAPINFO lpDIBheader = NULL;
1390 LPPALETTEENTRY lpPe = NULL;
1391
1392
1393 // Allocate space for a DIB header
1394 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1395 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1396 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1397
1398 GetPaletteEntries(hPal, 0, 256, lpPe);
1399
7b46ecac
JS
1400 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1401
7b46ecac
JS
1402 // Fill in the static parts of the DIB header
1403 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1404 lpDIBheader->bmiHeader.biWidth = xSize;
1405 lpDIBheader->bmiHeader.biHeight = ySize;
1406 lpDIBheader->bmiHeader.biPlanes = 1;
1407
1408 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1409 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1410 lpDIBheader->bmiHeader.biCompression = BI_RGB;
6d167489 1411 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
7b46ecac
JS
1412 lpDIBheader->bmiHeader.biClrUsed = 256;
1413
1414
1415 // Initialize the DIB palette
1416 for (i = 0; i < 256; i++) {
1417 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1418 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1419 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1420 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1421 }
1422
1423 *lpDIBHeader = lpDIBheader;
1424
6d167489 1425 return TRUE;
7b46ecac
JS
1426}
1427
6d167489 1428void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1429{
6d167489 1430 free(lpDIBHeader);
7b46ecac 1431}
04ef50df 1432#endif
7b46ecac 1433
4b7f2165
VZ
1434// ----------------------------------------------------------------------------
1435// other helper functions
1436// ----------------------------------------------------------------------------
1437
1438extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1439{
04ef50df 1440#ifndef __WXMICROWIN__
4b7f2165
VZ
1441 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1442
1443 // get width/height from the bitmap if not given
1444 if ( !w || !h )
1445 {
1446 BITMAP bm;
1447 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1448 w = bm.bmWidth;
1449 h = bm.bmHeight;
1450 }
1451
1452 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1453 HDC hdcDst = ::CreateCompatibleDC(NULL);
1454 if ( !hdcSrc || !hdcDst )
1455 {
f6bcfd97 1456 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1457 }
1458
1459 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1460 if ( !hbmpInvMask )
1461 {
f6bcfd97 1462 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1463 }
1464
1465 ::SelectObject(hdcSrc, hbmpMask);
1466 ::SelectObject(hdcDst, hbmpInvMask);
1467 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1468 hdcSrc, 0, 0,
1469 NOTSRCCOPY) )
1470 {
f6bcfd97 1471 wxLogLastError(wxT("BitBlt"));
4b7f2165 1472 }
7b46ecac 1473
4b7f2165
VZ
1474 ::DeleteDC(hdcSrc);
1475 ::DeleteDC(hdcDst);
1476
1477 return hbmpInvMask;
04ef50df
JS
1478#else
1479 return 0;
1480#endif
4b7f2165 1481}