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