]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
fix the size of the created subbitmap
[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 // this function should be called from all wxBitmap ctors
245 void wxBitmap::Init()
246 {
247 // m_refData = NULL; done in the base class ctor
248 }
249
250 wxGDIImageRefData *wxBitmap::CreateData() const
251 {
252 return new wxBitmapRefData;
253 }
254
255 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const
256 {
257 const wxBitmapRefData *
258 data = wx_static_cast(const wxBitmapRefData *, dataOrig);
259 if ( !data )
260 return NULL;
261
262 wxBitmap *self = wx_const_cast(wxBitmap *, this);
263
264 #if wxUSE_WXDIB
265 // copy the other bitmap
266 if ( data->m_hBitmap )
267 {
268 wxDIB dib((HBITMAP)(data->m_hBitmap));
269 self->CopyFromDIB(dib);
270 }
271 else
272 #endif // wxUSE_WXDIB
273 {
274 // don't copy the bitmap data, but do copy the size, depth, ...
275 self->m_refData = new wxBitmapRefData(*data);
276 }
277
278 return m_refData;
279 }
280
281 #ifdef __WIN32__
282
283 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
284 {
285 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
286 // it may be either HICON or HCURSOR
287 HICON hicon = (HICON)icon.GetHandle();
288
289 ICONINFO iconInfo;
290 if ( !::GetIconInfo(hicon, &iconInfo) )
291 {
292 wxLogLastError(wxT("GetIconInfo"));
293
294 return false;
295 }
296
297 wxBitmapRefData *refData = new wxBitmapRefData;
298 m_refData = refData;
299
300 int w = icon.GetWidth(),
301 h = icon.GetHeight();
302
303 refData->m_width = w;
304 refData->m_height = h;
305 refData->m_depth = wxDisplayDepth();
306
307 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
308
309 #if wxUSE_WXDIB
310 // If the icon is 32 bits per pixel then it may have alpha channel data,
311 // although there are some icons that are 32 bpp but have no alpha... So
312 // convert to a DIB and manually check the 4th byte for each pixel.
313 BITMAP bm;
314 if ( ::GetObject(iconInfo.hbmColor, sizeof(BITMAP), (LPVOID)&bm)
315 && bm.bmBitsPixel == 32)
316 {
317 wxDIB dib(iconInfo.hbmColor);
318 if (dib.IsOk())
319 {
320 unsigned char* pixels = dib.GetData();
321 for (int idx=0; idx<w*h*4; idx+=4)
322 {
323 if (pixels[idx+3] != 0)
324 {
325 // If there is an alpha byte that is non-zero then set the
326 // alpha flag and bail out of the loop.
327 refData->m_hasAlpha = true;
328 break;
329 }
330 }
331 }
332 }
333 #endif
334 if ( !refData->m_hasAlpha )
335 {
336 // the mask returned by GetIconInfo() is inverted compared to the usual
337 // wxWin convention
338 refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
339 }
340
341 // delete the old one now as we don't need it any more
342 ::DeleteObject(iconInfo.hbmMask);
343
344 return true;
345 #else
346 wxUnusedVar(icon);
347 return false;
348 #endif
349 }
350
351 #endif // Win32
352
353 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
354 {
355 UnRef();
356
357 if ( !cursor.Ok() )
358 return false;
359
360 return CopyFromIconOrCursor(cursor);
361 }
362
363 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
364 {
365 UnRef();
366
367 if ( !icon.Ok() )
368 return false;
369
370 return CopyFromIconOrCursor(icon);
371 }
372
373 #ifndef NEVER_USE_DIB
374
375 bool wxBitmap::CopyFromDIB(const wxDIB& dib)
376 {
377 wxCHECK_MSG( dib.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
378
379 #ifdef SOMETIMES_USE_DIB
380 HBITMAP hbitmap = dib.CreateDDB();
381 if ( !hbitmap )
382 return false;
383 #else // ALWAYS_USE_DIB
384 HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast
385 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
386
387 UnRef();
388
389 wxBitmapRefData *refData = new wxBitmapRefData;
390 m_refData = refData;
391
392 refData->m_width = dib.GetWidth();
393 refData->m_height = dib.GetHeight();
394 refData->m_depth = dib.GetDepth();
395
396 refData->m_hBitmap = (WXHBITMAP)hbitmap;
397
398 #if wxUSE_PALETTE
399 wxPalette *palette = dib.CreatePalette();
400 if ( palette )
401 {
402 refData->m_bitmapPalette = *palette;
403 }
404
405 delete palette;
406 #endif // wxUSE_PALETTE
407
408 return true;
409 }
410
411 #endif // NEVER_USE_DIB
412
413 wxBitmap::~wxBitmap()
414 {
415 }
416
417 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
418 {
419 Init();
420
421 #ifndef __WXMICROWIN__
422 wxBitmapRefData *refData = new wxBitmapRefData;
423 m_refData = refData;
424
425 refData->m_width = width;
426 refData->m_height = height;
427 refData->m_depth = depth;
428
429 char *data;
430 if ( depth == 1 )
431 {
432 // we assume that it is in XBM format which is not quite the same as
433 // the format CreateBitmap() wants because the order of bytes in the
434 // line is reversed!
435 const size_t bytesPerLine = (width + 7) / 8;
436 const size_t padding = bytesPerLine % 2;
437 const size_t len = height * ( padding + bytesPerLine );
438 data = (char *)malloc(len);
439 const char *src = bits;
440 char *dst = data;
441
442 for ( int rows = 0; rows < height; rows++ )
443 {
444 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
445 {
446 unsigned char val = *src++;
447 unsigned char reversed = 0;
448
449 for ( int bits = 0; bits < 8; bits++)
450 {
451 reversed <<= 1;
452 reversed |= (unsigned char)(val & 0x01);
453 val >>= 1;
454 }
455 *dst++ = reversed;
456 }
457
458 if ( padding )
459 *dst++ = 0;
460 }
461 }
462 else
463 {
464 // bits should already be in Windows standard format
465 data = (char *)bits; // const_cast is harmless
466 }
467
468 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
469 if ( !hbmp )
470 {
471 wxLogLastError(wxT("CreateBitmap"));
472 }
473
474 if ( data != bits )
475 {
476 free(data);
477 }
478
479 SetHBITMAP((WXHBITMAP)hbmp);
480 #endif
481 }
482
483 // Create from XPM data
484 bool wxBitmap::CreateFromXpm(const char **data)
485 {
486 #if wxUSE_IMAGE && wxUSE_XPM
487 Init();
488
489 wxCHECK_MSG( data != NULL, false, wxT("invalid bitmap data") )
490
491 wxXPMDecoder decoder;
492 wxImage img = decoder.ReadData(data);
493 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") )
494
495 *this = wxBitmap(img);
496 return true;
497 #else
498 wxUnusedVar(data);
499 return false;
500 #endif
501 }
502
503 wxBitmap::wxBitmap(int w, int h, int d)
504 {
505 Init();
506
507 (void)Create(w, h, d);
508 }
509
510 wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
511 {
512 Init();
513
514 (void)Create(w, h, dc);
515 }
516
517 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
518 {
519 Init();
520
521 (void)Create(data, type, width, height, depth);
522 }
523
524 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
525 {
526 Init();
527
528 LoadFile(filename, (int)type);
529 }
530
531 bool wxBitmap::Create(int width, int height, int depth)
532 {
533 return DoCreate(width, height, depth, 0);
534 }
535
536 bool wxBitmap::Create(int width, int height, const wxDC& dc)
537 {
538 wxCHECK_MSG( dc.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
539
540 return DoCreate(width, height, -1, dc.GetHDC());
541 }
542
543 bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
544 {
545 UnRef();
546
547 m_refData = new wxBitmapRefData;
548
549 GetBitmapData()->m_width = w;
550 GetBitmapData()->m_height = h;
551
552 HBITMAP hbmp wxDUMMY_INITIALIZE(0);
553
554 #ifndef NEVER_USE_DIB
555 if ( wxShouldCreateDIB(w, h, d, hdc) )
556 {
557 if ( d == -1 )
558 {
559 // create DIBs without alpha channel by default
560 d = 24;
561 }
562
563 wxDIB dib(w, h, d);
564 if ( !dib.IsOk() )
565 return false;
566
567 // don't delete the DIB section in dib object dtor
568 hbmp = dib.Detach();
569
570 GetBitmapData()->m_isDIB = true;
571 GetBitmapData()->m_depth = d;
572 }
573 else // create a DDB
574 #endif // NEVER_USE_DIB
575 {
576 #ifndef ALWAYS_USE_DIB
577 #ifndef __WXMICROWIN__
578 if ( d > 0 )
579 {
580 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
581 if ( !hbmp )
582 {
583 wxLogLastError(wxT("CreateBitmap"));
584 }
585
586 GetBitmapData()->m_depth = d;
587 }
588 else // d == 0, create bitmap compatible with the screen
589 #endif // !__WXMICROWIN__
590 {
591 ScreenHDC dc;
592 hbmp = ::CreateCompatibleBitmap(dc, w, h);
593 if ( !hbmp )
594 {
595 wxLogLastError(wxT("CreateCompatibleBitmap"));
596 }
597
598 GetBitmapData()->m_depth = wxDisplayDepth();
599 }
600 #endif // !ALWAYS_USE_DIB
601 }
602
603 SetHBITMAP((WXHBITMAP)hbmp);
604
605 return Ok();
606 }
607
608 #if wxUSE_IMAGE
609
610 // ----------------------------------------------------------------------------
611 // wxImage to/from conversions for Microwin
612 // ----------------------------------------------------------------------------
613
614 // Microwin versions are so different from normal ones that it really doesn't
615 // make sense to use #ifdefs inside the function bodies
616 #ifdef __WXMICROWIN__
617
618 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
619 {
620 // Set this to 1 to experiment with mask code,
621 // which currently doesn't work
622 #define USE_MASKS 0
623
624 m_refData = new wxBitmapRefData();
625
626 // Initial attempt at a simple-minded implementation.
627 // The bitmap will always be created at the screen depth,
628 // so the 'depth' argument is ignored.
629
630 HDC hScreenDC = ::GetDC(NULL);
631 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
632
633 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
634 HBITMAP hMaskBitmap = NULL;
635 HBITMAP hOldMaskBitmap = NULL;
636 HDC hMaskDC = NULL;
637 unsigned char maskR = 0;
638 unsigned char maskG = 0;
639 unsigned char maskB = 0;
640
641 // printf("Created bitmap %d\n", (int) hBitmap);
642 if (hBitmap == NULL)
643 {
644 ::ReleaseDC(NULL, hScreenDC);
645 return false;
646 }
647 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
648
649 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
650 ::ReleaseDC(NULL, hScreenDC);
651
652 // created an mono-bitmap for the possible mask
653 bool hasMask = image.HasMask();
654
655 if ( hasMask )
656 {
657 #if USE_MASKS
658 // FIXME: we should be able to pass bpp = 1, but
659 // GdBlit can't handle a different depth
660 #if 0
661 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
662 #else
663 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
664 #endif
665 maskR = image.GetMaskRed();
666 maskG = image.GetMaskGreen();
667 maskB = image.GetMaskBlue();
668
669 if (!hMaskBitmap)
670 {
671 hasMask = false;
672 }
673 else
674 {
675 hScreenDC = ::GetDC(NULL);
676 hMaskDC = ::CreateCompatibleDC(hScreenDC);
677 ::ReleaseDC(NULL, hScreenDC);
678
679 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
680 }
681 #else
682 hasMask = false;
683 #endif
684 }
685
686 int i, j;
687 for (i = 0; i < image.GetWidth(); i++)
688 {
689 for (j = 0; j < image.GetHeight(); j++)
690 {
691 unsigned char red = image.GetRed(i, j);
692 unsigned char green = image.GetGreen(i, j);
693 unsigned char blue = image.GetBlue(i, j);
694
695 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
696
697 if (hasMask)
698 {
699 // scan the bitmap for the transparent colour and set the corresponding
700 // pixels in the mask to BLACK and the rest to WHITE
701 if (maskR == red && maskG == green && maskB == blue)
702 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
703 else
704 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
705 }
706 }
707 }
708
709 ::SelectObject(hMemDC, hOldBitmap);
710 ::DeleteDC(hMemDC);
711 if (hasMask)
712 {
713 ::SelectObject(hMaskDC, hOldMaskBitmap);
714 ::DeleteDC(hMaskDC);
715
716 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
717 }
718
719 SetWidth(image.GetWidth());
720 SetHeight(image.GetHeight());
721 SetDepth(screenDepth);
722 SetHBITMAP( (WXHBITMAP) hBitmap );
723
724 #if wxUSE_PALETTE
725 // Copy the palette from the source image
726 SetPalette(image.GetPalette());
727 #endif // wxUSE_PALETTE
728
729 return true;
730 }
731
732 wxImage wxBitmap::ConvertToImage() const
733 {
734 // Initial attempt at a simple-minded implementation.
735 // The bitmap will always be created at the screen depth,
736 // so the 'depth' argument is ignored.
737 // TODO: transparency (create a mask image)
738
739 if (!Ok())
740 {
741 wxFAIL_MSG( wxT("bitmap is invalid") );
742 return wxNullImage;
743 }
744
745 wxImage image;
746
747 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
748
749 // create an wxImage object
750 int width = GetWidth();
751 int height = GetHeight();
752 image.Create( width, height );
753 unsigned char *data = image.GetData();
754 if( !data )
755 {
756 wxFAIL_MSG( wxT("could not allocate data for image") );
757 return wxNullImage;
758 }
759
760 HDC hScreenDC = ::GetDC(NULL);
761
762 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
763 ::ReleaseDC(NULL, hScreenDC);
764
765 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
766
767 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
768
769 int i, j;
770 for (i = 0; i < GetWidth(); i++)
771 {
772 for (j = 0; j < GetHeight(); j++)
773 {
774 COLORREF color = ::GetPixel(hMemDC, i, j);
775 unsigned char red = GetRValue(color);
776 unsigned char green = GetGValue(color);
777 unsigned char blue = GetBValue(color);
778
779 image.SetRGB(i, j, red, green, blue);
780 }
781 }
782
783 ::SelectObject(hMemDC, hOldBitmap);
784 ::DeleteDC(hMemDC);
785
786 #if wxUSE_PALETTE
787 // Copy the palette from the source image
788 if (GetPalette())
789 image.SetPalette(* GetPalette());
790 #endif // wxUSE_PALETTE
791
792 return image;
793 }
794
795 #endif // __WXMICROWIN__
796
797 // ----------------------------------------------------------------------------
798 // wxImage to/from conversions
799 // ----------------------------------------------------------------------------
800
801 #if wxUSE_WXDIB
802
803 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
804 {
805 return CreateFromImage(image, depth, 0);
806 }
807
808 bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
809 {
810 wxCHECK_MSG( dc.Ok(), false,
811 _T("invalid HDC in wxBitmap::CreateFromImage()") );
812
813 return CreateFromImage(image, -1, dc.GetHDC());
814 }
815
816 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
817 {
818 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
819
820 UnRef();
821
822 // first convert the image to DIB
823 const int h = image.GetHeight();
824 const int w = image.GetWidth();
825
826 wxDIB dib(image);
827 if ( !dib.IsOk() )
828 return false;
829
830
831 // store the bitmap parameters
832 wxBitmapRefData *refData = new wxBitmapRefData;
833 refData->m_width = w;
834 refData->m_height = h;
835 refData->m_hasAlpha = image.HasAlpha();
836
837 m_refData = refData;
838
839
840 // next either store DIB as is or create a DDB from it
841 HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
842
843 // are we going to use DIB?
844 //
845 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
846 if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
847 {
848 // don't delete the DIB section in dib object dtor
849 hbitmap = dib.Detach();
850
851 refData->m_isDIB = true;
852 refData->m_depth = dib.GetDepth();
853 }
854 #ifndef ALWAYS_USE_DIB
855 else // we need to convert DIB to DDB
856 {
857 hbitmap = dib.CreateDDB((HDC)hdc);
858
859 refData->m_depth = depth == -1 ? dib.GetDepth() : depth;
860 }
861 #endif // !ALWAYS_USE_DIB
862
863 // validate this object
864 SetHBITMAP((WXHBITMAP)hbitmap);
865
866 // finally also set the mask if we have one
867 if ( image.HasMask() )
868 {
869 const size_t len = 2*((w+15)/16);
870 BYTE *src = image.GetData();
871 BYTE *data = new BYTE[h*len];
872 memset(data, 0, h*len);
873 BYTE r = image.GetMaskRed(),
874 g = image.GetMaskGreen(),
875 b = image.GetMaskBlue();
876 BYTE *dst = data;
877 for ( int y = 0; y < h; y++, dst += len )
878 {
879 BYTE *dstLine = dst;
880 BYTE mask = 0x80;
881 for ( int x = 0; x < w; x++, src += 3 )
882 {
883 if (src[0] != r || src[1] != g || src[2] != b)
884 *dstLine |= mask;
885
886 if ( (mask >>= 1) == 0 )
887 {
888 dstLine++;
889 mask = 0x80;
890 }
891 }
892 }
893
894 hbitmap = ::CreateBitmap(w, h, 1, 1, data);
895 if ( !hbitmap )
896 {
897 wxLogLastError(_T("CreateBitmap(mask)"));
898 }
899 else
900 {
901 SetMask(new wxMask((WXHBITMAP)hbitmap));
902 }
903
904 delete[] data;
905 }
906
907 return true;
908 }
909
910 wxImage wxBitmap::ConvertToImage() const
911 {
912 // convert DDB to DIB
913 wxDIB dib(*this);
914
915 if ( !dib.IsOk() )
916 {
917 return wxNullImage;
918 }
919
920 // and then DIB to our wxImage
921 wxImage image = dib.ConvertToImage();
922 if ( !image.Ok() )
923 {
924 return wxNullImage;
925 }
926
927 // now do the same for the mask, if we have any
928 HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
929 if ( hbmpMask )
930 {
931 wxDIB dibMask(hbmpMask);
932 if ( dibMask.IsOk() )
933 {
934 // TODO: use wxRawBitmap to iterate over DIB
935
936 // we hard code the mask colour for now but we could also make an
937 // effort (and waste time) to choose a colour not present in the
938 // image already to avoid having to fudge the pixels below --
939 // whether it's worth to do it is unclear however
940 static const int MASK_RED = 1;
941 static const int MASK_GREEN = 2;
942 static const int MASK_BLUE = 3;
943 static const int MASK_BLUE_REPLACEMENT = 2;
944
945 const int h = dibMask.GetHeight();
946 const int w = dibMask.GetWidth();
947 const int bpp = dibMask.GetDepth();
948 const int maskBytesPerPixel = bpp >> 3;
949 const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
950 unsigned char *data = image.GetData();
951
952 // remember that DIBs are stored in bottom to top order
953 unsigned char *
954 maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
955
956 for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
957 {
958 // traverse one mask DIB line
959 unsigned char *mask = maskLineStart;
960 for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
961 {
962 // should this pixel be transparent?
963 if ( *mask )
964 {
965 // no, check that it isn't transparent by accident
966 if ( (data[0] == MASK_RED) &&
967 (data[1] == MASK_GREEN) &&
968 (data[2] == MASK_BLUE) )
969 {
970 // we have to fudge the colour a bit to prevent
971 // this pixel from appearing transparent
972 data[2] = MASK_BLUE_REPLACEMENT;
973 }
974
975 data += 3;
976 }
977 else // yes, transparent pixel
978 {
979 *data++ = MASK_RED;
980 *data++ = MASK_GREEN;
981 *data++ = MASK_BLUE;
982 }
983 }
984 }
985
986 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
987 }
988 }
989
990 return image;
991 }
992
993 #endif // 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
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
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 wxMask* mask;
1639 wxBitmap newbmp;
1640 if ( bmp.HasAlpha() )
1641 {
1642 // Convert alpha to a mask. NOTE: It would be better to actually put
1643 // the alpha into the icon instead of making a mask, but I don't have
1644 // time to figure that out today.
1645 wxImage img = bmp.ConvertToImage();
1646 img.ConvertAlphaToMask();
1647 newbmp = wxBitmap(img);
1648 mask = newbmp.GetMask();
1649 }
1650 else
1651 {
1652 mask = bmp.GetMask();
1653 }
1654
1655 if ( !mask )
1656 {
1657 // we must have a mask for an icon, so even if it's probably incorrect,
1658 // do create it (grey is the "standard" transparent colour)
1659 mask = new wxMask(bmp, *wxLIGHT_GREY);
1660 }
1661
1662 ICONINFO iconInfo;
1663 wxZeroMemory(iconInfo);
1664 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1665 if ( !iconWanted )
1666 {
1667 iconInfo.xHotspot = hotSpotX;
1668 iconInfo.yHotspot = hotSpotY;
1669 }
1670
1671 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1672 iconInfo.hbmColor = GetHbitmapOf(bmp);
1673
1674 // black out the transparent area to preserve background colour, because
1675 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1676 // the mask to the dest rect.
1677 {
1678 MemoryHDC dcSrc, dcDst;
1679 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1680 selectBitmap(dcDst, iconInfo.hbmColor);
1681
1682 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1683 dcSrc, 0, 0, SRCAND) )
1684 {
1685 wxLogLastError(_T("BitBlt"));
1686 }
1687 }
1688
1689 HICON hicon = ::CreateIconIndirect(&iconInfo);
1690
1691 if ( !bmp.GetMask() && !bmp.HasAlpha() )
1692 {
1693 // we created the mask, now delete it
1694 delete mask;
1695 }
1696
1697 // delete the inverted mask bitmap we created as well
1698 ::DeleteObject(iconInfo.hbmMask);
1699
1700 return hicon;
1701 }
1702
1703 HICON wxBitmapToHICON(const wxBitmap& bmp)
1704 {
1705 return wxBitmapToIconOrCursor(bmp, true, 0, 0);
1706 }
1707
1708 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1709 {
1710 return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY);
1711 }
1712
1713 HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1714 {
1715 #ifndef __WXMICROWIN__
1716 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1717
1718 // get width/height from the bitmap if not given
1719 if ( !w || !h )
1720 {
1721 BITMAP bm;
1722 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1723 w = bm.bmWidth;
1724 h = bm.bmHeight;
1725 }
1726
1727 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1728 HDC hdcDst = ::CreateCompatibleDC(NULL);
1729 if ( !hdcSrc || !hdcDst )
1730 {
1731 wxLogLastError(wxT("CreateCompatibleDC"));
1732 }
1733
1734 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1735 if ( !hbmpInvMask )
1736 {
1737 wxLogLastError(wxT("CreateBitmap"));
1738 }
1739
1740 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1741 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
1742 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1743 hdcSrc, 0, 0,
1744 NOTSRCCOPY) )
1745 {
1746 wxLogLastError(wxT("BitBlt"));
1747 }
1748
1749 // Deselect objects
1750 SelectObject(hdcSrc,srcTmp);
1751 SelectObject(hdcDst,dstTmp);
1752
1753 ::DeleteDC(hdcSrc);
1754 ::DeleteDC(hdcDst);
1755
1756 return hbmpInvMask;
1757 #else
1758 return 0;
1759 #endif
1760 }