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