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