1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/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 #include "wx/filename.h"
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
34 #include "wx/osx/uma.h"
36 #include "wx/osx/private.h"
39 #ifndef __WXOSX_IPHONE__
40 #include <QuickTime/QuickTime.h>
43 CGColorSpaceRef
wxMacGetGenericRGBColorSpace();
44 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
);
46 // Implementation Notes
47 // --------------------
49 // we are always working with a 32 bit deep pixel buffer
50 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
51 // therefore we have a separate GWorld there for blitting the mask in
53 // under Quartz then content is transformed into a CGImageRef representing the same data
54 // which can be transferred to the GPU by the OS for fast rendering
56 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
58 friend class WXDLLIMPEXP_FWD_CORE wxIcon
;
59 friend class WXDLLIMPEXP_FWD_CORE wxCursor
;
61 wxBitmapRefData(int width
, int height
, int depth
, double logicalscale
);
62 wxBitmapRefData(int width
, int height
, int depth
);
63 wxBitmapRefData(CGContextRef context
);
64 wxBitmapRefData(CGImageRef image
, double scale
);
66 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
68 virtual ~wxBitmapRefData();
70 virtual bool IsOk() const { return m_ok
; }
73 void SetOk( bool isOk
) { m_ok
= isOk
; }
75 void SetWidth( int width
) { m_width
= width
; }
76 void SetHeight( int height
) { m_height
= height
; }
77 void SetDepth( int depth
) { m_depth
= depth
; }
79 int GetWidth() const { return m_width
; }
80 int GetHeight() const { return m_height
; }
81 int GetDepth() const { return m_depth
; }
82 double GetScaleFactor() const { return m_scaleFactor
; }
83 void *GetRawAccess() const;
84 void *BeginRawAccess();
87 bool HasAlpha() const { return m_hasAlpha
; }
88 void UseAlpha( bool useAlpha
);
92 wxPalette m_bitmapPalette
;
93 #endif // wxUSE_PALETTE
95 wxMask
* m_bitmapMask
; // Optional mask
96 CGImageRef
CreateCGImage() const;
98 // returns true if the bitmap has a size that
99 // can be natively transferred into a true icon
100 // if no is returned GetIconRef will still produce
101 // an icon but it will be generated via a PICT and
102 // rescaled to 16 x 16
103 bool HasNativeSize();
105 #ifndef __WXOSX_IPHONE__
106 // caller should increase ref count if needed longer
107 // than the bitmap exists
108 IconRef
GetIconRef();
111 CGContextRef
GetBitmapContext() const;
113 int GetBytesPerRow() const { return m_bytesPerRow
; }
115 bool Create(int width
, int height
, int depth
);
116 bool Create(int width
, int height
, int depth
, double logicalScale
);
117 bool Create( CGImageRef image
, double scale
);
118 bool Create( CGContextRef bitmapcontext
);
126 wxMemoryBuffer m_memBuf
;
127 int m_rawAccessCount
;
129 mutable CGImageRef m_cgImageRef
;
131 #ifndef __WXOSX_IPHONE__
135 CGContextRef m_hBitmap
;
136 double m_scaleFactor
;
140 #define wxOSX_USE_PREMULTIPLIED_ALPHA 1
141 static const int kBestByteAlignement
= 16;
142 static const int kMaskBytesPerPixel
= 1;
144 static int GetBestBytesPerRow( int rawBytes
)
146 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
149 #if wxUSE_GUI && !defined(__WXOSX_IPHONE__)
151 // this is used for more controls than just the wxBitmap button, also for notebooks etc
153 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
155 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
158 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
162 if ( forceType
== 0 )
164 forceType
= kControlContentCGImageRef
;
167 if ( forceType
== kControlContentIconRef
)
170 wxBitmapRefData
* bmp
= bmap
;
172 if ( !bmap
->HasNativeSize() )
174 // as PICT conversion will only result in a 16x16 icon, let's attempt
175 // a few scales for better results
177 int w
= bitmap
.GetWidth() ;
178 int h
= bitmap
.GetHeight() ;
179 int sz
= wxMax( w
, h
) ;
180 if ( sz
== 24 || sz
== 64 )
182 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
183 bmp
= scaleBmp
.GetBitmapData() ;
187 info
->contentType
= kControlContentIconRef
;
188 info
->u
.iconRef
= bmp
->GetIconRef() ;
189 AcquireIconRef( info
->u
.iconRef
) ;
191 else if ( forceType
== kControlContentCGImageRef
)
193 info
->contentType
= kControlContentCGImageRef
;
194 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
199 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
201 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
204 return (CGImageRef
) bmap
->CreateCGImage();
207 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
209 if ( info
->contentType
== kControlContentIconRef
)
211 ReleaseIconRef( info
->u
.iconRef
) ;
213 else if ( info
->contentType
== kControlNoContent
)
215 // there's no bitmap at all, fall through silently
217 else if ( info
->contentType
== kControlContentPictHandle
)
219 // owned by the bitmap, no release here
221 else if ( info
->contentType
== kControlContentCGImageRef
)
223 CGImageRelease( info
->u
.imageRef
) ;
227 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
231 #endif //wxUSE_BMPBUTTON
233 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
235 void wxBitmapRefData::Init()
242 m_bitmapMask
= NULL
;
243 m_cgImageRef
= NULL
;
245 #ifndef __WXOSX_IPHONE__
250 m_rawAccessCount
= 0 ;
255 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
) : wxGDIRefData()
258 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
, tocopy
.m_scaleFactor
);
260 if (tocopy
.m_bitmapMask
)
261 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
262 else if (tocopy
.m_hasAlpha
)
265 unsigned char* dest
= (unsigned char*)GetRawAccess();
266 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
267 size_t numbytes
= m_bytesPerRow
* m_height
;
268 memcpy( dest
, source
, numbytes
);
271 wxBitmapRefData::wxBitmapRefData()
276 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
279 Create( w
, h
, d
) ;
282 wxBitmapRefData::wxBitmapRefData(int w
, int h
, int d
, double logicalscale
)
285 Create( w
, h
, d
, logicalscale
) ;
288 wxBitmapRefData::wxBitmapRefData(CGContextRef context
)
294 wxBitmapRefData::wxBitmapRefData(CGImageRef image
, double scale
)
297 Create( image
, scale
);
299 // code from Technical Q&A QA1509
301 bool wxBitmapRefData::Create(CGImageRef image
, double scale
)
305 m_width
= CGImageGetWidth(image
);
306 m_height
= CGImageGetHeight(image
);
309 m_scaleFactor
= scale
;
311 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
312 size_t size
= m_bytesPerRow
* m_height
;
313 void* data
= m_memBuf
.GetWriteBuf( size
) ;
316 memset( data
, 0 , size
) ;
317 m_memBuf
.UngetWriteBuf( size
) ;
318 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
319 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
321 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
326 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
328 CGRect rect
= CGRectMake(0,0,m_width
,m_height
);
329 CGContextDrawImage(m_hBitmap
, rect
, image
);
331 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
332 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
333 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
336 m_ok
= ( m_hBitmap
!= NULL
) ;
342 bool wxBitmapRefData::Create(CGContextRef context
)
344 if ( context
!= NULL
&& CGBitmapContextGetData(context
) )
347 m_bytesPerRow
= CGBitmapContextGetBytesPerRow(context
);
348 m_width
= CGBitmapContextGetWidth(context
);
349 m_height
= CGBitmapContextGetHeight(context
);
350 m_depth
= CGBitmapContextGetBitsPerPixel(context
) ;
352 // our own contexts conform to this, always.
353 wxASSERT( m_depth
== 32 );
355 // determine content scale
356 CGRect userrect
= CGRectMake(0, 0, 10, 10);
358 devicerect
= CGContextConvertRectToDeviceSpace(context
, userrect
);
359 m_scaleFactor
= devicerect
.size
.height
/ userrect
.size
.height
;
361 CGImageAlphaInfo alpha
= CGBitmapContextGetAlphaInfo(context
);
363 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
372 m_ok
= ( m_hBitmap
!= NULL
) ;
377 bool wxBitmapRefData::Create( int w
, int h
, int d
)
379 m_width
= wxMax(1, w
);
380 m_height
= wxMax(1, h
);
384 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
385 size_t size
= m_bytesPerRow
* m_height
;
386 void* data
= m_memBuf
.GetWriteBuf( size
) ;
389 memset( data
, 0 , size
) ;
390 m_memBuf
.UngetWriteBuf( size
) ;
392 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
393 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
394 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
395 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
397 m_ok
= ( m_hBitmap
!= NULL
) ;
402 bool wxBitmapRefData::Create( int w
, int h
, int d
, double logicalScale
)
404 m_scaleFactor
= logicalScale
;
405 return Create(w
*logicalScale
,h
*logicalScale
,d
);
408 void wxBitmapRefData::UseAlpha( bool use
)
410 if ( m_hasAlpha
== use
)
415 CGContextRelease( m_hBitmap
);
416 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
417 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
418 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
419 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
422 void *wxBitmapRefData::GetRawAccess() const
424 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
425 return m_memBuf
.GetData() ;
428 void *wxBitmapRefData::BeginRawAccess()
430 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
431 wxASSERT( m_rawAccessCount
== 0 ) ;
432 #ifndef __WXOSX_IPHONE__
433 wxASSERT_MSG( m_iconRef
== NULL
,
434 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
438 // we must destroy an existing cached image, as
439 // the bitmap data may change now
442 CGImageRelease( m_cgImageRef
) ;
443 m_cgImageRef
= NULL
;
446 return m_memBuf
.GetData() ;
449 void wxBitmapRefData::EndRawAccess()
451 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
452 wxASSERT( m_rawAccessCount
== 1 ) ;
457 bool wxBitmapRefData::HasNativeSize()
460 int h
= GetHeight() ;
461 int sz
= wxMax( w
, h
) ;
463 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
466 #ifndef __WXOSX_IPHONE__
467 IconRef
wxBitmapRefData::GetIconRef()
469 if ( m_iconRef
== NULL
)
471 // Create Icon Family Handle
473 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
476 int h
= GetHeight() ;
477 int sz
= wxMax( w
, h
) ;
479 OSType dataType
= 0 ;
480 OSType maskType
= 0 ;
482 // since we don't have PICT conversion available, use
483 // the next larger standard icon size
497 else if ( sz
<= 1024)
502 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
504 dataType
= kIconServices1024PixelDataARGB
;
508 dataType
= kIconServices512PixelDataARGB
;
512 dataType
= kIconServices256PixelDataARGB
;
516 dataType
= kIconServices128PixelDataARGB
;
520 dataType
= kIconServices48PixelDataARGB
;
524 dataType
= kIconServices32PixelDataARGB
;
528 dataType
= kIconServices16PixelDataARGB
;
539 size_t datasize
= sz
* sz
* 4 ;
540 Handle data
= NewHandle( datasize
) ;
542 unsigned char* ptr
= (unsigned char*) *data
;
543 memset( ptr
, 0, datasize
);
544 bool hasAlpha
= HasAlpha() ;
545 wxMask
*mask
= m_bitmapMask
;
546 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
547 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
549 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
551 unsigned char * source
= sourcePtr
;
552 unsigned char * masksource
= masksourcePtr
;
553 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
554 unsigned char a
, r
, g
, b
;
556 for ( int x
= 0 ; x
< w
; ++x
)
565 a
= 0xFF - *masksource
++ ;
567 else if ( !hasAlpha
)
571 #if wxOSX_USE_PREMULTIPLIED_ALPHA
572 // this must be non-premultiplied data
573 if ( a
!= 0xFF && a
!= 0 )
590 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
593 wxFAIL_MSG("Error when adding bitmap");
596 DisposeHandle( data
);
600 // setup the header properly
603 Handle maskdata
= NULL
;
604 unsigned char * maskptr
= NULL
;
605 unsigned char * ptr
= NULL
;
606 size_t datasize
, masksize
;
608 datasize
= sz
* sz
* 4 ;
609 data
= NewHandle( datasize
) ;
611 ptr
= (unsigned char*) *data
;
612 memset( ptr
, 0, datasize
) ;
615 maskdata
= NewHandle( masksize
) ;
617 maskptr
= (unsigned char*) *maskdata
;
618 memset( maskptr
, 0 , masksize
) ;
620 bool hasAlpha
= HasAlpha() ;
621 wxMask
*mask
= m_bitmapMask
;
622 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
623 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
625 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
627 unsigned char * source
= sourcePtr
;
628 unsigned char * masksource
= masksourcePtr
;
629 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
630 unsigned char * maskdest
= maskptr
+ y
* sz
;
631 unsigned char a
, r
, g
, b
;
633 for ( int x
= 0 ; x
< w
; ++x
)
646 *maskdest
++ = 0xFF - *masksource
++ ;
654 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
655 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
657 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
658 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
661 HUnlock( maskdata
) ;
662 DisposeHandle( data
) ;
663 DisposeHandle( maskdata
) ;
666 // transform into IconRef
668 // cleaner version existing from 10.3 upwards
669 HLock((Handle
) iconFamily
);
670 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
671 HUnlock((Handle
) iconFamily
);
672 DisposeHandle( (Handle
) iconFamily
) ;
674 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
682 CGImageRef
wxBitmapRefData::CreateCGImage() const
685 wxASSERT( m_rawAccessCount
>= 0 ) ;
687 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
689 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
692 // in order for this code to work properly, wxMask would have to invert black and white
693 // in the native bitmap
696 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
697 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
698 image
= CGImageCreateWithMask( tempImage
, tempMask
);
699 CGImageRelease(tempMask
);
700 CGImageRelease(tempImage
);
704 image
= CGBitmapContextCreateImage( m_hBitmap
);
708 size_t imageSize
= m_height
* m_bytesPerRow
;
709 void * dataBuffer
= m_memBuf
.GetData() ;
712 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
713 wxMemoryBuffer membuf
;
717 alphaInfo
= kCGImageAlphaFirst
;
718 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
719 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
720 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
721 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
722 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
724 unsigned char *sourcemask
= sourcemaskstart
;
725 unsigned char *destalpha
= destalphastart
;
726 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
728 *destalpha
= 0xFF - *sourcemask
;
731 membuf
.UngetWriteBuf( imageSize
);
737 #if wxOSX_USE_PREMULTIPLIED_ALPHA
738 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
740 alphaInfo
= kCGImageAlphaFirst
;
747 CGDataProviderRef dataProvider
= NULL
;
750 // TODO CHECK ALIGNMENT
751 wxMemoryBuffer maskBuf
;
752 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
753 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
754 // copy one color component
756 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
758 unsigned char *bufDataIter
= bufData
+3;
759 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
761 maskBufData
[i
] = *bufDataIter
;
764 maskBuf
.UngetWriteBuf( m_width
* m_height
);
767 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
769 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
773 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
774 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
777 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
778 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
780 CGDataProviderRelease( dataProvider
);
785 image
= m_cgImageRef
;
786 CGImageRetain( image
) ;
789 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
791 // we keep it for later use
792 m_cgImageRef
= image
;
793 CGImageRetain( image
) ;
799 CGContextRef
wxBitmapRefData::GetBitmapContext() const
804 void wxBitmapRefData::Free()
806 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
810 CGImageRelease( m_cgImageRef
) ;
811 m_cgImageRef
= NULL
;
813 #ifndef __WXOSX_IPHONE__
816 ReleaseIconRef( m_iconRef
) ;
822 CGContextRelease(m_hBitmap
);
826 wxDELETE(m_bitmapMask
);
829 wxBitmapRefData::~wxBitmapRefData()
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
842 bool created
= false ;
843 int w
= icon
.GetWidth() ;
844 int h
= icon
.GetHeight() ;
847 #ifdef __WXOSX_CARBON__
848 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
850 IconFamilyHandle iconFamily
= NULL
;
851 Handle imagehandle
= NewHandle( 0 ) ;
852 Handle maskhandle
= NewHandle( 0 ) ;
856 IconSelectorValue selector
= 0 ;
861 dataType
= kThumbnail32BitData
;
862 maskType
= kThumbnail8BitMask
;
863 selector
= kSelectorAllAvailableData
;
867 dataType
= kHuge32BitData
;
868 maskType
= kHuge8BitMask
;
869 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
873 dataType
= kLarge32BitData
;
874 maskType
= kLarge8BitMask
;
875 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
879 dataType
= kSmall32BitData
;
880 maskType
= kSmall8BitMask
;
881 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
888 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
890 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
891 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
892 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
893 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
895 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
897 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
898 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
902 unsigned char *source
= (unsigned char *) *imagehandle
;
903 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
904 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
906 for ( int y
= 0 ; y
< h
; ++y
)
908 for ( int x
= 0 ; x
< w
; ++x
)
910 unsigned char a
= *sourcemask
++;
913 #if wxOSX_USE_PREMULTIPLIED_ALPHA
914 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
915 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
916 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
918 *destination
++ = *source
++ ;
919 *destination
++ = *source
++ ;
920 *destination
++ = *source
++ ;
926 DisposeHandle( imagehandle
) ;
927 DisposeHandle( maskhandle
) ;
931 DisposeHandle( (Handle
) iconFamily
) ;
937 dc
.SelectObject( *this ) ;
938 dc
.DrawIcon( icon
, 0 , 0 ) ;
939 dc
.SelectObject( wxNullBitmap
) ;
945 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
947 wxBitmapRefData
* bitmapRefData
;
949 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
951 if (bitmapRefData
->IsOk())
955 int linesize
= the_width
/ 8;
959 unsigned char* linestart
= (unsigned char*) bits
;
960 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
962 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
964 unsigned char* destination
= destptr
;
965 int index
, bit
, mask
;
967 for ( int x
= 0 ; x
< the_width
; ++x
)
973 if ( linestart
[index
] & mask
)
975 *destination
++ = 0xFF ;
982 *destination
++ = 0xFF ;
983 *destination
++ = 0xFF ;
984 *destination
++ = 0xFF ;
985 *destination
++ = 0xFF ;
994 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
996 } /* bitmapRefData->IsOk() */
999 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1001 (void) Create(data
, type
, width
, height
, depth
);
1004 wxBitmap::wxBitmap(int width
, int height
, const wxDC
& dc
)
1006 (void)Create(width
, height
, dc
);
1009 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1011 LoadFile(filename
, type
);
1014 wxBitmap::wxBitmap(CGImageRef image
, double scale
)
1016 (void) Create(image
,scale
);
1019 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
1021 return new wxBitmapRefData
;
1024 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1026 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
1029 void * wxBitmap::GetRawAccess() const
1031 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1033 return M_BITMAPDATA
->GetRawAccess() ;
1036 void * wxBitmap::BeginRawAccess()
1038 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1040 return M_BITMAPDATA
->BeginRawAccess() ;
1043 void wxBitmap::EndRawAccess()
1045 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
1047 M_BITMAPDATA
->EndRawAccess() ;
1050 CGImageRef
wxBitmap::CreateCGImage() const
1052 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1054 return M_BITMAPDATA
->CreateCGImage() ;
1057 #ifndef __WXOSX_IPHONE__
1058 IconRef
wxBitmap::GetIconRef() const
1060 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1062 return M_BITMAPDATA
->GetIconRef() ;
1065 IconRef
wxBitmap::CreateIconRef() const
1067 IconRef icon
= GetIconRef();
1068 verify_noerr( AcquireIconRef(icon
) );
1075 wxBitmap::wxBitmap(WX_NSImage image
)
1077 (void)Create(image
);
1080 bool wxBitmap::Create(WX_NSImage image
)
1082 return Create(wxOSXCreateBitmapContextFromNSImage(image
));
1085 wxBitmap::wxBitmap(CGContextRef bitmapcontext
)
1087 (void)Create(bitmapcontext
);
1090 bool wxBitmap::Create(CGContextRef bitmapcontext
)
1094 m_refData
= new wxBitmapRefData( bitmapcontext
);
1096 return M_BITMAPDATA
->IsOk() ;
1099 WX_NSImage
wxBitmap::GetNSImage() const
1101 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1102 return wxOSXGetNSImageFromCGImage( cgimage
, GetScaleFactor() );
1107 #if wxOSX_USE_IPHONE
1109 WX_UIImage
wxBitmap::GetUIImage() const
1111 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1112 return wxOSXGetUIImageFromCGImage( cgimage
);
1116 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1118 wxCHECK_MSG( IsOk() &&
1119 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1120 (rect
.x
+rect
.width
<= GetWidth()) &&
1121 (rect
.y
+rect
.height
<= GetHeight()),
1122 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1125 double scale
= GetScaleFactor();
1126 ret
.CreateScaled( rect
.width
, rect
.height
, GetDepth(), scale
);
1127 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1129 int destwidth
= rect
.width
*scale
;
1130 int destheight
= rect
.height
*scale
;
1133 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1134 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1135 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1137 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1139 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1140 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1141 unsigned char *source
= sourcedata
+ int(rect
.x
* scale
* 4 + rect
.y
*scale
* sourcelinesize
) ;
1142 unsigned char *dest
= destdata
;
1144 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1146 memcpy( dest
, source
, destlinesize
) ;
1149 ret
.EndRawAccess() ;
1153 if ( M_BITMAPDATA
->m_bitmapMask
)
1155 wxMemoryBuffer maskbuf
;
1156 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1157 size_t maskbufsize
= rowBytes
* destheight
;
1159 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1160 int destlinesize
= rowBytes
;
1162 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1163 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1164 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1166 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1168 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1169 unsigned char *dest
= destdata
;
1171 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1173 memcpy( dest
, source
, destlinesize
) ;
1176 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1178 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1180 else if ( HasAlpha() )
1186 bool wxBitmap::Create(int w
, int h
, int d
)
1191 d
= wxDisplayDepth() ;
1193 m_refData
= new wxBitmapRefData( w
, h
, d
);
1195 return M_BITMAPDATA
->IsOk() ;
1198 bool wxBitmap::Create(int w
, int h
, const wxDC
& dc
)
1200 double factor
= dc
.GetContentScaleFactor();
1201 return CreateScaled(w
,h
,wxBITMAP_SCREEN_DEPTH
, factor
);
1204 bool wxBitmap::CreateScaled(int w
, int h
, int d
, double logicalScale
)
1209 d
= wxDisplayDepth() ;
1211 m_refData
= new wxBitmapRefData( w
, h
, d
, logicalScale
);
1213 return M_BITMAPDATA
->IsOk() ;
1216 bool wxBitmap::Create(CGImageRef image
, double scale
)
1220 m_refData
= new wxBitmapRefData( image
, scale
);
1222 return M_BITMAPDATA
->IsOk() ;
1225 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1229 wxBitmapHandler
*handler
= FindHandler(type
);
1233 m_refData
= new wxBitmapRefData
;
1235 return handler
->LoadFile(this, filename
, type
, -1, -1);
1241 wxString fname
= filename
;
1243 if ( type
== wxBITMAP_TYPE_PNG
)
1245 if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 )
1247 wxFileName
fn(filename
);
1249 fn
.SetName(fn
.GetName()+"@2x");
1253 fname
= fn
.GetFullPath();
1259 wxImage
loadimage(fname
, type
);
1260 if (loadimage
.IsOk())
1262 *this = wxBitmap(loadimage
,-1,scale
);
1269 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1274 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1278 m_refData
= new wxBitmapRefData
;
1280 wxBitmapHandler
*handler
= FindHandler(type
);
1282 if ( handler
== NULL
)
1284 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1289 return handler
->Create(this, data
, type
, width
, height
, depth
);
1294 wxBitmap::wxBitmap(const wxImage
& image
, int depth
, double scale
)
1296 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1298 // width and height of the device-dependent bitmap
1299 int width
= image
.GetWidth();
1300 int height
= image
.GetHeight();
1302 wxBitmapRefData
* bitmapRefData
;
1304 m_refData
= bitmapRefData
= new wxBitmapRefData( width
/scale
, height
/scale
, depth
, scale
) ;
1306 if ( bitmapRefData
->IsOk())
1310 bool hasAlpha
= false ;
1312 if ( image
.HasMask() )
1314 // takes precedence, don't mix with alpha info
1318 hasAlpha
= image
.HasAlpha() ;
1324 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1325 register unsigned char* data
= image
.GetData();
1326 if ( destinationstart
!= NULL
&& data
!= NULL
)
1328 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1329 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1331 unsigned char * destination
= destinationstart
;
1332 for (int x
= 0; x
< width
; x
++)
1336 const unsigned char a
= *alpha
++;
1337 *destination
++ = a
;
1339 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1340 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1341 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1342 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1344 *destination
++ = *data
++ ;
1345 *destination
++ = *data
++ ;
1346 *destination
++ = *data
++ ;
1351 *destination
++ = 0xFF ;
1352 *destination
++ = *data
++ ;
1353 *destination
++ = *data
++ ;
1354 *destination
++ = *data
++ ;
1361 if ( image
.HasMask() )
1362 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1363 } /* bitmapRefData->IsOk() */
1366 wxImage
wxBitmap::ConvertToImage() const
1370 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1372 // create an wxImage object
1373 int width
= GetWidth();
1374 int height
= GetHeight();
1375 image
.Create( width
, height
);
1377 unsigned char *data
= image
.GetData();
1378 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1380 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1382 bool hasAlpha
= false ;
1383 bool hasMask
= false ;
1384 int maskBytesPerRow
= 0 ;
1385 unsigned char *alpha
= NULL
;
1386 unsigned char *mask
= NULL
;
1394 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1395 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1401 alpha
= image
.GetAlpha() ;
1406 // The following masking algorithm is the same as well in msw/gtk:
1407 // the colour used as transparent one in wxImage and the one it is
1408 // replaced with when it actually occurs in the bitmap
1409 static const int MASK_RED
= 1;
1410 static const int MASK_GREEN
= 2;
1411 static const int MASK_BLUE
= 3;
1412 static const int MASK_BLUE_REPLACEMENT
= 2;
1414 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1416 unsigned char * maskp
= mask
;
1417 unsigned char * source
= sourcestart
;
1418 unsigned char a
, r
, g
, b
;
1421 for (int xx
= 0; xx
< width
; xx
++)
1423 color
= *((long*) source
) ;
1424 #ifdef WORDS_BIGENDIAN
1425 a
= ((color
&0xFF000000) >> 24) ;
1426 r
= ((color
&0x00FF0000) >> 16) ;
1427 g
= ((color
&0x0000FF00) >> 8) ;
1428 b
= (color
&0x000000FF);
1430 b
= ((color
&0xFF000000) >> 24) ;
1431 g
= ((color
&0x00FF0000) >> 16) ;
1432 r
= ((color
&0x0000FF00) >> 8) ;
1433 a
= (color
&0x000000FF);
1437 if ( *maskp
++ == 0xFF )
1443 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1444 b
= MASK_BLUE_REPLACEMENT
;
1446 else if ( hasAlpha
)
1449 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1450 // this must be non-premultiplied data
1451 if ( a
!= 0xFF && a
!= 0 )
1461 data
[index
+ 1] = g
;
1462 data
[index
+ 2] = b
;
1470 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1475 #endif //wxUSE_IMAGE
1477 bool wxBitmap::SaveFile( const wxString
& filename
,
1478 wxBitmapType type
, const wxPalette
*palette
) const
1480 bool success
= false;
1481 wxBitmapHandler
*handler
= FindHandler(type
);
1485 success
= handler
->SaveFile(this, filename
, type
, palette
);
1490 wxImage image
= ConvertToImage();
1491 success
= image
.SaveFile(filename
, type
);
1493 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1500 int wxBitmap::GetHeight() const
1502 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1504 return M_BITMAPDATA
->GetHeight();
1507 int wxBitmap::GetWidth() const
1509 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1511 return M_BITMAPDATA
->GetWidth() ;
1514 double wxBitmap::GetScaleFactor() const
1516 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1518 return M_BITMAPDATA
->GetScaleFactor() ;
1521 int wxBitmap::GetDepth() const
1523 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1525 return M_BITMAPDATA
->GetDepth();
1528 wxMask
*wxBitmap::GetMask() const
1530 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1532 return M_BITMAPDATA
->m_bitmapMask
;
1535 bool wxBitmap::HasAlpha() const
1537 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1539 return M_BITMAPDATA
->HasAlpha() ;
1542 void wxBitmap::SetWidth(int w
)
1545 M_BITMAPDATA
->SetWidth(w
);
1548 void wxBitmap::SetHeight(int h
)
1551 M_BITMAPDATA
->SetHeight(h
);
1554 void wxBitmap::SetDepth(int d
)
1557 M_BITMAPDATA
->SetDepth(d
);
1560 void wxBitmap::SetOk(bool isOk
)
1563 M_BITMAPDATA
->SetOk(isOk
);
1567 wxPalette
*wxBitmap::GetPalette() const
1569 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1571 return &M_BITMAPDATA
->m_bitmapPalette
;
1574 void wxBitmap::SetPalette(const wxPalette
& palette
)
1577 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1579 #endif // wxUSE_PALETTE
1581 void wxBitmap::SetMask(wxMask
*mask
)
1584 // Remove existing mask if there is one.
1585 delete M_BITMAPDATA
->m_bitmapMask
;
1587 M_BITMAPDATA
->m_bitmapMask
= mask
;
1590 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1594 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1597 // ----------------------------------------------------------------------------
1599 // ----------------------------------------------------------------------------
1606 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1610 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1611 m_width
= tocopy
.m_width
;
1612 m_height
= tocopy
.m_height
;
1614 size_t size
= m_bytesPerRow
* m_height
;
1615 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1616 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1617 memcpy( dest
, source
, size
);
1618 m_memBuf
.UngetWriteBuf( size
) ;
1622 // Construct a mask from a bitmap and a colour indicating
1623 // the transparent area
1624 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1627 Create( bitmap
, colour
);
1630 // Construct a mask from a mono bitmap (copies the bitmap).
1631 wxMask::wxMask( const wxBitmap
& bitmap
)
1637 // Construct a mask from a mono bitmap (copies the bitmap).
1639 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1642 Create( data
, width
, height
, bytesPerRow
);
1649 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1650 m_maskBitmap
= NULL
;
1656 m_width
= m_height
= m_bytesPerRow
= 0 ;
1657 m_maskBitmap
= NULL
;
1660 void *wxMask::GetRawAccess() const
1662 return m_memBuf
.GetData() ;
1665 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1666 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1668 void wxMask::RealizeNative()
1672 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1673 m_maskBitmap
= NULL
;
1676 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1677 // from MouseTracking sample :
1678 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1679 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1681 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1682 kCGImageAlphaNone
);
1683 CGColorSpaceRelease( colorspace
);
1684 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1687 // Create a mask from a mono bitmap (copies the bitmap).
1689 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1694 m_bytesPerRow
= bytesPerRow
;
1696 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1703 // Create a mask from a mono bitmap (copies the bitmap).
1704 bool wxMask::Create(const wxBitmap
& bitmap
)
1706 m_width
= bitmap
.GetWidth() ;
1707 m_height
= bitmap
.GetHeight() ;
1708 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1710 size_t size
= m_bytesPerRow
* m_height
;
1711 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1712 wxASSERT( destdatabase
!= NULL
) ;
1716 memset( destdatabase
, 0 , size
) ;
1717 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1719 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1721 unsigned char *destdata
= destdatabase
;
1722 unsigned char r
, g
, b
;
1724 for ( int x
= 0 ; x
< m_width
; ++x
)
1731 if ( ( r
+ g
+ b
) > 0x10 )
1732 *destdata
++ = 0xFF ;
1734 *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
) ;
1756 if ( destdatabase
!= NULL
)
1758 memset( destdatabase
, 0 , size
) ;
1759 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1760 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1762 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1764 unsigned char *srcdata
= srcdatabase
;
1765 unsigned char *destdata
= destdatabase
;
1766 unsigned char r
, g
, b
;
1768 for ( int x
= 0 ; x
< m_width
; ++x
)
1775 if ( colour
== wxColour( r
, g
, b
) )
1776 *destdata
++ = 0xFF ;
1778 *destdata
++ = 0x00 ;
1782 m_memBuf
.UngetWriteBuf( size
) ;
1788 wxBitmap
wxMask::GetBitmap() const
1790 wxBitmap
bitmap(m_width
, m_height
, 1);
1791 unsigned char* dst
= static_cast<unsigned char*>(bitmap
.BeginRawAccess());
1792 const int dst_stride
= bitmap
.GetBitmapData()->GetBytesPerRow();
1793 const unsigned char* src
= static_cast<unsigned char*>(GetRawAccess());
1794 for (int j
= 0; j
< m_height
; j
++, src
+= m_bytesPerRow
, dst
+= dst_stride
)
1796 unsigned char* d
= dst
;
1797 for (int i
= 0; i
< m_width
; i
++)
1799 const unsigned char byte
= src
[i
];
1806 bitmap
.EndRawAccess();
1810 WXHBITMAP
wxMask::GetHBITMAP() const
1812 return m_maskBitmap
;
1815 // ----------------------------------------------------------------------------
1816 // Standard Handlers
1817 // ----------------------------------------------------------------------------
1819 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1821 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1824 inline wxBundleResourceHandler()
1828 virtual bool LoadFile(wxBitmap
*bitmap
,
1829 const wxString
& name
,
1835 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1837 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1839 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1842 inline wxPNGResourceHandler()
1844 SetName(wxT("PNG resource"));
1845 SetExtension("PNG");
1846 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1850 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1852 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1854 DECLARE_DYNAMIC_CLASS(wxJPEGResourceHandler
)
1857 inline wxJPEGResourceHandler()
1859 SetName(wxT("JPEG resource"));
1860 SetExtension("JPEG");
1861 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1865 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1867 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1868 const wxString
& name
,
1869 wxBitmapType
WXUNUSED(type
),
1870 int WXUNUSED(desiredWidth
),
1871 int WXUNUSED(desiredHeight
))
1873 wxString ext
= GetExtension().Lower();
1874 wxCFStringRef
resname(name
);
1875 wxCFStringRef
resname2x(name
+"@2x");
1876 wxCFStringRef
restype(ext
);
1879 wxCFRef
<CFURLRef
> imageURL
;
1881 if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 )
1883 imageURL
.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname2x
, restype
, NULL
));
1887 if ( imageURL
.get() == NULL
)
1889 imageURL
.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1893 if ( imageURL
.get() != NULL
)
1895 // Create the data provider object
1896 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1897 CGImageRef image
= NULL
;
1899 if ( ext
== "jpeg" )
1900 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1901 kCGRenderingIntentDefault
);
1902 else if ( ext
== "png" )
1903 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1904 kCGRenderingIntentDefault
);
1905 if ( image
!= NULL
)
1907 bitmap
->Create(image
,scale
);
1908 CGImageRelease(image
);
1916 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1918 wxCFRef
<CGDataProviderRef
>
1919 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1921 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1922 kCGRenderingIntentDefault
));
1924 return wxBitmap(image
);
1927 void wxBitmap::InitStandardHandlers()
1929 #if wxOSX_USE_COCOA_OR_CARBON
1930 AddHandler( new wxICONResourceHandler
) ;
1932 AddHandler( new wxPNGResourceHandler
);
1933 AddHandler( new wxJPEGResourceHandler
);
1936 // ----------------------------------------------------------------------------
1937 // raw bitmap access support
1938 // ----------------------------------------------------------------------------
1940 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1943 // no bitmap, no data (raw or otherwise)
1946 data
.m_width
= GetWidth() ;
1947 data
.m_height
= GetHeight() ;
1948 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1950 return BeginRawAccess() ;
1953 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1958 void wxBitmap::UseAlpha()
1960 // remember that we are using alpha channel:
1961 // we'll need to create a proper mask in UngetRawData()
1962 M_BITMAPDATA
->UseAlpha( true );