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
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 // Implementation Notes
40 // --------------------
42 // we are always working with a 32 bit deep pixel buffer
43 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
44 // therefore we have a separate GWorld there for blitting the mask in
46 // under Quartz then content is transformed into a CGImageRef representing the same data
47 // which can be transferred to the GPU by the OS for fast rendering
49 // we don't dare use premultiplied alpha yet
50 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
54 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
56 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
59 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
63 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
66 wxBitmapRefData
* bmp
= bmap
;
68 if ( !bmap
->HasNativeSize() )
70 // as PICT conversion will only result in a 16x16 icon, let's attempt
71 // a few scales for better results
73 int w
= bitmap
.GetWidth() ;
74 int h
= bitmap
.GetHeight() ;
75 int sz
= wxMax( w
, h
) ;
76 if ( sz
== 24 || sz
== 64 )
78 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
79 bmp
= scaleBmp
.GetBitmapData() ;
83 info
->contentType
= kControlContentIconRef
;
84 info
->u
.iconRef
= bmp
->GetIconRef() ;
85 AcquireIconRef( info
->u
.iconRef
) ;
87 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
88 else if ( forceType
== kControlContentCGImageRef
)
90 info
->contentType
= kControlContentCGImageRef
;
91 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
97 info
->contentType
= kControlContentPictHandle
;
98 info
->u
.picture
= bmap
->GetPictHandle() ;
104 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
106 if ( info
->contentType
== kControlContentIconRef
)
108 ReleaseIconRef( info
->u
.iconRef
) ;
110 else if ( info
->contentType
== kControlNoContent
)
112 // there's no bitmap at all, fall through silently
114 else if ( info
->contentType
== kControlContentPictHandle
)
116 // owned by the bitmap, no release here
118 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
119 else if ( info
->contentType
== kControlContentCGImageRef
)
121 CGImageRelease( info
->u
.imageRef
) ;
126 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
130 #endif //wxUSE_BMPBUTTON
132 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
134 void wxBitmapRefData::Init()
140 m_bitmapMask
= NULL
;
143 m_cgImageRef
= NULL
;
147 m_pictHandle
= NULL
;
149 m_hMaskBitmap
= NULL
;
150 m_maskBytesPerRow
= 0 ;
152 m_rawAccessCount
= 0 ;
156 wxBitmapRefData::wxBitmapRefData()
161 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
164 Create( w
, h
, d
) ;
167 bool wxBitmapRefData::Create( int w
, int h
, int d
)
169 m_width
= wxMax(1, w
);
170 m_height
= wxMax(1, h
);
173 m_bytesPerRow
= w
* 4 ;
174 size_t size
= m_bytesPerRow
* h
;
175 void* data
= m_memBuf
.GetWriteBuf( size
) ;
176 memset( data
, 0 , size
) ;
177 m_memBuf
.UngetWriteBuf( size
) ;
180 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
182 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
183 (char*) data
, m_bytesPerRow
) ) ;
184 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
186 m_ok
= ( m_hBitmap
!= NULL
) ;
191 void wxBitmapRefData::UseAlpha( bool use
)
193 if ( m_hasAlpha
== use
)
199 wxASSERT( m_hMaskBitmap
== NULL
) ;
201 int width
= GetWidth() ;
202 int height
= GetHeight() ;
203 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
204 size_t size
= height
* m_maskBytesPerRow
;
205 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
206 wxASSERT( data
!= NULL
) ;
208 memset( data
, 0 , size
) ;
209 Rect rect
= { 0 , 0 , height
, width
} ;
211 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
212 (char*) data
, m_maskBytesPerRow
) ) ;
213 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
215 m_maskMemBuf
.UngetWriteBuf(size
) ;
217 #if !wxMAC_USE_CORE_GRAPHICS
224 DisposeGWorld( m_hMaskBitmap
) ;
226 m_hMaskBitmap
= NULL
;
227 m_maskBytesPerRow
= 0 ;
231 void *wxBitmapRefData::GetRawAccess() const
233 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
234 return m_memBuf
.GetData() ;
237 void *wxBitmapRefData::BeginRawAccess()
239 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
240 wxASSERT( m_rawAccessCount
== 0 ) ;
241 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
242 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
247 // we must destroy an existing cached image, as
248 // the bitmap data may change now
251 CGImageRelease( m_cgImageRef
) ;
252 m_cgImageRef
= NULL
;
256 return m_memBuf
.GetData() ;
259 void wxBitmapRefData::EndRawAccess()
261 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
262 wxASSERT( m_rawAccessCount
== 1 ) ;
266 #if !wxMAC_USE_CORE_GRAPHICS
271 bool wxBitmapRefData::HasNativeSize()
274 int h
= GetHeight() ;
275 int sz
= wxMax( w
, h
) ;
277 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
280 IconRef
wxBitmapRefData::GetIconRef()
282 if ( m_iconRef
== NULL
)
284 // Create Icon Family Handle
286 IconFamilyHandle iconFamily
= NULL
;
288 #ifdef WORDS_BIGENDIAN
289 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
290 (**iconFamily
).resourceType
= kIconFamilyType
;
291 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
293 // test this solution on big endian as well
294 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
298 int h
= GetHeight() ;
299 int sz
= wxMax( w
, h
) ;
301 OSType dataType
= 0 ;
302 OSType maskType
= 0 ;
307 dataType
= kThumbnail32BitData
;
308 maskType
= kThumbnail8BitMask
;
312 dataType
= kHuge32BitData
;
313 maskType
= kHuge8BitMask
;
317 dataType
= kLarge32BitData
;
318 maskType
= kLarge8BitMask
;
322 dataType
= kSmall32BitData
;
323 maskType
= kSmall8BitMask
;
332 // setup the header properly
335 Handle maskdata
= NULL
;
336 unsigned char * maskptr
= NULL
;
337 unsigned char * ptr
= NULL
;
338 size_t datasize
, masksize
;
340 datasize
= sz
* sz
* 4 ;
341 data
= NewHandle( datasize
) ;
343 ptr
= (unsigned char*) *data
;
344 memset( ptr
, 0, datasize
) ;
347 maskdata
= NewHandle( masksize
) ;
349 maskptr
= (unsigned char*) *maskdata
;
350 memset( maskptr
, 0 , masksize
) ;
352 bool hasAlpha
= HasAlpha() ;
353 wxMask
*mask
= m_bitmapMask
;
354 unsigned char * source
= (unsigned char*) GetRawAccess() ;
355 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
357 for ( int y
= 0 ; y
< h
; ++y
)
359 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
360 unsigned char * maskdest
= maskptr
+ y
* sz
;
361 unsigned char a
, r
, g
, b
;
363 for ( int x
= 0 ; x
< w
; ++x
)
377 *maskdest
++ = 0xFF - *masksource
++ ;
389 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
390 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
392 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
393 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
396 HUnlock( maskdata
) ;
397 DisposeHandle( data
) ;
398 DisposeHandle( maskdata
) ;
402 PicHandle pic
= GetPictHandle() ;
403 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
405 // transform into IconRef
406 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
407 // cleaner version existing from 10.3 upwards
408 HLock((Handle
) iconFamily
);
409 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
410 HUnlock((Handle
) iconFamily
);
411 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
413 static int iconCounter
= 2 ;
415 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
416 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
418 // we have to retain a reference, as Unregister will decrement it
419 AcquireIconRef( m_iconRef
) ;
420 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
423 DisposeHandle( (Handle
) iconFamily
) ;
429 PicHandle
wxBitmapRefData::GetPictHandle()
431 if ( m_pictHandle
== NULL
)
434 CGrafPtr origPort
= NULL
;
435 GDHandle origDev
= NULL
;
436 GWorldPtr wp
= NULL
;
437 GWorldPtr mask
= NULL
;
438 int height
= GetHeight() ;
439 int width
= GetWidth() ;
441 Rect rect
= { 0 , 0 , height
, width
} ;
442 RgnHandle clipRgn
= NULL
;
444 GetGWorld( &origPort
, &origDev
) ;
445 wp
= GetHBITMAP( &mask
) ;
449 GWorldPtr monoworld
;
451 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
453 LockPixels( GetGWorldPixMap( monoworld
) ) ;
454 LockPixels( GetGWorldPixMap( mask
) ) ;
455 SetGWorld( monoworld
, NULL
) ;
457 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
458 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
459 RGBForeColor( &black
) ;
460 RGBBackColor( &white
) ;
462 CopyBits(GetPortBitMapForCopyBits(mask
),
463 GetPortBitMapForCopyBits(monoworld
),
467 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
469 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
470 UnlockPixels( GetGWorldPixMap( mask
) ) ;
471 DisposeGWorld( monoworld
) ;
474 SetGWorld( wp
, NULL
) ;
476 GetPortBounds( wp
, &portRect
) ;
477 m_pictHandle
= OpenPicture(&portRect
);
481 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
482 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
484 RGBForeColor( &black
) ;
485 RGBBackColor( &white
) ;
490 LockPixels( GetGWorldPixMap( wp
) ) ;
491 CopyBits(GetPortBitMapForCopyBits(wp
),
492 GetPortBitMapForCopyBits(wp
),
496 UnlockPixels( GetGWorldPixMap( wp
) ) ;
500 SetGWorld( origPort
, origDev
) ;
502 DisposeRgn( clipRgn
) ;
506 return m_pictHandle
;
510 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
512 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
514 wxASSERT( data
== membuf
->GetData() ) ;
519 CGImageRef
wxBitmapRefData::CGImageCreate() const
522 wxASSERT( m_rawAccessCount
>= 0 ) ;
524 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
526 size_t imageSize
= m_width
* m_height
* 4 ;
527 void * dataBuffer
= m_memBuf
.GetData() ;
530 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
531 wxMemoryBuffer
* membuf
= NULL
;
535 alphaInfo
= kCGImageAlphaFirst
;
536 membuf
= new wxMemoryBuffer( imageSize
) ;
537 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
538 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
539 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
540 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
541 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
543 unsigned char *sourcemask
= sourcemaskstart
;
544 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
546 *destalpha
= 0xFF - *sourcemask
;
554 #if wxMAC_USE_PREMULTIPLIED_ALPHA
555 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
557 alphaInfo
= kCGImageAlphaFirst
;
561 membuf
= new wxMemoryBuffer( m_memBuf
) ;
564 CGDataProviderRef dataProvider
= NULL
;
567 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
568 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
569 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
570 // copy one color component
571 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
572 maskBufData
[i
] = bufData
[i
*4+3];
574 CGDataProviderCreateWithData(
575 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
576 wxMacMemoryBufferReleaseProc
);
577 // as we are now passing the mask buffer to the data provider, we have
578 // to release the membuf ourselves
581 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
585 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
587 CGDataProviderCreateWithData(
588 membuf
, (const void *)membuf
->GetData() , imageSize
,
589 wxMacMemoryBufferReleaseProc
);
592 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
593 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
595 CGDataProviderRelease( dataProvider
);
599 image
= m_cgImageRef
;
600 CGImageRetain( image
) ;
603 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
605 // we keep it for later use
606 m_cgImageRef
= image
;
607 CGImageRetain( image
) ;
614 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
616 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
622 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
624 else if ( m_hasAlpha
)
626 #if !wxMAC_USE_CORE_GRAPHICS
627 if ( m_rawAccessCount
> 0 )
630 // this structure is not kept in synch when using CG, so if something
631 // is really accessing the GrafPorts, we have to sync it
635 *mask
= m_hMaskBitmap
;
642 void wxBitmapRefData::UpdateAlphaMask() const
646 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
647 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
649 int h
= GetHeight() ;
652 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
654 unsigned char* destalpha
= destalphabase
;
656 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
658 // we must have 24 bit depth for non quartz smooth alpha
660 *destalpha
++ = 255 - *sourcemask
;
661 *destalpha
++ = 255 - *sourcemask
;
662 *destalpha
++ = 255 - *sourcemask
;
668 void wxBitmapRefData::Free()
670 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
675 CGImageRelease( m_cgImageRef
) ;
676 m_cgImageRef
= NULL
;
682 ReleaseIconRef( m_iconRef
) ;
689 KillPicture( m_pictHandle
) ;
690 m_pictHandle
= NULL
;
695 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
701 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
702 m_hMaskBitmap
= NULL
;
712 wxBitmapRefData::~wxBitmapRefData()
717 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
719 bool created
= false ;
720 int w
= icon
.GetWidth() ;
721 int h
= icon
.GetHeight() ;
723 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
725 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
727 IconFamilyHandle iconFamily
= NULL
;
728 Handle imagehandle
= NewHandle( 0 ) ;
729 Handle maskhandle
= NewHandle( 0 ) ;
733 IconSelectorValue selector
= 0 ;
738 dataType
= kThumbnail32BitData
;
739 maskType
= kThumbnail8BitMask
;
740 selector
= kSelectorAllAvailableData
;
744 dataType
= kHuge32BitData
;
745 maskType
= kHuge8BitMask
;
746 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
750 dataType
= kLarge32BitData
;
751 maskType
= kLarge8BitMask
;
752 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
756 dataType
= kSmall32BitData
;
757 maskType
= kSmall8BitMask
;
758 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
765 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
767 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
768 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
769 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
770 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
772 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
774 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
775 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
779 unsigned char *source
= (unsigned char *) *imagehandle
;
780 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
781 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
783 for ( int y
= 0 ; y
< h
; ++y
)
785 for ( int x
= 0 ; x
< w
; ++x
)
787 *destination
++ = *sourcemask
++ ;
789 *destination
++ = *source
++ ;
790 *destination
++ = *source
++ ;
791 *destination
++ = *source
++ ;
796 DisposeHandle( imagehandle
) ;
797 DisposeHandle( maskhandle
) ;
801 DisposeHandle( (Handle
) iconFamily
) ;
807 dc
.SelectObject( *this ) ;
808 dc
.DrawIcon( icon
, 0 , 0 ) ;
809 dc
.SelectObject( wxNullBitmap
) ;
819 wxBitmap::~wxBitmap()
823 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
825 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
829 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
830 if ( the_width
% (sizeof(unsigned char) * 8) )
831 linesize
+= sizeof(unsigned char);
833 unsigned char* linestart
= (unsigned char*) bits
;
834 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
836 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
838 int index
, bit
, mask
;
840 for ( int x
= 0 ; x
< the_width
; ++x
)
846 if ( linestart
[index
] & mask
)
848 *destination
++ = 0xFF ;
855 *destination
++ = 0xFF ;
856 *destination
++ = 0xFF ;
857 *destination
++ = 0xFF ;
858 *destination
++ = 0xFF ;
867 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
871 wxBitmap::wxBitmap(int w
, int h
, int d
)
873 (void)Create(w
, h
, d
);
876 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
878 (void) Create(data
, type
, width
, height
, depth
);
881 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
883 LoadFile(filename
, type
);
886 void * wxBitmap::GetRawAccess() const
888 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
890 return M_BITMAPDATA
->GetRawAccess() ;
893 void * wxBitmap::BeginRawAccess()
895 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
897 return M_BITMAPDATA
->BeginRawAccess() ;
900 void wxBitmap::EndRawAccess()
902 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
904 M_BITMAPDATA
->EndRawAccess() ;
908 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
910 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
912 return M_BITMAPDATA
->CGImageCreate() ;
916 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
919 (rect
.x
>= 0) && (rect
.y
>= 0) &&
920 (rect
.x
+rect
.width
<= GetWidth()) &&
921 (rect
.y
+rect
.height
<= GetHeight()),
922 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
924 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
925 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
927 int sourcewidth
= GetWidth() ;
928 int destwidth
= rect
.width
;
929 int destheight
= rect
.height
;
932 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
933 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
934 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
936 int sourcelinesize
= sourcewidth
* 4 ;
937 int destlinesize
= destwidth
* 4 ;
938 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
939 unsigned char *dest
= destdata
;
941 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
943 memcpy( dest
, source
, destlinesize
) ;
949 if ( M_BITMAPDATA
->m_bitmapMask
)
951 wxMemoryBuffer maskbuf
;
952 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
953 size_t maskbufsize
= rowBytes
* destheight
;
955 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
956 int destlinesize
= rowBytes
;
958 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
959 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
960 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
962 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
963 unsigned char *dest
= destdata
;
965 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
967 memcpy( dest
, source
, destlinesize
) ;
970 maskbuf
.UngetWriteBuf( maskbufsize
) ;
971 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
973 else if ( HasAlpha() )
979 bool wxBitmap::Create(int w
, int h
, int d
)
984 d
= wxDisplayDepth() ;
986 m_refData
= new wxBitmapRefData( w
, h
, d
);
988 return M_BITMAPDATA
->Ok() ;
991 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
995 wxBitmapHandler
*handler
= FindHandler(type
);
999 m_refData
= new wxBitmapRefData
;
1001 return handler
->LoadFile(this, filename
, type
, -1, -1);
1006 wxImage
loadimage(filename
, type
);
1016 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1021 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1025 m_refData
= new wxBitmapRefData
;
1027 wxBitmapHandler
*handler
= FindHandler(type
);
1029 if ( handler
== NULL
)
1031 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1036 return handler
->Create(this, data
, type
, width
, height
, depth
);
1041 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1043 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1045 // width and height of the device-dependent bitmap
1046 int width
= image
.GetWidth();
1047 int height
= image
.GetHeight();
1049 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1053 bool hasAlpha
= false ;
1055 if ( image
.HasMask() )
1057 // takes precedence, don't mix with alpha info
1061 hasAlpha
= image
.HasAlpha() ;
1067 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1068 register unsigned char* data
= image
.GetData();
1069 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1071 for (int y
= 0; y
< height
; y
++)
1073 for (int x
= 0; x
< width
; x
++)
1077 const unsigned char a
= *alpha
++;
1078 *destination
++ = a
;
1080 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1081 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1082 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1083 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1085 *destination
++ = *data
++ ;
1086 *destination
++ = *data
++ ;
1087 *destination
++ = *data
++ ;
1092 *destination
++ = 0xFF ;
1093 *destination
++ = *data
++ ;
1094 *destination
++ = *data
++ ;
1095 *destination
++ = *data
++ ;
1101 if ( image
.HasMask() )
1102 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1105 wxImage
wxBitmap::ConvertToImage() const
1109 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1111 // create an wxImage object
1112 int width
= GetWidth();
1113 int height
= GetHeight();
1114 image
.Create( width
, height
);
1116 unsigned char *data
= image
.GetData();
1117 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1119 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1121 bool hasAlpha
= false ;
1122 bool hasMask
= false ;
1123 int maskBytesPerRow
= 0 ;
1124 unsigned char *alpha
= NULL
;
1125 unsigned char *mask
= NULL
;
1133 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1134 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1140 alpha
= image
.GetAlpha() ;
1145 // The following masking algorithm is the same as well in msw/gtk:
1146 // the colour used as transparent one in wxImage and the one it is
1147 // replaced with when it actually occurs in the bitmap
1148 static const int MASK_RED
= 1;
1149 static const int MASK_GREEN
= 2;
1150 static const int MASK_BLUE
= 3;
1151 static const int MASK_BLUE_REPLACEMENT
= 2;
1153 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1155 unsigned char * maskp
= mask
;
1156 unsigned char a
, r
, g
, b
;
1159 for (int xx
= 0; xx
< width
; xx
++)
1161 color
= *((long*) source
) ;
1162 #ifdef WORDS_BIGENDIAN
1163 a
= ((color
&0xFF000000) >> 24) ;
1164 r
= ((color
&0x00FF0000) >> 16) ;
1165 g
= ((color
&0x0000FF00) >> 8) ;
1166 b
= (color
&0x000000FF);
1168 b
= ((color
&0xFF000000) >> 24) ;
1169 g
= ((color
&0x00FF0000) >> 16) ;
1170 r
= ((color
&0x0000FF00) >> 8) ;
1171 a
= (color
&0x000000FF);
1175 if ( *maskp
++ == 0xFF )
1181 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1182 b
= MASK_BLUE_REPLACEMENT
;
1188 else if ( hasAlpha
)
1192 data
[index
+ 1] = g
;
1193 data
[index
+ 2] = b
;
1201 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1206 #endif //wxUSE_IMAGE
1208 bool wxBitmap::SaveFile( const wxString
& filename
,
1209 wxBitmapType type
, const wxPalette
*palette
) const
1211 bool success
= false;
1212 wxBitmapHandler
*handler
= FindHandler(type
);
1216 success
= handler
->SaveFile(this, filename
, type
, palette
);
1221 wxImage image
= ConvertToImage();
1222 success
= image
.SaveFile(filename
, type
);
1224 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1231 bool wxBitmap::IsOk() const
1233 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1236 int wxBitmap::GetHeight() const
1238 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1240 return M_BITMAPDATA
->GetHeight();
1243 int wxBitmap::GetWidth() const
1245 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1247 return M_BITMAPDATA
->GetWidth() ;
1250 int wxBitmap::GetDepth() const
1252 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1254 return M_BITMAPDATA
->GetDepth();
1257 #if WXWIN_COMPATIBILITY_2_4
1258 int wxBitmap::GetQuality() const
1263 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1268 wxMask
*wxBitmap::GetMask() const
1270 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1272 return M_BITMAPDATA
->m_bitmapMask
;
1275 bool wxBitmap::HasAlpha() const
1277 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1279 return M_BITMAPDATA
->HasAlpha() ;
1282 void wxBitmap::SetWidth(int w
)
1285 M_BITMAPDATA
->SetWidth(w
);
1288 void wxBitmap::SetHeight(int h
)
1291 M_BITMAPDATA
->SetHeight(h
);
1294 void wxBitmap::SetDepth(int d
)
1297 M_BITMAPDATA
->SetDepth(d
);
1300 void wxBitmap::SetOk(bool isOk
)
1303 M_BITMAPDATA
->SetOk(isOk
);
1307 wxPalette
*wxBitmap::GetPalette() const
1309 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1311 return &M_BITMAPDATA
->m_bitmapPalette
;
1314 void wxBitmap::SetPalette(const wxPalette
& palette
)
1317 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1319 #endif // wxUSE_PALETTE
1321 void wxBitmap::SetMask(wxMask
*mask
)
1324 // Remove existing mask if there is one.
1325 delete M_BITMAPDATA
->m_bitmapMask
;
1327 M_BITMAPDATA
->m_bitmapMask
= mask
;
1330 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1332 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1335 // ----------------------------------------------------------------------------
1337 // ----------------------------------------------------------------------------
1344 // Construct a mask from a bitmap and a colour indicating
1345 // the transparent area
1346 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1349 Create( bitmap
, colour
);
1352 // Construct a mask from a mono bitmap (copies the bitmap).
1353 wxMask::wxMask( const wxBitmap
& bitmap
)
1359 // Construct a mask from a mono bitmap (copies the bitmap).
1361 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1364 Create( data
, width
, height
, bytesPerRow
);
1372 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1373 m_maskBitmap
= NULL
;
1380 m_width
= m_height
= m_bytesPerRow
= 0 ;
1381 m_maskBitmap
= NULL
;
1384 void *wxMask::GetRawAccess() const
1386 return m_memBuf
.GetData() ;
1389 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1390 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1392 void wxMask::RealizeNative()
1397 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1398 m_maskBitmap
= NULL
;
1401 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1403 OSStatus err
= NewGWorldFromPtr(
1404 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1405 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1406 verify_noerr( err
) ;
1410 // Create a mask from a mono bitmap (copies the bitmap).
1412 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1417 m_bytesPerRow
= bytesPerRow
;
1419 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1426 // Create a mask from a mono bitmap (copies the bitmap).
1427 bool wxMask::Create(const wxBitmap
& bitmap
)
1429 m_width
= bitmap
.GetWidth() ;
1430 m_height
= bitmap
.GetHeight() ;
1431 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1433 size_t size
= m_bytesPerRow
* m_height
;
1434 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1435 wxASSERT( destdatabase
!= NULL
) ;
1437 memset( destdatabase
, 0 , size
) ;
1438 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1440 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1442 unsigned char *destdata
= destdatabase
;
1443 unsigned char r
, g
, b
;
1445 for ( int x
= 0 ; x
< m_width
; ++x
)
1452 if ( ( r
+ g
+ b
) > 0x10 )
1454 *destdata
++ = 0xFF ;
1455 *destdata
++ = 0xFF ;
1456 *destdata
++ = 0xFF ;
1457 *destdata
++ = 0xFF ;
1461 *destdata
++ = 0x00 ;
1462 *destdata
++ = 0x00 ;
1463 *destdata
++ = 0x00 ;
1464 *destdata
++ = 0x00 ;
1469 m_memBuf
.UngetWriteBuf( size
) ;
1475 // Create a mask from a bitmap and a colour indicating
1476 // the transparent area
1477 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1479 m_width
= bitmap
.GetWidth() ;
1480 m_height
= bitmap
.GetHeight() ;
1481 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1483 size_t size
= m_bytesPerRow
* m_height
;
1484 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1485 wxASSERT( destdatabase
!= NULL
) ;
1487 memset( destdatabase
, 0 , size
) ;
1488 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1490 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1492 unsigned char *destdata
= destdatabase
;
1493 unsigned char r
, g
, b
;
1495 for ( int x
= 0 ; x
< m_width
; ++x
)
1502 if ( colour
== wxColour( r
, g
, b
) )
1504 *destdata
++ = 0xFF ;
1505 *destdata
++ = 0xFF ;
1506 *destdata
++ = 0xFF ;
1507 *destdata
++ = 0xFF ;
1511 *destdata
++ = 0x00 ;
1512 *destdata
++ = 0x00 ;
1513 *destdata
++ = 0x00 ;
1514 *destdata
++ = 0x00 ;
1519 m_memBuf
.UngetWriteBuf( size
) ;
1525 WXHBITMAP
wxMask::GetHBITMAP() const
1527 return m_maskBitmap
;
1530 // ----------------------------------------------------------------------------
1532 // ----------------------------------------------------------------------------
1534 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1536 // ----------------------------------------------------------------------------
1537 // Standard Handlers
1538 // ----------------------------------------------------------------------------
1540 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1542 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1545 inline wxPICTResourceHandler()
1547 SetName(wxT("Macintosh Pict resource"));
1548 SetExtension(wxEmptyString
);
1549 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1552 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1553 int desiredWidth
, int desiredHeight
);
1556 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1559 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1560 int desiredWidth
, int desiredHeight
)
1564 wxMacStringToPascal( name
, theName
) ;
1566 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1571 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1572 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1574 dc
.SelectObject( *bitmap
) ;
1576 dc
.SelectObject( wxNullBitmap
) ;
1585 void wxBitmap::InitStandardHandlers()
1587 AddHandler( new wxPICTResourceHandler
) ;
1588 AddHandler( new wxICONResourceHandler
) ;
1591 // ----------------------------------------------------------------------------
1592 // raw bitmap access support
1593 // ----------------------------------------------------------------------------
1595 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1598 // no bitmap, no data (raw or otherwise)
1601 data
.m_width
= GetWidth() ;
1602 data
.m_height
= GetHeight() ;
1603 data
.m_stride
= GetWidth() * 4 ;
1605 return BeginRawAccess() ;
1608 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1613 void wxBitmap::UseAlpha()
1615 // remember that we are using alpha channel:
1616 // we'll need to create a proper mask in UngetRawData()
1617 M_BITMAPDATA
->UseAlpha( true );