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