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"
22 #include "wx/metafile.h"
23 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
38 #include "wx/dcmemory.h"
40 // Implementation Notes
41 // --------------------
43 // we are always working with a 32 bit deep pixel buffer
44 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
45 // therefore we have a separate GWorld there for blitting the mask in
47 // under Quartz then content is transformed into a CGImageRef representing the same data
48 // which can be transferred to the GPU by the OS for fast rendering
50 // we don't dare use premultiplied alpha yet
51 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
55 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
57 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
60 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
64 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
67 wxBitmapRefData
* bmp
= bmap
;
69 if ( !bmap
->HasNativeSize() )
71 // as PICT conversion will only result in a 16x16 icon, let's attempt
72 // a few scales for better results
74 int w
= bitmap
.GetWidth() ;
75 int h
= bitmap
.GetHeight() ;
76 int sz
= wxMax( w
, h
) ;
77 if ( sz
== 24 || sz
== 64 )
79 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
80 bmp
= scaleBmp
.GetBitmapData() ;
84 info
->contentType
= kControlContentIconRef
;
85 info
->u
.iconRef
= bmp
->GetIconRef() ;
86 AcquireIconRef( info
->u
.iconRef
) ;
88 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
89 else if ( forceType
== kControlContentCGImageRef
)
91 info
->contentType
= kControlContentCGImageRef
;
92 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
97 info
->contentType
= kControlContentPictHandle
;
98 info
->u
.picture
= bmap
->GetPictHandle() ;
103 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
105 if ( info
->contentType
== kControlContentIconRef
)
107 ReleaseIconRef( info
->u
.iconRef
) ;
109 else if ( info
->contentType
== kControlNoContent
)
111 // there's no bitmap at all, fall through silently
113 else if ( info
->contentType
== kControlContentPictHandle
)
115 // owned by the bitmap, no release here
117 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
118 else if ( info
->contentType
== kControlContentCGImageRef
)
120 CGImageRelease( info
->u
.imageRef
) ;
125 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
129 #endif //wxUSE_BMPBUTTON
131 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
133 void wxBitmapRefData::Init()
139 m_bitmapMask
= NULL
;
142 m_cgImageRef
= NULL
;
146 m_pictHandle
= NULL
;
148 m_hMaskBitmap
= NULL
;
149 m_maskBytesPerRow
= 0 ;
151 m_rawAccessCount
= 0 ;
155 wxBitmapRefData::wxBitmapRefData()
160 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
163 Create( w
, h
, d
) ;
166 bool wxBitmapRefData::Create( int w
, int h
, int d
)
172 m_bytesPerRow
= w
* 4 ;
173 size_t size
= m_bytesPerRow
* h
;
174 void* data
= m_memBuf
.GetWriteBuf( size
) ;
175 memset( data
, 0 , size
) ;
176 m_memBuf
.UngetWriteBuf( size
) ;
179 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
180 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
181 (char*) data
, m_bytesPerRow
) ) ;
182 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
184 m_ok
= ( m_hBitmap
!= NULL
) ;
189 void wxBitmapRefData::UseAlpha( bool use
)
191 if ( m_hasAlpha
== use
)
197 wxASSERT( m_hMaskBitmap
== NULL
) ;
199 int width
= GetWidth() ;
200 int height
= GetHeight() ;
201 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
202 size_t size
= height
* m_maskBytesPerRow
;
203 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
204 wxASSERT( data
!= NULL
) ;
206 memset( data
, 0 , size
) ;
207 Rect rect
= { 0 , 0 , height
, width
} ;
208 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
209 (char*) data
, m_maskBytesPerRow
) ) ;
210 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
211 m_maskMemBuf
.UngetWriteBuf(size
) ;
213 #if !wxMAC_USE_CORE_GRAPHICS
219 DisposeGWorld( m_hMaskBitmap
) ;
220 m_hMaskBitmap
= NULL
;
221 m_maskBytesPerRow
= 0 ;
225 void *wxBitmapRefData::GetRawAccess() const
227 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
228 return m_memBuf
.GetData() ;
231 void *wxBitmapRefData::BeginRawAccess()
233 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
234 wxASSERT( m_rawAccessCount
== 0 ) ;
235 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
236 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
241 // we must destroy an existing cached image, as
242 // the bitmap data may change now
245 CGImageRelease( m_cgImageRef
) ;
246 m_cgImageRef
= NULL
;
250 return m_memBuf
.GetData() ;
253 void wxBitmapRefData::EndRawAccess()
255 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
256 wxASSERT( m_rawAccessCount
== 1 ) ;
260 #if !wxMAC_USE_CORE_GRAPHICS
265 bool wxBitmapRefData::HasNativeSize()
268 int h
= GetHeight() ;
269 int sz
= wxMax( w
, h
) ;
271 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
274 IconRef
wxBitmapRefData::GetIconRef()
276 if ( m_iconRef
== NULL
)
278 // Create Icon Family Handle
280 IconFamilyHandle iconFamily
= NULL
;
282 #ifdef WORDS_BIGENDIAN
283 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
284 (**iconFamily
).resourceType
= kIconFamilyType
;
285 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
287 // test this solution on big endian as well
288 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
292 int h
= GetHeight() ;
293 int sz
= wxMax( w
, h
) ;
295 OSType dataType
= 0 ;
296 OSType maskType
= 0 ;
301 dataType
= kThumbnail32BitData
;
302 maskType
= kThumbnail8BitMask
;
306 dataType
= kHuge32BitData
;
307 maskType
= kHuge8BitMask
;
311 dataType
= kLarge32BitData
;
312 maskType
= kLarge8BitMask
;
316 dataType
= kSmall32BitData
;
317 maskType
= kSmall8BitMask
;
326 // setup the header properly
329 Handle maskdata
= NULL
;
330 unsigned char * maskptr
= NULL
;
331 unsigned char * ptr
= NULL
;
332 size_t datasize
, masksize
;
334 datasize
= sz
* sz
* 4 ;
335 data
= NewHandle( datasize
) ;
337 ptr
= (unsigned char*) *data
;
338 memset( ptr
, 0, datasize
) ;
341 maskdata
= NewHandle( masksize
) ;
343 maskptr
= (unsigned char*) *maskdata
;
344 memset( maskptr
, 0 , masksize
) ;
346 bool hasAlpha
= HasAlpha() ;
347 wxMask
*mask
= m_bitmapMask
;
348 unsigned char * source
= (unsigned char*) GetRawAccess() ;
349 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
351 for ( int y
= 0 ; y
< h
; ++y
)
353 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
354 unsigned char * maskdest
= maskptr
+ y
* sz
;
355 unsigned char a
, r
, g
, b
;
357 for ( int x
= 0 ; x
< w
; ++x
)
371 *maskdest
++ = 0xFF - *masksource
++ ;
383 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
384 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
386 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
387 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
390 HUnlock( maskdata
) ;
391 DisposeHandle( data
) ;
392 DisposeHandle( maskdata
) ;
396 PicHandle pic
= GetPictHandle() ;
397 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
400 // transform into IconRef
402 static int iconCounter
= 2 ;
404 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
405 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
407 // we have to retain a reference, as Unregister will decrement it
408 AcquireIconRef( m_iconRef
) ;
409 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
410 DisposeHandle( (Handle
) iconFamily
) ;
417 PicHandle
wxBitmapRefData::GetPictHandle()
419 if ( m_pictHandle
== NULL
)
421 CGrafPtr origPort
= NULL
;
422 GDHandle origDev
= NULL
;
423 GWorldPtr wp
= NULL
;
424 GWorldPtr mask
= NULL
;
425 int height
= GetHeight() ;
426 int width
= GetWidth() ;
428 Rect rect
= { 0 , 0 , height
, width
} ;
429 RgnHandle clipRgn
= NULL
;
431 GetGWorld( &origPort
, &origDev
) ;
432 wp
= GetHBITMAP( &mask
) ;
436 GWorldPtr monoworld
;
438 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
440 LockPixels( GetGWorldPixMap( monoworld
) ) ;
441 LockPixels( GetGWorldPixMap( mask
) ) ;
442 SetGWorld( monoworld
, NULL
) ;
444 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
445 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
446 RGBForeColor( &black
) ;
447 RGBBackColor( &white
) ;
449 CopyBits(GetPortBitMapForCopyBits(mask
),
450 GetPortBitMapForCopyBits(monoworld
),
454 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
456 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
457 UnlockPixels( GetGWorldPixMap( mask
) ) ;
458 DisposeGWorld( monoworld
) ;
461 SetGWorld( wp
, NULL
) ;
463 GetPortBounds( wp
, &portRect
) ;
464 m_pictHandle
= OpenPicture(&portRect
);
468 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
469 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
471 RGBForeColor( &black
) ;
472 RGBBackColor( &white
) ;
477 LockPixels( GetGWorldPixMap( wp
) ) ;
478 CopyBits(GetPortBitMapForCopyBits(wp
),
479 GetPortBitMapForCopyBits(wp
),
483 UnlockPixels( GetGWorldPixMap( wp
) ) ;
487 SetGWorld( origPort
, origDev
) ;
489 DisposeRgn( clipRgn
) ;
492 return m_pictHandle
;
496 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
498 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
500 wxASSERT( data
== membuf
->GetData() ) ;
505 CGImageRef
wxBitmapRefData::CGImageCreate() const
508 wxASSERT( m_rawAccessCount
>= 0 ) ;
510 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
512 size_t imageSize
= m_width
* m_height
* 4 ;
513 void * dataBuffer
= m_memBuf
.GetData() ;
516 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
517 wxMemoryBuffer
* membuf
= NULL
;
521 alphaInfo
= kCGImageAlphaFirst
;
522 membuf
= new wxMemoryBuffer( imageSize
) ;
523 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
524 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
525 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
526 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
527 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
529 unsigned char *sourcemask
= sourcemaskstart
;
530 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
532 *destalpha
= 0xFF - *sourcemask
;
540 #if wxMAC_USE_PREMULTIPLIED_ALPHA
541 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
543 alphaInfo
= kCGImageAlphaFirst
;
547 membuf
= new wxMemoryBuffer( m_memBuf
) ;
550 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
551 CGDataProviderRef dataProvider
=
552 CGDataProviderCreateWithData(
553 membuf
, (const void *)membuf
->GetData() , imageSize
,
554 wxMacMemoryBufferReleaseProc
);
557 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
558 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
559 CGDataProviderRelease( dataProvider
);
563 image
= m_cgImageRef
;
564 CGImageRetain( image
) ;
567 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
569 // we keep it for later use
570 m_cgImageRef
= image
;
571 CGImageRetain( image
) ;
578 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
580 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
586 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
588 else if ( m_hasAlpha
)
590 #if !wxMAC_USE_CORE_GRAPHICS
591 if ( m_rawAccessCount
> 0 )
594 // this structure is not kept in synch when using CG, so if something
595 // is really accessing the GrafPorts, we have to sync it
599 *mask
= m_hMaskBitmap
;
606 void wxBitmapRefData::UpdateAlphaMask() const
610 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
611 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
613 int h
= GetHeight() ;
616 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
618 unsigned char* destalpha
= destalphabase
;
620 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
622 // we must have 24 bit depth for non quartz smooth alpha
624 *destalpha
++ = 255 - *sourcemask
;
625 *destalpha
++ = 255 - *sourcemask
;
626 *destalpha
++ = 255 - *sourcemask
;
632 void wxBitmapRefData::Free()
634 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
639 CGImageRelease( m_cgImageRef
) ;
640 m_cgImageRef
= NULL
;
646 ReleaseIconRef( m_iconRef
) ;
652 KillPicture( m_pictHandle
) ;
653 m_pictHandle
= NULL
;
658 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
664 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
665 m_hMaskBitmap
= NULL
;
675 wxBitmapRefData::~wxBitmapRefData()
680 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
682 bool created
= false ;
683 int w
= icon
.GetWidth() ;
684 int h
= icon
.GetHeight() ;
686 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
688 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
690 IconFamilyHandle iconFamily
= NULL
;
691 Handle imagehandle
= NewHandle( 0 ) ;
692 Handle maskhandle
= NewHandle( 0 ) ;
696 IconSelectorValue selector
= 0 ;
701 dataType
= kThumbnail32BitData
;
702 maskType
= kThumbnail8BitMask
;
703 selector
= kSelectorAllAvailableData
;
707 dataType
= kHuge32BitData
;
708 maskType
= kHuge8BitMask
;
709 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
713 dataType
= kLarge32BitData
;
714 maskType
= kLarge8BitMask
;
715 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
719 dataType
= kSmall32BitData
;
720 maskType
= kSmall8BitMask
;
721 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
728 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
730 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
731 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
732 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
733 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
735 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
737 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
738 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
742 unsigned char *source
= (unsigned char *) *imagehandle
;
743 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
744 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
746 for ( int y
= 0 ; y
< h
; ++y
)
748 for ( int x
= 0 ; x
< w
; ++x
)
750 *destination
++ = *sourcemask
++ ;
752 *destination
++ = *source
++ ;
753 *destination
++ = *source
++ ;
754 *destination
++ = *source
++ ;
759 DisposeHandle( imagehandle
) ;
760 DisposeHandle( maskhandle
) ;
764 DisposeHandle( (Handle
) iconFamily
) ;
770 dc
.SelectObject( *this ) ;
771 dc
.DrawIcon( icon
, 0 , 0 ) ;
772 dc
.SelectObject( wxNullBitmap
) ;
782 wxBitmap::~wxBitmap()
786 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
788 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
792 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
793 if ( the_width
% (sizeof(unsigned char) * 8) )
794 linesize
+= sizeof(unsigned char);
796 unsigned char* linestart
= (unsigned char*) bits
;
797 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
799 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
801 int index
, bit
, mask
;
803 for ( int x
= 0 ; x
< the_width
; ++x
)
809 if ( !(linestart
[index
] & mask
) )
811 *destination
++ = 0xFF ;
818 *destination
++ = 0xFF ;
819 *destination
++ = 0xFF ;
820 *destination
++ = 0xFF ;
821 *destination
++ = 0xFF ;
830 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
834 wxBitmap::wxBitmap(int w
, int h
, int d
)
836 (void)Create(w
, h
, d
);
839 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
841 (void) Create(data
, type
, width
, height
, depth
);
844 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
846 LoadFile(filename
, type
);
849 wxBitmap::wxBitmap(const char **bits
)
851 (void) CreateFromXpm(bits
);
854 wxBitmap::wxBitmap(char **bits
)
856 (void) CreateFromXpm((const char **)bits
);
859 void * wxBitmap::GetRawAccess() const
861 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
863 return M_BITMAPDATA
->GetRawAccess() ;
866 void * wxBitmap::BeginRawAccess()
868 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
870 return M_BITMAPDATA
->BeginRawAccess() ;
873 void wxBitmap::EndRawAccess()
875 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
877 M_BITMAPDATA
->EndRawAccess() ;
880 bool wxBitmap::CreateFromXpm(const char **bits
)
883 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
885 wxXPMDecoder decoder
;
886 wxImage img
= decoder
.ReadData(bits
);
887 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
889 *this = wxBitmap(img
);
899 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
901 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
903 return M_BITMAPDATA
->CGImageCreate() ;
907 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
910 (rect
.x
>= 0) && (rect
.y
>= 0) &&
911 (rect
.x
+rect
.width
<= GetWidth()) &&
912 (rect
.y
+rect
.height
<= GetHeight()),
913 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
915 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
916 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
918 int sourcewidth
= GetWidth() ;
919 int destwidth
= rect
.width
;
920 int destheight
= rect
.height
;
923 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
924 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
925 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
927 int sourcelinesize
= sourcewidth
* 4 ;
928 int destlinesize
= destwidth
* 4 ;
929 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
930 unsigned char *dest
= destdata
;
932 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
934 memcpy( dest
, source
, destlinesize
) ;
940 if ( M_BITMAPDATA
->m_bitmapMask
)
942 wxMemoryBuffer maskbuf
;
943 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
944 size_t maskbufsize
= rowBytes
* destheight
;
946 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
947 int destlinesize
= rowBytes
;
949 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
950 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
951 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
953 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
954 unsigned char *dest
= destdata
;
956 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
958 memcpy( dest
, source
, destlinesize
) ;
961 maskbuf
.UngetWriteBuf( maskbufsize
) ;
962 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
964 else if ( HasAlpha() )
970 bool wxBitmap::Create(int w
, int h
, int d
)
975 d
= wxDisplayDepth() ;
977 m_refData
= new wxBitmapRefData( w
, h
, d
);
979 return M_BITMAPDATA
->Ok() ;
982 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
986 wxBitmapHandler
*handler
= FindHandler(type
);
990 m_refData
= new wxBitmapRefData
;
992 return handler
->LoadFile(this, filename
, type
, -1, -1);
997 wxImage
loadimage(filename
, type
);
1007 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1012 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
1016 m_refData
= new wxBitmapRefData
;
1018 wxBitmapHandler
*handler
= FindHandler(type
);
1020 if ( handler
== NULL
)
1022 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1027 return handler
->Create(this, data
, type
, width
, height
, depth
);
1032 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1034 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1036 // width and height of the device-dependent bitmap
1037 int width
= image
.GetWidth();
1038 int height
= image
.GetHeight();
1040 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1044 bool hasAlpha
= false ;
1046 if ( image
.HasMask() )
1048 // takes precedence, don't mix with alpha info
1052 hasAlpha
= image
.HasAlpha() ;
1058 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1059 register unsigned char* data
= image
.GetData();
1060 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1062 for (int y
= 0; y
< height
; y
++)
1064 for (int x
= 0; x
< width
; x
++)
1068 const unsigned char a
= *alpha
++;
1069 *destination
++ = a
;
1071 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1072 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1073 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1074 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1076 *destination
++ = *data
++ ;
1077 *destination
++ = *data
++ ;
1078 *destination
++ = *data
++ ;
1083 *destination
++ = 0xFF ;
1084 *destination
++ = *data
++ ;
1085 *destination
++ = *data
++ ;
1086 *destination
++ = *data
++ ;
1092 if ( image
.HasMask() )
1093 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1096 wxImage
wxBitmap::ConvertToImage() const
1100 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1102 // create an wxImage object
1103 int width
= GetWidth();
1104 int height
= GetHeight();
1105 image
.Create( width
, height
);
1107 unsigned char *data
= image
.GetData();
1108 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1110 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1112 bool hasAlpha
= false ;
1113 bool hasMask
= false ;
1114 int maskBytesPerRow
= 0 ;
1115 unsigned char *alpha
= NULL
;
1116 unsigned char *mask
= NULL
;
1124 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1125 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1131 alpha
= image
.GetAlpha() ;
1136 // The following masking algorithm is the same as well in msw/gtk:
1137 // the colour used as transparent one in wxImage and the one it is
1138 // replaced with when it actually occurs in the bitmap
1139 static const int MASK_RED
= 1;
1140 static const int MASK_GREEN
= 2;
1141 static const int MASK_BLUE
= 3;
1142 static const int MASK_BLUE_REPLACEMENT
= 2;
1144 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1146 unsigned char * maskp
= mask
;
1147 unsigned char a
, r
, g
, b
;
1150 for (int xx
= 0; xx
< width
; xx
++)
1152 color
= *((long*) source
) ;
1153 #ifdef WORDS_BIGENDIAN
1154 a
= ((color
&0xFF000000) >> 24) ;
1155 r
= ((color
&0x00FF0000) >> 16) ;
1156 g
= ((color
&0x0000FF00) >> 8) ;
1157 b
= (color
&0x000000FF);
1159 b
= ((color
&0xFF000000) >> 24) ;
1160 g
= ((color
&0x00FF0000) >> 16) ;
1161 r
= ((color
&0x0000FF00) >> 8) ;
1162 a
= (color
&0x000000FF);
1166 if ( *maskp
++ == 0xFF )
1172 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1173 b
= MASK_BLUE_REPLACEMENT
;
1179 else if ( hasAlpha
)
1183 data
[index
+ 1] = g
;
1184 data
[index
+ 2] = b
;
1192 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1197 #endif //wxUSE_IMAGE
1199 bool wxBitmap::SaveFile( const wxString
& filename
,
1200 wxBitmapType type
, const wxPalette
*palette
) const
1202 bool success
= false;
1203 wxBitmapHandler
*handler
= FindHandler(type
);
1207 success
= handler
->SaveFile(this, filename
, type
, palette
);
1212 wxImage image
= ConvertToImage();
1213 success
= image
.SaveFile(filename
, type
);
1215 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1222 bool wxBitmap::Ok() const
1224 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1227 int wxBitmap::GetHeight() const
1229 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1231 return M_BITMAPDATA
->GetHeight();
1234 int wxBitmap::GetWidth() const
1236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1238 return M_BITMAPDATA
->GetWidth() ;
1241 int wxBitmap::GetDepth() const
1243 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1245 return M_BITMAPDATA
->GetDepth();
1248 #if WXWIN_COMPATIBILITY_2_4
1249 int wxBitmap::GetQuality() const
1254 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1259 wxMask
*wxBitmap::GetMask() const
1261 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1263 return M_BITMAPDATA
->m_bitmapMask
;
1266 bool wxBitmap::HasAlpha() const
1268 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1270 return M_BITMAPDATA
->HasAlpha() ;
1273 void wxBitmap::SetWidth(int w
)
1276 m_refData
= new wxBitmapRefData
;
1278 M_BITMAPDATA
->SetWidth(w
);
1281 void wxBitmap::SetHeight(int h
)
1284 m_refData
= new wxBitmapRefData
;
1286 M_BITMAPDATA
->SetHeight(h
);
1289 void wxBitmap::SetDepth(int d
)
1292 m_refData
= new wxBitmapRefData
;
1294 M_BITMAPDATA
->SetDepth(d
);
1297 void wxBitmap::SetOk(bool isOk
)
1300 m_refData
= new wxBitmapRefData
;
1302 M_BITMAPDATA
->SetOk(isOk
);
1306 wxPalette
*wxBitmap::GetPalette() const
1308 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1310 return &M_BITMAPDATA
->m_bitmapPalette
;
1313 void wxBitmap::SetPalette(const wxPalette
& palette
)
1316 m_refData
= new wxBitmapRefData
;
1318 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1320 #endif // wxUSE_PALETTE
1322 void wxBitmap::SetMask(wxMask
*mask
)
1325 m_refData
= new wxBitmapRefData
;
1327 // Remove existing mask if there is one.
1328 delete M_BITMAPDATA
->m_bitmapMask
;
1330 M_BITMAPDATA
->m_bitmapMask
= mask
;
1333 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1335 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1338 // ----------------------------------------------------------------------------
1340 // ----------------------------------------------------------------------------
1347 // Construct a mask from a bitmap and a colour indicating
1348 // the transparent area
1349 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1352 Create( bitmap
, colour
);
1355 // Construct a mask from a mono bitmap (copies the bitmap).
1356 wxMask::wxMask( const wxBitmap
& bitmap
)
1362 // Construct a mask from a mono bitmap (copies the bitmap).
1364 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1367 Create( data
, width
, height
, bytesPerRow
);
1374 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1375 m_maskBitmap
= NULL
;
1381 m_width
= m_height
= m_bytesPerRow
= 0 ;
1382 m_maskBitmap
= NULL
;
1385 void *wxMask::GetRawAccess() const
1387 return m_memBuf
.GetData() ;
1390 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1391 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1393 void wxMask::RealizeNative()
1397 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1398 m_maskBitmap
= NULL
;
1401 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1403 OSStatus err
= NewGWorldFromPtr(
1404 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1405 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1406 verify_noerr( err
) ;
1409 // Create a mask from a mono bitmap (copies the bitmap).
1411 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1416 m_bytesPerRow
= bytesPerRow
;
1418 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1425 // Create a mask from a mono bitmap (copies the bitmap).
1426 bool wxMask::Create(const wxBitmap
& bitmap
)
1428 m_width
= bitmap
.GetWidth() ;
1429 m_height
= bitmap
.GetHeight() ;
1430 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1432 size_t size
= m_bytesPerRow
* m_height
;
1433 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1434 wxASSERT( destdatabase
!= NULL
) ;
1436 memset( destdatabase
, 0 , size
) ;
1437 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1439 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1441 unsigned char *destdata
= destdatabase
;
1442 unsigned char r
, g
, b
;
1444 for ( int x
= 0 ; x
< m_width
; ++x
)
1451 if ( ( r
+ g
+ b
) > 0x10 )
1453 *destdata
++ = 0xFF ;
1454 *destdata
++ = 0xFF ;
1455 *destdata
++ = 0xFF ;
1456 *destdata
++ = 0xFF ;
1460 *destdata
++ = 0x00 ;
1461 *destdata
++ = 0x00 ;
1462 *destdata
++ = 0x00 ;
1463 *destdata
++ = 0x00 ;
1468 m_memBuf
.UngetWriteBuf( size
) ;
1474 // Create a mask from a bitmap and a colour indicating
1475 // the transparent area
1476 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1478 m_width
= bitmap
.GetWidth() ;
1479 m_height
= bitmap
.GetHeight() ;
1480 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1482 size_t size
= m_bytesPerRow
* m_height
;
1483 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1484 wxASSERT( destdatabase
!= NULL
) ;
1486 memset( destdatabase
, 0 , size
) ;
1487 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1489 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1491 unsigned char *destdata
= destdatabase
;
1492 unsigned char r
, g
, b
;
1494 for ( int x
= 0 ; x
< m_width
; ++x
)
1501 if ( colour
== wxColour( r
, g
, b
) )
1503 *destdata
++ = 0xFF ;
1504 *destdata
++ = 0xFF ;
1505 *destdata
++ = 0xFF ;
1506 *destdata
++ = 0xFF ;
1510 *destdata
++ = 0x00 ;
1511 *destdata
++ = 0x00 ;
1512 *destdata
++ = 0x00 ;
1513 *destdata
++ = 0x00 ;
1518 m_memBuf
.UngetWriteBuf( size
) ;
1524 WXHBITMAP
wxMask::GetHBITMAP() const
1526 return m_maskBitmap
;
1529 // ----------------------------------------------------------------------------
1531 // ----------------------------------------------------------------------------
1533 wxBitmapHandler::~wxBitmapHandler()
1537 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1542 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1543 int desiredWidth
, int desiredHeight
)
1548 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1553 // ----------------------------------------------------------------------------
1554 // Standard Handlers
1555 // ----------------------------------------------------------------------------
1557 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1559 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1562 inline wxPICTResourceHandler()
1564 SetName(wxT("Macintosh Pict resource"));
1565 SetExtension(wxEmptyString
);
1566 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1569 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1570 int desiredWidth
, int desiredHeight
);
1573 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1576 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1577 int desiredWidth
, int desiredHeight
)
1581 wxMacStringToPascal( name
, theName
) ;
1583 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1588 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1589 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1591 dc
.SelectObject( *bitmap
) ;
1593 dc
.SelectObject( wxNullBitmap
) ;
1602 void wxBitmap::InitStandardHandlers()
1604 AddHandler( new wxPICTResourceHandler
) ;
1605 AddHandler( new wxICONResourceHandler
) ;
1608 // ----------------------------------------------------------------------------
1609 // raw bitmap access support
1610 // ----------------------------------------------------------------------------
1612 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1615 // no bitmap, no data (raw or otherwise)
1618 data
.m_width
= GetWidth() ;
1619 data
.m_height
= GetHeight() ;
1620 data
.m_stride
= GetWidth() * 4 ;
1622 return BeginRawAccess() ;
1625 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1630 void wxBitmap::UseAlpha()
1632 // remember that we are using alpha channel:
1633 // we'll need to create a proper mask in UngetRawData()
1634 M_BITMAPDATA
->UseAlpha( true );