]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
build fixes for wxUSE_STL==1
[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{
888dde65 538 wxCHECK_MSG( dc.IsOk(), false, _T("invalid HDC in wxBitmap::Create()") );
2cf45d69 539
888dde65
RR
540 const wxMSWDCImpl *impl = wxDynamicCast( dc.GetImpl(), wxMSWDCImpl );
541
542 if (impl)
543 return DoCreate(width, height, -1, impl->GetHDC());
544 else
545 return false;
2cf45d69
VZ
546}
547
548bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
2bda0e17 549{
6d167489
VZ
550 UnRef();
551
552 m_refData = new wxBitmapRefData;
553
2cf45d69
VZ
554 GetBitmapData()->m_width = w;
555 GetBitmapData()->m_height = h;
556
be69f971 557 HBITMAP hbmp wxDUMMY_INITIALIZE(0);
2cf45d69 558
be69f971 559#ifndef NEVER_USE_DIB
2cf45d69 560 if ( wxShouldCreateDIB(w, h, d, hdc) )
6d167489 561 {
2cf45d69
VZ
562 if ( d == -1 )
563 {
564 // create DIBs without alpha channel by default
565 d = 24;
566 }
567
568 wxDIB dib(w, h, d);
569 if ( !dib.IsOk() )
fcf90ee1 570 return false;
2cf45d69
VZ
571
572 // don't delete the DIB section in dib object dtor
573 hbmp = dib.Detach();
574
fcf90ee1 575 GetBitmapData()->m_isDIB = true;
2cf45d69 576 GetBitmapData()->m_depth = d;
6d167489 577 }
2cf45d69 578 else // create a DDB
be69f971 579#endif // NEVER_USE_DIB
6d167489 580 {
be69f971 581#ifndef ALWAYS_USE_DIB
0becd470
VZ
582#ifndef __WXMICROWIN__
583 if ( d > 0 )
6d167489 584 {
0becd470
VZ
585 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
586 if ( !hbmp )
587 {
588 wxLogLastError(wxT("CreateBitmap"));
589 }
be69f971
VZ
590
591 GetBitmapData()->m_depth = d;
6d167489 592 }
be69f971 593 else // d == 0, create bitmap compatible with the screen
0becd470
VZ
594#endif // !__WXMICROWIN__
595 {
596 ScreenHDC dc;
597 hbmp = ::CreateCompatibleBitmap(dc, w, h);
598 if ( !hbmp )
599 {
600 wxLogLastError(wxT("CreateCompatibleBitmap"));
601 }
6d167489 602
0becd470
VZ
603 GetBitmapData()->m_depth = wxDisplayDepth();
604 }
be69f971 605#endif // !ALWAYS_USE_DIB
2cf45d69 606 }
2bda0e17 607
2cf45d69 608 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17 609
6d167489 610 return Ok();
2bda0e17
KB
611}
612
acf8e3d2 613#if wxUSE_IMAGE
0becd470 614
6d51f220 615// ----------------------------------------------------------------------------
acf8e3d2 616// wxImage to/from conversions for Microwin
6d51f220
VZ
617// ----------------------------------------------------------------------------
618
acf8e3d2
VZ
619// Microwin versions are so different from normal ones that it really doesn't
620// make sense to use #ifdefs inside the function bodies
621#ifdef __WXMICROWIN__
6d51f220 622
2cf45d69 623bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
fec19ea9 624{
acf8e3d2
VZ
625 // Set this to 1 to experiment with mask code,
626 // which currently doesn't work
627 #define USE_MASKS 0
62e1ba75 628
54a96d02 629 m_refData = new wxBitmapRefData();
e640f823
JS
630
631 // Initial attempt at a simple-minded implementation.
632 // The bitmap will always be created at the screen depth,
633 // so the 'depth' argument is ignored.
8e9ff815 634
e640f823
JS
635 HDC hScreenDC = ::GetDC(NULL);
636 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
637
638 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
62e1ba75
JS
639 HBITMAP hMaskBitmap = NULL;
640 HBITMAP hOldMaskBitmap = NULL;
641 HDC hMaskDC = NULL;
642 unsigned char maskR = 0;
643 unsigned char maskG = 0;
644 unsigned char maskB = 0;
645
54a96d02 646 // printf("Created bitmap %d\n", (int) hBitmap);
e640f823
JS
647 if (hBitmap == NULL)
648 {
649 ::ReleaseDC(NULL, hScreenDC);
fcf90ee1 650 return false;
e640f823
JS
651 }
652 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
e640f823
JS
653
654 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
62e1ba75
JS
655 ::ReleaseDC(NULL, hScreenDC);
656
657 // created an mono-bitmap for the possible mask
658 bool hasMask = image.HasMask();
659
660 if ( hasMask )
661 {
662#if USE_MASKS
663 // FIXME: we should be able to pass bpp = 1, but
664 // GdBlit can't handle a different depth
665#if 0
666 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
667#else
668 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
669#endif
670 maskR = image.GetMaskRed();
671 maskG = image.GetMaskGreen();
672 maskB = image.GetMaskBlue();
673
674 if (!hMaskBitmap)
675 {
fcf90ee1 676 hasMask = false;
8e9ff815 677 }
62e1ba75
JS
678 else
679 {
680 hScreenDC = ::GetDC(NULL);
681 hMaskDC = ::CreateCompatibleDC(hScreenDC);
682 ::ReleaseDC(NULL, hScreenDC);
683
684 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
8e9ff815 685 }
62e1ba75 686#else
fcf90ee1 687 hasMask = false;
62e1ba75
JS
688#endif
689 }
e640f823
JS
690
691 int i, j;
692 for (i = 0; i < image.GetWidth(); i++)
693 {
8e9ff815
VZ
694 for (j = 0; j < image.GetHeight(); j++)
695 {
696 unsigned char red = image.GetRed(i, j);
697 unsigned char green = image.GetGreen(i, j);
698 unsigned char blue = image.GetBlue(i, j);
e640f823 699
8e9ff815 700 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
62e1ba75
JS
701
702 if (hasMask)
703 {
704 // scan the bitmap for the transparent colour and set the corresponding
705 // pixels in the mask to BLACK and the rest to WHITE
706 if (maskR == red && maskG == green && maskB == blue)
707 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
708 else
709 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
8e9ff815
VZ
710 }
711 }
e640f823
JS
712 }
713
714 ::SelectObject(hMemDC, hOldBitmap);
715 ::DeleteDC(hMemDC);
62e1ba75
JS
716 if (hasMask)
717 {
718 ::SelectObject(hMaskDC, hOldMaskBitmap);
8e9ff815 719 ::DeleteDC(hMaskDC);
62e1ba75 720
8bbbae21 721 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
62e1ba75 722 }
8e9ff815 723
e640f823
JS
724 SetWidth(image.GetWidth());
725 SetHeight(image.GetHeight());
726 SetDepth(screenDepth);
727 SetHBITMAP( (WXHBITMAP) hBitmap );
8e9ff815 728
e640f823
JS
729#if wxUSE_PALETTE
730 // Copy the palette from the source image
731 SetPalette(image.GetPalette());
732#endif // wxUSE_PALETTE
733
fcf90ee1 734 return true;
fec19ea9
VS
735}
736
737wxImage wxBitmap::ConvertToImage() const
738{
e640f823
JS
739 // Initial attempt at a simple-minded implementation.
740 // The bitmap will always be created at the screen depth,
741 // so the 'depth' argument is ignored.
742 // TODO: transparency (create a mask image)
743
744 if (!Ok())
745 {
746 wxFAIL_MSG( wxT("bitmap is invalid") );
8e9ff815 747 return wxNullImage;
e640f823
JS
748 }
749
750 wxImage image;
751
752 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
753
754 // create an wxImage object
755 int width = GetWidth();
756 int height = GetHeight();
757 image.Create( width, height );
758 unsigned char *data = image.GetData();
759 if( !data )
760 {
761 wxFAIL_MSG( wxT("could not allocate data for image") );
762 return wxNullImage;
763 }
8e9ff815 764
e640f823
JS
765 HDC hScreenDC = ::GetDC(NULL);
766
767 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
768 ::ReleaseDC(NULL, hScreenDC);
769
770 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
8e9ff815 771
e640f823
JS
772 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
773
774 int i, j;
775 for (i = 0; i < GetWidth(); i++)
776 {
8e9ff815
VZ
777 for (j = 0; j < GetHeight(); j++)
778 {
779 COLORREF color = ::GetPixel(hMemDC, i, j);
780 unsigned char red = GetRValue(color);
781 unsigned char green = GetGValue(color);
782 unsigned char blue = GetBValue(color);
783
784 image.SetRGB(i, j, red, green, blue);
785 }
e640f823
JS
786 }
787
788 ::SelectObject(hMemDC, hOldBitmap);
789 ::DeleteDC(hMemDC);
8e9ff815 790
e640f823
JS
791#if wxUSE_PALETTE
792 // Copy the palette from the source image
793 if (GetPalette())
794 image.SetPalette(* GetPalette());
795#endif // wxUSE_PALETTE
796
797 return image;
acf8e3d2
VZ
798}
799
800#endif // __WXMICROWIN__
801
802// ----------------------------------------------------------------------------
803// wxImage to/from conversions
804// ----------------------------------------------------------------------------
805
2cf45d69
VZ
806bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
807{
808 return CreateFromImage(image, depth, 0);
809}
810
811bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
812{
888dde65 813 wxCHECK_MSG( dc.IsOk(), false,
2cf45d69
VZ
814 _T("invalid HDC in wxBitmap::CreateFromImage()") );
815
888dde65
RR
816 const wxMSWDCImpl *impl = wxDynamicCast( dc.GetImpl(), wxMSWDCImpl );
817
818 if (impl)
819 return CreateFromImage(image, -1, impl->GetHDC());
820 else
821 return false;
2cf45d69
VZ
822}
823
50a9dd77
VZ
824#if wxUSE_WXDIB
825
be69f971 826bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
acf8e3d2 827{
fcf90ee1 828 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
acf8e3d2
VZ
829
830 UnRef();
831
832 // first convert the image to DIB
833 const int h = image.GetHeight();
834 const int w = image.GetWidth();
835
836 wxDIB dib(image);
837 if ( !dib.IsOk() )
fcf90ee1 838 return false;
511fb92a
VZ
839
840 if ( depth == -1 )
841 depth = dib.GetDepth(); // Get depth from image if none specified
acf8e3d2
VZ
842
843 // store the bitmap parameters
844 wxBitmapRefData *refData = new wxBitmapRefData;
845 refData->m_width = w;
846 refData->m_height = h;
acf8e3d2
VZ
847 refData->m_hasAlpha = image.HasAlpha();
848
849 m_refData = refData;
850
851
852 // next either store DIB as is or create a DDB from it
be69f971 853 HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
acf8e3d2 854
2cf45d69 855 // are we going to use DIB?
4b8ab2f9
VZ
856 //
857 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
858 if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
2cf45d69
VZ
859 {
860 // don't delete the DIB section in dib object dtor
861 hbitmap = dib.Detach();
862
fcf90ee1 863 refData->m_isDIB = true;
7532f691 864 refData->m_depth = depth;
2cf45d69 865 }
be69f971 866#ifndef ALWAYS_USE_DIB
2cf45d69 867 else // we need to convert DIB to DDB
acf8e3d2 868 {
b0ea5d96 869 hbitmap = dib.CreateDDB((HDC)hdc);
2cf45d69 870
7532f691 871 refData->m_depth = depth;
acf8e3d2 872 }
be69f971 873#endif // !ALWAYS_USE_DIB
acf8e3d2
VZ
874
875 // validate this object
876 SetHBITMAP((WXHBITMAP)hbitmap);
877
acf8e3d2
VZ
878 // finally also set the mask if we have one
879 if ( image.HasMask() )
880 {
eabd3333
VZ
881 const size_t len = 2*((w+15)/16);
882 BYTE *src = image.GetData();
883 BYTE *data = new BYTE[h*len];
884 memset(data, 0, h*len);
885 BYTE r = image.GetMaskRed(),
886 g = image.GetMaskGreen(),
887 b = image.GetMaskBlue();
888 BYTE *dst = data;
889 for ( int y = 0; y < h; y++, dst += len )
890 {
891 BYTE *dstLine = dst;
892 BYTE mask = 0x80;
893 for ( int x = 0; x < w; x++, src += 3 )
894 {
895 if (src[0] != r || src[1] != g || src[2] != b)
896 *dstLine |= mask;
897
898 if ( (mask >>= 1) == 0 )
899 {
900 dstLine++;
901 mask = 0x80;
902 }
903 }
904 }
905
906 hbitmap = ::CreateBitmap(w, h, 1, 1, data);
907 if ( !hbitmap )
908 {
909 wxLogLastError(_T("CreateBitmap(mask)"));
910 }
911 else
912 {
913 SetMask(new wxMask((WXHBITMAP)hbitmap));
914 }
915
d0ee33f5 916 delete[] data;
acf8e3d2
VZ
917 }
918
fcf90ee1 919 return true;
acf8e3d2
VZ
920}
921
922wxImage wxBitmap::ConvertToImage() const
923{
6faac21b 924 // convert DDB to DIB
be69f971 925 wxDIB dib(*this);
c8688869 926
003e6c07
DS
927 if ( !dib.IsOk() )
928 {
929 return wxNullImage;
930 }
931
6faac21b 932 // and then DIB to our wxImage
003e6c07
DS
933 wxImage image = dib.ConvertToImage();
934 if ( !image.Ok() )
935 {
936 return wxNullImage;
937 }
938
6faac21b
VZ
939 // now do the same for the mask, if we have any
940 HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
941 if ( hbmpMask )
fec19ea9 942 {
6faac21b
VZ
943 wxDIB dibMask(hbmpMask);
944 if ( dibMask.IsOk() )
003e6c07 945 {
6faac21b
VZ
946 // TODO: use wxRawBitmap to iterate over DIB
947
948 // we hard code the mask colour for now but we could also make an
949 // effort (and waste time) to choose a colour not present in the
950 // image already to avoid having to fudge the pixels below --
951 // whether it's worth to do it is unclear however
952 static const int MASK_RED = 1;
953 static const int MASK_GREEN = 2;
954 static const int MASK_BLUE = 3;
955 static const int MASK_BLUE_REPLACEMENT = 2;
956
957 const int h = dibMask.GetHeight();
958 const int w = dibMask.GetWidth();
959 const int bpp = dibMask.GetDepth();
960 const int maskBytesPerPixel = bpp >> 3;
961 const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
962 unsigned char *data = image.GetData();
963
964 // remember that DIBs are stored in bottom to top order
965 unsigned char *
966 maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
967
968 for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
003e6c07 969 {
6faac21b
VZ
970 // traverse one mask DIB line
971 unsigned char *mask = maskLineStart;
972 for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
003e6c07 973 {
6faac21b
VZ
974 // should this pixel be transparent?
975 if ( *mask )
003e6c07 976 {
6faac21b
VZ
977 // no, check that it isn't transparent by accident
978 if ( (data[0] == MASK_RED) &&
979 (data[1] == MASK_GREEN) &&
980 (data[2] == MASK_BLUE) )
981 {
982 // we have to fudge the colour a bit to prevent
983 // this pixel from appearing transparent
984 data[2] = MASK_BLUE_REPLACEMENT;
985 }
986
987 data += 3;
988 }
989 else // yes, transparent pixel
990 {
991 *data++ = MASK_RED;
992 *data++ = MASK_GREEN;
993 *data++ = MASK_BLUE;
003e6c07 994 }
003e6c07 995 }
003e6c07 996 }
003e6c07 997
6faac21b
VZ
998 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
999 }
fec19ea9 1000 }
fec19ea9
VS
1001
1002 return image;
1003}
1004
50a9dd77
VZ
1005#else // !wxUSE_WXDIB
1006
1007bool
1008wxBitmap::CreateFromImage(const wxImage& WXUNUSED(image),
1009 int WXUNUSED(depth),
1010 WXHDC WXUNUSED(hdc))
1011{
1012 return false;
1013}
1014
1015wxImage wxBitmap::ConvertToImage() const
1016{
1017 return wxImage();
1018}
1019
1020#endif // wxUSE_WXDIB/!wxUSE_WXDIB
be69f971 1021
6d51f220
VZ
1022#endif // wxUSE_IMAGE
1023
acf8e3d2
VZ
1024// ----------------------------------------------------------------------------
1025// loading and saving bitmaps
1026// ----------------------------------------------------------------------------
1027
debe6624 1028bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 1029{
6d167489 1030 UnRef();
2bda0e17 1031
6d167489 1032 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1033
6d167489
VZ
1034 if ( handler )
1035 {
1036 m_refData = new wxBitmapRefData;
2bda0e17 1037
6d167489
VZ
1038 return handler->LoadFile(this, filename, type, -1, -1);
1039 }
64c288fa 1040#if wxUSE_IMAGE && wxUSE_WXDIB
be69f971 1041 else // no bitmap handler found
b75dd496 1042 {
6d167489 1043 wxImage image;
6d51f220
VZ
1044 if ( image.LoadFile( filename, type ) && image.Ok() )
1045 {
368d59f0 1046 *this = wxBitmap(image);
6d167489 1047
fcf90ee1 1048 return true;
6d51f220 1049 }
b75dd496 1050 }
6d51f220
VZ
1051#endif // wxUSE_IMAGE
1052
fcf90ee1 1053 return false;
2bda0e17
KB
1054}
1055
452418c4 1056bool wxBitmap::Create(const void* data, long type, int width, int height, int depth)
2bda0e17 1057{
6d167489 1058 UnRef();
2bda0e17 1059
6d167489 1060 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1061
6d167489
VZ
1062 if ( !handler )
1063 {
9b601c24 1064 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
2bda0e17 1065
fcf90ee1 1066 return false;
6d167489 1067 }
1d792928 1068
6d167489 1069 m_refData = new wxBitmapRefData;
1d792928 1070
6d167489 1071 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
1072}
1073
d275c7eb
VZ
1074bool wxBitmap::SaveFile(const wxString& filename,
1075 int type,
1076 const wxPalette *palette)
2bda0e17 1077{
6d167489 1078 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1079
6d167489
VZ
1080 if ( handler )
1081 {
1082 return handler->SaveFile(this, filename, type, palette);
1083 }
64c288fa 1084#if wxUSE_IMAGE && wxUSE_WXDIB
be69f971 1085 else // no bitmap handler found
6d167489
VZ
1086 {
1087 // FIXME what about palette? shouldn't we use it?
368d59f0 1088 wxImage image = ConvertToImage();
6d51f220
VZ
1089 if ( image.Ok() )
1090 {
1091 return image.SaveFile(filename, type);
1092 }
6d167489 1093 }
6d51f220
VZ
1094#endif // wxUSE_IMAGE
1095
fcf90ee1 1096 return false;
2bda0e17
KB
1097}
1098
4b7f2165
VZ
1099// ----------------------------------------------------------------------------
1100// sub bitmap extraction
1101// ----------------------------------------------------------------------------
18f2ae49
KO
1102wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect ) const
1103{
1104 MemoryHDC dcSrc;
1105 SelectInHDC selectSrc(dcSrc, GetHbitmap());
1106 return GetSubBitmapOfHDC( rect, (WXHDC)dcSrc );
1107}
4b7f2165 1108
18f2ae49 1109wxBitmap wxBitmap::GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const
4b7f2165
VZ
1110{
1111 wxCHECK_MSG( Ok() &&
1112 (rect.x >= 0) && (rect.y >= 0) &&
1113 (rect.x+rect.width <= GetWidth()) &&
1114 (rect.y+rect.height <= GetHeight()),
1115 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1116
e5d4ef74 1117 wxBitmap ret( rect.width, rect.height, GetDepth() );
4b7f2165
VZ
1118 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1119
8e9ff815 1120#ifndef __WXMICROWIN__
e5d4ef74
VZ
1121 // handle alpha channel, if any
1122 if (HasAlpha())
92218ce6 1123 ret.UseAlpha();
acf8e3d2 1124
4b7f2165 1125 // copy bitmap data
acf8e3d2
VZ
1126 MemoryHDC dcSrc,
1127 dcDst;
8e9ff815
VZ
1128
1129 {
18f2ae49
KO
1130 SelectInHDC selectDst(dcDst, GetHbitmapOf(ret));
1131
1132 if ( !selectDst )
acf8e3d2 1133 {
18f2ae49 1134 wxLogLastError(_T("SelectObject(destBitmap)"));
acf8e3d2
VZ
1135 }
1136
8e9ff815 1137 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
18f2ae49 1138 (HDC)hdc, rect.x, rect.y, SRCCOPY) )
8e9ff815
VZ
1139 {
1140 wxLogLastError(_T("BitBlt"));
1141 }
1142 }
4b7f2165
VZ
1143
1144 // copy mask if there is one
8e9ff815 1145 if ( GetMask() )
4b7f2165
VZ
1146 {
1147 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1148
8e9ff815
VZ
1149 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1150 selectDst(dcDst, hbmpMask);
1151
1152 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1153 dcSrc, rect.x, rect.y, SRCCOPY) )
1154 {
1155 wxLogLastError(_T("BitBlt"));
1156 }
4b7f2165
VZ
1157
1158 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1159 ret.SetMask(mask);
1160 }
8e9ff815 1161#endif // !__WXMICROWIN__
4b7f2165
VZ
1162
1163 return ret;
1164}
1165
6d167489
VZ
1166// ----------------------------------------------------------------------------
1167// wxBitmap accessors
1168// ----------------------------------------------------------------------------
2bda0e17 1169
adf9e099 1170#if wxUSE_PALETTE
8bbbae21 1171wxPalette* wxBitmap::GetPalette() const
2bda0e17 1172{
8bbbae21
VZ
1173 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1174 : (wxPalette *) NULL;
1175}
adf9e099 1176#endif
8bbbae21
VZ
1177
1178wxMask *wxBitmap::GetMask() const
1179{
1180 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1181}
2bda0e17 1182
ec023a6e
VZ
1183wxBitmap wxBitmap::GetMaskBitmap() const
1184{
1185 wxBitmap bmp;
1186 wxMask *mask = GetMask();
1187 if ( mask )
1188 bmp.SetHBITMAP(mask->GetMaskBitmap());
1189 return bmp;
1190}
1191
3ca22d5e
MB
1192#ifdef __WXDEBUG__
1193
8bbbae21
VZ
1194wxDC *wxBitmap::GetSelectedInto() const
1195{
1196 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
2bda0e17
KB
1197}
1198
3ca22d5e
MB
1199#endif
1200
10b41b53
VZ
1201void wxBitmap::UseAlpha()
1202{
1203 if ( GetBitmapData() )
1204 GetBitmapData()->m_hasAlpha = true;
1205}
1206
acf8e3d2
VZ
1207bool wxBitmap::HasAlpha() const
1208{
1209 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1210}
1211
8bbbae21
VZ
1212// ----------------------------------------------------------------------------
1213// wxBitmap setters
1214// ----------------------------------------------------------------------------
1215
3ca22d5e
MB
1216#ifdef __WXDEBUG__
1217
8bbbae21
VZ
1218void wxBitmap::SetSelectedInto(wxDC *dc)
1219{
1220 if ( GetBitmapData() )
1221 GetBitmapData()->m_selectedInto = dc;
2bda0e17
KB
1222}
1223
3ca22d5e
MB
1224#endif
1225
d275c7eb
VZ
1226#if wxUSE_PALETTE
1227
2bda0e17
KB
1228void wxBitmap::SetPalette(const wxPalette& palette)
1229{
7ff64980 1230 AllocExclusive();
2bda0e17 1231
6d167489 1232 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
1233}
1234
d275c7eb
VZ
1235#endif // wxUSE_PALETTE
1236
2bda0e17
KB
1237void wxBitmap::SetMask(wxMask *mask)
1238{
7ff64980 1239 AllocExclusive();
2bda0e17 1240
8bbbae21
VZ
1241 GetBitmapData()->SetMask(mask);
1242}
1243
2cf45d69
VZ
1244// ----------------------------------------------------------------------------
1245// raw bitmap access support
1246// ----------------------------------------------------------------------------
1247
1e3c12d7 1248#ifdef wxHAVE_RAW_BITMAP
b9bcaf11 1249void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
2cf45d69 1250{
4676948b 1251#if wxUSE_WXDIB
2cf45d69
VZ
1252 if ( !Ok() )
1253 {
1254 // no bitmap, no data (raw or otherwise)
b9bcaf11 1255 return NULL;
2cf45d69
VZ
1256 }
1257
fa275e86
VZ
1258 // if we're already a DIB we can access our data directly, but if not we
1259 // need to convert this DDB to a DIB section and use it for raw access and
1260 // then convert it back
1261 HBITMAP hDIB;
1262 if ( !GetBitmapData()->m_isDIB )
1263 {
d0ee33f5 1264 wxCHECK_MSG( !GetBitmapData()->m_dib, NULL,
fa275e86
VZ
1265 _T("GetRawData() may be called only once") );
1266
1267 wxDIB *dib = new wxDIB(*this);
1268 if ( !dib->IsOk() )
1269 {
1270 delete dib;
1271
b9bcaf11 1272 return NULL;
fa275e86
VZ
1273 }
1274
1275 // we'll free it in UngetRawData()
1276 GetBitmapData()->m_dib = dib;
1277
1278 hDIB = dib->GetHandle();
1279 }
1280 else // we're a DIB
1281 {
1282 hDIB = GetHbitmap();
1283 }
1284
2cf45d69 1285 DIBSECTION ds;
fa275e86 1286 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
2cf45d69 1287 {
fa275e86
VZ
1288 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1289
b9bcaf11
VZ
1290 return NULL;
1291 }
1292
1293 // check that the bitmap is in correct format
1294 if ( ds.dsBm.bmBitsPixel != bpp )
1295 {
1296 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1297
1298 return NULL;
2cf45d69
VZ
1299 }
1300
b9bcaf11 1301 // ok, store the relevant info in wxPixelDataBase
2cf45d69
VZ
1302 const LONG h = ds.dsBm.bmHeight;
1303
b9bcaf11
VZ
1304 data.m_width = ds.dsBm.bmWidth;
1305 data.m_height = h;
2cf45d69
VZ
1306
1307 // remember that DIBs are stored in top to bottom order!
e5d4ef74
VZ
1308 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1309 // multiple of 2, as required by the documentation. So we use the official
1310 // formula, which we already use elsewhere.)
1311 const LONG bytesPerRow =
1312 wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel);
b9bcaf11
VZ
1313 data.m_stride = -bytesPerRow;
1314
1315 char *bits = (char *)ds.dsBm.bmBits;
2cf45d69
VZ
1316 if ( h > 1 )
1317 {
b9bcaf11 1318 bits += (h - 1)*bytesPerRow;
2cf45d69
VZ
1319 }
1320
b9bcaf11 1321 return bits;
4676948b
JS
1322#else
1323 return NULL;
1324#endif
2cf45d69
VZ
1325}
1326
b9bcaf11 1327void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
13c13ecb 1328{
4676948b 1329#if wxUSE_WXDIB
fa275e86
VZ
1330 if ( !Ok() )
1331 return;
1332
e5d4ef74 1333 if ( !&dataBase )
7af64aac
VZ
1334 {
1335 // invalid data, don't crash -- but don't assert neither as we're
b9bcaf11 1336 // called automatically from wxPixelDataBase dtor and so there is no
7af64aac
VZ
1337 // way to prevent this from happening
1338 return;
1339 }
1340
289307fa
VZ
1341 // if we're a DDB we need to convert DIB back to DDB now to make the
1342 // changes made via raw bitmap access effective
1343 if ( !GetBitmapData()->m_isDIB )
1344 {
1345 wxDIB *dib = GetBitmapData()->m_dib;
1346 GetBitmapData()->m_dib = NULL;
fa275e86 1347
289307fa 1348 // TODO: convert
fa275e86 1349
289307fa 1350 delete dib;
fa275e86 1351 }
289307fa 1352#endif // wxUSE_WXDIB
13c13ecb 1353}
1e3c12d7 1354#endif // #ifdef wxHAVE_RAW_BITMAP
13c13ecb 1355
6d167489
VZ
1356// ----------------------------------------------------------------------------
1357// wxMask
1358// ----------------------------------------------------------------------------
2bda0e17 1359
10fcf31a 1360wxMask::wxMask()
2bda0e17
KB
1361{
1362 m_maskBitmap = 0;
1363}
1364
1b7c01c9
VZ
1365// Copy constructor
1366wxMask::wxMask(const wxMask &mask)
0a704d39 1367 : wxObject()
1b7c01c9
VZ
1368{
1369 BITMAP bmp;
1370
1371 HDC srcDC = CreateCompatibleDC(0);
1372 HDC destDC = CreateCompatibleDC(0);
1373
1374 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1375 // so we'll use GetObject() API here:
1376 if (::GetObject((HGDIOBJ)mask.m_maskBitmap, sizeof(bmp), &bmp) == 0)
1377 {
1378 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1379 return;
1380 }
1381
1382 // create our HBITMAP
1383 int w = bmp.bmWidth, h = bmp.bmHeight;
1384 m_maskBitmap = (WXHBITMAP)CreateCompatibleBitmap(srcDC, w, h);
1385
1386 // copy the mask's HBITMAP into our HBITMAP
1387 SelectObject(srcDC, (HBITMAP) mask.m_maskBitmap);
1388 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1389
1390 BitBlt(destDC, 0, 0, w, h, srcDC, 0, 0, SRCCOPY);
f4322df6 1391
1b7c01c9
VZ
1392 SelectObject(srcDC, 0);
1393 DeleteDC(srcDC);
1394 SelectObject(destDC, 0);
1395 DeleteDC(destDC);
1396}
1397
2bda0e17
KB
1398// Construct a mask from a bitmap and a colour indicating
1399// the transparent area
1400wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1401{
1402 m_maskBitmap = 0;
6d167489 1403 Create(bitmap, colour);
2bda0e17
KB
1404}
1405
1406// Construct a mask from a bitmap and a palette index indicating
1407// the transparent area
debe6624 1408wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1409{
1410 m_maskBitmap = 0;
6d167489 1411 Create(bitmap, paletteIndex);
2bda0e17
KB
1412}
1413
1414// Construct a mask from a mono bitmap (copies the bitmap).
1415wxMask::wxMask(const wxBitmap& bitmap)
1416{
1417 m_maskBitmap = 0;
6d167489 1418 Create(bitmap);
2bda0e17
KB
1419}
1420
10fcf31a 1421wxMask::~wxMask()
2bda0e17
KB
1422{
1423 if ( m_maskBitmap )
1424 ::DeleteObject((HBITMAP) m_maskBitmap);
1425}
1426
1427// Create a mask from a mono bitmap (copies the bitmap).
1428bool wxMask::Create(const wxBitmap& bitmap)
1429{
04ef50df 1430#ifndef __WXMICROWIN__
fcf90ee1 1431 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, false,
a58a12e9
VZ
1432 _T("can't create mask from invalid or not monochrome bitmap") );
1433
2bda0e17 1434 if ( m_maskBitmap )
6d167489
VZ
1435 {
1436 ::DeleteObject((HBITMAP) m_maskBitmap);
1437 m_maskBitmap = 0;
1438 }
a58a12e9 1439
6d167489
VZ
1440 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1441 bitmap.GetWidth(),
1442 bitmap.GetHeight(),
1443 1, 1, 0
1444 );
1445 HDC srcDC = CreateCompatibleDC(0);
1446 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1447 HDC destDC = CreateCompatibleDC(0);
1448 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1449 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1450 SelectObject(srcDC, 0);
1451 DeleteDC(srcDC);
1452 SelectObject(destDC, 0);
1453 DeleteDC(destDC);
fcf90ee1 1454 return true;
04ef50df 1455#else
fcf90ee1
WS
1456 wxUnusedVar(bitmap);
1457 return false;
04ef50df 1458#endif
2bda0e17
KB
1459}
1460
1461// Create a mask from a bitmap and a palette index indicating
1462// the transparent area
debe6624 1463bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1464{
1465 if ( m_maskBitmap )
1d792928 1466 {
6d167489
VZ
1467 ::DeleteObject((HBITMAP) m_maskBitmap);
1468 m_maskBitmap = 0;
1d792928 1469 }
d275c7eb
VZ
1470
1471#if wxUSE_PALETTE
6d167489
VZ
1472 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1473 {
1474 unsigned char red, green, blue;
1475 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1476 {
1477 wxColour transparentColour(red, green, blue);
1478 return Create(bitmap, transparentColour);
1479 }
1480 }
d275c7eb
VZ
1481#endif // wxUSE_PALETTE
1482
fcf90ee1 1483 return false;
2bda0e17
KB
1484}
1485
1486// Create a mask from a bitmap and a colour indicating
1487// the transparent area
1488bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1489{
04ef50df 1490#ifndef __WXMICROWIN__
fcf90ee1 1491 wxCHECK_MSG( bitmap.Ok(), false, _T("invalid bitmap in wxMask::Create") );
4b7f2165 1492
2bda0e17 1493 if ( m_maskBitmap )
1d792928 1494 {
6d167489
VZ
1495 ::DeleteObject((HBITMAP) m_maskBitmap);
1496 m_maskBitmap = 0;
1497 }
4b7f2165
VZ
1498
1499 int width = bitmap.GetWidth(),
1500 height = bitmap.GetHeight();
1501
1502 // scan the bitmap for the transparent colour and set the corresponding
1503 // pixels in the mask to BLACK and the rest to WHITE
f423f4db 1504 COLORREF maskColour = wxColourToPalRGB(colour);
4b7f2165
VZ
1505 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1506
1507 HDC srcDC = ::CreateCompatibleDC(NULL);
1508 HDC destDC = ::CreateCompatibleDC(NULL);
1509 if ( !srcDC || !destDC )
1510 {
f6bcfd97 1511 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1512 }
1513
fcf90ee1 1514 bool ok = true;
0bafad0c 1515
1e6feb95
VZ
1516 // SelectObject() will fail
1517 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1518 _T("bitmap can't be selected in another DC") );
1519
0bafad0c
VZ
1520 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1521 if ( !hbmpSrcOld )
6d167489 1522 {
f6bcfd97 1523 wxLogLastError(wxT("SelectObject"));
0bafad0c 1524
fcf90ee1 1525 ok = false;
4b7f2165 1526 }
0bafad0c
VZ
1527
1528 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1529 if ( !hbmpDstOld )
4b7f2165 1530 {
f6bcfd97 1531 wxLogLastError(wxT("SelectObject"));
0bafad0c 1532
fcf90ee1 1533 ok = false;
1d792928 1534 }
2bda0e17 1535
59ff46cb 1536 if ( ok )
2bda0e17 1537 {
59ff46cb
VZ
1538 // this will create a monochrome bitmap with 0 points for the pixels
1539 // which have the same value as the background colour and 1 for the
1540 // others
1541 ::SetBkColor(srcDC, maskColour);
1542 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
2bda0e17 1543 }
4b7f2165 1544
0bafad0c 1545 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1546 ::DeleteDC(srcDC);
0bafad0c 1547 ::SelectObject(destDC, hbmpDstOld);
6d167489 1548 ::DeleteDC(destDC);
4b7f2165 1549
0bafad0c 1550 return ok;
59ff46cb 1551#else // __WXMICROWIN__
fcf90ee1
WS
1552 wxUnusedVar(bitmap);
1553 wxUnusedVar(colour);
1554 return false;
59ff46cb 1555#endif // __WXMICROWIN__/!__WXMICROWIN__
6d167489
VZ
1556}
1557
1558// ----------------------------------------------------------------------------
1559// wxBitmapHandler
1560// ----------------------------------------------------------------------------
1d792928 1561
6d167489 1562bool wxBitmapHandler::Create(wxGDIImage *image,
452418c4 1563 const void* data,
6d167489
VZ
1564 long flags,
1565 int width, int height, int depth)
1566{
1567 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1568
452418c4 1569 return bitmap && Create(bitmap, data, flags, width, height, depth);
2bda0e17
KB
1570}
1571
6d167489
VZ
1572bool wxBitmapHandler::Load(wxGDIImage *image,
1573 const wxString& name,
1574 long flags,
1575 int width, int height)
2bda0e17 1576{
6d167489 1577 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1578
452418c4 1579 return bitmap && LoadFile(bitmap, name, flags, width, height);
6d167489 1580}
2bda0e17 1581
6d167489
VZ
1582bool wxBitmapHandler::Save(wxGDIImage *image,
1583 const wxString& name,
1584 int type)
2bda0e17 1585{
6d167489
VZ
1586 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1587
452418c4 1588 return bitmap && SaveFile(bitmap, name, type);
7b46ecac
JS
1589}
1590
8adefcac
PC
1591bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1592 const void* WXUNUSED(data),
1593 long WXUNUSED(type),
1594 int WXUNUSED(width),
1595 int WXUNUSED(height),
1596 int WXUNUSED(depth))
1597{
1598 return false;
1599}
1600
1601bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1602 const wxString& WXUNUSED(name),
1603 long WXUNUSED(type),
1604 int WXUNUSED(desiredWidth),
1605 int WXUNUSED(desiredHeight))
1606{
1607 return false;
1608}
1609
1610bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1611 const wxString& WXUNUSED(name),
1612 int WXUNUSED(type),
1613 const wxPalette *WXUNUSED(palette))
1614{
1615 return false;
1616}
1617
4b7f2165 1618// ----------------------------------------------------------------------------
211b54b1 1619// global helper functions implemented here
4b7f2165
VZ
1620// ----------------------------------------------------------------------------
1621
211b54b1
VZ
1622// helper of wxBitmapToHICON/HCURSOR
1623static
1624HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1625 bool iconWanted,
1626 int hotSpotX,
1627 int hotSpotY)
1628{
1629 if ( !bmp.Ok() )
1630 {
1631 // we can't create an icon/cursor form nothing
1632 return 0;
1633 }
1634
0806fc20
RD
1635 if ( bmp.HasAlpha() )
1636 {
07430847
JS
1637 // Create an empty mask bitmap.
1638 // it doesn't seem to work if we mess with the mask at all.
1639 HBITMAP hMonoBitmap = CreateBitmap(bmp.GetWidth(),bmp.GetHeight(),1,1,NULL);
1640
1641 ICONINFO iconInfo;
1642 wxZeroMemory(iconInfo);
1643 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1644 if ( !iconWanted )
1645 {
1646 iconInfo.xHotspot = hotSpotX;
1647 iconInfo.yHotspot = hotSpotY;
1648 }
1649
1650 iconInfo.hbmMask = hMonoBitmap;
1651 iconInfo.hbmColor = GetHbitmapOf(bmp);
1652
1653 HICON hicon = ::CreateIconIndirect(&iconInfo);
1654
1655 ::DeleteObject(hMonoBitmap);
1656
1657 return hicon;
0806fc20 1658 }
d0ee33f5 1659
07430847
JS
1660 wxMask* mask = bmp.GetMask();
1661
211b54b1
VZ
1662 if ( !mask )
1663 {
1664 // we must have a mask for an icon, so even if it's probably incorrect,
1665 // do create it (grey is the "standard" transparent colour)
1666 mask = new wxMask(bmp, *wxLIGHT_GREY);
1667 }
1668
1669 ICONINFO iconInfo;
4104d2b3 1670 wxZeroMemory(iconInfo);
211b54b1
VZ
1671 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1672 if ( !iconWanted )
1673 {
1674 iconInfo.xHotspot = hotSpotX;
1675 iconInfo.yHotspot = hotSpotY;
1676 }
1677
1678 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1679 iconInfo.hbmColor = GetHbitmapOf(bmp);
1680
1681 // black out the transparent area to preserve background colour, because
1682 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1683 // the mask to the dest rect.
1684 {
1685 MemoryHDC dcSrc, dcDst;
1686 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1687 selectBitmap(dcDst, iconInfo.hbmColor);
1688
1689 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1690 dcSrc, 0, 0, SRCAND) )
1691 {
1692 wxLogLastError(_T("BitBlt"));
1693 }
1694 }
1695
1696 HICON hicon = ::CreateIconIndirect(&iconInfo);
1697
0806fc20 1698 if ( !bmp.GetMask() && !bmp.HasAlpha() )
211b54b1
VZ
1699 {
1700 // we created the mask, now delete it
1701 delete mask;
1702 }
1703
1704 // delete the inverted mask bitmap we created as well
1705 ::DeleteObject(iconInfo.hbmMask);
1706
1707 return hicon;
1708}
1709
1710HICON wxBitmapToHICON(const wxBitmap& bmp)
1711{
fcf90ee1 1712 return wxBitmapToIconOrCursor(bmp, true, 0, 0);
211b54b1
VZ
1713}
1714
1715HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1716{
fcf90ee1 1717 return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY);
211b54b1
VZ
1718}
1719
1720HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
4b7f2165 1721{
04ef50df 1722#ifndef __WXMICROWIN__
4b7f2165
VZ
1723 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1724
1725 // get width/height from the bitmap if not given
1726 if ( !w || !h )
1727 {
1728 BITMAP bm;
1729 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1730 w = bm.bmWidth;
1731 h = bm.bmHeight;
1732 }
1733
1734 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1735 HDC hdcDst = ::CreateCompatibleDC(NULL);
1736 if ( !hdcSrc || !hdcDst )
1737 {
f6bcfd97 1738 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1739 }
1740
1741 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1742 if ( !hbmpInvMask )
1743 {
f6bcfd97 1744 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1745 }
1746
e7222d65
JS
1747 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1748 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
4b7f2165
VZ
1749 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1750 hdcSrc, 0, 0,
1751 NOTSRCCOPY) )
1752 {
f6bcfd97 1753 wxLogLastError(wxT("BitBlt"));
4b7f2165 1754 }
7b46ecac 1755
e7222d65
JS
1756 // Deselect objects
1757 SelectObject(hdcSrc,srcTmp);
1758 SelectObject(hdcDst,dstTmp);
fcf90ee1 1759
4b7f2165
VZ
1760 ::DeleteDC(hdcSrc);
1761 ::DeleteDC(hdcDst);
1762
1763 return hbmpInvMask;
04ef50df
JS
1764#else
1765 return 0;
1766#endif
4b7f2165 1767}