]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
Corrected typo
[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 if ( d == -1 )
524 d = wxDisplayDepth();
525
526 GetBitmapData()->m_depth = d;
527
528 #ifndef __WXMICROWIN__
529 if ( d > 0 )
530 {
531 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
532 if ( !hbmp )
533 {
534 wxLogLastError(wxT("CreateBitmap"));
535 }
536 }
537 else
538 #endif // !__WXMICROWIN__
539 {
540 ScreenHDC dc;
541 hbmp = ::CreateCompatibleBitmap(dc, w, h);
542 if ( !hbmp )
543 {
544 wxLogLastError(wxT("CreateCompatibleBitmap"));
545 }
546
547 GetBitmapData()->m_depth = wxDisplayDepth();
548 }
549 }
550
551 SetHBITMAP((WXHBITMAP)hbmp);
552
553 return Ok();
554 }
555
556 #if wxUSE_IMAGE
557
558 // ----------------------------------------------------------------------------
559 // wxImage to/from conversions for Microwin
560 // ----------------------------------------------------------------------------
561
562 // Microwin versions are so different from normal ones that it really doesn't
563 // make sense to use #ifdefs inside the function bodies
564 #ifdef __WXMICROWIN__
565
566 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
567 {
568 // Set this to 1 to experiment with mask code,
569 // which currently doesn't work
570 #define USE_MASKS 0
571
572 m_refData = new wxBitmapRefData();
573
574 // Initial attempt at a simple-minded implementation.
575 // The bitmap will always be created at the screen depth,
576 // so the 'depth' argument is ignored.
577
578 HDC hScreenDC = ::GetDC(NULL);
579 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
580
581 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
582 HBITMAP hMaskBitmap = NULL;
583 HBITMAP hOldMaskBitmap = NULL;
584 HDC hMaskDC = NULL;
585 unsigned char maskR = 0;
586 unsigned char maskG = 0;
587 unsigned char maskB = 0;
588
589 // printf("Created bitmap %d\n", (int) hBitmap);
590 if (hBitmap == NULL)
591 {
592 ::ReleaseDC(NULL, hScreenDC);
593 return FALSE;
594 }
595 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
596
597 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
598 ::ReleaseDC(NULL, hScreenDC);
599
600 // created an mono-bitmap for the possible mask
601 bool hasMask = image.HasMask();
602
603 if ( hasMask )
604 {
605 #if USE_MASKS
606 // FIXME: we should be able to pass bpp = 1, but
607 // GdBlit can't handle a different depth
608 #if 0
609 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
610 #else
611 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
612 #endif
613 maskR = image.GetMaskRed();
614 maskG = image.GetMaskGreen();
615 maskB = image.GetMaskBlue();
616
617 if (!hMaskBitmap)
618 {
619 hasMask = FALSE;
620 }
621 else
622 {
623 hScreenDC = ::GetDC(NULL);
624 hMaskDC = ::CreateCompatibleDC(hScreenDC);
625 ::ReleaseDC(NULL, hScreenDC);
626
627 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
628 }
629 #else
630 hasMask = FALSE;
631 #endif
632 }
633
634 int i, j;
635 for (i = 0; i < image.GetWidth(); i++)
636 {
637 for (j = 0; j < image.GetHeight(); j++)
638 {
639 unsigned char red = image.GetRed(i, j);
640 unsigned char green = image.GetGreen(i, j);
641 unsigned char blue = image.GetBlue(i, j);
642
643 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
644
645 if (hasMask)
646 {
647 // scan the bitmap for the transparent colour and set the corresponding
648 // pixels in the mask to BLACK and the rest to WHITE
649 if (maskR == red && maskG == green && maskB == blue)
650 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
651 else
652 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
653 }
654 }
655 }
656
657 ::SelectObject(hMemDC, hOldBitmap);
658 ::DeleteDC(hMemDC);
659 if (hasMask)
660 {
661 ::SelectObject(hMaskDC, hOldMaskBitmap);
662 ::DeleteDC(hMaskDC);
663
664 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
665 }
666
667 SetWidth(image.GetWidth());
668 SetHeight(image.GetHeight());
669 SetDepth(screenDepth);
670 SetHBITMAP( (WXHBITMAP) hBitmap );
671
672 #if wxUSE_PALETTE
673 // Copy the palette from the source image
674 SetPalette(image.GetPalette());
675 #endif // wxUSE_PALETTE
676
677 return TRUE;
678 }
679
680 wxImage wxBitmap::ConvertToImage() const
681 {
682 // Initial attempt at a simple-minded implementation.
683 // The bitmap will always be created at the screen depth,
684 // so the 'depth' argument is ignored.
685 // TODO: transparency (create a mask image)
686
687 if (!Ok())
688 {
689 wxFAIL_MSG( wxT("bitmap is invalid") );
690 return wxNullImage;
691 }
692
693 wxImage image;
694
695 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
696
697 // create an wxImage object
698 int width = GetWidth();
699 int height = GetHeight();
700 image.Create( width, height );
701 unsigned char *data = image.GetData();
702 if( !data )
703 {
704 wxFAIL_MSG( wxT("could not allocate data for image") );
705 return wxNullImage;
706 }
707
708 HDC hScreenDC = ::GetDC(NULL);
709
710 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
711 ::ReleaseDC(NULL, hScreenDC);
712
713 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
714
715 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
716
717 int i, j;
718 for (i = 0; i < GetWidth(); i++)
719 {
720 for (j = 0; j < GetHeight(); j++)
721 {
722 COLORREF color = ::GetPixel(hMemDC, i, j);
723 unsigned char red = GetRValue(color);
724 unsigned char green = GetGValue(color);
725 unsigned char blue = GetBValue(color);
726
727 image.SetRGB(i, j, red, green, blue);
728 }
729 }
730
731 ::SelectObject(hMemDC, hOldBitmap);
732 ::DeleteDC(hMemDC);
733
734 #if wxUSE_PALETTE
735 // Copy the palette from the source image
736 if (GetPalette())
737 image.SetPalette(* GetPalette());
738 #endif // wxUSE_PALETTE
739
740 return image;
741 }
742
743 #endif // __WXMICROWIN__
744
745 // ----------------------------------------------------------------------------
746 // wxImage to/from conversions
747 // ----------------------------------------------------------------------------
748
749 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
750 {
751 return CreateFromImage(image, depth, 0);
752 }
753
754 bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
755 {
756 wxCHECK_MSG( dc.Ok(), FALSE,
757 _T("invalid HDC in wxBitmap::CreateFromImage()") );
758
759 return CreateFromImage(image, -1, dc.GetHDC());
760 }
761
762 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
763 {
764 #if wxUSE_WXDIB
765 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
766
767 UnRef();
768
769 // first convert the image to DIB
770 const int h = image.GetHeight();
771 const int w = image.GetWidth();
772
773 wxDIB dib(image);
774 if ( !dib.IsOk() )
775 return FALSE;
776
777
778 // store the bitmap parameters
779 wxBitmapRefData *refData = new wxBitmapRefData;
780 refData->m_width = w;
781 refData->m_height = h;
782 refData->m_hasAlpha = image.HasAlpha();
783
784 m_refData = refData;
785
786
787 // next either store DIB as is or create a DDB from it
788 HBITMAP hbitmap;
789
790 // are we going to use DIB?
791 //
792 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
793 if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
794 {
795 // don't delete the DIB section in dib object dtor
796 hbitmap = dib.Detach();
797
798 refData->m_isDIB = TRUE;
799 refData->m_depth = dib.GetDepth();
800 }
801 else // we need to convert DIB to DDB
802 {
803 hbitmap = dib.CreateDDB((HDC)hdc);
804
805 refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
806 }
807
808 // validate this object
809 SetHBITMAP((WXHBITMAP)hbitmap);
810
811 // finally also set the mask if we have one
812 if ( image.HasMask() )
813 {
814 SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
815 image.GetMaskGreen(),
816 image.GetMaskBlue())));
817 }
818
819 return TRUE;
820 #else
821 // FIXME: wxWinCE doesn't support wxDIB yet
822 return FALSE;
823 #endif
824 }
825
826 wxImage wxBitmap::ConvertToImage() const
827 {
828 // FIXME: this is untested code for WinCE, and
829 // the mask is not yet handled.
830 // For tips, see:
831 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
832
833 #ifdef __WXWINCE__
834 // the colour used as transparent one in wxImage and the one it is replaced
835 // with when it really occurs in the bitmap
836 static const int MASK_RED = 1;
837 static const int MASK_GREEN = 2;
838 static const int MASK_BLUE = 3;
839 static const int MASK_BLUE_REPLACEMENT = 2;
840
841 wxImage image;
842
843 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
844
845 // create an wxImage object
846 int width = GetWidth();
847 int height = GetHeight();
848 image.Create( width, height );
849 unsigned char *data = image.GetData();
850 if( !data )
851 {
852 wxFAIL_MSG( wxT("could not allocate data for image") );
853 return wxNullImage;
854 }
855
856 // calc the number of bytes per scanline and padding in the DIB
857 int bytePerLine = width*3;
858 int sizeDWORD = sizeof( DWORD );
859 int lineBoundary = bytePerLine % sizeDWORD;
860 int padding = 0;
861 if( lineBoundary > 0 )
862 {
863 padding = sizeDWORD - lineBoundary;
864 bytePerLine += padding;
865 }
866
867 // create a DIB header
868 int headersize = sizeof(BITMAPINFOHEADER);
869 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
870 if( !lpDIBh )
871 {
872 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
873 free( data );
874 return wxNullImage;
875 }
876 // Fill in the DIB header
877 lpDIBh->bmiHeader.biSize = headersize;
878 lpDIBh->bmiHeader.biWidth = width;
879 lpDIBh->bmiHeader.biHeight = -height;
880 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
881 lpDIBh->bmiHeader.biPlanes = 1;
882 lpDIBh->bmiHeader.biBitCount = 24;
883 lpDIBh->bmiHeader.biCompression = BI_RGB;
884 lpDIBh->bmiHeader.biClrUsed = 0;
885 // These seem not really needed for our purpose here.
886 lpDIBh->bmiHeader.biClrImportant = 0;
887 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
888 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
889
890 // memory for DIB data is allocated by CreateDIBSection
891 void *lpBits = NULL;
892
893 // copy data from the device-dependent bitmap to the DIB
894 HDC hdc = ::GetDC(NULL);
895 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
896
897 HBITMAP hBitmapSection = ::CreateDIBSection( hdc, lpDIBh, DIB_RGB_COLORS, & lpBits, NULL, 0 );
898 if (!hBitmapSection)
899 {
900 wxFAIL_MSG( wxT("could not create a DIB section") );
901 return wxNullImage;
902 }
903
904 // Copy the image from the DDB to the DIBSection
905 // Need to copy the supplied bitmap onto the newly created DIBsection
906 HDC hMemDC = CreateCompatibleDC(hdc);
907 HDC hCopyDC = CreateCompatibleDC(hdc);
908
909 if (! hMemDC || ! hCopyDC)
910 {
911 wxFAIL_MSG( wxT("unable to create compatible DCs") );
912 return wxNullImage;
913 }
914
915 #if 0
916 if (m_hPal)
917 {
918 SelectPalette(hMemDC, m_hPal, FALSE); RealizePalette(hMemDC);
919 SelectPalette(hCopyDC, m_hPal, FALSE); RealizePalette(hCopyDC);
920 }
921 #endif
922
923 HBITMAP hOldMemBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
924 HBITMAP hOldCopyBitmap = (HBITMAP) SelectObject(hCopyDC, hBitmapSection);
925
926 BitBlt(hCopyDC, 0, 0, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);
927
928 SelectObject(hMemDC, hOldMemBitmap);
929 SelectObject(hCopyDC, hOldCopyBitmap);
930 DeleteDC(hMemDC);
931 DeleteDC(hCopyDC);
932
933 #if 0
934 if (m_hPal)
935 {
936 HGDIOBJ hObj = ::GetStockObject(DEFAULT_PALETTE);
937 SelectObject(hMemDC, hObj);
938 SelectObject(hCopyDC, hObj);
939 }
940 #endif
941
942 ReleaseDC(NULL, hdc);
943
944 // copy DIB data into the wxImage object
945 int i, j;
946 unsigned char *ptdata = data;
947 unsigned char *ptbits = (unsigned char*) lpBits;
948 for( i=0; i<height; i++ )
949 {
950 for( j=0; j<width; j++ )
951 {
952 *(ptdata++) = *(ptbits+2);
953 *(ptdata++) = *(ptbits+1);
954 *(ptdata++) = *(ptbits );
955 ptbits += 3;
956 }
957 ptbits += padding;
958 }
959
960 // TODO
961 #if 0
962 // similarly, set data according to the possible mask bitmap
963 if( GetMask() && GetMask()->GetMaskBitmap() )
964 {
965 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
966 // memory DC created, color set, data copied, and memory DC deleted
967 HDC memdc = ::CreateCompatibleDC( hdc );
968 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
969 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
970 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
971 ::DeleteDC( memdc );
972 ptdata = data;
973 ptbits = lpBits;
974 for( i=0; i<height; i++ )
975 {
976 for( j=0; j<width; j++ )
977 {
978 // is this pixel transparent?
979 if ( *ptbits != 0 )
980 {
981 if ( (ptdata[0] == MASK_RED) &&
982 (ptdata[1] == MASK_GREEN) &&
983 (ptdata[2] == MASK_BLUE) )
984 {
985 // we have to fudge the colour a bit to prevent this
986 // pixel from appearing transparent
987 ptdata[2] = MASK_BLUE_REPLACEMENT;
988 }
989 ptdata += 3;
990 }
991 else // masked pixel
992 {
993 *(ptdata++) = MASK_RED;
994 *(ptdata++) = MASK_GREEN;
995 *(ptdata++) = MASK_BLUE;
996 }
997 ptbits += 3;
998 }
999 ptbits += padding;
1000 }
1001
1002 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1003 image.SetMask( TRUE );
1004 }
1005 else
1006 #endif
1007 {
1008 image.SetMask( FALSE );
1009 }
1010
1011 // free allocated resources
1012 ::ReleaseDC(NULL, hdc);
1013 free(lpDIBh);
1014
1015 // Delete the DIB section
1016 ::DeleteObject(hBitmapSection);
1017
1018 return image;
1019 #else
1020 // the colour used as transparent one in wxImage and the one it is replaced
1021 // with when it really occurs in the bitmap
1022 static const int MASK_RED = 1;
1023 static const int MASK_GREEN = 2;
1024 static const int MASK_BLUE = 3;
1025 static const int MASK_BLUE_REPLACEMENT = 2;
1026
1027 wxImage image;
1028
1029 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1030
1031 // create an wxImage object
1032 int width = GetWidth();
1033 int height = GetHeight();
1034 image.Create( width, height );
1035 unsigned char *data = image.GetData();
1036 if( !data )
1037 {
1038 wxFAIL_MSG( wxT("could not allocate data for image") );
1039 return wxNullImage;
1040 }
1041
1042 // calc the number of bytes per scanline and padding in the DIB
1043 int bytePerLine = width*3;
1044 int sizeDWORD = sizeof( DWORD );
1045 int lineBoundary = bytePerLine % sizeDWORD;
1046 int padding = 0;
1047 if( lineBoundary > 0 )
1048 {
1049 padding = sizeDWORD - lineBoundary;
1050 bytePerLine += padding;
1051 }
1052
1053 // create a DIB header
1054 int headersize = sizeof(BITMAPINFOHEADER);
1055 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
1056 if( !lpDIBh )
1057 {
1058 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1059 free( data );
1060 return wxNullImage;
1061 }
1062 // Fill in the DIB header
1063 lpDIBh->bmiHeader.biSize = headersize;
1064 lpDIBh->bmiHeader.biWidth = width;
1065 lpDIBh->bmiHeader.biHeight = -height;
1066 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
1067 lpDIBh->bmiHeader.biPlanes = 1;
1068 lpDIBh->bmiHeader.biBitCount = 24;
1069 lpDIBh->bmiHeader.biCompression = BI_RGB;
1070 lpDIBh->bmiHeader.biClrUsed = 0;
1071 // These seem not really needed for our purpose here.
1072 lpDIBh->bmiHeader.biClrImportant = 0;
1073 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1074 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1075 // memory for DIB data
1076 unsigned char *lpBits;
1077 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
1078 if( !lpBits )
1079 {
1080 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1081 free( data );
1082 free( lpDIBh );
1083 return wxNullImage;
1084 }
1085
1086 // copy data from the device-dependent bitmap to the DIB
1087 HDC hdc = ::GetDC(NULL);
1088 HBITMAP hbitmap;
1089 hbitmap = (HBITMAP) GetHBITMAP();
1090 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1091
1092 // copy DIB data into the wxImage object
1093 int i, j;
1094 unsigned char *ptdata = data;
1095 unsigned char *ptbits = lpBits;
1096 for( i=0; i<height; i++ )
1097 {
1098 for( j=0; j<width; j++ )
1099 {
1100 *(ptdata++) = *(ptbits+2);
1101 *(ptdata++) = *(ptbits+1);
1102 *(ptdata++) = *(ptbits );
1103 ptbits += 3;
1104 }
1105 ptbits += padding;
1106 }
1107
1108 // similarly, set data according to the possible mask bitmap
1109 if( GetMask() && GetMask()->GetMaskBitmap() )
1110 {
1111 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
1112 // memory DC created, color set, data copied, and memory DC deleted
1113 HDC memdc = ::CreateCompatibleDC( hdc );
1114 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1115 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1116 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1117 ::DeleteDC( memdc );
1118 ptdata = data;
1119 ptbits = lpBits;
1120 for( i=0; i<height; i++ )
1121 {
1122 for( j=0; j<width; j++ )
1123 {
1124 // is this pixel transparent?
1125 if ( *ptbits != 0 )
1126 {
1127 if ( (ptdata[0] == MASK_RED) &&
1128 (ptdata[1] == MASK_GREEN) &&
1129 (ptdata[2] == MASK_BLUE) )
1130 {
1131 // we have to fudge the colour a bit to prevent this
1132 // pixel from appearing transparent
1133 ptdata[2] = MASK_BLUE_REPLACEMENT;
1134 }
1135 ptdata += 3;
1136 }
1137 else // masked pixel
1138 {
1139 *(ptdata++) = MASK_RED;
1140 *(ptdata++) = MASK_GREEN;
1141 *(ptdata++) = MASK_BLUE;
1142 }
1143 ptbits += 3;
1144 }
1145 ptbits += padding;
1146 }
1147
1148 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1149 image.SetMask( TRUE );
1150 }
1151 else
1152 {
1153 image.SetMask( FALSE );
1154 }
1155 // free allocated resources
1156 ::ReleaseDC(NULL, hdc);
1157 free(lpDIBh);
1158 free(lpBits);
1159
1160 return image;
1161 #endif
1162 }
1163
1164 #endif // wxUSE_IMAGE
1165
1166 // ----------------------------------------------------------------------------
1167 // loading and saving bitmaps
1168 // ----------------------------------------------------------------------------
1169
1170 bool wxBitmap::LoadFile(const wxString& filename, long type)
1171 {
1172 UnRef();
1173
1174 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1175
1176 if ( handler )
1177 {
1178 m_refData = new wxBitmapRefData;
1179
1180 return handler->LoadFile(this, filename, type, -1, -1);
1181 }
1182 #if wxUSE_IMAGE
1183 else
1184 {
1185 wxImage image;
1186 if ( image.LoadFile( filename, type ) && image.Ok() )
1187 {
1188 *this = wxBitmap(image);
1189
1190 return TRUE;
1191 }
1192 }
1193 #endif // wxUSE_IMAGE
1194
1195 return FALSE;
1196 }
1197
1198 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
1199 {
1200 UnRef();
1201
1202 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1203
1204 if ( !handler )
1205 {
1206 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
1207
1208 return FALSE;
1209 }
1210
1211 m_refData = new wxBitmapRefData;
1212
1213 return handler->Create(this, data, type, width, height, depth);
1214 }
1215
1216 bool wxBitmap::SaveFile(const wxString& filename,
1217 int type,
1218 const wxPalette *palette)
1219 {
1220 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1221
1222 if ( handler )
1223 {
1224 return handler->SaveFile(this, filename, type, palette);
1225 }
1226 #if wxUSE_IMAGE
1227 else
1228 {
1229 // FIXME what about palette? shouldn't we use it?
1230 wxImage image = ConvertToImage();
1231 if ( image.Ok() )
1232 {
1233 return image.SaveFile(filename, type);
1234 }
1235 }
1236 #endif // wxUSE_IMAGE
1237
1238 return FALSE;
1239 }
1240
1241 // ----------------------------------------------------------------------------
1242 // sub bitmap extraction
1243 // ----------------------------------------------------------------------------
1244
1245 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1246 {
1247 wxCHECK_MSG( Ok() &&
1248 (rect.x >= 0) && (rect.y >= 0) &&
1249 (rect.x+rect.width <= GetWidth()) &&
1250 (rect.y+rect.height <= GetHeight()),
1251 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1252
1253 wxBitmap ret( rect.width, rect.height );
1254 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1255
1256 #ifndef __WXMICROWIN__
1257 // TODO: copy alpha channel data if any
1258
1259 // copy bitmap data
1260 MemoryHDC dcSrc,
1261 dcDst;
1262
1263 {
1264 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
1265 selectDst(dcDst, GetHbitmapOf(ret));
1266
1267 if ( !selectSrc || !selectDst )
1268 {
1269 wxLogLastError(_T("SelectObjct(hBitmap)"));
1270 }
1271
1272 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1273 dcSrc, rect.x, rect.y, SRCCOPY) )
1274 {
1275 wxLogLastError(_T("BitBlt"));
1276 }
1277 }
1278
1279 // copy mask if there is one
1280 if ( GetMask() )
1281 {
1282 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1283
1284 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1285 selectDst(dcDst, hbmpMask);
1286
1287 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1288 dcSrc, rect.x, rect.y, SRCCOPY) )
1289 {
1290 wxLogLastError(_T("BitBlt"));
1291 }
1292
1293 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1294 ret.SetMask(mask);
1295 }
1296 #endif // !__WXMICROWIN__
1297
1298 return ret;
1299 }
1300
1301 // ----------------------------------------------------------------------------
1302 // wxBitmap accessors
1303 // ----------------------------------------------------------------------------
1304
1305 #if wxUSE_PALETTE
1306 wxPalette* wxBitmap::GetPalette() const
1307 {
1308 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1309 : (wxPalette *) NULL;
1310 }
1311 #endif
1312
1313 wxMask *wxBitmap::GetMask() const
1314 {
1315 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1316 }
1317
1318 #ifdef __WXDEBUG__
1319
1320 wxDC *wxBitmap::GetSelectedInto() const
1321 {
1322 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
1323 }
1324
1325 #endif
1326
1327 #if WXWIN_COMPATIBILITY_2_4
1328
1329 int wxBitmap::GetQuality() const
1330 {
1331 return 0;
1332 }
1333
1334 #endif // WXWIN_COMPATIBILITY_2_4
1335
1336 void wxBitmap::UseAlpha()
1337 {
1338 if ( GetBitmapData() )
1339 GetBitmapData()->m_hasAlpha = true;
1340 }
1341
1342 bool wxBitmap::HasAlpha() const
1343 {
1344 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1345 }
1346
1347 // ----------------------------------------------------------------------------
1348 // wxBitmap setters
1349 // ----------------------------------------------------------------------------
1350
1351 #ifdef __WXDEBUG__
1352
1353 void wxBitmap::SetSelectedInto(wxDC *dc)
1354 {
1355 if ( GetBitmapData() )
1356 GetBitmapData()->m_selectedInto = dc;
1357 }
1358
1359 #endif
1360
1361 #if wxUSE_PALETTE
1362
1363 void wxBitmap::SetPalette(const wxPalette& palette)
1364 {
1365 EnsureHasData();
1366
1367 GetBitmapData()->m_bitmapPalette = palette;
1368 }
1369
1370 #endif // wxUSE_PALETTE
1371
1372 void wxBitmap::SetMask(wxMask *mask)
1373 {
1374 EnsureHasData();
1375
1376 GetBitmapData()->SetMask(mask);
1377 }
1378
1379 #if WXWIN_COMPATIBILITY_2_4
1380
1381 void wxBitmap::SetQuality(int WXUNUSED(quality))
1382 {
1383 }
1384
1385 #endif // WXWIN_COMPATIBILITY_2_4
1386
1387 // ----------------------------------------------------------------------------
1388 // raw bitmap access support
1389 // ----------------------------------------------------------------------------
1390
1391 #ifdef wxHAVE_RAW_BITMAP
1392 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1393 {
1394 #if wxUSE_WXDIB
1395 if ( !Ok() )
1396 {
1397 // no bitmap, no data (raw or otherwise)
1398 return NULL;
1399 }
1400
1401 // if we're already a DIB we can access our data directly, but if not we
1402 // need to convert this DDB to a DIB section and use it for raw access and
1403 // then convert it back
1404 HBITMAP hDIB;
1405 if ( !GetBitmapData()->m_isDIB )
1406 {
1407 wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE,
1408 _T("GetRawData() may be called only once") );
1409
1410 wxDIB *dib = new wxDIB(*this);
1411 if ( !dib->IsOk() )
1412 {
1413 delete dib;
1414
1415 return NULL;
1416 }
1417
1418 // we'll free it in UngetRawData()
1419 GetBitmapData()->m_dib = dib;
1420
1421 hDIB = dib->GetHandle();
1422 }
1423 else // we're a DIB
1424 {
1425 hDIB = GetHbitmap();
1426 }
1427
1428 DIBSECTION ds;
1429 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
1430 {
1431 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1432
1433 return NULL;
1434 }
1435
1436 // check that the bitmap is in correct format
1437 if ( ds.dsBm.bmBitsPixel != bpp )
1438 {
1439 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1440
1441 return NULL;
1442 }
1443
1444 // ok, store the relevant info in wxPixelDataBase
1445 const LONG h = ds.dsBm.bmHeight;
1446
1447 data.m_width = ds.dsBm.bmWidth;
1448 data.m_height = h;
1449
1450 // remember that DIBs are stored in top to bottom order!
1451 const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
1452 data.m_stride = -bytesPerRow;
1453
1454 char *bits = (char *)ds.dsBm.bmBits;
1455 if ( h > 1 )
1456 {
1457 bits += (h - 1)*bytesPerRow;
1458 }
1459
1460 return bits;
1461 #else
1462 return NULL;
1463 #endif
1464 }
1465
1466 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1467 {
1468 #if wxUSE_WXDIB
1469 if ( !Ok() )
1470 return;
1471
1472 // the cast is ugly but we can't do without it and without making this
1473 // function template (and hence inline) unfortunately
1474 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> PixelData;
1475 PixelData& data = (PixelData &)dataBase;
1476
1477 if ( !data )
1478 {
1479 // invalid data, don't crash -- but don't assert neither as we're
1480 // called automatically from wxPixelDataBase dtor and so there is no
1481 // way to prevent this from happening
1482 return;
1483 }
1484
1485 if ( GetBitmapData()->m_hasAlpha )
1486 {
1487 // AlphaBlend() wants to have premultiplied source alpha but
1488 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1489 // them here now
1490 PixelData::Iterator p(data);
1491
1492 const int w = data.GetWidth();
1493 const int h = data.GetHeight();
1494
1495 for ( int y = 0; y < h; y++ )
1496 {
1497 PixelData::Iterator rowStart = p;
1498
1499 for ( int x = 0; x < w; x++ )
1500 {
1501 const unsigned alpha = p.Alpha();
1502
1503 p.Red() = (p.Red() * alpha + 127) / 255;
1504 p.Blue() = (p.Blue() * alpha + 127) / 255;
1505 p.Green() = (p.Green() * alpha + 127) / 255;
1506
1507 ++p;
1508 }
1509
1510 p = rowStart;
1511 p.OffsetY(data, 1);
1512 }
1513 }
1514
1515 // if we're a DDB we need to convert DIB back to DDB now to make the
1516 // changes made via raw bitmap access effective
1517 if ( !GetBitmapData()->m_isDIB )
1518 {
1519 wxDIB *dib = GetBitmapData()->m_dib;
1520 GetBitmapData()->m_dib = NULL;
1521
1522 // TODO: convert
1523
1524 delete dib;
1525 }
1526 #endif // wxUSE_WXDIB
1527 }
1528 #endif // #ifdef wxHAVE_RAW_BITMAP
1529
1530 // ----------------------------------------------------------------------------
1531 // wxMask
1532 // ----------------------------------------------------------------------------
1533
1534 wxMask::wxMask()
1535 {
1536 m_maskBitmap = 0;
1537 }
1538
1539 // Construct a mask from a bitmap and a colour indicating
1540 // the transparent area
1541 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1542 {
1543 m_maskBitmap = 0;
1544 Create(bitmap, colour);
1545 }
1546
1547 // Construct a mask from a bitmap and a palette index indicating
1548 // the transparent area
1549 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1550 {
1551 m_maskBitmap = 0;
1552 Create(bitmap, paletteIndex);
1553 }
1554
1555 // Construct a mask from a mono bitmap (copies the bitmap).
1556 wxMask::wxMask(const wxBitmap& bitmap)
1557 {
1558 m_maskBitmap = 0;
1559 Create(bitmap);
1560 }
1561
1562 wxMask::~wxMask()
1563 {
1564 if ( m_maskBitmap )
1565 ::DeleteObject((HBITMAP) m_maskBitmap);
1566 }
1567
1568 // Create a mask from a mono bitmap (copies the bitmap).
1569 bool wxMask::Create(const wxBitmap& bitmap)
1570 {
1571 #ifndef __WXMICROWIN__
1572 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1573 _T("can't create mask from invalid or not monochrome bitmap") );
1574
1575 if ( m_maskBitmap )
1576 {
1577 ::DeleteObject((HBITMAP) m_maskBitmap);
1578 m_maskBitmap = 0;
1579 }
1580
1581 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1582 bitmap.GetWidth(),
1583 bitmap.GetHeight(),
1584 1, 1, 0
1585 );
1586 HDC srcDC = CreateCompatibleDC(0);
1587 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1588 HDC destDC = CreateCompatibleDC(0);
1589 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1590 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1591 SelectObject(srcDC, 0);
1592 DeleteDC(srcDC);
1593 SelectObject(destDC, 0);
1594 DeleteDC(destDC);
1595 return TRUE;
1596 #else
1597 return FALSE;
1598 #endif
1599 }
1600
1601 // Create a mask from a bitmap and a palette index indicating
1602 // the transparent area
1603 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1604 {
1605 if ( m_maskBitmap )
1606 {
1607 ::DeleteObject((HBITMAP) m_maskBitmap);
1608 m_maskBitmap = 0;
1609 }
1610
1611 #if wxUSE_PALETTE
1612 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1613 {
1614 unsigned char red, green, blue;
1615 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1616 {
1617 wxColour transparentColour(red, green, blue);
1618 return Create(bitmap, transparentColour);
1619 }
1620 }
1621 #endif // wxUSE_PALETTE
1622
1623 return FALSE;
1624 }
1625
1626 // Create a mask from a bitmap and a colour indicating
1627 // the transparent area
1628 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1629 {
1630 #ifndef __WXMICROWIN__
1631 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1632
1633 if ( m_maskBitmap )
1634 {
1635 ::DeleteObject((HBITMAP) m_maskBitmap);
1636 m_maskBitmap = 0;
1637 }
1638
1639 int width = bitmap.GetWidth(),
1640 height = bitmap.GetHeight();
1641
1642 // scan the bitmap for the transparent colour and set the corresponding
1643 // pixels in the mask to BLACK and the rest to WHITE
1644 COLORREF maskColour = wxColourToPalRGB(colour);
1645 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1646
1647 HDC srcDC = ::CreateCompatibleDC(NULL);
1648 HDC destDC = ::CreateCompatibleDC(NULL);
1649 if ( !srcDC || !destDC )
1650 {
1651 wxLogLastError(wxT("CreateCompatibleDC"));
1652 }
1653
1654 bool ok = TRUE;
1655
1656 // SelectObject() will fail
1657 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1658 _T("bitmap can't be selected in another DC") );
1659
1660 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1661 if ( !hbmpSrcOld )
1662 {
1663 wxLogLastError(wxT("SelectObject"));
1664
1665 ok = FALSE;
1666 }
1667
1668 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1669 if ( !hbmpDstOld )
1670 {
1671 wxLogLastError(wxT("SelectObject"));
1672
1673 ok = FALSE;
1674 }
1675
1676 if ( ok )
1677 {
1678 // this will create a monochrome bitmap with 0 points for the pixels
1679 // which have the same value as the background colour and 1 for the
1680 // others
1681 ::SetBkColor(srcDC, maskColour);
1682 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
1683 }
1684
1685 ::SelectObject(srcDC, hbmpSrcOld);
1686 ::DeleteDC(srcDC);
1687 ::SelectObject(destDC, hbmpDstOld);
1688 ::DeleteDC(destDC);
1689
1690 return ok;
1691 #else // __WXMICROWIN__
1692 return FALSE;
1693 #endif // __WXMICROWIN__/!__WXMICROWIN__
1694 }
1695
1696 // ----------------------------------------------------------------------------
1697 // wxBitmapHandler
1698 // ----------------------------------------------------------------------------
1699
1700 bool wxBitmapHandler::Create(wxGDIImage *image,
1701 void *data,
1702 long flags,
1703 int width, int height, int depth)
1704 {
1705 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1706
1707 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1708 }
1709
1710 bool wxBitmapHandler::Load(wxGDIImage *image,
1711 const wxString& name,
1712 long flags,
1713 int width, int height)
1714 {
1715 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1716
1717 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1718 }
1719
1720 bool wxBitmapHandler::Save(wxGDIImage *image,
1721 const wxString& name,
1722 int type)
1723 {
1724 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1725
1726 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1727 }
1728
1729 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1730 void *WXUNUSED(data),
1731 long WXUNUSED(type),
1732 int WXUNUSED(width),
1733 int WXUNUSED(height),
1734 int WXUNUSED(depth))
1735 {
1736 return FALSE;
1737 }
1738
1739 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1740 const wxString& WXUNUSED(name),
1741 long WXUNUSED(type),
1742 int WXUNUSED(desiredWidth),
1743 int WXUNUSED(desiredHeight))
1744 {
1745 return FALSE;
1746 }
1747
1748 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1749 const wxString& WXUNUSED(name),
1750 int WXUNUSED(type),
1751 const wxPalette *WXUNUSED(palette))
1752 {
1753 return FALSE;
1754 }
1755
1756 // ----------------------------------------------------------------------------
1757 // DIB functions
1758 // ----------------------------------------------------------------------------
1759
1760 #ifndef __WXMICROWIN__
1761 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1762 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1763 {
1764 unsigned long i, headerSize;
1765
1766 // Allocate space for a DIB header
1767 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1768 LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1769 LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1770
1771 GetPaletteEntries(hPal, 0, 256, lpPe);
1772
1773 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1774
1775 // Fill in the static parts of the DIB header
1776 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1777 lpDIBheader->bmiHeader.biWidth = xSize;
1778 lpDIBheader->bmiHeader.biHeight = ySize;
1779 lpDIBheader->bmiHeader.biPlanes = 1;
1780
1781 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1782 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1783 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1784 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1785 lpDIBheader->bmiHeader.biClrUsed = 256;
1786
1787
1788 // Initialize the DIB palette
1789 for (i = 0; i < 256; i++) {
1790 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1791 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1792 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1793 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1794 }
1795
1796 *lpDIBHeader = lpDIBheader;
1797
1798 return TRUE;
1799 }
1800
1801 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1802 {
1803 free(lpDIBHeader);
1804 }
1805 #endif
1806
1807 // ----------------------------------------------------------------------------
1808 // global helper functions implemented here
1809 // ----------------------------------------------------------------------------
1810
1811 // helper of wxBitmapToHICON/HCURSOR
1812 static
1813 HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1814 bool iconWanted,
1815 int hotSpotX,
1816 int hotSpotY)
1817 {
1818 if ( !bmp.Ok() )
1819 {
1820 // we can't create an icon/cursor form nothing
1821 return 0;
1822 }
1823
1824 wxMask *mask = bmp.GetMask();
1825 if ( !mask )
1826 {
1827 // we must have a mask for an icon, so even if it's probably incorrect,
1828 // do create it (grey is the "standard" transparent colour)
1829 mask = new wxMask(bmp, *wxLIGHT_GREY);
1830 }
1831
1832 ICONINFO iconInfo;
1833 wxZeroMemory(iconInfo);
1834 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1835 if ( !iconWanted )
1836 {
1837 iconInfo.xHotspot = hotSpotX;
1838 iconInfo.yHotspot = hotSpotY;
1839 }
1840
1841 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1842 iconInfo.hbmColor = GetHbitmapOf(bmp);
1843
1844 // black out the transparent area to preserve background colour, because
1845 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1846 // the mask to the dest rect.
1847 {
1848 MemoryHDC dcSrc, dcDst;
1849 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1850 selectBitmap(dcDst, iconInfo.hbmColor);
1851
1852 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1853 dcSrc, 0, 0, SRCAND) )
1854 {
1855 wxLogLastError(_T("BitBlt"));
1856 }
1857 }
1858
1859 HICON hicon = ::CreateIconIndirect(&iconInfo);
1860
1861 if ( !bmp.GetMask() )
1862 {
1863 // we created the mask, now delete it
1864 delete mask;
1865 }
1866
1867 // delete the inverted mask bitmap we created as well
1868 ::DeleteObject(iconInfo.hbmMask);
1869
1870 return hicon;
1871 }
1872
1873 HICON wxBitmapToHICON(const wxBitmap& bmp)
1874 {
1875 return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
1876 }
1877
1878 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1879 {
1880 return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
1881 }
1882
1883 HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1884 {
1885 #ifndef __WXMICROWIN__
1886 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1887
1888 // get width/height from the bitmap if not given
1889 if ( !w || !h )
1890 {
1891 BITMAP bm;
1892 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1893 w = bm.bmWidth;
1894 h = bm.bmHeight;
1895 }
1896
1897 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1898 HDC hdcDst = ::CreateCompatibleDC(NULL);
1899 if ( !hdcSrc || !hdcDst )
1900 {
1901 wxLogLastError(wxT("CreateCompatibleDC"));
1902 }
1903
1904 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1905 if ( !hbmpInvMask )
1906 {
1907 wxLogLastError(wxT("CreateBitmap"));
1908 }
1909
1910 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1911 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
1912 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1913 hdcSrc, 0, 0,
1914 NOTSRCCOPY) )
1915 {
1916 wxLogLastError(wxT("BitBlt"));
1917 }
1918
1919 // Deselect objects
1920 SelectObject(hdcSrc,srcTmp);
1921 SelectObject(hdcDst,dstTmp);
1922
1923 ::DeleteDC(hdcSrc);
1924 ::DeleteDC(hdcDst);
1925
1926 return hbmpInvMask;
1927 #else
1928 return 0;
1929 #endif
1930 }