]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
Fixed compilation error
[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 HPALETTE hOldPalette = 0;
471 if (image.GetPalette().Ok())
472 {
473 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
474 ::RealizePalette(memdc);
475 }
476
477 // copy image data into DIB data and then into DDB (in a loop)
478 unsigned char *data = image.GetData();
479 int i, j, n;
480 int origin = 0;
481 unsigned char *ptdata = data;
482 unsigned char *ptbits;
483
484 for( n=0; n<numDIB; n++ )
485 {
486 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
487 {
488 // redefine height and size of the (possibly) last smaller DIB
489 // memory is not reallocated
490 height = hRemain;
491 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
492 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
493 }
494 ptbits = lpBits;
495
496 for( j=0; j<height; j++ )
497 {
498 for( i=0; i<width; i++ )
499 {
500 *(ptbits++) = *(ptdata+2);
501 *(ptbits++) = *(ptdata+1);
502 *(ptbits++) = *(ptdata );
503 ptdata += 3;
504 }
505 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
506 }
507 ::StretchDIBits( memdc, 0, origin, width, height,\
508 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
509 origin += height;
510 // if numDIB = 1, lines below can also be used
511 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
512 // The above line is equivalent to the following two lines.
513 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
514 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
515 // or the following lines
516 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
517 // HDC memdc = ::CreateCompatibleDC( hdc );
518 // ::SelectObject( memdc, hbitmap);
519 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
520 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
521 // ::SelectObject( memdc, 0 );
522 // ::DeleteDC( memdc );
523 }
524 SetHBITMAP( (WXHBITMAP) hbitmap );
525
526 if (hOldPalette)
527 SelectPalette(memdc, hOldPalette, FALSE);
528
529 // similarly, created an mono-bitmap for the possible mask
530 if( image.HasMask() )
531 {
532 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
533 HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
534 if( numDIB == 1 ) height = bmpHeight;
535 else height = sizeLimit/bytePerLine;
536 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
537 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
538 origin = 0;
539 unsigned char r = image.GetMaskRed();
540 unsigned char g = image.GetMaskGreen();
541 unsigned char b = image.GetMaskBlue();
542 unsigned char zero = 0, one = 255;
543 ptdata = data;
544 for( n=0; n<numDIB; n++ )
545 {
546 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
547 {
548 // redefine height and size of the (possibly) last smaller DIB
549 // memory is not reallocated
550 height = hRemain;
551 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
552 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
553 }
554 ptbits = lpBits;
555 for( int j=0; j<height; j++ )
556 {
557 for(i=0; i<width; i++ )
558 {
559 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
560 unsigned char cr = (*(ptdata++)) ;
561 unsigned char cg = (*(ptdata++)) ;
562 unsigned char cb = (*(ptdata++)) ;
563
564 if( ( cr !=r) || (cg!=g) || (cb!=b) )
565 {
566 *(ptbits++) = one;
567 *(ptbits++) = one;
568 *(ptbits++) = one;
569 }
570 else
571 {
572 *(ptbits++) = zero;
573 *(ptbits++) = zero;
574 *(ptbits++) = zero;
575 }
576 }
577 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
578 }
579 ::StretchDIBits( memdc, 0, origin, width, height,\
580 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
581 origin += height;
582 }
583 // create a wxMask object
584 wxMask *mask = new wxMask();
585 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
586 SetMask( mask );
587 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
588 /* The following can also be used but is slow to run
589 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
590 wxMask *mask = new wxMask( *this, colour );
591 SetMask( mask );
592 */
593
594 ::SelectObject( memdc, hbmpOld );
595 }
596
597 // free allocated resources
598 ::DeleteDC( memdc );
599 ::ReleaseDC(NULL, hdc);
600 free(lpDIBh);
601 free(lpBits);
602
603 #if WXWIN_COMPATIBILITY_2
604 // check the wxBitmap object
605 GetBitmapData()->SetOk();
606 #endif // WXWIN_COMPATIBILITY_2
607
608 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
609
610 return TRUE;
611 #endif
612 }
613
614 wxImage wxBitmap::ConvertToImage() const
615 {
616 #ifdef __WXMICROWIN__
617 // TODO
618 return wxImage();
619 #else
620 wxImage image;
621
622 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
623
624 // create an wxImage object
625 int width = GetWidth();
626 int height = GetHeight();
627 image.Create( width, height );
628 unsigned char *data = image.GetData();
629 if( !data )
630 {
631 wxFAIL_MSG( wxT("could not allocate data for image") );
632 return wxNullImage;
633 }
634
635 // calc the number of bytes per scanline and padding in the DIB
636 int bytePerLine = width*3;
637 int sizeDWORD = sizeof( DWORD );
638 int lineBoundary = bytePerLine % sizeDWORD;
639 int padding = 0;
640 if( lineBoundary > 0 )
641 {
642 padding = sizeDWORD - lineBoundary;
643 bytePerLine += padding;
644 }
645
646 // create a DIB header
647 int headersize = sizeof(BITMAPINFOHEADER);
648 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
649 if( !lpDIBh )
650 {
651 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
652 free( data );
653 return wxNullImage;
654 }
655 // Fill in the DIB header
656 lpDIBh->bmiHeader.biSize = headersize;
657 lpDIBh->bmiHeader.biWidth = width;
658 lpDIBh->bmiHeader.biHeight = -height;
659 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
660 lpDIBh->bmiHeader.biPlanes = 1;
661 lpDIBh->bmiHeader.biBitCount = 24;
662 lpDIBh->bmiHeader.biCompression = BI_RGB;
663 lpDIBh->bmiHeader.biClrUsed = 0;
664 // These seem not really needed for our purpose here.
665 lpDIBh->bmiHeader.biClrImportant = 0;
666 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
667 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
668 // memory for DIB data
669 unsigned char *lpBits;
670 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
671 if( !lpBits )
672 {
673 wxFAIL_MSG( wxT("could not allocate data for DIB") );
674 free( data );
675 free( lpDIBh );
676 return wxNullImage;
677 }
678
679 // copy data from the device-dependent bitmap to the DIB
680 HDC hdc = ::GetDC(NULL);
681 HBITMAP hbitmap;
682 hbitmap = (HBITMAP) GetHBITMAP();
683 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
684
685 // copy DIB data into the wxImage object
686 int i, j;
687 unsigned char *ptdata = data;
688 unsigned char *ptbits = lpBits;
689 for( i=0; i<height; i++ )
690 {
691 for( j=0; j<width; j++ )
692 {
693 *(ptdata++) = *(ptbits+2);
694 *(ptdata++) = *(ptbits+1);
695 *(ptdata++) = *(ptbits );
696 ptbits += 3;
697 }
698 ptbits += padding;
699 }
700
701 // similarly, set data according to the possible mask bitmap
702 if( GetMask() && GetMask()->GetMaskBitmap() )
703 {
704 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
705 // memory DC created, color set, data copied, and memory DC deleted
706 HDC memdc = ::CreateCompatibleDC( hdc );
707 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
708 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
709 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
710 ::DeleteDC( memdc );
711 // background color set to RGB(16,16,16) in consistent with wxGTK
712 unsigned char r=16, g=16, b=16;
713 ptdata = data;
714 ptbits = lpBits;
715 for( i=0; i<height; i++ )
716 {
717 for( j=0; j<width; j++ )
718 {
719 if( *ptbits != 0 )
720 ptdata += 3;
721 else
722 {
723 *(ptdata++) = r;
724 *(ptdata++) = g;
725 *(ptdata++) = b;
726 }
727 ptbits += 3;
728 }
729 ptbits += padding;
730 }
731 image.SetMaskColour( r, g, b );
732 image.SetMask( TRUE );
733 }
734 else
735 {
736 image.SetMask( FALSE );
737 }
738 // free allocated resources
739 ::ReleaseDC(NULL, hdc);
740 free(lpDIBh);
741 free(lpBits);
742
743 return image;
744 #endif
745 }
746
747 #endif // wxUSE_IMAGE
748
749 bool wxBitmap::LoadFile(const wxString& filename, long type)
750 {
751 UnRef();
752
753 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
754
755 if ( handler )
756 {
757 m_refData = new wxBitmapRefData;
758
759 return handler->LoadFile(this, filename, type, -1, -1);
760 }
761 #if wxUSE_IMAGE
762 else
763 {
764 wxImage image;
765 if ( image.LoadFile( filename, type ) && image.Ok() )
766 {
767 *this = image.ConvertToBitmap();
768
769 return TRUE;
770 }
771 }
772 #endif // wxUSE_IMAGE
773
774 return FALSE;
775 }
776
777 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
778 {
779 UnRef();
780
781 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
782
783 if ( !handler )
784 {
785 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
786
787 return FALSE;
788 }
789
790 m_refData = new wxBitmapRefData;
791
792 return handler->Create(this, data, type, width, height, depth);
793 }
794
795 bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
796 {
797 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
798
799 if ( handler )
800 {
801 return handler->SaveFile(this, filename, type, palette);
802 }
803 #if wxUSE_IMAGE
804 else
805 {
806 // FIXME what about palette? shouldn't we use it?
807 wxImage image( *this );
808 if ( image.Ok() )
809 {
810 return image.SaveFile(filename, type);
811 }
812 }
813 #endif // wxUSE_IMAGE
814
815 return FALSE;
816 }
817
818 // ----------------------------------------------------------------------------
819 // sub bitmap extraction
820 // ----------------------------------------------------------------------------
821
822 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
823 {
824 #ifndef __WXMICROWIN__
825 wxCHECK_MSG( Ok() &&
826 (rect.x >= 0) && (rect.y >= 0) &&
827 (rect.x+rect.width <= GetWidth()) &&
828 (rect.y+rect.height <= GetHeight()),
829 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
830
831 wxBitmap ret( rect.width, rect.height, GetDepth() );
832 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
833
834 // copy bitmap data
835 HDC dcSrc = ::CreateCompatibleDC(NULL);
836 HDC dcDst = ::CreateCompatibleDC(NULL);
837 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
838 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
839 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
840
841 // copy mask if there is one
842 if (GetMask())
843 {
844 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
845
846 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
847 SelectObject(dcDst, (HBITMAP) hbmpMask);
848 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
849
850 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
851 ret.SetMask(mask);
852 }
853
854 SelectObject(dcDst, NULL);
855 SelectObject(dcSrc, NULL);
856 DeleteDC(dcDst);
857 DeleteDC(dcSrc);
858
859 return ret;
860 #else
861 return wxBitmap();
862 #endif
863 }
864
865 // ----------------------------------------------------------------------------
866 // wxBitmap accessors
867 // ----------------------------------------------------------------------------
868
869 void wxBitmap::SetQuality(int q)
870 {
871 EnsureHasData();
872
873 GetBitmapData()->m_quality = q;
874 }
875
876 #if WXWIN_COMPATIBILITY_2
877 void wxBitmap::SetOk(bool isOk)
878 {
879 EnsureHasData();
880
881 GetBitmapData()->m_ok = isOk;
882 }
883 #endif // WXWIN_COMPATIBILITY_2
884
885 void wxBitmap::SetPalette(const wxPalette& palette)
886 {
887 EnsureHasData();
888
889 GetBitmapData()->m_bitmapPalette = palette;
890 }
891
892 void wxBitmap::SetMask(wxMask *mask)
893 {
894 EnsureHasData();
895
896 GetBitmapData()->m_bitmapMask = mask;
897 }
898
899 // Creates a bitmap that matches the device context, from
900 // an arbitray bitmap. At present, the original bitmap must have an
901 // associated palette. TODO: use a default palette if no palette exists.
902 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
903 wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
904 {
905 #ifdef __WXMICROWIN__
906 return wxBitmap();
907 #else
908 wxMemoryDC memDC;
909 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
910 HPALETTE hPal = (HPALETTE) NULL;
911 LPBITMAPINFO lpDib;
912 void *lpBits = (void*) NULL;
913
914 if( GetPalette() && GetPalette()->Ok() )
915 {
916 tmpBitmap.SetPalette(*GetPalette());
917 memDC.SelectObject(tmpBitmap);
918 memDC.SetPalette(*GetPalette());
919 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
920 }
921 else
922 {
923 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
924 wxPalette palette;
925 palette.SetHPALETTE( (WXHPALETTE)hPal );
926 tmpBitmap.SetPalette( palette );
927 memDC.SelectObject(tmpBitmap);
928 memDC.SetPalette( palette );
929 }
930
931 // set the height negative because in a DIB the order of the lines is
932 // reversed
933 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
934 {
935 return wxNullBitmap;
936 }
937
938 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
939
940 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
941
942 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
943 GetWidth(), GetHeight(),
944 0, 0, 0, GetHeight(),
945 lpBits, lpDib, DIB_RGB_COLORS);
946
947 free(lpBits);
948
949 wxFreeDIB(lpDib);
950
951 return tmpBitmap;
952 #endif
953 }
954
955 // ----------------------------------------------------------------------------
956 // wxMask
957 // ----------------------------------------------------------------------------
958
959 wxMask::wxMask()
960 {
961 m_maskBitmap = 0;
962 }
963
964 // Construct a mask from a bitmap and a colour indicating
965 // the transparent area
966 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
967 {
968 m_maskBitmap = 0;
969 Create(bitmap, colour);
970 }
971
972 // Construct a mask from a bitmap and a palette index indicating
973 // the transparent area
974 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
975 {
976 m_maskBitmap = 0;
977 Create(bitmap, paletteIndex);
978 }
979
980 // Construct a mask from a mono bitmap (copies the bitmap).
981 wxMask::wxMask(const wxBitmap& bitmap)
982 {
983 m_maskBitmap = 0;
984 Create(bitmap);
985 }
986
987 wxMask::~wxMask()
988 {
989 if ( m_maskBitmap )
990 ::DeleteObject((HBITMAP) m_maskBitmap);
991 }
992
993 // Create a mask from a mono bitmap (copies the bitmap).
994 bool wxMask::Create(const wxBitmap& bitmap)
995 {
996 #ifndef __WXMICROWIN__
997 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
998 _T("can't create mask from invalid or not monochrome bitmap") );
999
1000 if ( m_maskBitmap )
1001 {
1002 ::DeleteObject((HBITMAP) m_maskBitmap);
1003 m_maskBitmap = 0;
1004 }
1005
1006 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1007 bitmap.GetWidth(),
1008 bitmap.GetHeight(),
1009 1, 1, 0
1010 );
1011 HDC srcDC = CreateCompatibleDC(0);
1012 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1013 HDC destDC = CreateCompatibleDC(0);
1014 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1015 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1016 SelectObject(srcDC, 0);
1017 DeleteDC(srcDC);
1018 SelectObject(destDC, 0);
1019 DeleteDC(destDC);
1020 return TRUE;
1021 #else
1022 return FALSE;
1023 #endif
1024 }
1025
1026 // Create a mask from a bitmap and a palette index indicating
1027 // the transparent area
1028 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1029 {
1030 if ( m_maskBitmap )
1031 {
1032 ::DeleteObject((HBITMAP) m_maskBitmap);
1033 m_maskBitmap = 0;
1034 }
1035 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1036 {
1037 unsigned char red, green, blue;
1038 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1039 {
1040 wxColour transparentColour(red, green, blue);
1041 return Create(bitmap, transparentColour);
1042 }
1043 }
1044 return FALSE;
1045 }
1046
1047 // Create a mask from a bitmap and a colour indicating
1048 // the transparent area
1049 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1050 {
1051 #ifndef __WXMICROWIN__
1052 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1053
1054 if ( m_maskBitmap )
1055 {
1056 ::DeleteObject((HBITMAP) m_maskBitmap);
1057 m_maskBitmap = 0;
1058 }
1059
1060 int width = bitmap.GetWidth(),
1061 height = bitmap.GetHeight();
1062
1063 // scan the bitmap for the transparent colour and set the corresponding
1064 // pixels in the mask to BLACK and the rest to WHITE
1065 COLORREF maskColour = wxColourToRGB(colour);
1066 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1067
1068 HDC srcDC = ::CreateCompatibleDC(NULL);
1069 HDC destDC = ::CreateCompatibleDC(NULL);
1070 if ( !srcDC || !destDC )
1071 {
1072 wxLogLastError(wxT("CreateCompatibleDC"));
1073 }
1074
1075 bool ok = TRUE;
1076
1077 // SelectObject() will fail
1078 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1079 _T("bitmap can't be selected in another DC") );
1080
1081 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1082 if ( !hbmpSrcOld )
1083 {
1084 wxLogLastError(wxT("SelectObject"));
1085
1086 ok = FALSE;
1087 }
1088
1089 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1090 if ( !hbmpDstOld )
1091 {
1092 wxLogLastError(wxT("SelectObject"));
1093
1094 ok = FALSE;
1095 }
1096
1097 // this is not very efficient, but I can't think of a better way of doing
1098 // it
1099 for ( int w = 0; ok && (w < width); w++ )
1100 {
1101 for ( int h = 0; ok && (h < height); h++ )
1102 {
1103 COLORREF col = GetPixel(srcDC, w, h);
1104 if ( col == CLR_INVALID )
1105 {
1106 wxLogLastError(wxT("GetPixel"));
1107
1108 // doesn't make sense to continue
1109 ok = FALSE;
1110
1111 break;
1112 }
1113
1114 if ( col == maskColour )
1115 {
1116 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
1117 }
1118 else
1119 {
1120 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
1121 }
1122 }
1123 }
1124
1125 ::SelectObject(srcDC, hbmpSrcOld);
1126 ::DeleteDC(srcDC);
1127 ::SelectObject(destDC, hbmpDstOld);
1128 ::DeleteDC(destDC);
1129
1130 return ok;
1131 #else
1132 return FALSE;
1133 #endif
1134 }
1135
1136 // ----------------------------------------------------------------------------
1137 // wxBitmapHandler
1138 // ----------------------------------------------------------------------------
1139
1140 bool wxBitmapHandler::Create(wxGDIImage *image,
1141 void *data,
1142 long flags,
1143 int width, int height, int depth)
1144 {
1145 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1146
1147 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
1148 }
1149
1150 bool wxBitmapHandler::Load(wxGDIImage *image,
1151 const wxString& name,
1152 long flags,
1153 int width, int height)
1154 {
1155 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1156
1157 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1158 }
1159
1160 bool wxBitmapHandler::Save(wxGDIImage *image,
1161 const wxString& name,
1162 int type)
1163 {
1164 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1165
1166 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
1167 }
1168
1169 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1170 void *WXUNUSED(data),
1171 long WXUNUSED(type),
1172 int WXUNUSED(width),
1173 int WXUNUSED(height),
1174 int WXUNUSED(depth))
1175 {
1176 return FALSE;
1177 }
1178
1179 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1180 const wxString& WXUNUSED(name),
1181 long WXUNUSED(type),
1182 int WXUNUSED(desiredWidth),
1183 int WXUNUSED(desiredHeight))
1184 {
1185 return FALSE;
1186 }
1187
1188 bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1189 const wxString& WXUNUSED(name),
1190 int WXUNUSED(type),
1191 const wxPalette *WXUNUSED(palette))
1192 {
1193 return FALSE;
1194 }
1195
1196 // ----------------------------------------------------------------------------
1197 // DIB functions
1198 // ----------------------------------------------------------------------------
1199
1200 #ifndef __WXMICROWIN__
1201 bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1202 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
1203 {
1204 unsigned long i, headerSize;
1205 LPBITMAPINFO lpDIBheader = NULL;
1206 LPPALETTEENTRY lpPe = NULL;
1207
1208
1209 // Allocate space for a DIB header
1210 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1211 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1212 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1213
1214 GetPaletteEntries(hPal, 0, 256, lpPe);
1215
1216 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1217
1218 // Fill in the static parts of the DIB header
1219 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1220 lpDIBheader->bmiHeader.biWidth = xSize;
1221 lpDIBheader->bmiHeader.biHeight = ySize;
1222 lpDIBheader->bmiHeader.biPlanes = 1;
1223
1224 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1225 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1226 lpDIBheader->bmiHeader.biCompression = BI_RGB;
1227 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
1228 lpDIBheader->bmiHeader.biClrUsed = 256;
1229
1230
1231 // Initialize the DIB palette
1232 for (i = 0; i < 256; i++) {
1233 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1234 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1235 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1236 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1237 }
1238
1239 *lpDIBHeader = lpDIBheader;
1240
1241 return TRUE;
1242 }
1243
1244 void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
1245 {
1246 free(lpDIBHeader);
1247 }
1248 #endif
1249
1250 // ----------------------------------------------------------------------------
1251 // other helper functions
1252 // ----------------------------------------------------------------------------
1253
1254 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1255 {
1256 #ifndef __WXMICROWIN__
1257 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1258
1259 // get width/height from the bitmap if not given
1260 if ( !w || !h )
1261 {
1262 BITMAP bm;
1263 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1264 w = bm.bmWidth;
1265 h = bm.bmHeight;
1266 }
1267
1268 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1269 HDC hdcDst = ::CreateCompatibleDC(NULL);
1270 if ( !hdcSrc || !hdcDst )
1271 {
1272 wxLogLastError(wxT("CreateCompatibleDC"));
1273 }
1274
1275 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1276 if ( !hbmpInvMask )
1277 {
1278 wxLogLastError(wxT("CreateBitmap"));
1279 }
1280
1281 ::SelectObject(hdcSrc, hbmpMask);
1282 ::SelectObject(hdcDst, hbmpInvMask);
1283 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1284 hdcSrc, 0, 0,
1285 NOTSRCCOPY) )
1286 {
1287 wxLogLastError(wxT("BitBlt"));
1288 }
1289
1290 ::DeleteDC(hdcSrc);
1291 ::DeleteDC(hdcDst);
1292
1293 return hbmpInvMask;
1294 #else
1295 return 0;
1296 #endif
1297 }