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 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
153 else if ( info
->contentType
== kControlContentCGImageRef
)
155 CGImageRelease( info
->u
.imageRef
) ;
160 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
164 #endif //wxUSE_BMPBUTTON
166 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
168 void wxBitmapRefData::Init()
175 m_bitmapMask
= NULL
;
178 m_cgImageRef
= NULL
;
182 m_pictHandle
= NULL
;
184 #if! wxMAC_USE_CORE_GRAPHICS
185 m_hMaskBitmap
= NULL
;
186 m_maskBytesPerRow
= 0 ;
189 m_rawAccessCount
= 0 ;
193 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
196 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
198 if (tocopy
.m_bitmapMask
)
199 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
200 else if (tocopy
.m_hasAlpha
)
203 unsigned char* dest
= (unsigned char*)GetRawAccess();
204 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
205 size_t numbytes
= m_bytesPerRow
* m_height
;
206 memcpy( dest
, source
, numbytes
);
209 wxBitmapRefData::wxBitmapRefData()
214 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
217 Create( w
, h
, d
) ;
220 bool wxBitmapRefData::Create( int w
, int h
, int d
)
222 m_width
= wxMax(1, w
);
223 m_height
= wxMax(1, h
);
226 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
227 size_t size
= m_bytesPerRow
* h
;
228 void* data
= m_memBuf
.GetWriteBuf( size
) ;
229 memset( data
, 0 , size
) ;
230 m_memBuf
.UngetWriteBuf( size
) ;
233 #if !wxMAC_USE_CORE_GRAPHICS
234 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
235 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
236 (char*) data
, m_bytesPerRow
) ) ;
237 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
239 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
240 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
241 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
242 CGContextScaleCTM( m_hBitmap
, 1, -1 );
244 m_ok
= ( m_hBitmap
!= NULL
) ;
249 void wxBitmapRefData::UseAlpha( bool use
)
251 if ( m_hasAlpha
== use
)
255 #if wxMAC_USE_CORE_GRAPHICS
256 CGContextRelease( m_hBitmap
);
257 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
258 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
259 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
260 CGContextScaleCTM( m_hBitmap
, 1, -1 );
264 wxASSERT( m_hMaskBitmap
== NULL
) ;
266 int width
= GetWidth() ;
267 int height
= GetHeight() ;
268 m_maskBytesPerRow
= GetBestBytesPerRow( width
* kMaskBytesPerPixel
);
269 size_t size
= height
* m_maskBytesPerRow
;
270 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
271 wxASSERT( data
!= NULL
) ;
273 memset( data
, 0 , size
) ;
274 Rect rect
= { 0 , 0 , height
, width
} ;
276 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
277 (char*) data
, m_maskBytesPerRow
) ) ;
278 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
280 m_maskMemBuf
.UngetWriteBuf(size
) ;
287 DisposeGWorld( m_hMaskBitmap
) ;
288 m_hMaskBitmap
= NULL
;
289 m_maskBytesPerRow
= 0 ;
294 void *wxBitmapRefData::GetRawAccess() const
296 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
297 return m_memBuf
.GetData() ;
300 void *wxBitmapRefData::BeginRawAccess()
302 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
303 wxASSERT( m_rawAccessCount
== 0 ) ;
304 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
305 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
310 // we must destroy an existing cached image, as
311 // the bitmap data may change now
314 CGImageRelease( m_cgImageRef
) ;
315 m_cgImageRef
= NULL
;
319 return m_memBuf
.GetData() ;
322 void wxBitmapRefData::EndRawAccess()
324 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
325 wxASSERT( m_rawAccessCount
== 1 ) ;
329 #if !wxMAC_USE_CORE_GRAPHICS
334 bool wxBitmapRefData::HasNativeSize()
337 int h
= GetHeight() ;
338 int sz
= wxMax( w
, h
) ;
340 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
343 IconRef
wxBitmapRefData::GetIconRef()
345 if ( m_iconRef
== NULL
)
347 // Create Icon Family Handle
349 IconFamilyHandle iconFamily
= NULL
;
351 if ( UMAGetSystemVersion() < 0x1040 )
353 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
354 (**iconFamily
).resourceType
= kIconFamilyType
;
355 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
359 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
363 int h
= GetHeight() ;
364 int sz
= wxMax( w
, h
) ;
366 OSType dataType
= 0 ;
367 OSType maskType
= 0 ;
372 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
373 if ( UMAGetSystemVersion() >= 0x1050 )
375 dataType
= kIconServices128PixelDataARGB
;
380 dataType
= kThumbnail32BitData
;
381 maskType
= kThumbnail8BitMask
;
386 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
387 if ( UMAGetSystemVersion() >= 0x1050 )
389 dataType
= kIconServices48PixelDataARGB
;
394 dataType
= kHuge32BitData
;
395 maskType
= kHuge8BitMask
;
400 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
401 if ( UMAGetSystemVersion() >= 0x1050 )
403 dataType
= kIconServices32PixelDataARGB
;
408 dataType
= kLarge32BitData
;
409 maskType
= kLarge8BitMask
;
414 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
415 if ( UMAGetSystemVersion() >= 0x1050 )
417 dataType
= kIconServices16PixelDataARGB
;
422 dataType
= kSmall32BitData
;
423 maskType
= kSmall8BitMask
;
433 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
434 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
436 size_t datasize
= sz
* sz
* 4 ;
437 Handle data
= NewHandle( datasize
) ;
439 unsigned char* ptr
= (unsigned char*) *data
;
440 memset( ptr
, 0, datasize
);
441 bool hasAlpha
= HasAlpha() ;
442 wxMask
*mask
= m_bitmapMask
;
443 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
444 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
446 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
448 unsigned char * source
= sourcePtr
;
449 unsigned char * masksource
= masksourcePtr
;
450 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
451 unsigned char a
, r
, g
, b
;
453 for ( int x
= 0 ; x
< w
; ++x
)
462 a
= 0xFF - *masksource
++ ;
464 else if ( !hasAlpha
)
468 #if wxMAC_USE_PREMULTIPLIED_ALPHA
469 // this must be non-premultiplied data
470 if ( a
!= 0xFF && a
!= 0 )
486 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
487 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
488 DisposeHandle( data
);
493 // setup the header properly
496 Handle maskdata
= NULL
;
497 unsigned char * maskptr
= NULL
;
498 unsigned char * ptr
= NULL
;
499 size_t datasize
, masksize
;
501 datasize
= sz
* sz
* 4 ;
502 data
= NewHandle( datasize
) ;
504 ptr
= (unsigned char*) *data
;
505 memset( ptr
, 0, datasize
) ;
508 maskdata
= NewHandle( masksize
) ;
510 maskptr
= (unsigned char*) *maskdata
;
511 memset( maskptr
, 0 , masksize
) ;
513 bool hasAlpha
= HasAlpha() ;
514 wxMask
*mask
= m_bitmapMask
;
515 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
516 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
518 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
520 unsigned char * source
= sourcePtr
;
521 unsigned char * masksource
= masksourcePtr
;
522 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
523 unsigned char * maskdest
= maskptr
+ y
* sz
;
524 unsigned char a
, r
, g
, b
;
526 for ( int x
= 0 ; x
< w
; ++x
)
540 *maskdest
++ = 0xFF - *masksource
++ ;
541 #if !wxMAC_USE_CORE_GRAPHICS
554 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
555 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
557 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
558 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
561 HUnlock( maskdata
) ;
562 DisposeHandle( data
) ;
563 DisposeHandle( maskdata
) ;
568 PicHandle pic
= GetPictHandle() ;
569 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
571 // transform into IconRef
573 // cleaner version existing from 10.3 upwards
574 HLock((Handle
) iconFamily
);
575 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
576 HUnlock((Handle
) iconFamily
);
577 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
578 DisposeHandle( (Handle
) iconFamily
) ;
584 PicHandle
wxBitmapRefData::GetPictHandle()
586 if ( m_pictHandle
== NULL
)
588 #if !wxMAC_USE_CORE_GRAPHICS
589 CGrafPtr origPort
= NULL
;
590 GDHandle origDev
= NULL
;
591 GWorldPtr wp
= NULL
;
592 GWorldPtr mask
= NULL
;
593 int height
= GetHeight() ;
594 int width
= GetWidth() ;
596 Rect rect
= { 0 , 0 , height
, width
} ;
597 RgnHandle clipRgn
= NULL
;
599 GetGWorld( &origPort
, &origDev
) ;
600 wp
= GetHBITMAP( &mask
) ;
604 GWorldPtr monoworld
;
606 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
608 LockPixels( GetGWorldPixMap( monoworld
) ) ;
609 LockPixels( GetGWorldPixMap( mask
) ) ;
610 SetGWorld( monoworld
, NULL
) ;
612 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
613 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
614 RGBForeColor( &black
) ;
615 RGBBackColor( &white
) ;
617 CopyBits(GetPortBitMapForCopyBits(mask
),
618 GetPortBitMapForCopyBits(monoworld
),
622 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
624 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
625 UnlockPixels( GetGWorldPixMap( mask
) ) ;
626 DisposeGWorld( monoworld
) ;
629 SetGWorld( wp
, NULL
) ;
631 GetPortBounds( wp
, &portRect
) ;
632 m_pictHandle
= OpenPicture(&portRect
);
636 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
637 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
639 RGBForeColor( &black
) ;
640 RGBBackColor( &white
) ;
645 LockPixels( GetGWorldPixMap( wp
) ) ;
646 CopyBits(GetPortBitMapForCopyBits(wp
),
647 GetPortBitMapForCopyBits(wp
),
651 UnlockPixels( GetGWorldPixMap( wp
) ) ;
655 SetGWorld( origPort
, origDev
) ;
657 DisposeRgn( clipRgn
) ;
660 GraphicsExportComponent exporter
= 0;
661 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
664 m_pictHandle
= (PicHandle
) NewHandle(0);
667 // QT does not correctly export the mask
668 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
669 CGImageRef imageRef
= CGImageCreate();
670 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
671 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
672 err
= GraphicsExportDoExport(exporter
, NULL
);
673 CGImageRelease( imageRef
);
675 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
676 // the 512 bytes header is only needed for pict files, but not in memory
677 if ( handleSize
>= 512 )
679 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
680 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
683 CloseComponent( exporter
);
689 return m_pictHandle
;
693 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t WXUNUSED(size
))
695 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
697 wxASSERT( data
== membuf
->GetData() ) ;
702 CGImageRef
wxBitmapRefData::CGImageCreate() const
705 wxASSERT( m_rawAccessCount
>= 0 ) ;
707 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
709 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) && wxMAC_USE_CORE_GRAPHICS
710 if ( UMAGetSystemVersion() >= 0x1040 && m_depth
!= 1 && m_bitmapMask
== NULL
)
714 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
715 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
716 image
= CGImageCreateWithMask( tempImage
, tempMask
);
717 CGImageRelease(tempMask
);
718 CGImageRelease(tempImage
);
721 image
= CGBitmapContextCreateImage( m_hBitmap
);
726 size_t imageSize
= m_height
* m_bytesPerRow
;
727 void * dataBuffer
= m_memBuf
.GetData() ;
730 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
731 wxMemoryBuffer
* membuf
= NULL
;
735 alphaInfo
= kCGImageAlphaFirst
;
736 membuf
= new wxMemoryBuffer( imageSize
) ;
737 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
738 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
739 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
740 unsigned char *destalphastart
= (unsigned char *) membuf
->GetData() ;
741 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
743 unsigned char *sourcemask
= sourcemaskstart
;
744 unsigned char *destalpha
= destalphastart
;
745 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
747 *destalpha
= 0xFF - *sourcemask
;
755 #if wxMAC_USE_PREMULTIPLIED_ALPHA
756 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
758 alphaInfo
= kCGImageAlphaFirst
;
762 membuf
= new wxMemoryBuffer( m_memBuf
) ;
765 CGDataProviderRef dataProvider
= NULL
;
768 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
769 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
770 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
771 // copy one color component
772 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
773 maskBufData
[i
] = bufData
[i
*4+3];
775 CGDataProviderCreateWithData(
776 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
777 wxMacMemoryBufferReleaseProc
);
778 // as we are now passing the mask buffer to the data provider, we have
779 // to release the membuf ourselves
782 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
786 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
788 CGDataProviderCreateWithData(
789 membuf
, (const void *)membuf
->GetData() , imageSize
,
790 wxMacMemoryBufferReleaseProc
);
793 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
794 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
796 CGDataProviderRelease( dataProvider
);
801 image
= m_cgImageRef
;
802 CGImageRetain( image
) ;
805 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
807 // we keep it for later use
808 m_cgImageRef
= image
;
809 CGImageRetain( image
) ;
816 #if wxMAC_USE_CORE_GRAPHICS
817 CGContextRef
wxBitmapRefData::GetBitmapContext() const
822 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
824 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
830 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
832 else if ( m_hasAlpha
)
834 if ( m_rawAccessCount
> 0 )
836 *mask
= m_hMaskBitmap
;
844 #if !wxMAC_USE_CORE_GRAPHICS
845 void wxBitmapRefData::UpdateAlphaMask() const
849 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
850 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
852 int h
= GetHeight() ;
855 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
857 unsigned char* destalpha
= destalphabase
;
859 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
861 // we must have 24 bit depth for non quartz smooth alpha
863 *destalpha
++ = 255 - *sourcemask
;
864 *destalpha
++ = 255 - *sourcemask
;
865 *destalpha
++ = 255 - *sourcemask
;
872 void wxBitmapRefData::Free()
874 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
879 CGImageRelease( m_cgImageRef
) ;
880 m_cgImageRef
= NULL
;
886 ReleaseIconRef( m_iconRef
) ;
893 KillPicture( m_pictHandle
) ;
894 m_pictHandle
= NULL
;
900 #if !wxMAC_USE_CORE_GRAPHICS
901 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
903 CGContextRelease(m_hBitmap
);
908 #if !wxMAC_USE_CORE_GRAPHICS
911 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
912 m_hMaskBitmap
= NULL
;
922 wxBitmapRefData::~wxBitmapRefData()
927 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
929 bool created
= false ;
930 int w
= icon
.GetWidth() ;
931 int h
= icon
.GetHeight() ;
933 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
935 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
937 IconFamilyHandle iconFamily
= NULL
;
938 Handle imagehandle
= NewHandle( 0 ) ;
939 Handle maskhandle
= NewHandle( 0 ) ;
943 IconSelectorValue selector
= 0 ;
948 dataType
= kThumbnail32BitData
;
949 maskType
= kThumbnail8BitMask
;
950 selector
= kSelectorAllAvailableData
;
954 dataType
= kHuge32BitData
;
955 maskType
= kHuge8BitMask
;
956 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
960 dataType
= kLarge32BitData
;
961 maskType
= kLarge8BitMask
;
962 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
966 dataType
= kSmall32BitData
;
967 maskType
= kSmall8BitMask
;
968 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
975 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
977 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
978 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
979 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
980 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
982 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
984 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
985 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
989 unsigned char *source
= (unsigned char *) *imagehandle
;
990 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
991 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
993 for ( int y
= 0 ; y
< h
; ++y
)
995 for ( int x
= 0 ; x
< w
; ++x
)
997 unsigned char a
= *sourcemask
++;
1000 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1001 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1002 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1003 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
1005 *destination
++ = *source
++ ;
1006 *destination
++ = *source
++ ;
1007 *destination
++ = *source
++ ;
1013 DisposeHandle( imagehandle
) ;
1014 DisposeHandle( maskhandle
) ;
1018 DisposeHandle( (Handle
) iconFamily
) ;
1024 dc
.SelectObject( *this ) ;
1025 dc
.DrawIcon( icon
, 0 , 0 ) ;
1026 dc
.SelectObject( wxNullBitmap
) ;
1032 wxBitmap::wxBitmap()
1036 wxBitmap::~wxBitmap()
1040 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
1042 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
1046 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
1047 if ( the_width
% (sizeof(unsigned char) * 8) )
1048 linesize
+= sizeof(unsigned char);
1050 unsigned char* linestart
= (unsigned char*) bits
;
1051 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
1053 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
1055 unsigned char* destination
= destptr
;
1056 int index
, bit
, mask
;
1058 for ( int x
= 0 ; x
< the_width
; ++x
)
1064 if ( linestart
[index
] & mask
)
1066 *destination
++ = 0xFF ;
1067 *destination
++ = 0 ;
1068 *destination
++ = 0 ;
1069 *destination
++ = 0 ;
1073 *destination
++ = 0xFF ;
1074 *destination
++ = 0xFF ;
1075 *destination
++ = 0xFF ;
1076 *destination
++ = 0xFF ;
1085 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
1089 wxBitmap::wxBitmap(int w
, int h
, int d
)
1091 (void)Create(w
, h
, d
);
1094 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1096 (void) Create(data
, type
, width
, height
, depth
);
1099 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1101 LoadFile(filename
, type
);
1104 wxObjectRefData
* wxBitmap::CreateRefData() const
1106 return new wxBitmapRefData
;
1109 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
1111 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
1114 void * wxBitmap::GetRawAccess() const
1116 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1118 return M_BITMAPDATA
->GetRawAccess() ;
1121 void * wxBitmap::BeginRawAccess()
1123 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1125 return M_BITMAPDATA
->BeginRawAccess() ;
1128 void wxBitmap::EndRawAccess()
1130 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
1132 M_BITMAPDATA
->EndRawAccess() ;
1135 #ifdef __WXMAC_OSX__
1136 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
1138 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
1140 return M_BITMAPDATA
->CGImageCreate() ;
1144 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1146 wxCHECK_MSG( Ok() &&
1147 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1148 (rect
.x
+rect
.width
<= GetWidth()) &&
1149 (rect
.y
+rect
.height
<= GetHeight()),
1150 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1152 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1153 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1155 int destwidth
= rect
.width
;
1156 int destheight
= rect
.height
;
1159 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1160 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1161 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1163 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1164 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1165 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1166 unsigned char *dest
= destdata
;
1168 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1170 memcpy( dest
, source
, destlinesize
) ;
1174 ret
.EndRawAccess() ;
1176 if ( M_BITMAPDATA
->m_bitmapMask
)
1178 wxMemoryBuffer maskbuf
;
1179 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1180 size_t maskbufsize
= rowBytes
* destheight
;
1182 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1183 int destlinesize
= rowBytes
;
1185 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1186 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1187 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1189 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1190 unsigned char *dest
= destdata
;
1192 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1194 memcpy( dest
, source
, destlinesize
) ;
1197 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1198 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1200 else if ( HasAlpha() )
1206 bool wxBitmap::Create(int w
, int h
, int d
)
1211 d
= wxDisplayDepth() ;
1213 m_refData
= new wxBitmapRefData( w
, h
, d
);
1215 return M_BITMAPDATA
->Ok() ;
1218 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1222 wxBitmapHandler
*handler
= FindHandler(type
);
1226 m_refData
= new wxBitmapRefData
;
1228 return handler
->LoadFile(this, filename
, type
, -1, -1);
1233 wxImage
loadimage(filename
, type
);
1243 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1248 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1252 m_refData
= new wxBitmapRefData
;
1254 wxBitmapHandler
*handler
= FindHandler(type
);
1256 if ( handler
== NULL
)
1258 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1263 return handler
->Create(this, data
, type
, width
, height
, depth
);
1268 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1270 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1272 // width and height of the device-dependent bitmap
1273 int width
= image
.GetWidth();
1274 int height
= image
.GetHeight();
1276 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1280 bool hasAlpha
= false ;
1282 if ( image
.HasMask() )
1284 // takes precedence, don't mix with alpha info
1288 hasAlpha
= image
.HasAlpha() ;
1294 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1295 register unsigned char* data
= image
.GetData();
1296 if ( destinationstart
!= NULL
&& data
!= NULL
)
1298 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1299 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1301 unsigned char * destination
= destinationstart
;
1302 for (int x
= 0; x
< width
; x
++)
1306 const unsigned char a
= *alpha
++;
1307 *destination
++ = a
;
1309 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1310 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1311 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1312 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1314 *destination
++ = *data
++ ;
1315 *destination
++ = *data
++ ;
1316 *destination
++ = *data
++ ;
1321 *destination
++ = 0xFF ;
1322 *destination
++ = *data
++ ;
1323 *destination
++ = *data
++ ;
1324 *destination
++ = *data
++ ;
1331 if ( image
.HasMask() )
1332 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1335 wxImage
wxBitmap::ConvertToImage() const
1339 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1341 // create an wxImage object
1342 int width
= GetWidth();
1343 int height
= GetHeight();
1344 image
.Create( width
, height
);
1346 unsigned char *data
= image
.GetData();
1347 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1349 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1351 bool hasAlpha
= false ;
1352 bool hasMask
= false ;
1353 int maskBytesPerRow
= 0 ;
1354 unsigned char *alpha
= NULL
;
1355 unsigned char *mask
= NULL
;
1363 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1364 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1370 alpha
= image
.GetAlpha() ;
1375 // The following masking algorithm is the same as well in msw/gtk:
1376 // the colour used as transparent one in wxImage and the one it is
1377 // replaced with when it actually occurs in the bitmap
1378 static const int MASK_RED
= 1;
1379 static const int MASK_GREEN
= 2;
1380 static const int MASK_BLUE
= 3;
1381 static const int MASK_BLUE_REPLACEMENT
= 2;
1383 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1385 unsigned char * maskp
= mask
;
1386 unsigned char * source
= sourcestart
;
1387 unsigned char a
, r
, g
, b
;
1390 for (int xx
= 0; xx
< width
; xx
++)
1392 color
= *((long*) source
) ;
1393 #ifdef WORDS_BIGENDIAN
1394 a
= ((color
&0xFF000000) >> 24) ;
1395 r
= ((color
&0x00FF0000) >> 16) ;
1396 g
= ((color
&0x0000FF00) >> 8) ;
1397 b
= (color
&0x000000FF);
1399 b
= ((color
&0xFF000000) >> 24) ;
1400 g
= ((color
&0x00FF0000) >> 16) ;
1401 r
= ((color
&0x0000FF00) >> 8) ;
1402 a
= (color
&0x000000FF);
1406 if ( *maskp
++ == 0xFF )
1412 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1413 b
= MASK_BLUE_REPLACEMENT
;
1414 #if !wxMAC_USE_CORE_GRAPHICS
1420 else if ( hasAlpha
)
1423 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1424 // this must be non-premultiplied data
1425 if ( a
!= 0xFF && a
!= 0 )
1435 data
[index
+ 1] = g
;
1436 data
[index
+ 2] = b
;
1444 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1449 #endif //wxUSE_IMAGE
1451 bool wxBitmap::SaveFile( const wxString
& filename
,
1452 wxBitmapType type
, const wxPalette
*palette
) const
1454 bool success
= false;
1455 wxBitmapHandler
*handler
= FindHandler(type
);
1459 success
= handler
->SaveFile(this, filename
, type
, palette
);
1464 wxImage image
= ConvertToImage();
1465 success
= image
.SaveFile(filename
, type
);
1467 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1474 bool wxBitmap::IsOk() const
1476 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1479 int wxBitmap::GetHeight() const
1481 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1483 return M_BITMAPDATA
->GetHeight();
1486 int wxBitmap::GetWidth() const
1488 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1490 return M_BITMAPDATA
->GetWidth() ;
1493 int wxBitmap::GetDepth() const
1495 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1497 return M_BITMAPDATA
->GetDepth();
1500 wxMask
*wxBitmap::GetMask() const
1502 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1504 return M_BITMAPDATA
->m_bitmapMask
;
1507 bool wxBitmap::HasAlpha() const
1509 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1511 return M_BITMAPDATA
->HasAlpha() ;
1514 void wxBitmap::SetWidth(int w
)
1517 M_BITMAPDATA
->SetWidth(w
);
1520 void wxBitmap::SetHeight(int h
)
1523 M_BITMAPDATA
->SetHeight(h
);
1526 void wxBitmap::SetDepth(int d
)
1529 M_BITMAPDATA
->SetDepth(d
);
1532 void wxBitmap::SetOk(bool isOk
)
1535 M_BITMAPDATA
->SetOk(isOk
);
1539 wxPalette
*wxBitmap::GetPalette() const
1541 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1543 return &M_BITMAPDATA
->m_bitmapPalette
;
1546 void wxBitmap::SetPalette(const wxPalette
& palette
)
1549 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1551 #endif // wxUSE_PALETTE
1553 void wxBitmap::SetMask(wxMask
*mask
)
1556 // Remove existing mask if there is one.
1557 delete M_BITMAPDATA
->m_bitmapMask
;
1559 M_BITMAPDATA
->m_bitmapMask
= mask
;
1562 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1564 #if !wxMAC_USE_CORE_GRAPHICS
1565 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1567 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1571 // ----------------------------------------------------------------------------
1573 // ----------------------------------------------------------------------------
1580 wxMask::wxMask(const wxMask
&tocopy
)
1584 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1585 m_width
= tocopy
.m_width
;
1586 m_height
= tocopy
.m_height
;
1588 size_t size
= m_bytesPerRow
* m_height
;
1589 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1590 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1591 memcpy( dest
, source
, size
);
1592 m_memBuf
.UngetWriteBuf( size
) ;
1596 // Construct a mask from a bitmap and a colour indicating
1597 // the transparent area
1598 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1601 Create( bitmap
, colour
);
1604 // Construct a mask from a mono bitmap (copies the bitmap).
1605 wxMask::wxMask( const wxBitmap
& bitmap
)
1611 // Construct a mask from a mono bitmap (copies the bitmap).
1613 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1616 Create( data
, width
, height
, bytesPerRow
);
1623 #if wxMAC_USE_CORE_GRAPHICS
1624 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1626 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1628 m_maskBitmap
= NULL
;
1634 m_width
= m_height
= m_bytesPerRow
= 0 ;
1635 m_maskBitmap
= NULL
;
1638 void *wxMask::GetRawAccess() const
1640 return m_memBuf
.GetData() ;
1643 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1644 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1646 void wxMask::RealizeNative()
1650 #if wxMAC_USE_CORE_GRAPHICS
1651 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1653 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1655 m_maskBitmap
= NULL
;
1658 #if wxMAC_USE_CORE_GRAPHICS
1659 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1660 // from MouseTracking sample :
1661 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1662 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1664 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1665 kCGImageAlphaNone
);
1666 CGColorSpaceRelease( colorspace
);
1667 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1669 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1671 OSStatus err
= NewGWorldFromPtr(
1672 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1673 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1674 verify_noerr( err
) ;
1678 // Create a mask from a mono bitmap (copies the bitmap).
1680 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1685 m_bytesPerRow
= bytesPerRow
;
1687 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1694 // Create a mask from a mono bitmap (copies the bitmap).
1695 bool wxMask::Create(const wxBitmap
& bitmap
)
1697 m_width
= bitmap
.GetWidth() ;
1698 m_height
= bitmap
.GetHeight() ;
1699 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1701 size_t size
= m_bytesPerRow
* m_height
;
1702 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1703 wxASSERT( destdatabase
!= NULL
) ;
1705 memset( destdatabase
, 0 , size
) ;
1706 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1708 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1710 unsigned char *destdata
= destdatabase
;
1711 unsigned char r
, g
, b
;
1713 for ( int x
= 0 ; x
< m_width
; ++x
)
1720 if ( ( r
+ g
+ b
) > 0x10 )
1722 *destdata
++ = 0xFF ;
1723 #if !wxMAC_USE_CORE_GRAPHICS
1724 *destdata
++ = 0xFF ;
1725 *destdata
++ = 0xFF ;
1726 *destdata
++ = 0xFF ;
1731 *destdata
++ = 0x00 ;
1732 #if !wxMAC_USE_CORE_GRAPHICS
1733 *destdata
++ = 0x00 ;
1734 *destdata
++ = 0x00 ;
1735 *destdata
++ = 0x00 ;
1741 m_memBuf
.UngetWriteBuf( size
) ;
1747 // Create a mask from a bitmap and a colour indicating
1748 // the transparent area
1749 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1751 m_width
= bitmap
.GetWidth() ;
1752 m_height
= bitmap
.GetHeight() ;
1753 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1755 size_t size
= m_bytesPerRow
* m_height
;
1756 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1757 wxASSERT( destdatabase
!= NULL
) ;
1759 memset( destdatabase
, 0 , size
) ;
1760 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1761 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1763 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1765 unsigned char *srcdata
= srcdatabase
;
1766 unsigned char *destdata
= destdatabase
;
1767 unsigned char r
, g
, b
;
1769 for ( int x
= 0 ; x
< m_width
; ++x
)
1776 if ( colour
== wxColour( r
, g
, b
) )
1778 *destdata
++ = 0xFF ;
1779 #if !wxMAC_USE_CORE_GRAPHICS
1780 *destdata
++ = 0xFF ;
1781 *destdata
++ = 0xFF ;
1782 *destdata
++ = 0xFF ;
1787 *destdata
++ = 0x00 ;
1788 #if !wxMAC_USE_CORE_GRAPHICS
1789 *destdata
++ = 0x00 ;
1790 *destdata
++ = 0x00 ;
1791 *destdata
++ = 0x00 ;
1797 m_memBuf
.UngetWriteBuf( size
) ;
1803 WXHBITMAP
wxMask::GetHBITMAP() const
1805 return m_maskBitmap
;
1808 // ----------------------------------------------------------------------------
1810 // ----------------------------------------------------------------------------
1812 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1814 // ----------------------------------------------------------------------------
1815 // Standard Handlers
1816 // ----------------------------------------------------------------------------
1818 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1820 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1823 inline wxPICTResourceHandler()
1825 SetName(wxT("Macintosh Pict resource"));
1826 SetExtension(wxEmptyString
);
1827 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1830 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1831 int desiredWidth
, int desiredHeight
);
1834 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1837 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1838 const wxString
& name
,
1839 long WXUNUSED(flags
),
1840 int WXUNUSED(desiredWidth
),
1841 int WXUNUSED(desiredHeight
))
1845 wxMacStringToPascal( name
, theName
) ;
1847 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1852 mf
.SetPICT( thePict
) ;
1853 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1855 dc
.SelectObject( *bitmap
) ;
1857 dc
.SelectObject( wxNullBitmap
) ;
1866 void wxBitmap::InitStandardHandlers()
1868 AddHandler( new wxPICTResourceHandler
) ;
1869 AddHandler( new wxICONResourceHandler
) ;
1872 // ----------------------------------------------------------------------------
1873 // raw bitmap access support
1874 // ----------------------------------------------------------------------------
1876 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1879 // no bitmap, no data (raw or otherwise)
1882 data
.m_width
= GetWidth() ;
1883 data
.m_height
= GetHeight() ;
1884 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1886 return BeginRawAccess() ;
1889 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1894 void wxBitmap::UseAlpha()
1896 // remember that we are using alpha channel:
1897 // we'll need to create a proper mask in UngetRawData()
1898 M_BITMAPDATA
->UseAlpha( true );