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