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