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