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