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
);
60 wxBitmapRefData(CGImageRef image
);
62 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
64 virtual ~wxBitmapRefData();
66 virtual bool IsOk() const { return m_ok
; }
69 void SetOk( bool isOk
) { m_ok
= isOk
; }
71 void SetWidth( int width
) { m_width
= width
; }
72 void SetHeight( int height
) { m_height
= height
; }
73 void SetDepth( int depth
) { m_depth
= depth
; }
75 int GetWidth() const { return m_width
; }
76 int GetHeight() const { return m_height
; }
77 int GetDepth() const { return m_depth
; }
79 void *GetRawAccess() const;
80 void *BeginRawAccess();
83 bool HasAlpha() const { return m_hasAlpha
; }
84 void UseAlpha( bool useAlpha
);
88 wxPalette m_bitmapPalette
;
89 #endif // wxUSE_PALETTE
91 wxMask
* m_bitmapMask
; // Optional mask
92 CGImageRef
CreateCGImage() const;
94 // returns true if the bitmap has a size that
95 // can be natively transferred into a true icon
96 // if no is returned GetIconRef will still produce
97 // an icon but it will be generated via a PICT and
98 // rescaled to 16 x 16
101 // caller should increase ref count if needed longer
102 // than the bitmap exists
103 IconRef
GetIconRef();
105 #ifndef __WXOSX_IPHONE__
107 // returns a Pict from the bitmap content
108 PicHandle
GetPictHandle();
112 CGContextRef
GetBitmapContext() const;
114 int GetBytesPerRow() const { return m_bytesPerRow
; }
116 bool Create(int width
, int height
, int depth
);
117 bool Create( CGImageRef image
);
125 wxMemoryBuffer m_memBuf
;
126 int m_rawAccessCount
;
128 mutable CGImageRef m_cgImageRef
;
131 #ifndef __WXOSX_IPHONE__
133 PicHandle m_pictHandle
;
136 CGContextRef m_hBitmap
;
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 info
->contentType
= kControlContentPictHandle
;
200 info
->u
.picture
= bmap
->GetPictHandle() ;
206 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
208 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
211 return (CGImageRef
) bmap
->CreateCGImage();
214 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
216 if ( info
->contentType
== kControlContentIconRef
)
218 ReleaseIconRef( info
->u
.iconRef
) ;
220 else if ( info
->contentType
== kControlNoContent
)
222 // there's no bitmap at all, fall through silently
224 else if ( info
->contentType
== kControlContentPictHandle
)
226 // owned by the bitmap, no release here
228 else if ( info
->contentType
== kControlContentCGImageRef
)
230 CGImageRelease( info
->u
.imageRef
) ;
234 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
238 #endif //wxUSE_BMPBUTTON
240 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
242 void wxBitmapRefData::Init()
249 m_bitmapMask
= NULL
;
250 m_cgImageRef
= NULL
;
252 #ifndef __WXOSX_IPHONE__
255 m_pictHandle
= NULL
;
260 m_rawAccessCount
= 0 ;
264 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
) : wxGDIRefData()
267 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
269 if (tocopy
.m_bitmapMask
)
270 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
271 else if (tocopy
.m_hasAlpha
)
274 unsigned char* dest
= (unsigned char*)GetRawAccess();
275 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
276 size_t numbytes
= m_bytesPerRow
* m_height
;
277 memcpy( dest
, source
, numbytes
);
280 wxBitmapRefData::wxBitmapRefData()
285 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
288 Create( w
, h
, d
) ;
291 wxBitmapRefData::wxBitmapRefData(CGImageRef image
)
296 // code from Technical Q&A QA1509
298 bool wxBitmapRefData::Create(CGImageRef image
)
302 m_width
= CGImageGetWidth(image
);
303 m_height
= CGImageGetHeight(image
);
307 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
308 size_t size
= m_bytesPerRow
* m_height
;
309 void* data
= m_memBuf
.GetWriteBuf( size
) ;
312 memset( data
, 0 , size
) ;
313 m_memBuf
.UngetWriteBuf( size
) ;
314 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
315 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
317 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
322 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
324 CGRect rect
= CGRectMake(0,0,m_width
,m_height
);
325 CGContextDrawImage(m_hBitmap
, rect
, image
);
327 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
328 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
329 CGContextScaleCTM( m_hBitmap
, 1, -1 );
332 m_ok
= ( m_hBitmap
!= NULL
) ;
338 bool wxBitmapRefData::Create( int w
, int h
, int d
)
340 m_width
= wxMax(1, w
);
341 m_height
= wxMax(1, h
);
345 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
346 size_t size
= m_bytesPerRow
* m_height
;
347 void* data
= m_memBuf
.GetWriteBuf( size
) ;
350 memset( data
, 0 , size
) ;
351 m_memBuf
.UngetWriteBuf( size
) ;
353 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
354 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
355 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
356 CGContextScaleCTM( m_hBitmap
, 1, -1 );
358 m_ok
= ( m_hBitmap
!= NULL
) ;
363 void wxBitmapRefData::UseAlpha( bool use
)
365 if ( m_hasAlpha
== use
)
370 CGContextRelease( m_hBitmap
);
371 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
372 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
373 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
374 CGContextScaleCTM( m_hBitmap
, 1, -1 );
377 void *wxBitmapRefData::GetRawAccess() const
379 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
380 return m_memBuf
.GetData() ;
383 void *wxBitmapRefData::BeginRawAccess()
385 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
386 wxASSERT( m_rawAccessCount
== 0 ) ;
387 #ifndef __WXOSX_IPHONE__
389 wxASSERT_MSG( m_pictHandle
== NULL
,
390 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
392 wxASSERT_MSG( m_iconRef
== NULL
,
393 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
397 // we must destroy an existing cached image, as
398 // the bitmap data may change now
401 CGImageRelease( m_cgImageRef
) ;
402 m_cgImageRef
= NULL
;
405 return m_memBuf
.GetData() ;
408 void wxBitmapRefData::EndRawAccess()
410 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
411 wxASSERT( m_rawAccessCount
== 1 ) ;
416 bool wxBitmapRefData::HasNativeSize()
419 int h
= GetHeight() ;
420 int sz
= wxMax( w
, h
) ;
422 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
425 #ifndef __WXOSX_IPHONE__
426 IconRef
wxBitmapRefData::GetIconRef()
428 if ( m_iconRef
== NULL
)
430 // Create Icon Family Handle
432 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
435 int h
= GetHeight() ;
436 int sz
= wxMax( w
, h
) ;
438 OSType dataType
= 0 ;
439 OSType maskType
= 0 ;
442 // since we don't have PICT conversion available under 64 bit, use
443 // the next larger standard icon size
457 else if ( sz
<= 1024)
463 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
465 dataType
= kIconServices1024PixelDataARGB
;
469 dataType
= kIconServices512PixelDataARGB
;
473 dataType
= kIconServices256PixelDataARGB
;
477 dataType
= kIconServices128PixelDataARGB
;
481 dataType
= kIconServices48PixelDataARGB
;
485 dataType
= kIconServices32PixelDataARGB
;
489 dataType
= kIconServices16PixelDataARGB
;
500 size_t datasize
= sz
* sz
* 4 ;
501 Handle data
= NewHandle( datasize
) ;
503 unsigned char* ptr
= (unsigned char*) *data
;
504 memset( ptr
, 0, datasize
);
505 bool hasAlpha
= HasAlpha() ;
506 wxMask
*mask
= m_bitmapMask
;
507 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
508 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
510 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
512 unsigned char * source
= sourcePtr
;
513 unsigned char * masksource
= masksourcePtr
;
514 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
515 unsigned char a
, r
, g
, b
;
517 for ( int x
= 0 ; x
< w
; ++x
)
526 a
= 0xFF - *masksource
++ ;
528 else if ( !hasAlpha
)
532 #if wxOSX_USE_PREMULTIPLIED_ALPHA
533 // this must be non-premultiplied data
534 if ( a
!= 0xFF && a
!= 0 )
551 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
554 wxFAIL_MSG("Error when adding bitmap");
557 DisposeHandle( data
);
561 // setup the header properly
564 Handle maskdata
= NULL
;
565 unsigned char * maskptr
= NULL
;
566 unsigned char * ptr
= NULL
;
567 size_t datasize
, masksize
;
569 datasize
= sz
* sz
* 4 ;
570 data
= NewHandle( datasize
) ;
572 ptr
= (unsigned char*) *data
;
573 memset( ptr
, 0, datasize
) ;
576 maskdata
= NewHandle( masksize
) ;
578 maskptr
= (unsigned char*) *maskdata
;
579 memset( maskptr
, 0 , masksize
) ;
581 bool hasAlpha
= HasAlpha() ;
582 wxMask
*mask
= m_bitmapMask
;
583 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
584 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
586 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
588 unsigned char * source
= sourcePtr
;
589 unsigned char * masksource
= masksourcePtr
;
590 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
591 unsigned char * maskdest
= maskptr
+ y
* sz
;
592 unsigned char a
, r
, g
, b
;
594 for ( int x
= 0 ; x
< w
; ++x
)
607 *maskdest
++ = 0xFF - *masksource
++ ;
615 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
616 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
618 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
619 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
622 HUnlock( maskdata
) ;
623 DisposeHandle( data
) ;
624 DisposeHandle( maskdata
) ;
630 PicHandle pic
= GetPictHandle() ;
631 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
634 // transform into IconRef
636 // cleaner version existing from 10.3 upwards
637 HLock((Handle
) iconFamily
);
638 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
639 HUnlock((Handle
) iconFamily
);
640 DisposeHandle( (Handle
) iconFamily
) ;
642 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
648 #ifndef __WXOSX_IPHONE__
650 PicHandle
wxBitmapRefData::GetPictHandle()
652 return m_pictHandle
;
659 CGImageRef
wxBitmapRefData::CreateCGImage() const
662 wxASSERT( m_rawAccessCount
>= 0 ) ;
664 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
666 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
669 // in order for this code to work properly, wxMask would have to invert black and white
670 // in the native bitmap
673 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
674 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
675 image
= CGImageCreateWithMask( tempImage
, tempMask
);
676 CGImageRelease(tempMask
);
677 CGImageRelease(tempImage
);
681 image
= CGBitmapContextCreateImage( m_hBitmap
);
685 size_t imageSize
= m_height
* m_bytesPerRow
;
686 void * dataBuffer
= m_memBuf
.GetData() ;
689 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
690 wxMemoryBuffer membuf
;
694 alphaInfo
= kCGImageAlphaFirst
;
695 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
696 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
697 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
698 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
699 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
701 unsigned char *sourcemask
= sourcemaskstart
;
702 unsigned char *destalpha
= destalphastart
;
703 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
705 *destalpha
= 0xFF - *sourcemask
;
708 membuf
.UngetWriteBuf( imageSize
);
714 #if wxOSX_USE_PREMULTIPLIED_ALPHA
715 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
717 alphaInfo
= kCGImageAlphaFirst
;
724 CGDataProviderRef dataProvider
= NULL
;
727 // TODO CHECK ALIGNMENT
728 wxMemoryBuffer maskBuf
;
729 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
730 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
731 // copy one color component
733 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
735 unsigned char *bufDataIter
= bufData
+3;
736 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
738 maskBufData
[i
] = *bufDataIter
;
741 maskBuf
.UngetWriteBuf( m_width
* m_height
);
744 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
746 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
750 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
751 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
754 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
755 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
757 CGDataProviderRelease( dataProvider
);
762 image
= m_cgImageRef
;
763 CGImageRetain( image
) ;
766 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
768 // we keep it for later use
769 m_cgImageRef
= image
;
770 CGImageRetain( image
) ;
776 CGContextRef
wxBitmapRefData::GetBitmapContext() const
781 void wxBitmapRefData::Free()
783 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
787 CGImageRelease( m_cgImageRef
) ;
788 m_cgImageRef
= NULL
;
790 #ifndef __WXOSX_IPHONE__
793 ReleaseIconRef( m_iconRef
) ;
800 m_pictHandle
= NULL
;
806 CGContextRelease(m_hBitmap
);
810 wxDELETE(m_bitmapMask
);
813 wxBitmapRefData::~wxBitmapRefData()
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
824 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
826 bool created
= false ;
827 int w
= icon
.GetWidth() ;
828 int h
= icon
.GetHeight() ;
831 #ifdef __WXOSX_CARBON__
832 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
834 IconFamilyHandle iconFamily
= NULL
;
835 Handle imagehandle
= NewHandle( 0 ) ;
836 Handle maskhandle
= NewHandle( 0 ) ;
840 IconSelectorValue selector
= 0 ;
845 dataType
= kThumbnail32BitData
;
846 maskType
= kThumbnail8BitMask
;
847 selector
= kSelectorAllAvailableData
;
851 dataType
= kHuge32BitData
;
852 maskType
= kHuge8BitMask
;
853 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
857 dataType
= kLarge32BitData
;
858 maskType
= kLarge8BitMask
;
859 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
863 dataType
= kSmall32BitData
;
864 maskType
= kSmall8BitMask
;
865 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
872 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
874 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
875 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
876 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
877 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
879 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
881 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
882 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
886 unsigned char *source
= (unsigned char *) *imagehandle
;
887 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
888 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
890 for ( int y
= 0 ; y
< h
; ++y
)
892 for ( int x
= 0 ; x
< w
; ++x
)
894 unsigned char a
= *sourcemask
++;
897 #if wxOSX_USE_PREMULTIPLIED_ALPHA
898 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
899 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
900 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
902 *destination
++ = *source
++ ;
903 *destination
++ = *source
++ ;
904 *destination
++ = *source
++ ;
910 DisposeHandle( imagehandle
) ;
911 DisposeHandle( maskhandle
) ;
915 DisposeHandle( (Handle
) iconFamily
) ;
921 dc
.SelectObject( *this ) ;
922 dc
.DrawIcon( icon
, 0 , 0 ) ;
923 dc
.SelectObject( wxNullBitmap
) ;
929 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
931 wxBitmapRefData
* bitmapRefData
;
933 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
935 if (bitmapRefData
->IsOk())
939 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
940 if ( the_width
% (sizeof(unsigned char) * 8) )
941 linesize
+= sizeof(unsigned char);
943 unsigned char* linestart
= (unsigned char*) bits
;
944 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
946 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
948 unsigned char* destination
= destptr
;
949 int index
, bit
, mask
;
951 for ( int x
= 0 ; x
< the_width
; ++x
)
957 if ( linestart
[index
] & mask
)
959 *destination
++ = 0xFF ;
966 *destination
++ = 0xFF ;
967 *destination
++ = 0xFF ;
968 *destination
++ = 0xFF ;
969 *destination
++ = 0xFF ;
978 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
980 } /* bitmapRefData->IsOk() */
983 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
985 (void) Create(data
, type
, width
, height
, depth
);
988 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
990 LoadFile(filename
, type
);
993 wxBitmap::wxBitmap(CGImageRef image
)
995 (void) Create(image
);
998 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
1000 return new wxBitmapRefData
;
1003 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1005 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
1008 void * wxBitmap::GetRawAccess() const
1010 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1012 return M_BITMAPDATA
->GetRawAccess() ;
1015 void * wxBitmap::BeginRawAccess()
1017 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1019 return M_BITMAPDATA
->BeginRawAccess() ;
1022 void wxBitmap::EndRawAccess()
1024 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
1026 M_BITMAPDATA
->EndRawAccess() ;
1029 CGImageRef
wxBitmap::CreateCGImage() const
1031 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1033 return M_BITMAPDATA
->CreateCGImage() ;
1036 #ifndef __WXOSX_IPHONE__
1037 IconRef
wxBitmap::GetIconRef() const
1039 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1041 return M_BITMAPDATA
->GetIconRef() ;
1044 IconRef
wxBitmap::CreateIconRef() const
1046 IconRef icon
= GetIconRef();
1047 verify_noerr( AcquireIconRef(icon
) );
1054 WX_NSImage
wxBitmap::GetNSImage() const
1056 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1057 return wxOSXGetNSImageFromCGImage( cgimage
);
1062 #if wxOSX_USE_IPHONE
1064 WX_UIImage
wxBitmap::GetUIImage() const
1066 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1067 return wxOSXGetUIImageFromCGImage( cgimage
);
1071 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1073 wxCHECK_MSG( IsOk() &&
1074 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1075 (rect
.x
+rect
.width
<= GetWidth()) &&
1076 (rect
.y
+rect
.height
<= GetHeight()),
1077 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1079 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1080 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1082 int destwidth
= rect
.width
;
1083 int destheight
= rect
.height
;
1086 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1087 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1088 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1090 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1092 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1093 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1094 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1095 unsigned char *dest
= destdata
;
1097 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1099 memcpy( dest
, source
, destlinesize
) ;
1102 ret
.EndRawAccess() ;
1106 if ( M_BITMAPDATA
->m_bitmapMask
)
1108 wxMemoryBuffer maskbuf
;
1109 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1110 size_t maskbufsize
= rowBytes
* destheight
;
1112 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1113 int destlinesize
= rowBytes
;
1115 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1116 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1117 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1119 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1121 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1122 unsigned char *dest
= destdata
;
1124 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1126 memcpy( dest
, source
, destlinesize
) ;
1129 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1131 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1133 else if ( HasAlpha() )
1139 bool wxBitmap::Create(int w
, int h
, int d
)
1144 d
= wxDisplayDepth() ;
1146 m_refData
= new wxBitmapRefData( w
, h
, d
);
1148 return M_BITMAPDATA
->IsOk() ;
1152 bool wxBitmap::Create(CGImageRef image
)
1156 m_refData
= new wxBitmapRefData( image
);
1158 return M_BITMAPDATA
->IsOk() ;
1161 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1165 wxBitmapHandler
*handler
= FindHandler(type
);
1169 m_refData
= new wxBitmapRefData
;
1171 return handler
->LoadFile(this, filename
, type
, -1, -1);
1176 wxImage
loadimage(filename
, type
);
1177 if (loadimage
.IsOk())
1186 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1191 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1195 m_refData
= new wxBitmapRefData
;
1197 wxBitmapHandler
*handler
= FindHandler(type
);
1199 if ( handler
== NULL
)
1201 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1206 return handler
->Create(this, data
, type
, width
, height
, depth
);
1211 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1213 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1215 // width and height of the device-dependent bitmap
1216 int width
= image
.GetWidth();
1217 int height
= image
.GetHeight();
1219 wxBitmapRefData
* bitmapRefData
;
1221 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1223 if ( bitmapRefData
->IsOk())
1227 bool hasAlpha
= false ;
1229 if ( image
.HasMask() )
1231 // takes precedence, don't mix with alpha info
1235 hasAlpha
= image
.HasAlpha() ;
1241 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1242 register unsigned char* data
= image
.GetData();
1243 if ( destinationstart
!= NULL
&& data
!= NULL
)
1245 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1246 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1248 unsigned char * destination
= destinationstart
;
1249 for (int x
= 0; x
< width
; x
++)
1253 const unsigned char a
= *alpha
++;
1254 *destination
++ = a
;
1256 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1257 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1258 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1259 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1261 *destination
++ = *data
++ ;
1262 *destination
++ = *data
++ ;
1263 *destination
++ = *data
++ ;
1268 *destination
++ = 0xFF ;
1269 *destination
++ = *data
++ ;
1270 *destination
++ = *data
++ ;
1271 *destination
++ = *data
++ ;
1278 if ( image
.HasMask() )
1279 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1280 } /* bitmapRefData->IsOk() */
1283 wxImage
wxBitmap::ConvertToImage() const
1287 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1289 // create an wxImage object
1290 int width
= GetWidth();
1291 int height
= GetHeight();
1292 image
.Create( width
, height
);
1294 unsigned char *data
= image
.GetData();
1295 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1297 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1299 bool hasAlpha
= false ;
1300 bool hasMask
= false ;
1301 int maskBytesPerRow
= 0 ;
1302 unsigned char *alpha
= NULL
;
1303 unsigned char *mask
= NULL
;
1311 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1312 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1318 alpha
= image
.GetAlpha() ;
1323 // The following masking algorithm is the same as well in msw/gtk:
1324 // the colour used as transparent one in wxImage and the one it is
1325 // replaced with when it actually occurs in the bitmap
1326 static const int MASK_RED
= 1;
1327 static const int MASK_GREEN
= 2;
1328 static const int MASK_BLUE
= 3;
1329 static const int MASK_BLUE_REPLACEMENT
= 2;
1331 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1333 unsigned char * maskp
= mask
;
1334 unsigned char * source
= sourcestart
;
1335 unsigned char a
, r
, g
, b
;
1338 for (int xx
= 0; xx
< width
; xx
++)
1340 color
= *((long*) source
) ;
1341 #ifdef WORDS_BIGENDIAN
1342 a
= ((color
&0xFF000000) >> 24) ;
1343 r
= ((color
&0x00FF0000) >> 16) ;
1344 g
= ((color
&0x0000FF00) >> 8) ;
1345 b
= (color
&0x000000FF);
1347 b
= ((color
&0xFF000000) >> 24) ;
1348 g
= ((color
&0x00FF0000) >> 16) ;
1349 r
= ((color
&0x0000FF00) >> 8) ;
1350 a
= (color
&0x000000FF);
1354 if ( *maskp
++ == 0xFF )
1360 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1361 b
= MASK_BLUE_REPLACEMENT
;
1363 else if ( hasAlpha
)
1366 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1367 // this must be non-premultiplied data
1368 if ( a
!= 0xFF && a
!= 0 )
1378 data
[index
+ 1] = g
;
1379 data
[index
+ 2] = b
;
1387 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1392 #endif //wxUSE_IMAGE
1394 bool wxBitmap::SaveFile( const wxString
& filename
,
1395 wxBitmapType type
, const wxPalette
*palette
) const
1397 bool success
= false;
1398 wxBitmapHandler
*handler
= FindHandler(type
);
1402 success
= handler
->SaveFile(this, filename
, type
, palette
);
1407 wxImage image
= ConvertToImage();
1408 success
= image
.SaveFile(filename
, type
);
1410 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1417 int wxBitmap::GetHeight() const
1419 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1421 return M_BITMAPDATA
->GetHeight();
1424 int wxBitmap::GetWidth() const
1426 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1428 return M_BITMAPDATA
->GetWidth() ;
1431 int wxBitmap::GetDepth() const
1433 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1435 return M_BITMAPDATA
->GetDepth();
1438 wxMask
*wxBitmap::GetMask() const
1440 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1442 return M_BITMAPDATA
->m_bitmapMask
;
1445 bool wxBitmap::HasAlpha() const
1447 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1449 return M_BITMAPDATA
->HasAlpha() ;
1452 void wxBitmap::SetWidth(int w
)
1455 M_BITMAPDATA
->SetWidth(w
);
1458 void wxBitmap::SetHeight(int h
)
1461 M_BITMAPDATA
->SetHeight(h
);
1464 void wxBitmap::SetDepth(int d
)
1467 M_BITMAPDATA
->SetDepth(d
);
1470 void wxBitmap::SetOk(bool isOk
)
1473 M_BITMAPDATA
->SetOk(isOk
);
1477 wxPalette
*wxBitmap::GetPalette() const
1479 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1481 return &M_BITMAPDATA
->m_bitmapPalette
;
1484 void wxBitmap::SetPalette(const wxPalette
& palette
)
1487 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1489 #endif // wxUSE_PALETTE
1491 void wxBitmap::SetMask(wxMask
*mask
)
1494 // Remove existing mask if there is one.
1495 delete M_BITMAPDATA
->m_bitmapMask
;
1497 M_BITMAPDATA
->m_bitmapMask
= mask
;
1500 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1504 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1507 // ----------------------------------------------------------------------------
1509 // ----------------------------------------------------------------------------
1516 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1520 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1521 m_width
= tocopy
.m_width
;
1522 m_height
= tocopy
.m_height
;
1524 size_t size
= m_bytesPerRow
* m_height
;
1525 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1526 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1527 memcpy( dest
, source
, size
);
1528 m_memBuf
.UngetWriteBuf( size
) ;
1532 // Construct a mask from a bitmap and a colour indicating
1533 // the transparent area
1534 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1537 Create( bitmap
, colour
);
1540 // Construct a mask from a mono bitmap (copies the bitmap).
1541 wxMask::wxMask( const wxBitmap
& bitmap
)
1547 // Construct a mask from a mono bitmap (copies the bitmap).
1549 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1552 Create( data
, width
, height
, bytesPerRow
);
1559 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1560 m_maskBitmap
= NULL
;
1566 m_width
= m_height
= m_bytesPerRow
= 0 ;
1567 m_maskBitmap
= NULL
;
1570 void *wxMask::GetRawAccess() const
1572 return m_memBuf
.GetData() ;
1575 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1576 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1578 void wxMask::RealizeNative()
1582 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1583 m_maskBitmap
= NULL
;
1586 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1587 // from MouseTracking sample :
1588 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1589 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1591 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1592 kCGImageAlphaNone
);
1593 CGColorSpaceRelease( colorspace
);
1594 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1597 // Create a mask from a mono bitmap (copies the bitmap).
1599 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1604 m_bytesPerRow
= bytesPerRow
;
1606 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1613 // Create a mask from a mono bitmap (copies the bitmap).
1614 bool wxMask::Create(const wxBitmap
& bitmap
)
1616 m_width
= bitmap
.GetWidth() ;
1617 m_height
= bitmap
.GetHeight() ;
1618 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1620 size_t size
= m_bytesPerRow
* m_height
;
1621 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1622 wxASSERT( destdatabase
!= NULL
) ;
1626 memset( destdatabase
, 0 , size
) ;
1627 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1629 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1631 unsigned char *destdata
= destdatabase
;
1632 unsigned char r
, g
, b
;
1634 for ( int x
= 0 ; x
< m_width
; ++x
)
1641 if ( ( r
+ g
+ b
) > 0x10 )
1642 *destdata
++ = 0xFF ;
1644 *destdata
++ = 0x00 ;
1649 m_memBuf
.UngetWriteBuf( size
) ;
1655 // Create a mask from a bitmap and a colour indicating
1656 // the transparent area
1657 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1659 m_width
= bitmap
.GetWidth() ;
1660 m_height
= bitmap
.GetHeight() ;
1661 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1663 size_t size
= m_bytesPerRow
* m_height
;
1664 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1665 wxASSERT( destdatabase
!= NULL
) ;
1666 if ( destdatabase
!= NULL
)
1668 memset( destdatabase
, 0 , size
) ;
1669 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1670 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1672 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1674 unsigned char *srcdata
= srcdatabase
;
1675 unsigned char *destdata
= destdatabase
;
1676 unsigned char r
, g
, b
;
1678 for ( int x
= 0 ; x
< m_width
; ++x
)
1685 if ( colour
== wxColour( r
, g
, b
) )
1686 *destdata
++ = 0xFF ;
1688 *destdata
++ = 0x00 ;
1692 m_memBuf
.UngetWriteBuf( size
) ;
1698 WXHBITMAP
wxMask::GetHBITMAP() const
1700 return m_maskBitmap
;
1703 // ----------------------------------------------------------------------------
1704 // Standard Handlers
1705 // ----------------------------------------------------------------------------
1707 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1709 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1712 inline wxBundleResourceHandler()
1716 virtual bool LoadFile(wxBitmap
*bitmap
,
1717 const wxString
& name
,
1723 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1725 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1727 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1730 inline wxPNGResourceHandler()
1732 SetName(wxT("PNG resource"));
1733 SetExtension("PNG");
1734 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1738 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1740 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1742 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1745 inline wxJPEGResourceHandler()
1747 SetName(wxT("JPEG resource"));
1748 SetExtension("JPEG");
1749 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1753 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1755 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1756 const wxString
& name
,
1757 wxBitmapType
WXUNUSED(type
),
1758 int WXUNUSED(desiredWidth
),
1759 int WXUNUSED(desiredHeight
))
1761 wxString ext
= GetExtension().Lower();
1762 wxCFStringRef
resname(name
);
1763 wxCFStringRef
restype(ext
);
1765 wxCFRef
<CFURLRef
> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1767 if ( imageURL
.get() != NULL
)
1769 // Create the data provider object
1770 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1771 CGImageRef image
= NULL
;
1773 if ( ext
== "jpeg" )
1774 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1775 kCGRenderingIntentDefault
);
1776 else if ( ext
== "png" )
1777 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1778 kCGRenderingIntentDefault
);
1779 if ( image
!= NULL
)
1781 bitmap
->Create(image
);
1782 CGImageRelease(image
);
1790 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1792 wxCFRef
<CGDataProviderRef
>
1793 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1795 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1796 kCGRenderingIntentDefault
));
1798 return wxBitmap(image
);
1801 void wxBitmap::InitStandardHandlers()
1803 #if wxOSX_USE_COCOA_OR_CARBON
1804 AddHandler( new wxICONResourceHandler
) ;
1806 AddHandler( new wxPNGResourceHandler
);
1807 AddHandler( new wxJPEGResourceHandler
);
1810 // ----------------------------------------------------------------------------
1811 // raw bitmap access support
1812 // ----------------------------------------------------------------------------
1814 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1817 // no bitmap, no data (raw or otherwise)
1820 data
.m_width
= GetWidth() ;
1821 data
.m_height
= GetHeight() ;
1822 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1824 return BeginRawAccess() ;
1827 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1832 void wxBitmap::UseAlpha()
1834 // remember that we are using alpha channel:
1835 // we'll need to create a proper mask in UngetRawData()
1836 M_BITMAPDATA
->UseAlpha( true );