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