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
)
366 *maskdest
++ = *masksource
++ ;
374 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
375 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
377 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
378 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
381 HUnlock( maskdata
) ;
382 DisposeHandle( data
) ;
383 DisposeHandle( maskdata
) ;
387 PicHandle pic
= GetPictHandle() ;
388 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
391 // transform into IconRef
393 static int iconCounter
= 2 ;
395 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
396 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
398 // we have to retain a reference, as Unregister will decrement it
399 AcquireIconRef( m_iconRef
) ;
400 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
401 DisposeHandle( (Handle
) iconFamily
) ;
408 PicHandle
wxBitmapRefData::GetPictHandle()
410 if ( m_pictHandle
== NULL
)
412 CGrafPtr origPort
= NULL
;
413 GDHandle origDev
= NULL
;
414 GWorldPtr wp
= NULL
;
415 GWorldPtr mask
= NULL
;
416 int height
= GetHeight() ;
417 int width
= GetWidth() ;
419 Rect rect
= { 0 , 0 , height
, width
} ;
420 RgnHandle clipRgn
= NULL
;
422 GetGWorld( &origPort
, &origDev
) ;
423 wp
= GetHBITMAP( &mask
) ;
427 GWorldPtr monoworld
;
429 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
431 LockPixels( GetGWorldPixMap( monoworld
) ) ;
432 LockPixels( GetGWorldPixMap( mask
) ) ;
433 SetGWorld( monoworld
, NULL
) ;
435 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
436 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
437 RGBForeColor( &black
) ;
438 RGBBackColor( &white
) ;
440 CopyBits(GetPortBitMapForCopyBits(mask
),
441 GetPortBitMapForCopyBits(monoworld
),
445 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
447 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
448 UnlockPixels( GetGWorldPixMap( mask
) ) ;
449 DisposeGWorld( monoworld
) ;
452 SetGWorld( wp
, NULL
) ;
454 GetPortBounds( wp
, &portRect
) ;
455 m_pictHandle
= OpenPicture(&portRect
);
459 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
460 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
462 RGBForeColor( &black
) ;
463 RGBBackColor( &white
) ;
468 LockPixels( GetGWorldPixMap( wp
) ) ;
469 CopyBits(GetPortBitMapForCopyBits(wp
),
470 GetPortBitMapForCopyBits(wp
),
474 UnlockPixels( GetGWorldPixMap( wp
) ) ;
478 SetGWorld( origPort
, origDev
) ;
480 DisposeRgn( clipRgn
) ;
483 return m_pictHandle
;
487 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
489 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
491 wxASSERT( data
== membuf
->GetData() ) ;
496 CGImageRef
wxBitmapRefData::CGImageCreate() const
499 wxASSERT( m_rawAccessCount
>= 0 ) ;
501 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
503 size_t imageSize
= m_width
* m_height
* 4 ;
504 void * dataBuffer
= m_memBuf
.GetData() ;
507 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
508 wxMemoryBuffer
* membuf
= NULL
;
512 alphaInfo
= kCGImageAlphaFirst
;
513 membuf
= new wxMemoryBuffer( imageSize
) ;
514 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
515 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
516 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
517 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
518 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
520 unsigned char *sourcemask
= sourcemaskstart
;
521 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
523 *destalpha
= *sourcemask
;
531 #if wxMAC_USE_PREMULTIPLIED_ALPHA
532 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
534 alphaInfo
= kCGImageAlphaFirst
;
538 membuf
= new wxMemoryBuffer( m_memBuf
) ;
541 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
542 CGDataProviderRef dataProvider
=
543 CGDataProviderCreateWithData(
544 membuf
, (const void *)membuf
->GetData() , imageSize
,
545 wxMacMemoryBufferReleaseProc
);
548 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
549 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
550 CGDataProviderRelease( dataProvider
);
554 image
= m_cgImageRef
;
555 CGImageRetain( image
) ;
558 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
560 // we keep it for later use
561 m_cgImageRef
= image
;
562 CGImageRetain( image
) ;
569 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
571 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
577 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
579 else if ( m_hasAlpha
)
581 #if !wxMAC_USE_CORE_GRAPHICS
582 if ( m_rawAccessCount
> 0 )
585 // this structure is not kept in synch when using CG, so if something
586 // is really accessing the GrafPorts, we have to sync it
590 *mask
= m_hMaskBitmap
;
597 void wxBitmapRefData::UpdateAlphaMask() const
601 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
602 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
604 int h
= GetHeight() ;
607 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
609 unsigned char* destalpha
= destalphabase
;
611 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
613 // we must have 24 bit depth for non quartz smooth alpha
615 *destalpha
++ = 255 - *sourcemask
;
616 *destalpha
++ = 255 - *sourcemask
;
617 *destalpha
++ = 255 - *sourcemask
;
623 void wxBitmapRefData::Free()
625 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
630 CGImageRelease( m_cgImageRef
) ;
631 m_cgImageRef
= NULL
;
637 ReleaseIconRef( m_iconRef
) ;
643 KillPicture( m_pictHandle
) ;
644 m_pictHandle
= NULL
;
649 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
655 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
656 m_hMaskBitmap
= NULL
;
666 wxBitmapRefData::~wxBitmapRefData()
671 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
673 bool created
= false ;
674 int w
= icon
.GetWidth() ;
675 int h
= icon
.GetHeight() ;
677 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
679 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
681 IconFamilyHandle iconFamily
= NULL
;
682 Handle imagehandle
= NewHandle( 0 ) ;
683 Handle maskhandle
= NewHandle( 0 ) ;
687 IconSelectorValue selector
= 0 ;
692 dataType
= kThumbnail32BitData
;
693 maskType
= kThumbnail8BitMask
;
694 selector
= kSelectorAllAvailableData
;
698 dataType
= kHuge32BitData
;
699 maskType
= kHuge8BitMask
;
700 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
704 dataType
= kLarge32BitData
;
705 maskType
= kLarge8BitMask
;
706 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
710 dataType
= kSmall32BitData
;
711 maskType
= kSmall8BitMask
;
712 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
719 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
721 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
722 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
723 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
724 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
726 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
728 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
729 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
733 unsigned char *source
= (unsigned char *) *imagehandle
;
734 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
735 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
737 for ( int y
= 0 ; y
< h
; ++y
)
739 for ( int x
= 0 ; x
< w
; ++x
)
741 *destination
++ = *sourcemask
++ ;
743 *destination
++ = *source
++ ;
744 *destination
++ = *source
++ ;
745 *destination
++ = *source
++ ;
750 DisposeHandle( imagehandle
) ;
751 DisposeHandle( maskhandle
) ;
755 DisposeHandle( (Handle
) iconFamily
) ;
761 dc
.SelectObject( *this ) ;
762 dc
.DrawIcon( icon
, 0 , 0 ) ;
763 dc
.SelectObject( wxNullBitmap
) ;
773 wxBitmap::~wxBitmap()
777 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
779 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
783 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
784 if ( the_width
% (sizeof(unsigned char) * 8) )
785 linesize
+= sizeof(unsigned char);
787 unsigned char* linestart
= (unsigned char*) bits
;
788 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
790 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
792 int index
, bit
, mask
;
794 for ( int x
= 0 ; x
< the_width
; ++x
)
800 if ( linestart
[index
] & mask
)
802 *destination
++ = 0xFF ;
809 *destination
++ = 0xFF ;
810 *destination
++ = 0xFF ;
811 *destination
++ = 0xFF ;
812 *destination
++ = 0xFF ;
821 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
825 wxBitmap::wxBitmap(int w
, int h
, int d
)
827 (void)Create(w
, h
, d
);
830 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
832 (void) Create(data
, type
, width
, height
, depth
);
835 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
837 LoadFile(filename
, type
);
840 wxBitmap::wxBitmap(const char **bits
)
842 (void) CreateFromXpm(bits
);
845 wxBitmap::wxBitmap(char **bits
)
847 (void) CreateFromXpm((const char **)bits
);
850 void * wxBitmap::GetRawAccess() const
852 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
854 return M_BITMAPDATA
->GetRawAccess() ;
857 void * wxBitmap::BeginRawAccess()
859 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
861 return M_BITMAPDATA
->BeginRawAccess() ;
864 void wxBitmap::EndRawAccess()
866 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
868 M_BITMAPDATA
->EndRawAccess() ;
871 bool wxBitmap::CreateFromXpm(const char **bits
)
874 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
876 wxXPMDecoder decoder
;
877 wxImage img
= decoder
.ReadData(bits
);
878 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
880 *this = wxBitmap(img
);
890 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
892 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
894 return M_BITMAPDATA
->CGImageCreate() ;
898 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
901 (rect
.x
>= 0) && (rect
.y
>= 0) &&
902 (rect
.x
+rect
.width
<= GetWidth()) &&
903 (rect
.y
+rect
.height
<= GetHeight()),
904 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
906 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
907 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
909 int sourcewidth
= GetWidth() ;
910 int destwidth
= rect
.width
;
911 int destheight
= rect
.height
;
914 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
915 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
916 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
918 int sourcelinesize
= sourcewidth
* 4 ;
919 int destlinesize
= destwidth
* 4 ;
920 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
921 unsigned char *dest
= destdata
;
923 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
925 memcpy( dest
, source
, destlinesize
) ;
931 if ( M_BITMAPDATA
->m_bitmapMask
)
933 wxMemoryBuffer maskbuf
;
934 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
935 size_t maskbufsize
= rowBytes
* destheight
;
937 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
938 int destlinesize
= rowBytes
;
940 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
941 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
942 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
944 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
945 unsigned char *dest
= destdata
;
947 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
949 memcpy( dest
, source
, destlinesize
) ;
952 maskbuf
.UngetWriteBuf( maskbufsize
) ;
953 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
955 else if ( HasAlpha() )
961 bool wxBitmap::Create(int w
, int h
, int d
)
966 d
= wxDisplayDepth() ;
968 m_refData
= new wxBitmapRefData( w
, h
, d
);
970 return M_BITMAPDATA
->Ok() ;
973 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
977 wxBitmapHandler
*handler
= FindHandler(type
);
981 m_refData
= new wxBitmapRefData
;
983 return handler
->LoadFile(this, filename
, type
, -1, -1);
988 wxImage
loadimage(filename
, type
);
998 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1003 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
1007 m_refData
= new wxBitmapRefData
;
1009 wxBitmapHandler
*handler
= FindHandler(type
);
1011 if ( handler
== NULL
)
1013 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1018 return handler
->Create(this, data
, type
, width
, height
, depth
);
1023 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1025 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
1027 // width and height of the device-dependent bitmap
1028 int width
= image
.GetWidth();
1029 int height
= image
.GetHeight();
1031 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1035 bool hasAlpha
= false ;
1037 if ( image
.HasMask() )
1039 // takes precedence, don't mix with alpha info
1043 hasAlpha
= image
.HasAlpha() ;
1049 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1050 register unsigned char* data
= image
.GetData();
1051 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1053 for (int y
= 0; y
< height
; y
++)
1055 for (int x
= 0; x
< width
; x
++)
1059 const unsigned char a
= *alpha
++;
1060 *destination
++ = a
;
1062 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1063 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1064 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1065 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1067 *destination
++ = *data
++ ;
1068 *destination
++ = *data
++ ;
1069 *destination
++ = *data
++ ;
1074 *destination
++ = 0xFF ;
1075 *destination
++ = *data
++ ;
1076 *destination
++ = *data
++ ;
1077 *destination
++ = *data
++ ;
1083 if ( image
.HasMask() )
1084 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1087 wxImage
wxBitmap::ConvertToImage() const
1091 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1093 // create an wxImage object
1094 int width
= GetWidth();
1095 int height
= GetHeight();
1096 image
.Create( width
, height
);
1098 unsigned char *data
= image
.GetData();
1099 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1101 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1103 bool hasAlpha
= false ;
1104 bool hasMask
= false ;
1105 int maskBytesPerRow
= 0 ;
1106 unsigned char *alpha
= NULL
;
1107 unsigned char *mask
= NULL
;
1115 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1116 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1122 alpha
= image
.GetAlpha() ;
1127 // The following masking algorithm is the same as well in msw/gtk:
1128 // the colour used as transparent one in wxImage and the one it is
1129 // replaced with when it really occurs in the bitmap
1130 static const int MASK_RED
= 1;
1131 static const int MASK_GREEN
= 2;
1132 static const int MASK_BLUE
= 3;
1133 static const int MASK_BLUE_REPLACEMENT
= 2;
1135 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1137 unsigned char * maskp
= mask
;
1138 unsigned char a
, r
, g
, b
;
1141 for (int xx
= 0; xx
< width
; xx
++)
1143 color
= *((long*) source
) ;
1144 a
= ((color
&0xFF000000) >> 24) ;
1145 r
= ((color
&0x00FF0000) >> 16) ;
1146 g
= ((color
&0x0000FF00) >> 8) ;
1147 b
= (color
&0x000000FF);
1151 if ( *maskp
++ == 0 )
1157 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1158 b
= MASK_BLUE_REPLACEMENT
;
1160 else if ( hasAlpha
)
1164 data
[index
+ 1] = g
;
1165 data
[index
+ 2] = b
;
1173 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1178 #endif //wxUSE_IMAGE
1180 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1181 const wxPalette
*palette
) const
1183 bool success
= false;
1184 wxBitmapHandler
*handler
= FindHandler(type
);
1188 success
= handler
->SaveFile(this, filename
, type
, palette
);
1193 wxImage image
= ConvertToImage();
1194 success
= image
.SaveFile(filename
, type
);
1196 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1203 bool wxBitmap::Ok() const
1205 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1208 int wxBitmap::GetHeight() const
1210 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1212 return M_BITMAPDATA
->GetHeight();
1215 int wxBitmap::GetWidth() const
1217 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1219 return M_BITMAPDATA
->GetWidth() ;
1222 int wxBitmap::GetDepth() const
1224 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1226 return M_BITMAPDATA
->GetDepth();
1229 #if WXWIN_COMPATIBILITY_2_4
1230 int wxBitmap::GetQuality() const
1235 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1240 wxMask
*wxBitmap::GetMask() const
1242 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1244 return M_BITMAPDATA
->m_bitmapMask
;
1247 bool wxBitmap::HasAlpha() const
1249 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1251 return M_BITMAPDATA
->HasAlpha() ;
1254 void wxBitmap::SetWidth(int w
)
1257 m_refData
= new wxBitmapRefData
;
1259 M_BITMAPDATA
->SetWidth(w
);
1262 void wxBitmap::SetHeight(int h
)
1265 m_refData
= new wxBitmapRefData
;
1267 M_BITMAPDATA
->SetHeight(h
);
1270 void wxBitmap::SetDepth(int d
)
1273 m_refData
= new wxBitmapRefData
;
1275 M_BITMAPDATA
->SetDepth(d
);
1278 void wxBitmap::SetOk(bool isOk
)
1281 m_refData
= new wxBitmapRefData
;
1283 M_BITMAPDATA
->SetOk(isOk
);
1287 wxPalette
*wxBitmap::GetPalette() const
1289 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1291 return &M_BITMAPDATA
->m_bitmapPalette
;
1294 void wxBitmap::SetPalette(const wxPalette
& palette
)
1297 m_refData
= new wxBitmapRefData
;
1299 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1301 #endif // wxUSE_PALETTE
1303 void wxBitmap::SetMask(wxMask
*mask
)
1306 m_refData
= new wxBitmapRefData
;
1308 // Remove existing mask if there is one.
1309 delete M_BITMAPDATA
->m_bitmapMask
;
1311 M_BITMAPDATA
->m_bitmapMask
= mask
;
1314 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1316 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1319 // ----------------------------------------------------------------------------
1321 // ----------------------------------------------------------------------------
1328 // Construct a mask from a bitmap and a colour indicating
1329 // the transparent area
1330 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1333 Create(bitmap
, colour
);
1336 // Construct a mask from a mono bitmap (copies the bitmap).
1337 wxMask::wxMask(const wxBitmap
& bitmap
)
1343 // Construct a mask from a mono bitmap (copies the bitmap).
1344 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1347 Create(data
, width
, height
, bytesPerRow
);
1354 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1355 m_maskBitmap
= NULL
;
1361 m_width
= m_height
= m_bytesPerRow
= 0 ;
1362 m_maskBitmap
= NULL
;
1365 void *wxMask::GetRawAccess() const
1367 return m_memBuf
.GetData() ;
1370 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1371 // so no QD colorizing will occur when blitting
1373 void wxMask::RealizeNative()
1377 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1378 m_maskBitmap
= NULL
;
1381 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1382 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1383 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1386 // Create a mask from a mono bitmap (copies the bitmap).
1387 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1392 m_bytesPerRow
= bytesPerRow
;
1394 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1401 // Create a mask from a mono bitmap (copies the bitmap).
1402 bool wxMask::Create(const wxBitmap
& bitmap
)
1404 m_width
= bitmap
.GetWidth() ;
1405 m_height
= bitmap
.GetHeight() ;
1406 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1408 size_t size
= m_bytesPerRow
* m_height
;
1409 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1410 wxASSERT( destdatabase
!= NULL
) ;
1412 memset( destdatabase
, 0 , size
) ;
1413 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1415 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1417 unsigned char *destdata
= destdatabase
;
1418 unsigned char r
, g
, b
;
1420 for ( int x
= 0 ; x
< m_width
; ++x
)
1427 if ( ( r
+ g
+ b
) > 0x10 )
1428 *destdata
++ = 0x00 ;
1430 *destdata
++ = 0xFF ;
1434 m_memBuf
.UngetWriteBuf( size
) ;
1440 // Create a mask from a bitmap and a colour indicating
1441 // the transparent area
1442 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1444 m_width
= bitmap
.GetWidth() ;
1445 m_height
= bitmap
.GetHeight() ;
1446 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1448 size_t size
= m_bytesPerRow
* m_height
;
1449 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1450 wxASSERT( destdatabase
!= NULL
) ;
1452 memset( destdatabase
, 0 , size
) ;
1453 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1455 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1457 unsigned char *destdata
= destdatabase
;
1458 unsigned char r
, g
, b
;
1460 for ( int x
= 0 ; x
< m_width
; ++x
)
1467 if ( colour
== wxColour( r
, g
, b
) )
1468 *destdata
++ = 0x00 ;
1470 *destdata
++ = 0xFF ;
1474 m_memBuf
.UngetWriteBuf( size
) ;
1480 WXHBITMAP
wxMask::GetHBITMAP() const
1482 return m_maskBitmap
;
1485 // ----------------------------------------------------------------------------
1487 // ----------------------------------------------------------------------------
1489 wxBitmapHandler::~wxBitmapHandler()
1493 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1498 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1499 int desiredWidth
, int desiredHeight
)
1504 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1509 // ----------------------------------------------------------------------------
1510 // Standard Handlers
1511 // ----------------------------------------------------------------------------
1513 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1515 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1518 inline wxPICTResourceHandler()
1520 SetName(wxT("Macintosh Pict resource"));
1521 SetExtension(wxEmptyString
);
1522 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1525 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1526 int desiredWidth
, int desiredHeight
);
1528 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1531 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1532 int desiredWidth
, int desiredHeight
)
1536 wxMacStringToPascal( name
, theName
) ;
1538 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1542 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1543 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1545 dc
.SelectObject( *bitmap
) ;
1547 dc
.SelectObject( wxNullBitmap
) ;
1551 #endif //wxUSE_METAFILE
1556 void wxBitmap::InitStandardHandlers()
1558 AddHandler( new wxPICTResourceHandler
) ;
1559 AddHandler( new wxICONResourceHandler
) ;
1562 // ----------------------------------------------------------------------------
1563 // raw bitmap access support
1564 // ----------------------------------------------------------------------------
1566 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1569 // no bitmap, no data (raw or otherwise)
1572 data
.m_width
= GetWidth() ;
1573 data
.m_height
= GetHeight() ;
1574 data
.m_stride
= GetWidth() * 4 ;
1576 return GetRawAccess() ;
1579 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1584 // TODO : if we have some information about the API we should check
1585 // this code looks strange...
1587 if ( !M_BITMAPDATA
->HasAlpha() )
1590 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1591 int w
= data
.GetWidth();
1592 int h
= data
.GetHeight();
1594 wxBitmap
bmpMask( GetWidth(), GetHeight(), 32 );
1595 wxAlphaPixelData
dataMask( bmpMask
, data
.GetOrigin(), wxSize( w
, h
) );
1596 wxAlphaPixelData::Iterator
pMask( dataMask
), p( data
);
1598 for ( int y
= 0; y
< h
; y
++ )
1600 wxAlphaPixelData::Iterator rowStartMask
= pMask
;
1601 wxAlphaPixelData::Iterator rowStart
= p
;
1603 for ( int x
= 0; x
< w
; x
++ )
1605 const wxAlphaPixelData::Iterator::ChannelType alpha
= p
.Alpha();
1607 pMask
.Red() = alpha
;
1608 pMask
.Green() = alpha
;
1609 pMask
.Blue() = alpha
;
1616 p
.OffsetY( data
, 1 );
1618 pMask
= rowStartMask
;
1619 pMask
.OffsetY( dataMask
, 1 );
1622 SetMask( new wxMask( bmpMask
) );
1625 void wxBitmap::UseAlpha()
1627 // remember that we are using alpha channel:
1628 // we'll need to create a proper mask in UngetRawData()
1629 M_BITMAPDATA
->UseAlpha( true );