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