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 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
556 CGDataProviderRef dataProvider
=
557 CGDataProviderCreateWithData(
558 membuf
, (const void *)membuf
->GetData() , imageSize
,
559 wxMacMemoryBufferReleaseProc
);
562 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
563 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
564 CGDataProviderRelease( dataProvider
);
568 image
= m_cgImageRef
;
569 CGImageRetain( image
) ;
572 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
574 // we keep it for later use
575 m_cgImageRef
= image
;
576 CGImageRetain( image
) ;
583 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
585 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
591 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
593 else if ( m_hasAlpha
)
595 #if !wxMAC_USE_CORE_GRAPHICS
596 if ( m_rawAccessCount
> 0 )
599 // this structure is not kept in synch when using CG, so if something
600 // is really accessing the GrafPorts, we have to sync it
604 *mask
= m_hMaskBitmap
;
611 void wxBitmapRefData::UpdateAlphaMask() const
615 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
616 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
618 int h
= GetHeight() ;
621 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
623 unsigned char* destalpha
= destalphabase
;
625 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
627 // we must have 24 bit depth for non quartz smooth alpha
629 *destalpha
++ = 255 - *sourcemask
;
630 *destalpha
++ = 255 - *sourcemask
;
631 *destalpha
++ = 255 - *sourcemask
;
637 void wxBitmapRefData::Free()
639 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
644 CGImageRelease( m_cgImageRef
) ;
645 m_cgImageRef
= NULL
;
651 ReleaseIconRef( m_iconRef
) ;
657 KillPicture( m_pictHandle
) ;
658 m_pictHandle
= NULL
;
663 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
669 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
670 m_hMaskBitmap
= NULL
;
680 wxBitmapRefData::~wxBitmapRefData()
685 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
687 bool created
= false ;
688 int w
= icon
.GetWidth() ;
689 int h
= icon
.GetHeight() ;
691 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
693 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
695 IconFamilyHandle iconFamily
= NULL
;
696 Handle imagehandle
= NewHandle( 0 ) ;
697 Handle maskhandle
= NewHandle( 0 ) ;
701 IconSelectorValue selector
= 0 ;
706 dataType
= kThumbnail32BitData
;
707 maskType
= kThumbnail8BitMask
;
708 selector
= kSelectorAllAvailableData
;
712 dataType
= kHuge32BitData
;
713 maskType
= kHuge8BitMask
;
714 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
718 dataType
= kLarge32BitData
;
719 maskType
= kLarge8BitMask
;
720 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
724 dataType
= kSmall32BitData
;
725 maskType
= kSmall8BitMask
;
726 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
733 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
735 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
736 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
737 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
738 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
740 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
742 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
743 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
747 unsigned char *source
= (unsigned char *) *imagehandle
;
748 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
749 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
751 for ( int y
= 0 ; y
< h
; ++y
)
753 for ( int x
= 0 ; x
< w
; ++x
)
755 *destination
++ = *sourcemask
++ ;
757 *destination
++ = *source
++ ;
758 *destination
++ = *source
++ ;
759 *destination
++ = *source
++ ;
764 DisposeHandle( imagehandle
) ;
765 DisposeHandle( maskhandle
) ;
769 DisposeHandle( (Handle
) iconFamily
) ;
775 dc
.SelectObject( *this ) ;
776 dc
.DrawIcon( icon
, 0 , 0 ) ;
777 dc
.SelectObject( wxNullBitmap
) ;
787 wxBitmap::~wxBitmap()
791 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
793 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
797 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
798 if ( the_width
% (sizeof(unsigned char) * 8) )
799 linesize
+= sizeof(unsigned char);
801 unsigned char* linestart
= (unsigned char*) bits
;
802 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
804 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
806 int index
, bit
, mask
;
808 for ( int x
= 0 ; x
< the_width
; ++x
)
814 if ( !(linestart
[index
] & mask
) )
816 *destination
++ = 0xFF ;
823 *destination
++ = 0xFF ;
824 *destination
++ = 0xFF ;
825 *destination
++ = 0xFF ;
826 *destination
++ = 0xFF ;
835 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
839 wxBitmap::wxBitmap(int w
, int h
, int d
)
841 (void)Create(w
, h
, d
);
844 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
846 (void) Create(data
, type
, width
, height
, depth
);
849 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
851 LoadFile(filename
, type
);
854 void * wxBitmap::GetRawAccess() const
856 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
858 return M_BITMAPDATA
->GetRawAccess() ;
861 void * wxBitmap::BeginRawAccess()
863 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
865 return M_BITMAPDATA
->BeginRawAccess() ;
868 void wxBitmap::EndRawAccess()
870 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
872 M_BITMAPDATA
->EndRawAccess() ;
876 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
878 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
880 return M_BITMAPDATA
->CGImageCreate() ;
884 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
887 (rect
.x
>= 0) && (rect
.y
>= 0) &&
888 (rect
.x
+rect
.width
<= GetWidth()) &&
889 (rect
.y
+rect
.height
<= GetHeight()),
890 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
892 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
893 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
895 int sourcewidth
= GetWidth() ;
896 int destwidth
= rect
.width
;
897 int destheight
= rect
.height
;
900 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
901 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
902 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
904 int sourcelinesize
= sourcewidth
* 4 ;
905 int destlinesize
= destwidth
* 4 ;
906 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
907 unsigned char *dest
= destdata
;
909 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
911 memcpy( dest
, source
, destlinesize
) ;
917 if ( M_BITMAPDATA
->m_bitmapMask
)
919 wxMemoryBuffer maskbuf
;
920 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
921 size_t maskbufsize
= rowBytes
* destheight
;
923 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
924 int destlinesize
= rowBytes
;
926 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
927 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
928 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
930 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
931 unsigned char *dest
= destdata
;
933 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
935 memcpy( dest
, source
, destlinesize
) ;
938 maskbuf
.UngetWriteBuf( maskbufsize
) ;
939 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
941 else if ( HasAlpha() )
947 bool wxBitmap::Create(int w
, int h
, int d
)
952 d
= wxDisplayDepth() ;
954 m_refData
= new wxBitmapRefData( w
, h
, d
);
956 return M_BITMAPDATA
->Ok() ;
959 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
963 wxBitmapHandler
*handler
= FindHandler(type
);
967 m_refData
= new wxBitmapRefData
;
969 return handler
->LoadFile(this, filename
, type
, -1, -1);
974 wxImage
loadimage(filename
, type
);
984 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
989 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
993 m_refData
= new wxBitmapRefData
;
995 wxBitmapHandler
*handler
= FindHandler(type
);
997 if ( handler
== NULL
)
999 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1004 return handler
->Create(this, data
, type
, width
, height
, depth
);
1009 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1011 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1013 // width and height of the device-dependent bitmap
1014 int width
= image
.GetWidth();
1015 int height
= image
.GetHeight();
1017 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1021 bool hasAlpha
= false ;
1023 if ( image
.HasMask() )
1025 // takes precedence, don't mix with alpha info
1029 hasAlpha
= image
.HasAlpha() ;
1035 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1036 register unsigned char* data
= image
.GetData();
1037 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1039 for (int y
= 0; y
< height
; y
++)
1041 for (int x
= 0; x
< width
; x
++)
1045 const unsigned char a
= *alpha
++;
1046 *destination
++ = a
;
1048 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1049 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1050 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1051 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1053 *destination
++ = *data
++ ;
1054 *destination
++ = *data
++ ;
1055 *destination
++ = *data
++ ;
1060 *destination
++ = 0xFF ;
1061 *destination
++ = *data
++ ;
1062 *destination
++ = *data
++ ;
1063 *destination
++ = *data
++ ;
1069 if ( image
.HasMask() )
1070 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1073 wxImage
wxBitmap::ConvertToImage() const
1077 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1079 // create an wxImage object
1080 int width
= GetWidth();
1081 int height
= GetHeight();
1082 image
.Create( width
, height
);
1084 unsigned char *data
= image
.GetData();
1085 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1087 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1089 bool hasAlpha
= false ;
1090 bool hasMask
= false ;
1091 int maskBytesPerRow
= 0 ;
1092 unsigned char *alpha
= NULL
;
1093 unsigned char *mask
= NULL
;
1101 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1102 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1108 alpha
= image
.GetAlpha() ;
1113 // The following masking algorithm is the same as well in msw/gtk:
1114 // the colour used as transparent one in wxImage and the one it is
1115 // replaced with when it actually occurs in the bitmap
1116 static const int MASK_RED
= 1;
1117 static const int MASK_GREEN
= 2;
1118 static const int MASK_BLUE
= 3;
1119 static const int MASK_BLUE_REPLACEMENT
= 2;
1121 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1123 unsigned char * maskp
= mask
;
1124 unsigned char a
, r
, g
, b
;
1127 for (int xx
= 0; xx
< width
; xx
++)
1129 color
= *((long*) source
) ;
1130 #ifdef WORDS_BIGENDIAN
1131 a
= ((color
&0xFF000000) >> 24) ;
1132 r
= ((color
&0x00FF0000) >> 16) ;
1133 g
= ((color
&0x0000FF00) >> 8) ;
1134 b
= (color
&0x000000FF);
1136 b
= ((color
&0xFF000000) >> 24) ;
1137 g
= ((color
&0x00FF0000) >> 16) ;
1138 r
= ((color
&0x0000FF00) >> 8) ;
1139 a
= (color
&0x000000FF);
1143 if ( *maskp
++ == 0xFF )
1149 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1150 b
= MASK_BLUE_REPLACEMENT
;
1156 else if ( hasAlpha
)
1160 data
[index
+ 1] = g
;
1161 data
[index
+ 2] = b
;
1169 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1174 #endif //wxUSE_IMAGE
1176 bool wxBitmap::SaveFile( const wxString
& filename
,
1177 wxBitmapType type
, const wxPalette
*palette
) const
1179 bool success
= false;
1180 wxBitmapHandler
*handler
= FindHandler(type
);
1184 success
= handler
->SaveFile(this, filename
, type
, palette
);
1189 wxImage image
= ConvertToImage();
1190 success
= image
.SaveFile(filename
, type
);
1192 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1199 bool wxBitmap::Ok() const
1201 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1204 int wxBitmap::GetHeight() const
1206 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1208 return M_BITMAPDATA
->GetHeight();
1211 int wxBitmap::GetWidth() const
1213 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1215 return M_BITMAPDATA
->GetWidth() ;
1218 int wxBitmap::GetDepth() const
1220 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1222 return M_BITMAPDATA
->GetDepth();
1225 #if WXWIN_COMPATIBILITY_2_4
1226 int wxBitmap::GetQuality() const
1231 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1236 wxMask
*wxBitmap::GetMask() const
1238 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1240 return M_BITMAPDATA
->m_bitmapMask
;
1243 bool wxBitmap::HasAlpha() const
1245 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1247 return M_BITMAPDATA
->HasAlpha() ;
1250 void wxBitmap::SetWidth(int w
)
1253 m_refData
= new wxBitmapRefData
;
1255 M_BITMAPDATA
->SetWidth(w
);
1258 void wxBitmap::SetHeight(int h
)
1261 m_refData
= new wxBitmapRefData
;
1263 M_BITMAPDATA
->SetHeight(h
);
1266 void wxBitmap::SetDepth(int d
)
1269 m_refData
= new wxBitmapRefData
;
1271 M_BITMAPDATA
->SetDepth(d
);
1274 void wxBitmap::SetOk(bool isOk
)
1277 m_refData
= new wxBitmapRefData
;
1279 M_BITMAPDATA
->SetOk(isOk
);
1283 wxPalette
*wxBitmap::GetPalette() const
1285 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1287 return &M_BITMAPDATA
->m_bitmapPalette
;
1290 void wxBitmap::SetPalette(const wxPalette
& palette
)
1293 m_refData
= new wxBitmapRefData
;
1295 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1297 #endif // wxUSE_PALETTE
1299 void wxBitmap::SetMask(wxMask
*mask
)
1302 m_refData
= new wxBitmapRefData
;
1304 // Remove existing mask if there is one.
1305 delete M_BITMAPDATA
->m_bitmapMask
;
1307 M_BITMAPDATA
->m_bitmapMask
= mask
;
1310 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1312 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1315 // ----------------------------------------------------------------------------
1317 // ----------------------------------------------------------------------------
1324 // Construct a mask from a bitmap and a colour indicating
1325 // the transparent area
1326 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1329 Create( bitmap
, colour
);
1332 // Construct a mask from a mono bitmap (copies the bitmap).
1333 wxMask::wxMask( const wxBitmap
& bitmap
)
1339 // Construct a mask from a mono bitmap (copies the bitmap).
1341 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1344 Create( data
, width
, height
, bytesPerRow
);
1351 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1352 m_maskBitmap
= NULL
;
1358 m_width
= m_height
= m_bytesPerRow
= 0 ;
1359 m_maskBitmap
= NULL
;
1362 void *wxMask::GetRawAccess() const
1364 return m_memBuf
.GetData() ;
1367 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1368 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1370 void wxMask::RealizeNative()
1374 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1375 m_maskBitmap
= NULL
;
1378 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1380 OSStatus err
= NewGWorldFromPtr(
1381 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1382 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1383 verify_noerr( err
) ;
1386 // Create a mask from a mono bitmap (copies the bitmap).
1388 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1393 m_bytesPerRow
= bytesPerRow
;
1395 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1402 // Create a mask from a mono bitmap (copies the bitmap).
1403 bool wxMask::Create(const wxBitmap
& bitmap
)
1405 m_width
= bitmap
.GetWidth() ;
1406 m_height
= bitmap
.GetHeight() ;
1407 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1409 size_t size
= m_bytesPerRow
* m_height
;
1410 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1411 wxASSERT( destdatabase
!= NULL
) ;
1413 memset( destdatabase
, 0 , size
) ;
1414 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1416 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1418 unsigned char *destdata
= destdatabase
;
1419 unsigned char r
, g
, b
;
1421 for ( int x
= 0 ; x
< m_width
; ++x
)
1428 if ( ( r
+ g
+ b
) > 0x10 )
1430 *destdata
++ = 0xFF ;
1431 *destdata
++ = 0xFF ;
1432 *destdata
++ = 0xFF ;
1433 *destdata
++ = 0xFF ;
1437 *destdata
++ = 0x00 ;
1438 *destdata
++ = 0x00 ;
1439 *destdata
++ = 0x00 ;
1440 *destdata
++ = 0x00 ;
1445 m_memBuf
.UngetWriteBuf( size
) ;
1451 // Create a mask from a bitmap and a colour indicating
1452 // the transparent area
1453 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1455 m_width
= bitmap
.GetWidth() ;
1456 m_height
= bitmap
.GetHeight() ;
1457 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1459 size_t size
= m_bytesPerRow
* m_height
;
1460 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1461 wxASSERT( destdatabase
!= NULL
) ;
1463 memset( destdatabase
, 0 , size
) ;
1464 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1466 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1468 unsigned char *destdata
= destdatabase
;
1469 unsigned char r
, g
, b
;
1471 for ( int x
= 0 ; x
< m_width
; ++x
)
1478 if ( colour
== wxColour( r
, g
, b
) )
1480 *destdata
++ = 0xFF ;
1481 *destdata
++ = 0xFF ;
1482 *destdata
++ = 0xFF ;
1483 *destdata
++ = 0xFF ;
1487 *destdata
++ = 0x00 ;
1488 *destdata
++ = 0x00 ;
1489 *destdata
++ = 0x00 ;
1490 *destdata
++ = 0x00 ;
1495 m_memBuf
.UngetWriteBuf( size
) ;
1501 WXHBITMAP
wxMask::GetHBITMAP() const
1503 return m_maskBitmap
;
1506 // ----------------------------------------------------------------------------
1508 // ----------------------------------------------------------------------------
1510 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1512 // ----------------------------------------------------------------------------
1513 // Standard Handlers
1514 // ----------------------------------------------------------------------------
1516 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1518 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1521 inline wxPICTResourceHandler()
1523 SetName(wxT("Macintosh Pict resource"));
1524 SetExtension(wxEmptyString
);
1525 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1528 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1529 int desiredWidth
, int desiredHeight
);
1532 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1535 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1536 int desiredWidth
, int desiredHeight
)
1540 wxMacStringToPascal( name
, theName
) ;
1542 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1547 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1548 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1550 dc
.SelectObject( *bitmap
) ;
1552 dc
.SelectObject( wxNullBitmap
) ;
1561 void wxBitmap::InitStandardHandlers()
1563 AddHandler( new wxPICTResourceHandler
) ;
1564 AddHandler( new wxICONResourceHandler
) ;
1567 // ----------------------------------------------------------------------------
1568 // raw bitmap access support
1569 // ----------------------------------------------------------------------------
1571 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1574 // no bitmap, no data (raw or otherwise)
1577 data
.m_width
= GetWidth() ;
1578 data
.m_height
= GetHeight() ;
1579 data
.m_stride
= GetWidth() * 4 ;
1581 return BeginRawAccess() ;
1584 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1589 void wxBitmap::UseAlpha()
1591 // remember that we are using alpha channel:
1592 // we'll need to create a proper mask in UngetRawData()
1593 M_BITMAPDATA
->UseAlpha( true );