1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
18 #include "wx/metafile.h"
19 #include "wx/xpmdecod.h"
21 #include "wx/rawbmp.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
24 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
25 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
28 #include <ApplicationServices/ApplicationServices.h>
30 #include <PictUtils.h>
33 #include "wx/mac/uma.h"
34 #include "wx/dcmemory.h"
36 // Implementation Notes
37 // --------------------
39 // we are always working with a 32 bit deep pixel buffer
40 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
41 // therefore we have a separate GWorld there for blitting the mask in
43 // under Quartz then content is transformed into a CGImageRef representing the same data
44 // which can be transferred to the GPU by the OS for fast rendering
46 // we don't dare use premultiplied alpha yet
47 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
51 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
53 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
56 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
60 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
63 wxBitmapRefData
* bmp
= bmap
;
65 if ( !bmap
->HasNativeSize() )
67 // as PICT conversion will only result in a 16x16 icon, let's attempt
68 // a few scales for better results
70 int w
= bitmap
.GetWidth() ;
71 int h
= bitmap
.GetHeight() ;
72 int sz
= wxMax( w
, h
) ;
73 if ( sz
== 24 || sz
== 64 )
75 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
76 bmp
= scaleBmp
.GetBitmapData() ;
80 info
->contentType
= kControlContentIconRef
;
81 info
->u
.iconRef
= bmp
->GetIconRef() ;
82 AcquireIconRef( info
->u
.iconRef
) ;
84 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
85 else if ( forceType
== kControlContentCGImageRef
)
87 info
->contentType
= kControlContentCGImageRef
;
88 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
93 info
->contentType
= kControlContentPictHandle
;
94 info
->u
.picture
= bmap
->GetPictHandle() ;
99 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
101 if ( info
->contentType
== kControlContentIconRef
)
103 ReleaseIconRef( info
->u
.iconRef
) ;
105 else if ( info
->contentType
== kControlNoContent
)
107 // there's no bitmap at all, fall through silently
109 else if ( info
->contentType
== kControlContentPictHandle
)
111 // owned by the bitmap, no release here
113 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
114 else if ( info
->contentType
== kControlContentCGImageRef
)
116 CGImageRelease( info
->u
.imageRef
) ;
121 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
125 #endif //wxUSE_BMPBUTTON
127 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
129 void wxBitmapRefData::Init()
135 m_bitmapMask
= NULL
;
138 m_cgImageRef
= NULL
;
142 m_pictHandle
= NULL
;
144 m_hMaskBitmap
= NULL
;
145 m_maskBytesPerRow
= 0 ;
147 m_rawAccessCount
= 0 ;
151 wxBitmapRefData::wxBitmapRefData()
156 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
159 Create( w
, h
, d
) ;
162 bool wxBitmapRefData::Create( int w
, int h
, int d
)
168 m_bytesPerRow
= w
* 4 ;
169 size_t size
= m_bytesPerRow
* h
;
170 void* data
= m_memBuf
.GetWriteBuf( size
) ;
171 memset( data
, 0 , size
) ;
172 m_memBuf
.UngetWriteBuf( size
) ;
175 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
177 (char*) data
, m_bytesPerRow
) ) ;
178 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
180 m_ok
= ( m_hBitmap
!= NULL
) ;
185 void wxBitmapRefData::UseAlpha( bool use
)
187 if ( m_hasAlpha
== use
)
193 wxASSERT( m_hMaskBitmap
== NULL
) ;
195 int width
= GetWidth() ;
196 int height
= GetHeight() ;
197 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
198 size_t size
= height
* m_maskBytesPerRow
;
199 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
200 wxASSERT( data
!= NULL
) ;
202 memset( data
, 0 , size
) ;
203 Rect rect
= { 0 , 0 , height
, width
} ;
204 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
205 (char*) data
, m_maskBytesPerRow
) ) ;
206 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
207 m_maskMemBuf
.UngetWriteBuf(size
) ;
209 #if !wxMAC_USE_CORE_GRAPHICS
215 DisposeGWorld( m_hMaskBitmap
) ;
216 m_hMaskBitmap
= NULL
;
217 m_maskBytesPerRow
= 0 ;
221 void *wxBitmapRefData::GetRawAccess() const
223 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
224 return m_memBuf
.GetData() ;
227 void *wxBitmapRefData::BeginRawAccess()
229 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
230 wxASSERT( m_rawAccessCount
== 0 ) ;
231 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
232 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
237 // we must destroy an existing cached image, as
238 // the bitmap data may change now
241 CGImageRelease( m_cgImageRef
) ;
242 m_cgImageRef
= NULL
;
246 return m_memBuf
.GetData() ;
249 void wxBitmapRefData::EndRawAccess()
251 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
252 wxASSERT( m_rawAccessCount
== 1 ) ;
256 #if !wxMAC_USE_CORE_GRAPHICS
261 bool wxBitmapRefData::HasNativeSize()
264 int h
= GetHeight() ;
265 int sz
= wxMax( w
, h
) ;
267 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
270 IconRef
wxBitmapRefData::GetIconRef()
272 if ( m_iconRef
== NULL
)
274 // Create Icon Family Handle
276 IconFamilyHandle iconFamily
= NULL
;
278 #ifdef WORDS_BIGENDIAN
279 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
280 (**iconFamily
).resourceType
= kIconFamilyType
;
281 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
283 // test this solution on big endian as well
284 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
288 int h
= GetHeight() ;
289 int sz
= wxMax( w
, h
) ;
291 OSType dataType
= 0 ;
292 OSType maskType
= 0 ;
297 dataType
= kThumbnail32BitData
;
298 maskType
= kThumbnail8BitMask
;
302 dataType
= kHuge32BitData
;
303 maskType
= kHuge8BitMask
;
307 dataType
= kLarge32BitData
;
308 maskType
= kLarge8BitMask
;
312 dataType
= kSmall32BitData
;
313 maskType
= kSmall8BitMask
;
322 // setup the header properly
325 Handle maskdata
= NULL
;
326 unsigned char * maskptr
= NULL
;
327 unsigned char * ptr
= NULL
;
328 size_t datasize
, masksize
;
330 datasize
= sz
* sz
* 4 ;
331 data
= NewHandle( datasize
) ;
333 ptr
= (unsigned char*) *data
;
334 memset( ptr
, 0, datasize
) ;
337 maskdata
= NewHandle( masksize
) ;
339 maskptr
= (unsigned char*) *maskdata
;
340 memset( maskptr
, 0 , masksize
) ;
342 bool hasAlpha
= HasAlpha() ;
343 wxMask
*mask
= m_bitmapMask
;
344 unsigned char * source
= (unsigned char*) GetRawAccess() ;
345 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
347 for ( int y
= 0 ; y
< h
; ++y
)
349 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
350 unsigned char * maskdest
= maskptr
+ y
* sz
;
351 unsigned char a
, r
, g
, b
;
353 for ( int x
= 0 ; x
< w
; ++x
)
367 *maskdest
++ = 0xFF - *masksource
++ ;
379 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
380 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
382 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
383 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
386 HUnlock( maskdata
) ;
387 DisposeHandle( data
) ;
388 DisposeHandle( maskdata
) ;
392 PicHandle pic
= GetPictHandle() ;
393 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
396 // transform into IconRef
398 static int iconCounter
= 2 ;
400 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
401 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
403 // we have to retain a reference, as Unregister will decrement it
404 AcquireIconRef( m_iconRef
) ;
405 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
406 DisposeHandle( (Handle
) iconFamily
) ;
413 PicHandle
wxBitmapRefData::GetPictHandle()
415 if ( m_pictHandle
== NULL
)
417 CGrafPtr origPort
= NULL
;
418 GDHandle origDev
= NULL
;
419 GWorldPtr wp
= NULL
;
420 GWorldPtr mask
= NULL
;
421 int height
= GetHeight() ;
422 int width
= GetWidth() ;
424 Rect rect
= { 0 , 0 , height
, width
} ;
425 RgnHandle clipRgn
= NULL
;
427 GetGWorld( &origPort
, &origDev
) ;
428 wp
= GetHBITMAP( &mask
) ;
432 GWorldPtr monoworld
;
434 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
436 LockPixels( GetGWorldPixMap( monoworld
) ) ;
437 LockPixels( GetGWorldPixMap( mask
) ) ;
438 SetGWorld( monoworld
, NULL
) ;
440 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
441 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
442 RGBForeColor( &black
) ;
443 RGBBackColor( &white
) ;
445 CopyBits(GetPortBitMapForCopyBits(mask
),
446 GetPortBitMapForCopyBits(monoworld
),
450 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
452 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
453 UnlockPixels( GetGWorldPixMap( mask
) ) ;
454 DisposeGWorld( monoworld
) ;
457 SetGWorld( wp
, NULL
) ;
459 GetPortBounds( wp
, &portRect
) ;
460 m_pictHandle
= OpenPicture(&portRect
);
464 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
465 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
467 RGBForeColor( &black
) ;
468 RGBBackColor( &white
) ;
473 LockPixels( GetGWorldPixMap( wp
) ) ;
474 CopyBits(GetPortBitMapForCopyBits(wp
),
475 GetPortBitMapForCopyBits(wp
),
479 UnlockPixels( GetGWorldPixMap( wp
) ) ;
483 SetGWorld( origPort
, origDev
) ;
485 DisposeRgn( clipRgn
) ;
488 return m_pictHandle
;
492 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
494 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
496 wxASSERT( data
== membuf
->GetData() ) ;
501 CGImageRef
wxBitmapRefData::CGImageCreate() const
504 wxASSERT( m_rawAccessCount
>= 0 ) ;
506 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
508 size_t imageSize
= m_width
* m_height
* 4 ;
509 void * dataBuffer
= m_memBuf
.GetData() ;
512 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
513 wxMemoryBuffer
* membuf
= NULL
;
517 alphaInfo
= kCGImageAlphaFirst
;
518 membuf
= new wxMemoryBuffer( imageSize
) ;
519 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
520 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
521 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
522 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
523 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
525 unsigned char *sourcemask
= sourcemaskstart
;
526 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
528 *destalpha
= 0xFF - *sourcemask
;
536 #if wxMAC_USE_PREMULTIPLIED_ALPHA
537 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
539 alphaInfo
= kCGImageAlphaFirst
;
543 membuf
= new wxMemoryBuffer( m_memBuf
) ;
546 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
547 CGDataProviderRef dataProvider
=
548 CGDataProviderCreateWithData(
549 membuf
, (const void *)membuf
->GetData() , imageSize
,
550 wxMacMemoryBufferReleaseProc
);
553 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
554 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
555 CGDataProviderRelease( dataProvider
);
559 image
= m_cgImageRef
;
560 CGImageRetain( image
) ;
563 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
565 // we keep it for later use
566 m_cgImageRef
= image
;
567 CGImageRetain( image
) ;
574 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
576 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
582 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
584 else if ( m_hasAlpha
)
586 #if !wxMAC_USE_CORE_GRAPHICS
587 if ( m_rawAccessCount
> 0 )
590 // this structure is not kept in synch when using CG, so if something
591 // is really accessing the GrafPorts, we have to sync it
595 *mask
= m_hMaskBitmap
;
602 void wxBitmapRefData::UpdateAlphaMask() const
606 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
607 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
609 int h
= GetHeight() ;
612 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
614 unsigned char* destalpha
= destalphabase
;
616 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
618 // we must have 24 bit depth for non quartz smooth alpha
620 *destalpha
++ = 255 - *sourcemask
;
621 *destalpha
++ = 255 - *sourcemask
;
622 *destalpha
++ = 255 - *sourcemask
;
628 void wxBitmapRefData::Free()
630 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
635 CGImageRelease( m_cgImageRef
) ;
636 m_cgImageRef
= NULL
;
642 ReleaseIconRef( m_iconRef
) ;
648 KillPicture( m_pictHandle
) ;
649 m_pictHandle
= NULL
;
654 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
660 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
661 m_hMaskBitmap
= NULL
;
671 wxBitmapRefData::~wxBitmapRefData()
676 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
678 bool created
= false ;
679 int w
= icon
.GetWidth() ;
680 int h
= icon
.GetHeight() ;
682 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
684 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
686 IconFamilyHandle iconFamily
= NULL
;
687 Handle imagehandle
= NewHandle( 0 ) ;
688 Handle maskhandle
= NewHandle( 0 ) ;
692 IconSelectorValue selector
= 0 ;
697 dataType
= kThumbnail32BitData
;
698 maskType
= kThumbnail8BitMask
;
699 selector
= kSelectorAllAvailableData
;
703 dataType
= kHuge32BitData
;
704 maskType
= kHuge8BitMask
;
705 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
709 dataType
= kLarge32BitData
;
710 maskType
= kLarge8BitMask
;
711 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
715 dataType
= kSmall32BitData
;
716 maskType
= kSmall8BitMask
;
717 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
724 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
726 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
727 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
728 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
729 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
731 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
733 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
734 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
738 unsigned char *source
= (unsigned char *) *imagehandle
;
739 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
740 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
742 for ( int y
= 0 ; y
< h
; ++y
)
744 for ( int x
= 0 ; x
< w
; ++x
)
746 *destination
++ = *sourcemask
++ ;
748 *destination
++ = *source
++ ;
749 *destination
++ = *source
++ ;
750 *destination
++ = *source
++ ;
755 DisposeHandle( imagehandle
) ;
756 DisposeHandle( maskhandle
) ;
760 DisposeHandle( (Handle
) iconFamily
) ;
766 dc
.SelectObject( *this ) ;
767 dc
.DrawIcon( icon
, 0 , 0 ) ;
768 dc
.SelectObject( wxNullBitmap
) ;
778 wxBitmap::~wxBitmap()
782 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
784 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
788 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
789 if ( the_width
% (sizeof(unsigned char) * 8) )
790 linesize
+= sizeof(unsigned char);
792 unsigned char* linestart
= (unsigned char*) bits
;
793 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
795 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
797 int index
, bit
, mask
;
799 for ( int x
= 0 ; x
< the_width
; ++x
)
805 if ( !(linestart
[index
] & mask
) )
807 *destination
++ = 0xFF ;
814 *destination
++ = 0xFF ;
815 *destination
++ = 0xFF ;
816 *destination
++ = 0xFF ;
817 *destination
++ = 0xFF ;
826 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
830 wxBitmap::wxBitmap(int w
, int h
, int d
)
832 (void)Create(w
, h
, d
);
835 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
837 (void) Create(data
, type
, width
, height
, depth
);
840 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
842 LoadFile(filename
, type
);
845 wxBitmap::wxBitmap(const char **bits
)
847 (void) CreateFromXpm(bits
);
850 wxBitmap::wxBitmap(char **bits
)
852 (void) CreateFromXpm((const char **)bits
);
855 void * wxBitmap::GetRawAccess() const
857 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
859 return M_BITMAPDATA
->GetRawAccess() ;
862 void * wxBitmap::BeginRawAccess()
864 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
866 return M_BITMAPDATA
->BeginRawAccess() ;
869 void wxBitmap::EndRawAccess()
871 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
873 M_BITMAPDATA
->EndRawAccess() ;
876 bool wxBitmap::CreateFromXpm(const char **bits
)
879 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
881 wxXPMDecoder decoder
;
882 wxImage img
= decoder
.ReadData(bits
);
883 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
885 *this = wxBitmap(img
);
895 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
897 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
899 return M_BITMAPDATA
->CGImageCreate() ;
903 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
906 (rect
.x
>= 0) && (rect
.y
>= 0) &&
907 (rect
.x
+rect
.width
<= GetWidth()) &&
908 (rect
.y
+rect
.height
<= GetHeight()),
909 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
911 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
912 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
914 int sourcewidth
= GetWidth() ;
915 int destwidth
= rect
.width
;
916 int destheight
= rect
.height
;
919 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
920 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
921 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
923 int sourcelinesize
= sourcewidth
* 4 ;
924 int destlinesize
= destwidth
* 4 ;
925 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
926 unsigned char *dest
= destdata
;
928 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
930 memcpy( dest
, source
, destlinesize
) ;
936 if ( M_BITMAPDATA
->m_bitmapMask
)
938 wxMemoryBuffer maskbuf
;
939 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
940 size_t maskbufsize
= rowBytes
* destheight
;
942 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
943 int destlinesize
= rowBytes
;
945 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
946 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
947 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
949 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
950 unsigned char *dest
= destdata
;
952 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
954 memcpy( dest
, source
, destlinesize
) ;
957 maskbuf
.UngetWriteBuf( maskbufsize
) ;
958 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
960 else if ( HasAlpha() )
966 bool wxBitmap::Create(int w
, int h
, int d
)
971 d
= wxDisplayDepth() ;
973 m_refData
= new wxBitmapRefData( w
, h
, d
);
975 return M_BITMAPDATA
->Ok() ;
978 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
982 wxBitmapHandler
*handler
= FindHandler(type
);
986 m_refData
= new wxBitmapRefData
;
988 return handler
->LoadFile(this, filename
, type
, -1, -1);
993 wxImage
loadimage(filename
, type
);
1003 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1008 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
1012 m_refData
= new wxBitmapRefData
;
1014 wxBitmapHandler
*handler
= FindHandler(type
);
1016 if ( handler
== NULL
)
1018 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1023 return handler
->Create(this, data
, type
, width
, height
, depth
);
1028 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1030 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
1032 // width and height of the device-dependent bitmap
1033 int width
= image
.GetWidth();
1034 int height
= image
.GetHeight();
1036 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1040 bool hasAlpha
= false ;
1042 if ( image
.HasMask() )
1044 // takes precedence, don't mix with alpha info
1048 hasAlpha
= image
.HasAlpha() ;
1054 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1055 register unsigned char* data
= image
.GetData();
1056 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1058 for (int y
= 0; y
< height
; y
++)
1060 for (int x
= 0; x
< width
; x
++)
1064 const unsigned char a
= *alpha
++;
1065 *destination
++ = a
;
1067 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1068 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1069 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1070 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1072 *destination
++ = *data
++ ;
1073 *destination
++ = *data
++ ;
1074 *destination
++ = *data
++ ;
1079 *destination
++ = 0xFF ;
1080 *destination
++ = *data
++ ;
1081 *destination
++ = *data
++ ;
1082 *destination
++ = *data
++ ;
1088 if ( image
.HasMask() )
1089 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1092 wxImage
wxBitmap::ConvertToImage() const
1096 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1098 // create an wxImage object
1099 int width
= GetWidth();
1100 int height
= GetHeight();
1101 image
.Create( width
, height
);
1103 unsigned char *data
= image
.GetData();
1104 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1106 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1108 bool hasAlpha
= false ;
1109 bool hasMask
= false ;
1110 int maskBytesPerRow
= 0 ;
1111 unsigned char *alpha
= NULL
;
1112 unsigned char *mask
= NULL
;
1120 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1121 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1127 alpha
= image
.GetAlpha() ;
1132 // The following masking algorithm is the same as well in msw/gtk:
1133 // the colour used as transparent one in wxImage and the one it is
1134 // replaced with when it actually occurs in the bitmap
1135 static const int MASK_RED
= 1;
1136 static const int MASK_GREEN
= 2;
1137 static const int MASK_BLUE
= 3;
1138 static const int MASK_BLUE_REPLACEMENT
= 2;
1140 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1142 unsigned char * maskp
= mask
;
1143 unsigned char a
, r
, g
, b
;
1146 for (int xx
= 0; xx
< width
; xx
++)
1148 color
= *((long*) source
) ;
1149 a
= ((color
&0xFF000000) >> 24) ;
1150 r
= ((color
&0x00FF0000) >> 16) ;
1151 g
= ((color
&0x0000FF00) >> 8) ;
1152 b
= (color
&0x000000FF);
1156 if ( *maskp
++ == 0xFF )
1162 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1163 b
= MASK_BLUE_REPLACEMENT
;
1169 else if ( hasAlpha
)
1173 data
[index
+ 1] = g
;
1174 data
[index
+ 2] = b
;
1182 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1187 #endif //wxUSE_IMAGE
1189 bool wxBitmap::SaveFile( const wxString
& filename
,
1190 wxBitmapType type
, const wxPalette
*palette
) const
1192 bool success
= false;
1193 wxBitmapHandler
*handler
= FindHandler(type
);
1197 success
= handler
->SaveFile(this, filename
, type
, palette
);
1202 wxImage image
= ConvertToImage();
1203 success
= image
.SaveFile(filename
, type
);
1205 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1212 bool wxBitmap::Ok() const
1214 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1217 int wxBitmap::GetHeight() const
1219 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1221 return M_BITMAPDATA
->GetHeight();
1224 int wxBitmap::GetWidth() const
1226 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1228 return M_BITMAPDATA
->GetWidth() ;
1231 int wxBitmap::GetDepth() const
1233 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1235 return M_BITMAPDATA
->GetDepth();
1238 #if WXWIN_COMPATIBILITY_2_4
1239 int wxBitmap::GetQuality() const
1244 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1249 wxMask
*wxBitmap::GetMask() const
1251 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1253 return M_BITMAPDATA
->m_bitmapMask
;
1256 bool wxBitmap::HasAlpha() const
1258 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1260 return M_BITMAPDATA
->HasAlpha() ;
1263 void wxBitmap::SetWidth(int w
)
1266 m_refData
= new wxBitmapRefData
;
1268 M_BITMAPDATA
->SetWidth(w
);
1271 void wxBitmap::SetHeight(int h
)
1274 m_refData
= new wxBitmapRefData
;
1276 M_BITMAPDATA
->SetHeight(h
);
1279 void wxBitmap::SetDepth(int d
)
1282 m_refData
= new wxBitmapRefData
;
1284 M_BITMAPDATA
->SetDepth(d
);
1287 void wxBitmap::SetOk(bool isOk
)
1290 m_refData
= new wxBitmapRefData
;
1292 M_BITMAPDATA
->SetOk(isOk
);
1296 wxPalette
*wxBitmap::GetPalette() const
1298 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1300 return &M_BITMAPDATA
->m_bitmapPalette
;
1303 void wxBitmap::SetPalette(const wxPalette
& palette
)
1306 m_refData
= new wxBitmapRefData
;
1308 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1310 #endif // wxUSE_PALETTE
1312 void wxBitmap::SetMask(wxMask
*mask
)
1315 m_refData
= new wxBitmapRefData
;
1317 // Remove existing mask if there is one.
1318 delete M_BITMAPDATA
->m_bitmapMask
;
1320 M_BITMAPDATA
->m_bitmapMask
= mask
;
1323 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1325 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1328 // ----------------------------------------------------------------------------
1330 // ----------------------------------------------------------------------------
1337 // Construct a mask from a bitmap and a colour indicating
1338 // the transparent area
1339 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1342 Create( bitmap
, colour
);
1345 // Construct a mask from a mono bitmap (copies the bitmap).
1346 wxMask::wxMask( const wxBitmap
& bitmap
)
1352 // Construct a mask from a mono bitmap (copies the bitmap).
1354 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1357 Create( data
, width
, height
, bytesPerRow
);
1364 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1365 m_maskBitmap
= NULL
;
1371 m_width
= m_height
= m_bytesPerRow
= 0 ;
1372 m_maskBitmap
= NULL
;
1375 void *wxMask::GetRawAccess() const
1377 return m_memBuf
.GetData() ;
1380 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1381 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1383 void wxMask::RealizeNative()
1387 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1388 m_maskBitmap
= NULL
;
1391 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1393 OSStatus err
= NewGWorldFromPtr(
1394 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1395 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1396 verify_noerr( err
) ;
1399 // Create a mask from a mono bitmap (copies the bitmap).
1401 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1406 m_bytesPerRow
= bytesPerRow
;
1408 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1415 // Create a mask from a mono bitmap (copies the bitmap).
1416 bool wxMask::Create(const wxBitmap
& bitmap
)
1418 m_width
= bitmap
.GetWidth() ;
1419 m_height
= bitmap
.GetHeight() ;
1420 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1422 size_t size
= m_bytesPerRow
* m_height
;
1423 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1424 wxASSERT( destdatabase
!= NULL
) ;
1426 memset( destdatabase
, 0 , size
) ;
1427 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1429 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1431 unsigned char *destdata
= destdatabase
;
1432 unsigned char r
, g
, b
;
1434 for ( int x
= 0 ; x
< m_width
; ++x
)
1441 if ( ( r
+ g
+ b
) > 0x10 )
1443 *destdata
++ = 0xFF ;
1444 *destdata
++ = 0xFF ;
1445 *destdata
++ = 0xFF ;
1446 *destdata
++ = 0xFF ;
1450 *destdata
++ = 0x00 ;
1451 *destdata
++ = 0x00 ;
1452 *destdata
++ = 0x00 ;
1453 *destdata
++ = 0x00 ;
1458 m_memBuf
.UngetWriteBuf( size
) ;
1464 // Create a mask from a bitmap and a colour indicating
1465 // the transparent area
1466 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1468 m_width
= bitmap
.GetWidth() ;
1469 m_height
= bitmap
.GetHeight() ;
1470 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1472 size_t size
= m_bytesPerRow
* m_height
;
1473 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1474 wxASSERT( destdatabase
!= NULL
) ;
1476 memset( destdatabase
, 0 , size
) ;
1477 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1479 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1481 unsigned char *destdata
= destdatabase
;
1482 unsigned char r
, g
, b
;
1484 for ( int x
= 0 ; x
< m_width
; ++x
)
1491 if ( colour
== wxColour( r
, g
, b
) )
1493 *destdata
++ = 0xFF ;
1494 *destdata
++ = 0xFF ;
1495 *destdata
++ = 0xFF ;
1496 *destdata
++ = 0xFF ;
1500 *destdata
++ = 0x00 ;
1501 *destdata
++ = 0x00 ;
1502 *destdata
++ = 0x00 ;
1503 *destdata
++ = 0x00 ;
1508 m_memBuf
.UngetWriteBuf( size
) ;
1514 WXHBITMAP
wxMask::GetHBITMAP() const
1516 return m_maskBitmap
;
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1523 wxBitmapHandler::~wxBitmapHandler()
1527 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1532 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1533 int desiredWidth
, int desiredHeight
)
1538 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1543 // ----------------------------------------------------------------------------
1544 // Standard Handlers
1545 // ----------------------------------------------------------------------------
1547 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1549 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1552 inline wxPICTResourceHandler()
1554 SetName(wxT("Macintosh Pict resource"));
1555 SetExtension(wxEmptyString
);
1556 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1559 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1560 int desiredWidth
, int desiredHeight
);
1563 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1566 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1567 int desiredWidth
, int desiredHeight
)
1571 wxMacStringToPascal( name
, theName
) ;
1573 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1578 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1579 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1581 dc
.SelectObject( *bitmap
) ;
1583 dc
.SelectObject( wxNullBitmap
) ;
1592 void wxBitmap::InitStandardHandlers()
1594 AddHandler( new wxPICTResourceHandler
) ;
1595 AddHandler( new wxICONResourceHandler
) ;
1598 // ----------------------------------------------------------------------------
1599 // raw bitmap access support
1600 // ----------------------------------------------------------------------------
1602 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1605 // no bitmap, no data (raw or otherwise)
1608 data
.m_width
= GetWidth() ;
1609 data
.m_height
= GetHeight() ;
1610 data
.m_stride
= GetWidth() * 4 ;
1612 return GetRawAccess() ;
1615 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1620 // TODO: if we have some information about the API we should check
1621 // this code looks strange...
1623 if ( !M_BITMAPDATA
->HasAlpha() )
1626 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1627 int w
= data
.GetWidth();
1628 int h
= data
.GetHeight();
1630 wxBitmap
bmpMask( GetWidth(), GetHeight(), 32 );
1631 wxAlphaPixelData
dataMask( bmpMask
, data
.GetOrigin(), wxSize( w
, h
) );
1632 wxAlphaPixelData::Iterator
pMask( dataMask
), p( data
);
1634 for ( int y
= 0; y
< h
; y
++ )
1636 wxAlphaPixelData::Iterator rowStartMask
= pMask
;
1637 wxAlphaPixelData::Iterator rowStart
= p
;
1639 for ( int x
= 0; x
< w
; x
++ )
1641 const wxAlphaPixelData::Iterator::ChannelType alpha
= p
.Alpha();
1643 pMask
.Red() = alpha
;
1644 pMask
.Green() = alpha
;
1645 pMask
.Blue() = alpha
;
1652 p
.OffsetY( data
, 1 );
1654 pMask
= rowStartMask
;
1655 pMask
.OffsetY( dataMask
, 1 );
1658 SetMask( new wxMask( bmpMask
) );
1661 void wxBitmap::UseAlpha()
1663 // remember that we are using alpha channel:
1664 // we'll need to create a proper mask in UngetRawData()
1665 M_BITMAPDATA
->UseAlpha( true );