]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/bitmap.cpp
cleanup - reformat; added (disabled) support for secondary highlight color
[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 *maskdest++ = *masksource++ ;
367 else if ( hasAlpha )
368 *maskdest++ = a ;
369 else
370 *maskdest++ = 0xFF ;
371 }
372 }
373
374 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
375 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
376
377 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
378 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
379
380 HUnlock( data ) ;
381 HUnlock( maskdata ) ;
382 DisposeHandle( data ) ;
383 DisposeHandle( maskdata ) ;
384 }
385 else
386 {
387 PicHandle pic = GetPictHandle() ;
388 SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
389 }
390
391 // transform into IconRef
392
393 static int iconCounter = 2 ;
394
395 OSStatus err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ;
396 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
397
398 // we have to retain a reference, as Unregister will decrement it
399 AcquireIconRef( m_iconRef ) ;
400 UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
401 DisposeHandle( (Handle) iconFamily ) ;
402 ++iconCounter ;
403 }
404
405 return m_iconRef ;
406 }
407
408 PicHandle wxBitmapRefData::GetPictHandle()
409 {
410 if ( m_pictHandle == NULL )
411 {
412 CGrafPtr origPort = NULL ;
413 GDHandle origDev = NULL ;
414 GWorldPtr wp = NULL ;
415 GWorldPtr mask = NULL ;
416 int height = GetHeight() ;
417 int width = GetWidth() ;
418
419 Rect rect = { 0 , 0 , height , width } ;
420 RgnHandle clipRgn = NULL ;
421
422 GetGWorld( &origPort , &origDev ) ;
423 wp = GetHBITMAP( &mask ) ;
424
425 if ( mask )
426 {
427 GWorldPtr monoworld ;
428 clipRgn = NewRgn() ;
429 OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
430 verify_noerr(err) ;
431 LockPixels( GetGWorldPixMap( monoworld ) ) ;
432 LockPixels( GetGWorldPixMap( mask ) ) ;
433 SetGWorld( monoworld , NULL ) ;
434
435 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
436 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
437 RGBForeColor( &black ) ;
438 RGBBackColor( &white ) ;
439
440 CopyBits(GetPortBitMapForCopyBits(mask),
441 GetPortBitMapForCopyBits(monoworld),
442 &rect,
443 &rect,
444 srcCopy, NULL);
445 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
446
447 UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
448 UnlockPixels( GetGWorldPixMap( mask ) ) ;
449 DisposeGWorld( monoworld ) ;
450 }
451
452 SetGWorld( wp , NULL ) ;
453 Rect portRect ;
454 GetPortBounds( wp , &portRect ) ;
455 m_pictHandle = OpenPicture(&portRect);
456
457 if (m_pictHandle)
458 {
459 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
460 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
461
462 RGBForeColor( &black ) ;
463 RGBBackColor( &white ) ;
464
465 if ( clipRgn )
466 SetClip( clipRgn ) ;
467
468 LockPixels( GetGWorldPixMap( wp ) ) ;
469 CopyBits(GetPortBitMapForCopyBits(wp),
470 GetPortBitMapForCopyBits(wp),
471 &portRect,
472 &portRect,
473 srcCopy,clipRgn);
474 UnlockPixels( GetGWorldPixMap( wp ) ) ;
475 ClosePicture();
476 }
477
478 SetGWorld( origPort , origDev ) ;
479 if ( clipRgn )
480 DisposeRgn( clipRgn ) ;
481 }
482
483 return m_pictHandle ;
484 }
485
486 #ifdef __WXMAC_OSX__
487 void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
488 {
489 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
490
491 wxASSERT( data == membuf->GetData() ) ;
492
493 delete membuf ;
494 }
495
496 CGImageRef wxBitmapRefData::CGImageCreate() const
497 {
498 wxASSERT( m_ok ) ;
499 wxASSERT( m_rawAccessCount >= 0 ) ;
500 CGImageRef image ;
501 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
502 {
503 size_t imageSize = m_width * m_height * 4 ;
504 void * dataBuffer = m_memBuf.GetData() ;
505 int w = m_width ;
506 int h = m_height ;
507 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
508 wxMemoryBuffer* membuf = NULL ;
509
510 if ( m_bitmapMask )
511 {
512 alphaInfo = kCGImageAlphaFirst ;
513 membuf = new wxMemoryBuffer( imageSize ) ;
514 memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
515 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
516 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
517 unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
518 for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
519 {
520 unsigned char *sourcemask = sourcemaskstart ;
521 for ( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
522 {
523 *destalpha = *sourcemask ;
524 }
525 }
526 }
527 else
528 {
529 if ( m_hasAlpha )
530 {
531 #if wxMAC_USE_PREMULTIPLIED_ALPHA
532 alphaInfo = kCGImageAlphaPremultipliedFirst ;
533 #else
534 alphaInfo = kCGImageAlphaFirst ;
535 #endif
536 }
537
538 membuf = new wxMemoryBuffer( m_memBuf ) ;
539 }
540
541 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
542 CGDataProviderRef dataProvider =
543 CGDataProviderCreateWithData(
544 membuf , (const void *)membuf->GetData() , imageSize,
545 wxMacMemoryBufferReleaseProc );
546 image =
547 ::CGImageCreate(
548 w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
549 dataProvider, NULL , false , kCGRenderingIntentDefault );
550 CGDataProviderRelease( dataProvider);
551 }
552 else
553 {
554 image = m_cgImageRef ;
555 CGImageRetain( image ) ;
556 }
557
558 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
559 {
560 // we keep it for later use
561 m_cgImageRef = image ;
562 CGImageRetain( image ) ;
563 }
564
565 return image ;
566 }
567 #endif
568
569 GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
570 {
571 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
572 if ( mask )
573 {
574 *mask = NULL ;
575 if ( m_bitmapMask )
576 {
577 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
578 }
579 else if ( m_hasAlpha )
580 {
581 #if !wxMAC_USE_CORE_GRAPHICS
582 if ( m_rawAccessCount > 0 )
583 UpdateAlphaMask() ;
584 #else
585 // this structure is not kept in synch when using CG, so if something
586 // is really accessing the GrafPorts, we have to sync it
587 UpdateAlphaMask() ;
588 #endif
589
590 *mask = m_hMaskBitmap ;
591 }
592 }
593
594 return m_hBitmap ;
595 }
596
597 void wxBitmapRefData::UpdateAlphaMask() const
598 {
599 if ( m_hasAlpha )
600 {
601 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
602 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
603
604 int h = GetHeight() ;
605 int w = GetWidth() ;
606
607 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
608 {
609 unsigned char* destalpha = destalphabase ;
610
611 for ( int x = 0 ; x < w ; ++x , sourcemask += 4 )
612 {
613 // we must have 24 bit depth for non quartz smooth alpha
614 *destalpha++ = 255 ;
615 *destalpha++ = 255 - *sourcemask ;
616 *destalpha++ = 255 - *sourcemask ;
617 *destalpha++ = 255 - *sourcemask ;
618 }
619 }
620 }
621 }
622
623 void wxBitmapRefData::Free()
624 {
625 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
626
627 #ifdef __WXMAC_OSX__
628 if ( m_cgImageRef )
629 {
630 CGImageRelease( m_cgImageRef ) ;
631 m_cgImageRef = NULL ;
632 }
633 #endif
634
635 if ( m_iconRef )
636 {
637 ReleaseIconRef( m_iconRef ) ;
638 m_iconRef = NULL ;
639 }
640
641 if ( m_pictHandle )
642 {
643 KillPicture( m_pictHandle ) ;
644 m_pictHandle = NULL ;
645 }
646
647 if ( m_hBitmap )
648 {
649 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
650 m_hBitmap = NULL ;
651 }
652
653 if ( m_hMaskBitmap )
654 {
655 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
656 m_hMaskBitmap = NULL ;
657 }
658
659 if (m_bitmapMask)
660 {
661 delete m_bitmapMask;
662 m_bitmapMask = NULL;
663 }
664 }
665
666 wxBitmapRefData::~wxBitmapRefData()
667 {
668 Free() ;
669 }
670
671 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
672 {
673 bool created = false ;
674 int w = icon.GetWidth() ;
675 int h = icon.GetHeight() ;
676
677 Create( icon.GetWidth() , icon.GetHeight() ) ;
678
679 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
680 {
681 IconFamilyHandle iconFamily = NULL ;
682 Handle imagehandle = NewHandle( 0 ) ;
683 Handle maskhandle = NewHandle( 0 ) ;
684
685 OSType maskType = 0;
686 OSType dataType = 0;
687 IconSelectorValue selector = 0 ;
688
689 switch (w)
690 {
691 case 128:
692 dataType = kThumbnail32BitData ;
693 maskType = kThumbnail8BitMask ;
694 selector = kSelectorAllAvailableData ;
695 break;
696
697 case 48:
698 dataType = kHuge32BitData ;
699 maskType = kHuge8BitMask ;
700 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
701 break;
702
703 case 32:
704 dataType = kLarge32BitData ;
705 maskType = kLarge8BitMask ;
706 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
707 break;
708
709 case 16:
710 dataType = kSmall32BitData ;
711 maskType = kSmall8BitMask ;
712 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
713 break;
714
715 default:
716 break;
717 }
718
719 OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ;
720
721 err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ;
722 err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ;
723 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
724 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
725
726 if ( imagehandlesize != 0 && maskhandlesize != 0 )
727 {
728 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
729 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
730
731 UseAlpha() ;
732
733 unsigned char *source = (unsigned char *) *imagehandle ;
734 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
735 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
736
737 for ( int y = 0 ; y < h ; ++y )
738 {
739 for ( int x = 0 ; x < w ; ++x )
740 {
741 *destination++ = *sourcemask++ ;
742 source++ ;
743 *destination++ = *source++ ;
744 *destination++ = *source++ ;
745 *destination++ = *source++ ;
746 }
747 }
748
749 EndRawAccess() ;
750 DisposeHandle( imagehandle ) ;
751 DisposeHandle( maskhandle ) ;
752 created = true ;
753 }
754
755 DisposeHandle( (Handle) iconFamily ) ;
756 }
757
758 if ( !created )
759 {
760 wxMemoryDC dc ;
761 dc.SelectObject( *this ) ;
762 dc.DrawIcon( icon , 0 , 0 ) ;
763 dc.SelectObject( wxNullBitmap ) ;
764 }
765
766 return true;
767 }
768
769 wxBitmap::wxBitmap()
770 {
771 }
772
773 wxBitmap::~wxBitmap()
774 {
775 }
776
777 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
778 {
779 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
780
781 if ( no_bits == 1 )
782 {
783 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
784 if ( the_width % (sizeof(unsigned char) * 8) )
785 linesize += sizeof(unsigned char);
786
787 unsigned char* linestart = (unsigned char*) bits ;
788 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
789
790 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
791 {
792 int index, bit, mask;
793
794 for ( int x = 0 ; x < the_width ; ++x )
795 {
796 index = x / 8 ;
797 bit = x % 8 ;
798 mask = 1 << bit ;
799
800 if ( linestart[index] & mask )
801 {
802 *destination++ = 0xFF ;
803 *destination++ = 0 ;
804 *destination++ = 0 ;
805 *destination++ = 0 ;
806 }
807 else
808 {
809 *destination++ = 0xFF ;
810 *destination++ = 0xFF ;
811 *destination++ = 0xFF ;
812 *destination++ = 0xFF ;
813 }
814 }
815 }
816
817 EndRawAccess() ;
818 }
819 else
820 {
821 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
822 }
823 }
824
825 wxBitmap::wxBitmap(int w, int h, int d)
826 {
827 (void)Create(w, h, d);
828 }
829
830 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
831 {
832 (void) Create(data, type, width, height, depth);
833 }
834
835 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
836 {
837 LoadFile(filename, type);
838 }
839
840 wxBitmap::wxBitmap(const char **bits)
841 {
842 (void) CreateFromXpm(bits);
843 }
844
845 wxBitmap::wxBitmap(char **bits)
846 {
847 (void) CreateFromXpm((const char **)bits);
848 }
849
850 void * wxBitmap::GetRawAccess() const
851 {
852 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
853
854 return M_BITMAPDATA->GetRawAccess() ;
855 }
856
857 void * wxBitmap::BeginRawAccess()
858 {
859 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
860
861 return M_BITMAPDATA->BeginRawAccess() ;
862 }
863
864 void wxBitmap::EndRawAccess()
865 {
866 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
867
868 M_BITMAPDATA->EndRawAccess() ;
869 }
870
871 bool wxBitmap::CreateFromXpm(const char **bits)
872 {
873 #if wxUSE_IMAGE
874 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") )
875
876 wxXPMDecoder decoder;
877 wxImage img = decoder.ReadData(bits);
878 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") )
879
880 *this = wxBitmap(img);
881
882 return true;
883 #else
884
885 return false;
886 #endif
887 }
888
889 #ifdef __WXMAC_OSX__
890 WXCGIMAGEREF wxBitmap::CGImageCreate() const
891 {
892 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
893
894 return M_BITMAPDATA->CGImageCreate() ;
895 }
896 #endif
897
898 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
899 {
900 wxCHECK_MSG( Ok() &&
901 (rect.x >= 0) && (rect.y >= 0) &&
902 (rect.x+rect.width <= GetWidth()) &&
903 (rect.y+rect.height <= GetHeight()),
904 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
905
906 wxBitmap ret( rect.width, rect.height, GetDepth() );
907 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
908
909 int sourcewidth = GetWidth() ;
910 int destwidth = rect.width ;
911 int destheight = rect.height ;
912
913 {
914 unsigned char *sourcedata = (unsigned char*) GetRawAccess() ;
915 unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ;
916 wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ;
917
918 int sourcelinesize = sourcewidth * 4 ;
919 int destlinesize = destwidth * 4 ;
920 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
921 unsigned char *dest = destdata ;
922
923 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
924 {
925 memcpy( dest , source , destlinesize ) ;
926 }
927 }
928
929 ret.EndRawAccess() ;
930
931 if ( M_BITMAPDATA->m_bitmapMask )
932 {
933 wxMemoryBuffer maskbuf ;
934 int rowBytes = ( destwidth + 3 ) & 0xFFFFFFC ;
935 size_t maskbufsize = rowBytes * destheight ;
936
937 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
938 int destlinesize = rowBytes ;
939
940 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
941 unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
942 wxASSERT( (source != NULL) && (destdata != NULL) ) ;
943
944 source += rect.x + rect.y * sourcelinesize ;
945 unsigned char *dest = destdata ;
946
947 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
948 {
949 memcpy( dest , source , destlinesize ) ;
950 }
951
952 maskbuf.UngetWriteBuf( maskbufsize ) ;
953 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
954 }
955 else if ( HasAlpha() )
956 ret.UseAlpha() ;
957
958 return ret;
959 }
960
961 bool wxBitmap::Create(int w, int h, int d)
962 {
963 UnRef();
964
965 if ( d < 0 )
966 d = wxDisplayDepth() ;
967
968 m_refData = new wxBitmapRefData( w , h , d );
969
970 return M_BITMAPDATA->Ok() ;
971 }
972
973 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
974 {
975 UnRef();
976
977 wxBitmapHandler *handler = FindHandler(type);
978
979 if ( handler )
980 {
981 m_refData = new wxBitmapRefData;
982
983 return handler->LoadFile(this, filename, type, -1, -1);
984 }
985 else
986 {
987 #if wxUSE_IMAGE
988 wxImage loadimage(filename, type);
989 if (loadimage.Ok())
990 {
991 *this = loadimage;
992
993 return true;
994 }
995 #endif
996 }
997
998 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
999
1000 return false;
1001 }
1002
1003 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
1004 {
1005 UnRef();
1006
1007 m_refData = new wxBitmapRefData;
1008
1009 wxBitmapHandler *handler = FindHandler(type);
1010
1011 if ( handler == NULL )
1012 {
1013 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1014
1015 return false;
1016 }
1017
1018 return handler->Create(this, data, type, width, height, depth);
1019 }
1020
1021 #if wxUSE_IMAGE
1022
1023 wxBitmap::wxBitmap(const wxImage& image, int depth)
1024 {
1025 wxCHECK_RET( image.Ok(), wxT("invalid image") )
1026
1027 // width and height of the device-dependent bitmap
1028 int width = image.GetWidth();
1029 int height = image.GetHeight();
1030
1031 m_refData = new wxBitmapRefData( width , height , depth ) ;
1032
1033 // Create picture
1034
1035 bool hasAlpha = false ;
1036
1037 if ( image.HasMask() )
1038 {
1039 // takes precedence, don't mix with alpha info
1040 }
1041 else
1042 {
1043 hasAlpha = image.HasAlpha() ;
1044 }
1045
1046 if ( hasAlpha )
1047 UseAlpha() ;
1048
1049 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
1050 register unsigned char* data = image.GetData();
1051 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
1052
1053 for (int y = 0; y < height; y++)
1054 {
1055 for (int x = 0; x < width; x++)
1056 {
1057 if ( hasAlpha )
1058 {
1059 const unsigned char a = *alpha++;
1060 *destination++ = a ;
1061
1062 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1063 *destination++ = ((*data++) * a + 127) / 255 ;
1064 *destination++ = ((*data++) * a + 127) / 255 ;
1065 *destination++ = ((*data++) * a + 127) / 255 ;
1066 #else
1067 *destination++ = *data++ ;
1068 *destination++ = *data++ ;
1069 *destination++ = *data++ ;
1070 #endif
1071 }
1072 else
1073 {
1074 *destination++ = 0xFF ;
1075 *destination++ = *data++ ;
1076 *destination++ = *data++ ;
1077 *destination++ = *data++ ;
1078 }
1079 }
1080 }
1081
1082 EndRawAccess() ;
1083 if ( image.HasMask() )
1084 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
1085 }
1086
1087 wxImage wxBitmap::ConvertToImage() const
1088 {
1089 wxImage image;
1090
1091 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1092
1093 // create an wxImage object
1094 int width = GetWidth();
1095 int height = GetHeight();
1096 image.Create( width, height );
1097
1098 unsigned char *data = image.GetData();
1099 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1100
1101 unsigned char* source = (unsigned char*) GetRawAccess() ;
1102
1103 bool hasAlpha = false ;
1104 bool hasMask = false ;
1105 int maskBytesPerRow = 0 ;
1106 unsigned char *alpha = NULL ;
1107 unsigned char *mask = NULL ;
1108
1109 if ( HasAlpha() )
1110 hasAlpha = true ;
1111
1112 if ( GetMask() )
1113 {
1114 hasMask = true ;
1115 mask = (unsigned char*) GetMask()->GetRawAccess() ;
1116 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
1117 }
1118
1119 if ( hasAlpha )
1120 {
1121 image.SetAlpha() ;
1122 alpha = image.GetAlpha() ;
1123 }
1124
1125 int index = 0;
1126
1127 // The following masking algorithm is the same as well in msw/gtk:
1128 // the colour used as transparent one in wxImage and the one it is
1129 // replaced with when it really occurs in the bitmap
1130 static const int MASK_RED = 1;
1131 static const int MASK_GREEN = 2;
1132 static const int MASK_BLUE = 3;
1133 static const int MASK_BLUE_REPLACEMENT = 2;
1134
1135 for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow )
1136 {
1137 unsigned char * maskp = mask ;
1138 unsigned char a, r, g, b;
1139 long color;
1140
1141 for (int xx = 0; xx < width; xx++)
1142 {
1143 color = *((long*) source) ;
1144 a = ((color&0xFF000000) >> 24) ;
1145 r = ((color&0x00FF0000) >> 16) ;
1146 g = ((color&0x0000FF00) >> 8) ;
1147 b = (color&0x000000FF);
1148
1149 if ( hasMask )
1150 {
1151 if ( *maskp++ == 0 )
1152 {
1153 r = MASK_RED ;
1154 g = MASK_GREEN ;
1155 b = MASK_BLUE ;
1156 }
1157 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1158 b = MASK_BLUE_REPLACEMENT ;
1159 }
1160 else if ( hasAlpha )
1161 *alpha++ = a ;
1162
1163 data[index ] = r ;
1164 data[index + 1] = g ;
1165 data[index + 2] = b ;
1166
1167 index += 3;
1168 source += 4 ;
1169 }
1170 }
1171
1172 if ( hasMask )
1173 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1174
1175 return image;
1176 }
1177
1178 #endif //wxUSE_IMAGE
1179
1180 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
1181 const wxPalette *palette) const
1182 {
1183 bool success = false;
1184 wxBitmapHandler *handler = FindHandler(type);
1185
1186 if ( handler )
1187 {
1188 success = handler->SaveFile(this, filename, type, palette);
1189 }
1190 else
1191 {
1192 #if wxUSE_IMAGE
1193 wxImage image = ConvertToImage();
1194 success = image.SaveFile(filename, type);
1195 #else
1196 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1197 #endif
1198 }
1199
1200 return success;
1201 }
1202
1203 bool wxBitmap::Ok() const
1204 {
1205 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
1206 }
1207
1208 int wxBitmap::GetHeight() const
1209 {
1210 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1211
1212 return M_BITMAPDATA->GetHeight();
1213 }
1214
1215 int wxBitmap::GetWidth() const
1216 {
1217 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1218
1219 return M_BITMAPDATA->GetWidth() ;
1220 }
1221
1222 int wxBitmap::GetDepth() const
1223 {
1224 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1225
1226 return M_BITMAPDATA->GetDepth();
1227 }
1228
1229 #if WXWIN_COMPATIBILITY_2_4
1230 int wxBitmap::GetQuality() const
1231 {
1232 return 0;
1233 }
1234
1235 void wxBitmap::SetQuality(int WXUNUSED(quality))
1236 {
1237 }
1238 #endif
1239
1240 wxMask *wxBitmap::GetMask() const
1241 {
1242 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1243
1244 return M_BITMAPDATA->m_bitmapMask;
1245 }
1246
1247 bool wxBitmap::HasAlpha() const
1248 {
1249 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1250
1251 return M_BITMAPDATA->HasAlpha() ;
1252 }
1253
1254 void wxBitmap::SetWidth(int w)
1255 {
1256 if (!M_BITMAPDATA)
1257 m_refData = new wxBitmapRefData;
1258
1259 M_BITMAPDATA->SetWidth(w);
1260 }
1261
1262 void wxBitmap::SetHeight(int h)
1263 {
1264 if (!M_BITMAPDATA)
1265 m_refData = new wxBitmapRefData;
1266
1267 M_BITMAPDATA->SetHeight(h);
1268 }
1269
1270 void wxBitmap::SetDepth(int d)
1271 {
1272 if (!M_BITMAPDATA)
1273 m_refData = new wxBitmapRefData;
1274
1275 M_BITMAPDATA->SetDepth(d);
1276 }
1277
1278 void wxBitmap::SetOk(bool isOk)
1279 {
1280 if (!M_BITMAPDATA)
1281 m_refData = new wxBitmapRefData;
1282
1283 M_BITMAPDATA->SetOk(isOk);
1284 }
1285
1286 #if wxUSE_PALETTE
1287 wxPalette *wxBitmap::GetPalette() const
1288 {
1289 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1290
1291 return &M_BITMAPDATA->m_bitmapPalette;
1292 }
1293
1294 void wxBitmap::SetPalette(const wxPalette& palette)
1295 {
1296 if (!M_BITMAPDATA)
1297 m_refData = new wxBitmapRefData;
1298
1299 M_BITMAPDATA->m_bitmapPalette = palette ;
1300 }
1301 #endif // wxUSE_PALETTE
1302
1303 void wxBitmap::SetMask(wxMask *mask)
1304 {
1305 if (!M_BITMAPDATA)
1306 m_refData = new wxBitmapRefData;
1307
1308 // Remove existing mask if there is one.
1309 delete M_BITMAPDATA->m_bitmapMask;
1310
1311 M_BITMAPDATA->m_bitmapMask = mask ;
1312 }
1313
1314 WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1315 {
1316 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
1317 }
1318
1319 // ----------------------------------------------------------------------------
1320 // wxMask
1321 // ----------------------------------------------------------------------------
1322
1323 wxMask::wxMask()
1324 {
1325 Init() ;
1326 }
1327
1328 // Construct a mask from a bitmap and a colour indicating
1329 // the transparent area
1330 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1331 {
1332 Init() ;
1333 Create(bitmap, colour);
1334 }
1335
1336 // Construct a mask from a mono bitmap (copies the bitmap).
1337 wxMask::wxMask(const wxBitmap& bitmap)
1338 {
1339 Init() ;
1340 Create(bitmap);
1341 }
1342
1343 // Construct a mask from a mono bitmap (copies the bitmap).
1344 wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1345 {
1346 Init() ;
1347 Create(data, width , height , bytesPerRow );
1348 }
1349
1350 wxMask::~wxMask()
1351 {
1352 if ( m_maskBitmap )
1353 {
1354 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1355 m_maskBitmap = NULL ;
1356 }
1357 }
1358
1359 void wxMask::Init()
1360 {
1361 m_width = m_height = m_bytesPerRow = 0 ;
1362 m_maskBitmap = NULL ;
1363 }
1364
1365 void *wxMask::GetRawAccess() const
1366 {
1367 return m_memBuf.GetData() ;
1368 }
1369
1370 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1371 // so no QD colorizing will occur when blitting
1372
1373 void wxMask::RealizeNative()
1374 {
1375 if ( m_maskBitmap )
1376 {
1377 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1378 m_maskBitmap = NULL ;
1379 }
1380
1381 Rect rect = { 0 , 0 , m_height , m_width } ;
1382 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_maskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 ,
1383 (char*) m_memBuf.GetData() , m_bytesPerRow ) ) ;
1384 }
1385
1386 // Create a mask from a mono bitmap (copies the bitmap).
1387 bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1388 {
1389 m_memBuf = data ;
1390 m_width = width ;
1391 m_height = height ;
1392 m_bytesPerRow = bytesPerRow ;
1393
1394 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1395
1396 RealizeNative() ;
1397
1398 return true ;
1399 }
1400
1401 // Create a mask from a mono bitmap (copies the bitmap).
1402 bool wxMask::Create(const wxBitmap& bitmap)
1403 {
1404 m_width = bitmap.GetWidth() ;
1405 m_height = bitmap.GetHeight() ;
1406 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1407
1408 size_t size = m_bytesPerRow * m_height ;
1409 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1410 wxASSERT( destdatabase != NULL ) ;
1411
1412 memset( destdatabase , 0 , size ) ;
1413 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1414
1415 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1416 {
1417 unsigned char *destdata = destdatabase ;
1418 unsigned char r, g, b;
1419
1420 for ( int x = 0 ; x < m_width ; ++x )
1421 {
1422 srcdata++ ;
1423 r = *srcdata++ ;
1424 g = *srcdata++ ;
1425 b = *srcdata++ ;
1426
1427 if ( ( r + g + b ) > 0x10 )
1428 *destdata++ = 0x00 ;
1429 else
1430 *destdata++ = 0xFF ;
1431 }
1432 }
1433
1434 m_memBuf.UngetWriteBuf( size ) ;
1435 RealizeNative() ;
1436
1437 return true;
1438 }
1439
1440 // Create a mask from a bitmap and a colour indicating
1441 // the transparent area
1442 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1443 {
1444 m_width = bitmap.GetWidth() ;
1445 m_height = bitmap.GetHeight() ;
1446 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1447
1448 size_t size = m_bytesPerRow * m_height ;
1449 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1450 wxASSERT( destdatabase != NULL ) ;
1451
1452 memset( destdatabase , 0 , size ) ;
1453 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1454
1455 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
1456 {
1457 unsigned char *destdata = destdatabase ;
1458 unsigned char r, g, b;
1459
1460 for ( int x = 0 ; x < m_width ; ++x )
1461 {
1462 srcdata++ ;
1463 r = *srcdata++ ;
1464 g = *srcdata++ ;
1465 b = *srcdata++ ;
1466
1467 if ( colour == wxColour( r , g , b) )
1468 *destdata++ = 0x00 ;
1469 else
1470 *destdata++ = 0xFF ;
1471 }
1472 }
1473
1474 m_memBuf.UngetWriteBuf( size ) ;
1475 RealizeNative() ;
1476
1477 return true;
1478 }
1479
1480 WXHBITMAP wxMask::GetHBITMAP() const
1481 {
1482 return m_maskBitmap ;
1483 }
1484
1485 // ----------------------------------------------------------------------------
1486 // wxBitmapHandler
1487 // ----------------------------------------------------------------------------
1488
1489 wxBitmapHandler::~wxBitmapHandler()
1490 {
1491 }
1492
1493 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1494 {
1495 return false;
1496 }
1497
1498 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1499 int desiredWidth, int desiredHeight)
1500 {
1501 return false;
1502 }
1503
1504 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1505 {
1506 return false;
1507 }
1508
1509 // ----------------------------------------------------------------------------
1510 // Standard Handlers
1511 // ----------------------------------------------------------------------------
1512
1513 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1514 {
1515 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1516
1517 public:
1518 inline wxPICTResourceHandler()
1519 {
1520 SetName(wxT("Macintosh Pict resource"));
1521 SetExtension(wxEmptyString);
1522 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
1523 };
1524
1525 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1526 int desiredWidth, int desiredHeight);
1527 };
1528 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1529
1530
1531 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1532 int desiredWidth, int desiredHeight)
1533 {
1534 #if wxUSE_METAFILE
1535 Str255 theName ;
1536 wxMacStringToPascal( name , theName ) ;
1537
1538 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1539 if ( thePict )
1540 {
1541 wxMetafile mf ;
1542 mf.SetHMETAFILE( (WXHMETAFILE) thePict ) ;
1543 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1544 wxMemoryDC dc ;
1545 dc.SelectObject( *bitmap ) ;
1546 mf.Play( &dc ) ;
1547 dc.SelectObject( wxNullBitmap ) ;
1548
1549 return true ;
1550 }
1551 #endif //wxUSE_METAFILE
1552
1553 return false ;
1554 }
1555
1556 void wxBitmap::InitStandardHandlers()
1557 {
1558 AddHandler( new wxPICTResourceHandler ) ;
1559 AddHandler( new wxICONResourceHandler ) ;
1560 }
1561
1562 // ----------------------------------------------------------------------------
1563 // raw bitmap access support
1564 // ----------------------------------------------------------------------------
1565
1566 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1567 {
1568 if ( !Ok() )
1569 // no bitmap, no data (raw or otherwise)
1570 return NULL;
1571
1572 data.m_width = GetWidth() ;
1573 data.m_height = GetHeight() ;
1574 data.m_stride = GetWidth() * 4 ;
1575
1576 return GetRawAccess() ;
1577 }
1578
1579 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1580 {
1581 if ( !Ok() )
1582 return;
1583
1584 // TODO : if we have some information about the API we should check
1585 // this code looks strange...
1586
1587 if ( !M_BITMAPDATA->HasAlpha() )
1588 return;
1589
1590 wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;
1591 int w = data.GetWidth();
1592 int h = data.GetHeight();
1593
1594 wxBitmap bmpMask( GetWidth(), GetHeight(), 32 );
1595 wxAlphaPixelData dataMask( bmpMask, data.GetOrigin(), wxSize( w, h ) );
1596 wxAlphaPixelData::Iterator pMask( dataMask ), p( data );
1597
1598 for ( int y = 0; y < h; y++ )
1599 {
1600 wxAlphaPixelData::Iterator rowStartMask = pMask;
1601 wxAlphaPixelData::Iterator rowStart = p;
1602
1603 for ( int x = 0; x < w; x++ )
1604 {
1605 const wxAlphaPixelData::Iterator::ChannelType alpha = p.Alpha();
1606
1607 pMask.Red() = alpha;
1608 pMask.Green() = alpha;
1609 pMask.Blue() = alpha;
1610
1611 ++p;
1612 ++pMask;
1613 }
1614
1615 p = rowStart;
1616 p.OffsetY( data, 1 );
1617
1618 pMask = rowStartMask;
1619 pMask.OffsetY( dataMask, 1 );
1620 }
1621
1622 SetMask( new wxMask( bmpMask ) );
1623 }
1624
1625 void wxBitmap::UseAlpha()
1626 {
1627 // remember that we are using alpha channel:
1628 // we'll need to create a proper mask in UngetRawData()
1629 M_BITMAPDATA->UseAlpha( true );
1630 }