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(CGImageRef image
);
63 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
65 virtual ~wxBitmapRefData();
67 virtual bool IsOk() const { return m_ok
; }
70 void SetOk( bool isOk
) { m_ok
= isOk
; }
72 void SetWidth( int width
) { m_width
= width
; }
73 void SetHeight( int height
) { m_height
= height
; }
74 void SetDepth( int depth
) { m_depth
= depth
; }
76 int GetWidth() const { return m_width
; }
77 int GetHeight() const { return m_height
; }
78 int GetDepth() const { return m_depth
; }
79 double GetScaleFactor() const { return m_scaleFactor
; }
80 void *GetRawAccess() const;
81 void *BeginRawAccess();
84 bool HasAlpha() const { return m_hasAlpha
; }
85 void UseAlpha( bool useAlpha
);
89 wxPalette m_bitmapPalette
;
90 #endif // wxUSE_PALETTE
92 wxMask
* m_bitmapMask
; // Optional mask
93 CGImageRef
CreateCGImage() const;
95 // returns true if the bitmap has a size that
96 // can be natively transferred into a true icon
97 // if no is returned GetIconRef will still produce
98 // an icon but it will be generated via a PICT and
99 // rescaled to 16 x 16
100 bool HasNativeSize();
102 #ifndef __WXOSX_IPHONE__
103 // caller should increase ref count if needed longer
104 // than the bitmap exists
105 IconRef
GetIconRef();
108 CGContextRef
GetBitmapContext() const;
110 int GetBytesPerRow() const { return m_bytesPerRow
; }
112 bool Create(int width
, int height
, int depth
);
113 bool Create(int width
, int height
, int depth
, double logicalScale
);
114 bool Create( CGImageRef image
);
122 wxMemoryBuffer m_memBuf
;
123 int m_rawAccessCount
;
125 mutable CGImageRef m_cgImageRef
;
127 #ifndef __WXOSX_IPHONE__
131 CGContextRef m_hBitmap
;
132 double m_scaleFactor
;
136 #define wxOSX_USE_PREMULTIPLIED_ALPHA 1
137 static const int kBestByteAlignement
= 16;
138 static const int kMaskBytesPerPixel
= 1;
140 static int GetBestBytesPerRow( int rawBytes
)
142 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
145 #if wxUSE_GUI && !defined(__WXOSX_IPHONE__)
147 // this is used for more controls than just the wxBitmap button, also for notebooks etc
149 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
151 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
154 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
158 if ( forceType
== 0 )
160 forceType
= kControlContentCGImageRef
;
163 if ( forceType
== kControlContentIconRef
)
166 wxBitmapRefData
* bmp
= bmap
;
168 if ( !bmap
->HasNativeSize() )
170 // as PICT conversion will only result in a 16x16 icon, let's attempt
171 // a few scales for better results
173 int w
= bitmap
.GetWidth() ;
174 int h
= bitmap
.GetHeight() ;
175 int sz
= wxMax( w
, h
) ;
176 if ( sz
== 24 || sz
== 64 )
178 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
179 bmp
= scaleBmp
.GetBitmapData() ;
183 info
->contentType
= kControlContentIconRef
;
184 info
->u
.iconRef
= bmp
->GetIconRef() ;
185 AcquireIconRef( info
->u
.iconRef
) ;
187 else if ( forceType
== kControlContentCGImageRef
)
189 info
->contentType
= kControlContentCGImageRef
;
190 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
195 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
197 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
200 return (CGImageRef
) bmap
->CreateCGImage();
203 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
205 if ( info
->contentType
== kControlContentIconRef
)
207 ReleaseIconRef( info
->u
.iconRef
) ;
209 else if ( info
->contentType
== kControlNoContent
)
211 // there's no bitmap at all, fall through silently
213 else if ( info
->contentType
== kControlContentPictHandle
)
215 // owned by the bitmap, no release here
217 else if ( info
->contentType
== kControlContentCGImageRef
)
219 CGImageRelease( info
->u
.imageRef
) ;
223 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
227 #endif //wxUSE_BMPBUTTON
229 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
231 void wxBitmapRefData::Init()
238 m_bitmapMask
= NULL
;
239 m_cgImageRef
= NULL
;
241 #ifndef __WXOSX_IPHONE__
246 m_rawAccessCount
= 0 ;
251 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
) : wxGDIRefData()
254 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
, tocopy
.m_scaleFactor
);
256 if (tocopy
.m_bitmapMask
)
257 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
258 else if (tocopy
.m_hasAlpha
)
261 unsigned char* dest
= (unsigned char*)GetRawAccess();
262 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
263 size_t numbytes
= m_bytesPerRow
* m_height
;
264 memcpy( dest
, source
, numbytes
);
267 wxBitmapRefData::wxBitmapRefData()
272 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
275 Create( w
, h
, d
) ;
278 wxBitmapRefData::wxBitmapRefData(int w
, int h
, int d
, double logicalscale
)
281 Create( w
, h
, d
, logicalscale
) ;
284 wxBitmapRefData::wxBitmapRefData(CGImageRef image
)
289 // code from Technical Q&A QA1509
291 bool wxBitmapRefData::Create(CGImageRef image
)
295 m_width
= CGImageGetWidth(image
);
296 m_height
= CGImageGetHeight(image
);
300 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
301 size_t size
= m_bytesPerRow
* m_height
;
302 void* data
= m_memBuf
.GetWriteBuf( size
) ;
305 memset( data
, 0 , size
) ;
306 m_memBuf
.UngetWriteBuf( size
) ;
307 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
308 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
310 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
315 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
317 CGRect rect
= CGRectMake(0,0,m_width
,m_height
);
318 CGContextDrawImage(m_hBitmap
, rect
, image
);
320 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
321 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
322 CGContextScaleCTM( m_hBitmap
, 1, -1 );
325 m_ok
= ( m_hBitmap
!= NULL
) ;
331 bool wxBitmapRefData::Create( int w
, int h
, int d
)
333 m_width
= wxMax(1, w
);
334 m_height
= wxMax(1, h
);
338 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
339 size_t size
= m_bytesPerRow
* m_height
;
340 void* data
= m_memBuf
.GetWriteBuf( size
) ;
343 memset( data
, 0 , size
) ;
344 m_memBuf
.UngetWriteBuf( size
) ;
346 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
347 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
348 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
349 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
351 m_ok
= ( m_hBitmap
!= NULL
) ;
356 bool wxBitmapRefData::Create( int w
, int h
, int d
, double logicalScale
)
358 m_scaleFactor
= logicalScale
;
359 return Create(w
*logicalScale
,h
*logicalScale
,d
);
362 void wxBitmapRefData::UseAlpha( bool use
)
364 if ( m_hasAlpha
== use
)
369 CGContextRelease( m_hBitmap
);
370 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
371 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
372 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
373 CGContextScaleCTM( m_hBitmap
, 1*m_scaleFactor
, -1*m_scaleFactor
);
376 void *wxBitmapRefData::GetRawAccess() const
378 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
379 return m_memBuf
.GetData() ;
382 void *wxBitmapRefData::BeginRawAccess()
384 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
385 wxASSERT( m_rawAccessCount
== 0 ) ;
386 #ifndef __WXOSX_IPHONE__
387 wxASSERT_MSG( m_iconRef
== NULL
,
388 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
392 // we must destroy an existing cached image, as
393 // the bitmap data may change now
396 CGImageRelease( m_cgImageRef
) ;
397 m_cgImageRef
= NULL
;
400 return m_memBuf
.GetData() ;
403 void wxBitmapRefData::EndRawAccess()
405 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
406 wxASSERT( m_rawAccessCount
== 1 ) ;
411 bool wxBitmapRefData::HasNativeSize()
414 int h
= GetHeight() ;
415 int sz
= wxMax( w
, h
) ;
417 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
420 #ifndef __WXOSX_IPHONE__
421 IconRef
wxBitmapRefData::GetIconRef()
423 if ( m_iconRef
== NULL
)
425 // Create Icon Family Handle
427 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
430 int h
= GetHeight() ;
431 int sz
= wxMax( w
, h
) ;
433 OSType dataType
= 0 ;
434 OSType maskType
= 0 ;
436 // since we don't have PICT conversion available, use
437 // the next larger standard icon size
451 else if ( sz
<= 1024)
456 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
458 dataType
= kIconServices1024PixelDataARGB
;
462 dataType
= kIconServices512PixelDataARGB
;
466 dataType
= kIconServices256PixelDataARGB
;
470 dataType
= kIconServices128PixelDataARGB
;
474 dataType
= kIconServices48PixelDataARGB
;
478 dataType
= kIconServices32PixelDataARGB
;
482 dataType
= kIconServices16PixelDataARGB
;
493 size_t datasize
= sz
* sz
* 4 ;
494 Handle data
= NewHandle( datasize
) ;
496 unsigned char* ptr
= (unsigned char*) *data
;
497 memset( ptr
, 0, datasize
);
498 bool hasAlpha
= HasAlpha() ;
499 wxMask
*mask
= m_bitmapMask
;
500 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
501 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
503 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
505 unsigned char * source
= sourcePtr
;
506 unsigned char * masksource
= masksourcePtr
;
507 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
508 unsigned char a
, r
, g
, b
;
510 for ( int x
= 0 ; x
< w
; ++x
)
519 a
= 0xFF - *masksource
++ ;
521 else if ( !hasAlpha
)
525 #if wxOSX_USE_PREMULTIPLIED_ALPHA
526 // this must be non-premultiplied data
527 if ( a
!= 0xFF && a
!= 0 )
544 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
547 wxFAIL_MSG("Error when adding bitmap");
550 DisposeHandle( data
);
554 // setup the header properly
557 Handle maskdata
= NULL
;
558 unsigned char * maskptr
= NULL
;
559 unsigned char * ptr
= NULL
;
560 size_t datasize
, masksize
;
562 datasize
= sz
* sz
* 4 ;
563 data
= NewHandle( datasize
) ;
565 ptr
= (unsigned char*) *data
;
566 memset( ptr
, 0, datasize
) ;
569 maskdata
= NewHandle( masksize
) ;
571 maskptr
= (unsigned char*) *maskdata
;
572 memset( maskptr
, 0 , masksize
) ;
574 bool hasAlpha
= HasAlpha() ;
575 wxMask
*mask
= m_bitmapMask
;
576 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
577 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
579 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
581 unsigned char * source
= sourcePtr
;
582 unsigned char * masksource
= masksourcePtr
;
583 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
584 unsigned char * maskdest
= maskptr
+ y
* sz
;
585 unsigned char a
, r
, g
, b
;
587 for ( int x
= 0 ; x
< w
; ++x
)
600 *maskdest
++ = 0xFF - *masksource
++ ;
608 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
609 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
611 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
612 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
615 HUnlock( maskdata
) ;
616 DisposeHandle( data
) ;
617 DisposeHandle( maskdata
) ;
620 // transform into IconRef
622 // cleaner version existing from 10.3 upwards
623 HLock((Handle
) iconFamily
);
624 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
625 HUnlock((Handle
) iconFamily
);
626 DisposeHandle( (Handle
) iconFamily
) ;
628 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
636 CGImageRef
wxBitmapRefData::CreateCGImage() const
639 wxASSERT( m_rawAccessCount
>= 0 ) ;
641 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
643 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
646 // in order for this code to work properly, wxMask would have to invert black and white
647 // in the native bitmap
650 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
651 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
652 image
= CGImageCreateWithMask( tempImage
, tempMask
);
653 CGImageRelease(tempMask
);
654 CGImageRelease(tempImage
);
658 image
= CGBitmapContextCreateImage( m_hBitmap
);
662 size_t imageSize
= m_height
* m_bytesPerRow
;
663 void * dataBuffer
= m_memBuf
.GetData() ;
666 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
667 wxMemoryBuffer membuf
;
671 alphaInfo
= kCGImageAlphaFirst
;
672 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
673 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
674 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
675 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
676 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
678 unsigned char *sourcemask
= sourcemaskstart
;
679 unsigned char *destalpha
= destalphastart
;
680 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
682 *destalpha
= 0xFF - *sourcemask
;
685 membuf
.UngetWriteBuf( imageSize
);
691 #if wxOSX_USE_PREMULTIPLIED_ALPHA
692 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
694 alphaInfo
= kCGImageAlphaFirst
;
701 CGDataProviderRef dataProvider
= NULL
;
704 // TODO CHECK ALIGNMENT
705 wxMemoryBuffer maskBuf
;
706 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
707 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
708 // copy one color component
710 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
712 unsigned char *bufDataIter
= bufData
+3;
713 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
715 maskBufData
[i
] = *bufDataIter
;
718 maskBuf
.UngetWriteBuf( m_width
* m_height
);
721 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
723 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
727 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
728 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
731 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
732 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
734 CGDataProviderRelease( dataProvider
);
739 image
= m_cgImageRef
;
740 CGImageRetain( image
) ;
743 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
745 // we keep it for later use
746 m_cgImageRef
= image
;
747 CGImageRetain( image
) ;
753 CGContextRef
wxBitmapRefData::GetBitmapContext() const
758 void wxBitmapRefData::Free()
760 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
764 CGImageRelease( m_cgImageRef
) ;
765 m_cgImageRef
= NULL
;
767 #ifndef __WXOSX_IPHONE__
770 ReleaseIconRef( m_iconRef
) ;
776 CGContextRelease(m_hBitmap
);
780 wxDELETE(m_bitmapMask
);
783 wxBitmapRefData::~wxBitmapRefData()
790 // ----------------------------------------------------------------------------
792 // ----------------------------------------------------------------------------
794 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
796 bool created
= false ;
797 int w
= icon
.GetWidth() ;
798 int h
= icon
.GetHeight() ;
801 #ifdef __WXOSX_CARBON__
802 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
804 IconFamilyHandle iconFamily
= NULL
;
805 Handle imagehandle
= NewHandle( 0 ) ;
806 Handle maskhandle
= NewHandle( 0 ) ;
810 IconSelectorValue selector
= 0 ;
815 dataType
= kThumbnail32BitData
;
816 maskType
= kThumbnail8BitMask
;
817 selector
= kSelectorAllAvailableData
;
821 dataType
= kHuge32BitData
;
822 maskType
= kHuge8BitMask
;
823 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
827 dataType
= kLarge32BitData
;
828 maskType
= kLarge8BitMask
;
829 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
833 dataType
= kSmall32BitData
;
834 maskType
= kSmall8BitMask
;
835 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
842 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
844 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
845 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
846 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
847 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
849 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
851 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
852 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
856 unsigned char *source
= (unsigned char *) *imagehandle
;
857 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
858 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
860 for ( int y
= 0 ; y
< h
; ++y
)
862 for ( int x
= 0 ; x
< w
; ++x
)
864 unsigned char a
= *sourcemask
++;
867 #if wxOSX_USE_PREMULTIPLIED_ALPHA
868 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
869 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
870 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
872 *destination
++ = *source
++ ;
873 *destination
++ = *source
++ ;
874 *destination
++ = *source
++ ;
880 DisposeHandle( imagehandle
) ;
881 DisposeHandle( maskhandle
) ;
885 DisposeHandle( (Handle
) iconFamily
) ;
891 dc
.SelectObject( *this ) ;
892 dc
.DrawIcon( icon
, 0 , 0 ) ;
893 dc
.SelectObject( wxNullBitmap
) ;
899 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
901 wxBitmapRefData
* bitmapRefData
;
903 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
905 if (bitmapRefData
->IsOk())
909 int linesize
= the_width
/ 8;
913 unsigned char* linestart
= (unsigned char*) bits
;
914 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
916 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
918 unsigned char* destination
= destptr
;
919 int index
, bit
, mask
;
921 for ( int x
= 0 ; x
< the_width
; ++x
)
927 if ( linestart
[index
] & mask
)
929 *destination
++ = 0xFF ;
936 *destination
++ = 0xFF ;
937 *destination
++ = 0xFF ;
938 *destination
++ = 0xFF ;
939 *destination
++ = 0xFF ;
948 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
950 } /* bitmapRefData->IsOk() */
953 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
955 (void) Create(data
, type
, width
, height
, depth
);
958 wxBitmap::wxBitmap(int width
, int height
, const wxDC
& dc
)
960 (void)Create(width
, height
, dc
);
963 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
965 LoadFile(filename
, type
);
968 wxBitmap::wxBitmap(CGImageRef image
)
970 (void) Create(image
);
973 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
975 return new wxBitmapRefData
;
978 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
980 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
983 void * wxBitmap::GetRawAccess() const
985 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
987 return M_BITMAPDATA
->GetRawAccess() ;
990 void * wxBitmap::BeginRawAccess()
992 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
994 return M_BITMAPDATA
->BeginRawAccess() ;
997 void wxBitmap::EndRawAccess()
999 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
1001 M_BITMAPDATA
->EndRawAccess() ;
1004 CGImageRef
wxBitmap::CreateCGImage() const
1006 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1008 return M_BITMAPDATA
->CreateCGImage() ;
1011 #ifndef __WXOSX_IPHONE__
1012 IconRef
wxBitmap::GetIconRef() const
1014 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1016 return M_BITMAPDATA
->GetIconRef() ;
1019 IconRef
wxBitmap::CreateIconRef() const
1021 IconRef icon
= GetIconRef();
1022 verify_noerr( AcquireIconRef(icon
) );
1029 wxBitmap::wxBitmap(WX_NSImage image
)
1031 (void)Create(image
);
1034 bool wxBitmap::Create(WX_NSImage image
)
1036 wxCFRef
<CGImageRef
> cgimage(wxOSXCreateCGImageFromNSImage(image
));
1037 return Create(cgimage
);
1040 WX_NSImage
wxBitmap::GetNSImage() const
1042 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1043 return wxOSXGetNSImageFromCGImage( cgimage
);
1048 #if wxOSX_USE_IPHONE
1050 WX_UIImage
wxBitmap::GetUIImage() const
1052 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1053 return wxOSXGetUIImageFromCGImage( cgimage
);
1057 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1059 wxCHECK_MSG( IsOk() &&
1060 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1061 (rect
.x
+rect
.width
<= GetWidth()) &&
1062 (rect
.y
+rect
.height
<= GetHeight()),
1063 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1066 double scale
= GetScaleFactor();
1067 ret
.CreateScaled( rect
.width
, rect
.height
, GetDepth(), scale
);
1068 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1070 int destwidth
= rect
.width
*scale
;
1071 int destheight
= rect
.height
*scale
;
1074 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1075 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1076 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1078 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1080 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1081 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1082 unsigned char *source
= sourcedata
+ int(rect
.x
* scale
* 4 + rect
.y
*scale
* sourcelinesize
) ;
1083 unsigned char *dest
= destdata
;
1085 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1087 memcpy( dest
, source
, destlinesize
) ;
1090 ret
.EndRawAccess() ;
1094 if ( M_BITMAPDATA
->m_bitmapMask
)
1096 wxMemoryBuffer maskbuf
;
1097 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1098 size_t maskbufsize
= rowBytes
* destheight
;
1100 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1101 int destlinesize
= rowBytes
;
1103 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1104 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1105 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1107 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1109 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1110 unsigned char *dest
= destdata
;
1112 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1114 memcpy( dest
, source
, destlinesize
) ;
1117 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1119 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1121 else if ( HasAlpha() )
1127 bool wxBitmap::Create(int w
, int h
, int d
)
1132 d
= wxDisplayDepth() ;
1134 m_refData
= new wxBitmapRefData( w
, h
, d
);
1136 return M_BITMAPDATA
->IsOk() ;
1139 bool wxBitmap::Create(int w
, int h
, const wxDC
& dc
)
1141 double factor
= dc
.GetContentScaleFactor();
1142 return CreateScaled(w
,h
,wxBITMAP_SCREEN_DEPTH
, factor
);
1145 bool wxBitmap::CreateScaled(int w
, int h
, int d
, double logicalScale
)
1150 d
= wxDisplayDepth() ;
1152 m_refData
= new wxBitmapRefData( w
, h
, d
, logicalScale
);
1154 return M_BITMAPDATA
->IsOk() ;
1157 bool wxBitmap::Create(CGImageRef image
)
1161 m_refData
= new wxBitmapRefData( image
);
1163 return M_BITMAPDATA
->IsOk() ;
1166 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1170 wxBitmapHandler
*handler
= FindHandler(type
);
1174 m_refData
= new wxBitmapRefData
;
1176 return handler
->LoadFile(this, filename
, type
, -1, -1);
1181 wxImage
loadimage(filename
, type
);
1182 if (loadimage
.IsOk())
1191 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1196 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1200 m_refData
= new wxBitmapRefData
;
1202 wxBitmapHandler
*handler
= FindHandler(type
);
1204 if ( handler
== NULL
)
1206 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1211 return handler
->Create(this, data
, type
, width
, height
, depth
);
1216 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1218 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1220 // width and height of the device-dependent bitmap
1221 int width
= image
.GetWidth();
1222 int height
= image
.GetHeight();
1224 wxBitmapRefData
* bitmapRefData
;
1226 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1228 if ( bitmapRefData
->IsOk())
1232 bool hasAlpha
= false ;
1234 if ( image
.HasMask() )
1236 // takes precedence, don't mix with alpha info
1240 hasAlpha
= image
.HasAlpha() ;
1246 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1247 register unsigned char* data
= image
.GetData();
1248 if ( destinationstart
!= NULL
&& data
!= NULL
)
1250 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1251 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1253 unsigned char * destination
= destinationstart
;
1254 for (int x
= 0; x
< width
; x
++)
1258 const unsigned char a
= *alpha
++;
1259 *destination
++ = a
;
1261 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1262 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1263 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1264 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1266 *destination
++ = *data
++ ;
1267 *destination
++ = *data
++ ;
1268 *destination
++ = *data
++ ;
1273 *destination
++ = 0xFF ;
1274 *destination
++ = *data
++ ;
1275 *destination
++ = *data
++ ;
1276 *destination
++ = *data
++ ;
1283 if ( image
.HasMask() )
1284 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1285 } /* bitmapRefData->IsOk() */
1288 wxImage
wxBitmap::ConvertToImage() const
1292 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1294 // create an wxImage object
1295 int width
= GetWidth();
1296 int height
= GetHeight();
1297 image
.Create( width
, height
);
1299 unsigned char *data
= image
.GetData();
1300 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1302 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1304 bool hasAlpha
= false ;
1305 bool hasMask
= false ;
1306 int maskBytesPerRow
= 0 ;
1307 unsigned char *alpha
= NULL
;
1308 unsigned char *mask
= NULL
;
1316 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1317 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1323 alpha
= image
.GetAlpha() ;
1328 // The following masking algorithm is the same as well in msw/gtk:
1329 // the colour used as transparent one in wxImage and the one it is
1330 // replaced with when it actually occurs in the bitmap
1331 static const int MASK_RED
= 1;
1332 static const int MASK_GREEN
= 2;
1333 static const int MASK_BLUE
= 3;
1334 static const int MASK_BLUE_REPLACEMENT
= 2;
1336 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1338 unsigned char * maskp
= mask
;
1339 unsigned char * source
= sourcestart
;
1340 unsigned char a
, r
, g
, b
;
1343 for (int xx
= 0; xx
< width
; xx
++)
1345 color
= *((long*) source
) ;
1346 #ifdef WORDS_BIGENDIAN
1347 a
= ((color
&0xFF000000) >> 24) ;
1348 r
= ((color
&0x00FF0000) >> 16) ;
1349 g
= ((color
&0x0000FF00) >> 8) ;
1350 b
= (color
&0x000000FF);
1352 b
= ((color
&0xFF000000) >> 24) ;
1353 g
= ((color
&0x00FF0000) >> 16) ;
1354 r
= ((color
&0x0000FF00) >> 8) ;
1355 a
= (color
&0x000000FF);
1359 if ( *maskp
++ == 0xFF )
1365 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1366 b
= MASK_BLUE_REPLACEMENT
;
1368 else if ( hasAlpha
)
1371 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1372 // this must be non-premultiplied data
1373 if ( a
!= 0xFF && a
!= 0 )
1383 data
[index
+ 1] = g
;
1384 data
[index
+ 2] = b
;
1392 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1397 #endif //wxUSE_IMAGE
1399 bool wxBitmap::SaveFile( const wxString
& filename
,
1400 wxBitmapType type
, const wxPalette
*palette
) const
1402 bool success
= false;
1403 wxBitmapHandler
*handler
= FindHandler(type
);
1407 success
= handler
->SaveFile(this, filename
, type
, palette
);
1412 wxImage image
= ConvertToImage();
1413 success
= image
.SaveFile(filename
, type
);
1415 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1422 int wxBitmap::GetHeight() const
1424 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1426 return M_BITMAPDATA
->GetHeight();
1429 int wxBitmap::GetWidth() const
1431 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1433 return M_BITMAPDATA
->GetWidth() ;
1436 double wxBitmap::GetScaleFactor() const
1438 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1440 return M_BITMAPDATA
->GetScaleFactor() ;
1443 int wxBitmap::GetDepth() const
1445 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1447 return M_BITMAPDATA
->GetDepth();
1450 wxMask
*wxBitmap::GetMask() const
1452 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1454 return M_BITMAPDATA
->m_bitmapMask
;
1457 bool wxBitmap::HasAlpha() const
1459 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1461 return M_BITMAPDATA
->HasAlpha() ;
1464 void wxBitmap::SetWidth(int w
)
1467 M_BITMAPDATA
->SetWidth(w
);
1470 void wxBitmap::SetHeight(int h
)
1473 M_BITMAPDATA
->SetHeight(h
);
1476 void wxBitmap::SetDepth(int d
)
1479 M_BITMAPDATA
->SetDepth(d
);
1482 void wxBitmap::SetOk(bool isOk
)
1485 M_BITMAPDATA
->SetOk(isOk
);
1489 wxPalette
*wxBitmap::GetPalette() const
1491 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1493 return &M_BITMAPDATA
->m_bitmapPalette
;
1496 void wxBitmap::SetPalette(const wxPalette
& palette
)
1499 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1501 #endif // wxUSE_PALETTE
1503 void wxBitmap::SetMask(wxMask
*mask
)
1506 // Remove existing mask if there is one.
1507 delete M_BITMAPDATA
->m_bitmapMask
;
1509 M_BITMAPDATA
->m_bitmapMask
= mask
;
1512 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1516 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1528 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1532 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1533 m_width
= tocopy
.m_width
;
1534 m_height
= tocopy
.m_height
;
1536 size_t size
= m_bytesPerRow
* m_height
;
1537 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1538 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1539 memcpy( dest
, source
, size
);
1540 m_memBuf
.UngetWriteBuf( size
) ;
1544 // Construct a mask from a bitmap and a colour indicating
1545 // the transparent area
1546 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1549 Create( bitmap
, colour
);
1552 // Construct a mask from a mono bitmap (copies the bitmap).
1553 wxMask::wxMask( const wxBitmap
& bitmap
)
1559 // Construct a mask from a mono bitmap (copies the bitmap).
1561 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1564 Create( data
, width
, height
, bytesPerRow
);
1571 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1572 m_maskBitmap
= NULL
;
1578 m_width
= m_height
= m_bytesPerRow
= 0 ;
1579 m_maskBitmap
= NULL
;
1582 void *wxMask::GetRawAccess() const
1584 return m_memBuf
.GetData() ;
1587 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1588 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1590 void wxMask::RealizeNative()
1594 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1595 m_maskBitmap
= NULL
;
1598 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1599 // from MouseTracking sample :
1600 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1601 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1603 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1604 kCGImageAlphaNone
);
1605 CGColorSpaceRelease( colorspace
);
1606 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1609 // Create a mask from a mono bitmap (copies the bitmap).
1611 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1616 m_bytesPerRow
= bytesPerRow
;
1618 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1625 // Create a mask from a mono bitmap (copies the bitmap).
1626 bool wxMask::Create(const wxBitmap
& bitmap
)
1628 m_width
= bitmap
.GetWidth() ;
1629 m_height
= bitmap
.GetHeight() ;
1630 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1632 size_t size
= m_bytesPerRow
* m_height
;
1633 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1634 wxASSERT( destdatabase
!= NULL
) ;
1638 memset( destdatabase
, 0 , size
) ;
1639 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1641 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1643 unsigned char *destdata
= destdatabase
;
1644 unsigned char r
, g
, b
;
1646 for ( int x
= 0 ; x
< m_width
; ++x
)
1653 if ( ( r
+ g
+ b
) > 0x10 )
1654 *destdata
++ = 0xFF ;
1656 *destdata
++ = 0x00 ;
1661 m_memBuf
.UngetWriteBuf( size
) ;
1667 // Create a mask from a bitmap and a colour indicating
1668 // the transparent area
1669 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1671 m_width
= bitmap
.GetWidth() ;
1672 m_height
= bitmap
.GetHeight() ;
1673 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1675 size_t size
= m_bytesPerRow
* m_height
;
1676 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1677 wxASSERT( destdatabase
!= NULL
) ;
1678 if ( destdatabase
!= NULL
)
1680 memset( destdatabase
, 0 , size
) ;
1681 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1682 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1684 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1686 unsigned char *srcdata
= srcdatabase
;
1687 unsigned char *destdata
= destdatabase
;
1688 unsigned char r
, g
, b
;
1690 for ( int x
= 0 ; x
< m_width
; ++x
)
1697 if ( colour
== wxColour( r
, g
, b
) )
1698 *destdata
++ = 0xFF ;
1700 *destdata
++ = 0x00 ;
1704 m_memBuf
.UngetWriteBuf( size
) ;
1710 wxBitmap
wxMask::GetBitmap() const
1712 wxBitmap
bitmap(m_width
, m_height
, 1);
1713 unsigned char* dst
= static_cast<unsigned char*>(bitmap
.BeginRawAccess());
1714 const int dst_stride
= bitmap
.GetBitmapData()->GetBytesPerRow();
1715 const unsigned char* src
= static_cast<unsigned char*>(GetRawAccess());
1716 for (int j
= 0; j
< m_height
; j
++, src
+= m_bytesPerRow
, dst
+= dst_stride
)
1718 unsigned char* d
= dst
;
1719 for (int i
= 0; i
< m_width
; i
++)
1721 const unsigned char byte
= src
[i
];
1728 bitmap
.EndRawAccess();
1732 WXHBITMAP
wxMask::GetHBITMAP() const
1734 return m_maskBitmap
;
1737 // ----------------------------------------------------------------------------
1738 // Standard Handlers
1739 // ----------------------------------------------------------------------------
1741 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1743 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1746 inline wxBundleResourceHandler()
1750 virtual bool LoadFile(wxBitmap
*bitmap
,
1751 const wxString
& name
,
1757 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1759 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1761 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1764 inline wxPNGResourceHandler()
1766 SetName(wxT("PNG resource"));
1767 SetExtension("PNG");
1768 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1772 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1774 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1776 DECLARE_DYNAMIC_CLASS(wxJPEGResourceHandler
)
1779 inline wxJPEGResourceHandler()
1781 SetName(wxT("JPEG resource"));
1782 SetExtension("JPEG");
1783 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1787 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1789 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1790 const wxString
& name
,
1791 wxBitmapType
WXUNUSED(type
),
1792 int WXUNUSED(desiredWidth
),
1793 int WXUNUSED(desiredHeight
))
1795 wxString ext
= GetExtension().Lower();
1796 wxCFStringRef
resname(name
);
1797 wxCFStringRef
restype(ext
);
1799 wxCFRef
<CFURLRef
> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1801 if ( imageURL
.get() != NULL
)
1803 // Create the data provider object
1804 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1805 CGImageRef image
= NULL
;
1807 if ( ext
== "jpeg" )
1808 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1809 kCGRenderingIntentDefault
);
1810 else if ( ext
== "png" )
1811 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1812 kCGRenderingIntentDefault
);
1813 if ( image
!= NULL
)
1815 bitmap
->Create(image
);
1816 CGImageRelease(image
);
1824 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1826 wxCFRef
<CGDataProviderRef
>
1827 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1829 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1830 kCGRenderingIntentDefault
));
1832 return wxBitmap(image
);
1835 void wxBitmap::InitStandardHandlers()
1837 #if wxOSX_USE_COCOA_OR_CARBON
1838 AddHandler( new wxICONResourceHandler
) ;
1840 AddHandler( new wxPNGResourceHandler
);
1841 AddHandler( new wxJPEGResourceHandler
);
1844 // ----------------------------------------------------------------------------
1845 // raw bitmap access support
1846 // ----------------------------------------------------------------------------
1848 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1851 // no bitmap, no data (raw or otherwise)
1854 data
.m_width
= GetWidth() ;
1855 data
.m_height
= GetHeight() ;
1856 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1858 return BeginRawAccess() ;
1861 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1866 void wxBitmap::UseAlpha()
1868 // remember that we are using alpha channel:
1869 // we'll need to create a proper mask in UngetRawData()
1870 M_BITMAPDATA
->UseAlpha( true );