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