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