]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
fixed spurious assert failure in wxMenuBar::Insert
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 !defined(__WXMICROWIN__)
47 #include "wx/msw/dib.h"
48 #endif
49
50 #include "wx/image.h"
51 #include "wx/xpmdecod.h"
52
53 // missing from mingw32 header
54 #ifndef CLR_INVALID
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
57
58 // ----------------------------------------------------------------------------
59 // macros
60 // ----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
63 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
64
65 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxBitmapRefData
73 // ----------------------------------------------------------------------------
74
75 wxBitmapRefData::wxBitmapRefData()
76 {
77 m_quality = 0;
78 m_selectedInto = NULL;
79 m_numColors = 0;
80 m_bitmapMask = NULL;
81 m_hBitmap = (WXHBITMAP) NULL;
82 }
83
84 void wxBitmapRefData::Free()
85 {
86 wxASSERT_MSG( !m_selectedInto,
87 wxT("deleting bitmap still selected into wxMemoryDC") );
88
89 if ( m_hBitmap)
90 {
91 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
92 {
93 wxLogLastError(wxT("DeleteObject(hbitmap)"));
94 }
95 }
96
97 delete m_bitmapMask;
98 m_bitmapMask = NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // wxBitmap creation
103 // ----------------------------------------------------------------------------
104
105 // this function should be called from all wxBitmap ctors
106 void wxBitmap::Init()
107 {
108 // m_refData = NULL; done in the base class ctor
109
110 if ( wxTheBitmapList )
111 wxTheBitmapList->AddBitmap(this);
112 }
113
114 #ifdef __WIN32__
115
116 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
117 {
118 #ifndef __WXMICROWIN__
119 // it may be either HICON or HCURSOR
120 HICON hicon = (HICON)icon.GetHandle();
121
122 ICONINFO iconInfo;
123 if ( !::GetIconInfo(hicon, &iconInfo) )
124 {
125 wxLogLastError(wxT("GetIconInfo"));
126
127 return FALSE;
128 }
129
130 wxBitmapRefData *refData = new wxBitmapRefData;
131 m_refData = refData;
132
133 int w = icon.GetWidth(),
134 h = icon.GetHeight();
135
136 refData->m_width = w;
137 refData->m_height = h;
138 refData->m_depth = wxDisplayDepth();
139
140 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
141
142 // the mask returned by GetIconInfo() is inversed compared to the usual
143 // wxWin convention
144 refData->m_bitmapMask = new wxMask((WXHBITMAP)
145 wxInvertMask(iconInfo.hbmMask, w, h));
146
147 #if WXWIN_COMPATIBILITY_2
148 refData->m_ok = TRUE;
149 #endif // WXWIN_COMPATIBILITY_2
150
151 return TRUE;
152 #else
153 return FALSE;
154 #endif
155 }
156
157 #endif // Win32
158
159 bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
160 {
161 UnRef();
162
163 if ( !cursor.Ok() )
164 return FALSE;
165
166 #ifdef __WIN16__
167 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
168
169 return FALSE;
170 #else
171 return CopyFromIconOrCursor(cursor);
172 #endif // Win16
173 }
174
175 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
176 {
177 UnRef();
178
179 if ( !icon.Ok() )
180 return FALSE;
181
182 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
183 // to create a bitmap from icon there - but using this way we won't have
184 // the mask (FIXME)
185 #ifdef __WIN16__
186 int width = icon.GetWidth(),
187 height = icon.GetHeight();
188
189 // copy the icon to the bitmap
190 ScreenHDC hdcScreen;
191 HDC hdc = ::CreateCompatibleDC(hdcScreen);
192 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
193 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
194
195 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
196
197 ::SelectObject(hdc, hbmpOld);
198 ::DeleteDC(hdc);
199
200 wxBitmapRefData *refData = new wxBitmapRefData;
201 m_refData = refData;
202
203 refData->m_width = width;
204 refData->m_height = height;
205 refData->m_depth = wxDisplayDepth();
206
207 refData->m_hBitmap = (WXHBITMAP)hbitmap;
208
209 #if WXWIN_COMPATIBILITY_2
210 refData->m_ok = TRUE;
211 #endif // WXWIN_COMPATIBILITY_2
212
213 return TRUE;
214 #else // Win32
215 return CopyFromIconOrCursor(icon);
216 #endif // Win16/Win32
217 }
218
219 wxBitmap::~wxBitmap()
220 {
221 if (wxTheBitmapList)
222 wxTheBitmapList->DeleteObject(this);
223 }
224
225 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
226 {
227 Init();
228
229 #ifndef __WXMICROWIN__
230 wxBitmapRefData *refData = new wxBitmapRefData;
231 m_refData = refData;
232
233 refData->m_width = width;
234 refData->m_height = height;
235 refData->m_depth = depth;
236 refData->m_numColors = 0;
237 refData->m_selectedInto = NULL;
238
239 char *data;
240 if ( depth == 1 )
241 {
242 // we assume that it is in XBM format which is not quite the same as
243 // the format CreateBitmap() wants because the order of bytes in the
244 // line is inversed!
245 const size_t bytesPerLine = (width + 7) / 8;
246 const size_t padding = bytesPerLine % 2;
247 const size_t len = height * ( padding + bytesPerLine );
248 data = (char *)malloc(len);
249 const char *src = bits;
250 char *dst = data;
251
252 for ( int rows = 0; rows < height; rows++ )
253 {
254 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
255 {
256 unsigned char val = *src++;
257 unsigned char reversed = 0;
258
259 for ( int bits = 0; bits < 8; bits++)
260 {
261 reversed <<= 1;
262 reversed |= (val & 0x01);
263 val >>= 1;
264 }
265 *dst++ = reversed;
266 }
267
268 if ( padding )
269 *dst++ = 0;
270 }
271 }
272 else
273 {
274 // bits should already be in Windows standard format
275 data = (char *)bits; // const_cast is harmless
276 }
277
278 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
279 if ( !hbmp )
280 {
281 wxLogLastError(wxT("CreateBitmap"));
282 }
283
284 if ( data != bits )
285 {
286 free(data);
287 }
288
289 SetHBITMAP((WXHBITMAP)hbmp);
290 #endif
291 }
292
293 // Create from XPM data
294 bool wxBitmap::CreateFromXpm(const char **data)
295 {
296 #if wxUSE_IMAGE && wxUSE_XPM
297 Init();
298
299 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
300
301 wxXPMDecoder decoder;
302 wxImage img = decoder.ReadData(data);
303 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
304
305 *this = wxBitmap(img);
306 return TRUE;
307 #else
308 return FALSE;
309 #endif
310 }
311
312 wxBitmap::wxBitmap(int w, int h, int d)
313 {
314 Init();
315
316 (void)Create(w, h, d);
317 }
318
319 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
320 {
321 Init();
322
323 (void)Create(data, type, width, height, depth);
324 }
325
326 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
327 {
328 Init();
329
330 LoadFile(filename, (int)type);
331 }
332
333 bool wxBitmap::Create(int w, int h, int d)
334 {
335 #ifndef __WXMICROWIN__
336 UnRef();
337
338 m_refData = new wxBitmapRefData;
339
340 GetBitmapData()->m_width = w;
341 GetBitmapData()->m_height = h;
342 GetBitmapData()->m_depth = d;
343
344 HBITMAP hbmp;
345
346 if ( d > 0 )
347 {
348 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
349 if ( !hbmp )
350 {
351 wxLogLastError(wxT("CreateBitmap"));
352 }
353 }
354 else
355 {
356 ScreenHDC dc;
357 hbmp = ::CreateCompatibleBitmap(dc, w, h);
358 if ( !hbmp )
359 {
360 wxLogLastError(wxT("CreateCompatibleBitmap"));
361 }
362
363 GetBitmapData()->m_depth = wxDisplayDepth();
364 }
365
366 SetHBITMAP((WXHBITMAP)hbmp);
367
368 #if WXWIN_COMPATIBILITY_2
369 GetBitmapData()->m_ok = hbmp != 0;
370 #endif // WXWIN_COMPATIBILITY_2
371 return Ok();
372 #else
373 return FALSE;
374 #endif
375 }
376
377 // ----------------------------------------------------------------------------
378 // wxImage to/from conversions
379 // ----------------------------------------------------------------------------
380
381 #if wxUSE_IMAGE
382
383 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
384 {
385 #ifdef __WXMICROWIN__
386 // TODO
387 return FALSE;
388 #else
389 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
390
391 m_refData = new wxBitmapRefData();
392
393 // sizeLimit is the MS upper limit for the DIB size
394 #ifdef WIN32
395 int sizeLimit = 1024*768*3;
396 #else
397 int sizeLimit = 0x7fff ;
398 #endif
399
400 // width and height of the device-dependent bitmap
401 int width = image.GetWidth();
402 int bmpHeight = image.GetHeight();
403
404 // calc the number of bytes per scanline and padding
405 int bytePerLine = width*3;
406 int sizeDWORD = sizeof( DWORD );
407 int lineBoundary = bytePerLine % sizeDWORD;
408 int padding = 0;
409 if( lineBoundary > 0 )
410 {
411 padding = sizeDWORD - lineBoundary;
412 bytePerLine += padding;
413 }
414 // calc the number of DIBs and heights of DIBs
415 int numDIB = 1;
416 int hRemain = 0;
417 int height = sizeLimit/bytePerLine;
418 if( height >= bmpHeight )
419 height = bmpHeight;
420 else
421 {
422 numDIB = bmpHeight / height;
423 hRemain = bmpHeight % height;
424 if( hRemain >0 ) numDIB++;
425 }
426
427 // set bitmap parameters
428 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
429 SetWidth( width );
430 SetHeight( bmpHeight );
431 if (depth == -1) depth = wxDisplayDepth();
432 SetDepth( depth );
433
434 // create a DIB header
435 int headersize = sizeof(BITMAPINFOHEADER);
436 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
437 wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
438 // Fill in the DIB header
439 lpDIBh->bmiHeader.biSize = headersize;
440 lpDIBh->bmiHeader.biWidth = (DWORD)width;
441 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
442 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
443 // the general formula for biSizeImage:
444 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
445 lpDIBh->bmiHeader.biPlanes = 1;
446 lpDIBh->bmiHeader.biBitCount = 24;
447 lpDIBh->bmiHeader.biCompression = BI_RGB;
448 lpDIBh->bmiHeader.biClrUsed = 0;
449 // These seem not really needed for our purpose here.
450 lpDIBh->bmiHeader.biClrImportant = 0;
451 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
452 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
453 // memory for DIB data
454 unsigned char *lpBits;
455 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
456 if( !lpBits )
457 {
458 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
459 free( lpDIBh );
460 return FALSE;
461 }
462
463 // create and set the device-dependent bitmap
464 HDC hdc = ::GetDC(NULL);
465 HDC memdc = ::CreateCompatibleDC( hdc );
466 HBITMAP hbitmap;
467 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
468 ::SelectObject( memdc, hbitmap);
469
470 #if wxUSE_PALETTE
471 HPALETTE hOldPalette = 0;
472 if (image.GetPalette().Ok())
473 {
474 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
475 ::RealizePalette(memdc);
476 }
477 #endif // wxUSE_PALETTE
478
479 // copy image data into DIB data and then into DDB (in a loop)
480 unsigned char *data = image.GetData();
481 int i, j, n;
482 int origin = 0;
483 unsigned char *ptdata = data;
484 unsigned char *ptbits;
485
486 for( n=0; n<numDIB; n++ )
487 {
488 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
489 {
490 // redefine height and size of the (possibly) last smaller DIB
491 // memory is not reallocated
492 height = hRemain;
493 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
494 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
495 }
496 ptbits = lpBits;
497
498 for( j=0; j<height; j++ )
499 {
500 for( i=0; i<width; i++ )
501 {
502 *(ptbits++) = *(ptdata+2);
503 *(ptbits++) = *(ptdata+1);
504 *(ptbits++) = *(ptdata );
505 ptdata += 3;
506 }
507 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
508 }
509 ::StretchDIBits( memdc, 0, origin, width, height,\
510 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
511 origin += height;
512 // if numDIB = 1, lines below can also be used
513 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
514 // The above line is equivalent to the following two lines.
515 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
516 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
517 // or the following lines
518 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
519 // HDC memdc = ::CreateCompatibleDC( hdc );
520 // ::SelectObject( memdc, hbitmap);
521 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
522 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
523 // ::SelectObject( memdc, 0 );
524 // ::DeleteDC( memdc );
525 }
526 SetHBITMAP( (WXHBITMAP) hbitmap );
527
528 #if wxUSE_PALETTE
529 if (hOldPalette)
530 SelectPalette(memdc, hOldPalette, FALSE);
531 #endif // wxUSE_PALETTE
532
533 // similarly, created an mono-bitmap for the possible mask
534 if( image.HasMask() )
535 {
536 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
537 HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
538 if( numDIB == 1 ) height = bmpHeight;
539 else height = sizeLimit/bytePerLine;
540 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
541 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
542 origin = 0;
543 unsigned char r = image.GetMaskRed();
544 unsigned char g = image.GetMaskGreen();
545 unsigned char b = image.GetMaskBlue();
546 unsigned char zero = 0, one = 255;
547 ptdata = data;
548 for( n=0; n<numDIB; n++ )
549 {
550 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
551 {
552 // redefine height and size of the (possibly) last smaller DIB
553 // memory is not reallocated
554 height = hRemain;
555 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
556 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
557 }
558 ptbits = lpBits;
559 for( int j=0; j<height; j++ )
560 {
561 for(i=0; i<width; i++ )
562 {
563 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
564 unsigned char cr = (*(ptdata++)) ;
565 unsigned char cg = (*(ptdata++)) ;
566 unsigned char cb = (*(ptdata++)) ;
567
568 if( ( cr !=r) || (cg!=g) || (cb!=b) )
569 {
570 *(ptbits++) = one;
571 *(ptbits++) = one;
572 *(ptbits++) = one;
573 }
574 else
575 {
576 *(ptbits++) = zero;
577 *(ptbits++) = zero;
578 *(ptbits++) = zero;
579 }
580 }
581 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
582 }
583 ::StretchDIBits( memdc, 0, origin, width, height,\
584 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
585 origin += height;
586 }
587 // create a wxMask object
588 wxMask *mask = new wxMask();
589 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
590 SetMask( mask );
591 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
592 /* The following can also be used but is slow to run
593 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
594 wxMask *mask = new wxMask( *this, colour );
595 SetMask( mask );
596 */
597
598 ::SelectObject( memdc, hbmpOld );
599 }
600
601 // free allocated resources
602 ::DeleteDC( memdc );
603 ::ReleaseDC(NULL, hdc);
604 free(lpDIBh);
605 free(lpBits);
606
607 #if WXWIN_COMPATIBILITY_2
608 // check the wxBitmap object
609 GetBitmapData()->SetOk();
610 #endif // WXWIN_COMPATIBILITY_2
611
612 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
613
614 return TRUE;
615 #endif
616 }
617
618 wxImage wxBitmap::ConvertToImage() const
619 {
620 #ifdef __WXMICROWIN__
621 // TODO
622 return wxImage();
623 #else
624 wxImage image;
625
626 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
627
628 // create an wxImage object
629 int width = GetWidth();
630 int height = GetHeight();
631 image.Create( width, height );
632 unsigned char *data = image.GetData();
633 if( !data )
634 {
635 wxFAIL_MSG( wxT("could not allocate data for image") );
636 return wxNullImage;
637 }
638
639 // calc the number of bytes per scanline and padding in the DIB
640 int bytePerLine = width*3;
641 int sizeDWORD = sizeof( DWORD );
642 int lineBoundary = bytePerLine % sizeDWORD;
643 int padding = 0;
644 if( lineBoundary > 0 )
645 {
646 padding = sizeDWORD - lineBoundary;
647 bytePerLine += padding;
648 }
649
650 // create a DIB header
651 int headersize = sizeof(BITMAPINFOHEADER);
652 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
653 if( !lpDIBh )
654 {
655 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
656 free( data );
657 return wxNullImage;
658 }
659 // Fill in the DIB header
660 lpDIBh->bmiHeader.biSize = headersize;
661 lpDIBh->bmiHeader.biWidth = width;
662 lpDIBh->bmiHeader.biHeight = -height;
663 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
664 lpDIBh->bmiHeader.biPlanes = 1;
665 lpDIBh->bmiHeader.biBitCount = 24;
666 lpDIBh->bmiHeader.biCompression = BI_RGB;
667 lpDIBh->bmiHeader.biClrUsed = 0;
668 // These seem not really needed for our purpose here.
669 lpDIBh->bmiHeader.biClrImportant = 0;
670 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
671 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
672 // memory for DIB data
673 unsigned char *lpBits;
674 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
675 if( !lpBits )
676 {
677 wxFAIL_MSG( wxT("could not allocate data for DIB") );
678 free( data );
679 free( lpDIBh );
680 return wxNullImage;
681 }
682
683 // copy data from the device-dependent bitmap to the DIB
684 HDC hdc = ::GetDC(NULL);
685 HBITMAP hbitmap;
686 hbitmap = (HBITMAP) GetHBITMAP();
687 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
688
689 // copy DIB data into the wxImage object
690 int i, j;
691 unsigned char *ptdata = data;
692 unsigned char *ptbits = lpBits;
693 for( i=0; i<height; i++ )
694 {
695 for( j=0; j<width; j++ )
696 {
697 *(ptdata++) = *(ptbits+2);
698 *(ptdata++) = *(ptbits+1);
699 *(ptdata++) = *(ptbits );
700 ptbits += 3;
701 }
702 ptbits += padding;
703 }
704
705 // similarly, set data according to the possible mask bitmap
706 if( GetMask() && GetMask()->GetMaskBitmap() )
707 {
708 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
709 // memory DC created, color set, data copied, and memory DC deleted
710 HDC memdc = ::CreateCompatibleDC( hdc );
711 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
712 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
713 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
714 ::DeleteDC( memdc );
715 // background color set to RGB(16,16,16) in consistent with wxGTK
716 unsigned char r=16, g=16, b=16;
717 ptdata = data;
718 ptbits = lpBits;
719 for( i=0; i<height; i++ )
720 {
721 for( j=0; j<width; j++ )
722 {
723 if( *ptbits != 0 )
724 ptdata += 3;
725 else
726 {
727 *(ptdata++) = r;
728 *(ptdata++) = g;
729 *(ptdata++) = b;
730 }
731 ptbits += 3;
732 }
733 ptbits += padding;
734 }
735 image.SetMaskColour( r, g, b );
736 image.SetMask( TRUE );
737 }
738 else
739 {
740 image.SetMask( FALSE );
741 }
742 // free allocated resources
743 ::ReleaseDC(NULL, hdc);
744 free(lpDIBh);
745 free(lpBits);
746
747 return image;
748 #endif
749 }
750
751 #endif // wxUSE_IMAGE
752
753 bool wxBitmap::LoadFile(const wxString& filename, long type)
754 {
755 UnRef();
756
757 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
758
759 if ( handler )
760 {
761 m_refData = new wxBitmapRefData;
762
763 return handler->LoadFile(this, filename, type, -1, -1);
764 }
765 #if wxUSE_IMAGE
766 else
767 {
768 wxImage image;
769 if ( image.LoadFile( filename, type ) && image.Ok() )
770 {
771 *this = image.ConvertToBitmap();
772
773 return TRUE;
774 }
775 }
776 #endif // wxUSE_IMAGE
777
778 return FALSE;
779 }
780
781 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
782 {
783 UnRef();
784
785 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
786
787 if ( !handler )
788 {
789 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
790
791 return FALSE;
792 }
793
794 m_refData = new wxBitmapRefData;
795
796 return handler->Create(this, data, type, width, height, depth);
797 }
798
799 bool wxBitmap::SaveFile(const wxString& filename,
800 int type,
801 const wxPalette *palette)
802 {
803 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
804
805 if ( handler )
806 {
807 return handler->SaveFile(this, filename, type, palette);
808 }
809 #if wxUSE_IMAGE
810 else
811 {
812 // FIXME what about palette? shouldn't we use it?
813 wxImage image( *this );
814 if ( image.Ok() )
815 {
816 return image.SaveFile(filename, type);
817 }
818 }
819 #endif // wxUSE_IMAGE
820
821 return FALSE;
822 }
823
824 // ----------------------------------------------------------------------------
825 // sub bitmap extraction
826 // ----------------------------------------------------------------------------
827
828 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
829 {
830 #ifndef __WXMICROWIN__
831 wxCHECK_MSG( Ok() &&
832 (rect.x >= 0) && (rect.y >= 0) &&
833 (rect.x+rect.width <= GetWidth()) &&
834 (rect.y+rect.height <= GetHeight()),
835 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
836
837 wxBitmap ret( rect.width, rect.height, GetDepth() );
838 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
839
840 // copy bitmap data
841 HDC dcSrc = ::CreateCompatibleDC(NULL);
842 HDC dcDst = ::CreateCompatibleDC(NULL);
843 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
844 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
845 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
846
847 // copy mask if there is one
848 if (GetMask())
849 {
850 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
851
852 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
853 SelectObject(dcDst, (HBITMAP) hbmpMask);
854 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
855
856 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
857 ret.SetMask(mask);
858 }
859
860 SelectObject(dcDst, NULL);
861 SelectObject(dcSrc, NULL);
862 DeleteDC(dcDst);
863 DeleteDC(dcSrc);
864
865 return ret;
866 #else
867 return wxBitmap();
868 #endif
869 }
870
871 // ----------------------------------------------------------------------------
872 // wxBitmap accessors
873 // ----------------------------------------------------------------------------
874
875 void wxBitmap::SetQuality(int q)
876 {
877 EnsureHasData();
878
879 GetBitmapData()->m_quality = q;
880 }
881
882 #if WXWIN_COMPATIBILITY_2
883 void wxBitmap::SetOk(bool isOk)
884 {
885 EnsureHasData();
886
887 GetBitmapData()->m_ok = isOk;
888 }
889 #endif // WXWIN_COMPATIBILITY_2
890
891 #if wxUSE_PALETTE
892
893 void wxBitmap::SetPalette(const wxPalette& palette)
894 {
895 EnsureHasData();
896
897 GetBitmapData()->m_bitmapPalette = palette;
898 }
899
900 #endif // wxUSE_PALETTE
901
902 void wxBitmap::SetMask(wxMask *mask)
903 {
904 EnsureHasData();
905
906 GetBitmapData()->m_bitmapMask = mask;
907 }
908
909 // Creates a bitmap that matches the device context, from
910 // an arbitray bitmap. At present, the original bitmap must have an
911 // associated palette. TODO: use a default palette if no palette exists.
912 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
913 wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
914 {
915 #ifdef __WXMICROWIN__
916 return wxBitmap();
917 #else
918 wxMemoryDC memDC;
919 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
920 HPALETTE hPal = (HPALETTE) NULL;
921 LPBITMAPINFO lpDib;
922 void *lpBits = (void*) NULL;
923
924 #if wxUSE_PALETTE
925 if( GetPalette() && GetPalette()->Ok() )
926 {
927 tmpBitmap.SetPalette(*GetPalette());
928 memDC.SelectObject(tmpBitmap);
929 memDC.SetPalette(*GetPalette());
930 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
931 }
932 else
933 {
934 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
935 wxPalette palette;
936 palette.SetHPALETTE( (WXHPALETTE)hPal );
937 tmpBitmap.SetPalette( palette );
938 memDC.SelectObject(tmpBitmap);
939 memDC.SetPalette( palette );
940 }
941 #else // !wxUSE_PALETTE
942 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
943 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
944
945 // set the height negative because in a DIB the order of the lines is
946 // reversed
947 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
948 {
949 return wxNullBitmap;
950 }
951
952 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
953
954 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
955
956 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
957 GetWidth(), GetHeight(),
958 0, 0, 0, GetHeight(),
959 lpBits, lpDib, DIB_RGB_COLORS);
960
961 free(lpBits);
962
963 wxFreeDIB(lpDib);
964
965 return tmpBitmap;
966 #endif
967 }
968
969 // ----------------------------------------------------------------------------
970 // wxMask
971 // ----------------------------------------------------------------------------
972
973 wxMask::wxMask()
974 {
975 m_maskBitmap = 0;
976 }
977
978 // Construct a mask from a bitmap and a colour indicating
979 // the transparent area
980 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
981 {
982 m_maskBitmap = 0;
983 Create(bitmap, colour);
984 }
985
986 // Construct a mask from a bitmap and a palette index indicating
987 // the transparent area
988 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
989 {
990 m_maskBitmap = 0;
991 Create(bitmap, paletteIndex);
992 }
993
994 // Construct a mask from a mono bitmap (copies the bitmap).
995 wxMask::wxMask(const wxBitmap& bitmap)
996 {
997 m_maskBitmap = 0;
998 Create(bitmap);
999 }
1000
1001 wxMask::~wxMask()
1002 {
1003 if ( m_maskBitmap )
1004 ::DeleteObject((HBITMAP) m_maskBitmap);
1005 }
1006
1007 // Create a mask from a mono bitmap (copies the bitmap).
1008 bool wxMask::Create(const wxBitmap& bitmap)
1009 {
1010 #ifndef __WXMICROWIN__
1011 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1012 _T("can't create mask from invalid or not monochrome bitmap") );
1013
1014 if ( m_maskBitmap )
1015 {
1016 ::DeleteObject((HBITMAP) m_maskBitmap);
1017 m_maskBitmap = 0;
1018 }
1019
1020 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1021 bitmap.GetWidth(),
1022 bitmap.GetHeight(),
1023 1, 1, 0
1024 );
1025 HDC srcDC = CreateCompatibleDC(0);
1026 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1027 HDC destDC = CreateCompatibleDC(0);
1028 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1029 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1030 SelectObject(srcDC, 0);
1031 DeleteDC(srcDC);
1032 SelectObject(destDC, 0);
1033 DeleteDC(destDC);
1034 return TRUE;
1035 #else
1036 return FALSE;
1037 #endif
1038 }
1039
1040 // Create a mask from a bitmap and a palette index indicating
1041 // the transparent area
1042 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1043 {
1044 if ( m_maskBitmap )
1045 {
1046 ::DeleteObject((HBITMAP) m_maskBitmap);
1047 m_maskBitmap = 0;
1048 }
1049
1050 #if wxUSE_PALETTE
1051 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1052 {
1053 unsigned char red, green, blue;
1054 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1055 {
1056 wxColour transparentColour(red, green, blue);
1057 return Create(bitmap, transparentColour);
1058 }
1059 }
1060 #endif // wxUSE_PALETTE
1061
1062 return FALSE;
1063 }
1064
1065 // Create a mask from a bitmap and a colour indicating
1066 // the transparent area
1067 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1068 {
1069 #ifndef __WXMICROWIN__
1070 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1071
1072 if ( m_maskBitmap )
1073 {
1074 ::DeleteObject((HBITMAP) m_maskBitmap);
1075 m_maskBitmap = 0;
1076 }
1077
1078 int width = bitmap.GetWidth(),
1079 height = bitmap.GetHeight();
1080
1081 // scan the bitmap for the transparent colour and set the corresponding
1082 // pixels in the mask to BLACK and the rest to WHITE
1083 COLORREF maskColour = wxColourToRGB(colour);
1084 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1085
1086 HDC srcDC = ::CreateCompatibleDC(NULL);
1087 HDC destDC = ::CreateCompatibleDC(NULL);
1088 if ( !srcDC || !destDC )
1089 {
1090 wxLogLastError(wxT("CreateCompatibleDC"));
1091 }
1092
1093 bool ok = TRUE;
1094
1095 // SelectObject() will fail
1096 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1097 _T("bitmap can't be selected in another DC") );
1098
1099 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1100 if ( !hbmpSrcOld )
1101 {
1102 wxLogLastError(wxT("SelectObject"));
1103
1104 ok = FALSE;
1105 }
1106
1107 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1108 if ( !hbmpDstOld )
1109 {
1110 wxLogLastError(wxT("SelectObject"));
1111
1112 ok = FALSE;
1113 }
1114
1115 // this is not very efficient, but I can't think of a better way of doing
1116 // it
1117 for ( int w = 0; ok && (w < width); w++ )
1118 {
1119 for ( int h = 0; ok && (h < height); h++ )
1120 {
1121 COLORREF col = GetPixel(srcDC, w, h);
1122 if ( col == CLR_INVALID )
1123 {
1124 wxLogLastError(wxT("GetPixel"));
1125
1126 // doesn't make sense to continue
1127 ok = FALSE;
1128
1129 break;
1130 }
1131
1132 if ( col == maskColour )
1133 {
1134 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
1135 }
1136 else
1137 {
1138 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
1139 }
1140 }
1141 }
1142
1143 ::SelectObject(srcDC, hbmpSrcOld);
1144 ::DeleteDC(srcDC);
1145 ::SelectObject(destDC, hbmpDstOld);
1146 ::DeleteDC(destDC);
1147
1148 return ok;
1149 #else
1150 return FALSE;
1151 #endif
1152 }
1153
1154 // ----------------------------------------------------------------------------
1155 // wxBitmapHandler
1156 // ----------------------------------------------------------------------------
1157
1158 bool wxBitmapHandler::Create(wxGDIImage *image,
1159 void *data,
1160 long flags,
1161 int width, int height, int depth)
1162 {
1163 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1164
1165 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1166 }
1167
1168 bool wxBitmapHandler::Load(wxGDIImage *image,
1169 const wxString& name,
1170 long flags,
1171 int width, int height)
1172 {
1173 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1174
1175 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1176 }
1177
1178 bool wxBitmapHandler::Save(wxGDIImage *image,
1179 const wxString& name,
1180 int type)
1181 {
1182 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1183
1184 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1185 }
1186
1187 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1188 void *WXUNUSED(data),
1189 long WXUNUSED(type),
1190 int WXUNUSED(width),
1191 int WXUNUSED(height),
1192 int WXUNUSED(depth))
1193 {
1194 return FALSE;
1195 }
1196
1197 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1198 const wxString& WXUNUSED(name),
1199 long WXUNUSED(type),
1200 int WXUNUSED(desiredWidth),
1201 int WXUNUSED(desiredHeight))
1202 {
1203 return FALSE;
1204 }
1205
1206 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1207 const wxString& WXUNUSED(name),
1208 int WXUNUSED(type),
1209 const wxPalette *WXUNUSED(palette))
1210 {
1211 return FALSE;
1212 }
1213
1214 // ----------------------------------------------------------------------------
1215 // DIB functions
1216 // ----------------------------------------------------------------------------
1217
1218 #ifndef __WXMICROWIN__
1219 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1220 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1221 {
1222 unsigned long i, headerSize;
1223 LPBITMAPINFO lpDIBheader = NULL;
1224 LPPALETTEENTRY lpPe = NULL;
1225
1226
1227 // Allocate space for a DIB header
1228 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1229 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1230 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1231
1232 GetPaletteEntries(hPal, 0, 256, lpPe);
1233
1234 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1235
1236 // Fill in the static parts of the DIB header
1237 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1238 lpDIBheader->bmiHeader.biWidth = xSize;
1239 lpDIBheader->bmiHeader.biHeight = ySize;
1240 lpDIBheader->bmiHeader.biPlanes = 1;
1241
1242 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1243 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1244 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1245 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1246 lpDIBheader->bmiHeader.biClrUsed = 256;
1247
1248
1249 // Initialize the DIB palette
1250 for (i = 0; i < 256; i++) {
1251 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1252 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1253 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1254 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1255 }
1256
1257 *lpDIBHeader = lpDIBheader;
1258
1259 return TRUE;
1260 }
1261
1262 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1263 {
1264 free(lpDIBHeader);
1265 }
1266 #endif
1267
1268 // ----------------------------------------------------------------------------
1269 // other helper functions
1270 // ----------------------------------------------------------------------------
1271
1272 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1273 {
1274 #ifndef __WXMICROWIN__
1275 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1276
1277 // get width/height from the bitmap if not given
1278 if ( !w || !h )
1279 {
1280 BITMAP bm;
1281 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1282 w = bm.bmWidth;
1283 h = bm.bmHeight;
1284 }
1285
1286 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1287 HDC hdcDst = ::CreateCompatibleDC(NULL);
1288 if ( !hdcSrc || !hdcDst )
1289 {
1290 wxLogLastError(wxT("CreateCompatibleDC"));
1291 }
1292
1293 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1294 if ( !hbmpInvMask )
1295 {
1296 wxLogLastError(wxT("CreateBitmap"));
1297 }
1298
1299 ::SelectObject(hdcSrc, hbmpMask);
1300 ::SelectObject(hdcDst, hbmpInvMask);
1301 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1302 hdcSrc, 0, 0,
1303 NOTSRCCOPY) )
1304 {
1305 wxLogLastError(wxT("BitBlt"));
1306 }
1307
1308 ::DeleteDC(hdcSrc);
1309 ::DeleteDC(hdcDst);
1310
1311 return hbmpInvMask;
1312 #else
1313 return 0;
1314 #endif
1315 }