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 #ifndef __WXOSX_IPHONE__
102 // caller should increase ref count if needed longer
103 // than the bitmap exists
104 IconRef
GetIconRef();
107 CGContextRef
GetBitmapContext() const;
109 int GetBytesPerRow() const { return m_bytesPerRow
; }
111 bool Create(int width
, int height
, int depth
);
112 bool Create( CGImageRef image
);
120 wxMemoryBuffer m_memBuf
;
121 int m_rawAccessCount
;
123 mutable CGImageRef m_cgImageRef
;
125 #ifndef __WXOSX_IPHONE__
129 CGContextRef m_hBitmap
;
133 #define wxOSX_USE_PREMULTIPLIED_ALPHA 1
134 static const int kBestByteAlignement
= 16;
135 static const int kMaskBytesPerPixel
= 1;
137 static int GetBestBytesPerRow( int rawBytes
)
139 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
142 #if wxUSE_GUI && !defined(__WXOSX_IPHONE__)
144 // this is used for more controls than just the wxBitmap button, also for notebooks etc
146 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
148 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
151 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
155 if ( forceType
== 0 )
157 forceType
= kControlContentCGImageRef
;
160 if ( forceType
== kControlContentIconRef
)
163 wxBitmapRefData
* bmp
= bmap
;
165 if ( !bmap
->HasNativeSize() )
167 // as PICT conversion will only result in a 16x16 icon, let's attempt
168 // a few scales for better results
170 int w
= bitmap
.GetWidth() ;
171 int h
= bitmap
.GetHeight() ;
172 int sz
= wxMax( w
, h
) ;
173 if ( sz
== 24 || sz
== 64 )
175 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
176 bmp
= scaleBmp
.GetBitmapData() ;
180 info
->contentType
= kControlContentIconRef
;
181 info
->u
.iconRef
= bmp
->GetIconRef() ;
182 AcquireIconRef( info
->u
.iconRef
) ;
184 else if ( forceType
== kControlContentCGImageRef
)
186 info
->contentType
= kControlContentCGImageRef
;
187 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
192 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
194 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
197 return (CGImageRef
) bmap
->CreateCGImage();
200 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
202 if ( info
->contentType
== kControlContentIconRef
)
204 ReleaseIconRef( info
->u
.iconRef
) ;
206 else if ( info
->contentType
== kControlNoContent
)
208 // there's no bitmap at all, fall through silently
210 else if ( info
->contentType
== kControlContentPictHandle
)
212 // owned by the bitmap, no release here
214 else if ( info
->contentType
== kControlContentCGImageRef
)
216 CGImageRelease( info
->u
.imageRef
) ;
220 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
224 #endif //wxUSE_BMPBUTTON
226 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
228 void wxBitmapRefData::Init()
235 m_bitmapMask
= NULL
;
236 m_cgImageRef
= NULL
;
238 #ifndef __WXOSX_IPHONE__
243 m_rawAccessCount
= 0 ;
247 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
) : wxGDIRefData()
250 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
252 if (tocopy
.m_bitmapMask
)
253 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
254 else if (tocopy
.m_hasAlpha
)
257 unsigned char* dest
= (unsigned char*)GetRawAccess();
258 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
259 size_t numbytes
= m_bytesPerRow
* m_height
;
260 memcpy( dest
, source
, numbytes
);
263 wxBitmapRefData::wxBitmapRefData()
268 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
271 Create( w
, h
, d
) ;
274 wxBitmapRefData::wxBitmapRefData(CGImageRef image
)
279 // code from Technical Q&A QA1509
281 bool wxBitmapRefData::Create(CGImageRef image
)
285 m_width
= CGImageGetWidth(image
);
286 m_height
= CGImageGetHeight(image
);
290 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
291 size_t size
= m_bytesPerRow
* m_height
;
292 void* data
= m_memBuf
.GetWriteBuf( size
) ;
295 memset( data
, 0 , size
) ;
296 m_memBuf
.UngetWriteBuf( size
) ;
297 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
298 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipFirst
|| alpha
== kCGImageAlphaNoneSkipLast
)
300 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
305 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
307 CGRect rect
= CGRectMake(0,0,m_width
,m_height
);
308 CGContextDrawImage(m_hBitmap
, rect
, image
);
310 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
311 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
312 CGContextScaleCTM( m_hBitmap
, 1, -1 );
315 m_ok
= ( m_hBitmap
!= NULL
) ;
321 bool wxBitmapRefData::Create( int w
, int h
, int d
)
323 m_width
= wxMax(1, w
);
324 m_height
= wxMax(1, h
);
328 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
329 size_t size
= m_bytesPerRow
* m_height
;
330 void* data
= m_memBuf
.GetWriteBuf( size
) ;
333 memset( data
, 0 , size
) ;
334 m_memBuf
.UngetWriteBuf( size
) ;
336 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
337 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
338 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
339 CGContextScaleCTM( m_hBitmap
, 1, -1 );
341 m_ok
= ( m_hBitmap
!= NULL
) ;
346 void wxBitmapRefData::UseAlpha( bool use
)
348 if ( m_hasAlpha
== use
)
353 CGContextRelease( m_hBitmap
);
354 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
355 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
356 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
357 CGContextScaleCTM( m_hBitmap
, 1, -1 );
360 void *wxBitmapRefData::GetRawAccess() const
362 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
363 return m_memBuf
.GetData() ;
366 void *wxBitmapRefData::BeginRawAccess()
368 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
369 wxASSERT( m_rawAccessCount
== 0 ) ;
370 #ifndef __WXOSX_IPHONE__
371 wxASSERT_MSG( m_iconRef
== NULL
,
372 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
376 // we must destroy an existing cached image, as
377 // the bitmap data may change now
380 CGImageRelease( m_cgImageRef
) ;
381 m_cgImageRef
= NULL
;
384 return m_memBuf
.GetData() ;
387 void wxBitmapRefData::EndRawAccess()
389 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
390 wxASSERT( m_rawAccessCount
== 1 ) ;
395 bool wxBitmapRefData::HasNativeSize()
398 int h
= GetHeight() ;
399 int sz
= wxMax( w
, h
) ;
401 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
404 #ifndef __WXOSX_IPHONE__
405 IconRef
wxBitmapRefData::GetIconRef()
407 if ( m_iconRef
== NULL
)
409 // Create Icon Family Handle
411 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
414 int h
= GetHeight() ;
415 int sz
= wxMax( w
, h
) ;
417 OSType dataType
= 0 ;
418 OSType maskType
= 0 ;
420 // since we don't have PICT conversion available, use
421 // the next larger standard icon size
435 else if ( sz
<= 1024)
440 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
442 dataType
= kIconServices1024PixelDataARGB
;
446 dataType
= kIconServices512PixelDataARGB
;
450 dataType
= kIconServices256PixelDataARGB
;
454 dataType
= kIconServices128PixelDataARGB
;
458 dataType
= kIconServices48PixelDataARGB
;
462 dataType
= kIconServices32PixelDataARGB
;
466 dataType
= kIconServices16PixelDataARGB
;
477 size_t datasize
= sz
* sz
* 4 ;
478 Handle data
= NewHandle( datasize
) ;
480 unsigned char* ptr
= (unsigned char*) *data
;
481 memset( ptr
, 0, datasize
);
482 bool hasAlpha
= HasAlpha() ;
483 wxMask
*mask
= m_bitmapMask
;
484 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
485 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
487 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
489 unsigned char * source
= sourcePtr
;
490 unsigned char * masksource
= masksourcePtr
;
491 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
492 unsigned char a
, r
, g
, b
;
494 for ( int x
= 0 ; x
< w
; ++x
)
503 a
= 0xFF - *masksource
++ ;
505 else if ( !hasAlpha
)
509 #if wxOSX_USE_PREMULTIPLIED_ALPHA
510 // this must be non-premultiplied data
511 if ( a
!= 0xFF && a
!= 0 )
528 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
531 wxFAIL_MSG("Error when adding bitmap");
534 DisposeHandle( data
);
538 // setup the header properly
541 Handle maskdata
= NULL
;
542 unsigned char * maskptr
= NULL
;
543 unsigned char * ptr
= NULL
;
544 size_t datasize
, masksize
;
546 datasize
= sz
* sz
* 4 ;
547 data
= NewHandle( datasize
) ;
549 ptr
= (unsigned char*) *data
;
550 memset( ptr
, 0, datasize
) ;
553 maskdata
= NewHandle( masksize
) ;
555 maskptr
= (unsigned char*) *maskdata
;
556 memset( maskptr
, 0 , masksize
) ;
558 bool hasAlpha
= HasAlpha() ;
559 wxMask
*mask
= m_bitmapMask
;
560 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
561 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
563 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
565 unsigned char * source
= sourcePtr
;
566 unsigned char * masksource
= masksourcePtr
;
567 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
568 unsigned char * maskdest
= maskptr
+ y
* sz
;
569 unsigned char a
, r
, g
, b
;
571 for ( int x
= 0 ; x
< w
; ++x
)
584 *maskdest
++ = 0xFF - *masksource
++ ;
592 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
593 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
595 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
596 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
599 HUnlock( maskdata
) ;
600 DisposeHandle( data
) ;
601 DisposeHandle( maskdata
) ;
604 // transform into IconRef
606 // cleaner version existing from 10.3 upwards
607 HLock((Handle
) iconFamily
);
608 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
609 HUnlock((Handle
) iconFamily
);
610 DisposeHandle( (Handle
) iconFamily
) ;
612 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
620 CGImageRef
wxBitmapRefData::CreateCGImage() const
623 wxASSERT( m_rawAccessCount
>= 0 ) ;
625 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
627 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
630 // in order for this code to work properly, wxMask would have to invert black and white
631 // in the native bitmap
634 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
635 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
636 image
= CGImageCreateWithMask( tempImage
, tempMask
);
637 CGImageRelease(tempMask
);
638 CGImageRelease(tempImage
);
642 image
= CGBitmapContextCreateImage( m_hBitmap
);
646 size_t imageSize
= m_height
* m_bytesPerRow
;
647 void * dataBuffer
= m_memBuf
.GetData() ;
650 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
651 wxMemoryBuffer membuf
;
655 alphaInfo
= kCGImageAlphaFirst
;
656 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
657 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
658 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
659 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
660 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
662 unsigned char *sourcemask
= sourcemaskstart
;
663 unsigned char *destalpha
= destalphastart
;
664 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
666 *destalpha
= 0xFF - *sourcemask
;
669 membuf
.UngetWriteBuf( imageSize
);
675 #if wxOSX_USE_PREMULTIPLIED_ALPHA
676 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
678 alphaInfo
= kCGImageAlphaFirst
;
685 CGDataProviderRef dataProvider
= NULL
;
688 // TODO CHECK ALIGNMENT
689 wxMemoryBuffer maskBuf
;
690 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
691 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
692 // copy one color component
694 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
696 unsigned char *bufDataIter
= bufData
+3;
697 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
699 maskBufData
[i
] = *bufDataIter
;
702 maskBuf
.UngetWriteBuf( m_width
* m_height
);
705 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
707 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
711 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
712 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
715 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
716 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
718 CGDataProviderRelease( dataProvider
);
723 image
= m_cgImageRef
;
724 CGImageRetain( image
) ;
727 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
729 // we keep it for later use
730 m_cgImageRef
= image
;
731 CGImageRetain( image
) ;
737 CGContextRef
wxBitmapRefData::GetBitmapContext() const
742 void wxBitmapRefData::Free()
744 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
748 CGImageRelease( m_cgImageRef
) ;
749 m_cgImageRef
= NULL
;
751 #ifndef __WXOSX_IPHONE__
754 ReleaseIconRef( m_iconRef
) ;
760 CGContextRelease(m_hBitmap
);
764 wxDELETE(m_bitmapMask
);
767 wxBitmapRefData::~wxBitmapRefData()
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
778 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
780 bool created
= false ;
781 int w
= icon
.GetWidth() ;
782 int h
= icon
.GetHeight() ;
785 #ifdef __WXOSX_CARBON__
786 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
788 IconFamilyHandle iconFamily
= NULL
;
789 Handle imagehandle
= NewHandle( 0 ) ;
790 Handle maskhandle
= NewHandle( 0 ) ;
794 IconSelectorValue selector
= 0 ;
799 dataType
= kThumbnail32BitData
;
800 maskType
= kThumbnail8BitMask
;
801 selector
= kSelectorAllAvailableData
;
805 dataType
= kHuge32BitData
;
806 maskType
= kHuge8BitMask
;
807 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
811 dataType
= kLarge32BitData
;
812 maskType
= kLarge8BitMask
;
813 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
817 dataType
= kSmall32BitData
;
818 maskType
= kSmall8BitMask
;
819 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
826 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
828 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
829 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
830 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
831 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
833 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
835 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
836 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
840 unsigned char *source
= (unsigned char *) *imagehandle
;
841 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
842 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
844 for ( int y
= 0 ; y
< h
; ++y
)
846 for ( int x
= 0 ; x
< w
; ++x
)
848 unsigned char a
= *sourcemask
++;
851 #if wxOSX_USE_PREMULTIPLIED_ALPHA
852 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
853 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
854 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
856 *destination
++ = *source
++ ;
857 *destination
++ = *source
++ ;
858 *destination
++ = *source
++ ;
864 DisposeHandle( imagehandle
) ;
865 DisposeHandle( maskhandle
) ;
869 DisposeHandle( (Handle
) iconFamily
) ;
875 dc
.SelectObject( *this ) ;
876 dc
.DrawIcon( icon
, 0 , 0 ) ;
877 dc
.SelectObject( wxNullBitmap
) ;
883 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
885 wxBitmapRefData
* bitmapRefData
;
887 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
889 if (bitmapRefData
->IsOk())
893 int linesize
= the_width
/ 8;
897 unsigned char* linestart
= (unsigned char*) bits
;
898 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
900 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
902 unsigned char* destination
= destptr
;
903 int index
, bit
, mask
;
905 for ( int x
= 0 ; x
< the_width
; ++x
)
911 if ( linestart
[index
] & mask
)
913 *destination
++ = 0xFF ;
920 *destination
++ = 0xFF ;
921 *destination
++ = 0xFF ;
922 *destination
++ = 0xFF ;
923 *destination
++ = 0xFF ;
932 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
934 } /* bitmapRefData->IsOk() */
937 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
939 (void) Create(data
, type
, width
, height
, depth
);
942 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
944 LoadFile(filename
, type
);
947 wxBitmap::wxBitmap(CGImageRef image
)
949 (void) Create(image
);
952 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
954 return new wxBitmapRefData
;
957 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
959 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
962 void * wxBitmap::GetRawAccess() const
964 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
966 return M_BITMAPDATA
->GetRawAccess() ;
969 void * wxBitmap::BeginRawAccess()
971 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
973 return M_BITMAPDATA
->BeginRawAccess() ;
976 void wxBitmap::EndRawAccess()
978 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
980 M_BITMAPDATA
->EndRawAccess() ;
983 CGImageRef
wxBitmap::CreateCGImage() const
985 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
987 return M_BITMAPDATA
->CreateCGImage() ;
990 #ifndef __WXOSX_IPHONE__
991 IconRef
wxBitmap::GetIconRef() const
993 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
995 return M_BITMAPDATA
->GetIconRef() ;
998 IconRef
wxBitmap::CreateIconRef() const
1000 IconRef icon
= GetIconRef();
1001 verify_noerr( AcquireIconRef(icon
) );
1008 wxBitmap::wxBitmap(WX_NSImage image
)
1010 (void)Create(image
);
1013 bool wxBitmap::Create(WX_NSImage image
)
1015 wxCFRef
<CGImageRef
> cgimage(wxOSXCreateCGImageFromNSImage(image
));
1016 return Create(cgimage
);
1019 WX_NSImage
wxBitmap::GetNSImage() const
1021 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1022 return wxOSXGetNSImageFromCGImage( cgimage
);
1027 #if wxOSX_USE_IPHONE
1029 WX_UIImage
wxBitmap::GetUIImage() const
1031 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1032 return wxOSXGetUIImageFromCGImage( cgimage
);
1036 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1038 wxCHECK_MSG( IsOk() &&
1039 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1040 (rect
.x
+rect
.width
<= GetWidth()) &&
1041 (rect
.y
+rect
.height
<= GetHeight()),
1042 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1044 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1045 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1047 int destwidth
= rect
.width
;
1048 int destheight
= rect
.height
;
1051 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1052 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1053 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1055 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1057 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1058 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1059 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1060 unsigned char *dest
= destdata
;
1062 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1064 memcpy( dest
, source
, destlinesize
) ;
1067 ret
.EndRawAccess() ;
1071 if ( M_BITMAPDATA
->m_bitmapMask
)
1073 wxMemoryBuffer maskbuf
;
1074 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1075 size_t maskbufsize
= rowBytes
* destheight
;
1077 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1078 int destlinesize
= rowBytes
;
1080 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1081 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1082 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1084 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1086 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1087 unsigned char *dest
= destdata
;
1089 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1091 memcpy( dest
, source
, destlinesize
) ;
1094 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1096 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1098 else if ( HasAlpha() )
1104 bool wxBitmap::Create(int w
, int h
, int d
)
1109 d
= wxDisplayDepth() ;
1111 m_refData
= new wxBitmapRefData( w
, h
, d
);
1113 return M_BITMAPDATA
->IsOk() ;
1117 bool wxBitmap::Create(CGImageRef image
)
1121 m_refData
= new wxBitmapRefData( image
);
1123 return M_BITMAPDATA
->IsOk() ;
1126 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1130 wxBitmapHandler
*handler
= FindHandler(type
);
1134 m_refData
= new wxBitmapRefData
;
1136 return handler
->LoadFile(this, filename
, type
, -1, -1);
1141 wxImage
loadimage(filename
, type
);
1142 if (loadimage
.IsOk())
1151 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1156 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1160 m_refData
= new wxBitmapRefData
;
1162 wxBitmapHandler
*handler
= FindHandler(type
);
1164 if ( handler
== NULL
)
1166 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1171 return handler
->Create(this, data
, type
, width
, height
, depth
);
1176 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1178 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1180 // width and height of the device-dependent bitmap
1181 int width
= image
.GetWidth();
1182 int height
= image
.GetHeight();
1184 wxBitmapRefData
* bitmapRefData
;
1186 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1188 if ( bitmapRefData
->IsOk())
1192 bool hasAlpha
= false ;
1194 if ( image
.HasMask() )
1196 // takes precedence, don't mix with alpha info
1200 hasAlpha
= image
.HasAlpha() ;
1206 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1207 register unsigned char* data
= image
.GetData();
1208 if ( destinationstart
!= NULL
&& data
!= NULL
)
1210 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1211 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1213 unsigned char * destination
= destinationstart
;
1214 for (int x
= 0; x
< width
; x
++)
1218 const unsigned char a
= *alpha
++;
1219 *destination
++ = a
;
1221 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1222 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1223 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1224 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1226 *destination
++ = *data
++ ;
1227 *destination
++ = *data
++ ;
1228 *destination
++ = *data
++ ;
1233 *destination
++ = 0xFF ;
1234 *destination
++ = *data
++ ;
1235 *destination
++ = *data
++ ;
1236 *destination
++ = *data
++ ;
1243 if ( image
.HasMask() )
1244 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1245 } /* bitmapRefData->IsOk() */
1248 wxImage
wxBitmap::ConvertToImage() const
1252 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1254 // create an wxImage object
1255 int width
= GetWidth();
1256 int height
= GetHeight();
1257 image
.Create( width
, height
);
1259 unsigned char *data
= image
.GetData();
1260 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1262 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1264 bool hasAlpha
= false ;
1265 bool hasMask
= false ;
1266 int maskBytesPerRow
= 0 ;
1267 unsigned char *alpha
= NULL
;
1268 unsigned char *mask
= NULL
;
1276 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1277 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1283 alpha
= image
.GetAlpha() ;
1288 // The following masking algorithm is the same as well in msw/gtk:
1289 // the colour used as transparent one in wxImage and the one it is
1290 // replaced with when it actually occurs in the bitmap
1291 static const int MASK_RED
= 1;
1292 static const int MASK_GREEN
= 2;
1293 static const int MASK_BLUE
= 3;
1294 static const int MASK_BLUE_REPLACEMENT
= 2;
1296 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1298 unsigned char * maskp
= mask
;
1299 unsigned char * source
= sourcestart
;
1300 unsigned char a
, r
, g
, b
;
1303 for (int xx
= 0; xx
< width
; xx
++)
1305 color
= *((long*) source
) ;
1306 #ifdef WORDS_BIGENDIAN
1307 a
= ((color
&0xFF000000) >> 24) ;
1308 r
= ((color
&0x00FF0000) >> 16) ;
1309 g
= ((color
&0x0000FF00) >> 8) ;
1310 b
= (color
&0x000000FF);
1312 b
= ((color
&0xFF000000) >> 24) ;
1313 g
= ((color
&0x00FF0000) >> 16) ;
1314 r
= ((color
&0x0000FF00) >> 8) ;
1315 a
= (color
&0x000000FF);
1319 if ( *maskp
++ == 0xFF )
1325 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1326 b
= MASK_BLUE_REPLACEMENT
;
1328 else if ( hasAlpha
)
1331 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1332 // this must be non-premultiplied data
1333 if ( a
!= 0xFF && a
!= 0 )
1343 data
[index
+ 1] = g
;
1344 data
[index
+ 2] = b
;
1352 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1357 #endif //wxUSE_IMAGE
1359 bool wxBitmap::SaveFile( const wxString
& filename
,
1360 wxBitmapType type
, const wxPalette
*palette
) const
1362 bool success
= false;
1363 wxBitmapHandler
*handler
= FindHandler(type
);
1367 success
= handler
->SaveFile(this, filename
, type
, palette
);
1372 wxImage image
= ConvertToImage();
1373 success
= image
.SaveFile(filename
, type
);
1375 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1382 int wxBitmap::GetHeight() const
1384 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1386 return M_BITMAPDATA
->GetHeight();
1389 int wxBitmap::GetWidth() const
1391 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1393 return M_BITMAPDATA
->GetWidth() ;
1396 int wxBitmap::GetDepth() const
1398 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1400 return M_BITMAPDATA
->GetDepth();
1403 wxMask
*wxBitmap::GetMask() const
1405 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1407 return M_BITMAPDATA
->m_bitmapMask
;
1410 bool wxBitmap::HasAlpha() const
1412 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1414 return M_BITMAPDATA
->HasAlpha() ;
1417 void wxBitmap::SetWidth(int w
)
1420 M_BITMAPDATA
->SetWidth(w
);
1423 void wxBitmap::SetHeight(int h
)
1426 M_BITMAPDATA
->SetHeight(h
);
1429 void wxBitmap::SetDepth(int d
)
1432 M_BITMAPDATA
->SetDepth(d
);
1435 void wxBitmap::SetOk(bool isOk
)
1438 M_BITMAPDATA
->SetOk(isOk
);
1442 wxPalette
*wxBitmap::GetPalette() const
1444 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1446 return &M_BITMAPDATA
->m_bitmapPalette
;
1449 void wxBitmap::SetPalette(const wxPalette
& palette
)
1452 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1454 #endif // wxUSE_PALETTE
1456 void wxBitmap::SetMask(wxMask
*mask
)
1459 // Remove existing mask if there is one.
1460 delete M_BITMAPDATA
->m_bitmapMask
;
1462 M_BITMAPDATA
->m_bitmapMask
= mask
;
1465 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1469 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1472 // ----------------------------------------------------------------------------
1474 // ----------------------------------------------------------------------------
1481 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1485 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1486 m_width
= tocopy
.m_width
;
1487 m_height
= tocopy
.m_height
;
1489 size_t size
= m_bytesPerRow
* m_height
;
1490 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1491 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1492 memcpy( dest
, source
, size
);
1493 m_memBuf
.UngetWriteBuf( size
) ;
1497 // Construct a mask from a bitmap and a colour indicating
1498 // the transparent area
1499 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1502 Create( bitmap
, colour
);
1505 // Construct a mask from a mono bitmap (copies the bitmap).
1506 wxMask::wxMask( const wxBitmap
& bitmap
)
1512 // Construct a mask from a mono bitmap (copies the bitmap).
1514 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1517 Create( data
, width
, height
, bytesPerRow
);
1524 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1525 m_maskBitmap
= NULL
;
1531 m_width
= m_height
= m_bytesPerRow
= 0 ;
1532 m_maskBitmap
= NULL
;
1535 void *wxMask::GetRawAccess() const
1537 return m_memBuf
.GetData() ;
1540 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1541 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1543 void wxMask::RealizeNative()
1547 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1548 m_maskBitmap
= NULL
;
1551 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1552 // from MouseTracking sample :
1553 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1554 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1556 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1557 kCGImageAlphaNone
);
1558 CGColorSpaceRelease( colorspace
);
1559 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1562 // Create a mask from a mono bitmap (copies the bitmap).
1564 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1569 m_bytesPerRow
= bytesPerRow
;
1571 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1578 // Create a mask from a mono bitmap (copies the bitmap).
1579 bool wxMask::Create(const wxBitmap
& bitmap
)
1581 m_width
= bitmap
.GetWidth() ;
1582 m_height
= bitmap
.GetHeight() ;
1583 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1585 size_t size
= m_bytesPerRow
* m_height
;
1586 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1587 wxASSERT( destdatabase
!= NULL
) ;
1591 memset( destdatabase
, 0 , size
) ;
1592 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1594 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1596 unsigned char *destdata
= destdatabase
;
1597 unsigned char r
, g
, b
;
1599 for ( int x
= 0 ; x
< m_width
; ++x
)
1606 if ( ( r
+ g
+ b
) > 0x10 )
1607 *destdata
++ = 0xFF ;
1609 *destdata
++ = 0x00 ;
1614 m_memBuf
.UngetWriteBuf( size
) ;
1620 // Create a mask from a bitmap and a colour indicating
1621 // the transparent area
1622 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1624 m_width
= bitmap
.GetWidth() ;
1625 m_height
= bitmap
.GetHeight() ;
1626 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1628 size_t size
= m_bytesPerRow
* m_height
;
1629 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1630 wxASSERT( destdatabase
!= NULL
) ;
1631 if ( destdatabase
!= NULL
)
1633 memset( destdatabase
, 0 , size
) ;
1634 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1635 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1637 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1639 unsigned char *srcdata
= srcdatabase
;
1640 unsigned char *destdata
= destdatabase
;
1641 unsigned char r
, g
, b
;
1643 for ( int x
= 0 ; x
< m_width
; ++x
)
1650 if ( colour
== wxColour( r
, g
, b
) )
1651 *destdata
++ = 0xFF ;
1653 *destdata
++ = 0x00 ;
1657 m_memBuf
.UngetWriteBuf( size
) ;
1663 wxBitmap
wxMask::GetBitmap() const
1665 wxBitmap
bitmap(m_width
, m_height
, 1);
1666 unsigned char* dst
= static_cast<unsigned char*>(bitmap
.BeginRawAccess());
1667 const int dst_stride
= bitmap
.GetBitmapData()->GetBytesPerRow();
1668 const unsigned char* src
= static_cast<unsigned char*>(GetRawAccess());
1669 for (int j
= 0; j
< m_height
; j
++, src
+= m_bytesPerRow
, dst
+= dst_stride
)
1671 unsigned char* d
= dst
;
1672 for (int i
= 0; i
< m_width
; i
++)
1674 const unsigned char byte
= src
[i
];
1681 bitmap
.EndRawAccess();
1685 WXHBITMAP
wxMask::GetHBITMAP() const
1687 return m_maskBitmap
;
1690 // ----------------------------------------------------------------------------
1691 // Standard Handlers
1692 // ----------------------------------------------------------------------------
1694 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1696 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1699 inline wxBundleResourceHandler()
1703 virtual bool LoadFile(wxBitmap
*bitmap
,
1704 const wxString
& name
,
1710 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1712 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1714 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1717 inline wxPNGResourceHandler()
1719 SetName(wxT("PNG resource"));
1720 SetExtension("PNG");
1721 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1725 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1727 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1729 DECLARE_DYNAMIC_CLASS(wxJPEGResourceHandler
)
1732 inline wxJPEGResourceHandler()
1734 SetName(wxT("JPEG resource"));
1735 SetExtension("JPEG");
1736 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1740 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1742 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1743 const wxString
& name
,
1744 wxBitmapType
WXUNUSED(type
),
1745 int WXUNUSED(desiredWidth
),
1746 int WXUNUSED(desiredHeight
))
1748 wxString ext
= GetExtension().Lower();
1749 wxCFStringRef
resname(name
);
1750 wxCFStringRef
restype(ext
);
1752 wxCFRef
<CFURLRef
> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1754 if ( imageURL
.get() != NULL
)
1756 // Create the data provider object
1757 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1758 CGImageRef image
= NULL
;
1760 if ( ext
== "jpeg" )
1761 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1762 kCGRenderingIntentDefault
);
1763 else if ( ext
== "png" )
1764 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1765 kCGRenderingIntentDefault
);
1766 if ( image
!= NULL
)
1768 bitmap
->Create(image
);
1769 CGImageRelease(image
);
1777 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1779 wxCFRef
<CGDataProviderRef
>
1780 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1782 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1783 kCGRenderingIntentDefault
));
1785 return wxBitmap(image
);
1788 void wxBitmap::InitStandardHandlers()
1790 #if wxOSX_USE_COCOA_OR_CARBON
1791 AddHandler( new wxICONResourceHandler
) ;
1793 AddHandler( new wxPNGResourceHandler
);
1794 AddHandler( new wxJPEGResourceHandler
);
1797 // ----------------------------------------------------------------------------
1798 // raw bitmap access support
1799 // ----------------------------------------------------------------------------
1801 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1804 // no bitmap, no data (raw or otherwise)
1807 data
.m_width
= GetWidth() ;
1808 data
.m_height
= GetHeight() ;
1809 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1811 return BeginRawAccess() ;
1814 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1819 void wxBitmap::UseAlpha()
1821 // remember that we are using alpha channel:
1822 // we'll need to create a proper mask in UngetRawData()
1823 M_BITMAPDATA
->UseAlpha( true );