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