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