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