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