1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/bitmap.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
18 #include "wx/dcmemory.h"
23 #include "wx/metafile.h"
24 #include "wx/xpmdecod.h"
26 #include "wx/rawbmp.h"
28 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
33 #include <QuickTime/QuickTime.h>
35 #include <PictUtils.h>
38 #include "wx/mac/uma.h"
40 // Implementation Notes
41 // --------------------
43 // we are always working with a 32 bit deep pixel buffer
44 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
45 // therefore we have a separate GWorld there for blitting the mask in
47 // under Quartz then content is transformed into a CGImageRef representing the same data
48 // which can be transferred to the GPU by the OS for fast rendering
50 #if wxMAC_USE_CORE_GRAPHICS
51 #define wxMAC_USE_PREMULTIPLIED_ALPHA 1
52 static const int kBestByteAlignement
= 16;
53 static const int kMaskBytesPerPixel
= 1;
55 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
56 static const int kBestByteAlignement
= 4;
57 static const int kMaskBytesPerPixel
= 4;
60 static int GetBestBytesPerRow( int rawBytes
)
62 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
67 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
69 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
72 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
78 // NOTE : For testing Panther behaviour under higher
79 // Systems make this always be false
80 if ( UMAGetSystemVersion() >= 0x1040 )
82 // as soon as it is supported, it's a better default
83 forceType
= kControlContentCGImageRef
;
85 else if ( bmap
->HasNativeSize() )
87 forceType
= kControlContentIconRef
;
91 if ( forceType
== kControlContentIconRef
)
94 wxBitmapRefData
* bmp
= bmap
;
96 if ( !bmap
->HasNativeSize() )
98 // as PICT conversion will only result in a 16x16 icon, let's attempt
99 // a few scales for better results
101 int w
= bitmap
.GetWidth() ;
102 int h
= bitmap
.GetHeight() ;
103 int sz
= wxMax( w
, h
) ;
104 if ( sz
== 24 || sz
== 64 )
106 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
107 bmp
= scaleBmp
.GetBitmapData() ;
111 info
->contentType
= kControlContentIconRef
;
112 info
->u
.iconRef
= bmp
->GetIconRef() ;
113 AcquireIconRef( info
->u
.iconRef
) ;
115 else if ( forceType
== kControlContentCGImageRef
)
117 info
->contentType
= kControlContentCGImageRef
;
118 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
123 info
->contentType
= kControlContentPictHandle
;
124 info
->u
.picture
= bmap
->GetPictHandle() ;
130 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
132 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
135 return (CGImageRef
) bmap
->CGImageCreate();
138 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
140 if ( info
->contentType
== kControlContentIconRef
)
142 ReleaseIconRef( info
->u
.iconRef
) ;
144 else if ( info
->contentType
== kControlNoContent
)
146 // there's no bitmap at all, fall through silently
148 else if ( info
->contentType
== kControlContentPictHandle
)
150 // owned by the bitmap, no release here
152 else if ( info
->contentType
== kControlContentCGImageRef
)
154 CGImageRelease( info
->u
.imageRef
) ;
158 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
162 #endif //wxUSE_BMPBUTTON
164 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
166 void wxBitmapRefData::Init()
173 m_bitmapMask
= NULL
;
176 m_cgImageRef
= NULL
;
180 m_pictHandle
= NULL
;
182 #if! wxMAC_USE_CORE_GRAPHICS
183 m_hMaskBitmap
= NULL
;
184 m_maskBytesPerRow
= 0 ;
187 m_rawAccessCount
= 0 ;
191 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
194 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
196 if (tocopy
.m_bitmapMask
)
197 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
198 else if (tocopy
.m_hasAlpha
)
201 unsigned char* dest
= (unsigned char*)GetRawAccess();
202 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
203 size_t numbytes
= m_bytesPerRow
* m_height
;
204 memcpy( dest
, source
, numbytes
);
207 wxBitmapRefData::wxBitmapRefData()
212 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
215 Create( w
, h
, d
) ;
218 bool wxBitmapRefData::Create( int w
, int h
, int d
)
220 m_width
= wxMax(1, w
);
221 m_height
= wxMax(1, h
);
224 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
225 size_t size
= m_bytesPerRow
* h
;
226 void* data
= m_memBuf
.GetWriteBuf( size
) ;
227 memset( data
, 0 , size
) ;
228 m_memBuf
.UngetWriteBuf( size
) ;
231 #if !wxMAC_USE_CORE_GRAPHICS
232 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
233 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
234 (char*) data
, m_bytesPerRow
) ) ;
235 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
237 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
238 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
239 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
240 CGContextScaleCTM( m_hBitmap
, 1, -1 );
242 m_ok
= ( m_hBitmap
!= NULL
) ;
247 void wxBitmapRefData::UseAlpha( bool use
)
249 if ( m_hasAlpha
== use
)
253 #if wxMAC_USE_CORE_GRAPHICS
254 CGContextRelease( m_hBitmap
);
255 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
256 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
257 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
258 CGContextScaleCTM( m_hBitmap
, 1, -1 );
262 wxASSERT( m_hMaskBitmap
== NULL
) ;
264 int width
= GetWidth() ;
265 int height
= GetHeight() ;
266 m_maskBytesPerRow
= GetBestBytesPerRow( width
* kMaskBytesPerPixel
);
267 size_t size
= height
* m_maskBytesPerRow
;
268 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
269 wxASSERT( data
!= NULL
) ;
271 memset( data
, 0 , size
) ;
272 Rect rect
= { 0 , 0 , height
, width
} ;
274 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
275 (char*) data
, m_maskBytesPerRow
) ) ;
276 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
278 m_maskMemBuf
.UngetWriteBuf(size
) ;
285 DisposeGWorld( m_hMaskBitmap
) ;
286 m_hMaskBitmap
= NULL
;
287 m_maskBytesPerRow
= 0 ;
292 void *wxBitmapRefData::GetRawAccess() const
294 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
295 return m_memBuf
.GetData() ;
298 void *wxBitmapRefData::BeginRawAccess()
300 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
301 wxASSERT( m_rawAccessCount
== 0 ) ;
302 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
303 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
308 // we must destroy an existing cached image, as
309 // the bitmap data may change now
312 CGImageRelease( m_cgImageRef
) ;
313 m_cgImageRef
= NULL
;
317 return m_memBuf
.GetData() ;
320 void wxBitmapRefData::EndRawAccess()
322 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
323 wxASSERT( m_rawAccessCount
== 1 ) ;
327 #if !wxMAC_USE_CORE_GRAPHICS
332 bool wxBitmapRefData::HasNativeSize()
335 int h
= GetHeight() ;
336 int sz
= wxMax( w
, h
) ;
338 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
341 IconRef
wxBitmapRefData::GetIconRef()
343 if ( m_iconRef
== NULL
)
345 // Create Icon Family Handle
347 IconFamilyHandle iconFamily
= NULL
;
349 if ( UMAGetSystemVersion() < 0x1040 )
351 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
352 (**iconFamily
).resourceType
= kIconFamilyType
;
353 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
357 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
361 int h
= GetHeight() ;
362 int sz
= wxMax( w
, h
) ;
364 OSType dataType
= 0 ;
365 OSType maskType
= 0 ;
370 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
371 if ( UMAGetSystemVersion() >= 0x1050 )
373 dataType
= kIconServices128PixelDataARGB
;
378 dataType
= kThumbnail32BitData
;
379 maskType
= kThumbnail8BitMask
;
384 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
385 if ( UMAGetSystemVersion() >= 0x1050 )
387 dataType
= kIconServices48PixelDataARGB
;
392 dataType
= kHuge32BitData
;
393 maskType
= kHuge8BitMask
;
398 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
399 if ( UMAGetSystemVersion() >= 0x1050 )
401 dataType
= kIconServices32PixelDataARGB
;
406 dataType
= kLarge32BitData
;
407 maskType
= kLarge8BitMask
;
412 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
413 if ( UMAGetSystemVersion() >= 0x1050 )
415 dataType
= kIconServices16PixelDataARGB
;
420 dataType
= kSmall32BitData
;
421 maskType
= kSmall8BitMask
;
431 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
432 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
434 size_t datasize
= sz
* sz
* 4 ;
435 Handle data
= NewHandle( datasize
) ;
437 unsigned char* ptr
= (unsigned char*) *data
;
438 memset( ptr
, 0, datasize
);
439 bool hasAlpha
= HasAlpha() ;
440 wxMask
*mask
= m_bitmapMask
;
441 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
442 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
444 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
446 unsigned char * source
= sourcePtr
;
447 unsigned char * masksource
= masksourcePtr
;
448 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
449 unsigned char a
, r
, g
, b
;
451 for ( int x
= 0 ; x
< w
; ++x
)
460 a
= 0xFF - *masksource
++ ;
462 else if ( !hasAlpha
)
466 #if wxMAC_USE_PREMULTIPLIED_ALPHA
467 // this must be non-premultiplied data
468 if ( a
!= 0xFF && a
!= 0 )
484 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
485 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
486 DisposeHandle( data
);
491 // setup the header properly
494 Handle maskdata
= NULL
;
495 unsigned char * maskptr
= NULL
;
496 unsigned char * ptr
= NULL
;
497 size_t datasize
, masksize
;
499 datasize
= sz
* sz
* 4 ;
500 data
= NewHandle( datasize
) ;
502 ptr
= (unsigned char*) *data
;
503 memset( ptr
, 0, datasize
) ;
506 maskdata
= NewHandle( masksize
) ;
508 maskptr
= (unsigned char*) *maskdata
;
509 memset( maskptr
, 0 , masksize
) ;
511 bool hasAlpha
= HasAlpha() ;
512 wxMask
*mask
= m_bitmapMask
;
513 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
514 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
516 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
518 unsigned char * source
= sourcePtr
;
519 unsigned char * masksource
= masksourcePtr
;
520 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
521 unsigned char * maskdest
= maskptr
+ y
* sz
;
522 unsigned char a
, r
, g
, b
;
524 for ( int x
= 0 ; x
< w
; ++x
)
538 *maskdest
++ = 0xFF - *masksource
++ ;
539 #if !wxMAC_USE_CORE_GRAPHICS
552 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
553 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
555 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
556 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
559 HUnlock( maskdata
) ;
560 DisposeHandle( data
) ;
561 DisposeHandle( maskdata
) ;
566 PicHandle pic
= GetPictHandle() ;
567 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
569 // transform into IconRef
571 // cleaner version existing from 10.3 upwards
572 HLock((Handle
) iconFamily
);
573 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
574 HUnlock((Handle
) iconFamily
);
575 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
576 DisposeHandle( (Handle
) iconFamily
) ;
582 PicHandle
wxBitmapRefData::GetPictHandle()
584 if ( m_pictHandle
== NULL
)
586 #if !wxMAC_USE_CORE_GRAPHICS
587 CGrafPtr origPort
= NULL
;
588 GDHandle origDev
= NULL
;
589 GWorldPtr wp
= NULL
;
590 GWorldPtr mask
= NULL
;
591 int height
= GetHeight() ;
592 int width
= GetWidth() ;
594 Rect rect
= { 0 , 0 , height
, width
} ;
595 RgnHandle clipRgn
= NULL
;
597 GetGWorld( &origPort
, &origDev
) ;
598 wp
= GetHBITMAP( &mask
) ;
602 GWorldPtr monoworld
;
604 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
606 LockPixels( GetGWorldPixMap( monoworld
) ) ;
607 LockPixels( GetGWorldPixMap( mask
) ) ;
608 SetGWorld( monoworld
, NULL
) ;
610 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
611 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
612 RGBForeColor( &black
) ;
613 RGBBackColor( &white
) ;
615 CopyBits(GetPortBitMapForCopyBits(mask
),
616 GetPortBitMapForCopyBits(monoworld
),
620 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
622 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
623 UnlockPixels( GetGWorldPixMap( mask
) ) ;
624 DisposeGWorld( monoworld
) ;
627 SetGWorld( wp
, NULL
) ;
629 GetPortBounds( wp
, &portRect
) ;
630 m_pictHandle
= OpenPicture(&portRect
);
634 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
635 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
637 RGBForeColor( &black
) ;
638 RGBBackColor( &white
) ;
643 LockPixels( GetGWorldPixMap( wp
) ) ;
644 CopyBits(GetPortBitMapForCopyBits(wp
),
645 GetPortBitMapForCopyBits(wp
),
649 UnlockPixels( GetGWorldPixMap( wp
) ) ;
653 SetGWorld( origPort
, origDev
) ;
655 DisposeRgn( clipRgn
) ;
658 GraphicsExportComponent exporter
= 0;
659 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
662 m_pictHandle
= (PicHandle
) NewHandle(0);
665 // QT does not correctly export the mask
666 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
667 CGImageRef imageRef
= CGImageCreate();
668 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
669 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
670 err
= GraphicsExportDoExport(exporter
, NULL
);
671 CGImageRelease( imageRef
);
673 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
674 // the 512 bytes header is only needed for pict files, but not in memory
675 if ( handleSize
>= 512 )
677 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
678 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
681 CloseComponent( exporter
);
687 return m_pictHandle
;
691 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t WXUNUSED(size
))
693 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
695 wxASSERT( data
== membuf
->GetData() ) ;
700 CGImageRef
wxBitmapRefData::CGImageCreate() const
703 wxASSERT( m_rawAccessCount
>= 0 ) ;
705 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
707 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) && wxMAC_USE_CORE_GRAPHICS
708 if ( UMAGetSystemVersion() >= 0x1040 && m_depth
!= 1 && m_bitmapMask
== NULL
)
712 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
713 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
714 image
= CGImageCreateWithMask( tempImage
, tempMask
);
715 CGImageRelease(tempMask
);
716 CGImageRelease(tempImage
);
719 image
= CGBitmapContextCreateImage( m_hBitmap
);
724 size_t imageSize
= m_height
* m_bytesPerRow
;
725 void * dataBuffer
= m_memBuf
.GetData() ;
728 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
729 wxMemoryBuffer
* membuf
= NULL
;
733 alphaInfo
= kCGImageAlphaFirst
;
734 membuf
= new wxMemoryBuffer( imageSize
) ;
735 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
736 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
737 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
738 unsigned char *destalphastart
= (unsigned char *) membuf
->GetData() ;
739 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
741 unsigned char *sourcemask
= sourcemaskstart
;
742 unsigned char *destalpha
= destalphastart
;
743 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
745 *destalpha
= 0xFF - *sourcemask
;
753 #if wxMAC_USE_PREMULTIPLIED_ALPHA
754 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
756 alphaInfo
= kCGImageAlphaFirst
;
760 membuf
= new wxMemoryBuffer( m_memBuf
) ;
763 CGDataProviderRef dataProvider
= NULL
;
766 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
767 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
768 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
769 // copy one color component
770 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
771 maskBufData
[i
] = bufData
[i
*4+3];
773 CGDataProviderCreateWithData(
774 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
775 wxMacMemoryBufferReleaseProc
);
776 // as we are now passing the mask buffer to the data provider, we have
777 // to release the membuf ourselves
780 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
784 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
786 CGDataProviderCreateWithData(
787 membuf
, (const void *)membuf
->GetData() , imageSize
,
788 wxMacMemoryBufferReleaseProc
);
791 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
792 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
794 CGDataProviderRelease( dataProvider
);
799 image
= m_cgImageRef
;
800 CGImageRetain( image
) ;
803 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
805 // we keep it for later use
806 m_cgImageRef
= image
;
807 CGImageRetain( image
) ;
814 #if wxMAC_USE_CORE_GRAPHICS
815 CGContextRef
wxBitmapRefData::GetBitmapContext() const
820 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
822 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
828 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
830 else if ( m_hasAlpha
)
832 if ( m_rawAccessCount
> 0 )
834 *mask
= m_hMaskBitmap
;
842 #if !wxMAC_USE_CORE_GRAPHICS
843 void wxBitmapRefData::UpdateAlphaMask() const
847 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
848 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
850 int h
= GetHeight() ;
853 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
855 unsigned char* destalpha
= destalphabase
;
857 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
859 // we must have 24 bit depth for non quartz smooth alpha
861 *destalpha
++ = 255 - *sourcemask
;
862 *destalpha
++ = 255 - *sourcemask
;
863 *destalpha
++ = 255 - *sourcemask
;
870 void wxBitmapRefData::Free()
872 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
877 CGImageRelease( m_cgImageRef
) ;
878 m_cgImageRef
= NULL
;
884 ReleaseIconRef( m_iconRef
) ;
891 KillPicture( m_pictHandle
) ;
892 m_pictHandle
= NULL
;
898 #if !wxMAC_USE_CORE_GRAPHICS
899 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
901 CGContextRelease(m_hBitmap
);
906 #if !wxMAC_USE_CORE_GRAPHICS
909 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
910 m_hMaskBitmap
= NULL
;
920 wxBitmapRefData::~wxBitmapRefData()
925 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
927 bool created
= false ;
928 int w
= icon
.GetWidth() ;
929 int h
= icon
.GetHeight() ;
931 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
933 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
935 IconFamilyHandle iconFamily
= NULL
;
936 Handle imagehandle
= NewHandle( 0 ) ;
937 Handle maskhandle
= NewHandle( 0 ) ;
941 IconSelectorValue selector
= 0 ;
946 dataType
= kThumbnail32BitData
;
947 maskType
= kThumbnail8BitMask
;
948 selector
= kSelectorAllAvailableData
;
952 dataType
= kHuge32BitData
;
953 maskType
= kHuge8BitMask
;
954 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
958 dataType
= kLarge32BitData
;
959 maskType
= kLarge8BitMask
;
960 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
964 dataType
= kSmall32BitData
;
965 maskType
= kSmall8BitMask
;
966 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
973 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
975 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
976 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
977 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
978 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
980 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
982 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
983 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
987 unsigned char *source
= (unsigned char *) *imagehandle
;
988 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
989 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
991 for ( int y
= 0 ; y
< h
; ++y
)
993 for ( int x
= 0 ; x
< w
; ++x
)
995 unsigned char a
= *sourcemask
++;
998 #if wxMAC_USE_PREMULTIPLIED_ALPHA
999 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1000 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1001 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1003 *destination
++ = *source
++ ;
1004 *destination
++ = *source
++ ;
1005 *destination
++ = *source
++ ;
1011 DisposeHandle( imagehandle
) ;
1012 DisposeHandle( maskhandle
) ;
1016 DisposeHandle( (Handle
) iconFamily
) ;
1022 dc
.SelectObject( *this ) ;
1023 dc
.DrawIcon( icon
, 0 , 0 ) ;
1024 dc
.SelectObject( wxNullBitmap
) ;
1030 wxBitmap::wxBitmap()
1034 wxBitmap::~wxBitmap()
1038 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
1040 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
1044 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
1045 if ( the_width
% (sizeof(unsigned char) * 8) )
1046 linesize
+= sizeof(unsigned char);
1048 unsigned char* linestart
= (unsigned char*) bits
;
1049 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
1051 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
1053 unsigned char* destination
= destptr
;
1054 int index
, bit
, mask
;
1056 for ( int x
= 0 ; x
< the_width
; ++x
)
1062 if ( linestart
[index
] & mask
)
1064 *destination
++ = 0xFF ;
1065 *destination
++ = 0 ;
1066 *destination
++ = 0 ;
1067 *destination
++ = 0 ;
1071 *destination
++ = 0xFF ;
1072 *destination
++ = 0xFF ;
1073 *destination
++ = 0xFF ;
1074 *destination
++ = 0xFF ;
1083 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
1087 wxBitmap::wxBitmap(int w
, int h
, int d
)
1089 (void)Create(w
, h
, d
);
1092 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1094 (void) Create(data
, type
, width
, height
, depth
);
1097 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1099 LoadFile(filename
, type
);
1102 wxObjectRefData
* wxBitmap::CreateRefData() const
1104 return new wxBitmapRefData
;
1107 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
1109 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
1112 void * wxBitmap::GetRawAccess() const
1114 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1116 return M_BITMAPDATA
->GetRawAccess() ;
1119 void * wxBitmap::BeginRawAccess()
1121 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1123 return M_BITMAPDATA
->BeginRawAccess() ;
1126 void wxBitmap::EndRawAccess()
1128 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
1130 M_BITMAPDATA
->EndRawAccess() ;
1133 #ifdef __WXMAC_OSX__
1134 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
1136 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
1138 return M_BITMAPDATA
->CGImageCreate() ;
1142 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1144 wxCHECK_MSG( Ok() &&
1145 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1146 (rect
.x
+rect
.width
<= GetWidth()) &&
1147 (rect
.y
+rect
.height
<= GetHeight()),
1148 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1150 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1151 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1153 int destwidth
= rect
.width
;
1154 int destheight
= rect
.height
;
1157 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1158 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1159 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1161 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1162 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1163 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1164 unsigned char *dest
= destdata
;
1166 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1168 memcpy( dest
, source
, destlinesize
) ;
1172 ret
.EndRawAccess() ;
1174 if ( M_BITMAPDATA
->m_bitmapMask
)
1176 wxMemoryBuffer maskbuf
;
1177 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1178 size_t maskbufsize
= rowBytes
* destheight
;
1180 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1181 int destlinesize
= rowBytes
;
1183 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1184 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1185 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1187 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1188 unsigned char *dest
= destdata
;
1190 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1192 memcpy( dest
, source
, destlinesize
) ;
1195 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1196 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1198 else if ( HasAlpha() )
1204 bool wxBitmap::Create(int w
, int h
, int d
)
1209 d
= wxDisplayDepth() ;
1211 m_refData
= new wxBitmapRefData( w
, h
, d
);
1213 return M_BITMAPDATA
->Ok() ;
1216 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1220 wxBitmapHandler
*handler
= FindHandler(type
);
1224 m_refData
= new wxBitmapRefData
;
1226 return handler
->LoadFile(this, filename
, type
, -1, -1);
1231 wxImage
loadimage(filename
, type
);
1241 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1246 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1250 m_refData
= new wxBitmapRefData
;
1252 wxBitmapHandler
*handler
= FindHandler(type
);
1254 if ( handler
== NULL
)
1256 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1261 return handler
->Create(this, data
, type
, width
, height
, depth
);
1266 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1268 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1270 // width and height of the device-dependent bitmap
1271 int width
= image
.GetWidth();
1272 int height
= image
.GetHeight();
1274 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1278 bool hasAlpha
= false ;
1280 if ( image
.HasMask() )
1282 // takes precedence, don't mix with alpha info
1286 hasAlpha
= image
.HasAlpha() ;
1292 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1293 register unsigned char* data
= image
.GetData();
1294 if ( destinationstart
!= NULL
&& data
!= NULL
)
1296 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1297 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1299 unsigned char * destination
= destinationstart
;
1300 for (int x
= 0; x
< width
; x
++)
1304 const unsigned char a
= *alpha
++;
1305 *destination
++ = a
;
1307 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1308 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1309 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1310 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1312 *destination
++ = *data
++ ;
1313 *destination
++ = *data
++ ;
1314 *destination
++ = *data
++ ;
1319 *destination
++ = 0xFF ;
1320 *destination
++ = *data
++ ;
1321 *destination
++ = *data
++ ;
1322 *destination
++ = *data
++ ;
1329 if ( image
.HasMask() )
1330 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1333 wxImage
wxBitmap::ConvertToImage() const
1337 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1339 // create an wxImage object
1340 int width
= GetWidth();
1341 int height
= GetHeight();
1342 image
.Create( width
, height
);
1344 unsigned char *data
= image
.GetData();
1345 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1347 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1349 bool hasAlpha
= false ;
1350 bool hasMask
= false ;
1351 int maskBytesPerRow
= 0 ;
1352 unsigned char *alpha
= NULL
;
1353 unsigned char *mask
= NULL
;
1361 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1362 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1368 alpha
= image
.GetAlpha() ;
1373 // The following masking algorithm is the same as well in msw/gtk:
1374 // the colour used as transparent one in wxImage and the one it is
1375 // replaced with when it actually occurs in the bitmap
1376 static const int MASK_RED
= 1;
1377 static const int MASK_GREEN
= 2;
1378 static const int MASK_BLUE
= 3;
1379 static const int MASK_BLUE_REPLACEMENT
= 2;
1381 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1383 unsigned char * maskp
= mask
;
1384 unsigned char * source
= sourcestart
;
1385 unsigned char a
, r
, g
, b
;
1388 for (int xx
= 0; xx
< width
; xx
++)
1390 color
= *((long*) source
) ;
1391 #ifdef WORDS_BIGENDIAN
1392 a
= ((color
&0xFF000000) >> 24) ;
1393 r
= ((color
&0x00FF0000) >> 16) ;
1394 g
= ((color
&0x0000FF00) >> 8) ;
1395 b
= (color
&0x000000FF);
1397 b
= ((color
&0xFF000000) >> 24) ;
1398 g
= ((color
&0x00FF0000) >> 16) ;
1399 r
= ((color
&0x0000FF00) >> 8) ;
1400 a
= (color
&0x000000FF);
1404 if ( *maskp
++ == 0xFF )
1410 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1411 b
= MASK_BLUE_REPLACEMENT
;
1412 #if !wxMAC_USE_CORE_GRAPHICS
1418 else if ( hasAlpha
)
1421 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1422 // this must be non-premultiplied data
1423 if ( a
!= 0xFF && a
!= 0 )
1433 data
[index
+ 1] = g
;
1434 data
[index
+ 2] = b
;
1442 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1447 #endif //wxUSE_IMAGE
1449 bool wxBitmap::SaveFile( const wxString
& filename
,
1450 wxBitmapType type
, const wxPalette
*palette
) const
1452 bool success
= false;
1453 wxBitmapHandler
*handler
= FindHandler(type
);
1457 success
= handler
->SaveFile(this, filename
, type
, palette
);
1462 wxImage image
= ConvertToImage();
1463 success
= image
.SaveFile(filename
, type
);
1465 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1472 bool wxBitmap::IsOk() const
1474 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1477 int wxBitmap::GetHeight() const
1479 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1481 return M_BITMAPDATA
->GetHeight();
1484 int wxBitmap::GetWidth() const
1486 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1488 return M_BITMAPDATA
->GetWidth() ;
1491 int wxBitmap::GetDepth() const
1493 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1495 return M_BITMAPDATA
->GetDepth();
1498 wxMask
*wxBitmap::GetMask() const
1500 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1502 return M_BITMAPDATA
->m_bitmapMask
;
1505 bool wxBitmap::HasAlpha() const
1507 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1509 return M_BITMAPDATA
->HasAlpha() ;
1512 void wxBitmap::SetWidth(int w
)
1515 M_BITMAPDATA
->SetWidth(w
);
1518 void wxBitmap::SetHeight(int h
)
1521 M_BITMAPDATA
->SetHeight(h
);
1524 void wxBitmap::SetDepth(int d
)
1527 M_BITMAPDATA
->SetDepth(d
);
1530 void wxBitmap::SetOk(bool isOk
)
1533 M_BITMAPDATA
->SetOk(isOk
);
1537 wxPalette
*wxBitmap::GetPalette() const
1539 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1541 return &M_BITMAPDATA
->m_bitmapPalette
;
1544 void wxBitmap::SetPalette(const wxPalette
& palette
)
1547 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1549 #endif // wxUSE_PALETTE
1551 void wxBitmap::SetMask(wxMask
*mask
)
1554 // Remove existing mask if there is one.
1555 delete M_BITMAPDATA
->m_bitmapMask
;
1557 M_BITMAPDATA
->m_bitmapMask
= mask
;
1560 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1562 #if !wxMAC_USE_CORE_GRAPHICS
1563 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1565 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1569 // ----------------------------------------------------------------------------
1571 // ----------------------------------------------------------------------------
1578 wxMask::wxMask(const wxMask
&tocopy
)
1582 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1583 m_width
= tocopy
.m_width
;
1584 m_height
= tocopy
.m_height
;
1586 size_t size
= m_bytesPerRow
* m_height
;
1587 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1588 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1589 memcpy( dest
, source
, size
);
1590 m_memBuf
.UngetWriteBuf( size
) ;
1594 // Construct a mask from a bitmap and a colour indicating
1595 // the transparent area
1596 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1599 Create( bitmap
, colour
);
1602 // Construct a mask from a mono bitmap (copies the bitmap).
1603 wxMask::wxMask( const wxBitmap
& bitmap
)
1609 // Construct a mask from a mono bitmap (copies the bitmap).
1611 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1614 Create( data
, width
, height
, bytesPerRow
);
1621 #if wxMAC_USE_CORE_GRAPHICS
1622 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1624 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1626 m_maskBitmap
= NULL
;
1632 m_width
= m_height
= m_bytesPerRow
= 0 ;
1633 m_maskBitmap
= NULL
;
1636 void *wxMask::GetRawAccess() const
1638 return m_memBuf
.GetData() ;
1641 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1642 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1644 void wxMask::RealizeNative()
1648 #if wxMAC_USE_CORE_GRAPHICS
1649 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1651 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1653 m_maskBitmap
= NULL
;
1656 #if wxMAC_USE_CORE_GRAPHICS
1657 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1658 // from MouseTracking sample :
1659 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1660 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1662 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1663 kCGImageAlphaNone
);
1664 CGColorSpaceRelease( colorspace
);
1665 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1667 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1669 OSStatus err
= NewGWorldFromPtr(
1670 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1671 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1672 verify_noerr( err
) ;
1676 // Create a mask from a mono bitmap (copies the bitmap).
1678 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1683 m_bytesPerRow
= bytesPerRow
;
1685 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1692 // Create a mask from a mono bitmap (copies the bitmap).
1693 bool wxMask::Create(const wxBitmap
& bitmap
)
1695 m_width
= bitmap
.GetWidth() ;
1696 m_height
= bitmap
.GetHeight() ;
1697 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1699 size_t size
= m_bytesPerRow
* m_height
;
1700 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1701 wxASSERT( destdatabase
!= NULL
) ;
1703 memset( destdatabase
, 0 , size
) ;
1704 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1706 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1708 unsigned char *destdata
= destdatabase
;
1709 unsigned char r
, g
, b
;
1711 for ( int x
= 0 ; x
< m_width
; ++x
)
1718 if ( ( r
+ g
+ b
) > 0x10 )
1720 *destdata
++ = 0xFF ;
1721 #if !wxMAC_USE_CORE_GRAPHICS
1722 *destdata
++ = 0xFF ;
1723 *destdata
++ = 0xFF ;
1724 *destdata
++ = 0xFF ;
1729 *destdata
++ = 0x00 ;
1730 #if !wxMAC_USE_CORE_GRAPHICS
1731 *destdata
++ = 0x00 ;
1732 *destdata
++ = 0x00 ;
1733 *destdata
++ = 0x00 ;
1739 m_memBuf
.UngetWriteBuf( size
) ;
1745 // Create a mask from a bitmap and a colour indicating
1746 // the transparent area
1747 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1749 m_width
= bitmap
.GetWidth() ;
1750 m_height
= bitmap
.GetHeight() ;
1751 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1753 size_t size
= m_bytesPerRow
* m_height
;
1754 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1755 wxASSERT( destdatabase
!= NULL
) ;
1757 memset( destdatabase
, 0 , size
) ;
1758 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1759 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1761 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1763 unsigned char *srcdata
= srcdatabase
;
1764 unsigned char *destdata
= destdatabase
;
1765 unsigned char r
, g
, b
;
1767 for ( int x
= 0 ; x
< m_width
; ++x
)
1774 if ( colour
== wxColour( r
, g
, b
) )
1776 *destdata
++ = 0xFF ;
1777 #if !wxMAC_USE_CORE_GRAPHICS
1778 *destdata
++ = 0xFF ;
1779 *destdata
++ = 0xFF ;
1780 *destdata
++ = 0xFF ;
1785 *destdata
++ = 0x00 ;
1786 #if !wxMAC_USE_CORE_GRAPHICS
1787 *destdata
++ = 0x00 ;
1788 *destdata
++ = 0x00 ;
1789 *destdata
++ = 0x00 ;
1795 m_memBuf
.UngetWriteBuf( size
) ;
1801 WXHBITMAP
wxMask::GetHBITMAP() const
1803 return m_maskBitmap
;
1806 // ----------------------------------------------------------------------------
1808 // ----------------------------------------------------------------------------
1810 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1812 // ----------------------------------------------------------------------------
1813 // Standard Handlers
1814 // ----------------------------------------------------------------------------
1816 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1818 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1821 inline wxPICTResourceHandler()
1823 SetName(wxT("Macintosh Pict resource"));
1824 SetExtension(wxEmptyString
);
1825 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1828 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1829 int desiredWidth
, int desiredHeight
);
1832 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1835 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1836 const wxString
& name
,
1837 long WXUNUSED(flags
),
1838 int WXUNUSED(desiredWidth
),
1839 int WXUNUSED(desiredHeight
))
1843 wxMacStringToPascal( name
, theName
) ;
1845 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1850 mf
.SetPICT( thePict
) ;
1851 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1853 dc
.SelectObject( *bitmap
) ;
1855 dc
.SelectObject( wxNullBitmap
) ;
1864 void wxBitmap::InitStandardHandlers()
1866 AddHandler( new wxPICTResourceHandler
) ;
1867 AddHandler( new wxICONResourceHandler
) ;
1870 // ----------------------------------------------------------------------------
1871 // raw bitmap access support
1872 // ----------------------------------------------------------------------------
1874 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1877 // no bitmap, no data (raw or otherwise)
1880 data
.m_width
= GetWidth() ;
1881 data
.m_height
= GetHeight() ;
1882 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1884 return BeginRawAccess() ;
1887 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1892 void wxBitmap::UseAlpha()
1894 // remember that we are using alpha channel:
1895 // we'll need to create a proper mask in UngetRawData()
1896 M_BITMAPDATA
->UseAlpha( true );