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