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