]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/bitmap.cpp
get rid of one of the extra slashes in the download URLs
[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 #if WXWIN_COMPATIBILITY_2_4
1290 int wxBitmap::GetQuality() const
1291 {
1292 return 0;
1293 }
1294
1295 void wxBitmap::SetQuality(int WXUNUSED(quality))
1296 {
1297 }
1298 #endif
1299
1300 wxMask *wxBitmap::GetMask() const
1301 {
1302 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1303
1304 return M_BITMAPDATA->m_bitmapMask;
1305 }
1306
1307 bool wxBitmap::HasAlpha() const
1308 {
1309 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1310
1311 return M_BITMAPDATA->HasAlpha() ;
1312 }
1313
1314 void wxBitmap::SetWidth(int w)
1315 {
1316 AllocExclusive();
1317 M_BITMAPDATA->SetWidth(w);
1318 }
1319
1320 void wxBitmap::SetHeight(int h)
1321 {
1322 AllocExclusive();
1323 M_BITMAPDATA->SetHeight(h);
1324 }
1325
1326 void wxBitmap::SetDepth(int d)
1327 {
1328 AllocExclusive();
1329 M_BITMAPDATA->SetDepth(d);
1330 }
1331
1332 void wxBitmap::SetOk(bool isOk)
1333 {
1334 AllocExclusive();
1335 M_BITMAPDATA->SetOk(isOk);
1336 }
1337
1338 #if wxUSE_PALETTE
1339 wxPalette *wxBitmap::GetPalette() const
1340 {
1341 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1342
1343 return &M_BITMAPDATA->m_bitmapPalette;
1344 }
1345
1346 void wxBitmap::SetPalette(const wxPalette& palette)
1347 {
1348 AllocExclusive();
1349 M_BITMAPDATA->m_bitmapPalette = palette ;
1350 }
1351 #endif // wxUSE_PALETTE
1352
1353 void wxBitmap::SetMask(wxMask *mask)
1354 {
1355 AllocExclusive();
1356 // Remove existing mask if there is one.
1357 delete M_BITMAPDATA->m_bitmapMask;
1358
1359 M_BITMAPDATA->m_bitmapMask = mask ;
1360 }
1361
1362 WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1363 {
1364 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
1365 }
1366
1367 // ----------------------------------------------------------------------------
1368 // wxMask
1369 // ----------------------------------------------------------------------------
1370
1371 wxMask::wxMask()
1372 {
1373 Init() ;
1374 }
1375
1376 wxMask::wxMask(const wxMask &tocopy)
1377 {
1378 Init();
1379
1380 m_bytesPerRow = tocopy.m_bytesPerRow;
1381 m_width = tocopy.m_width;
1382 m_height = tocopy.m_height;
1383
1384 size_t size = m_bytesPerRow * m_height;
1385 unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size );
1386 unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData();
1387 for (size_t i=0; i<size; i++)
1388 {
1389 *dest++ = *source++;
1390 }
1391
1392 m_memBuf.UngetWriteBuf( size ) ;
1393 RealizeNative() ;
1394 }
1395
1396 // Construct a mask from a bitmap and a colour indicating
1397 // the transparent area
1398 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
1399 {
1400 Init() ;
1401 Create( bitmap, colour );
1402 }
1403
1404 // Construct a mask from a mono bitmap (copies the bitmap).
1405 wxMask::wxMask( const wxBitmap& bitmap )
1406 {
1407 Init() ;
1408 Create( bitmap );
1409 }
1410
1411 // Construct a mask from a mono bitmap (copies the bitmap).
1412
1413 wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1414 {
1415 Init() ;
1416 Create( data, width , height , bytesPerRow );
1417 }
1418
1419 wxMask::~wxMask()
1420 {
1421 #ifndef __LP64__
1422 if ( m_maskBitmap )
1423 {
1424 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
1425 m_maskBitmap = NULL ;
1426 }
1427 #endif
1428 }
1429
1430 void wxMask::Init()
1431 {
1432 m_width = m_height = m_bytesPerRow = 0 ;
1433 m_maskBitmap = NULL ;
1434 }
1435
1436 void *wxMask::GetRawAccess() const
1437 {
1438 return m_memBuf.GetData() ;
1439 }
1440
1441 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1442 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1443
1444 void wxMask::RealizeNative()
1445 {
1446 #ifndef __LP64__
1447 if ( m_maskBitmap )
1448 {
1449 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
1450 m_maskBitmap = NULL ;
1451 }
1452
1453 Rect rect = { 0 , 0 , m_height , m_width } ;
1454
1455 OSStatus err = NewGWorldFromPtr(
1456 (GWorldPtr*) &m_maskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
1457 (char*) m_memBuf.GetData() , m_bytesPerRow ) ;
1458 verify_noerr( err ) ;
1459 #endif
1460 }
1461
1462 // Create a mask from a mono bitmap (copies the bitmap).
1463
1464 bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1465 {
1466 m_memBuf = data ;
1467 m_width = width ;
1468 m_height = height ;
1469 m_bytesPerRow = bytesPerRow ;
1470
1471 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1472
1473 RealizeNative() ;
1474
1475 return true ;
1476 }
1477
1478 // Create a mask from a mono bitmap (copies the bitmap).
1479 bool wxMask::Create(const wxBitmap& bitmap)
1480 {
1481 m_width = bitmap.GetWidth() ;
1482 m_height = bitmap.GetHeight() ;
1483 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
1484
1485 size_t size = m_bytesPerRow * m_height ;
1486 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1487 wxASSERT( destdatabase != NULL ) ;
1488
1489 memset( destdatabase , 0 , size ) ;
1490 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1491
1492 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1493 {
1494 unsigned char *destdata = destdatabase ;
1495 unsigned char r, g, b;
1496
1497 for ( int x = 0 ; x < m_width ; ++x )
1498 {
1499 srcdata++ ;
1500 r = *srcdata++ ;
1501 g = *srcdata++ ;
1502 b = *srcdata++ ;
1503
1504 if ( ( r + g + b ) > 0x10 )
1505 {
1506 *destdata++ = 0xFF ;
1507 *destdata++ = 0xFF ;
1508 *destdata++ = 0xFF ;
1509 *destdata++ = 0xFF ;
1510 }
1511 else
1512 {
1513 *destdata++ = 0x00 ;
1514 *destdata++ = 0x00 ;
1515 *destdata++ = 0x00 ;
1516 *destdata++ = 0x00 ;
1517 }
1518 }
1519 }
1520
1521 m_memBuf.UngetWriteBuf( size ) ;
1522 RealizeNative() ;
1523
1524 return true;
1525 }
1526
1527 // Create a mask from a bitmap and a colour indicating
1528 // the transparent area
1529 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1530 {
1531 m_width = bitmap.GetWidth() ;
1532 m_height = bitmap.GetHeight() ;
1533 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
1534
1535 size_t size = m_bytesPerRow * m_height ;
1536 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1537 wxASSERT( destdatabase != NULL ) ;
1538
1539 memset( destdatabase , 0 , size ) ;
1540 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1541
1542 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
1543 {
1544 unsigned char *destdata = destdatabase ;
1545 unsigned char r, g, b;
1546
1547 for ( int x = 0 ; x < m_width ; ++x )
1548 {
1549 srcdata++ ;
1550 r = *srcdata++ ;
1551 g = *srcdata++ ;
1552 b = *srcdata++ ;
1553
1554 if ( colour == wxColour( r , g , b ) )
1555 {
1556 *destdata++ = 0xFF ;
1557 *destdata++ = 0xFF ;
1558 *destdata++ = 0xFF ;
1559 *destdata++ = 0xFF ;
1560 }
1561 else
1562 {
1563 *destdata++ = 0x00 ;
1564 *destdata++ = 0x00 ;
1565 *destdata++ = 0x00 ;
1566 *destdata++ = 0x00 ;
1567 }
1568 }
1569 }
1570
1571 m_memBuf.UngetWriteBuf( size ) ;
1572 RealizeNative() ;
1573
1574 return true;
1575 }
1576
1577 WXHBITMAP wxMask::GetHBITMAP() const
1578 {
1579 return m_maskBitmap ;
1580 }
1581
1582 // ----------------------------------------------------------------------------
1583 // wxBitmapHandler
1584 // ----------------------------------------------------------------------------
1585
1586 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
1587
1588 // ----------------------------------------------------------------------------
1589 // Standard Handlers
1590 // ----------------------------------------------------------------------------
1591
1592 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1593 {
1594 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1595
1596 public:
1597 inline wxPICTResourceHandler()
1598 {
1599 SetName(wxT("Macintosh Pict resource"));
1600 SetExtension(wxEmptyString);
1601 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
1602 };
1603
1604 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1605 int desiredWidth, int desiredHeight);
1606 };
1607
1608 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1609
1610
1611 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1612 int desiredWidth, int desiredHeight)
1613 {
1614 #if wxUSE_METAFILE
1615 Str255 theName ;
1616 wxMacStringToPascal( name , theName ) ;
1617
1618 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1619 if ( thePict )
1620 {
1621 wxMetafile mf ;
1622
1623 mf.SetHMETAFILE( (WXHMETAFILE) thePict ) ;
1624 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1625 wxMemoryDC dc ;
1626 dc.SelectObject( *bitmap ) ;
1627 mf.Play( &dc ) ;
1628 dc.SelectObject( wxNullBitmap ) ;
1629
1630 return true ;
1631 }
1632 #endif
1633
1634 return false ;
1635 }
1636
1637 void wxBitmap::InitStandardHandlers()
1638 {
1639 AddHandler( new wxPICTResourceHandler ) ;
1640 AddHandler( new wxICONResourceHandler ) ;
1641 }
1642
1643 // ----------------------------------------------------------------------------
1644 // raw bitmap access support
1645 // ----------------------------------------------------------------------------
1646
1647 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1648 {
1649 if ( !Ok() )
1650 // no bitmap, no data (raw or otherwise)
1651 return NULL;
1652
1653 data.m_width = GetWidth() ;
1654 data.m_height = GetHeight() ;
1655 data.m_stride = GetWidth() * 4 ;
1656
1657 return BeginRawAccess() ;
1658 }
1659
1660 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1661 {
1662 EndRawAccess() ;
1663 }
1664
1665 void wxBitmap::UseAlpha()
1666 {
1667 // remember that we are using alpha channel:
1668 // we'll need to create a proper mask in UngetRawData()
1669 M_BITMAPDATA->UseAlpha( true );
1670 }