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)
464 dataType
= kIconServices1024PixelDataARGB
;
468 dataType
= kIconServices512PixelDataARGB
;
472 dataType
= kIconServices256PixelDataARGB
;
476 dataType
= kIconServices128PixelDataARGB
;
480 dataType
= kIconServices48PixelDataARGB
;
484 dataType
= kIconServices32PixelDataARGB
;
488 dataType
= kIconServices16PixelDataARGB
;
499 size_t datasize
= sz
* sz
* 4 ;
500 Handle data
= NewHandle( datasize
) ;
502 unsigned char* ptr
= (unsigned char*) *data
;
503 memset( ptr
, 0, datasize
);
504 bool hasAlpha
= HasAlpha() ;
505 wxMask
*mask
= m_bitmapMask
;
506 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
507 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
509 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
511 unsigned char * source
= sourcePtr
;
512 unsigned char * masksource
= masksourcePtr
;
513 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
514 unsigned char a
, r
, g
, b
;
516 for ( int x
= 0 ; x
< w
; ++x
)
525 a
= 0xFF - *masksource
++ ;
527 else if ( !hasAlpha
)
531 #if wxOSX_USE_PREMULTIPLIED_ALPHA
532 // this must be non-premultiplied data
533 if ( a
!= 0xFF && a
!= 0 )
550 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
553 wxFAIL_MSG("Error when adding bitmap");
556 DisposeHandle( data
);
560 // setup the header properly
563 Handle maskdata
= NULL
;
564 unsigned char * maskptr
= NULL
;
565 unsigned char * ptr
= NULL
;
566 size_t datasize
, masksize
;
568 datasize
= sz
* sz
* 4 ;
569 data
= NewHandle( datasize
) ;
571 ptr
= (unsigned char*) *data
;
572 memset( ptr
, 0, datasize
) ;
575 maskdata
= NewHandle( masksize
) ;
577 maskptr
= (unsigned char*) *maskdata
;
578 memset( maskptr
, 0 , masksize
) ;
580 bool hasAlpha
= HasAlpha() ;
581 wxMask
*mask
= m_bitmapMask
;
582 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
583 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
585 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
587 unsigned char * source
= sourcePtr
;
588 unsigned char * masksource
= masksourcePtr
;
589 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
590 unsigned char * maskdest
= maskptr
+ y
* sz
;
591 unsigned char a
, r
, g
, b
;
593 for ( int x
= 0 ; x
< w
; ++x
)
606 *maskdest
++ = 0xFF - *masksource
++ ;
614 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
615 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
617 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
618 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
621 HUnlock( maskdata
) ;
622 DisposeHandle( data
) ;
623 DisposeHandle( maskdata
) ;
629 PicHandle pic
= GetPictHandle() ;
630 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
633 // transform into IconRef
635 // cleaner version existing from 10.3 upwards
636 HLock((Handle
) iconFamily
);
637 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
638 HUnlock((Handle
) iconFamily
);
639 DisposeHandle( (Handle
) iconFamily
) ;
641 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
647 #ifndef __WXOSX_IPHONE__
649 PicHandle
wxBitmapRefData::GetPictHandle()
651 return m_pictHandle
;
658 CGImageRef
wxBitmapRefData::CreateCGImage() const
661 wxASSERT( m_rawAccessCount
>= 0 ) ;
663 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
665 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
668 // in order for this code to work properly, wxMask would have to invert black and white
669 // in the native bitmap
672 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
673 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
674 image
= CGImageCreateWithMask( tempImage
, tempMask
);
675 CGImageRelease(tempMask
);
676 CGImageRelease(tempImage
);
680 image
= CGBitmapContextCreateImage( m_hBitmap
);
684 size_t imageSize
= m_height
* m_bytesPerRow
;
685 void * dataBuffer
= m_memBuf
.GetData() ;
688 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
689 wxMemoryBuffer membuf
;
693 alphaInfo
= kCGImageAlphaFirst
;
694 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
695 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
696 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
697 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
698 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
700 unsigned char *sourcemask
= sourcemaskstart
;
701 unsigned char *destalpha
= destalphastart
;
702 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
704 *destalpha
= 0xFF - *sourcemask
;
707 membuf
.UngetWriteBuf( imageSize
);
713 #if wxOSX_USE_PREMULTIPLIED_ALPHA
714 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
716 alphaInfo
= kCGImageAlphaFirst
;
723 CGDataProviderRef dataProvider
= NULL
;
726 // TODO CHECK ALIGNMENT
727 wxMemoryBuffer maskBuf
;
728 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
729 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
730 // copy one color component
732 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
734 unsigned char *bufDataIter
= bufData
+3;
735 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
737 maskBufData
[i
] = *bufDataIter
;
740 maskBuf
.UngetWriteBuf( m_width
* m_height
);
743 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
745 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
749 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
750 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
753 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
754 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
756 CGDataProviderRelease( dataProvider
);
761 image
= m_cgImageRef
;
762 CGImageRetain( image
) ;
765 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
767 // we keep it for later use
768 m_cgImageRef
= image
;
769 CGImageRetain( image
) ;
775 CGContextRef
wxBitmapRefData::GetBitmapContext() const
780 void wxBitmapRefData::Free()
782 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
786 CGImageRelease( m_cgImageRef
) ;
787 m_cgImageRef
= NULL
;
789 #ifndef __WXOSX_IPHONE__
792 ReleaseIconRef( m_iconRef
) ;
799 m_pictHandle
= NULL
;
805 CGContextRelease(m_hBitmap
);
809 wxDELETE(m_bitmapMask
);
812 wxBitmapRefData::~wxBitmapRefData()
819 // ----------------------------------------------------------------------------
821 // ----------------------------------------------------------------------------
823 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
825 bool created
= false ;
826 int w
= icon
.GetWidth() ;
827 int h
= icon
.GetHeight() ;
830 #ifdef __WXOSX_CARBON__
831 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
833 IconFamilyHandle iconFamily
= NULL
;
834 Handle imagehandle
= NewHandle( 0 ) ;
835 Handle maskhandle
= NewHandle( 0 ) ;
839 IconSelectorValue selector
= 0 ;
844 dataType
= kThumbnail32BitData
;
845 maskType
= kThumbnail8BitMask
;
846 selector
= kSelectorAllAvailableData
;
850 dataType
= kHuge32BitData
;
851 maskType
= kHuge8BitMask
;
852 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
856 dataType
= kLarge32BitData
;
857 maskType
= kLarge8BitMask
;
858 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
862 dataType
= kSmall32BitData
;
863 maskType
= kSmall8BitMask
;
864 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
871 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
873 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
874 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
875 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
876 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
878 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
880 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
881 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
885 unsigned char *source
= (unsigned char *) *imagehandle
;
886 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
887 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
889 for ( int y
= 0 ; y
< h
; ++y
)
891 for ( int x
= 0 ; x
< w
; ++x
)
893 unsigned char a
= *sourcemask
++;
896 #if wxOSX_USE_PREMULTIPLIED_ALPHA
897 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
898 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
899 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
901 *destination
++ = *source
++ ;
902 *destination
++ = *source
++ ;
903 *destination
++ = *source
++ ;
909 DisposeHandle( imagehandle
) ;
910 DisposeHandle( maskhandle
) ;
914 DisposeHandle( (Handle
) iconFamily
) ;
920 dc
.SelectObject( *this ) ;
921 dc
.DrawIcon( icon
, 0 , 0 ) ;
922 dc
.SelectObject( wxNullBitmap
) ;
928 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
930 wxBitmapRefData
* bitmapRefData
;
932 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
934 if (bitmapRefData
->IsOk())
938 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
939 if ( the_width
% (sizeof(unsigned char) * 8) )
940 linesize
+= sizeof(unsigned char);
942 unsigned char* linestart
= (unsigned char*) bits
;
943 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
945 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
947 unsigned char* destination
= destptr
;
948 int index
, bit
, mask
;
950 for ( int x
= 0 ; x
< the_width
; ++x
)
956 if ( linestart
[index
] & mask
)
958 *destination
++ = 0xFF ;
965 *destination
++ = 0xFF ;
966 *destination
++ = 0xFF ;
967 *destination
++ = 0xFF ;
968 *destination
++ = 0xFF ;
977 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
979 } /* bitmapRefData->IsOk() */
982 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
984 (void) Create(data
, type
, width
, height
, depth
);
987 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
989 LoadFile(filename
, type
);
992 wxBitmap::wxBitmap(CGImageRef image
)
994 (void) Create(image
);
997 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
999 return new wxBitmapRefData
;
1002 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1004 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
1007 void * wxBitmap::GetRawAccess() const
1009 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1011 return M_BITMAPDATA
->GetRawAccess() ;
1014 void * wxBitmap::BeginRawAccess()
1016 wxCHECK_MSG( IsOk() , NULL
, wxT("invalid bitmap") ) ;
1018 return M_BITMAPDATA
->BeginRawAccess() ;
1021 void wxBitmap::EndRawAccess()
1023 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
1025 M_BITMAPDATA
->EndRawAccess() ;
1028 CGImageRef
wxBitmap::CreateCGImage() const
1030 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1032 return M_BITMAPDATA
->CreateCGImage() ;
1035 #ifndef __WXOSX_IPHONE__
1036 IconRef
wxBitmap::GetIconRef() const
1038 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
1040 return M_BITMAPDATA
->GetIconRef() ;
1043 IconRef
wxBitmap::CreateIconRef() const
1045 IconRef icon
= GetIconRef();
1046 verify_noerr( AcquireIconRef(icon
) );
1053 WX_NSImage
wxBitmap::GetNSImage() const
1055 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1056 return wxOSXGetNSImageFromCGImage( cgimage
);
1061 #if wxOSX_USE_IPHONE
1063 WX_UIImage
wxBitmap::GetUIImage() const
1065 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1066 return wxOSXGetUIImageFromCGImage( cgimage
);
1070 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1072 wxCHECK_MSG( IsOk() &&
1073 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1074 (rect
.x
+rect
.width
<= GetWidth()) &&
1075 (rect
.y
+rect
.height
<= GetHeight()),
1076 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1078 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1079 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1081 int destwidth
= rect
.width
;
1082 int destheight
= rect
.height
;
1085 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1086 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1087 wxASSERT((sourcedata
!= NULL
) && (destdata
!= NULL
));
1089 if ( (sourcedata
!= NULL
) && (destdata
!= NULL
) )
1091 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1092 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1093 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1094 unsigned char *dest
= destdata
;
1096 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1098 memcpy( dest
, source
, destlinesize
) ;
1101 ret
.EndRawAccess() ;
1105 if ( M_BITMAPDATA
->m_bitmapMask
)
1107 wxMemoryBuffer maskbuf
;
1108 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1109 size_t maskbufsize
= rowBytes
* destheight
;
1111 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1112 int destlinesize
= rowBytes
;
1114 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1115 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1116 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1118 if ( (source
!= NULL
) && (destdata
!= NULL
) )
1120 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1121 unsigned char *dest
= destdata
;
1123 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1125 memcpy( dest
, source
, destlinesize
) ;
1128 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1130 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1132 else if ( HasAlpha() )
1138 bool wxBitmap::Create(int w
, int h
, int d
)
1143 d
= wxDisplayDepth() ;
1145 m_refData
= new wxBitmapRefData( w
, h
, d
);
1147 return M_BITMAPDATA
->IsOk() ;
1151 bool wxBitmap::Create(CGImageRef image
)
1155 m_refData
= new wxBitmapRefData( image
);
1157 return M_BITMAPDATA
->IsOk() ;
1160 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1164 wxBitmapHandler
*handler
= FindHandler(type
);
1168 m_refData
= new wxBitmapRefData
;
1170 return handler
->LoadFile(this, filename
, type
, -1, -1);
1175 wxImage
loadimage(filename
, type
);
1176 if (loadimage
.IsOk())
1185 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1190 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1194 m_refData
= new wxBitmapRefData
;
1196 wxBitmapHandler
*handler
= FindHandler(type
);
1198 if ( handler
== NULL
)
1200 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1205 return handler
->Create(this, data
, type
, width
, height
, depth
);
1210 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1212 wxCHECK_RET( image
.IsOk(), wxT("invalid image") );
1214 // width and height of the device-dependent bitmap
1215 int width
= image
.GetWidth();
1216 int height
= image
.GetHeight();
1218 wxBitmapRefData
* bitmapRefData
;
1220 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1222 if ( bitmapRefData
->IsOk())
1226 bool hasAlpha
= false ;
1228 if ( image
.HasMask() )
1230 // takes precedence, don't mix with alpha info
1234 hasAlpha
= image
.HasAlpha() ;
1240 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1241 register unsigned char* data
= image
.GetData();
1242 if ( destinationstart
!= NULL
&& data
!= NULL
)
1244 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1245 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1247 unsigned char * destination
= destinationstart
;
1248 for (int x
= 0; x
< width
; x
++)
1252 const unsigned char a
= *alpha
++;
1253 *destination
++ = a
;
1255 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1256 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1257 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1258 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1260 *destination
++ = *data
++ ;
1261 *destination
++ = *data
++ ;
1262 *destination
++ = *data
++ ;
1267 *destination
++ = 0xFF ;
1268 *destination
++ = *data
++ ;
1269 *destination
++ = *data
++ ;
1270 *destination
++ = *data
++ ;
1277 if ( image
.HasMask() )
1278 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1279 } /* bitmapRefData->IsOk() */
1282 wxImage
wxBitmap::ConvertToImage() const
1286 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
1288 // create an wxImage object
1289 int width
= GetWidth();
1290 int height
= GetHeight();
1291 image
.Create( width
, height
);
1293 unsigned char *data
= image
.GetData();
1294 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1296 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1298 bool hasAlpha
= false ;
1299 bool hasMask
= false ;
1300 int maskBytesPerRow
= 0 ;
1301 unsigned char *alpha
= NULL
;
1302 unsigned char *mask
= NULL
;
1310 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1311 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1317 alpha
= image
.GetAlpha() ;
1322 // The following masking algorithm is the same as well in msw/gtk:
1323 // the colour used as transparent one in wxImage and the one it is
1324 // replaced with when it actually occurs in the bitmap
1325 static const int MASK_RED
= 1;
1326 static const int MASK_GREEN
= 2;
1327 static const int MASK_BLUE
= 3;
1328 static const int MASK_BLUE_REPLACEMENT
= 2;
1330 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1332 unsigned char * maskp
= mask
;
1333 unsigned char * source
= sourcestart
;
1334 unsigned char a
, r
, g
, b
;
1337 for (int xx
= 0; xx
< width
; xx
++)
1339 color
= *((long*) source
) ;
1340 #ifdef WORDS_BIGENDIAN
1341 a
= ((color
&0xFF000000) >> 24) ;
1342 r
= ((color
&0x00FF0000) >> 16) ;
1343 g
= ((color
&0x0000FF00) >> 8) ;
1344 b
= (color
&0x000000FF);
1346 b
= ((color
&0xFF000000) >> 24) ;
1347 g
= ((color
&0x00FF0000) >> 16) ;
1348 r
= ((color
&0x0000FF00) >> 8) ;
1349 a
= (color
&0x000000FF);
1353 if ( *maskp
++ == 0xFF )
1359 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1360 b
= MASK_BLUE_REPLACEMENT
;
1362 else if ( hasAlpha
)
1365 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1366 // this must be non-premultiplied data
1367 if ( a
!= 0xFF && a
!= 0 )
1377 data
[index
+ 1] = g
;
1378 data
[index
+ 2] = b
;
1386 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1391 #endif //wxUSE_IMAGE
1393 bool wxBitmap::SaveFile( const wxString
& filename
,
1394 wxBitmapType type
, const wxPalette
*palette
) const
1396 bool success
= false;
1397 wxBitmapHandler
*handler
= FindHandler(type
);
1401 success
= handler
->SaveFile(this, filename
, type
, palette
);
1406 wxImage image
= ConvertToImage();
1407 success
= image
.SaveFile(filename
, type
);
1409 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1416 int wxBitmap::GetHeight() const
1418 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1420 return M_BITMAPDATA
->GetHeight();
1423 int wxBitmap::GetWidth() const
1425 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1427 return M_BITMAPDATA
->GetWidth() ;
1430 int wxBitmap::GetDepth() const
1432 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1434 return M_BITMAPDATA
->GetDepth();
1437 wxMask
*wxBitmap::GetMask() const
1439 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1441 return M_BITMAPDATA
->m_bitmapMask
;
1444 bool wxBitmap::HasAlpha() const
1446 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1448 return M_BITMAPDATA
->HasAlpha() ;
1451 void wxBitmap::SetWidth(int w
)
1454 M_BITMAPDATA
->SetWidth(w
);
1457 void wxBitmap::SetHeight(int h
)
1460 M_BITMAPDATA
->SetHeight(h
);
1463 void wxBitmap::SetDepth(int d
)
1466 M_BITMAPDATA
->SetDepth(d
);
1469 void wxBitmap::SetOk(bool isOk
)
1472 M_BITMAPDATA
->SetOk(isOk
);
1476 wxPalette
*wxBitmap::GetPalette() const
1478 wxCHECK_MSG( IsOk(), NULL
, wxT("Invalid bitmap GetPalette()") );
1480 return &M_BITMAPDATA
->m_bitmapPalette
;
1483 void wxBitmap::SetPalette(const wxPalette
& palette
)
1486 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1488 #endif // wxUSE_PALETTE
1490 void wxBitmap::SetMask(wxMask
*mask
)
1493 // Remove existing mask if there is one.
1494 delete M_BITMAPDATA
->m_bitmapMask
;
1496 M_BITMAPDATA
->m_bitmapMask
= mask
;
1499 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1503 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1506 // ----------------------------------------------------------------------------
1508 // ----------------------------------------------------------------------------
1515 wxMask::wxMask(const wxMask
&tocopy
) : wxObject()
1519 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1520 m_width
= tocopy
.m_width
;
1521 m_height
= tocopy
.m_height
;
1523 size_t size
= m_bytesPerRow
* m_height
;
1524 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1525 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1526 memcpy( dest
, source
, size
);
1527 m_memBuf
.UngetWriteBuf( size
) ;
1531 // Construct a mask from a bitmap and a colour indicating
1532 // the transparent area
1533 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1536 Create( bitmap
, colour
);
1539 // Construct a mask from a mono bitmap (copies the bitmap).
1540 wxMask::wxMask( const wxBitmap
& bitmap
)
1546 // Construct a mask from a mono bitmap (copies the bitmap).
1548 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1551 Create( data
, width
, height
, bytesPerRow
);
1558 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1559 m_maskBitmap
= NULL
;
1565 m_width
= m_height
= m_bytesPerRow
= 0 ;
1566 m_maskBitmap
= NULL
;
1569 void *wxMask::GetRawAccess() const
1571 return m_memBuf
.GetData() ;
1574 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1575 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1577 void wxMask::RealizeNative()
1581 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1582 m_maskBitmap
= NULL
;
1585 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1586 // from MouseTracking sample :
1587 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1588 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1590 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1591 kCGImageAlphaNone
);
1592 CGColorSpaceRelease( colorspace
);
1593 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1596 // Create a mask from a mono bitmap (copies the bitmap).
1598 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1603 m_bytesPerRow
= bytesPerRow
;
1605 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1612 // Create a mask from a mono bitmap (copies the bitmap).
1613 bool wxMask::Create(const wxBitmap
& bitmap
)
1615 m_width
= bitmap
.GetWidth() ;
1616 m_height
= bitmap
.GetHeight() ;
1617 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1619 size_t size
= m_bytesPerRow
* m_height
;
1620 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1621 wxASSERT( destdatabase
!= NULL
) ;
1625 memset( destdatabase
, 0 , size
) ;
1626 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1628 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1630 unsigned char *destdata
= destdatabase
;
1631 unsigned char r
, g
, b
;
1633 for ( int x
= 0 ; x
< m_width
; ++x
)
1640 if ( ( r
+ g
+ b
) > 0x10 )
1641 *destdata
++ = 0xFF ;
1643 *destdata
++ = 0x00 ;
1648 m_memBuf
.UngetWriteBuf( size
) ;
1654 // Create a mask from a bitmap and a colour indicating
1655 // the transparent area
1656 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1658 m_width
= bitmap
.GetWidth() ;
1659 m_height
= bitmap
.GetHeight() ;
1660 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1662 size_t size
= m_bytesPerRow
* m_height
;
1663 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1664 wxASSERT( destdatabase
!= NULL
) ;
1665 if ( destdatabase
!= NULL
)
1667 memset( destdatabase
, 0 , size
) ;
1668 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1669 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1671 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1673 unsigned char *srcdata
= srcdatabase
;
1674 unsigned char *destdata
= destdatabase
;
1675 unsigned char r
, g
, b
;
1677 for ( int x
= 0 ; x
< m_width
; ++x
)
1684 if ( colour
== wxColour( r
, g
, b
) )
1685 *destdata
++ = 0xFF ;
1687 *destdata
++ = 0x00 ;
1691 m_memBuf
.UngetWriteBuf( size
) ;
1697 WXHBITMAP
wxMask::GetHBITMAP() const
1699 return m_maskBitmap
;
1702 // ----------------------------------------------------------------------------
1703 // Standard Handlers
1704 // ----------------------------------------------------------------------------
1706 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1708 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler
)
1711 inline wxBundleResourceHandler()
1715 virtual bool LoadFile(wxBitmap
*bitmap
,
1716 const wxString
& name
,
1722 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1724 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1726 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1729 inline wxPNGResourceHandler()
1731 SetName(wxT("PNG resource"));
1732 SetExtension("PNG");
1733 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1737 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1739 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1741 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1744 inline wxJPEGResourceHandler()
1746 SetName(wxT("JPEG resource"));
1747 SetExtension("JPEG");
1748 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1752 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1754 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1755 const wxString
& name
,
1756 wxBitmapType
WXUNUSED(type
),
1757 int WXUNUSED(desiredWidth
),
1758 int WXUNUSED(desiredHeight
))
1760 wxString ext
= GetExtension().Lower();
1761 wxCFStringRef
resname(name
);
1762 wxCFStringRef
restype(ext
);
1764 wxCFRef
<CFURLRef
> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1766 if ( imageURL
.get() != NULL
)
1768 // Create the data provider object
1769 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1770 CGImageRef image
= NULL
;
1772 if ( ext
== "jpeg" )
1773 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1774 kCGRenderingIntentDefault
);
1775 else if ( ext
== "png" )
1776 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1777 kCGRenderingIntentDefault
);
1778 if ( image
!= NULL
)
1780 bitmap
->Create(image
);
1781 CGImageRelease(image
);
1789 wxBitmap
wxBitmapHelpers::NewFromPNGData(const void* data
, size_t size
)
1791 wxCFRef
<CGDataProviderRef
>
1792 provider(CGDataProviderCreateWithData(NULL
, data
, size
, NULL
) );
1794 image(CGImageCreateWithPNGDataProvider(provider
, NULL
, true,
1795 kCGRenderingIntentDefault
));
1797 return wxBitmap(image
);
1800 void wxBitmap::InitStandardHandlers()
1802 #if wxOSX_USE_COCOA_OR_CARBON
1803 AddHandler( new wxICONResourceHandler
) ;
1805 AddHandler( new wxPNGResourceHandler
);
1806 AddHandler( new wxJPEGResourceHandler
);
1809 // ----------------------------------------------------------------------------
1810 // raw bitmap access support
1811 // ----------------------------------------------------------------------------
1813 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1816 // no bitmap, no data (raw or otherwise)
1819 data
.m_width
= GetWidth() ;
1820 data
.m_height
= GetHeight() ;
1821 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1823 return BeginRawAccess() ;
1826 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1831 void wxBitmap::UseAlpha()
1833 // remember that we are using alpha channel:
1834 // we'll need to create a proper mask in UngetRawData()
1835 M_BITMAPDATA
->UseAlpha( true );