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