]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/bitmap.cpp
[ 1473731 ] 'wxColourBase and wxString <-> wxColour implementation' with minor modifi...
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/bitmap.h"
15 #include "wx/icon.h"
16 #include "wx/log.h"
17 #include "wx/image.h"
18 #include "wx/metafile.h"
19 #include "wx/xpmdecod.h"
20
21 #include "wx/rawbmp.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
24 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
25 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
26
27 #ifdef __DARWIN__
28 #include <ApplicationServices/ApplicationServices.h>
29 #else
30 #include <PictUtils.h>
31 #endif
32
33 #include "wx/mac/uma.h"
34 #include "wx/dcmemory.h"
35
36 // Implementation Notes
37 // --------------------
38 //
39 // we are always working with a 32 bit deep pixel buffer
40 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
41 // therefore we have a separate GWorld there for blitting the mask in
42
43 // under Quartz then content is transformed into a CGImageRef representing the same data
44 // which can be transferred to the GPU by the OS for fast rendering
45
46 // we don't dare use premultiplied alpha yet
47 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
48
49 #if wxUSE_BMPBUTTON
50
51 void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
52 {
53 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
54 if ( bitmap.Ok() )
55 {
56 wxBitmapRefData * bmap = bitmap.GetBitmapData() ;
57 if ( bmap == NULL )
58 return ;
59
60 if ( ( bmap->HasNativeSize() && forceType == 0 ) || forceType == kControlContentIconRef )
61 {
62 wxBitmap scaleBmp ;
63 wxBitmapRefData* bmp = bmap ;
64
65 if ( !bmap->HasNativeSize() )
66 {
67 // as PICT conversion will only result in a 16x16 icon, let's attempt
68 // a few scales for better results
69
70 int w = bitmap.GetWidth() ;
71 int h = bitmap.GetHeight() ;
72 int sz = wxMax( w , h ) ;
73 if ( sz == 24 || sz == 64 )
74 {
75 scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ;
76 bmp = scaleBmp.GetBitmapData() ;
77 }
78 }
79
80 info->contentType = kControlContentIconRef ;
81 info->u.iconRef = bmp->GetIconRef() ;
82 AcquireIconRef( info->u.iconRef ) ;
83 }
84 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
85 else if ( forceType == kControlContentCGImageRef )
86 {
87 info->contentType = kControlContentCGImageRef ;
88 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
89 }
90 #endif
91 else
92 {
93 info->contentType = kControlContentPictHandle ;
94 info->u.picture = bmap->GetPictHandle() ;
95 }
96 }
97 }
98
99 void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
100 {
101 if ( info->contentType == kControlContentIconRef )
102 {
103 ReleaseIconRef( info->u.iconRef ) ;
104 }
105 else if ( info->contentType == kControlNoContent )
106 {
107 // there's no bitmap at all, fall through silently
108 }
109 else if ( info->contentType == kControlContentPictHandle )
110 {
111 // owned by the bitmap, no release here
112 }
113 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
114 else if ( info->contentType == kControlContentCGImageRef )
115 {
116 CGImageRelease( info->u.imageRef ) ;
117 }
118 #endif
119 else
120 {
121 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
122 }
123 }
124
125 #endif //wxUSE_BMPBUTTON
126
127 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
128
129 void wxBitmapRefData::Init()
130 {
131 m_width = 0 ;
132 m_height = 0 ;
133 m_depth = 0 ;
134 m_ok = false ;
135 m_bitmapMask = NULL ;
136
137 #ifdef __WXMAC_OSX__
138 m_cgImageRef = NULL ;
139 #endif
140
141 m_iconRef = NULL ;
142 m_pictHandle = NULL ;
143 m_hBitmap = NULL ;
144 m_hMaskBitmap = NULL;
145 m_maskBytesPerRow = 0 ;
146
147 m_rawAccessCount = 0 ;
148 m_hasAlpha = false;
149 }
150
151 wxBitmapRefData::wxBitmapRefData()
152 {
153 Init() ;
154 }
155
156 wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
157 {
158 Init() ;
159 Create( w , h , d ) ;
160 }
161
162 bool wxBitmapRefData::Create( int w , int h , int d )
163 {
164 m_width = w ;
165 m_height = h ;
166 m_depth = d ;
167
168 m_bytesPerRow = w * 4 ;
169 size_t size = m_bytesPerRow * h ;
170 void* data = m_memBuf.GetWriteBuf( size ) ;
171 memset( data , 0 , size ) ;
172 m_memBuf.UngetWriteBuf( size ) ;
173
174 m_hBitmap = NULL ;
175 Rect rect = { 0 , 0 , m_height , m_width } ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
177 (char*) data , m_bytesPerRow ) ) ;
178 wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ;
179
180 m_ok = ( m_hBitmap != NULL ) ;
181
182 return m_ok ;
183 }
184
185 void wxBitmapRefData::UseAlpha( bool use )
186 {
187 if ( m_hasAlpha == use )
188 return ;
189
190 m_hasAlpha = use ;
191 if ( m_hasAlpha )
192 {
193 wxASSERT( m_hMaskBitmap == NULL ) ;
194
195 int width = GetWidth() ;
196 int height = GetHeight() ;
197 m_maskBytesPerRow = ( width * 4 + 3 ) & 0xFFFFFFC ;
198 size_t size = height * m_maskBytesPerRow ;
199 unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ;
200 wxASSERT( data != NULL ) ;
201
202 memset( data , 0 , size ) ;
203 Rect rect = { 0 , 0 , height , width } ;
204 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
205 (char*) data , m_maskBytesPerRow ) ) ;
206 wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ;
207 m_maskMemBuf.UngetWriteBuf(size) ;
208
209 #if !wxMAC_USE_CORE_GRAPHICS
210 UpdateAlphaMask() ;
211 #endif
212 }
213 else
214 {
215 DisposeGWorld( m_hMaskBitmap ) ;
216 m_hMaskBitmap = NULL ;
217 m_maskBytesPerRow = 0 ;
218 }
219 }
220
221 void *wxBitmapRefData::GetRawAccess() const
222 {
223 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
224 return m_memBuf.GetData() ;
225 }
226
227 void *wxBitmapRefData::BeginRawAccess()
228 {
229 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
230 wxASSERT( m_rawAccessCount == 0 ) ;
231 wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
232 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
233
234 ++m_rawAccessCount ;
235
236 #ifdef __WXMAC_OSX__
237 // we must destroy an existing cached image, as
238 // the bitmap data may change now
239 if ( m_cgImageRef )
240 {
241 CGImageRelease( m_cgImageRef ) ;
242 m_cgImageRef = NULL ;
243 }
244 #endif
245
246 return m_memBuf.GetData() ;
247 }
248
249 void wxBitmapRefData::EndRawAccess()
250 {
251 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
252 wxASSERT( m_rawAccessCount == 1 ) ;
253
254 --m_rawAccessCount ;
255
256 #if !wxMAC_USE_CORE_GRAPHICS
257 UpdateAlphaMask() ;
258 #endif
259 }
260
261 bool wxBitmapRefData::HasNativeSize()
262 {
263 int w = GetWidth() ;
264 int h = GetHeight() ;
265 int sz = wxMax( w , h ) ;
266
267 return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 );
268 }
269
270 IconRef wxBitmapRefData::GetIconRef()
271 {
272 if ( m_iconRef == NULL )
273 {
274 // Create Icon Family Handle
275
276 IconFamilyHandle iconFamily = NULL ;
277
278 #ifdef WORDS_BIGENDIAN
279 iconFamily = (IconFamilyHandle) NewHandle( 8 ) ;
280 (**iconFamily).resourceType = kIconFamilyType ;
281 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
282 #else
283 // test this solution on big endian as well
284 iconFamily = (IconFamilyHandle) NewHandle( 0 ) ;
285 #endif
286
287 int w = GetWidth() ;
288 int h = GetHeight() ;
289 int sz = wxMax( w , h ) ;
290
291 OSType dataType = 0 ;
292 OSType maskType = 0 ;
293
294 switch (sz)
295 {
296 case 128:
297 dataType = kThumbnail32BitData ;
298 maskType = kThumbnail8BitMask ;
299 break;
300
301 case 48:
302 dataType = kHuge32BitData ;
303 maskType = kHuge8BitMask ;
304 break;
305
306 case 32:
307 dataType = kLarge32BitData ;
308 maskType = kLarge8BitMask ;
309 break;
310
311 case 16:
312 dataType = kSmall32BitData ;
313 maskType = kSmall8BitMask ;
314 break;
315
316 default:
317 break;
318 }
319
320 if ( dataType != 0 )
321 {
322 // setup the header properly
323
324 Handle data = NULL ;
325 Handle maskdata = NULL ;
326 unsigned char * maskptr = NULL ;
327 unsigned char * ptr = NULL ;
328 size_t datasize, masksize ;
329
330 datasize = sz * sz * 4 ;
331 data = NewHandle( datasize ) ;
332 HLock( data ) ;
333 ptr = (unsigned char*) *data ;
334 memset( ptr , 0, datasize ) ;
335
336 masksize = sz * sz ;
337 maskdata = NewHandle( masksize ) ;
338 HLock( maskdata ) ;
339 maskptr = (unsigned char*) *maskdata ;
340 memset( maskptr , 0 , masksize ) ;
341
342 bool hasAlpha = HasAlpha() ;
343 wxMask *mask = m_bitmapMask ;
344 unsigned char * source = (unsigned char*) GetRawAccess() ;
345 unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
346
347 for ( int y = 0 ; y < h ; ++y )
348 {
349 unsigned char * dest = ptr + y * sz * 4 ;
350 unsigned char * maskdest = maskptr + y * sz ;
351 unsigned char a, r, g, b;
352
353 for ( int x = 0 ; x < w ; ++x )
354 {
355 a = *source ++ ;
356 r = *source ++ ;
357 g = *source ++ ;
358 b = *source ++ ;
359
360 *dest++ = 0 ;
361 *dest++ = r ;
362 *dest++ = g ;
363 *dest++ = b ;
364
365 if ( mask )
366 {
367 *maskdest++ = 0xFF - *masksource++ ;
368 masksource++ ;
369 masksource++ ;
370 masksource++ ;
371 }
372 else if ( hasAlpha )
373 *maskdest++ = a ;
374 else
375 *maskdest++ = 0xFF ;
376 }
377 }
378
379 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
380 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
381
382 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
383 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
384
385 HUnlock( data ) ;
386 HUnlock( maskdata ) ;
387 DisposeHandle( data ) ;
388 DisposeHandle( maskdata ) ;
389 }
390 else
391 {
392 PicHandle pic = GetPictHandle() ;
393 SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
394 }
395
396 // transform into IconRef
397
398 static int iconCounter = 2 ;
399
400 OSStatus err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ;
401 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
402
403 // we have to retain a reference, as Unregister will decrement it
404 AcquireIconRef( m_iconRef ) ;
405 UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
406 DisposeHandle( (Handle) iconFamily ) ;
407 ++iconCounter ;
408 }
409
410 return m_iconRef ;
411 }
412
413 PicHandle wxBitmapRefData::GetPictHandle()
414 {
415 if ( m_pictHandle == NULL )
416 {
417 CGrafPtr origPort = NULL ;
418 GDHandle origDev = NULL ;
419 GWorldPtr wp = NULL ;
420 GWorldPtr mask = NULL ;
421 int height = GetHeight() ;
422 int width = GetWidth() ;
423
424 Rect rect = { 0 , 0 , height , width } ;
425 RgnHandle clipRgn = NULL ;
426
427 GetGWorld( &origPort , &origDev ) ;
428 wp = GetHBITMAP( &mask ) ;
429
430 if ( mask )
431 {
432 GWorldPtr monoworld ;
433 clipRgn = NewRgn() ;
434 OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
435 verify_noerr(err) ;
436 LockPixels( GetGWorldPixMap( monoworld ) ) ;
437 LockPixels( GetGWorldPixMap( mask ) ) ;
438 SetGWorld( monoworld , NULL ) ;
439
440 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
441 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
442 RGBForeColor( &black ) ;
443 RGBBackColor( &white ) ;
444
445 CopyBits(GetPortBitMapForCopyBits(mask),
446 GetPortBitMapForCopyBits(monoworld),
447 &rect,
448 &rect,
449 srcCopy, NULL);
450 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
451
452 UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
453 UnlockPixels( GetGWorldPixMap( mask ) ) ;
454 DisposeGWorld( monoworld ) ;
455 }
456
457 SetGWorld( wp , NULL ) ;
458 Rect portRect ;
459 GetPortBounds( wp , &portRect ) ;
460 m_pictHandle = OpenPicture(&portRect);
461
462 if (m_pictHandle)
463 {
464 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
465 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
466
467 RGBForeColor( &black ) ;
468 RGBBackColor( &white ) ;
469
470 if ( clipRgn )
471 SetClip( clipRgn ) ;
472
473 LockPixels( GetGWorldPixMap( wp ) ) ;
474 CopyBits(GetPortBitMapForCopyBits(wp),
475 GetPortBitMapForCopyBits(wp),
476 &portRect,
477 &portRect,
478 srcCopy,clipRgn);
479 UnlockPixels( GetGWorldPixMap( wp ) ) ;
480 ClosePicture();
481 }
482
483 SetGWorld( origPort , origDev ) ;
484 if ( clipRgn )
485 DisposeRgn( clipRgn ) ;
486 }
487
488 return m_pictHandle ;
489 }
490
491 #ifdef __WXMAC_OSX__
492 void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
493 {
494 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
495
496 wxASSERT( data == membuf->GetData() ) ;
497
498 delete membuf ;
499 }
500
501 CGImageRef wxBitmapRefData::CGImageCreate() const
502 {
503 wxASSERT( m_ok ) ;
504 wxASSERT( m_rawAccessCount >= 0 ) ;
505 CGImageRef image ;
506 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
507 {
508 size_t imageSize = m_width * m_height * 4 ;
509 void * dataBuffer = m_memBuf.GetData() ;
510 int w = m_width ;
511 int h = m_height ;
512 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
513 wxMemoryBuffer* membuf = NULL ;
514
515 if ( m_bitmapMask )
516 {
517 alphaInfo = kCGImageAlphaFirst ;
518 membuf = new wxMemoryBuffer( imageSize ) ;
519 memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
520 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
521 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
522 unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
523 for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
524 {
525 unsigned char *sourcemask = sourcemaskstart ;
526 for ( int x = 0 ; x < w ; ++x , sourcemask += 4 , destalpha += 4 )
527 {
528 *destalpha = 0xFF - *sourcemask ;
529 }
530 }
531 }
532 else
533 {
534 if ( m_hasAlpha )
535 {
536 #if wxMAC_USE_PREMULTIPLIED_ALPHA
537 alphaInfo = kCGImageAlphaPremultipliedFirst ;
538 #else
539 alphaInfo = kCGImageAlphaFirst ;
540 #endif
541 }
542
543 membuf = new wxMemoryBuffer( m_memBuf ) ;
544 }
545
546 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
547 CGDataProviderRef dataProvider =
548 CGDataProviderCreateWithData(
549 membuf , (const void *)membuf->GetData() , imageSize,
550 wxMacMemoryBufferReleaseProc );
551 image =
552 ::CGImageCreate(
553 w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
554 dataProvider, NULL , false , kCGRenderingIntentDefault );
555 CGDataProviderRelease( dataProvider);
556 }
557 else
558 {
559 image = m_cgImageRef ;
560 CGImageRetain( image ) ;
561 }
562
563 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
564 {
565 // we keep it for later use
566 m_cgImageRef = image ;
567 CGImageRetain( image ) ;
568 }
569
570 return image ;
571 }
572 #endif
573
574 GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
575 {
576 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
577 if ( mask )
578 {
579 *mask = NULL ;
580 if ( m_bitmapMask )
581 {
582 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
583 }
584 else if ( m_hasAlpha )
585 {
586 #if !wxMAC_USE_CORE_GRAPHICS
587 if ( m_rawAccessCount > 0 )
588 UpdateAlphaMask() ;
589 #else
590 // this structure is not kept in synch when using CG, so if something
591 // is really accessing the GrafPorts, we have to sync it
592 UpdateAlphaMask() ;
593 #endif
594
595 *mask = m_hMaskBitmap ;
596 }
597 }
598
599 return m_hBitmap ;
600 }
601
602 void wxBitmapRefData::UpdateAlphaMask() const
603 {
604 if ( m_hasAlpha )
605 {
606 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
607 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
608
609 int h = GetHeight() ;
610 int w = GetWidth() ;
611
612 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
613 {
614 unsigned char* destalpha = destalphabase ;
615
616 for ( int x = 0 ; x < w ; ++x , sourcemask += 4 )
617 {
618 // we must have 24 bit depth for non quartz smooth alpha
619 *destalpha++ = 255 ;
620 *destalpha++ = 255 - *sourcemask ;
621 *destalpha++ = 255 - *sourcemask ;
622 *destalpha++ = 255 - *sourcemask ;
623 }
624 }
625 }
626 }
627
628 void wxBitmapRefData::Free()
629 {
630 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
631
632 #ifdef __WXMAC_OSX__
633 if ( m_cgImageRef )
634 {
635 CGImageRelease( m_cgImageRef ) ;
636 m_cgImageRef = NULL ;
637 }
638 #endif
639
640 if ( m_iconRef )
641 {
642 ReleaseIconRef( m_iconRef ) ;
643 m_iconRef = NULL ;
644 }
645
646 if ( m_pictHandle )
647 {
648 KillPicture( m_pictHandle ) ;
649 m_pictHandle = NULL ;
650 }
651
652 if ( m_hBitmap )
653 {
654 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
655 m_hBitmap = NULL ;
656 }
657
658 if ( m_hMaskBitmap )
659 {
660 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
661 m_hMaskBitmap = NULL ;
662 }
663
664 if (m_bitmapMask)
665 {
666 delete m_bitmapMask;
667 m_bitmapMask = NULL;
668 }
669 }
670
671 wxBitmapRefData::~wxBitmapRefData()
672 {
673 Free() ;
674 }
675
676 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
677 {
678 bool created = false ;
679 int w = icon.GetWidth() ;
680 int h = icon.GetHeight() ;
681
682 Create( icon.GetWidth() , icon.GetHeight() ) ;
683
684 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
685 {
686 IconFamilyHandle iconFamily = NULL ;
687 Handle imagehandle = NewHandle( 0 ) ;
688 Handle maskhandle = NewHandle( 0 ) ;
689
690 OSType maskType = 0;
691 OSType dataType = 0;
692 IconSelectorValue selector = 0 ;
693
694 switch (w)
695 {
696 case 128:
697 dataType = kThumbnail32BitData ;
698 maskType = kThumbnail8BitMask ;
699 selector = kSelectorAllAvailableData ;
700 break;
701
702 case 48:
703 dataType = kHuge32BitData ;
704 maskType = kHuge8BitMask ;
705 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
706 break;
707
708 case 32:
709 dataType = kLarge32BitData ;
710 maskType = kLarge8BitMask ;
711 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
712 break;
713
714 case 16:
715 dataType = kSmall32BitData ;
716 maskType = kSmall8BitMask ;
717 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
718 break;
719
720 default:
721 break;
722 }
723
724 OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ;
725
726 err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ;
727 err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ;
728 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
729 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
730
731 if ( imagehandlesize != 0 && maskhandlesize != 0 )
732 {
733 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
734 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
735
736 UseAlpha() ;
737
738 unsigned char *source = (unsigned char *) *imagehandle ;
739 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
740 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
741
742 for ( int y = 0 ; y < h ; ++y )
743 {
744 for ( int x = 0 ; x < w ; ++x )
745 {
746 *destination++ = *sourcemask++ ;
747 source++ ;
748 *destination++ = *source++ ;
749 *destination++ = *source++ ;
750 *destination++ = *source++ ;
751 }
752 }
753
754 EndRawAccess() ;
755 DisposeHandle( imagehandle ) ;
756 DisposeHandle( maskhandle ) ;
757 created = true ;
758 }
759
760 DisposeHandle( (Handle) iconFamily ) ;
761 }
762
763 if ( !created )
764 {
765 wxMemoryDC dc ;
766 dc.SelectObject( *this ) ;
767 dc.DrawIcon( icon , 0 , 0 ) ;
768 dc.SelectObject( wxNullBitmap ) ;
769 }
770
771 return true;
772 }
773
774 wxBitmap::wxBitmap()
775 {
776 }
777
778 wxBitmap::~wxBitmap()
779 {
780 }
781
782 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
783 {
784 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
785
786 if ( no_bits == 1 )
787 {
788 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
789 if ( the_width % (sizeof(unsigned char) * 8) )
790 linesize += sizeof(unsigned char);
791
792 unsigned char* linestart = (unsigned char*) bits ;
793 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
794
795 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
796 {
797 int index, bit, mask;
798
799 for ( int x = 0 ; x < the_width ; ++x )
800 {
801 index = x / 8 ;
802 bit = x % 8 ;
803 mask = 1 << bit ;
804
805 if ( !(linestart[index] & mask ) )
806 {
807 *destination++ = 0xFF ;
808 *destination++ = 0 ;
809 *destination++ = 0 ;
810 *destination++ = 0 ;
811 }
812 else
813 {
814 *destination++ = 0xFF ;
815 *destination++ = 0xFF ;
816 *destination++ = 0xFF ;
817 *destination++ = 0xFF ;
818 }
819 }
820 }
821
822 EndRawAccess() ;
823 }
824 else
825 {
826 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
827 }
828 }
829
830 wxBitmap::wxBitmap(int w, int h, int d)
831 {
832 (void)Create(w, h, d);
833 }
834
835 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
836 {
837 (void) Create(data, type, width, height, depth);
838 }
839
840 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
841 {
842 LoadFile(filename, type);
843 }
844
845 wxBitmap::wxBitmap(const char **bits)
846 {
847 (void) CreateFromXpm(bits);
848 }
849
850 wxBitmap::wxBitmap(char **bits)
851 {
852 (void) CreateFromXpm((const char **)bits);
853 }
854
855 void * wxBitmap::GetRawAccess() const
856 {
857 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
858
859 return M_BITMAPDATA->GetRawAccess() ;
860 }
861
862 void * wxBitmap::BeginRawAccess()
863 {
864 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
865
866 return M_BITMAPDATA->BeginRawAccess() ;
867 }
868
869 void wxBitmap::EndRawAccess()
870 {
871 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
872
873 M_BITMAPDATA->EndRawAccess() ;
874 }
875
876 bool wxBitmap::CreateFromXpm(const char **bits)
877 {
878 #if wxUSE_IMAGE
879 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
880
881 wxXPMDecoder decoder;
882 wxImage img = decoder.ReadData(bits);
883 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
884
885 *this = wxBitmap(img);
886
887 return true;
888 #else
889
890 return false;
891 #endif
892 }
893
894 #ifdef __WXMAC_OSX__
895 WXCGIMAGEREF wxBitmap::CGImageCreate() const
896 {
897 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
898
899 return M_BITMAPDATA->CGImageCreate() ;
900 }
901 #endif
902
903 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
904 {
905 wxCHECK_MSG( Ok() &&
906 (rect.x >= 0) && (rect.y >= 0) &&
907 (rect.x+rect.width <= GetWidth()) &&
908 (rect.y+rect.height <= GetHeight()),
909 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
910
911 wxBitmap ret( rect.width, rect.height, GetDepth() );
912 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
913
914 int sourcewidth = GetWidth() ;
915 int destwidth = rect.width ;
916 int destheight = rect.height ;
917
918 {
919 unsigned char *sourcedata = (unsigned char*) GetRawAccess() ;
920 unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ;
921 wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ;
922
923 int sourcelinesize = sourcewidth * 4 ;
924 int destlinesize = destwidth * 4 ;
925 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
926 unsigned char *dest = destdata ;
927
928 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
929 {
930 memcpy( dest , source , destlinesize ) ;
931 }
932 }
933
934 ret.EndRawAccess() ;
935
936 if ( M_BITMAPDATA->m_bitmapMask )
937 {
938 wxMemoryBuffer maskbuf ;
939 int rowBytes = ( destwidth * 4 + 3 ) & 0xFFFFFFC ;
940 size_t maskbufsize = rowBytes * destheight ;
941
942 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
943 int destlinesize = rowBytes ;
944
945 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
946 unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
947 wxASSERT( (source != NULL) && (destdata != NULL) ) ;
948
949 source += rect.x * 4 + rect.y * sourcelinesize ;
950 unsigned char *dest = destdata ;
951
952 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
953 {
954 memcpy( dest , source , destlinesize ) ;
955 }
956
957 maskbuf.UngetWriteBuf( maskbufsize ) ;
958 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
959 }
960 else if ( HasAlpha() )
961 ret.UseAlpha() ;
962
963 return ret;
964 }
965
966 bool wxBitmap::Create(int w, int h, int d)
967 {
968 UnRef();
969
970 if ( d < 0 )
971 d = wxDisplayDepth() ;
972
973 m_refData = new wxBitmapRefData( w , h , d );
974
975 return M_BITMAPDATA->Ok() ;
976 }
977
978 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
979 {
980 UnRef();
981
982 wxBitmapHandler *handler = FindHandler(type);
983
984 if ( handler )
985 {
986 m_refData = new wxBitmapRefData;
987
988 return handler->LoadFile(this, filename, type, -1, -1);
989 }
990 else
991 {
992 #if wxUSE_IMAGE
993 wxImage loadimage(filename, type);
994 if (loadimage.Ok())
995 {
996 *this = loadimage;
997
998 return true;
999 }
1000 #endif
1001 }
1002
1003 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1004
1005 return false;
1006 }
1007
1008 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
1009 {
1010 UnRef();
1011
1012 m_refData = new wxBitmapRefData;
1013
1014 wxBitmapHandler *handler = FindHandler(type);
1015
1016 if ( handler == NULL )
1017 {
1018 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1019
1020 return false;
1021 }
1022
1023 return handler->Create(this, data, type, width, height, depth);
1024 }
1025
1026 #if wxUSE_IMAGE
1027
1028 wxBitmap::wxBitmap(const wxImage& image, int depth)
1029 {
1030 wxCHECK_RET( image.Ok(), wxT("invalid image") );
1031
1032 // width and height of the device-dependent bitmap
1033 int width = image.GetWidth();
1034 int height = image.GetHeight();
1035
1036 m_refData = new wxBitmapRefData( width , height , depth ) ;
1037
1038 // Create picture
1039
1040 bool hasAlpha = false ;
1041
1042 if ( image.HasMask() )
1043 {
1044 // takes precedence, don't mix with alpha info
1045 }
1046 else
1047 {
1048 hasAlpha = image.HasAlpha() ;
1049 }
1050
1051 if ( hasAlpha )
1052 UseAlpha() ;
1053
1054 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
1055 register unsigned char* data = image.GetData();
1056 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
1057
1058 for (int y = 0; y < height; y++)
1059 {
1060 for (int x = 0; x < width; x++)
1061 {
1062 if ( hasAlpha )
1063 {
1064 const unsigned char a = *alpha++;
1065 *destination++ = a ;
1066
1067 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1068 *destination++ = ((*data++) * a + 127) / 255 ;
1069 *destination++ = ((*data++) * a + 127) / 255 ;
1070 *destination++ = ((*data++) * a + 127) / 255 ;
1071 #else
1072 *destination++ = *data++ ;
1073 *destination++ = *data++ ;
1074 *destination++ = *data++ ;
1075 #endif
1076 }
1077 else
1078 {
1079 *destination++ = 0xFF ;
1080 *destination++ = *data++ ;
1081 *destination++ = *data++ ;
1082 *destination++ = *data++ ;
1083 }
1084 }
1085 }
1086
1087 EndRawAccess() ;
1088 if ( image.HasMask() )
1089 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
1090 }
1091
1092 wxImage wxBitmap::ConvertToImage() const
1093 {
1094 wxImage image;
1095
1096 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1097
1098 // create an wxImage object
1099 int width = GetWidth();
1100 int height = GetHeight();
1101 image.Create( width, height );
1102
1103 unsigned char *data = image.GetData();
1104 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1105
1106 unsigned char* source = (unsigned char*) GetRawAccess() ;
1107
1108 bool hasAlpha = false ;
1109 bool hasMask = false ;
1110 int maskBytesPerRow = 0 ;
1111 unsigned char *alpha = NULL ;
1112 unsigned char *mask = NULL ;
1113
1114 if ( HasAlpha() )
1115 hasAlpha = true ;
1116
1117 if ( GetMask() )
1118 {
1119 hasMask = true ;
1120 mask = (unsigned char*) GetMask()->GetRawAccess() ;
1121 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
1122 }
1123
1124 if ( hasAlpha )
1125 {
1126 image.SetAlpha() ;
1127 alpha = image.GetAlpha() ;
1128 }
1129
1130 int index = 0;
1131
1132 // The following masking algorithm is the same as well in msw/gtk:
1133 // the colour used as transparent one in wxImage and the one it is
1134 // replaced with when it actually occurs in the bitmap
1135 static const int MASK_RED = 1;
1136 static const int MASK_GREEN = 2;
1137 static const int MASK_BLUE = 3;
1138 static const int MASK_BLUE_REPLACEMENT = 2;
1139
1140 for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow )
1141 {
1142 unsigned char * maskp = mask ;
1143 unsigned char a, r, g, b;
1144 long color;
1145
1146 for (int xx = 0; xx < width; xx++)
1147 {
1148 color = *((long*) source) ;
1149 #ifdef WORDS_BIGENDIAN
1150 a = ((color&0xFF000000) >> 24) ;
1151 r = ((color&0x00FF0000) >> 16) ;
1152 g = ((color&0x0000FF00) >> 8) ;
1153 b = (color&0x000000FF);
1154 #else
1155 b = ((color&0xFF000000) >> 24) ;
1156 g = ((color&0x00FF0000) >> 16) ;
1157 r = ((color&0x0000FF00) >> 8) ;
1158 a = (color&0x000000FF);
1159 #endif
1160 if ( hasMask )
1161 {
1162 if ( *maskp++ == 0xFF )
1163 {
1164 r = MASK_RED ;
1165 g = MASK_GREEN ;
1166 b = MASK_BLUE ;
1167 }
1168 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1169 b = MASK_BLUE_REPLACEMENT ;
1170
1171 maskp++ ;
1172 maskp++ ;
1173 maskp++ ;
1174 }
1175 else if ( hasAlpha )
1176 *alpha++ = a ;
1177
1178 data[index ] = r ;
1179 data[index + 1] = g ;
1180 data[index + 2] = b ;
1181
1182 index += 3;
1183 source += 4 ;
1184 }
1185 }
1186
1187 if ( hasMask )
1188 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1189
1190 return image;
1191 }
1192
1193 #endif //wxUSE_IMAGE
1194
1195 bool wxBitmap::SaveFile( const wxString& filename,
1196 wxBitmapType type, const wxPalette *palette ) const
1197 {
1198 bool success = false;
1199 wxBitmapHandler *handler = FindHandler(type);
1200
1201 if ( handler )
1202 {
1203 success = handler->SaveFile(this, filename, type, palette);
1204 }
1205 else
1206 {
1207 #if wxUSE_IMAGE
1208 wxImage image = ConvertToImage();
1209 success = image.SaveFile(filename, type);
1210 #else
1211 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1212 #endif
1213 }
1214
1215 return success;
1216 }
1217
1218 bool wxBitmap::Ok() const
1219 {
1220 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
1221 }
1222
1223 int wxBitmap::GetHeight() const
1224 {
1225 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1226
1227 return M_BITMAPDATA->GetHeight();
1228 }
1229
1230 int wxBitmap::GetWidth() const
1231 {
1232 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1233
1234 return M_BITMAPDATA->GetWidth() ;
1235 }
1236
1237 int wxBitmap::GetDepth() const
1238 {
1239 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1240
1241 return M_BITMAPDATA->GetDepth();
1242 }
1243
1244 #if WXWIN_COMPATIBILITY_2_4
1245 int wxBitmap::GetQuality() const
1246 {
1247 return 0;
1248 }
1249
1250 void wxBitmap::SetQuality(int WXUNUSED(quality))
1251 {
1252 }
1253 #endif
1254
1255 wxMask *wxBitmap::GetMask() const
1256 {
1257 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1258
1259 return M_BITMAPDATA->m_bitmapMask;
1260 }
1261
1262 bool wxBitmap::HasAlpha() const
1263 {
1264 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1265
1266 return M_BITMAPDATA->HasAlpha() ;
1267 }
1268
1269 void wxBitmap::SetWidth(int w)
1270 {
1271 if (!M_BITMAPDATA)
1272 m_refData = new wxBitmapRefData;
1273
1274 M_BITMAPDATA->SetWidth(w);
1275 }
1276
1277 void wxBitmap::SetHeight(int h)
1278 {
1279 if (!M_BITMAPDATA)
1280 m_refData = new wxBitmapRefData;
1281
1282 M_BITMAPDATA->SetHeight(h);
1283 }
1284
1285 void wxBitmap::SetDepth(int d)
1286 {
1287 if (!M_BITMAPDATA)
1288 m_refData = new wxBitmapRefData;
1289
1290 M_BITMAPDATA->SetDepth(d);
1291 }
1292
1293 void wxBitmap::SetOk(bool isOk)
1294 {
1295 if (!M_BITMAPDATA)
1296 m_refData = new wxBitmapRefData;
1297
1298 M_BITMAPDATA->SetOk(isOk);
1299 }
1300
1301 #if wxUSE_PALETTE
1302 wxPalette *wxBitmap::GetPalette() const
1303 {
1304 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1305
1306 return &M_BITMAPDATA->m_bitmapPalette;
1307 }
1308
1309 void wxBitmap::SetPalette(const wxPalette& palette)
1310 {
1311 if (!M_BITMAPDATA)
1312 m_refData = new wxBitmapRefData;
1313
1314 M_BITMAPDATA->m_bitmapPalette = palette ;
1315 }
1316 #endif // wxUSE_PALETTE
1317
1318 void wxBitmap::SetMask(wxMask *mask)
1319 {
1320 if (!M_BITMAPDATA)
1321 m_refData = new wxBitmapRefData;
1322
1323 // Remove existing mask if there is one.
1324 delete M_BITMAPDATA->m_bitmapMask;
1325
1326 M_BITMAPDATA->m_bitmapMask = mask ;
1327 }
1328
1329 WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1330 {
1331 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
1332 }
1333
1334 // ----------------------------------------------------------------------------
1335 // wxMask
1336 // ----------------------------------------------------------------------------
1337
1338 wxMask::wxMask()
1339 {
1340 Init() ;
1341 }
1342
1343 // Construct a mask from a bitmap and a colour indicating
1344 // the transparent area
1345 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
1346 {
1347 Init() ;
1348 Create( bitmap, colour );
1349 }
1350
1351 // Construct a mask from a mono bitmap (copies the bitmap).
1352 wxMask::wxMask( const wxBitmap& bitmap )
1353 {
1354 Init() ;
1355 Create( bitmap );
1356 }
1357
1358 // Construct a mask from a mono bitmap (copies the bitmap).
1359
1360 wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1361 {
1362 Init() ;
1363 Create( data, width , height , bytesPerRow );
1364 }
1365
1366 wxMask::~wxMask()
1367 {
1368 if ( m_maskBitmap )
1369 {
1370 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
1371 m_maskBitmap = NULL ;
1372 }
1373 }
1374
1375 void wxMask::Init()
1376 {
1377 m_width = m_height = m_bytesPerRow = 0 ;
1378 m_maskBitmap = NULL ;
1379 }
1380
1381 void *wxMask::GetRawAccess() const
1382 {
1383 return m_memBuf.GetData() ;
1384 }
1385
1386 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1387 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1388
1389 void wxMask::RealizeNative()
1390 {
1391 if ( m_maskBitmap )
1392 {
1393 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
1394 m_maskBitmap = NULL ;
1395 }
1396
1397 Rect rect = { 0 , 0 , m_height , m_width } ;
1398
1399 OSStatus err = NewGWorldFromPtr(
1400 (GWorldPtr*) &m_maskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
1401 (char*) m_memBuf.GetData() , m_bytesPerRow ) ;
1402 verify_noerr( err ) ;
1403 }
1404
1405 // Create a mask from a mono bitmap (copies the bitmap).
1406
1407 bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1408 {
1409 m_memBuf = data ;
1410 m_width = width ;
1411 m_height = height ;
1412 m_bytesPerRow = bytesPerRow ;
1413
1414 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1415
1416 RealizeNative() ;
1417
1418 return true ;
1419 }
1420
1421 // Create a mask from a mono bitmap (copies the bitmap).
1422 bool wxMask::Create(const wxBitmap& bitmap)
1423 {
1424 m_width = bitmap.GetWidth() ;
1425 m_height = bitmap.GetHeight() ;
1426 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
1427
1428 size_t size = m_bytesPerRow * m_height ;
1429 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1430 wxASSERT( destdatabase != NULL ) ;
1431
1432 memset( destdatabase , 0 , size ) ;
1433 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1434
1435 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1436 {
1437 unsigned char *destdata = destdatabase ;
1438 unsigned char r, g, b;
1439
1440 for ( int x = 0 ; x < m_width ; ++x )
1441 {
1442 srcdata++ ;
1443 r = *srcdata++ ;
1444 g = *srcdata++ ;
1445 b = *srcdata++ ;
1446
1447 if ( ( r + g + b ) > 0x10 )
1448 {
1449 *destdata++ = 0xFF ;
1450 *destdata++ = 0xFF ;
1451 *destdata++ = 0xFF ;
1452 *destdata++ = 0xFF ;
1453 }
1454 else
1455 {
1456 *destdata++ = 0x00 ;
1457 *destdata++ = 0x00 ;
1458 *destdata++ = 0x00 ;
1459 *destdata++ = 0x00 ;
1460 }
1461 }
1462 }
1463
1464 m_memBuf.UngetWriteBuf( size ) ;
1465 RealizeNative() ;
1466
1467 return true;
1468 }
1469
1470 // Create a mask from a bitmap and a colour indicating
1471 // the transparent area
1472 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1473 {
1474 m_width = bitmap.GetWidth() ;
1475 m_height = bitmap.GetHeight() ;
1476 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
1477
1478 size_t size = m_bytesPerRow * m_height ;
1479 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1480 wxASSERT( destdatabase != NULL ) ;
1481
1482 memset( destdatabase , 0 , size ) ;
1483 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1484
1485 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
1486 {
1487 unsigned char *destdata = destdatabase ;
1488 unsigned char r, g, b;
1489
1490 for ( int x = 0 ; x < m_width ; ++x )
1491 {
1492 srcdata++ ;
1493 r = *srcdata++ ;
1494 g = *srcdata++ ;
1495 b = *srcdata++ ;
1496
1497 if ( colour == wxColour( r , g , b ) )
1498 {
1499 *destdata++ = 0xFF ;
1500 *destdata++ = 0xFF ;
1501 *destdata++ = 0xFF ;
1502 *destdata++ = 0xFF ;
1503 }
1504 else
1505 {
1506 *destdata++ = 0x00 ;
1507 *destdata++ = 0x00 ;
1508 *destdata++ = 0x00 ;
1509 *destdata++ = 0x00 ;
1510 }
1511 }
1512 }
1513
1514 m_memBuf.UngetWriteBuf( size ) ;
1515 RealizeNative() ;
1516
1517 return true;
1518 }
1519
1520 WXHBITMAP wxMask::GetHBITMAP() const
1521 {
1522 return m_maskBitmap ;
1523 }
1524
1525 // ----------------------------------------------------------------------------
1526 // wxBitmapHandler
1527 // ----------------------------------------------------------------------------
1528
1529 wxBitmapHandler::~wxBitmapHandler()
1530 {
1531 }
1532
1533 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1534 {
1535 return false;
1536 }
1537
1538 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1539 int desiredWidth, int desiredHeight)
1540 {
1541 return false;
1542 }
1543
1544 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1545 {
1546 return false;
1547 }
1548
1549 // ----------------------------------------------------------------------------
1550 // Standard Handlers
1551 // ----------------------------------------------------------------------------
1552
1553 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1554 {
1555 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1556
1557 public:
1558 inline wxPICTResourceHandler()
1559 {
1560 SetName(wxT("Macintosh Pict resource"));
1561 SetExtension(wxEmptyString);
1562 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
1563 };
1564
1565 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1566 int desiredWidth, int desiredHeight);
1567 };
1568
1569 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1570
1571
1572 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1573 int desiredWidth, int desiredHeight)
1574 {
1575 #if wxUSE_METAFILE
1576 Str255 theName ;
1577 wxMacStringToPascal( name , theName ) ;
1578
1579 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1580 if ( thePict )
1581 {
1582 wxMetafile mf ;
1583
1584 mf.SetHMETAFILE( (WXHMETAFILE) thePict ) ;
1585 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1586 wxMemoryDC dc ;
1587 dc.SelectObject( *bitmap ) ;
1588 mf.Play( &dc ) ;
1589 dc.SelectObject( wxNullBitmap ) ;
1590
1591 return true ;
1592 }
1593 #endif
1594
1595 return false ;
1596 }
1597
1598 void wxBitmap::InitStandardHandlers()
1599 {
1600 AddHandler( new wxPICTResourceHandler ) ;
1601 AddHandler( new wxICONResourceHandler ) ;
1602 }
1603
1604 // ----------------------------------------------------------------------------
1605 // raw bitmap access support
1606 // ----------------------------------------------------------------------------
1607
1608 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1609 {
1610 if ( !Ok() )
1611 // no bitmap, no data (raw or otherwise)
1612 return NULL;
1613
1614 data.m_width = GetWidth() ;
1615 data.m_height = GetHeight() ;
1616 data.m_stride = GetWidth() * 4 ;
1617
1618 return BeginRawAccess() ;
1619 }
1620
1621 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1622 {
1623 EndRawAccess() ;
1624 }
1625
1626 void wxBitmap::UseAlpha()
1627 {
1628 // remember that we are using alpha channel:
1629 // we'll need to create a proper mask in UngetRawData()
1630 M_BITMAPDATA->UseAlpha( true );
1631 }