]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/bitmap.cpp
wxPalette unified. Source cleaning.
[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
35 #include "wx/dcmemory.h"
36
37 // Implementation Notes
38 // --------------------
39 //
40 // we are always working with a 32 bit deep pixel buffer
41 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
42 // therefore we have a separate GWorld there for blitting the mask in
43
44 // under Quartz then content is transformed into a CGImageRef representing the same data
45 // which can be transferred to the GPU by the OS for fast rendering
46
47 // we don't dare premultiplied alpha yet
48 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
49
50 #if wxUSE_BMPBUTTON
51
52 void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
53 {
54 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
55 if ( bitmap.Ok() )
56 {
57 wxBitmapRefData * bmap = bitmap.GetBitmapData() ;
58 if ( bmap == NULL )
59 return ;
60
61 if ( ( bmap->HasNativeSize() && forceType == 0 ) || forceType == kControlContentIconRef )
62 {
63 wxBitmap scaleBmp ;
64
65 wxBitmapRefData* bmp = bmap ;
66
67 if ( !bmap->HasNativeSize() )
68 {
69 // as PICT conversion will only result in a 16x16 icon, let's attempt
70 // a few scales for better results
71
72 int w = bitmap.GetWidth() ;
73 int h = bitmap.GetHeight() ;
74 int sz = wxMax( w , h ) ;
75 if ( sz == 24 || sz == 64)
76 {
77 scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ;
78 bmp = scaleBmp.GetBitmapData() ;
79 }
80 }
81
82 info->contentType = kControlContentIconRef ;
83 info->u.iconRef = bmp->GetIconRef() ;
84 AcquireIconRef( info->u.iconRef ) ;
85 }
86 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
87 else if ( forceType == kControlContentCGImageRef )
88 {
89 info->contentType = kControlContentCGImageRef ;
90 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
91 }
92 #endif
93 else
94 {
95 info->contentType = kControlContentPictHandle ;
96 info->u.picture = bmap->GetPictHandle() ;
97 }
98 }
99 }
100
101 void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
102 {
103 if ( info->contentType == kControlContentIconRef )
104 {
105 ReleaseIconRef( info->u.iconRef ) ;
106 }
107 else if ( info->contentType == kControlNoContent )
108 {
109 // there's no bitmap at all, fall through silently
110 }
111 else if ( info->contentType == kControlContentPictHandle )
112 {
113 // owned by the bitmap, no release here
114 }
115 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
116 else if ( info->contentType == kControlContentCGImageRef )
117 {
118 CGImageRelease( info->u.imageRef ) ;
119 }
120 #endif
121 else
122 {
123 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
124 }
125 }
126
127 #endif //wxUSE_BMPBUTTON
128
129 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
130
131 void wxBitmapRefData::Init()
132 {
133 m_width = 0 ;
134 m_height = 0 ;
135 m_depth = 0 ;
136 m_ok = false ;
137 m_bitmapMask = NULL ;
138 #if wxMAC_USE_CORE_GRAPHICS
139 m_cgImageRef = NULL ;
140 #endif
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 m_ok = ( m_hBitmap != NULL ) ;
180
181 return m_ok ;
182 }
183
184 void wxBitmapRefData::UseAlpha( bool use )
185 {
186 if ( m_hasAlpha == use )
187 return ;
188
189 m_hasAlpha = use ;
190 if ( m_hasAlpha )
191 {
192 int width = GetWidth() ;
193 int height = GetHeight() ;
194 m_maskBytesPerRow = ( width * 4 + 3 ) & 0xFFFFFFC ;
195 size_t size = height * m_maskBytesPerRow ;
196 unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ;
197 memset( data , 0 , size ) ;
198 wxASSERT( m_hMaskBitmap == NULL ) ;
199 Rect rect = { 0 , 0 , height , width } ;
200 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
201 (char*) data , m_maskBytesPerRow ) ) ;
202 wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ;
203 m_maskMemBuf.UngetWriteBuf(size) ;
204 #if !wxMAC_USE_CORE_GRAPHICS
205 UpdateAlphaMask() ;
206 #endif
207 }
208 else
209 {
210 DisposeGWorld( m_hMaskBitmap ) ;
211 m_hMaskBitmap = NULL ;
212 m_maskBytesPerRow = 0 ;
213 }
214 }
215
216 void *wxBitmapRefData::GetRawAccess() const
217 {
218 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
219 return m_memBuf.GetData() ;
220 }
221
222 void *wxBitmapRefData::BeginRawAccess()
223 {
224 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
225 wxASSERT( m_rawAccessCount == 0 ) ;
226 ++m_rawAccessCount ;
227 // we must destroy an existing cached image, as
228 // the bitmap data may change now
229 wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
230 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
231 #if wxMAC_USE_CORE_GRAPHICS
232 if ( m_cgImageRef )
233 {
234 CGImageRelease( m_cgImageRef ) ;
235 m_cgImageRef = NULL ;
236 }
237 #endif
238 return m_memBuf.GetData() ;
239 }
240
241 void wxBitmapRefData::EndRawAccess()
242 {
243 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
244 wxASSERT( m_rawAccessCount == 1 ) ;
245 --m_rawAccessCount ;
246 #if !wxMAC_USE_CORE_GRAPHICS
247 UpdateAlphaMask() ;
248 #endif
249 }
250
251 bool wxBitmapRefData::HasNativeSize()
252 {
253 int w = GetWidth() ;
254 int h = GetHeight() ;
255 int sz = wxMax( w , h ) ;
256
257 if ( sz == 128 || sz == 48 || sz == 32 || sz == 16 )
258 return true ;
259 return false ;
260 }
261
262 IconRef wxBitmapRefData::GetIconRef()
263 {
264 if ( m_iconRef == NULL )
265 {
266 // Create Icon Family Handle
267
268 IconFamilyHandle iconFamily = NULL ;
269
270 #ifdef WORDS_BIGENDIAN
271 iconFamily = (IconFamilyHandle) NewHandle(8) ;
272 (**iconFamily).resourceType = kIconFamilyType ;
273 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
274 #else
275 // test this solution on big endian as well
276 iconFamily = (IconFamilyHandle) NewHandle(0) ;
277 #endif
278
279 int w = GetWidth() ;
280 int h = GetHeight() ;
281 int sz = wxMax( w , h ) ;
282
283 OSType dataType = 0 ;
284 OSType maskType = 0 ;
285
286 if ( sz == 128 )
287 {
288 dataType = kThumbnail32BitData ;
289 maskType = kThumbnail8BitMask ;
290 }
291 else if ( sz == 48 )
292 {
293 dataType = kHuge32BitData ;
294 maskType = kHuge8BitMask ;
295 }
296 else if ( sz == 32 )
297 {
298 dataType = kLarge32BitData ;
299 maskType = kLarge8BitMask ;
300 }
301 else if ( sz == 16 )
302 {
303 dataType = kSmall32BitData ;
304 maskType = kSmall8BitMask ;
305 }
306
307 if ( dataType != 0 )
308 {
309 // setup the header properly
310
311 Handle data = NULL ;
312 Handle maskdata = NULL ;
313 unsigned char * maskptr = NULL ;
314 unsigned char * ptr = NULL ;
315 size_t size ;
316 size_t masksize ;
317
318 size = sz * sz * 4 ;
319 data = NewHandle( size) ;
320 HLock( data ) ;
321 ptr = (unsigned char*) *data ;
322 memset( ptr , 0, size ) ;
323
324 masksize = sz * sz ;
325 maskdata = NewHandle( masksize ) ;
326 HLock( maskdata ) ;
327 maskptr = (unsigned char*) *maskdata ;
328 memset( maskptr , 0 , masksize ) ;
329
330 bool hasAlpha = HasAlpha() ;
331 wxMask *mask = m_bitmapMask ;
332 unsigned char * source = (unsigned char*) GetRawAccess() ;
333 unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
334 for ( int y = 0 ; y < h ; ++y )
335 {
336 unsigned char * dest = ptr + y * sz * 4 ;
337 unsigned char * maskdest = maskptr + y * sz ;
338 for ( int x = 0 ; x < w ; ++x )
339 {
340 unsigned char a = *source ++ ;
341 unsigned char r = *source ++ ;
342 unsigned char g = *source ++ ;
343 unsigned char b = *source ++ ;
344
345 *dest++ = 0 ;
346 *dest++ = r ;
347 *dest++ = g ;
348 *dest++ = b ;
349
350 if ( mask )
351 *maskdest++ = *masksource++ ;
352 else if ( hasAlpha )
353 *maskdest++ = a ;
354 else
355 *maskdest++ = 0xFF ;
356 }
357 }
358
359 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
360 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
361
362 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
363 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
364 HUnlock( data ) ;
365 HUnlock( maskdata ) ;
366 DisposeHandle( data ) ;
367 DisposeHandle( maskdata ) ;
368 }
369 else
370 {
371 PicHandle pic = GetPictHandle() ;
372 SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
373 }
374
375 // transform into IconRef
376
377 static int iconCounter = 2 ;
378 #ifdef __WXDEBUG__
379 OSStatus err =
380 #endif
381 RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ;
382 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
383 // we have to retain a reference, as Unregister will decrement it
384 AcquireIconRef( m_iconRef ) ;
385 UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
386 DisposeHandle( (Handle) iconFamily ) ;
387 ++iconCounter ;
388 }
389 return m_iconRef ;
390 }
391
392 PicHandle wxBitmapRefData::GetPictHandle()
393 {
394 if ( m_pictHandle == NULL )
395 {
396 CGrafPtr origPort = NULL ;
397 GDHandle origDev = NULL ;
398 GWorldPtr wp = NULL ;
399 GWorldPtr mask = NULL ;
400 int height = GetHeight() ;
401 int width = GetWidth() ;
402
403 Rect rect = { 0 , 0 , height , width } ;
404
405 GetGWorld( &origPort , &origDev ) ;
406
407 wp = GetHBITMAP( &mask ) ;
408
409 RgnHandle clipRgn = NULL ;
410
411 if ( mask )
412 {
413 GWorldPtr monoworld ;
414 clipRgn = NewRgn() ;
415 OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
416 verify_noerr(err) ;
417 LockPixels( GetGWorldPixMap( monoworld ) ) ;
418 LockPixels( GetGWorldPixMap( mask ) ) ;
419 SetGWorld( monoworld , NULL ) ;
420 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
421 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
422 RGBForeColor( &black ) ;
423 RGBBackColor( &white ) ;
424 CopyBits(GetPortBitMapForCopyBits(mask),
425 GetPortBitMapForCopyBits(monoworld),
426 &rect,
427 &rect,
428 srcCopy,NULL);
429 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
430 UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
431 UnlockPixels( GetGWorldPixMap( mask ) ) ;
432 DisposeGWorld( monoworld ) ;
433 }
434
435 SetGWorld( wp , NULL ) ;
436 Rect portRect ;
437 GetPortBounds( wp , &portRect ) ;
438 m_pictHandle = OpenPicture(&portRect);
439
440 if(m_pictHandle)
441 {
442 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
443 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
444 RGBForeColor( &black ) ;
445 RGBBackColor( &white ) ;
446
447 if ( clipRgn )
448 SetClip( clipRgn ) ;
449
450 LockPixels( GetGWorldPixMap( wp ) ) ;
451 CopyBits(GetPortBitMapForCopyBits(wp),
452 GetPortBitMapForCopyBits(wp),
453 &portRect,
454 &portRect,
455 srcCopy,clipRgn);
456 UnlockPixels( GetGWorldPixMap( wp ) ) ;
457 ClosePicture();
458 }
459 SetGWorld( origPort , origDev ) ;
460 if ( clipRgn )
461 DisposeRgn( clipRgn ) ;
462 }
463 return m_pictHandle ;
464 }
465
466 #ifdef __WXMAC_OSX__
467 void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
468 {
469 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
470 wxASSERT( data == membuf->GetData() ) ;
471 delete membuf ;
472 }
473
474 CGImageRef wxBitmapRefData::CGImageCreate() const
475 {
476 wxASSERT( m_ok ) ;
477 wxASSERT( m_rawAccessCount >= 0 ) ;
478 CGImageRef image ;
479 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
480 {
481 size_t imageSize = m_width * m_height * 4 ;
482 void * dataBuffer = m_memBuf.GetData() ;
483 int w = m_width ;
484 int h = m_height ;
485 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
486 wxMemoryBuffer* membuf = NULL ;
487
488 if ( m_bitmapMask )
489 {
490 membuf = new wxMemoryBuffer( imageSize ) ;
491 memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
492 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
493 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
494 unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
495 alphaInfo = kCGImageAlphaFirst ;
496 for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
497 {
498 unsigned char *sourcemask = sourcemaskstart ;
499 for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
500 {
501 *destalpha = *sourcemask ;
502 }
503 }
504 }
505 else if ( m_hasAlpha )
506 {
507 #if wxMAC_USE_PREMULTIPLIED_ALPHA
508 alphaInfo = kCGImageAlphaPremultipliedFirst ;
509 #else
510 alphaInfo = kCGImageAlphaFirst ;
511 #endif
512 membuf = new wxMemoryBuffer( m_memBuf ) ;
513 }
514 else
515 {
516 membuf = new wxMemoryBuffer( m_memBuf ) ;
517 }
518 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
519 CGDataProviderRef dataProvider =
520 CGDataProviderCreateWithData( membuf , (const void *)membuf->GetData() , imageSize,
521 wxMacMemoryBufferReleaseProc );
522 image =
523 ::CGImageCreate( w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
524 dataProvider, NULL , false , kCGRenderingIntentDefault );
525 CGDataProviderRelease( dataProvider);
526 }
527 else
528 {
529 image = m_cgImageRef ;
530 CGImageRetain( image ) ;
531 }
532 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
533 {
534 // we keep it for later use
535 m_cgImageRef = image ;
536 CGImageRetain( image ) ;
537 }
538 return image ;
539 }
540 #endif
541
542 GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
543 {
544 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
545 if ( mask )
546 {
547 *mask = NULL ;
548 if ( m_bitmapMask )
549 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
550 else if ( m_hasAlpha )
551 {
552 #if !wxMAC_USE_CORE_GRAPHICS
553 if ( m_rawAccessCount > 0 )
554 UpdateAlphaMask() ;
555 #else
556 // this structure is not kept in synch when using CG, so if someone
557 // is really accessing the Graphports, we have to sync it
558 UpdateAlphaMask() ;
559 #endif
560 *mask = m_hMaskBitmap ;
561 }
562 }
563 return m_hBitmap ;
564 }
565
566 void wxBitmapRefData::UpdateAlphaMask() const
567 {
568 if ( m_hasAlpha )
569 {
570 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
571 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
572
573 int h = GetHeight() ;
574 int w = GetWidth() ;
575
576 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
577 {
578 unsigned char* destalpha = destalphabase ;
579 for( int x = 0 ; x < w ; ++x , sourcemask += 4 )
580 {
581 // we must have 24 bit depth for non quartz smooth alpha
582 *destalpha++ = 255 ;
583 *destalpha++ = 255 - *sourcemask ;
584 *destalpha++ = 255 - *sourcemask ;
585 *destalpha++ = 255 - *sourcemask ;
586 }
587 }
588 }
589 }
590
591 void wxBitmapRefData::Free()
592 {
593 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
594
595 #if wxMAC_USE_CORE_GRAPHICS
596 if ( m_cgImageRef )
597 {
598 CGImageRelease( m_cgImageRef ) ;
599 m_cgImageRef = NULL ;
600 }
601 #endif
602 if ( m_iconRef )
603 {
604 ReleaseIconRef( m_iconRef ) ;
605 m_iconRef = NULL ;
606 }
607 if ( m_pictHandle )
608 {
609 KillPicture( m_pictHandle ) ;
610 m_pictHandle = NULL ;
611 }
612 if ( m_hBitmap )
613 {
614 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
615 m_hBitmap = NULL ;
616 }
617 if ( m_hMaskBitmap )
618 {
619 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
620 m_hMaskBitmap = NULL ;
621 }
622
623 if (m_bitmapMask)
624 {
625 delete m_bitmapMask;
626 m_bitmapMask = NULL;
627 }
628 }
629
630 wxBitmapRefData::~wxBitmapRefData()
631 {
632 Free() ;
633 }
634
635 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
636 {
637 int w = icon.GetWidth() ;
638 int h = icon.GetHeight() ;
639
640
641 Create( icon.GetWidth() , icon.GetHeight() ) ;
642
643 bool created = false ;
644
645 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
646 {
647 IconFamilyHandle iconFamily = NULL ;
648 Handle imagehandle = NewHandle(0) ;
649 Handle maskhandle = NewHandle(0) ;
650
651 OSType maskType = 0;
652 OSType dataType = 0;
653 IconSelectorValue selector = 0 ;
654 if ( w == 128 )
655 {
656 dataType = kThumbnail32BitData ;
657 maskType = kThumbnail8BitMask ;
658 selector = kSelectorAllAvailableData ;
659 }
660 else if ( w == 48 )
661 {
662 dataType = kHuge32BitData ;
663 maskType = kHuge8BitMask ;
664 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
665 }
666 else if ( w == 32 )
667 {
668 dataType = kLarge32BitData ;
669 maskType = kLarge8BitMask ;
670 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
671 }
672 else if ( w == 16 )
673 {
674 dataType = kSmall32BitData ;
675 maskType = kSmall8BitMask ;
676 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
677 }
678
679
680 OSStatus err = ( IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ) ;
681
682 err =( GetIconFamilyData( iconFamily , dataType , imagehandle ) ) ;
683 err =( GetIconFamilyData( iconFamily , maskType , maskhandle ) ) ;
684 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
685 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
686
687 if ( imagehandlesize != 0 && maskhandlesize != 0 )
688 {
689 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
690 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
691 UseAlpha() ;
692 unsigned char *source = (unsigned char *) *imagehandle ;
693 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
694
695 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
696 for ( int y = 0 ; y < h ; ++y )
697 {
698 for ( int x = 0 ; x < w ; ++x )
699 {
700 *destination++ = *sourcemask++ ;
701 source++ ;
702 *destination++ = *source++ ;
703 *destination++ = *source++ ;
704 *destination++ = *source++ ;
705 }
706 }
707 EndRawAccess() ;
708 DisposeHandle( imagehandle ) ;
709 DisposeHandle( maskhandle ) ;
710 created = true ;
711 }
712 DisposeHandle( (Handle) iconFamily ) ;
713
714 }
715
716 if ( !created )
717 {
718 wxMemoryDC dc ;
719 dc.SelectObject( *this ) ;
720 dc.DrawIcon( icon , 0 , 0 ) ;
721 dc.SelectObject( wxNullBitmap ) ;
722 }
723 return true;
724 }
725
726 wxBitmap::wxBitmap()
727 {
728 }
729
730 wxBitmap::~wxBitmap()
731 {
732 }
733
734 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
735 {
736 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
737
738 if ( no_bits == 1 )
739 {
740 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
741 if ( the_width % (sizeof(unsigned char) * 8) ) {
742 linesize += sizeof(unsigned char);
743 }
744 unsigned char* linestart = (unsigned char*) bits ;
745 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
746 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
747 {
748 for ( int x = 0 ; x < the_width ; ++x )
749 {
750 int index = x / 8 ;
751 int bit = x % 8 ;
752 int mask = 1 << bit ;
753 if ( linestart[index] & mask )
754 {
755 *destination++ = 0xFF ;
756 *destination++ = 0 ;
757 *destination++ = 0 ;
758 *destination++ = 0 ;
759 }
760 else
761 {
762 *destination++ = 0xFF ;
763 *destination++ = 0xFF ;
764 *destination++ = 0xFF ;
765 *destination++ = 0xFF ;
766 }
767 }
768 }
769 EndRawAccess() ;
770 }
771 else
772 {
773 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
774 }
775 }
776
777 wxBitmap::wxBitmap(int w, int h, int d)
778 {
779 (void)Create(w, h, d);
780 }
781
782 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
783 {
784 (void) Create(data, type, width, height, depth);
785 }
786
787 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
788 {
789 LoadFile(filename, type);
790 }
791
792 wxBitmap::wxBitmap(const char **bits)
793 {
794 (void) CreateFromXpm(bits);
795 }
796
797 wxBitmap::wxBitmap(char **bits)
798 {
799 (void) CreateFromXpm((const char **)bits);
800 }
801
802 void* wxBitmap::GetRawAccess() const
803 {
804 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
805 return M_BITMAPDATA->GetRawAccess() ;
806 }
807
808 void* wxBitmap::BeginRawAccess()
809 {
810 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
811 return M_BITMAPDATA->BeginRawAccess() ;
812 }
813
814 void wxBitmap::EndRawAccess()
815 {
816 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
817 M_BITMAPDATA->EndRawAccess() ;
818 }
819
820 bool wxBitmap::CreateFromXpm(const char **bits)
821 {
822 #if wxUSE_IMAGE
823 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") )
824 wxXPMDecoder decoder;
825 wxImage img = decoder.ReadData(bits);
826 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") )
827 *this = wxBitmap(img);
828 return true;
829 #else
830 return false;
831 #endif
832 }
833
834 #ifdef __WXMAC_OSX__
835 WXCGIMAGEREF wxBitmap::CGImageCreate() const
836 {
837 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
838 return M_BITMAPDATA->CGImageCreate() ;
839 }
840 #endif
841
842 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
843 {
844 wxCHECK_MSG( Ok() &&
845 (rect.x >= 0) && (rect.y >= 0) &&
846 (rect.x+rect.width <= GetWidth()) &&
847 (rect.y+rect.height <= GetHeight()),
848 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
849
850
851 wxBitmap ret( rect.width, rect.height, GetDepth() );
852 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
853
854
855 int sourcewidth = GetWidth() ;
856 int destwidth = rect.width ;
857 int destheight = rect.height ;
858 {
859 unsigned char * sourcedata = (unsigned char*) GetRawAccess() ;
860 unsigned char * destdata = (unsigned char*) ret.BeginRawAccess() ;
861 int sourcelinesize = sourcewidth * 4 ;
862 int destlinesize = destwidth * 4 ;
863 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
864 unsigned char *dest = destdata ;
865 for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
866 {
867 memcpy( dest , source , destlinesize ) ;
868 }
869 }
870 ret.EndRawAccess() ;
871
872 if ( M_BITMAPDATA->m_bitmapMask )
873 {
874 wxMemoryBuffer maskbuf ;
875 int rowBytes = ( destwidth + 3 ) & 0xFFFFFFC ;
876 size_t maskbufsize = rowBytes * destheight ;
877 unsigned char * destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
878
879 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
880 int destlinesize = rowBytes ;
881 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
882 source += rect.x + rect.y * sourcelinesize ;
883 unsigned char *dest = destdata ;
884
885 for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
886 {
887 memcpy( dest , source , destlinesize ) ;
888 }
889 maskbuf.UngetWriteBuf( maskbufsize ) ;
890 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
891 }
892 else if ( HasAlpha() )
893 ret.UseAlpha() ;
894
895 return ret;
896 }
897
898 bool wxBitmap::Create(int w, int h, int d)
899 {
900 UnRef();
901
902 if ( d < 0 )
903 d = wxDisplayDepth() ;
904
905 m_refData = new wxBitmapRefData( w , h , d );
906
907 return M_BITMAPDATA->Ok() ;
908 }
909
910 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
911 {
912 UnRef();
913
914 wxBitmapHandler *handler = FindHandler(type);
915
916 if ( handler )
917 {
918 m_refData = new wxBitmapRefData;
919
920 return handler->LoadFile(this, filename, type, -1, -1);
921 }
922 else
923 {
924 #if wxUSE_IMAGE
925 wxImage loadimage(filename, type);
926 if (loadimage.Ok()) {
927 *this = loadimage;
928 return true;
929 }
930 #endif
931 }
932 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
933 return false;
934 }
935
936 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
937 {
938 UnRef();
939
940 m_refData = new wxBitmapRefData;
941
942 wxBitmapHandler *handler = FindHandler(type);
943
944 if ( handler == NULL ) {
945 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
946
947 return false;
948 }
949
950 return handler->Create(this, data, type, width, height, depth);
951 }
952
953 #if wxUSE_IMAGE
954
955 wxBitmap::wxBitmap(const wxImage& image, int depth)
956 {
957 wxCHECK_RET( image.Ok(), wxT("invalid image") )
958
959 // width and height of the device-dependent bitmap
960 int width = image.GetWidth();
961 int height = image.GetHeight();
962
963 m_refData = new wxBitmapRefData( width , height , depth ) ;
964
965 // Create picture
966
967 bool hasAlpha = false ;
968
969 if ( image.HasMask() )
970 {
971 // takes precedence, don't mix with alpha info
972 }
973 else
974 {
975 hasAlpha = image.HasAlpha() ;
976 }
977
978 if ( hasAlpha )
979 UseAlpha() ;
980
981 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
982
983 register unsigned char* data = image.GetData();
984 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
985 for (int y = 0; y < height; y++)
986 {
987 for (int x = 0; x < width; x++)
988 {
989 if ( hasAlpha )
990 {
991 const unsigned char a = *alpha++;
992 *destination++ = a ;
993 #if wxMAC_USE_PREMULTIPLIED_ALPHA
994 *destination++ = ((*data++) * a + 127 ) / 255 ;
995 *destination++ = ((*data++) * a + 127 ) / 255 ;
996 *destination++ = ((*data++) * a + 127 ) / 255 ;
997 #else
998 *destination++ = *data++ ;
999 *destination++ = *data++ ;
1000 *destination++ = *data++ ;
1001 #endif
1002 }
1003 else
1004 {
1005 *destination++ = 0xFF ;
1006 *destination++ = *data++ ;
1007 *destination++ = *data++ ;
1008 *destination++ = *data++ ;
1009 }
1010 }
1011 }
1012 EndRawAccess() ;
1013 if ( image.HasMask() )
1014 {
1015 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
1016 }
1017 }
1018
1019 wxImage wxBitmap::ConvertToImage() const
1020 {
1021 wxImage image;
1022
1023 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1024
1025 // create an wxImage object
1026 int width = GetWidth();
1027 int height = GetHeight();
1028 image.Create( width, height );
1029
1030 unsigned char *data = image.GetData();
1031 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1032
1033 unsigned char* source = (unsigned char*) GetRawAccess() ;
1034
1035 bool hasAlpha = false ;
1036 bool hasMask = false ;
1037 int maskBytesPerRow = 0 ;
1038 unsigned char *alpha = NULL ;
1039 unsigned char *mask = NULL ;
1040 if ( HasAlpha() )
1041 {
1042 hasAlpha = true ;
1043 }
1044
1045 if ( GetMask() )
1046 {
1047 hasMask = true ;
1048 mask = (unsigned char*) GetMask()->GetRawAccess() ;
1049 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
1050 }
1051
1052 if ( hasAlpha )
1053 {
1054 image.SetAlpha() ;
1055 alpha = image.GetAlpha() ;
1056 }
1057 int index = 0;
1058
1059 // The following masking algorithm is the same as well in msw/gtk:
1060 // the colour used as transparent one in wxImage and the one it is
1061 // replaced with when it really occurs in the bitmap
1062 static const int MASK_RED = 1;
1063 static const int MASK_GREEN = 2;
1064 static const int MASK_BLUE = 3;
1065 static const int MASK_BLUE_REPLACEMENT = 2;
1066
1067 for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow )
1068 {
1069 unsigned char * maskp = mask ;
1070 for (int xx = 0; xx < width; xx++)
1071 {
1072 long color = *((long*) source) ;
1073 unsigned char a = ((color&0xFF000000) >> 24) ;
1074 unsigned char r = ((color&0x00FF0000) >> 16) ;
1075 unsigned char g = ((color&0x0000FF00) >> 8) ;
1076 unsigned char b = (color&0x000000FF);
1077 if ( hasMask )
1078 {
1079 if ( *maskp++ == 0 )
1080 {
1081 r = MASK_RED ;
1082 g = MASK_GREEN ;
1083 b = MASK_BLUE ;
1084 }
1085 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1086 b = MASK_BLUE_REPLACEMENT ;
1087 }
1088 else if ( hasAlpha )
1089 *alpha++ = a ;
1090
1091 data[index ] = r ;
1092 data[index + 1] = g ;
1093 data[index + 2] = b ;
1094 index += 3;
1095 source += 4 ;
1096 }
1097 }
1098 if ( hasMask )
1099 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1100 return image;
1101 }
1102
1103 #endif //wxUSE_IMAGE
1104
1105 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
1106 const wxPalette *palette) const
1107 {
1108 bool success = false;
1109 wxBitmapHandler *handler = FindHandler(type);
1110
1111 if ( handler )
1112 {
1113 success = handler->SaveFile(this, filename, type, palette);
1114 }
1115 else
1116 {
1117 #if wxUSE_IMAGE
1118 wxImage image = ConvertToImage();
1119 success = image.SaveFile(filename, type);
1120 #else
1121 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1122 #endif
1123 }
1124
1125 return success;
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 }