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