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