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