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 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
32 #include "wx/osx/uma.h"
34 #include "wx/osx/private.h"
37 #ifndef __WXOSX_IPHONE__
38 #include <QuickTime/QuickTime.h>
41 CGColorSpaceRef
wxMacGetGenericRGBColorSpace();
42 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
);
44 // Implementation Notes
45 // --------------------
47 // we are always working with a 32 bit deep pixel buffer
48 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
49 // therefore we have a separate GWorld there for blitting the mask in
51 // under Quartz then content is transformed into a CGImageRef representing the same data
52 // which can be transferred to the GPU by the OS for fast rendering
54 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
56 friend class WXDLLIMPEXP_FWD_CORE wxIcon
;
57 friend class WXDLLIMPEXP_FWD_CORE wxCursor
;
59 wxBitmapRefData(int width
, int height
, int depth
, double logicalscale
);
60 wxBitmapRefData(int width
, int height
, int depth
);
61 wxBitmapRefData(CGContextRef context
);
62 wxBitmapRefData(CGImageRef image
, double scale
);
64 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
66 virtual ~wxBitmapRefData();
68 virtual bool IsOk() const { return m_ok
; }
71 void SetOk( bool isOk
) { m_ok
= isOk
; }
73 void SetWidth( int width
) { m_width
= width
; }
74 void SetHeight( int height
) { m_height
= height
; }
75 void SetDepth( int depth
) { m_depth
= depth
; }
77 int GetWidth() const { return m_width
; }
78 int GetHeight() const { return m_height
; }
79 int GetDepth() const { return m_depth
; }
80 double GetScaleFactor() const { return m_scaleFactor
; }
81 void *GetRawAccess() const;
82 void *BeginRawAccess();
85 bool HasAlpha() const { return m_hasAlpha
; }
86 void UseAlpha( bool useAlpha
);
90 wxPalette m_bitmapPalette
;
91 #endif // wxUSE_PALETTE
93 wxMask
* m_bitmapMask
; // Optional mask
94 CGImageRef
CreateCGImage() const;
96 // returns true if the bitmap has a size that
97 // can be natively transferred into a true icon
98 // if no is returned GetIconRef will still produce
99 // an icon but it will be generated via a PICT and
100 // rescaled to 16 x 16
101 bool HasNativeSize();
103 #ifndef __WXOSX_IPHONE__
104 // caller should increase ref count if needed longer
105 // than the bitmap exists
106 IconRef
GetIconRef();
109 CGContextRef
GetBitmapContext() const;
111 int GetBytesPerRow() const { return m_bytesPerRow
; }
113 bool Create(int width
, int height
, int depth
);
114 bool Create(int width
, int height
, int depth
, double logicalScale
);
115 bool Create( CGImageRef image
, double scale
);
116 bool Create( CGContextRef bitmapcontext
);
124 wxMemoryBuffer m_memBuf
;
125 int m_rawAccessCount
;
127 mutable CGImageRef m_cgImageRef
;
129 #ifndef __WXOSX_IPHONE__
133 CGContextRef m_hBitmap
;
134 double m_scaleFactor
;
138 #define wxOSX_USE_PREMULTIPLIED_ALPHA 1
139 static const int kBestByteAlignement
= 16;
140 static const int kMaskBytesPerPixel
= 1;
142 static int GetBestBytesPerRow( int rawBytes
)
144 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
147 #if wxUSE_GUI && !defined(__WXOSX_IPHONE__)
149 // this is used for more controls than just the wxBitmap button, also for notebooks etc
151 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
153 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
156 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
160 if ( forceType
== 0 )
162 forceType
= kControlContentCGImageRef
;
165 if ( forceType
== kControlContentIconRef
)
168 wxBitmapRefData
* bmp
= bmap
;
170 if ( !bmap
->HasNativeSize() )
172 // as PICT conversion will only result in a 16x16 icon, let's attempt
173 // a few scales for better results
175 int w
= bitmap
.GetWidth() ;
176 int h
= bitmap
.GetHeight() ;
177 int sz
= wxMax( w
, h
) ;
178 if ( sz
== 24 || sz
== 64 )
180 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
181 bmp
= scaleBmp
.GetBitmapData() ;
185 info
->contentType
= kControlContentIconRef
;
186 info
->u
.iconRef
= bmp
->GetIconRef() ;
187 AcquireIconRef( info
->u
.iconRef
) ;
189 else if ( forceType
== kControlContentCGImageRef
)
191 info
->contentType
= kControlContentCGImageRef
;
192 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
197 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
199 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
202 return (CGImageRef
) bmap
->CreateCGImage();
205 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
207 if ( info
->contentType
== kControlContentIconRef
)
209 ReleaseIconRef( info
->u
.iconRef
) ;
211 else if ( info
->contentType
== kControlNoContent
)
213 // there's no bitmap at all, fall through silently
215 else if ( info
->contentType
== kControlContentPictHandle
)
217 // owned by the bitmap, no release here
219 else if ( info
->contentType
== kControlContentCGImageRef
)
221 CGImageRelease( info
->u
.imageRef
) ;
225 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
229 #endif //wxUSE_BMPBUTTON
231 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
233 void wxBitmapRefData::Init()
240 m_bitmapMask
= NULL
;
241 m_cgImageRef
= NULL
;
243 #ifndef __WXOSX_IPHONE__
248 m_rawAccessCount
= 0 ;
253 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
) : wxGDIRefData()
256 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
, tocopy
.m_scaleFactor
);
258 if (tocopy
.m_bitmapMask
)
259 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
260 else if (tocopy
.m_hasAlpha
)
263 unsigned char* dest
= (unsigned char*)GetRawAccess();
264 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
265 size_t numbytes
= m_bytesPerRow
* m_height
;
266 memcpy( dest
, source
, numbytes
);
269 wxBitmapRefData::wxBitmapRefData()
274 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
277 Create( w
, h
, d
) ;
280 wxBitmapRefData::wxBitmapRefData(int w
, int h
, int d
, double logicalscale
)
283 Create( w
, h
, d
, logicalscale
) ;
286 wxBitmapRefData::wxBitmapRefData(CGContextRef context
)
292 wxBitmapRefData::wxBitmapRefData(CGImageRef image
, double scale
)
295 Create( image
, scale
);
297 // code from Technical Q&A QA1509
299 bool wxBitmapRefData::Create(CGImageRef image
, double scale
)
303 m_width
= CGImageGetWidth(image
);
304 m_height
= CGImageGetHeight(image
);
307 m_scaleFactor
= scale
;
309 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
310 size_t size
= m_bytesPerRow
* m_height
;
311 void* data
= m_memBuf
.GetWriteBuf( size
) ;
314 memset( data
, 0 , size
) ;
315 m_memBuf
.UngetWriteBuf( size
) ;
316 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
317 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
319 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
324 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
326 CGRect rect
= CGRectMake(0,0,m_width
,m_height
);
327 CGContextDrawImage(m_hBitmap
, rect
, image
);
329 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
330 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
331 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
334 m_ok
= ( m_hBitmap
!= NULL
) ;
340 bool wxBitmapRefData::Create(CGContextRef context
)
342 if ( context
!= NULL
&& CGBitmapContextGetData(context
) )
345 m_bytesPerRow
= CGBitmapContextGetBytesPerRow(context
);
346 m_width
= CGBitmapContextGetWidth(context
);
347 m_height
= CGBitmapContextGetHeight(context
);
348 m_depth
= CGBitmapContextGetBitsPerPixel(context
) ;
350 // our own contexts conform to this, always.
351 wxASSERT( m_depth
== 32 );
353 // determine content scale
354 CGRect userrect
= CGRectMake(0, 0, 10, 10);
356 devicerect
= CGContextConvertRectToDeviceSpace(context
, userrect
);
357 m_scaleFactor
= devicerect
.size
.height
/ userrect
.size
.height
;
359 CGImageAlphaInfo alpha
= CGBitmapContextGetAlphaInfo(context
);
361 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
370 m_ok
= ( m_hBitmap
!= NULL
) ;
375 bool wxBitmapRefData::Create( int w
, int h
, int d
)
377 m_width
= wxMax(1, w
);
378 m_height
= wxMax(1, h
);
382 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
383 size_t size
= m_bytesPerRow
* m_height
;
384 void* data
= m_memBuf
.GetWriteBuf( size
) ;
387 memset( data
, 0 , size
) ;
388 m_memBuf
.UngetWriteBuf( size
) ;
390 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
391 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
392 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
393 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
395 m_ok
= ( m_hBitmap
!= NULL
) ;
400 bool wxBitmapRefData::Create( int w
, int h
, int d
, double logicalScale
)
402 m_scaleFactor
= logicalScale
;
403 return Create(w
*logicalScale
,h
*logicalScale
,d
);
406 void wxBitmapRefData::UseAlpha( bool use
)
408 if ( m_hasAlpha
== use
)
413 CGContextRelease( m_hBitmap
);
414 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
415 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
416 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
417 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
420 void *wxBitmapRefData::GetRawAccess() const
422 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
423 return m_memBuf
.GetData() ;
426 void *wxBitmapRefData::BeginRawAccess()
428 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
429 wxASSERT( m_rawAccessCount
== 0 ) ;
430 #ifndef __WXOSX_IPHONE__
431 wxASSERT_MSG( m_iconRef
== NULL
,
432 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
436 // we must destroy an existing cached image, as
437 // the bitmap data may change now
440 CGImageRelease( m_cgImageRef
) ;
441 m_cgImageRef
= NULL
;
444 return m_memBuf
.GetData() ;
447 void wxBitmapRefData::EndRawAccess()
449 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
450 wxASSERT( m_rawAccessCount
== 1 ) ;
455 bool wxBitmapRefData::HasNativeSize()
458 int h
= GetHeight() ;
459 int sz
= wxMax( w
, h
) ;
461 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
464 #ifndef __WXOSX_IPHONE__
465 IconRef
wxBitmapRefData::GetIconRef()
467 if ( m_iconRef
== NULL
)
469 // Create Icon Family Handle
471 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
474 int h
= GetHeight() ;
475 int sz
= wxMax( w
, h
) ;
477 OSType dataType
= 0 ;
478 OSType maskType
= 0 ;
480 // since we don't have PICT conversion available, use
481 // the next larger standard icon size
495 else if ( sz
<= 1024)
500 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
502 dataType
= kIconServices1024PixelDataARGB
;
506 dataType
= kIconServices512PixelDataARGB
;
510 dataType
= kIconServices256PixelDataARGB
;
514 dataType
= kIconServices128PixelDataARGB
;
518 dataType
= kIconServices48PixelDataARGB
;
522 dataType
= kIconServices32PixelDataARGB
;
526 dataType
= kIconServices16PixelDataARGB
;
537 size_t datasize
= sz
* sz
* 4 ;
538 Handle data
= NewHandle( datasize
) ;
540 unsigned char* ptr
= (unsigned char*) *data
;
541 memset( ptr
, 0, datasize
);
542 bool hasAlpha
= HasAlpha() ;
543 wxMask
*mask
= m_bitmapMask
;
544 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
545 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
547 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
549 unsigned char * source
= sourcePtr
;
550 unsigned char * masksource
= masksourcePtr
;
551 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
552 unsigned char a
, r
, g
, b
;
554 for ( int x
= 0 ; x
< w
; ++x
)
563 a
= 0xFF - *masksource
++ ;
565 else if ( !hasAlpha
)
569 #if wxOSX_USE_PREMULTIPLIED_ALPHA
570 // this must be non-premultiplied data
571 if ( a
!= 0xFF && a
!= 0 )
588 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
591 wxFAIL_MSG("Error when adding bitmap");
594 DisposeHandle( data
);
598 // setup the header properly
601 Handle maskdata
= NULL
;
602 unsigned char * maskptr
= NULL
;
603 unsigned char * ptr
= NULL
;
604 size_t datasize
, masksize
;
606 datasize
= sz
* sz
* 4 ;
607 data
= NewHandle( datasize
) ;
609 ptr
= (unsigned char*) *data
;
610 memset( ptr
, 0, datasize
) ;
613 maskdata
= NewHandle( masksize
) ;
615 maskptr
= (unsigned char*) *maskdata
;
616 memset( maskptr
, 0 , masksize
) ;
618 bool hasAlpha
= HasAlpha() ;
619 wxMask
*mask
= m_bitmapMask
;
620 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
621 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
623 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
625 unsigned char * source
= sourcePtr
;
626 unsigned char * masksource
= masksourcePtr
;
627 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
628 unsigned char * maskdest
= maskptr
+ y
* sz
;
629 unsigned char a
, r
, g
, b
;
631 for ( int x
= 0 ; x
< w
; ++x
)
644 *maskdest
++ = 0xFF - *masksource
++ ;
652 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
653 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
655 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
656 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
659 HUnlock( maskdata
) ;
660 DisposeHandle( data
) ;
661 DisposeHandle( maskdata
) ;
664 // transform into IconRef
666 // cleaner version existing from 10.3 upwards
667 HLock((Handle
) iconFamily
);
668 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
669 HUnlock((Handle
) iconFamily
);
670 DisposeHandle( (Handle
) iconFamily
) ;
672 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
680 CGImageRef
wxBitmapRefData::CreateCGImage() const
683 wxASSERT( m_rawAccessCount
>= 0 ) ;
685 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
687 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
690 // in order for this code to work properly, wxMask would have to invert black and white
691 // in the native bitmap
694 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
695 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
696 image
= CGImageCreateWithMask( tempImage
, tempMask
);
697 CGImageRelease(tempMask
);
698 CGImageRelease(tempImage
);
702 image
= CGBitmapContextCreateImage( m_hBitmap
);
706 size_t imageSize
= m_height
* m_bytesPerRow
;
707 void * dataBuffer
= m_memBuf
.GetData() ;
710 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
711 wxMemoryBuffer membuf
;
715 alphaInfo
= kCGImageAlphaFirst
;
716 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
717 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
718 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
719 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
720 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
722 unsigned char *sourcemask
= sourcemaskstart
;
723 unsigned char *destalpha
= destalphastart
;
724 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
726 *destalpha
= 0xFF - *sourcemask
;
729 membuf
.UngetWriteBuf( imageSize
);
735 #if wxOSX_USE_PREMULTIPLIED_ALPHA
736 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
738 alphaInfo
= kCGImageAlphaFirst
;
745 CGDataProviderRef dataProvider
= NULL
;
748 // TODO CHECK ALIGNMENT
749 wxMemoryBuffer maskBuf
;
750 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
751 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
752 // copy one color component
754 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
756 unsigned char *bufDataIter
= bufData
+3;
757 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
759 maskBufData
[i
] = *bufDataIter
;
762 maskBuf
.UngetWriteBuf( m_width
* m_height
);
765 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
767 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
771 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
772 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
775 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
776 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
778 CGDataProviderRelease( dataProvider
);
783 image
= m_cgImageRef
;
784 CGImageRetain( image
) ;
787 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
789 // we keep it for later use
790 m_cgImageRef
= image
;
791 CGImageRetain( image
) ;
797 CGContextRef
wxBitmapRefData::GetBitmapContext() const
802 void wxBitmapRefData::Free()
804 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
808 CGImageRelease( m_cgImageRef
) ;
809 m_cgImageRef
= NULL
;
811 #ifndef __WXOSX_IPHONE__
814 ReleaseIconRef( m_iconRef
) ;
820 CGContextRelease(m_hBitmap
);
824 wxDELETE(m_bitmapMask
);
827 wxBitmapRefData::~wxBitmapRefData()
834 // ----------------------------------------------------------------------------
836 // ----------------------------------------------------------------------------
838 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
840 bool created
= false ;
841 int w
= icon
.GetWidth() ;
842 int h
= icon
.GetHeight() ;
845 #ifdef __WXOSX_CARBON__
846 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
848 IconFamilyHandle iconFamily
= NULL
;
849 Handle imagehandle
= NewHandle( 0 ) ;
850 Handle maskhandle
= NewHandle( 0 ) ;
854 IconSelectorValue selector
= 0 ;
859 dataType
= kThumbnail32BitData
;
860 maskType
= kThumbnail8BitMask
;
861 selector
= kSelectorAllAvailableData
;
865 dataType
= kHuge32BitData
;
866 maskType
= kHuge8BitMask
;
867 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
871 dataType
= kLarge32BitData
;
872 maskType
= kLarge8BitMask
;
873 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
877 dataType
= kSmall32BitData
;
878 maskType
= kSmall8BitMask
;
879 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
886 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
888 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
889 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
890 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
891 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
893 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
895 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
896 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
900 unsigned char *source
= (unsigned char *) *imagehandle
;
901 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
902 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
904 for ( int y
= 0 ; y
< h
; ++y
)
906 for ( int x
= 0 ; x
< w
; ++x
)
908 unsigned char a
= *sourcemask
++;
911 #if wxOSX_USE_PREMULTIPLIED_ALPHA
912 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
913 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
914 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
916 *destination
++ = *source
++ ;
917 *destination
++ = *source
++ ;
918 *destination
++ = *source
++ ;
924 DisposeHandle( imagehandle
) ;
925 DisposeHandle( maskhandle
) ;
929 DisposeHandle( (Handle
) iconFamily
) ;
935 dc
.SelectObject( *this ) ;
936 dc
.DrawIcon( icon
, 0 , 0 ) ;
937 dc
.SelectObject( wxNullBitmap
) ;
943 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
945 wxBitmapRefData
* bitmapRefData
;
947 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
949 if (bitmapRefData
->IsOk())
953 int linesize
= the_width
/ 8;
957 unsigned char* linestart
= (unsigned char*) bits
;
958 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
960 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
962 unsigned char* destination
= destptr
;
963 int index
, bit
, mask
;
965 for ( int x
= 0 ; x
< the_width
; ++x
)
971 if ( linestart
[index
] & mask
)
973 *destination
++ = 0xFF ;
980 *destination
++ = 0xFF ;
981 *destination
++ = 0xFF ;
982 *destination
++ = 0xFF ;
983 *destination
++ = 0xFF ;
992 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
994 } /* bitmapRefData->IsOk() */
997 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
999 (void) Create(data
, type
, width
, height
, depth
);
1002 wxBitmap::wxBitmap(int width
, int height
, const wxDC
& dc
)
1004 (void)Create(width
, height
, dc
);
1007 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1009 LoadFile(filename
, type
);
1012 wxBitmap::wxBitmap(CGImageRef image
, double scale
)
1014 (void) Create(image
,scale
);
1017 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
1019 return new wxBitmapRefData
;
1022 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1024 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
1027 void * wxBitmap::GetRawAccess() const
1029 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1031 return M_BITMAPDATA
->GetRawAccess() ;
1034 void * wxBitmap::BeginRawAccess()
1036 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1038 return M_BITMAPDATA
->BeginRawAccess() ;
1041 void wxBitmap::EndRawAccess()
1043 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
1045 M_BITMAPDATA
->EndRawAccess() ;
1048 CGImageRef
wxBitmap::CreateCGImage() const
1050 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1052 return M_BITMAPDATA
->CreateCGImage() ;
1055 #ifndef __WXOSX_IPHONE__
1056 IconRef
wxBitmap::GetIconRef() const
1058 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1060 return M_BITMAPDATA
->GetIconRef() ;
1063 IconRef
wxBitmap::CreateIconRef() const
1065 IconRef icon
= GetIconRef();
1066 verify_noerr( AcquireIconRef(icon
) );
1073 wxBitmap::wxBitmap(WX_NSImage image
)
1075 (void)Create(image
);
1078 bool wxBitmap::Create(WX_NSImage image
)
1080 return Create(wxOSXCreateBitmapContextFromNSImage(image
));
1083 wxBitmap::wxBitmap(CGContextRef bitmapcontext
)
1085 (void)Create(bitmapcontext
);
1088 bool wxBitmap::Create(CGContextRef bitmapcontext
)
1092 m_refData
= new wxBitmapRefData( bitmapcontext
);
1094 return M_BITMAPDATA
->IsOk() ;
1097 WX_NSImage
wxBitmap::GetNSImage() const
1099 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1100 return wxOSXGetNSImageFromCGImage( cgimage
, GetScaleFactor() );
1105 #if wxOSX_USE_IPHONE
1107 WX_UIImage
wxBitmap::GetUIImage() const
1109 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1110 return wxOSXGetUIImageFromCGImage( cgimage
);
1114 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1116 wxCHECK_MSG( IsOk() &&
1117 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1118 (rect
.x
+rect
.width
<= GetWidth()) &&
1119 (rect
.y
+rect
.height
<= GetHeight()),
1120 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1123 double scale
= GetScaleFactor();
1124 ret
.CreateScaled( rect
.width
, rect
.height
, GetDepth(), scale
);
1125 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1127 int destwidth
= rect
.width
*scale
;
1128 int destheight
= rect
.height
*scale
;
1131 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1132 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1133 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1135 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1137 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1138 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1139 unsigned char *source
= sourcedata
+ int(rect
.x
* scale
* 4 + rect
.y
*scale
* sourcelinesize
) ;
1140 unsigned char *dest
= destdata
;
1142 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1144 memcpy( dest
, source
, destlinesize
) ;
1147 ret
.EndRawAccess() ;
1151 if ( M_BITMAPDATA
->m_bitmapMask
)
1153 wxMemoryBuffer maskbuf
;
1154 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1155 size_t maskbufsize
= rowBytes
* destheight
;
1157 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1158 int destlinesize
= rowBytes
;
1160 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1161 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1162 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1164 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1166 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1167 unsigned char *dest
= destdata
;
1169 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1171 memcpy( dest
, source
, destlinesize
) ;
1174 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1176 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1178 else if ( HasAlpha() )
1184 bool wxBitmap::Create(int w
, int h
, int d
)
1189 d
= wxDisplayDepth() ;
1191 m_refData
= new wxBitmapRefData( w
, h
, d
);
1193 return M_BITMAPDATA
->IsOk() ;
1196 bool wxBitmap::Create(int w
, int h
, const wxDC
& dc
)
1198 double factor
= dc
.GetContentScaleFactor();
1199 return CreateScaled(w
,h
,wxBITMAP_SCREEN_DEPTH
, factor
);
1202 bool wxBitmap::CreateScaled(int w
, int h
, int d
, double logicalScale
)
1207 d
= wxDisplayDepth() ;
1209 m_refData
= new wxBitmapRefData( w
, h
, d
, logicalScale
);
1211 return M_BITMAPDATA
->IsOk() ;
1214 bool wxBitmap::Create(CGImageRef image
, double scale
)
1218 m_refData
= new wxBitmapRefData( image
, scale
);
1220 return M_BITMAPDATA
->IsOk() ;
1223 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1227 wxBitmapHandler
*handler
= FindHandler(type
);
1231 m_refData
= new wxBitmapRefData
;
1233 return handler
->LoadFile(this, filename
, type
, -1, -1);
1238 wxImage
loadimage(filename
, type
);
1239 if (loadimage
.IsOk())
1248 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1253 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1257 m_refData
= new wxBitmapRefData
;
1259 wxBitmapHandler
*handler
= FindHandler(type
);
1261 if ( handler
== NULL
)
1263 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1268 return handler
->Create(this, data
, type
, width
, height
, depth
);
1273 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1275 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1277 // width and height of the device-dependent bitmap
1278 int width
= image
.GetWidth();
1279 int height
= image
.GetHeight();
1281 wxBitmapRefData
* bitmapRefData
;
1283 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1285 if ( bitmapRefData
->IsOk())
1289 bool hasAlpha
= false ;
1291 if ( image
.HasMask() )
1293 // takes precedence, don't mix with alpha info
1297 hasAlpha
= image
.HasAlpha() ;
1303 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1304 register unsigned char* data
= image
.GetData();
1305 if ( destinationstart
!= NULL
&& data
!= NULL
)
1307 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1308 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1310 unsigned char * destination
= destinationstart
;
1311 for (int x
= 0; x
< width
; x
++)
1315 const unsigned char a
= *alpha
++;
1316 *destination
++ = a
;
1318 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1319 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1320 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1321 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1323 *destination
++ = *data
++ ;
1324 *destination
++ = *data
++ ;
1325 *destination
++ = *data
++ ;
1330 *destination
++ = 0xFF ;
1331 *destination
++ = *data
++ ;
1332 *destination
++ = *data
++ ;
1333 *destination
++ = *data
++ ;
1340 if ( image
.HasMask() )
1341 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1342 } /* bitmapRefData->IsOk() */
1345 wxImage
wxBitmap::ConvertToImage() const
1349 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1351 // create an wxImage object
1352 int width
= GetWidth();
1353 int height
= GetHeight();
1354 image
.Create( width
, height
);
1356 unsigned char *data
= image
.GetData();
1357 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1359 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1361 bool hasAlpha
= false ;
1362 bool hasMask
= false ;
1363 int maskBytesPerRow
= 0 ;
1364 unsigned char *alpha
= NULL
;
1365 unsigned char *mask
= NULL
;
1373 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1374 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1380 alpha
= image
.GetAlpha() ;
1385 // The following masking algorithm is the same as well in msw/gtk:
1386 // the colour used as transparent one in wxImage and the one it is
1387 // replaced with when it actually occurs in the bitmap
1388 static const int MASK_RED
= 1;
1389 static const int MASK_GREEN
= 2;
1390 static const int MASK_BLUE
= 3;
1391 static const int MASK_BLUE_REPLACEMENT
= 2;
1393 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1395 unsigned char * maskp
= mask
;
1396 unsigned char * source
= sourcestart
;
1397 unsigned char a
, r
, g
, b
;
1400 for (int xx
= 0; xx
< width
; xx
++)
1402 color
= *((long*) source
) ;
1403 #ifdef WORDS_BIGENDIAN
1404 a
= ((color
&0xFF000000) >> 24) ;
1405 r
= ((color
&0x00FF0000) >> 16) ;
1406 g
= ((color
&0x0000FF00) >> 8) ;
1407 b
= (color
&0x000000FF);
1409 b
= ((color
&0xFF000000) >> 24) ;
1410 g
= ((color
&0x00FF0000) >> 16) ;
1411 r
= ((color
&0x0000FF00) >> 8) ;
1412 a
= (color
&0x000000FF);
1416 if ( *maskp
++ == 0xFF )
1422 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1423 b
= MASK_BLUE_REPLACEMENT
;
1425 else if ( hasAlpha
)
1428 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1429 // this must be non-premultiplied data
1430 if ( a
!= 0xFF && a
!= 0 )
1440 data
[index
+ 1] = g
;
1441 data
[index
+ 2] = b
;
1449 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1454 #endif //wxUSE_IMAGE
1456 bool wxBitmap::SaveFile( const wxString
& filename
,
1457 wxBitmapType type
, const wxPalette
*palette
) const
1459 bool success
= false;
1460 wxBitmapHandler
*handler
= FindHandler(type
);
1464 success
= handler
->SaveFile(this, filename
, type
, palette
);
1469 wxImage image
= ConvertToImage();
1470 success
= image
.SaveFile(filename
, type
);
1472 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1479 int wxBitmap::GetHeight() const
1481 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1483 return M_BITMAPDATA
->GetHeight();
1486 int wxBitmap::GetWidth() const
1488 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1490 return M_BITMAPDATA
->GetWidth() ;
1493 double wxBitmap::GetScaleFactor() const
1495 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1497 return M_BITMAPDATA
->GetScaleFactor() ;
1500 int wxBitmap::GetDepth() const
1502 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1504 return M_BITMAPDATA
->GetDepth();
1507 wxMask
*wxBitmap::GetMask() const
1509 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1511 return M_BITMAPDATA
->m_bitmapMask
;
1514 bool wxBitmap::HasAlpha() const
1516 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1518 return M_BITMAPDATA
->HasAlpha() ;
1521 void wxBitmap::SetWidth(int w
)
1524 M_BITMAPDATA
->SetWidth(w
);
1527 void wxBitmap::SetHeight(int h
)
1530 M_BITMAPDATA
->SetHeight(h
);
1533 void wxBitmap::SetDepth(int d
)
1536 M_BITMAPDATA
->SetDepth(d
);
1539 void wxBitmap::SetOk(bool isOk
)
1542 M_BITMAPDATA
->SetOk(isOk
);
1546 wxPalette
*wxBitmap::GetPalette() const
1548 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1550 return &M_BITMAPDATA
->m_bitmapPalette
;
1553 void wxBitmap::SetPalette(const wxPalette
& palette
)
1556 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1558 #endif // wxUSE_PALETTE
1560 void wxBitmap::SetMask(wxMask
*mask
)
1563 // Remove existing mask if there is one.
1564 delete M_BITMAPDATA
->m_bitmapMask
;
1566 M_BITMAPDATA
->m_bitmapMask
= mask
;
1569 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1573 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1576 // ----------------------------------------------------------------------------
1578 // ----------------------------------------------------------------------------
1585 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1589 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1590 m_width
= tocopy
.m_width
;
1591 m_height
= tocopy
.m_height
;
1593 size_t size
= m_bytesPerRow
* m_height
;
1594 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1595 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1596 memcpy( dest
, source
, size
);
1597 m_memBuf
.UngetWriteBuf( size
) ;
1601 // Construct a mask from a bitmap and a colour indicating
1602 // the transparent area
1603 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1606 Create( bitmap
, colour
);
1609 // Construct a mask from a mono bitmap (copies the bitmap).
1610 wxMask::wxMask( const wxBitmap
& bitmap
)
1616 // Construct a mask from a mono bitmap (copies the bitmap).
1618 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1621 Create( data
, width
, height
, bytesPerRow
);
1628 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1629 m_maskBitmap
= NULL
;
1635 m_width
= m_height
= m_bytesPerRow
= 0 ;
1636 m_maskBitmap
= NULL
;
1639 void *wxMask::GetRawAccess() const
1641 return m_memBuf
.GetData() ;
1644 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1645 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1647 void wxMask::RealizeNative()
1651 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1652 m_maskBitmap
= NULL
;
1655 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1656 // from MouseTracking sample :
1657 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1658 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1660 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1661 kCGImageAlphaNone
);
1662 CGColorSpaceRelease( colorspace
);
1663 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1666 // Create a mask from a mono bitmap (copies the bitmap).
1668 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1673 m_bytesPerRow
= bytesPerRow
;
1675 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1682 // Create a mask from a mono bitmap (copies the bitmap).
1683 bool wxMask::Create(const wxBitmap
& bitmap
)
1685 m_width
= bitmap
.GetWidth() ;
1686 m_height
= bitmap
.GetHeight() ;
1687 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1689 size_t size
= m_bytesPerRow
* m_height
;
1690 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1691 wxASSERT( destdatabase
!= NULL
) ;
1695 memset( destdatabase
, 0 , size
) ;
1696 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1698 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1700 unsigned char *destdata
= destdatabase
;
1701 unsigned char r
, g
, b
;
1703 for ( int x
= 0 ; x
< m_width
; ++x
)
1710 if ( ( r
+ g
+ b
) > 0x10 )
1711 *destdata
++ = 0xFF ;
1713 *destdata
++ = 0x00 ;
1718 m_memBuf
.UngetWriteBuf( size
) ;
1724 // Create a mask from a bitmap and a colour indicating
1725 // the transparent area
1726 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1728 m_width
= bitmap
.GetWidth() ;
1729 m_height
= bitmap
.GetHeight() ;
1730 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1732 size_t size
= m_bytesPerRow
* m_height
;
1733 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1734 wxASSERT( destdatabase
!= NULL
) ;
1735 if ( destdatabase
!= NULL
)
1737 memset( destdatabase
, 0 , size
) ;
1738 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1739 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1741 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1743 unsigned char *srcdata
= srcdatabase
;
1744 unsigned char *destdata
= destdatabase
;
1745 unsigned char r
, g
, b
;
1747 for ( int x
= 0 ; x
< m_width
; ++x
)
1754 if ( colour
== wxColour( r
, g
, b
) )
1755 *destdata
++ = 0xFF ;
1757 *destdata
++ = 0x00 ;
1761 m_memBuf
.UngetWriteBuf( size
) ;
1767 wxBitmap
wxMask::GetBitmap() const
1769 wxBitmap
bitmap(m_width
, m_height
, 1);
1770 unsigned char* dst
= static_cast<unsigned char*>(bitmap
.BeginRawAccess());
1771 const int dst_stride
= bitmap
.GetBitmapData()->GetBytesPerRow();
1772 const unsigned char* src
= static_cast<unsigned char*>(GetRawAccess());
1773 for (int j
= 0; j
< m_height
; j
++, src
+= m_bytesPerRow
, dst
+= dst_stride
)
1775 unsigned char* d
= dst
;
1776 for (int i
= 0; i
< m_width
; i
++)
1778 const unsigned char byte
= src
[i
];
1785 bitmap
.EndRawAccess();
1789 WXHBITMAP
wxMask::GetHBITMAP() const
1791 return m_maskBitmap
;
1794 // ----------------------------------------------------------------------------
1795 // Standard Handlers
1796 // ----------------------------------------------------------------------------
1798 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1800 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1803 inline wxBundleResourceHandler()
1807 virtual bool LoadFile(wxBitmap
*bitmap
,
1808 const wxString
& name
,
1814 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1816 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1818 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1821 inline wxPNGResourceHandler()
1823 SetName(wxT("PNG resource"));
1824 SetExtension("PNG");
1825 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1829 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1831 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1833 DECLARE_DYNAMIC_CLASS(wxJPEGResourceHandler
)
1836 inline wxJPEGResourceHandler()
1838 SetName(wxT("JPEG resource"));
1839 SetExtension("JPEG");
1840 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1844 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1846 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1847 const wxString
& name
,
1848 wxBitmapType
WXUNUSED(type
),
1849 int WXUNUSED(desiredWidth
),
1850 int WXUNUSED(desiredHeight
))
1852 wxString ext
= GetExtension().Lower();
1853 wxCFStringRef
resname(name
);
1854 wxCFStringRef
resname2x(name
+"@2x");
1855 wxCFStringRef
restype(ext
);
1858 wxCFRef
<CFURLRef
> imageURL
;
1860 if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 )
1862 imageURL
.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname2x
, restype
, NULL
));
1866 if ( imageURL
.get() == NULL
)
1868 imageURL
.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1872 if ( imageURL
.get() != NULL
)
1874 // Create the data provider object
1875 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1876 CGImageRef image
= NULL
;
1878 if ( ext
== "jpeg" )
1879 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1880 kCGRenderingIntentDefault
);
1881 else if ( ext
== "png" )
1882 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1883 kCGRenderingIntentDefault
);
1884 if ( image
!= NULL
)
1886 bitmap
->Create(image
,scale
);
1887 CGImageRelease(image
);
1895 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1897 wxCFRef
<CGDataProviderRef
>
1898 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1900 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1901 kCGRenderingIntentDefault
));
1903 return wxBitmap(image
);
1906 void wxBitmap::InitStandardHandlers()
1908 #if wxOSX_USE_COCOA_OR_CARBON
1909 AddHandler( new wxICONResourceHandler
) ;
1911 AddHandler( new wxPNGResourceHandler
);
1912 AddHandler( new wxJPEGResourceHandler
);
1915 // ----------------------------------------------------------------------------
1916 // raw bitmap access support
1917 // ----------------------------------------------------------------------------
1919 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1922 // no bitmap, no data (raw or otherwise)
1925 data
.m_width
= GetWidth() ;
1926 data
.m_height
= GetHeight() ;
1927 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1929 return BeginRawAccess() ;
1932 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1937 void wxBitmap::UseAlpha()
1939 // remember that we are using alpha channel:
1940 // we'll need to create a proper mask in UngetRawData()
1941 M_BITMAPDATA
->UseAlpha( true );