]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
Avoid an assert in wxFrame::SetTitle
[wxWidgets.git] / src / msw / bitmap.cpp
CommitLineData
0becd470 1////////////////////////////////////////////////////////////////////////////
2bda0e17
KB
2// Name: bitmap.cpp
3// Purpose: wxBitmap
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
10fcf31a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
4676948b 46#if wxUSE_WXDIB
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
1e3c12d7 53#ifdef wxHAVE_RAW_BITMAP
2cf45d69 54#include "wx/rawbmp.h"
1e3c12d7 55#endif
2cf45d69 56
3c1a88d8
VZ
57// missing from mingw32 header
58#ifndef CLR_INVALID
59 #define CLR_INVALID ((COLORREF)-1)
60#endif // no CLR_INVALID
61
8bbbae21
VZ
62// ----------------------------------------------------------------------------
63// Bitmap data
64// ----------------------------------------------------------------------------
65
66class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
67{
68public:
69 wxBitmapRefData();
70 virtual ~wxBitmapRefData() { Free(); }
71
72 virtual void Free();
73
74 // set the mask object to use as the mask, we take ownership of it
75 void SetMask(wxMask *mask)
76 {
77 delete m_bitmapMask;
78 m_bitmapMask = mask;
79 }
80
81 // set the HBITMAP to use as the mask
82 void SetMask(HBITMAP hbmpMask)
83 {
84 SetMask(new wxMask((WXHBITMAP)hbmpMask));
85 }
86
87 // return the mask
88 wxMask *GetMask() const { return m_bitmapMask; }
89
90public:
8bbbae21
VZ
91#if wxUSE_PALETTE
92 wxPalette m_bitmapPalette;
93#endif // wxUSE_PALETTE
94
95 // MSW-specific
96 // ------------
97
acf8e3d2 98#ifdef __WXDEBUG__
8bbbae21
VZ
99 // this field is solely for error checking: we detect selecting a bitmap
100 // into more than one DC at once or deleting a bitmap still selected into a
101 // DC (both are serious programming errors under Windows)
102 wxDC *m_selectedInto;
acf8e3d2 103#endif // __WXDEBUG__
8bbbae21 104
4676948b 105#if wxUSE_WXDIB
fa275e86
VZ
106 // when GetRawData() is called for a DDB we need to convert it to a DIB
107 // first to be able to provide direct access to it and we cache that DIB
108 // here and convert it back to DDB when UngetRawData() is called
109 wxDIB *m_dib;
4676948b 110#endif
fa275e86 111
acf8e3d2
VZ
112 // true if we have alpha transparency info and can be drawn using
113 // AlphaBlend()
114 bool m_hasAlpha;
8bbbae21 115
fa275e86
VZ
116 // true if our HBITMAP is a DIB section, false if it is a DDB
117 bool m_isDIB;
118
8bbbae21
VZ
119private:
120 // optional mask for transparent drawing
121 wxMask *m_bitmapMask;
122
123 DECLARE_NO_COPY_CLASS(wxBitmapRefData)
124};
125
10fcf31a
VZ
126// ----------------------------------------------------------------------------
127// macros
128// ----------------------------------------------------------------------------
129
4b7f2165
VZ
130IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
131IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
6d167489 132
4b7f2165 133IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
2bda0e17 134
10fcf31a
VZ
135// ============================================================================
136// implementation
137// ============================================================================
138
2cf45d69
VZ
139// ----------------------------------------------------------------------------
140// helper functions
141// ----------------------------------------------------------------------------
142
143// decide whether we should create a DIB or a DDB for the given parameters
be69f971
VZ
144//
145// NB: we always use DIBs under Windows CE as this is much simpler (even if
146// also less efficient...) and we obviously can't use them if there is no
147// DIB support compiled in at all
148#ifdef __WXWINCE__
149 static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; }
150
151 #define ALWAYS_USE_DIB
152#elif !wxUSE_WXDIB
153 // no sense in defining wxShouldCreateDIB() as we can't compile code
154 // executed if it is true, so we have to use #if's anyhow
155 #define NEVER_USE_DIB
156#else // wxUSE_WXDIB && !__WXWINCE__
157 static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
158 {
159 // here is the logic:
160 //
161 // (a) if hdc is specified, the caller explicitly wants DDB
162 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
163 // or less DIBs anyhow)
164 // (c) finally, create DIBs under Win9x even if the depth hasn't been
165 // explicitly specified but the current display depth is 24 or
166 // more and the image is "big", i.e. > 16Mb which is the
167 // theoretical limit for DDBs under Win9x
168 //
169 // consequences (all of which seem to make sense):
170 //
171 // (i) by default, DDBs are created (depth == -1 usually)
172 // (ii) DIBs can be created by explicitly specifying the depth
173 // (iii) using a DC always forces creating a DDB
174 return !hdc &&
175 (d >= 24 ||
176 (d == -1 &&
177 wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
178 }
179
180 #define SOMETIMES_USE_DIB
181#endif // different DIB usage scenarious
2cf45d69 182
10fcf31a
VZ
183// ----------------------------------------------------------------------------
184// wxBitmapRefData
185// ----------------------------------------------------------------------------
186
187wxBitmapRefData::wxBitmapRefData()
2bda0e17 188{
3ca22d5e 189#ifdef __WXDEBUG__
6d167489 190 m_selectedInto = NULL;
3ca22d5e 191#endif
6d167489 192 m_bitmapMask = NULL;
fa275e86 193
340196c0 194 m_hBitmap = (WXHBITMAP) NULL;
4676948b 195#if wxUSE_WXDIB
fa275e86 196 m_dib = NULL;
4676948b 197#endif
fa275e86
VZ
198
199 m_isDIB =
acf8e3d2 200 m_hasAlpha = FALSE;
2bda0e17
KB
201}
202
6d167489 203void wxBitmapRefData::Free()
2bda0e17 204{
d59ceba5
VZ
205 wxASSERT_MSG( !m_selectedInto,
206 wxT("deleting bitmap still selected into wxMemoryDC") );
2bda0e17 207
4676948b 208#if wxUSE_WXDIB
fa275e86 209 wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") );
4676948b 210#endif
fa275e86 211
d59ceba5 212 if ( m_hBitmap)
6d167489
VZ
213 {
214 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
215 {
f6bcfd97 216 wxLogLastError(wxT("DeleteObject(hbitmap)"));
6d167489
VZ
217 }
218 }
e7003166 219
6d167489
VZ
220 delete m_bitmapMask;
221 m_bitmapMask = NULL;
2bda0e17
KB
222}
223
10fcf31a 224// ----------------------------------------------------------------------------
6d167489 225// wxBitmap creation
10fcf31a
VZ
226// ----------------------------------------------------------------------------
227
4fe5383d
VZ
228// this function should be called from all wxBitmap ctors
229void wxBitmap::Init()
2bda0e17 230{
4fe5383d 231 // m_refData = NULL; done in the base class ctor
4fe5383d
VZ
232}
233
8bbbae21
VZ
234wxGDIImageRefData *wxBitmap::CreateData() const
235{
236 return new wxBitmapRefData;
237}
238
6d167489
VZ
239#ifdef __WIN32__
240
241bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
242{
4676948b 243#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
6d167489
VZ
244 // it may be either HICON or HCURSOR
245 HICON hicon = (HICON)icon.GetHandle();
246
247 ICONINFO iconInfo;
248 if ( !::GetIconInfo(hicon, &iconInfo) )
249 {
f6bcfd97 250 wxLogLastError(wxT("GetIconInfo"));
6d167489 251
7010702f 252 return false;
6d167489
VZ
253 }
254
255 wxBitmapRefData *refData = new wxBitmapRefData;
256 m_refData = refData;
257
d9c8e68e
VZ
258 int w = icon.GetWidth(),
259 h = icon.GetHeight();
260
261 refData->m_width = w;
262 refData->m_height = h;
6d167489
VZ
263 refData->m_depth = wxDisplayDepth();
264
265 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
d9c8e68e
VZ
266
267 // the mask returned by GetIconInfo() is inversed compared to the usual
268 // wxWin convention
8bbbae21 269 refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
6d167489 270
68f36a2c
VZ
271
272 // delete the old one now as we don't need it any more
273 ::DeleteObject(iconInfo.hbmMask);
274
7010702f 275 return true;
04ef50df 276#else
7010702f
WS
277 wxUnusedVar(icon);
278 return false;
04ef50df 279#endif
6d167489
VZ
280}
281
282#endif // Win32
283
284bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
4fe5383d
VZ
285{
286 UnRef();
07cf98cb 287
6d167489 288 if ( !cursor.Ok() )
4fe5383d 289 return FALSE;
07cf98cb 290
6d167489
VZ
291 return CopyFromIconOrCursor(cursor);
292}
293
294bool wxBitmap::CopyFromIcon(const wxIcon& icon)
295{
296 UnRef();
07cf98cb 297
6d167489
VZ
298 if ( !icon.Ok() )
299 return FALSE;
4fe5383d 300
6d167489 301 return CopyFromIconOrCursor(icon);
10fcf31a
VZ
302}
303
be69f971
VZ
304#ifndef NEVER_USE_DIB
305
b0ea5d96
VZ
306bool wxBitmap::CopyFromDIB(const wxDIB& dib)
307{
308 wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
309
be69f971 310#ifdef SOMETIMES_USE_DIB
b0ea5d96
VZ
311 HBITMAP hbitmap = dib.CreateDDB();
312 if ( !hbitmap )
313 return FALSE;
be69f971
VZ
314#else // ALWAYS_USE_DIB
315 HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast
316#endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
b0ea5d96
VZ
317
318 UnRef();
319
320 wxBitmapRefData *refData = new wxBitmapRefData;
321 m_refData = refData;
322
323 refData->m_width = dib.GetWidth();
324 refData->m_height = dib.GetHeight();
325 refData->m_depth = dib.GetDepth();
326
327 refData->m_hBitmap = (WXHBITMAP)hbitmap;
328
329#if wxUSE_PALETTE
330 wxPalette *palette = dib.CreatePalette();
331 if ( palette )
332 {
333 refData->m_bitmapPalette = *palette;
334 }
335
336 delete palette;
337#endif // wxUSE_PALETTE
338
339 return TRUE;
340}
be69f971
VZ
341
342#endif // NEVER_USE_DIB
b0ea5d96 343
10fcf31a 344wxBitmap::~wxBitmap()
2bda0e17 345{
2bda0e17
KB
346}
347
5bd3a2da 348wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
2bda0e17 349{
4fe5383d 350 Init();
2bda0e17 351
04ef50df 352#ifndef __WXMICROWIN__
6d167489
VZ
353 wxBitmapRefData *refData = new wxBitmapRefData;
354 m_refData = refData;
2bda0e17 355
5bd3a2da
VZ
356 refData->m_width = width;
357 refData->m_height = height;
358 refData->m_depth = depth;
2bda0e17 359
5bd3a2da
VZ
360 char *data;
361 if ( depth == 1 )
362 {
363 // we assume that it is in XBM format which is not quite the same as
364 // the format CreateBitmap() wants because the order of bytes in the
365 // line is inversed!
4d24ece7
VZ
366 const size_t bytesPerLine = (width + 7) / 8;
367 const size_t padding = bytesPerLine % 2;
368 const size_t len = height * ( padding + bytesPerLine );
5bd3a2da
VZ
369 data = (char *)malloc(len);
370 const char *src = bits;
371 char *dst = data;
372
373 for ( int rows = 0; rows < height; rows++ )
374 {
0765adca 375 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
5bd3a2da 376 {
0765adca
VZ
377 unsigned char val = *src++;
378 unsigned char reversed = 0;
379
380 for ( int bits = 0; bits < 8; bits++)
381 {
382 reversed <<= 1;
383 reversed |= (val & 0x01);
384 val >>= 1;
385 }
386 *dst++ = reversed;
5bd3a2da
VZ
387 }
388
389 if ( padding )
390 *dst++ = 0;
5bd3a2da
VZ
391 }
392 }
393 else
394 {
395 // bits should already be in Windows standard format
396 data = (char *)bits; // const_cast is harmless
397 }
398
399 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
6d167489
VZ
400 if ( !hbmp )
401 {
f6bcfd97 402 wxLogLastError(wxT("CreateBitmap"));
6d167489 403 }
2bda0e17 404
5bd3a2da
VZ
405 if ( data != bits )
406 {
407 free(data);
408 }
409
6d167489 410 SetHBITMAP((WXHBITMAP)hbmp);
04ef50df 411#endif
2bda0e17
KB
412}
413
2fd284a4 414// Create from XPM data
1904aa72 415#if wxUSE_IMAGE && wxUSE_XPM
4b7f2165 416bool wxBitmap::CreateFromXpm(const char **data)
1904aa72
DS
417#else
418bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data))
419#endif
2fd284a4 420{
66e23ad2 421#if wxUSE_IMAGE && wxUSE_XPM
4fe5383d
VZ
422 Init();
423
66e23ad2 424 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
4d24ece7 425
66e23ad2
VS
426 wxXPMDecoder decoder;
427 wxImage img = decoder.ReadData(data);
428 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
4d24ece7 429
c59b4b01 430 *this = wxBitmap(img);
66e23ad2
VS
431 return TRUE;
432#else
4d24ece7 433 return FALSE;
66e23ad2 434#endif
2fd284a4
JS
435}
436
debe6624 437wxBitmap::wxBitmap(int w, int h, int d)
2bda0e17 438{
4fe5383d 439 Init();
2bda0e17 440
4fe5383d 441 (void)Create(w, h, d);
2bda0e17
KB
442}
443
98ea6b7d
VZ
444wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
445{
446 Init();
447
448 (void)Create(w, h, dc);
449}
450
debe6624 451wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
2bda0e17 452{
4fe5383d 453 Init();
2bda0e17 454
6d167489 455 (void)Create(data, type, width, height, depth);
2bda0e17
KB
456}
457
2aeec9ec 458wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
2bda0e17 459{
4fe5383d 460 Init();
2bda0e17 461
4fe5383d 462 LoadFile(filename, (int)type);
2bda0e17
KB
463}
464
2cf45d69
VZ
465bool wxBitmap::Create(int width, int height, int depth)
466{
467 return DoCreate(width, height, depth, 0);
468}
469
470bool wxBitmap::Create(int width, int height, const wxDC& dc)
471{
472 wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") );
473
474 return DoCreate(width, height, -1, dc.GetHDC());
475}
476
477bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
2bda0e17 478{
6d167489
VZ
479 UnRef();
480
481 m_refData = new wxBitmapRefData;
482
2cf45d69
VZ
483 GetBitmapData()->m_width = w;
484 GetBitmapData()->m_height = h;
485
be69f971 486 HBITMAP hbmp wxDUMMY_INITIALIZE(0);
2cf45d69 487
be69f971 488#ifndef NEVER_USE_DIB
2cf45d69 489 if ( wxShouldCreateDIB(w, h, d, hdc) )
6d167489 490 {
2cf45d69
VZ
491 if ( d == -1 )
492 {
493 // create DIBs without alpha channel by default
494 d = 24;
495 }
496
497 wxDIB dib(w, h, d);
498 if ( !dib.IsOk() )
0becd470 499 return FALSE;
2cf45d69
VZ
500
501 // don't delete the DIB section in dib object dtor
502 hbmp = dib.Detach();
503
fa275e86 504 GetBitmapData()->m_isDIB = TRUE;
2cf45d69 505 GetBitmapData()->m_depth = d;
6d167489 506 }
2cf45d69 507 else // create a DDB
be69f971 508#endif // NEVER_USE_DIB
6d167489 509 {
be69f971 510#ifndef ALWAYS_USE_DIB
0becd470
VZ
511#ifndef __WXMICROWIN__
512 if ( d > 0 )
6d167489 513 {
0becd470
VZ
514 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
515 if ( !hbmp )
516 {
517 wxLogLastError(wxT("CreateBitmap"));
518 }
be69f971
VZ
519
520 GetBitmapData()->m_depth = d;
6d167489 521 }
be69f971 522 else // d == 0, create bitmap compatible with the screen
0becd470
VZ
523#endif // !__WXMICROWIN__
524 {
525 ScreenHDC dc;
526 hbmp = ::CreateCompatibleBitmap(dc, w, h);
527 if ( !hbmp )
528 {
529 wxLogLastError(wxT("CreateCompatibleBitmap"));
530 }
6d167489 531
0becd470
VZ
532 GetBitmapData()->m_depth = wxDisplayDepth();
533 }
be69f971 534#endif // !ALWAYS_USE_DIB
2cf45d69 535 }
2bda0e17 536
2cf45d69 537 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17 538
6d167489 539 return Ok();
2bda0e17
KB
540}
541
acf8e3d2 542#if wxUSE_IMAGE
0becd470 543
6d51f220 544// ----------------------------------------------------------------------------
acf8e3d2 545// wxImage to/from conversions for Microwin
6d51f220
VZ
546// ----------------------------------------------------------------------------
547
acf8e3d2
VZ
548// Microwin versions are so different from normal ones that it really doesn't
549// make sense to use #ifdefs inside the function bodies
550#ifdef __WXMICROWIN__
6d51f220 551
2cf45d69 552bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
fec19ea9 553{
acf8e3d2
VZ
554 // Set this to 1 to experiment with mask code,
555 // which currently doesn't work
556 #define USE_MASKS 0
62e1ba75 557
54a96d02 558 m_refData = new wxBitmapRefData();
e640f823
JS
559
560 // Initial attempt at a simple-minded implementation.
561 // The bitmap will always be created at the screen depth,
562 // so the 'depth' argument is ignored.
8e9ff815 563
e640f823
JS
564 HDC hScreenDC = ::GetDC(NULL);
565 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
566
567 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
62e1ba75
JS
568 HBITMAP hMaskBitmap = NULL;
569 HBITMAP hOldMaskBitmap = NULL;
570 HDC hMaskDC = NULL;
571 unsigned char maskR = 0;
572 unsigned char maskG = 0;
573 unsigned char maskB = 0;
574
54a96d02 575 // printf("Created bitmap %d\n", (int) hBitmap);
e640f823
JS
576 if (hBitmap == NULL)
577 {
578 ::ReleaseDC(NULL, hScreenDC);
8e9ff815 579 return FALSE;
e640f823
JS
580 }
581 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
e640f823
JS
582
583 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
62e1ba75
JS
584 ::ReleaseDC(NULL, hScreenDC);
585
586 // created an mono-bitmap for the possible mask
587 bool hasMask = image.HasMask();
588
589 if ( hasMask )
590 {
591#if USE_MASKS
592 // FIXME: we should be able to pass bpp = 1, but
593 // GdBlit can't handle a different depth
594#if 0
595 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
596#else
597 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
598#endif
599 maskR = image.GetMaskRed();
600 maskG = image.GetMaskGreen();
601 maskB = image.GetMaskBlue();
602
603 if (!hMaskBitmap)
604 {
605 hasMask = FALSE;
8e9ff815 606 }
62e1ba75
JS
607 else
608 {
609 hScreenDC = ::GetDC(NULL);
610 hMaskDC = ::CreateCompatibleDC(hScreenDC);
611 ::ReleaseDC(NULL, hScreenDC);
612
613 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
8e9ff815 614 }
62e1ba75
JS
615#else
616 hasMask = FALSE;
617#endif
618 }
e640f823
JS
619
620 int i, j;
621 for (i = 0; i < image.GetWidth(); i++)
622 {
8e9ff815
VZ
623 for (j = 0; j < image.GetHeight(); j++)
624 {
625 unsigned char red = image.GetRed(i, j);
626 unsigned char green = image.GetGreen(i, j);
627 unsigned char blue = image.GetBlue(i, j);
e640f823 628
8e9ff815 629 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
62e1ba75
JS
630
631 if (hasMask)
632 {
633 // scan the bitmap for the transparent colour and set the corresponding
634 // pixels in the mask to BLACK and the rest to WHITE
635 if (maskR == red && maskG == green && maskB == blue)
636 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
637 else
638 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
8e9ff815
VZ
639 }
640 }
e640f823
JS
641 }
642
643 ::SelectObject(hMemDC, hOldBitmap);
644 ::DeleteDC(hMemDC);
62e1ba75
JS
645 if (hasMask)
646 {
647 ::SelectObject(hMaskDC, hOldMaskBitmap);
8e9ff815 648 ::DeleteDC(hMaskDC);
62e1ba75 649
8bbbae21 650 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
62e1ba75 651 }
8e9ff815 652
e640f823
JS
653 SetWidth(image.GetWidth());
654 SetHeight(image.GetHeight());
655 SetDepth(screenDepth);
656 SetHBITMAP( (WXHBITMAP) hBitmap );
8e9ff815 657
e640f823
JS
658#if wxUSE_PALETTE
659 // Copy the palette from the source image
660 SetPalette(image.GetPalette());
661#endif // wxUSE_PALETTE
662
663 return TRUE;
fec19ea9
VS
664}
665
666wxImage wxBitmap::ConvertToImage() const
667{
e640f823
JS
668 // Initial attempt at a simple-minded implementation.
669 // The bitmap will always be created at the screen depth,
670 // so the 'depth' argument is ignored.
671 // TODO: transparency (create a mask image)
672
673 if (!Ok())
674 {
675 wxFAIL_MSG( wxT("bitmap is invalid") );
8e9ff815 676 return wxNullImage;
e640f823
JS
677 }
678
679 wxImage image;
680
681 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
682
683 // create an wxImage object
684 int width = GetWidth();
685 int height = GetHeight();
686 image.Create( width, height );
687 unsigned char *data = image.GetData();
688 if( !data )
689 {
690 wxFAIL_MSG( wxT("could not allocate data for image") );
691 return wxNullImage;
692 }
8e9ff815 693
e640f823
JS
694 HDC hScreenDC = ::GetDC(NULL);
695
696 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
697 ::ReleaseDC(NULL, hScreenDC);
698
699 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
8e9ff815 700
e640f823
JS
701 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
702
703 int i, j;
704 for (i = 0; i < GetWidth(); i++)
705 {
8e9ff815
VZ
706 for (j = 0; j < GetHeight(); j++)
707 {
708 COLORREF color = ::GetPixel(hMemDC, i, j);
709 unsigned char red = GetRValue(color);
710 unsigned char green = GetGValue(color);
711 unsigned char blue = GetBValue(color);
712
713 image.SetRGB(i, j, red, green, blue);
714 }
e640f823
JS
715 }
716
717 ::SelectObject(hMemDC, hOldBitmap);
718 ::DeleteDC(hMemDC);
8e9ff815 719
e640f823
JS
720#if wxUSE_PALETTE
721 // Copy the palette from the source image
722 if (GetPalette())
723 image.SetPalette(* GetPalette());
724#endif // wxUSE_PALETTE
725
726 return image;
acf8e3d2
VZ
727}
728
729#endif // __WXMICROWIN__
730
731// ----------------------------------------------------------------------------
732// wxImage to/from conversions
733// ----------------------------------------------------------------------------
734
be69f971
VZ
735#if wxUSE_WXDIB
736
2cf45d69
VZ
737bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
738{
739 return CreateFromImage(image, depth, 0);
740}
741
742bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
743{
744 wxCHECK_MSG( dc.Ok(), FALSE,
745 _T("invalid HDC in wxBitmap::CreateFromImage()") );
746
747 return CreateFromImage(image, -1, dc.GetHDC());
748}
749
be69f971 750bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
acf8e3d2
VZ
751{
752 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
753
754 UnRef();
755
756 // first convert the image to DIB
757 const int h = image.GetHeight();
758 const int w = image.GetWidth();
759
760 wxDIB dib(image);
761 if ( !dib.IsOk() )
762 return FALSE;
763
764
765 // store the bitmap parameters
766 wxBitmapRefData *refData = new wxBitmapRefData;
767 refData->m_width = w;
768 refData->m_height = h;
acf8e3d2
VZ
769 refData->m_hasAlpha = image.HasAlpha();
770
771 m_refData = refData;
772
773
774 // next either store DIB as is or create a DDB from it
be69f971 775 HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
acf8e3d2 776
2cf45d69 777 // are we going to use DIB?
4b8ab2f9
VZ
778 //
779 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
780 if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
2cf45d69
VZ
781 {
782 // don't delete the DIB section in dib object dtor
783 hbitmap = dib.Detach();
784
fa275e86 785 refData->m_isDIB = TRUE;
2cf45d69
VZ
786 refData->m_depth = dib.GetDepth();
787 }
be69f971 788#ifndef ALWAYS_USE_DIB
2cf45d69 789 else // we need to convert DIB to DDB
acf8e3d2 790 {
b0ea5d96 791 hbitmap = dib.CreateDDB((HDC)hdc);
2cf45d69 792
3cfd077f 793 refData->m_depth = depth == -1 ? dib.GetDepth() : depth;
acf8e3d2 794 }
be69f971 795#endif // !ALWAYS_USE_DIB
acf8e3d2
VZ
796
797 // validate this object
798 SetHBITMAP((WXHBITMAP)hbitmap);
799
acf8e3d2
VZ
800 // finally also set the mask if we have one
801 if ( image.HasMask() )
802 {
803 SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
804 image.GetMaskGreen(),
805 image.GetMaskBlue())));
806 }
807
808 return TRUE;
809}
810
811wxImage wxBitmap::ConvertToImage() const
812{
6faac21b 813 // convert DDB to DIB
be69f971 814 wxDIB dib(*this);
c8688869 815
003e6c07
DS
816 if ( !dib.IsOk() )
817 {
818 return wxNullImage;
819 }
820
6faac21b 821 // and then DIB to our wxImage
003e6c07
DS
822 wxImage image = dib.ConvertToImage();
823 if ( !image.Ok() )
824 {
825 return wxNullImage;
826 }
827
6faac21b
VZ
828 // now do the same for the mask, if we have any
829 HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
830 if ( hbmpMask )
fec19ea9 831 {
6faac21b
VZ
832 wxDIB dibMask(hbmpMask);
833 if ( dibMask.IsOk() )
003e6c07 834 {
6faac21b
VZ
835 // TODO: use wxRawBitmap to iterate over DIB
836
837 // we hard code the mask colour for now but we could also make an
838 // effort (and waste time) to choose a colour not present in the
839 // image already to avoid having to fudge the pixels below --
840 // whether it's worth to do it is unclear however
841 static const int MASK_RED = 1;
842 static const int MASK_GREEN = 2;
843 static const int MASK_BLUE = 3;
844 static const int MASK_BLUE_REPLACEMENT = 2;
845
846 const int h = dibMask.GetHeight();
847 const int w = dibMask.GetWidth();
848 const int bpp = dibMask.GetDepth();
849 const int maskBytesPerPixel = bpp >> 3;
850 const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
851 unsigned char *data = image.GetData();
852
853 // remember that DIBs are stored in bottom to top order
854 unsigned char *
855 maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
856
857 for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
003e6c07 858 {
6faac21b
VZ
859 // traverse one mask DIB line
860 unsigned char *mask = maskLineStart;
861 for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
003e6c07 862 {
6faac21b
VZ
863 // should this pixel be transparent?
864 if ( *mask )
003e6c07 865 {
6faac21b
VZ
866 // no, check that it isn't transparent by accident
867 if ( (data[0] == MASK_RED) &&
868 (data[1] == MASK_GREEN) &&
869 (data[2] == MASK_BLUE) )
870 {
871 // we have to fudge the colour a bit to prevent
872 // this pixel from appearing transparent
873 data[2] = MASK_BLUE_REPLACEMENT;
874 }
875
876 data += 3;
877 }
878 else // yes, transparent pixel
879 {
880 *data++ = MASK_RED;
881 *data++ = MASK_GREEN;
882 *data++ = MASK_BLUE;
003e6c07 883 }
003e6c07 884 }
003e6c07 885 }
003e6c07 886
6faac21b
VZ
887 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
888 }
fec19ea9 889 }
fec19ea9
VS
890
891 return image;
892}
893
be69f971
VZ
894#endif // wxUSE_WXDIB
895
6d51f220
VZ
896#endif // wxUSE_IMAGE
897
acf8e3d2
VZ
898// ----------------------------------------------------------------------------
899// loading and saving bitmaps
900// ----------------------------------------------------------------------------
901
debe6624 902bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 903{
6d167489 904 UnRef();
2bda0e17 905
6d167489 906 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 907
6d167489
VZ
908 if ( handler )
909 {
910 m_refData = new wxBitmapRefData;
2bda0e17 911
6d167489
VZ
912 return handler->LoadFile(this, filename, type, -1, -1);
913 }
6d51f220 914#if wxUSE_IMAGE
be69f971 915 else // no bitmap handler found
b75dd496 916 {
6d167489 917 wxImage image;
6d51f220
VZ
918 if ( image.LoadFile( filename, type ) && image.Ok() )
919 {
368d59f0 920 *this = wxBitmap(image);
6d167489 921
6d51f220
VZ
922 return TRUE;
923 }
b75dd496 924 }
6d51f220
VZ
925#endif // wxUSE_IMAGE
926
927 return FALSE;
2bda0e17
KB
928}
929
debe6624 930bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 931{
6d167489 932 UnRef();
2bda0e17 933
6d167489 934 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 935
6d167489
VZ
936 if ( !handler )
937 {
9b601c24 938 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
2bda0e17 939
6d167489
VZ
940 return FALSE;
941 }
1d792928 942
6d167489 943 m_refData = new wxBitmapRefData;
1d792928 944
6d167489 945 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
946}
947
d275c7eb
VZ
948bool wxBitmap::SaveFile(const wxString& filename,
949 int type,
950 const wxPalette *palette)
2bda0e17 951{
6d167489 952 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 953
6d167489
VZ
954 if ( handler )
955 {
956 return handler->SaveFile(this, filename, type, palette);
957 }
6d51f220 958#if wxUSE_IMAGE
be69f971 959 else // no bitmap handler found
6d167489
VZ
960 {
961 // FIXME what about palette? shouldn't we use it?
368d59f0 962 wxImage image = ConvertToImage();
6d51f220
VZ
963 if ( image.Ok() )
964 {
965 return image.SaveFile(filename, type);
966 }
6d167489 967 }
6d51f220
VZ
968#endif // wxUSE_IMAGE
969
970 return FALSE;
2bda0e17
KB
971}
972
4b7f2165
VZ
973// ----------------------------------------------------------------------------
974// sub bitmap extraction
975// ----------------------------------------------------------------------------
976
977wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
978{
979 wxCHECK_MSG( Ok() &&
980 (rect.x >= 0) && (rect.y >= 0) &&
981 (rect.x+rect.width <= GetWidth()) &&
982 (rect.y+rect.height <= GetHeight()),
983 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
984
acf8e3d2 985 wxBitmap ret( rect.width, rect.height );
4b7f2165
VZ
986 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
987
8e9ff815 988#ifndef __WXMICROWIN__
acf8e3d2
VZ
989 // TODO: copy alpha channel data if any
990
4b7f2165 991 // copy bitmap data
acf8e3d2
VZ
992 MemoryHDC dcSrc,
993 dcDst;
8e9ff815
VZ
994
995 {
996 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
997 selectDst(dcDst, GetHbitmapOf(ret));
998
acf8e3d2
VZ
999 if ( !selectSrc || !selectDst )
1000 {
1001 wxLogLastError(_T("SelectObjct(hBitmap)"));
1002 }
1003
8e9ff815
VZ
1004 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1005 dcSrc, rect.x, rect.y, SRCCOPY) )
1006 {
1007 wxLogLastError(_T("BitBlt"));
1008 }
1009 }
4b7f2165
VZ
1010
1011 // copy mask if there is one
8e9ff815 1012 if ( GetMask() )
4b7f2165
VZ
1013 {
1014 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1015
8e9ff815
VZ
1016 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1017 selectDst(dcDst, hbmpMask);
1018
1019 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1020 dcSrc, rect.x, rect.y, SRCCOPY) )
1021 {
1022 wxLogLastError(_T("BitBlt"));
1023 }
4b7f2165
VZ
1024
1025 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1026 ret.SetMask(mask);
1027 }
8e9ff815 1028#endif // !__WXMICROWIN__
4b7f2165
VZ
1029
1030 return ret;
1031}
1032
6d167489
VZ
1033// ----------------------------------------------------------------------------
1034// wxBitmap accessors
1035// ----------------------------------------------------------------------------
2bda0e17 1036
adf9e099 1037#if wxUSE_PALETTE
8bbbae21 1038wxPalette* wxBitmap::GetPalette() const
2bda0e17 1039{
8bbbae21
VZ
1040 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1041 : (wxPalette *) NULL;
1042}
adf9e099 1043#endif
8bbbae21
VZ
1044
1045wxMask *wxBitmap::GetMask() const
1046{
1047 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1048}
2bda0e17 1049
3ca22d5e
MB
1050#ifdef __WXDEBUG__
1051
8bbbae21
VZ
1052wxDC *wxBitmap::GetSelectedInto() const
1053{
1054 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
2bda0e17
KB
1055}
1056
3ca22d5e
MB
1057#endif
1058
8bbbae21
VZ
1059#if WXWIN_COMPATIBILITY_2_4
1060
1061int wxBitmap::GetQuality() const
1062{
1063 return 0;
1064}
1065
1066#endif // WXWIN_COMPATIBILITY_2_4
1067
10b41b53
VZ
1068void wxBitmap::UseAlpha()
1069{
1070 if ( GetBitmapData() )
1071 GetBitmapData()->m_hasAlpha = true;
1072}
1073
acf8e3d2
VZ
1074bool wxBitmap::HasAlpha() const
1075{
1076 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1077}
1078
8bbbae21
VZ
1079// ----------------------------------------------------------------------------
1080// wxBitmap setters
1081// ----------------------------------------------------------------------------
1082
3ca22d5e
MB
1083#ifdef __WXDEBUG__
1084
8bbbae21
VZ
1085void wxBitmap::SetSelectedInto(wxDC *dc)
1086{
1087 if ( GetBitmapData() )
1088 GetBitmapData()->m_selectedInto = dc;
2bda0e17
KB
1089}
1090
3ca22d5e
MB
1091#endif
1092
d275c7eb
VZ
1093#if wxUSE_PALETTE
1094
2bda0e17
KB
1095void wxBitmap::SetPalette(const wxPalette& palette)
1096{
6d167489 1097 EnsureHasData();
2bda0e17 1098
6d167489 1099 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
1100}
1101
d275c7eb
VZ
1102#endif // wxUSE_PALETTE
1103
2bda0e17
KB
1104void wxBitmap::SetMask(wxMask *mask)
1105{
6d167489 1106 EnsureHasData();
2bda0e17 1107
8bbbae21
VZ
1108 GetBitmapData()->SetMask(mask);
1109}
1110
8bbbae21
VZ
1111#if WXWIN_COMPATIBILITY_2_4
1112
1113void wxBitmap::SetQuality(int WXUNUSED(quality))
1114{
2bda0e17
KB
1115}
1116
8bbbae21
VZ
1117#endif // WXWIN_COMPATIBILITY_2_4
1118
2cf45d69
VZ
1119// ----------------------------------------------------------------------------
1120// raw bitmap access support
1121// ----------------------------------------------------------------------------
1122
1e3c12d7 1123#ifdef wxHAVE_RAW_BITMAP
b9bcaf11 1124void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
2cf45d69 1125{
4676948b 1126#if wxUSE_WXDIB
2cf45d69
VZ
1127 if ( !Ok() )
1128 {
1129 // no bitmap, no data (raw or otherwise)
b9bcaf11 1130 return NULL;
2cf45d69
VZ
1131 }
1132
fa275e86
VZ
1133 // if we're already a DIB we can access our data directly, but if not we
1134 // need to convert this DDB to a DIB section and use it for raw access and
1135 // then convert it back
1136 HBITMAP hDIB;
1137 if ( !GetBitmapData()->m_isDIB )
1138 {
1139 wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE,
1140 _T("GetRawData() may be called only once") );
1141
1142 wxDIB *dib = new wxDIB(*this);
1143 if ( !dib->IsOk() )
1144 {
1145 delete dib;
1146
b9bcaf11 1147 return NULL;
fa275e86
VZ
1148 }
1149
1150 // we'll free it in UngetRawData()
1151 GetBitmapData()->m_dib = dib;
1152
1153 hDIB = dib->GetHandle();
1154 }
1155 else // we're a DIB
1156 {
1157 hDIB = GetHbitmap();
1158 }
1159
2cf45d69 1160 DIBSECTION ds;
fa275e86 1161 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
2cf45d69 1162 {
fa275e86
VZ
1163 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1164
b9bcaf11
VZ
1165 return NULL;
1166 }
1167
1168 // check that the bitmap is in correct format
1169 if ( ds.dsBm.bmBitsPixel != bpp )
1170 {
1171 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1172
1173 return NULL;
2cf45d69
VZ
1174 }
1175
b9bcaf11 1176 // ok, store the relevant info in wxPixelDataBase
2cf45d69
VZ
1177 const LONG h = ds.dsBm.bmHeight;
1178
b9bcaf11
VZ
1179 data.m_width = ds.dsBm.bmWidth;
1180 data.m_height = h;
2cf45d69
VZ
1181
1182 // remember that DIBs are stored in top to bottom order!
1183 const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
b9bcaf11
VZ
1184 data.m_stride = -bytesPerRow;
1185
1186 char *bits = (char *)ds.dsBm.bmBits;
2cf45d69
VZ
1187 if ( h > 1 )
1188 {
b9bcaf11 1189 bits += (h - 1)*bytesPerRow;
2cf45d69
VZ
1190 }
1191
b9bcaf11 1192 return bits;
4676948b
JS
1193#else
1194 return NULL;
1195#endif
2cf45d69
VZ
1196}
1197
b9bcaf11 1198void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
13c13ecb 1199{
4676948b 1200#if wxUSE_WXDIB
fa275e86
VZ
1201 if ( !Ok() )
1202 return;
1203
b9bcaf11
VZ
1204 // the cast is ugly but we can't do without it and without making this
1205 // function template (and hence inline) unfortunately
1206 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> PixelData;
1207 PixelData& data = (PixelData &)dataBase;
1208
1209 if ( !data )
7af64aac
VZ
1210 {
1211 // invalid data, don't crash -- but don't assert neither as we're
b9bcaf11 1212 // called automatically from wxPixelDataBase dtor and so there is no
7af64aac
VZ
1213 // way to prevent this from happening
1214 return;
1215 }
1216
b9bcaf11 1217 if ( GetBitmapData()->m_hasAlpha )
13c13ecb 1218 {
b9bcaf11
VZ
1219 // AlphaBlend() wants to have premultiplied source alpha but
1220 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1221 // them here now
1222 PixelData::Iterator p(data);
106c33bb 1223
b9bcaf11
VZ
1224 const int w = data.GetWidth();
1225 const int h = data.GetHeight();
1226
1227 for ( int y = 0; y < h; y++ )
13c13ecb 1228 {
b9bcaf11 1229 PixelData::Iterator rowStart = p;
106c33bb 1230
b9bcaf11
VZ
1231 for ( int x = 0; x < w; x++ )
1232 {
1233 const unsigned alpha = p.Alpha();
106c33bb 1234
b9bcaf11
VZ
1235 p.Red() = (p.Red() * alpha + 127) / 255;
1236 p.Blue() = (p.Blue() * alpha + 127) / 255;
1237 p.Green() = (p.Green() * alpha + 127) / 255;
106c33bb 1238
b9bcaf11
VZ
1239 ++p;
1240 }
fa275e86 1241
b9bcaf11
VZ
1242 p = rowStart;
1243 p.OffsetY(data, 1);
1244 }
289307fa 1245 }
b9bcaf11 1246
289307fa
VZ
1247 // if we're a DDB we need to convert DIB back to DDB now to make the
1248 // changes made via raw bitmap access effective
1249 if ( !GetBitmapData()->m_isDIB )
1250 {
1251 wxDIB *dib = GetBitmapData()->m_dib;
1252 GetBitmapData()->m_dib = NULL;
fa275e86 1253
289307fa 1254 // TODO: convert
fa275e86 1255
289307fa 1256 delete dib;
fa275e86 1257 }
289307fa 1258#endif // wxUSE_WXDIB
13c13ecb 1259}
1e3c12d7 1260#endif // #ifdef wxHAVE_RAW_BITMAP
13c13ecb 1261
6d167489
VZ
1262// ----------------------------------------------------------------------------
1263// wxMask
1264// ----------------------------------------------------------------------------
2bda0e17 1265
10fcf31a 1266wxMask::wxMask()
2bda0e17
KB
1267{
1268 m_maskBitmap = 0;
1269}
1270
1271// Construct a mask from a bitmap and a colour indicating
1272// the transparent area
1273wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1274{
1275 m_maskBitmap = 0;
6d167489 1276 Create(bitmap, colour);
2bda0e17
KB
1277}
1278
1279// Construct a mask from a bitmap and a palette index indicating
1280// the transparent area
debe6624 1281wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1282{
1283 m_maskBitmap = 0;
6d167489 1284 Create(bitmap, paletteIndex);
2bda0e17
KB
1285}
1286
1287// Construct a mask from a mono bitmap (copies the bitmap).
1288wxMask::wxMask(const wxBitmap& bitmap)
1289{
1290 m_maskBitmap = 0;
6d167489 1291 Create(bitmap);
2bda0e17
KB
1292}
1293
10fcf31a 1294wxMask::~wxMask()
2bda0e17
KB
1295{
1296 if ( m_maskBitmap )
1297 ::DeleteObject((HBITMAP) m_maskBitmap);
1298}
1299
1300// Create a mask from a mono bitmap (copies the bitmap).
1301bool wxMask::Create(const wxBitmap& bitmap)
1302{
04ef50df 1303#ifndef __WXMICROWIN__
a58a12e9
VZ
1304 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1305 _T("can't create mask from invalid or not monochrome bitmap") );
1306
2bda0e17 1307 if ( m_maskBitmap )
6d167489
VZ
1308 {
1309 ::DeleteObject((HBITMAP) m_maskBitmap);
1310 m_maskBitmap = 0;
1311 }
a58a12e9 1312
6d167489
VZ
1313 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1314 bitmap.GetWidth(),
1315 bitmap.GetHeight(),
1316 1, 1, 0
1317 );
1318 HDC srcDC = CreateCompatibleDC(0);
1319 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1320 HDC destDC = CreateCompatibleDC(0);
1321 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1322 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1323 SelectObject(srcDC, 0);
1324 DeleteDC(srcDC);
1325 SelectObject(destDC, 0);
1326 DeleteDC(destDC);
1327 return TRUE;
04ef50df
JS
1328#else
1329 return FALSE;
1330#endif
2bda0e17
KB
1331}
1332
1333// Create a mask from a bitmap and a palette index indicating
1334// the transparent area
debe6624 1335bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1336{
1337 if ( m_maskBitmap )
1d792928 1338 {
6d167489
VZ
1339 ::DeleteObject((HBITMAP) m_maskBitmap);
1340 m_maskBitmap = 0;
1d792928 1341 }
d275c7eb
VZ
1342
1343#if wxUSE_PALETTE
6d167489
VZ
1344 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1345 {
1346 unsigned char red, green, blue;
1347 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1348 {
1349 wxColour transparentColour(red, green, blue);
1350 return Create(bitmap, transparentColour);
1351 }
1352 }
d275c7eb
VZ
1353#endif // wxUSE_PALETTE
1354
6d167489 1355 return FALSE;
2bda0e17
KB
1356}
1357
1358// Create a mask from a bitmap and a colour indicating
1359// the transparent area
1360bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1361{
04ef50df 1362#ifndef __WXMICROWIN__
4b7f2165
VZ
1363 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1364
2bda0e17 1365 if ( m_maskBitmap )
1d792928 1366 {
6d167489
VZ
1367 ::DeleteObject((HBITMAP) m_maskBitmap);
1368 m_maskBitmap = 0;
1369 }
4b7f2165
VZ
1370
1371 int width = bitmap.GetWidth(),
1372 height = bitmap.GetHeight();
1373
1374 // scan the bitmap for the transparent colour and set the corresponding
1375 // pixels in the mask to BLACK and the rest to WHITE
f423f4db 1376 COLORREF maskColour = wxColourToPalRGB(colour);
4b7f2165
VZ
1377 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1378
1379 HDC srcDC = ::CreateCompatibleDC(NULL);
1380 HDC destDC = ::CreateCompatibleDC(NULL);
1381 if ( !srcDC || !destDC )
1382 {
f6bcfd97 1383 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1384 }
1385
0bafad0c
VZ
1386 bool ok = TRUE;
1387
1e6feb95
VZ
1388 // SelectObject() will fail
1389 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1390 _T("bitmap can't be selected in another DC") );
1391
0bafad0c
VZ
1392 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1393 if ( !hbmpSrcOld )
6d167489 1394 {
f6bcfd97 1395 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1396
1397 ok = FALSE;
4b7f2165 1398 }
0bafad0c
VZ
1399
1400 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1401 if ( !hbmpDstOld )
4b7f2165 1402 {
f6bcfd97 1403 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1404
1405 ok = FALSE;
1d792928 1406 }
2bda0e17 1407
59ff46cb 1408 if ( ok )
2bda0e17 1409 {
59ff46cb
VZ
1410 // this will create a monochrome bitmap with 0 points for the pixels
1411 // which have the same value as the background colour and 1 for the
1412 // others
1413 ::SetBkColor(srcDC, maskColour);
1414 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
2bda0e17 1415 }
4b7f2165 1416
0bafad0c 1417 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1418 ::DeleteDC(srcDC);
0bafad0c 1419 ::SelectObject(destDC, hbmpDstOld);
6d167489 1420 ::DeleteDC(destDC);
4b7f2165 1421
0bafad0c 1422 return ok;
59ff46cb 1423#else // __WXMICROWIN__
04ef50df 1424 return FALSE;
59ff46cb 1425#endif // __WXMICROWIN__/!__WXMICROWIN__
6d167489
VZ
1426}
1427
1428// ----------------------------------------------------------------------------
1429// wxBitmapHandler
1430// ----------------------------------------------------------------------------
1d792928 1431
6d167489
VZ
1432bool wxBitmapHandler::Create(wxGDIImage *image,
1433 void *data,
1434 long flags,
1435 int width, int height, int depth)
1436{
1437 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1438
8c1375b9 1439 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
2bda0e17
KB
1440}
1441
6d167489
VZ
1442bool wxBitmapHandler::Load(wxGDIImage *image,
1443 const wxString& name,
1444 long flags,
1445 int width, int height)
2bda0e17 1446{
6d167489 1447 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1448
6d167489
VZ
1449 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1450}
2bda0e17 1451
6d167489
VZ
1452bool wxBitmapHandler::Save(wxGDIImage *image,
1453 const wxString& name,
1454 int type)
2bda0e17 1455{
6d167489
VZ
1456 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1457
1458 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
2bda0e17
KB
1459}
1460
6d167489
VZ
1461bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1462 void *WXUNUSED(data),
1463 long WXUNUSED(type),
1464 int WXUNUSED(width),
1465 int WXUNUSED(height),
1466 int WXUNUSED(depth))
2bda0e17 1467{
6d167489 1468 return FALSE;
2bda0e17
KB
1469}
1470
6d167489
VZ
1471bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1472 const wxString& WXUNUSED(name),
1473 long WXUNUSED(type),
1474 int WXUNUSED(desiredWidth),
1475 int WXUNUSED(desiredHeight))
2bda0e17 1476{
6d167489 1477 return FALSE;
2bda0e17
KB
1478}
1479
6d167489
VZ
1480bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1481 const wxString& WXUNUSED(name),
1482 int WXUNUSED(type),
1483 const wxPalette *WXUNUSED(palette))
2bda0e17 1484{
6d167489 1485 return FALSE;
7b46ecac
JS
1486}
1487
6d167489
VZ
1488// ----------------------------------------------------------------------------
1489// DIB functions
1490// ----------------------------------------------------------------------------
1491
04ef50df 1492#ifndef __WXMICROWIN__
6d167489
VZ
1493bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1494 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1495{
1496 unsigned long i, headerSize;
7b46ecac
JS
1497
1498 // Allocate space for a DIB header
1499 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
999836aa
VZ
1500 LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1501 LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
7b46ecac
JS
1502
1503 GetPaletteEntries(hPal, 0, 256, lpPe);
1504
7b46ecac
JS
1505 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1506
7b46ecac
JS
1507 // Fill in the static parts of the DIB header
1508 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1509 lpDIBheader->bmiHeader.biWidth = xSize;
1510 lpDIBheader->bmiHeader.biHeight = ySize;
1511 lpDIBheader->bmiHeader.biPlanes = 1;
1512
1513 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1514 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1515 lpDIBheader->bmiHeader.biCompression = BI_RGB;
6d167489 1516 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
7b46ecac
JS
1517 lpDIBheader->bmiHeader.biClrUsed = 256;
1518
1519
1520 // Initialize the DIB palette
1521 for (i = 0; i < 256; i++) {
1522 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1523 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1524 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1525 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1526 }
1527
1528 *lpDIBHeader = lpDIBheader;
1529
6d167489 1530 return TRUE;
7b46ecac
JS
1531}
1532
6d167489 1533void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1534{
6d167489 1535 free(lpDIBHeader);
7b46ecac 1536}
04ef50df 1537#endif
7b46ecac 1538
4b7f2165 1539// ----------------------------------------------------------------------------
211b54b1 1540// global helper functions implemented here
4b7f2165
VZ
1541// ----------------------------------------------------------------------------
1542
211b54b1
VZ
1543// helper of wxBitmapToHICON/HCURSOR
1544static
1545HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1546 bool iconWanted,
1547 int hotSpotX,
1548 int hotSpotY)
1549{
1550 if ( !bmp.Ok() )
1551 {
1552 // we can't create an icon/cursor form nothing
1553 return 0;
1554 }
1555
1556 wxMask *mask = bmp.GetMask();
1557 if ( !mask )
1558 {
1559 // we must have a mask for an icon, so even if it's probably incorrect,
1560 // do create it (grey is the "standard" transparent colour)
1561 mask = new wxMask(bmp, *wxLIGHT_GREY);
1562 }
1563
1564 ICONINFO iconInfo;
4104d2b3 1565 wxZeroMemory(iconInfo);
211b54b1
VZ
1566 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1567 if ( !iconWanted )
1568 {
1569 iconInfo.xHotspot = hotSpotX;
1570 iconInfo.yHotspot = hotSpotY;
1571 }
1572
1573 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1574 iconInfo.hbmColor = GetHbitmapOf(bmp);
1575
1576 // black out the transparent area to preserve background colour, because
1577 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1578 // the mask to the dest rect.
1579 {
1580 MemoryHDC dcSrc, dcDst;
1581 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1582 selectBitmap(dcDst, iconInfo.hbmColor);
1583
1584 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1585 dcSrc, 0, 0, SRCAND) )
1586 {
1587 wxLogLastError(_T("BitBlt"));
1588 }
1589 }
1590
1591 HICON hicon = ::CreateIconIndirect(&iconInfo);
1592
1593 if ( !bmp.GetMask() )
1594 {
1595 // we created the mask, now delete it
1596 delete mask;
1597 }
1598
1599 // delete the inverted mask bitmap we created as well
1600 ::DeleteObject(iconInfo.hbmMask);
1601
1602 return hicon;
1603}
1604
1605HICON wxBitmapToHICON(const wxBitmap& bmp)
1606{
1607 return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
1608}
1609
1610HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1611{
1612 return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
1613}
1614
1615HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
4b7f2165 1616{
04ef50df 1617#ifndef __WXMICROWIN__
4b7f2165
VZ
1618 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1619
1620 // get width/height from the bitmap if not given
1621 if ( !w || !h )
1622 {
1623 BITMAP bm;
1624 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1625 w = bm.bmWidth;
1626 h = bm.bmHeight;
1627 }
1628
1629 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1630 HDC hdcDst = ::CreateCompatibleDC(NULL);
1631 if ( !hdcSrc || !hdcDst )
1632 {
f6bcfd97 1633 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1634 }
1635
1636 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1637 if ( !hbmpInvMask )
1638 {
f6bcfd97 1639 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1640 }
1641
e7222d65
JS
1642 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1643 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
4b7f2165
VZ
1644 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1645 hdcSrc, 0, 0,
1646 NOTSRCCOPY) )
1647 {
f6bcfd97 1648 wxLogLastError(wxT("BitBlt"));
4b7f2165 1649 }
7b46ecac 1650
e7222d65
JS
1651 // Deselect objects
1652 SelectObject(hdcSrc,srcTmp);
1653 SelectObject(hdcDst,dstTmp);
1654
4b7f2165
VZ
1655 ::DeleteDC(hdcSrc);
1656 ::DeleteDC(hdcDst);
1657
1658 return hbmpInvMask;
04ef50df
JS
1659#else
1660 return 0;
1661#endif
4b7f2165 1662}