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