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() ;
96 info
->contentType
= kControlContentPictHandle
;
97 info
->u
.picture
= bmap
->GetPictHandle() ;
102 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
104 if ( info
->contentType
== kControlContentIconRef
)
106 ReleaseIconRef( info
->u
.iconRef
) ;
108 else if ( info
->contentType
== kControlNoContent
)
110 // there's no bitmap at all, fall through silently
112 else if ( info
->contentType
== kControlContentPictHandle
)
114 // owned by the bitmap, no release here
116 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
117 else if ( info
->contentType
== kControlContentCGImageRef
)
119 CGImageRelease( info
->u
.imageRef
) ;
124 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
128 #endif //wxUSE_BMPBUTTON
130 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
132 void wxBitmapRefData::Init()
138 m_bitmapMask
= NULL
;
141 m_cgImageRef
= NULL
;
145 m_pictHandle
= NULL
;
147 m_hMaskBitmap
= NULL
;
148 m_maskBytesPerRow
= 0 ;
150 m_rawAccessCount
= 0 ;
154 wxBitmapRefData::wxBitmapRefData()
159 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
162 Create( w
, h
, d
) ;
165 bool wxBitmapRefData::Create( int w
, int h
, int d
)
167 m_width
= wxMax(1, w
);
168 m_height
= wxMax(1, h
);
171 m_bytesPerRow
= w
* 4 ;
172 size_t size
= m_bytesPerRow
* h
;
173 void* data
= m_memBuf
.GetWriteBuf( size
) ;
174 memset( data
, 0 , size
) ;
175 m_memBuf
.UngetWriteBuf( size
) ;
178 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
179 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
180 (char*) data
, m_bytesPerRow
) ) ;
181 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
183 m_ok
= ( m_hBitmap
!= NULL
) ;
188 void wxBitmapRefData::UseAlpha( bool use
)
190 if ( m_hasAlpha
== use
)
196 wxASSERT( m_hMaskBitmap
== NULL
) ;
198 int width
= GetWidth() ;
199 int height
= GetHeight() ;
200 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
201 size_t size
= height
* m_maskBytesPerRow
;
202 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
203 wxASSERT( data
!= NULL
) ;
205 memset( data
, 0 , size
) ;
206 Rect rect
= { 0 , 0 , height
, width
} ;
207 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
208 (char*) data
, m_maskBytesPerRow
) ) ;
209 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
210 m_maskMemBuf
.UngetWriteBuf(size
) ;
212 #if !wxMAC_USE_CORE_GRAPHICS
218 DisposeGWorld( m_hMaskBitmap
) ;
219 m_hMaskBitmap
= NULL
;
220 m_maskBytesPerRow
= 0 ;
224 void *wxBitmapRefData::GetRawAccess() const
226 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
227 return m_memBuf
.GetData() ;
230 void *wxBitmapRefData::BeginRawAccess()
232 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
233 wxASSERT( m_rawAccessCount
== 0 ) ;
234 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
235 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
240 // we must destroy an existing cached image, as
241 // the bitmap data may change now
244 CGImageRelease( m_cgImageRef
) ;
245 m_cgImageRef
= NULL
;
249 return m_memBuf
.GetData() ;
252 void wxBitmapRefData::EndRawAccess()
254 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
255 wxASSERT( m_rawAccessCount
== 1 ) ;
259 #if !wxMAC_USE_CORE_GRAPHICS
264 bool wxBitmapRefData::HasNativeSize()
267 int h
= GetHeight() ;
268 int sz
= wxMax( w
, h
) ;
270 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
273 IconRef
wxBitmapRefData::GetIconRef()
275 if ( m_iconRef
== NULL
)
277 // Create Icon Family Handle
279 IconFamilyHandle iconFamily
= NULL
;
281 #ifdef WORDS_BIGENDIAN
282 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
283 (**iconFamily
).resourceType
= kIconFamilyType
;
284 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
286 // test this solution on big endian as well
287 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
291 int h
= GetHeight() ;
292 int sz
= wxMax( w
, h
) ;
294 OSType dataType
= 0 ;
295 OSType maskType
= 0 ;
300 dataType
= kThumbnail32BitData
;
301 maskType
= kThumbnail8BitMask
;
305 dataType
= kHuge32BitData
;
306 maskType
= kHuge8BitMask
;
310 dataType
= kLarge32BitData
;
311 maskType
= kLarge8BitMask
;
315 dataType
= kSmall32BitData
;
316 maskType
= kSmall8BitMask
;
325 // setup the header properly
328 Handle maskdata
= NULL
;
329 unsigned char * maskptr
= NULL
;
330 unsigned char * ptr
= NULL
;
331 size_t datasize
, masksize
;
333 datasize
= sz
* sz
* 4 ;
334 data
= NewHandle( datasize
) ;
336 ptr
= (unsigned char*) *data
;
337 memset( ptr
, 0, datasize
) ;
340 maskdata
= NewHandle( masksize
) ;
342 maskptr
= (unsigned char*) *maskdata
;
343 memset( maskptr
, 0 , masksize
) ;
345 bool hasAlpha
= HasAlpha() ;
346 wxMask
*mask
= m_bitmapMask
;
347 unsigned char * source
= (unsigned char*) GetRawAccess() ;
348 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
350 for ( int y
= 0 ; y
< h
; ++y
)
352 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
353 unsigned char * maskdest
= maskptr
+ y
* sz
;
354 unsigned char a
, r
, g
, b
;
356 for ( int x
= 0 ; x
< w
; ++x
)
370 *maskdest
++ = 0xFF - *masksource
++ ;
382 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
383 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
385 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
386 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
389 HUnlock( maskdata
) ;
390 DisposeHandle( data
) ;
391 DisposeHandle( maskdata
) ;
395 PicHandle pic
= GetPictHandle() ;
396 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
398 // transform into IconRef
399 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
400 // cleaner version existing from 10.3 upwards
401 HLock((Handle
) iconFamily
);
402 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
403 HUnlock((Handle
) iconFamily
);
404 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
406 static int iconCounter
= 2 ;
408 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
409 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
411 // we have to retain a reference, as Unregister will decrement it
412 AcquireIconRef( m_iconRef
) ;
413 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
416 DisposeHandle( (Handle
) iconFamily
) ;
422 PicHandle
wxBitmapRefData::GetPictHandle()
424 if ( m_pictHandle
== NULL
)
426 CGrafPtr origPort
= NULL
;
427 GDHandle origDev
= NULL
;
428 GWorldPtr wp
= NULL
;
429 GWorldPtr mask
= NULL
;
430 int height
= GetHeight() ;
431 int width
= GetWidth() ;
433 Rect rect
= { 0 , 0 , height
, width
} ;
434 RgnHandle clipRgn
= NULL
;
436 GetGWorld( &origPort
, &origDev
) ;
437 wp
= GetHBITMAP( &mask
) ;
441 GWorldPtr monoworld
;
443 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
445 LockPixels( GetGWorldPixMap( monoworld
) ) ;
446 LockPixels( GetGWorldPixMap( mask
) ) ;
447 SetGWorld( monoworld
, NULL
) ;
449 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
450 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
451 RGBForeColor( &black
) ;
452 RGBBackColor( &white
) ;
454 CopyBits(GetPortBitMapForCopyBits(mask
),
455 GetPortBitMapForCopyBits(monoworld
),
459 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
461 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
462 UnlockPixels( GetGWorldPixMap( mask
) ) ;
463 DisposeGWorld( monoworld
) ;
466 SetGWorld( wp
, NULL
) ;
468 GetPortBounds( wp
, &portRect
) ;
469 m_pictHandle
= OpenPicture(&portRect
);
473 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
474 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
476 RGBForeColor( &black
) ;
477 RGBBackColor( &white
) ;
482 LockPixels( GetGWorldPixMap( wp
) ) ;
483 CopyBits(GetPortBitMapForCopyBits(wp
),
484 GetPortBitMapForCopyBits(wp
),
488 UnlockPixels( GetGWorldPixMap( wp
) ) ;
492 SetGWorld( origPort
, origDev
) ;
494 DisposeRgn( clipRgn
) ;
497 return m_pictHandle
;
501 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
503 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
505 wxASSERT( data
== membuf
->GetData() ) ;
510 CGImageRef
wxBitmapRefData::CGImageCreate() const
513 wxASSERT( m_rawAccessCount
>= 0 ) ;
515 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
517 size_t imageSize
= m_width
* m_height
* 4 ;
518 void * dataBuffer
= m_memBuf
.GetData() ;
521 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
522 wxMemoryBuffer
* membuf
= NULL
;
526 alphaInfo
= kCGImageAlphaFirst
;
527 membuf
= new wxMemoryBuffer( imageSize
) ;
528 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
529 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
530 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
531 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
532 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
534 unsigned char *sourcemask
= sourcemaskstart
;
535 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
537 *destalpha
= 0xFF - *sourcemask
;
545 #if wxMAC_USE_PREMULTIPLIED_ALPHA
546 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
548 alphaInfo
= kCGImageAlphaFirst
;
552 membuf
= new wxMemoryBuffer( m_memBuf
) ;
555 CGDataProviderRef dataProvider
= NULL
;
558 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
559 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
560 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
561 // copy one color component
562 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
563 maskBufData
[i
] = bufData
[i
*4+3];
565 CGDataProviderCreateWithData(
566 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
567 wxMacMemoryBufferReleaseProc
);
568 // as we are now passing the mask buffer to the data provider, we have
569 // to release the membuf ourselves
572 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
576 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
578 CGDataProviderCreateWithData(
579 membuf
, (const void *)membuf
->GetData() , imageSize
,
580 wxMacMemoryBufferReleaseProc
);
583 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
584 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
586 CGDataProviderRelease( dataProvider
);
590 image
= m_cgImageRef
;
591 CGImageRetain( image
) ;
594 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
596 // we keep it for later use
597 m_cgImageRef
= image
;
598 CGImageRetain( image
) ;
605 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
607 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
613 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
615 else if ( m_hasAlpha
)
617 #if !wxMAC_USE_CORE_GRAPHICS
618 if ( m_rawAccessCount
> 0 )
621 // this structure is not kept in synch when using CG, so if something
622 // is really accessing the GrafPorts, we have to sync it
626 *mask
= m_hMaskBitmap
;
633 void wxBitmapRefData::UpdateAlphaMask() const
637 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
638 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
640 int h
= GetHeight() ;
643 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
645 unsigned char* destalpha
= destalphabase
;
647 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
649 // we must have 24 bit depth for non quartz smooth alpha
651 *destalpha
++ = 255 - *sourcemask
;
652 *destalpha
++ = 255 - *sourcemask
;
653 *destalpha
++ = 255 - *sourcemask
;
659 void wxBitmapRefData::Free()
661 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
666 CGImageRelease( m_cgImageRef
) ;
667 m_cgImageRef
= NULL
;
673 ReleaseIconRef( m_iconRef
) ;
679 KillPicture( m_pictHandle
) ;
680 m_pictHandle
= NULL
;
685 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
691 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
692 m_hMaskBitmap
= NULL
;
702 wxBitmapRefData::~wxBitmapRefData()
707 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
709 bool created
= false ;
710 int w
= icon
.GetWidth() ;
711 int h
= icon
.GetHeight() ;
713 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
715 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
717 IconFamilyHandle iconFamily
= NULL
;
718 Handle imagehandle
= NewHandle( 0 ) ;
719 Handle maskhandle
= NewHandle( 0 ) ;
723 IconSelectorValue selector
= 0 ;
728 dataType
= kThumbnail32BitData
;
729 maskType
= kThumbnail8BitMask
;
730 selector
= kSelectorAllAvailableData
;
734 dataType
= kHuge32BitData
;
735 maskType
= kHuge8BitMask
;
736 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
740 dataType
= kLarge32BitData
;
741 maskType
= kLarge8BitMask
;
742 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
746 dataType
= kSmall32BitData
;
747 maskType
= kSmall8BitMask
;
748 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
755 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
757 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
758 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
759 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
760 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
762 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
764 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
765 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
769 unsigned char *source
= (unsigned char *) *imagehandle
;
770 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
771 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
773 for ( int y
= 0 ; y
< h
; ++y
)
775 for ( int x
= 0 ; x
< w
; ++x
)
777 *destination
++ = *sourcemask
++ ;
779 *destination
++ = *source
++ ;
780 *destination
++ = *source
++ ;
781 *destination
++ = *source
++ ;
786 DisposeHandle( imagehandle
) ;
787 DisposeHandle( maskhandle
) ;
791 DisposeHandle( (Handle
) iconFamily
) ;
797 dc
.SelectObject( *this ) ;
798 dc
.DrawIcon( icon
, 0 , 0 ) ;
799 dc
.SelectObject( wxNullBitmap
) ;
809 wxBitmap::~wxBitmap()
813 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
815 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
819 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
820 if ( the_width
% (sizeof(unsigned char) * 8) )
821 linesize
+= sizeof(unsigned char);
823 unsigned char* linestart
= (unsigned char*) bits
;
824 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
826 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
828 int index
, bit
, mask
;
830 for ( int x
= 0 ; x
< the_width
; ++x
)
836 if ( linestart
[index
] & mask
)
838 *destination
++ = 0xFF ;
845 *destination
++ = 0xFF ;
846 *destination
++ = 0xFF ;
847 *destination
++ = 0xFF ;
848 *destination
++ = 0xFF ;
857 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
861 wxBitmap::wxBitmap(int w
, int h
, int d
)
863 (void)Create(w
, h
, d
);
866 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
868 (void) Create(data
, type
, width
, height
, depth
);
871 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
873 LoadFile(filename
, type
);
876 void * wxBitmap::GetRawAccess() const
878 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
880 return M_BITMAPDATA
->GetRawAccess() ;
883 void * wxBitmap::BeginRawAccess()
885 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
887 return M_BITMAPDATA
->BeginRawAccess() ;
890 void wxBitmap::EndRawAccess()
892 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
894 M_BITMAPDATA
->EndRawAccess() ;
898 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
900 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
902 return M_BITMAPDATA
->CGImageCreate() ;
906 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
909 (rect
.x
>= 0) && (rect
.y
>= 0) &&
910 (rect
.x
+rect
.width
<= GetWidth()) &&
911 (rect
.y
+rect
.height
<= GetHeight()),
912 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
914 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
915 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
917 int sourcewidth
= GetWidth() ;
918 int destwidth
= rect
.width
;
919 int destheight
= rect
.height
;
922 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
923 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
924 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
926 int sourcelinesize
= sourcewidth
* 4 ;
927 int destlinesize
= destwidth
* 4 ;
928 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
929 unsigned char *dest
= destdata
;
931 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
933 memcpy( dest
, source
, destlinesize
) ;
939 if ( M_BITMAPDATA
->m_bitmapMask
)
941 wxMemoryBuffer maskbuf
;
942 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
943 size_t maskbufsize
= rowBytes
* destheight
;
945 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
946 int destlinesize
= rowBytes
;
948 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
949 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
950 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
952 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
953 unsigned char *dest
= destdata
;
955 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
957 memcpy( dest
, source
, destlinesize
) ;
960 maskbuf
.UngetWriteBuf( maskbufsize
) ;
961 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
963 else if ( HasAlpha() )
969 bool wxBitmap::Create(int w
, int h
, int d
)
974 d
= wxDisplayDepth() ;
976 m_refData
= new wxBitmapRefData( w
, h
, d
);
978 return M_BITMAPDATA
->Ok() ;
981 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
985 wxBitmapHandler
*handler
= FindHandler(type
);
989 m_refData
= new wxBitmapRefData
;
991 return handler
->LoadFile(this, filename
, type
, -1, -1);
996 wxImage
loadimage(filename
, type
);
1006 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1011 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1015 m_refData
= new wxBitmapRefData
;
1017 wxBitmapHandler
*handler
= FindHandler(type
);
1019 if ( handler
== NULL
)
1021 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1026 return handler
->Create(this, data
, type
, width
, height
, depth
);
1031 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1033 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1035 // width and height of the device-dependent bitmap
1036 int width
= image
.GetWidth();
1037 int height
= image
.GetHeight();
1039 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1043 bool hasAlpha
= false ;
1045 if ( image
.HasMask() )
1047 // takes precedence, don't mix with alpha info
1051 hasAlpha
= image
.HasAlpha() ;
1057 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1058 register unsigned char* data
= image
.GetData();
1059 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1061 for (int y
= 0; y
< height
; y
++)
1063 for (int x
= 0; x
< width
; x
++)
1067 const unsigned char a
= *alpha
++;
1068 *destination
++ = a
;
1070 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1071 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1072 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1073 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1075 *destination
++ = *data
++ ;
1076 *destination
++ = *data
++ ;
1077 *destination
++ = *data
++ ;
1082 *destination
++ = 0xFF ;
1083 *destination
++ = *data
++ ;
1084 *destination
++ = *data
++ ;
1085 *destination
++ = *data
++ ;
1091 if ( image
.HasMask() )
1092 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1095 wxImage
wxBitmap::ConvertToImage() const
1099 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1101 // create an wxImage object
1102 int width
= GetWidth();
1103 int height
= GetHeight();
1104 image
.Create( width
, height
);
1106 unsigned char *data
= image
.GetData();
1107 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1109 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1111 bool hasAlpha
= false ;
1112 bool hasMask
= false ;
1113 int maskBytesPerRow
= 0 ;
1114 unsigned char *alpha
= NULL
;
1115 unsigned char *mask
= NULL
;
1123 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1124 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1130 alpha
= image
.GetAlpha() ;
1135 // The following masking algorithm is the same as well in msw/gtk:
1136 // the colour used as transparent one in wxImage and the one it is
1137 // replaced with when it actually occurs in the bitmap
1138 static const int MASK_RED
= 1;
1139 static const int MASK_GREEN
= 2;
1140 static const int MASK_BLUE
= 3;
1141 static const int MASK_BLUE_REPLACEMENT
= 2;
1143 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1145 unsigned char * maskp
= mask
;
1146 unsigned char a
, r
, g
, b
;
1149 for (int xx
= 0; xx
< width
; xx
++)
1151 color
= *((long*) source
) ;
1152 #ifdef WORDS_BIGENDIAN
1153 a
= ((color
&0xFF000000) >> 24) ;
1154 r
= ((color
&0x00FF0000) >> 16) ;
1155 g
= ((color
&0x0000FF00) >> 8) ;
1156 b
= (color
&0x000000FF);
1158 b
= ((color
&0xFF000000) >> 24) ;
1159 g
= ((color
&0x00FF0000) >> 16) ;
1160 r
= ((color
&0x0000FF00) >> 8) ;
1161 a
= (color
&0x000000FF);
1165 if ( *maskp
++ == 0xFF )
1171 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1172 b
= MASK_BLUE_REPLACEMENT
;
1178 else if ( hasAlpha
)
1182 data
[index
+ 1] = g
;
1183 data
[index
+ 2] = b
;
1191 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1196 #endif //wxUSE_IMAGE
1198 bool wxBitmap::SaveFile( const wxString
& filename
,
1199 wxBitmapType type
, const wxPalette
*palette
) const
1201 bool success
= false;
1202 wxBitmapHandler
*handler
= FindHandler(type
);
1206 success
= handler
->SaveFile(this, filename
, type
, palette
);
1211 wxImage image
= ConvertToImage();
1212 success
= image
.SaveFile(filename
, type
);
1214 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1221 bool wxBitmap::IsOk() const
1223 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1226 int wxBitmap::GetHeight() const
1228 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1230 return M_BITMAPDATA
->GetHeight();
1233 int wxBitmap::GetWidth() const
1235 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1237 return M_BITMAPDATA
->GetWidth() ;
1240 int wxBitmap::GetDepth() const
1242 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1244 return M_BITMAPDATA
->GetDepth();
1247 #if WXWIN_COMPATIBILITY_2_4
1248 int wxBitmap::GetQuality() const
1253 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1258 wxMask
*wxBitmap::GetMask() const
1260 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1262 return M_BITMAPDATA
->m_bitmapMask
;
1265 bool wxBitmap::HasAlpha() const
1267 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1269 return M_BITMAPDATA
->HasAlpha() ;
1272 void wxBitmap::SetWidth(int w
)
1275 m_refData
= new wxBitmapRefData
;
1277 M_BITMAPDATA
->SetWidth(w
);
1280 void wxBitmap::SetHeight(int h
)
1283 m_refData
= new wxBitmapRefData
;
1285 M_BITMAPDATA
->SetHeight(h
);
1288 void wxBitmap::SetDepth(int d
)
1291 m_refData
= new wxBitmapRefData
;
1293 M_BITMAPDATA
->SetDepth(d
);
1296 void wxBitmap::SetOk(bool isOk
)
1299 m_refData
= new wxBitmapRefData
;
1301 M_BITMAPDATA
->SetOk(isOk
);
1305 wxPalette
*wxBitmap::GetPalette() const
1307 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1309 return &M_BITMAPDATA
->m_bitmapPalette
;
1312 void wxBitmap::SetPalette(const wxPalette
& palette
)
1315 m_refData
= new wxBitmapRefData
;
1317 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1319 #endif // wxUSE_PALETTE
1321 void wxBitmap::SetMask(wxMask
*mask
)
1324 m_refData
= new wxBitmapRefData
;
1326 // Remove existing mask if there is one.
1327 delete M_BITMAPDATA
->m_bitmapMask
;
1329 M_BITMAPDATA
->m_bitmapMask
= mask
;
1332 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1334 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1337 // ----------------------------------------------------------------------------
1339 // ----------------------------------------------------------------------------
1346 // Construct a mask from a bitmap and a colour indicating
1347 // the transparent area
1348 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1351 Create( bitmap
, colour
);
1354 // Construct a mask from a mono bitmap (copies the bitmap).
1355 wxMask::wxMask( const wxBitmap
& bitmap
)
1361 // Construct a mask from a mono bitmap (copies the bitmap).
1363 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1366 Create( data
, width
, height
, bytesPerRow
);
1373 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1374 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()
1396 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1397 m_maskBitmap
= NULL
;
1400 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1402 OSStatus err
= NewGWorldFromPtr(
1403 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1404 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1405 verify_noerr( err
) ;
1408 // Create a mask from a mono bitmap (copies the bitmap).
1410 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1415 m_bytesPerRow
= bytesPerRow
;
1417 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1424 // Create a mask from a mono bitmap (copies the bitmap).
1425 bool wxMask::Create(const wxBitmap
& bitmap
)
1427 m_width
= bitmap
.GetWidth() ;
1428 m_height
= bitmap
.GetHeight() ;
1429 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1431 size_t size
= m_bytesPerRow
* m_height
;
1432 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1433 wxASSERT( destdatabase
!= NULL
) ;
1435 memset( destdatabase
, 0 , size
) ;
1436 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1438 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1440 unsigned char *destdata
= destdatabase
;
1441 unsigned char r
, g
, b
;
1443 for ( int x
= 0 ; x
< m_width
; ++x
)
1450 if ( ( r
+ g
+ b
) > 0x10 )
1452 *destdata
++ = 0xFF ;
1453 *destdata
++ = 0xFF ;
1454 *destdata
++ = 0xFF ;
1455 *destdata
++ = 0xFF ;
1459 *destdata
++ = 0x00 ;
1460 *destdata
++ = 0x00 ;
1461 *destdata
++ = 0x00 ;
1462 *destdata
++ = 0x00 ;
1467 m_memBuf
.UngetWriteBuf( size
) ;
1473 // Create a mask from a bitmap and a colour indicating
1474 // the transparent area
1475 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1477 m_width
= bitmap
.GetWidth() ;
1478 m_height
= bitmap
.GetHeight() ;
1479 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1481 size_t size
= m_bytesPerRow
* m_height
;
1482 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1483 wxASSERT( destdatabase
!= NULL
) ;
1485 memset( destdatabase
, 0 , size
) ;
1486 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1488 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1490 unsigned char *destdata
= destdatabase
;
1491 unsigned char r
, g
, b
;
1493 for ( int x
= 0 ; x
< m_width
; ++x
)
1500 if ( colour
== wxColour( r
, g
, b
) )
1502 *destdata
++ = 0xFF ;
1503 *destdata
++ = 0xFF ;
1504 *destdata
++ = 0xFF ;
1505 *destdata
++ = 0xFF ;
1509 *destdata
++ = 0x00 ;
1510 *destdata
++ = 0x00 ;
1511 *destdata
++ = 0x00 ;
1512 *destdata
++ = 0x00 ;
1517 m_memBuf
.UngetWriteBuf( size
) ;
1523 WXHBITMAP
wxMask::GetHBITMAP() const
1525 return m_maskBitmap
;
1528 // ----------------------------------------------------------------------------
1530 // ----------------------------------------------------------------------------
1532 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1534 // ----------------------------------------------------------------------------
1535 // Standard Handlers
1536 // ----------------------------------------------------------------------------
1538 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1540 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1543 inline wxPICTResourceHandler()
1545 SetName(wxT("Macintosh Pict resource"));
1546 SetExtension(wxEmptyString
);
1547 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1550 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1551 int desiredWidth
, int desiredHeight
);
1554 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1557 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1558 int desiredWidth
, int desiredHeight
)
1562 wxMacStringToPascal( name
, theName
) ;
1564 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1569 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1570 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1572 dc
.SelectObject( *bitmap
) ;
1574 dc
.SelectObject( wxNullBitmap
) ;
1583 void wxBitmap::InitStandardHandlers()
1585 AddHandler( new wxPICTResourceHandler
) ;
1586 AddHandler( new wxICONResourceHandler
) ;
1589 // ----------------------------------------------------------------------------
1590 // raw bitmap access support
1591 // ----------------------------------------------------------------------------
1593 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1596 // no bitmap, no data (raw or otherwise)
1599 data
.m_width
= GetWidth() ;
1600 data
.m_height
= GetHeight() ;
1601 data
.m_stride
= GetWidth() * 4 ;
1603 return BeginRawAccess() ;
1606 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1611 void wxBitmap::UseAlpha()
1613 // remember that we are using alpha channel:
1614 // we'll need to create a proper mask in UngetRawData()
1615 M_BITMAPDATA
->UseAlpha( true );