1 /////////////////////////////////////////////////////////////////////////////
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/metafile.h"
19 #include "wx/xpmdecod.h"
21 #include "wx/rawbmp.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
24 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
25 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
28 #include <ApplicationServices/ApplicationServices.h>
30 #include <PictUtils.h>
33 #include "wx/mac/uma.h"
35 #include "wx/dcmemory.h"
37 // Implementation Notes
38 // --------------------
40 // we are always working with a 32 bit deep pixel buffer
41 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
42 // therefore we have a separate GWorld there for blitting the mask in
44 // under Quartz then content is transformed into a CGImageRef representing the same data
45 // which can be transferred to the GPU by the OS for fast rendering
47 // we don't dare premultiplied alpha yet
48 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
52 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
54 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
57 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
61 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
65 wxBitmapRefData
* bmp
= bmap
;
67 if ( !bmap
->HasNativeSize() )
69 // as PICT conversion will only result in a 16x16 icon, let's attempt
70 // a few scales for better results
72 int w
= bitmap
.GetWidth() ;
73 int h
= bitmap
.GetHeight() ;
74 int sz
= wxMax( w
, h
) ;
75 if ( sz
== 24 || sz
== 64)
77 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
78 bmp
= scaleBmp
.GetBitmapData() ;
82 info
->contentType
= kControlContentIconRef
;
83 info
->u
.iconRef
= bmp
->GetIconRef() ;
84 AcquireIconRef( info
->u
.iconRef
) ;
86 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
87 else if ( forceType
== kControlContentCGImageRef
)
89 info
->contentType
= kControlContentCGImageRef
;
90 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
95 info
->contentType
= kControlContentPictHandle
;
96 info
->u
.picture
= bmap
->GetPictHandle() ;
101 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
103 if ( info
->contentType
== kControlContentIconRef
)
105 ReleaseIconRef( info
->u
.iconRef
) ;
107 else if ( info
->contentType
== kControlNoContent
)
109 // there's no bitmap at all, fall through silently
111 else if ( info
->contentType
== kControlContentPictHandle
)
113 // owned by the bitmap, no release here
115 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
116 else if ( info
->contentType
== kControlContentCGImageRef
)
118 CGImageRelease( info
->u
.imageRef
) ;
123 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
127 #endif //wxUSE_BMPBUTTON
129 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
131 void wxBitmapRefData::Init()
137 m_bitmapMask
= NULL
;
138 #if wxMAC_USE_CORE_GRAPHICS
139 m_cgImageRef
= NULL
;
142 m_pictHandle
= NULL
;
144 m_hMaskBitmap
= NULL
;
145 m_maskBytesPerRow
= 0 ;
147 m_rawAccessCount
= 0 ;
151 wxBitmapRefData::wxBitmapRefData()
156 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
159 Create( w
, h
, d
) ;
162 bool wxBitmapRefData::Create( int w
, int h
, int d
)
168 m_bytesPerRow
= w
* 4 ;
169 size_t size
= m_bytesPerRow
* h
;
170 void* data
= m_memBuf
.GetWriteBuf(size
) ;
171 memset( data
, 0 , size
) ;
172 m_memBuf
.UngetWriteBuf(size
) ;
175 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
177 (char*) data
, m_bytesPerRow
) ) ;
178 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
179 m_ok
= ( m_hBitmap
!= NULL
) ;
184 void wxBitmapRefData::UseAlpha( bool use
)
186 if ( m_hasAlpha
== use
)
192 int width
= GetWidth() ;
193 int height
= GetHeight() ;
194 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
195 size_t size
= height
* m_maskBytesPerRow
;
196 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
197 memset( data
, 0 , size
) ;
198 wxASSERT( m_hMaskBitmap
== NULL
) ;
199 Rect rect
= { 0 , 0 , height
, width
} ;
200 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
201 (char*) data
, m_maskBytesPerRow
) ) ;
202 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
203 m_maskMemBuf
.UngetWriteBuf(size
) ;
204 #if !wxMAC_USE_CORE_GRAPHICS
210 DisposeGWorld( m_hMaskBitmap
) ;
211 m_hMaskBitmap
= NULL
;
212 m_maskBytesPerRow
= 0 ;
216 void *wxBitmapRefData::GetRawAccess() const
218 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
219 return m_memBuf
.GetData() ;
222 void *wxBitmapRefData::BeginRawAccess()
224 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
225 wxASSERT( m_rawAccessCount
== 0 ) ;
227 // we must destroy an existing cached image, as
228 // the bitmap data may change now
229 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
230 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
231 #if wxMAC_USE_CORE_GRAPHICS
234 CGImageRelease( m_cgImageRef
) ;
235 m_cgImageRef
= NULL
;
238 return m_memBuf
.GetData() ;
241 void wxBitmapRefData::EndRawAccess()
243 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
244 wxASSERT( m_rawAccessCount
== 1 ) ;
246 #if !wxMAC_USE_CORE_GRAPHICS
251 bool wxBitmapRefData::HasNativeSize()
254 int h
= GetHeight() ;
255 int sz
= wxMax( w
, h
) ;
257 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
262 IconRef
wxBitmapRefData::GetIconRef()
264 if ( m_iconRef
== NULL
)
266 // Create Icon Family Handle
268 IconFamilyHandle iconFamily
= NULL
;
270 #ifdef WORDS_BIGENDIAN
271 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
272 (**iconFamily
).resourceType
= kIconFamilyType
;
273 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
275 // test this solution on big endian as well
276 iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
280 int h
= GetHeight() ;
281 int sz
= wxMax( w
, h
) ;
283 OSType dataType
= 0 ;
284 OSType maskType
= 0 ;
288 dataType
= kThumbnail32BitData
;
289 maskType
= kThumbnail8BitMask
;
293 dataType
= kHuge32BitData
;
294 maskType
= kHuge8BitMask
;
298 dataType
= kLarge32BitData
;
299 maskType
= kLarge8BitMask
;
303 dataType
= kSmall32BitData
;
304 maskType
= kSmall8BitMask
;
309 // setup the header properly
312 Handle maskdata
= NULL
;
313 unsigned char * maskptr
= NULL
;
314 unsigned char * ptr
= NULL
;
319 data
= NewHandle( size
) ;
321 ptr
= (unsigned char*) *data
;
322 memset( ptr
, 0, size
) ;
325 maskdata
= NewHandle( masksize
) ;
327 maskptr
= (unsigned char*) *maskdata
;
328 memset( maskptr
, 0 , masksize
) ;
330 bool hasAlpha
= HasAlpha() ;
331 wxMask
*mask
= m_bitmapMask
;
332 unsigned char * source
= (unsigned char*) GetRawAccess() ;
333 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
334 for ( int y
= 0 ; y
< h
; ++y
)
336 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
337 unsigned char * maskdest
= maskptr
+ y
* sz
;
338 for ( int x
= 0 ; x
< w
; ++x
)
340 unsigned char a
= *source
++ ;
341 unsigned char r
= *source
++ ;
342 unsigned char g
= *source
++ ;
343 unsigned char b
= *source
++ ;
351 *maskdest
++ = *masksource
++ ;
359 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
360 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
362 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
363 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
365 HUnlock( maskdata
) ;
366 DisposeHandle( data
) ;
367 DisposeHandle( maskdata
) ;
371 PicHandle pic
= GetPictHandle() ;
372 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
375 // transform into IconRef
377 static int iconCounter
= 2 ;
381 RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
382 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
383 // we have to retain a reference, as Unregister will decrement it
384 AcquireIconRef( m_iconRef
) ;
385 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
386 DisposeHandle( (Handle
) iconFamily
) ;
392 PicHandle
wxBitmapRefData::GetPictHandle()
394 if ( m_pictHandle
== NULL
)
396 CGrafPtr origPort
= NULL
;
397 GDHandle origDev
= NULL
;
398 GWorldPtr wp
= NULL
;
399 GWorldPtr mask
= NULL
;
400 int height
= GetHeight() ;
401 int width
= GetWidth() ;
403 Rect rect
= { 0 , 0 , height
, width
} ;
405 GetGWorld( &origPort
, &origDev
) ;
407 wp
= GetHBITMAP( &mask
) ;
409 RgnHandle clipRgn
= NULL
;
413 GWorldPtr monoworld
;
415 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
417 LockPixels( GetGWorldPixMap( monoworld
) ) ;
418 LockPixels( GetGWorldPixMap( mask
) ) ;
419 SetGWorld( monoworld
, NULL
) ;
420 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
421 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
422 RGBForeColor( &black
) ;
423 RGBBackColor( &white
) ;
424 CopyBits(GetPortBitMapForCopyBits(mask
),
425 GetPortBitMapForCopyBits(monoworld
),
429 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
430 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
431 UnlockPixels( GetGWorldPixMap( mask
) ) ;
432 DisposeGWorld( monoworld
) ;
435 SetGWorld( wp
, NULL
) ;
437 GetPortBounds( wp
, &portRect
) ;
438 m_pictHandle
= OpenPicture(&portRect
);
442 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
443 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
444 RGBForeColor( &black
) ;
445 RGBBackColor( &white
) ;
450 LockPixels( GetGWorldPixMap( wp
) ) ;
451 CopyBits(GetPortBitMapForCopyBits(wp
),
452 GetPortBitMapForCopyBits(wp
),
456 UnlockPixels( GetGWorldPixMap( wp
) ) ;
459 SetGWorld( origPort
, origDev
) ;
461 DisposeRgn( clipRgn
) ;
463 return m_pictHandle
;
467 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
469 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
470 wxASSERT( data
== membuf
->GetData() ) ;
474 CGImageRef
wxBitmapRefData::CGImageCreate() const
477 wxASSERT( m_rawAccessCount
>= 0 ) ;
479 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
481 size_t imageSize
= m_width
* m_height
* 4 ;
482 void * dataBuffer
= m_memBuf
.GetData() ;
485 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
486 wxMemoryBuffer
* membuf
= NULL
;
490 membuf
= new wxMemoryBuffer( imageSize
) ;
491 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
492 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
493 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
494 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
495 alphaInfo
= kCGImageAlphaFirst
;
496 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
498 unsigned char *sourcemask
= sourcemaskstart
;
499 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
501 *destalpha
= *sourcemask
;
505 else if ( m_hasAlpha
)
507 #if wxMAC_USE_PREMULTIPLIED_ALPHA
508 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
510 alphaInfo
= kCGImageAlphaFirst
;
512 membuf
= new wxMemoryBuffer( m_memBuf
) ;
516 membuf
= new wxMemoryBuffer( m_memBuf
) ;
518 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
519 CGDataProviderRef dataProvider
=
520 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
521 wxMacMemoryBufferReleaseProc
);
523 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
524 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
525 CGDataProviderRelease( dataProvider
);
529 image
= m_cgImageRef
;
530 CGImageRetain( image
) ;
532 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
534 // we keep it for later use
535 m_cgImageRef
= image
;
536 CGImageRetain( image
) ;
542 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
544 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
549 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
550 else if ( m_hasAlpha
)
552 #if !wxMAC_USE_CORE_GRAPHICS
553 if ( m_rawAccessCount
> 0 )
556 // this structure is not kept in synch when using CG, so if someone
557 // is really accessing the Graphports, we have to sync it
560 *mask
= m_hMaskBitmap
;
566 void wxBitmapRefData::UpdateAlphaMask() const
570 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
571 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
573 int h
= GetHeight() ;
576 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
578 unsigned char* destalpha
= destalphabase
;
579 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
581 // we must have 24 bit depth for non quartz smooth alpha
583 *destalpha
++ = 255 - *sourcemask
;
584 *destalpha
++ = 255 - *sourcemask
;
585 *destalpha
++ = 255 - *sourcemask
;
591 void wxBitmapRefData::Free()
593 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
595 #if wxMAC_USE_CORE_GRAPHICS
598 CGImageRelease( m_cgImageRef
) ;
599 m_cgImageRef
= NULL
;
604 ReleaseIconRef( m_iconRef
) ;
609 KillPicture( m_pictHandle
) ;
610 m_pictHandle
= NULL
;
614 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
619 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
620 m_hMaskBitmap
= NULL
;
630 wxBitmapRefData::~wxBitmapRefData()
635 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
637 int w
= icon
.GetWidth() ;
638 int h
= icon
.GetHeight() ;
641 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
643 bool created
= false ;
645 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
647 IconFamilyHandle iconFamily
= NULL
;
648 Handle imagehandle
= NewHandle(0) ;
649 Handle maskhandle
= NewHandle(0) ;
653 IconSelectorValue selector
= 0 ;
656 dataType
= kThumbnail32BitData
;
657 maskType
= kThumbnail8BitMask
;
658 selector
= kSelectorAllAvailableData
;
662 dataType
= kHuge32BitData
;
663 maskType
= kHuge8BitMask
;
664 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
668 dataType
= kLarge32BitData
;
669 maskType
= kLarge8BitMask
;
670 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
674 dataType
= kSmall32BitData
;
675 maskType
= kSmall8BitMask
;
676 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
680 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
682 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
683 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
684 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
685 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
687 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
689 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
690 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
692 unsigned char *source
= (unsigned char *) *imagehandle
;
693 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
695 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
696 for ( int y
= 0 ; y
< h
; ++y
)
698 for ( int x
= 0 ; x
< w
; ++x
)
700 *destination
++ = *sourcemask
++ ;
702 *destination
++ = *source
++ ;
703 *destination
++ = *source
++ ;
704 *destination
++ = *source
++ ;
708 DisposeHandle( imagehandle
) ;
709 DisposeHandle( maskhandle
) ;
712 DisposeHandle( (Handle
) iconFamily
) ;
719 dc
.SelectObject( *this ) ;
720 dc
.DrawIcon( icon
, 0 , 0 ) ;
721 dc
.SelectObject( wxNullBitmap
) ;
730 wxBitmap::~wxBitmap()
734 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
736 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
740 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
741 if ( the_width
% (sizeof(unsigned char) * 8) ) {
742 linesize
+= sizeof(unsigned char);
744 unsigned char* linestart
= (unsigned char*) bits
;
745 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
746 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
748 for ( int x
= 0 ; x
< the_width
; ++x
)
752 int mask
= 1 << bit
;
753 if ( linestart
[index
] & mask
)
755 *destination
++ = 0xFF ;
762 *destination
++ = 0xFF ;
763 *destination
++ = 0xFF ;
764 *destination
++ = 0xFF ;
765 *destination
++ = 0xFF ;
773 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
777 wxBitmap::wxBitmap(int w
, int h
, int d
)
779 (void)Create(w
, h
, d
);
782 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
784 (void) Create(data
, type
, width
, height
, depth
);
787 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
789 LoadFile(filename
, type
);
792 wxBitmap::wxBitmap(const char **bits
)
794 (void) CreateFromXpm(bits
);
797 wxBitmap::wxBitmap(char **bits
)
799 (void) CreateFromXpm((const char **)bits
);
802 void* wxBitmap::GetRawAccess() const
804 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
805 return M_BITMAPDATA
->GetRawAccess() ;
808 void* wxBitmap::BeginRawAccess()
810 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
811 return M_BITMAPDATA
->BeginRawAccess() ;
814 void wxBitmap::EndRawAccess()
816 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
817 M_BITMAPDATA
->EndRawAccess() ;
820 bool wxBitmap::CreateFromXpm(const char **bits
)
823 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
824 wxXPMDecoder decoder
;
825 wxImage img
= decoder
.ReadData(bits
);
826 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
827 *this = wxBitmap(img
);
835 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
837 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
838 return M_BITMAPDATA
->CGImageCreate() ;
842 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
845 (rect
.x
>= 0) && (rect
.y
>= 0) &&
846 (rect
.x
+rect
.width
<= GetWidth()) &&
847 (rect
.y
+rect
.height
<= GetHeight()),
848 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
851 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
852 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
855 int sourcewidth
= GetWidth() ;
856 int destwidth
= rect
.width
;
857 int destheight
= rect
.height
;
859 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
860 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
861 int sourcelinesize
= sourcewidth
* 4 ;
862 int destlinesize
= destwidth
* 4 ;
863 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
864 unsigned char *dest
= destdata
;
865 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
867 memcpy( dest
, source
, destlinesize
) ;
872 if ( M_BITMAPDATA
->m_bitmapMask
)
874 wxMemoryBuffer maskbuf
;
875 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
876 size_t maskbufsize
= rowBytes
* destheight
;
877 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
879 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
880 int destlinesize
= rowBytes
;
881 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
882 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
883 unsigned char *dest
= destdata
;
885 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
887 memcpy( dest
, source
, destlinesize
) ;
889 maskbuf
.UngetWriteBuf( maskbufsize
) ;
890 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
892 else if ( HasAlpha() )
898 bool wxBitmap::Create(int w
, int h
, int d
)
903 d
= wxDisplayDepth() ;
905 m_refData
= new wxBitmapRefData( w
, h
, d
);
907 return M_BITMAPDATA
->Ok() ;
910 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
914 wxBitmapHandler
*handler
= FindHandler(type
);
918 m_refData
= new wxBitmapRefData
;
920 return handler
->LoadFile(this, filename
, type
, -1, -1);
925 wxImage
loadimage(filename
, type
);
926 if (loadimage
.Ok()) {
932 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
936 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
940 m_refData
= new wxBitmapRefData
;
942 wxBitmapHandler
*handler
= FindHandler(type
);
944 if ( handler
== NULL
) {
945 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
950 return handler
->Create(this, data
, type
, width
, height
, depth
);
955 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
957 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
959 // width and height of the device-dependent bitmap
960 int width
= image
.GetWidth();
961 int height
= image
.GetHeight();
963 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
967 bool hasAlpha
= false ;
969 if ( image
.HasMask() )
971 // takes precedence, don't mix with alpha info
975 hasAlpha
= image
.HasAlpha() ;
981 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
983 register unsigned char* data
= image
.GetData();
984 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
985 for (int y
= 0; y
< height
; y
++)
987 for (int x
= 0; x
< width
; x
++)
991 const unsigned char a
= *alpha
++;
993 #if wxMAC_USE_PREMULTIPLIED_ALPHA
994 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
995 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
996 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
998 *destination
++ = *data
++ ;
999 *destination
++ = *data
++ ;
1000 *destination
++ = *data
++ ;
1005 *destination
++ = 0xFF ;
1006 *destination
++ = *data
++ ;
1007 *destination
++ = *data
++ ;
1008 *destination
++ = *data
++ ;
1013 if ( image
.HasMask() )
1015 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1019 wxImage
wxBitmap::ConvertToImage() const
1023 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1025 // create an wxImage object
1026 int width
= GetWidth();
1027 int height
= GetHeight();
1028 image
.Create( width
, height
);
1030 unsigned char *data
= image
.GetData();
1031 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1033 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1035 bool hasAlpha
= false ;
1036 bool hasMask
= false ;
1037 int maskBytesPerRow
= 0 ;
1038 unsigned char *alpha
= NULL
;
1039 unsigned char *mask
= NULL
;
1048 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1049 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1055 alpha
= image
.GetAlpha() ;
1059 // The following masking algorithm is the same as well in msw/gtk:
1060 // the colour used as transparent one in wxImage and the one it is
1061 // replaced with when it really occurs in the bitmap
1062 static const int MASK_RED
= 1;
1063 static const int MASK_GREEN
= 2;
1064 static const int MASK_BLUE
= 3;
1065 static const int MASK_BLUE_REPLACEMENT
= 2;
1067 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1069 unsigned char * maskp
= mask
;
1070 for (int xx
= 0; xx
< width
; xx
++)
1072 long color
= *((long*) source
) ;
1073 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1074 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1075 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1076 unsigned char b
= (color
&0x000000FF);
1079 if ( *maskp
++ == 0 )
1085 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1086 b
= MASK_BLUE_REPLACEMENT
;
1088 else if ( hasAlpha
)
1092 data
[index
+ 1] = g
;
1093 data
[index
+ 2] = b
;
1099 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1103 #endif //wxUSE_IMAGE
1105 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1106 const wxPalette
*palette
) const
1108 bool success
= false;
1109 wxBitmapHandler
*handler
= FindHandler(type
);
1113 success
= handler
->SaveFile(this, filename
, type
, palette
);
1118 wxImage image
= ConvertToImage();
1119 success
= image
.SaveFile(filename
, type
);
1121 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1128 bool wxBitmap::Ok() const
1130 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1133 int wxBitmap::GetHeight() const
1135 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1137 return M_BITMAPDATA
->GetHeight();
1140 int wxBitmap::GetWidth() const
1142 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1144 return M_BITMAPDATA
->GetWidth() ;
1147 int wxBitmap::GetDepth() const
1149 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1151 return M_BITMAPDATA
->GetDepth();
1154 #if WXWIN_COMPATIBILITY_2_4
1156 int wxBitmap::GetQuality() const
1163 wxMask
*wxBitmap::GetMask() const
1165 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1167 return M_BITMAPDATA
->m_bitmapMask
;
1170 bool wxBitmap::HasAlpha() const
1172 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1174 return M_BITMAPDATA
->HasAlpha() ;
1177 void wxBitmap::SetWidth(int w
)
1180 m_refData
= new wxBitmapRefData
;
1182 M_BITMAPDATA
->SetWidth(w
);
1185 void wxBitmap::SetHeight(int h
)
1188 m_refData
= new wxBitmapRefData
;
1190 M_BITMAPDATA
->SetHeight(h
);
1193 void wxBitmap::SetDepth(int d
)
1196 m_refData
= new wxBitmapRefData
;
1198 M_BITMAPDATA
->SetDepth(d
);
1201 #if WXWIN_COMPATIBILITY_2_4
1203 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1209 void wxBitmap::SetOk(bool isOk
)
1212 m_refData
= new wxBitmapRefData
;
1214 M_BITMAPDATA
->SetOk(isOk
);
1218 wxPalette
*wxBitmap::GetPalette() const
1220 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1222 return &M_BITMAPDATA
->m_bitmapPalette
;
1225 void wxBitmap::SetPalette(const wxPalette
& palette
)
1228 m_refData
= new wxBitmapRefData
;
1230 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1232 #endif // wxUSE_PALETTE
1234 void wxBitmap::SetMask(wxMask
*mask
)
1237 m_refData
= new wxBitmapRefData
;
1239 // Remove existing mask if there is one.
1240 delete M_BITMAPDATA
->m_bitmapMask
;
1242 M_BITMAPDATA
->m_bitmapMask
= mask
;
1245 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1247 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1250 // ----------------------------------------------------------------------------
1252 // ----------------------------------------------------------------------------
1259 // Construct a mask from a bitmap and a colour indicating
1260 // the transparent area
1261 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1264 Create(bitmap
, colour
);
1267 // Construct a mask from a mono bitmap (copies the bitmap).
1268 wxMask::wxMask(const wxBitmap
& bitmap
)
1274 // Construct a mask from a mono bitmap (copies the bitmap).
1275 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1278 Create(data
, width
, height
, bytesPerRow
);
1285 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1286 m_maskBitmap
= NULL
;
1292 m_width
= m_height
= m_bytesPerRow
= 0 ;
1293 m_maskBitmap
= NULL
;
1296 void *wxMask::GetRawAccess() const
1298 return m_memBuf
.GetData() ;
1301 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1302 // so no rainbox colors will be created by QD when blitting
1304 void wxMask::RealizeNative()
1308 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1309 m_maskBitmap
= NULL
;
1311 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1312 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1313 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1316 // Create a mask from a mono bitmap (copies the bitmap).
1317 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1322 m_bytesPerRow
= bytesPerRow
;
1323 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1328 // Create a mask from a mono bitmap (copies the bitmap).
1329 bool wxMask::Create(const wxBitmap
& bitmap
)
1331 m_width
= bitmap
.GetWidth() ;
1332 m_height
= bitmap
.GetHeight() ;
1333 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1334 size_t size
= m_bytesPerRow
* m_height
;
1335 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1336 memset( destdatabase
, 0 , size
) ;
1337 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1338 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1340 unsigned char *destdata
= destdatabase
;
1341 for( int x
= 0 ; x
< m_width
; ++x
)
1344 unsigned char r
= *srcdata
++ ;
1345 unsigned char g
= *srcdata
++ ;
1346 unsigned char b
= *srcdata
++ ;
1347 if ( ( r
+ g
+ b
) > 0x10 )
1348 *destdata
++ = 0x00 ;
1350 *destdata
++ = 0xFF ;
1353 m_memBuf
.UngetWriteBuf( size
) ;
1358 // Create a mask from a bitmap and a colour indicating
1359 // the transparent area
1360 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1362 m_width
= bitmap
.GetWidth() ;
1363 m_height
= bitmap
.GetHeight() ;
1364 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1365 size_t size
= m_bytesPerRow
* m_height
;
1367 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1368 memset( destdatabase
, 0 , size
) ;
1369 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1370 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1372 unsigned char *destdata
= destdatabase
;
1373 for( int x
= 0 ; x
< m_width
; ++x
)
1376 unsigned char r
= *srcdata
++ ;
1377 unsigned char g
= *srcdata
++ ;
1378 unsigned char b
= *srcdata
++ ;
1379 if ( colour
== wxColour( r
, g
, b
) )
1380 *destdata
++ = 0x00 ;
1382 *destdata
++ = 0xFF ;
1385 m_memBuf
.UngetWriteBuf( size
) ;
1390 WXHBITMAP
wxMask::GetHBITMAP() const
1392 return m_maskBitmap
;
1395 // ----------------------------------------------------------------------------
1397 // ----------------------------------------------------------------------------
1399 wxBitmapHandler::~wxBitmapHandler()
1403 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1408 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1409 int desiredWidth
, int desiredHeight
)
1414 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1419 // ----------------------------------------------------------------------------
1420 // Standard Handlers
1421 // ----------------------------------------------------------------------------
1423 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1425 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1427 inline wxPICTResourceHandler()
1429 SetName(wxT("Macintosh Pict resource"));
1430 SetExtension(wxEmptyString
);
1431 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1434 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1435 int desiredWidth
, int desiredHeight
);
1437 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1440 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1441 int desiredWidth
, int desiredHeight
)
1445 wxMacStringToPascal( name
, theName
) ;
1447 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1451 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1452 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1454 dc
.SelectObject( *bitmap
) ;
1456 dc
.SelectObject( wxNullBitmap
) ;
1459 #endif //wxUSE_METAFILE
1464 void wxBitmap::InitStandardHandlers()
1466 AddHandler(new wxPICTResourceHandler
) ;
1467 AddHandler(new wxICONResourceHandler
) ;
1470 // ----------------------------------------------------------------------------
1471 // raw bitmap access support
1472 // ----------------------------------------------------------------------------
1474 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1478 // no bitmap, no data (raw or otherwise)
1482 data
.m_width
= GetWidth() ;
1483 data
.m_height
= GetHeight() ;
1484 data
.m_stride
= GetWidth() * 4 ;
1485 return GetRawAccess() ;
1488 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1493 // TODO : if we have some information about the API we should check
1494 // this code looks strange...
1496 if ( M_BITMAPDATA
->HasAlpha() )
1498 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1500 int w
= data
.GetWidth(),
1501 h
= data
.GetHeight();
1503 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1504 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1505 wxAlphaPixelData::Iterator
pMask(dataMask
),
1507 for ( int y
= 0; y
< h
; y
++ )
1509 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1512 for ( int x
= 0; x
< w
; x
++ )
1514 const wxAlphaPixelData::Iterator::ChannelType
1517 pMask
.Red() = alpha
;
1518 pMask
.Green() = alpha
;
1519 pMask
.Blue() = alpha
;
1528 pMask
= rowStartMask
;
1529 pMask
.OffsetY(dataMask
, 1);
1532 SetMask(new wxMask(bmpMask
));
1536 void wxBitmap::UseAlpha()
1538 // remember that we are using alpha channel, we'll need to create a proper
1539 // mask in UngetRawData()
1540 M_BITMAPDATA
->UseAlpha( true );