1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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
)
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
33 #include <ApplicationServices/ApplicationServices.h>
35 #include <PictUtils.h>
38 #include "wx/mac/uma.h"
40 // Implementation Notes
41 // --------------------
43 // we are always working with a 32 bit deep pixel buffer
44 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
45 // therefore we have a separate GWorld there for blitting the mask in
47 // under Quartz then content is transformed into a CGImageRef representing the same data
48 // which can be transferred to the GPU by the OS for fast rendering
50 // we don't dare use premultiplied alpha yet
51 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
55 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
57 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
60 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
64 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
67 wxBitmapRefData
* bmp
= bmap
;
69 if ( !bmap
->HasNativeSize() )
71 // as PICT conversion will only result in a 16x16 icon, let's attempt
72 // a few scales for better results
74 int w
= bitmap
.GetWidth() ;
75 int h
= bitmap
.GetHeight() ;
76 int sz
= wxMax( w
, h
) ;
77 if ( sz
== 24 || sz
== 64 )
79 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
80 bmp
= scaleBmp
.GetBitmapData() ;
84 info
->contentType
= kControlContentIconRef
;
85 info
->u
.iconRef
= bmp
->GetIconRef() ;
86 AcquireIconRef( info
->u
.iconRef
) ;
88 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
89 else if ( forceType
== kControlContentCGImageRef
)
91 info
->contentType
= kControlContentCGImageRef
;
92 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
97 info
->contentType
= kControlContentPictHandle
;
98 info
->u
.picture
= bmap
->GetPictHandle() ;
103 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
105 if ( info
->contentType
== kControlContentIconRef
)
107 ReleaseIconRef( info
->u
.iconRef
) ;
109 else if ( info
->contentType
== kControlNoContent
)
111 // there's no bitmap at all, fall through silently
113 else if ( info
->contentType
== kControlContentPictHandle
)
115 // owned by the bitmap, no release here
117 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
118 else if ( info
->contentType
== kControlContentCGImageRef
)
120 CGImageRelease( info
->u
.imageRef
) ;
125 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
129 #endif //wxUSE_BMPBUTTON
131 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
133 void wxBitmapRefData::Init()
139 m_bitmapMask
= NULL
;
142 m_cgImageRef
= NULL
;
146 m_pictHandle
= NULL
;
148 m_hMaskBitmap
= NULL
;
149 m_maskBytesPerRow
= 0 ;
151 m_rawAccessCount
= 0 ;
155 wxBitmapRefData::wxBitmapRefData()
160 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
163 Create( w
, h
, d
) ;
166 bool wxBitmapRefData::Create( int w
, int h
, int d
)
168 m_width
= wxMax(1, w
);
169 m_height
= wxMax(1, h
);
172 m_bytesPerRow
= w
* 4 ;
173 size_t size
= m_bytesPerRow
* h
;
174 void* data
= m_memBuf
.GetWriteBuf( size
) ;
175 memset( data
, 0 , size
) ;
176 m_memBuf
.UngetWriteBuf( size
) ;
179 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
180 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
181 (char*) data
, m_bytesPerRow
) ) ;
182 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
184 m_ok
= ( m_hBitmap
!= NULL
) ;
189 void wxBitmapRefData::UseAlpha( bool use
)
191 if ( m_hasAlpha
== use
)
197 wxASSERT( m_hMaskBitmap
== NULL
) ;
199 int width
= GetWidth() ;
200 int height
= GetHeight() ;
201 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
202 size_t size
= height
* m_maskBytesPerRow
;
203 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
204 wxASSERT( data
!= NULL
) ;
206 memset( data
, 0 , size
) ;
207 Rect rect
= { 0 , 0 , height
, width
} ;
208 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
209 (char*) data
, m_maskBytesPerRow
) ) ;
210 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
211 m_maskMemBuf
.UngetWriteBuf(size
) ;
213 #if !wxMAC_USE_CORE_GRAPHICS
219 DisposeGWorld( m_hMaskBitmap
) ;
220 m_hMaskBitmap
= NULL
;
221 m_maskBytesPerRow
= 0 ;
225 void *wxBitmapRefData::GetRawAccess() const
227 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
228 return m_memBuf
.GetData() ;
231 void *wxBitmapRefData::BeginRawAccess()
233 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
234 wxASSERT( m_rawAccessCount
== 0 ) ;
235 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
236 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
241 // we must destroy an existing cached image, as
242 // the bitmap data may change now
245 CGImageRelease( m_cgImageRef
) ;
246 m_cgImageRef
= NULL
;
250 return m_memBuf
.GetData() ;
253 void wxBitmapRefData::EndRawAccess()
255 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
256 wxASSERT( m_rawAccessCount
== 1 ) ;
260 #if !wxMAC_USE_CORE_GRAPHICS
265 bool wxBitmapRefData::HasNativeSize()
268 int h
= GetHeight() ;
269 int sz
= wxMax( w
, h
) ;
271 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
274 IconRef
wxBitmapRefData::GetIconRef()
276 if ( m_iconRef
== NULL
)
278 // Create Icon Family Handle
280 IconFamilyHandle iconFamily
= NULL
;
282 #ifdef WORDS_BIGENDIAN
283 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
284 (**iconFamily
).resourceType
= kIconFamilyType
;
285 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
287 // test this solution on big endian as well
288 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
292 int h
= GetHeight() ;
293 int sz
= wxMax( w
, h
) ;
295 OSType dataType
= 0 ;
296 OSType maskType
= 0 ;
301 dataType
= kThumbnail32BitData
;
302 maskType
= kThumbnail8BitMask
;
306 dataType
= kHuge32BitData
;
307 maskType
= kHuge8BitMask
;
311 dataType
= kLarge32BitData
;
312 maskType
= kLarge8BitMask
;
316 dataType
= kSmall32BitData
;
317 maskType
= kSmall8BitMask
;
326 // setup the header properly
329 Handle maskdata
= NULL
;
330 unsigned char * maskptr
= NULL
;
331 unsigned char * ptr
= NULL
;
332 size_t datasize
, masksize
;
334 datasize
= sz
* sz
* 4 ;
335 data
= NewHandle( datasize
) ;
337 ptr
= (unsigned char*) *data
;
338 memset( ptr
, 0, datasize
) ;
341 maskdata
= NewHandle( masksize
) ;
343 maskptr
= (unsigned char*) *maskdata
;
344 memset( maskptr
, 0 , masksize
) ;
346 bool hasAlpha
= HasAlpha() ;
347 wxMask
*mask
= m_bitmapMask
;
348 unsigned char * source
= (unsigned char*) GetRawAccess() ;
349 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
351 for ( int y
= 0 ; y
< h
; ++y
)
353 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
354 unsigned char * maskdest
= maskptr
+ y
* sz
;
355 unsigned char a
, r
, g
, b
;
357 for ( int x
= 0 ; x
< w
; ++x
)
371 *maskdest
++ = 0xFF - *masksource
++ ;
383 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
384 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
386 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
387 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
390 HUnlock( maskdata
) ;
391 DisposeHandle( data
) ;
392 DisposeHandle( maskdata
) ;
396 PicHandle pic
= GetPictHandle() ;
397 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
399 // transform into IconRef
400 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
401 // cleaner version existing from 10.3 upwards
402 HLock((Handle
) iconFamily
);
403 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
404 HUnlock((Handle
) iconFamily
);
405 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
407 static int iconCounter
= 2 ;
409 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
410 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
412 // we have to retain a reference, as Unregister will decrement it
413 AcquireIconRef( m_iconRef
) ;
414 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
417 DisposeHandle( (Handle
) iconFamily
) ;
423 PicHandle
wxBitmapRefData::GetPictHandle()
425 if ( m_pictHandle
== NULL
)
427 CGrafPtr origPort
= NULL
;
428 GDHandle origDev
= NULL
;
429 GWorldPtr wp
= NULL
;
430 GWorldPtr mask
= NULL
;
431 int height
= GetHeight() ;
432 int width
= GetWidth() ;
434 Rect rect
= { 0 , 0 , height
, width
} ;
435 RgnHandle clipRgn
= NULL
;
437 GetGWorld( &origPort
, &origDev
) ;
438 wp
= GetHBITMAP( &mask
) ;
442 GWorldPtr monoworld
;
444 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
446 LockPixels( GetGWorldPixMap( monoworld
) ) ;
447 LockPixels( GetGWorldPixMap( mask
) ) ;
448 SetGWorld( monoworld
, NULL
) ;
450 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
451 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
452 RGBForeColor( &black
) ;
453 RGBBackColor( &white
) ;
455 CopyBits(GetPortBitMapForCopyBits(mask
),
456 GetPortBitMapForCopyBits(monoworld
),
460 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
462 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
463 UnlockPixels( GetGWorldPixMap( mask
) ) ;
464 DisposeGWorld( monoworld
) ;
467 SetGWorld( wp
, NULL
) ;
469 GetPortBounds( wp
, &portRect
) ;
470 m_pictHandle
= OpenPicture(&portRect
);
474 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
475 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
477 RGBForeColor( &black
) ;
478 RGBBackColor( &white
) ;
483 LockPixels( GetGWorldPixMap( wp
) ) ;
484 CopyBits(GetPortBitMapForCopyBits(wp
),
485 GetPortBitMapForCopyBits(wp
),
489 UnlockPixels( GetGWorldPixMap( wp
) ) ;
493 SetGWorld( origPort
, origDev
) ;
495 DisposeRgn( clipRgn
) ;
498 return m_pictHandle
;
502 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
504 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
506 wxASSERT( data
== membuf
->GetData() ) ;
511 CGImageRef
wxBitmapRefData::CGImageCreate() const
514 wxASSERT( m_rawAccessCount
>= 0 ) ;
516 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
518 size_t imageSize
= m_width
* m_height
* 4 ;
519 void * dataBuffer
= m_memBuf
.GetData() ;
522 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
523 wxMemoryBuffer
* membuf
= NULL
;
527 alphaInfo
= kCGImageAlphaFirst
;
528 membuf
= new wxMemoryBuffer( imageSize
) ;
529 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
530 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
531 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
532 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
533 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
535 unsigned char *sourcemask
= sourcemaskstart
;
536 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
538 *destalpha
= 0xFF - *sourcemask
;
546 #if wxMAC_USE_PREMULTIPLIED_ALPHA
547 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
549 alphaInfo
= kCGImageAlphaFirst
;
553 membuf
= new wxMemoryBuffer( m_memBuf
) ;
556 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
557 CGDataProviderRef dataProvider
=
558 CGDataProviderCreateWithData(
559 membuf
, (const void *)membuf
->GetData() , imageSize
,
560 wxMacMemoryBufferReleaseProc
);
563 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
564 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
565 CGDataProviderRelease( dataProvider
);
569 image
= m_cgImageRef
;
570 CGImageRetain( image
) ;
573 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
575 // we keep it for later use
576 m_cgImageRef
= image
;
577 CGImageRetain( image
) ;
584 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
586 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
592 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
594 else if ( m_hasAlpha
)
596 #if !wxMAC_USE_CORE_GRAPHICS
597 if ( m_rawAccessCount
> 0 )
600 // this structure is not kept in synch when using CG, so if something
601 // is really accessing the GrafPorts, we have to sync it
605 *mask
= m_hMaskBitmap
;
612 void wxBitmapRefData::UpdateAlphaMask() const
616 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
617 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
619 int h
= GetHeight() ;
622 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
624 unsigned char* destalpha
= destalphabase
;
626 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
628 // we must have 24 bit depth for non quartz smooth alpha
630 *destalpha
++ = 255 - *sourcemask
;
631 *destalpha
++ = 255 - *sourcemask
;
632 *destalpha
++ = 255 - *sourcemask
;
638 void wxBitmapRefData::Free()
640 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
645 CGImageRelease( m_cgImageRef
) ;
646 m_cgImageRef
= NULL
;
652 ReleaseIconRef( m_iconRef
) ;
658 KillPicture( m_pictHandle
) ;
659 m_pictHandle
= NULL
;
664 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
670 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
671 m_hMaskBitmap
= NULL
;
681 wxBitmapRefData::~wxBitmapRefData()
686 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
688 bool created
= false ;
689 int w
= icon
.GetWidth() ;
690 int h
= icon
.GetHeight() ;
692 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
694 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
696 IconFamilyHandle iconFamily
= NULL
;
697 Handle imagehandle
= NewHandle( 0 ) ;
698 Handle maskhandle
= NewHandle( 0 ) ;
702 IconSelectorValue selector
= 0 ;
707 dataType
= kThumbnail32BitData
;
708 maskType
= kThumbnail8BitMask
;
709 selector
= kSelectorAllAvailableData
;
713 dataType
= kHuge32BitData
;
714 maskType
= kHuge8BitMask
;
715 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
719 dataType
= kLarge32BitData
;
720 maskType
= kLarge8BitMask
;
721 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
725 dataType
= kSmall32BitData
;
726 maskType
= kSmall8BitMask
;
727 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
734 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
736 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
737 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
738 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
739 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
741 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
743 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
744 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
748 unsigned char *source
= (unsigned char *) *imagehandle
;
749 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
750 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
752 for ( int y
= 0 ; y
< h
; ++y
)
754 for ( int x
= 0 ; x
< w
; ++x
)
756 *destination
++ = *sourcemask
++ ;
758 *destination
++ = *source
++ ;
759 *destination
++ = *source
++ ;
760 *destination
++ = *source
++ ;
765 DisposeHandle( imagehandle
) ;
766 DisposeHandle( maskhandle
) ;
770 DisposeHandle( (Handle
) iconFamily
) ;
776 dc
.SelectObject( *this ) ;
777 dc
.DrawIcon( icon
, 0 , 0 ) ;
778 dc
.SelectObject( wxNullBitmap
) ;
788 wxBitmap::~wxBitmap()
792 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
794 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
798 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
799 if ( the_width
% (sizeof(unsigned char) * 8) )
800 linesize
+= sizeof(unsigned char);
802 unsigned char* linestart
= (unsigned char*) bits
;
803 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
805 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
807 int index
, bit
, mask
;
809 for ( int x
= 0 ; x
< the_width
; ++x
)
815 if ( !(linestart
[index
] & mask
) )
817 *destination
++ = 0xFF ;
824 *destination
++ = 0xFF ;
825 *destination
++ = 0xFF ;
826 *destination
++ = 0xFF ;
827 *destination
++ = 0xFF ;
836 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
840 wxBitmap::wxBitmap(int w
, int h
, int d
)
842 (void)Create(w
, h
, d
);
845 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
847 (void) Create(data
, type
, width
, height
, depth
);
850 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
852 LoadFile(filename
, type
);
855 wxBitmap::wxBitmap(const char **bits
)
857 (void) CreateFromXpm(bits
);
860 wxBitmap::wxBitmap(char **bits
)
862 (void) CreateFromXpm((const char **)bits
);
865 void * wxBitmap::GetRawAccess() const
867 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
869 return M_BITMAPDATA
->GetRawAccess() ;
872 void * wxBitmap::BeginRawAccess()
874 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
876 return M_BITMAPDATA
->BeginRawAccess() ;
879 void wxBitmap::EndRawAccess()
881 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
883 M_BITMAPDATA
->EndRawAccess() ;
886 bool wxBitmap::CreateFromXpm(const char **bits
)
889 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
891 wxXPMDecoder decoder
;
892 wxImage img
= decoder
.ReadData(bits
);
893 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
895 *this = wxBitmap(img
);
905 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
907 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
909 return M_BITMAPDATA
->CGImageCreate() ;
913 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
916 (rect
.x
>= 0) && (rect
.y
>= 0) &&
917 (rect
.x
+rect
.width
<= GetWidth()) &&
918 (rect
.y
+rect
.height
<= GetHeight()),
919 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
921 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
922 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
924 int sourcewidth
= GetWidth() ;
925 int destwidth
= rect
.width
;
926 int destheight
= rect
.height
;
929 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
930 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
931 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
933 int sourcelinesize
= sourcewidth
* 4 ;
934 int destlinesize
= destwidth
* 4 ;
935 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
936 unsigned char *dest
= destdata
;
938 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
940 memcpy( dest
, source
, destlinesize
) ;
946 if ( M_BITMAPDATA
->m_bitmapMask
)
948 wxMemoryBuffer maskbuf
;
949 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
950 size_t maskbufsize
= rowBytes
* destheight
;
952 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
953 int destlinesize
= rowBytes
;
955 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
956 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
957 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
959 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
960 unsigned char *dest
= destdata
;
962 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
964 memcpy( dest
, source
, destlinesize
) ;
967 maskbuf
.UngetWriteBuf( maskbufsize
) ;
968 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
970 else if ( HasAlpha() )
976 bool wxBitmap::Create(int w
, int h
, int d
)
981 d
= wxDisplayDepth() ;
983 m_refData
= new wxBitmapRefData( w
, h
, d
);
985 return M_BITMAPDATA
->Ok() ;
988 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
992 wxBitmapHandler
*handler
= FindHandler(type
);
996 m_refData
= new wxBitmapRefData
;
998 return handler
->LoadFile(this, filename
, type
, -1, -1);
1003 wxImage
loadimage(filename
, type
);
1013 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1018 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
1022 m_refData
= new wxBitmapRefData
;
1024 wxBitmapHandler
*handler
= FindHandler(type
);
1026 if ( handler
== NULL
)
1028 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1033 return handler
->Create(this, data
, type
, width
, height
, depth
);
1038 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1040 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1042 // width and height of the device-dependent bitmap
1043 int width
= image
.GetWidth();
1044 int height
= image
.GetHeight();
1046 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1050 bool hasAlpha
= false ;
1052 if ( image
.HasMask() )
1054 // takes precedence, don't mix with alpha info
1058 hasAlpha
= image
.HasAlpha() ;
1064 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1065 register unsigned char* data
= image
.GetData();
1066 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1068 for (int y
= 0; y
< height
; y
++)
1070 for (int x
= 0; x
< width
; x
++)
1074 const unsigned char a
= *alpha
++;
1075 *destination
++ = a
;
1077 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1078 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1079 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1080 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1082 *destination
++ = *data
++ ;
1083 *destination
++ = *data
++ ;
1084 *destination
++ = *data
++ ;
1089 *destination
++ = 0xFF ;
1090 *destination
++ = *data
++ ;
1091 *destination
++ = *data
++ ;
1092 *destination
++ = *data
++ ;
1098 if ( image
.HasMask() )
1099 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1102 wxImage
wxBitmap::ConvertToImage() const
1106 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1108 // create an wxImage object
1109 int width
= GetWidth();
1110 int height
= GetHeight();
1111 image
.Create( width
, height
);
1113 unsigned char *data
= image
.GetData();
1114 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1116 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1118 bool hasAlpha
= false ;
1119 bool hasMask
= false ;
1120 int maskBytesPerRow
= 0 ;
1121 unsigned char *alpha
= NULL
;
1122 unsigned char *mask
= NULL
;
1130 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1131 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1137 alpha
= image
.GetAlpha() ;
1142 // The following masking algorithm is the same as well in msw/gtk:
1143 // the colour used as transparent one in wxImage and the one it is
1144 // replaced with when it actually occurs in the bitmap
1145 static const int MASK_RED
= 1;
1146 static const int MASK_GREEN
= 2;
1147 static const int MASK_BLUE
= 3;
1148 static const int MASK_BLUE_REPLACEMENT
= 2;
1150 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1152 unsigned char * maskp
= mask
;
1153 unsigned char a
, r
, g
, b
;
1156 for (int xx
= 0; xx
< width
; xx
++)
1158 color
= *((long*) source
) ;
1159 #ifdef WORDS_BIGENDIAN
1160 a
= ((color
&0xFF000000) >> 24) ;
1161 r
= ((color
&0x00FF0000) >> 16) ;
1162 g
= ((color
&0x0000FF00) >> 8) ;
1163 b
= (color
&0x000000FF);
1165 b
= ((color
&0xFF000000) >> 24) ;
1166 g
= ((color
&0x00FF0000) >> 16) ;
1167 r
= ((color
&0x0000FF00) >> 8) ;
1168 a
= (color
&0x000000FF);
1172 if ( *maskp
++ == 0xFF )
1178 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1179 b
= MASK_BLUE_REPLACEMENT
;
1185 else if ( hasAlpha
)
1189 data
[index
+ 1] = g
;
1190 data
[index
+ 2] = b
;
1198 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1203 #endif //wxUSE_IMAGE
1205 bool wxBitmap::SaveFile( const wxString
& filename
,
1206 wxBitmapType type
, const wxPalette
*palette
) const
1208 bool success
= false;
1209 wxBitmapHandler
*handler
= FindHandler(type
);
1213 success
= handler
->SaveFile(this, filename
, type
, palette
);
1218 wxImage image
= ConvertToImage();
1219 success
= image
.SaveFile(filename
, type
);
1221 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1228 bool wxBitmap::Ok() const
1230 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1233 int wxBitmap::GetHeight() const
1235 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1237 return M_BITMAPDATA
->GetHeight();
1240 int wxBitmap::GetWidth() const
1242 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1244 return M_BITMAPDATA
->GetWidth() ;
1247 int wxBitmap::GetDepth() const
1249 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1251 return M_BITMAPDATA
->GetDepth();
1254 #if WXWIN_COMPATIBILITY_2_4
1255 int wxBitmap::GetQuality() const
1260 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1265 wxMask
*wxBitmap::GetMask() const
1267 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1269 return M_BITMAPDATA
->m_bitmapMask
;
1272 bool wxBitmap::HasAlpha() const
1274 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1276 return M_BITMAPDATA
->HasAlpha() ;
1279 void wxBitmap::SetWidth(int w
)
1282 m_refData
= new wxBitmapRefData
;
1284 M_BITMAPDATA
->SetWidth(w
);
1287 void wxBitmap::SetHeight(int h
)
1290 m_refData
= new wxBitmapRefData
;
1292 M_BITMAPDATA
->SetHeight(h
);
1295 void wxBitmap::SetDepth(int d
)
1298 m_refData
= new wxBitmapRefData
;
1300 M_BITMAPDATA
->SetDepth(d
);
1303 void wxBitmap::SetOk(bool isOk
)
1306 m_refData
= new wxBitmapRefData
;
1308 M_BITMAPDATA
->SetOk(isOk
);
1312 wxPalette
*wxBitmap::GetPalette() const
1314 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1316 return &M_BITMAPDATA
->m_bitmapPalette
;
1319 void wxBitmap::SetPalette(const wxPalette
& palette
)
1322 m_refData
= new wxBitmapRefData
;
1324 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1326 #endif // wxUSE_PALETTE
1328 void wxBitmap::SetMask(wxMask
*mask
)
1331 m_refData
= new wxBitmapRefData
;
1333 // Remove existing mask if there is one.
1334 delete M_BITMAPDATA
->m_bitmapMask
;
1336 M_BITMAPDATA
->m_bitmapMask
= mask
;
1339 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1341 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1353 // Construct a mask from a bitmap and a colour indicating
1354 // the transparent area
1355 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1358 Create( bitmap
, colour
);
1361 // Construct a mask from a mono bitmap (copies the bitmap).
1362 wxMask::wxMask( const wxBitmap
& bitmap
)
1368 // Construct a mask from a mono bitmap (copies the bitmap).
1370 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1373 Create( data
, width
, height
, bytesPerRow
);
1380 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1381 m_maskBitmap
= NULL
;
1387 m_width
= m_height
= m_bytesPerRow
= 0 ;
1388 m_maskBitmap
= NULL
;
1391 void *wxMask::GetRawAccess() const
1393 return m_memBuf
.GetData() ;
1396 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1397 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1399 void wxMask::RealizeNative()
1403 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1404 m_maskBitmap
= NULL
;
1407 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1409 OSStatus err
= NewGWorldFromPtr(
1410 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1411 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1412 verify_noerr( err
) ;
1415 // Create a mask from a mono bitmap (copies the bitmap).
1417 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1422 m_bytesPerRow
= bytesPerRow
;
1424 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1431 // Create a mask from a mono bitmap (copies the bitmap).
1432 bool wxMask::Create(const wxBitmap
& bitmap
)
1434 m_width
= bitmap
.GetWidth() ;
1435 m_height
= bitmap
.GetHeight() ;
1436 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1438 size_t size
= m_bytesPerRow
* m_height
;
1439 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1440 wxASSERT( destdatabase
!= NULL
) ;
1442 memset( destdatabase
, 0 , size
) ;
1443 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1445 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1447 unsigned char *destdata
= destdatabase
;
1448 unsigned char r
, g
, b
;
1450 for ( int x
= 0 ; x
< m_width
; ++x
)
1457 if ( ( r
+ g
+ b
) > 0x10 )
1459 *destdata
++ = 0xFF ;
1460 *destdata
++ = 0xFF ;
1461 *destdata
++ = 0xFF ;
1462 *destdata
++ = 0xFF ;
1466 *destdata
++ = 0x00 ;
1467 *destdata
++ = 0x00 ;
1468 *destdata
++ = 0x00 ;
1469 *destdata
++ = 0x00 ;
1474 m_memBuf
.UngetWriteBuf( size
) ;
1480 // Create a mask from a bitmap and a colour indicating
1481 // the transparent area
1482 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1484 m_width
= bitmap
.GetWidth() ;
1485 m_height
= bitmap
.GetHeight() ;
1486 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1488 size_t size
= m_bytesPerRow
* m_height
;
1489 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1490 wxASSERT( destdatabase
!= NULL
) ;
1492 memset( destdatabase
, 0 , size
) ;
1493 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1495 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1497 unsigned char *destdata
= destdatabase
;
1498 unsigned char r
, g
, b
;
1500 for ( int x
= 0 ; x
< m_width
; ++x
)
1507 if ( colour
== wxColour( r
, g
, b
) )
1509 *destdata
++ = 0xFF ;
1510 *destdata
++ = 0xFF ;
1511 *destdata
++ = 0xFF ;
1512 *destdata
++ = 0xFF ;
1516 *destdata
++ = 0x00 ;
1517 *destdata
++ = 0x00 ;
1518 *destdata
++ = 0x00 ;
1519 *destdata
++ = 0x00 ;
1524 m_memBuf
.UngetWriteBuf( size
) ;
1530 WXHBITMAP
wxMask::GetHBITMAP() const
1532 return m_maskBitmap
;
1535 // ----------------------------------------------------------------------------
1537 // ----------------------------------------------------------------------------
1539 wxBitmapHandler::~wxBitmapHandler()
1543 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1548 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1549 int desiredWidth
, int desiredHeight
)
1554 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1559 // ----------------------------------------------------------------------------
1560 // Standard Handlers
1561 // ----------------------------------------------------------------------------
1563 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1565 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1568 inline wxPICTResourceHandler()
1570 SetName(wxT("Macintosh Pict resource"));
1571 SetExtension(wxEmptyString
);
1572 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1575 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1576 int desiredWidth
, int desiredHeight
);
1579 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1582 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1583 int desiredWidth
, int desiredHeight
)
1587 wxMacStringToPascal( name
, theName
) ;
1589 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1594 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1595 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1597 dc
.SelectObject( *bitmap
) ;
1599 dc
.SelectObject( wxNullBitmap
) ;
1608 void wxBitmap::InitStandardHandlers()
1610 AddHandler( new wxPICTResourceHandler
) ;
1611 AddHandler( new wxICONResourceHandler
) ;
1614 // ----------------------------------------------------------------------------
1615 // raw bitmap access support
1616 // ----------------------------------------------------------------------------
1618 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1621 // no bitmap, no data (raw or otherwise)
1624 data
.m_width
= GetWidth() ;
1625 data
.m_height
= GetHeight() ;
1626 data
.m_stride
= GetWidth() * 4 ;
1628 return BeginRawAccess() ;
1631 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1636 void wxBitmap::UseAlpha()
1638 // remember that we are using alpha channel:
1639 // we'll need to create a proper mask in UngetRawData()
1640 M_BITMAPDATA
->UseAlpha( true );