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