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