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