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() ;
97 info
->contentType
= kControlContentPictHandle
;
98 info
->u
.picture
= bmap
->GetPictHandle() ;
104 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
106 if ( info
->contentType
== kControlContentIconRef
)
108 ReleaseIconRef( info
->u
.iconRef
) ;
110 else if ( info
->contentType
== kControlNoContent
)
112 // there's no bitmap at all, fall through silently
114 else if ( info
->contentType
== kControlContentPictHandle
)
116 // owned by the bitmap, no release here
118 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
119 else if ( info
->contentType
== kControlContentCGImageRef
)
121 CGImageRelease( info
->u
.imageRef
) ;
126 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
130 #endif //wxUSE_BMPBUTTON
132 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
134 void wxBitmapRefData::Init()
140 m_bitmapMask
= NULL
;
143 m_cgImageRef
= NULL
;
147 m_pictHandle
= NULL
;
149 m_hMaskBitmap
= NULL
;
150 m_maskBytesPerRow
= 0 ;
152 m_rawAccessCount
= 0 ;
156 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
159 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
161 if (tocopy
.m_bitmapMask
)
162 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
164 unsigned char* dest
= (unsigned char*)GetRawAccess();
165 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
166 size_t numbytes
= tocopy
.m_width
* tocopy
.m_height
* 4;
168 for (size_t i
=0; i
<numbytes
; i
++)
173 UseAlpha(tocopy
.m_hasAlpha
);
175 // TODO: Copy palette?
178 wxBitmapRefData::wxBitmapRefData()
183 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
186 Create( w
, h
, d
) ;
189 bool wxBitmapRefData::Create( int w
, int h
, int d
)
191 m_width
= wxMax(1, w
);
192 m_height
= wxMax(1, h
);
195 m_bytesPerRow
= w
* 4 ;
196 size_t size
= m_bytesPerRow
* h
;
197 void* data
= m_memBuf
.GetWriteBuf( size
) ;
198 memset( data
, 0 , size
) ;
199 m_memBuf
.UngetWriteBuf( size
) ;
202 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
204 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
205 (char*) data
, m_bytesPerRow
) ) ;
206 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
208 m_ok
= ( m_hBitmap
!= NULL
) ;
213 void wxBitmapRefData::UseAlpha( bool use
)
215 if ( m_hasAlpha
== use
)
221 wxASSERT( m_hMaskBitmap
== NULL
) ;
223 int width
= GetWidth() ;
224 int height
= GetHeight() ;
225 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
226 size_t size
= height
* m_maskBytesPerRow
;
227 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
228 wxASSERT( data
!= NULL
) ;
230 memset( data
, 0 , size
) ;
231 Rect rect
= { 0 , 0 , height
, width
} ;
233 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
234 (char*) data
, m_maskBytesPerRow
) ) ;
235 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
237 m_maskMemBuf
.UngetWriteBuf(size
) ;
239 #if !wxMAC_USE_CORE_GRAPHICS
246 DisposeGWorld( m_hMaskBitmap
) ;
248 m_hMaskBitmap
= NULL
;
249 m_maskBytesPerRow
= 0 ;
253 void *wxBitmapRefData::GetRawAccess() const
255 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
256 return m_memBuf
.GetData() ;
259 void *wxBitmapRefData::BeginRawAccess()
261 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
262 wxASSERT( m_rawAccessCount
== 0 ) ;
263 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
264 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
269 // we must destroy an existing cached image, as
270 // the bitmap data may change now
273 CGImageRelease( m_cgImageRef
) ;
274 m_cgImageRef
= NULL
;
278 return m_memBuf
.GetData() ;
281 void wxBitmapRefData::EndRawAccess()
283 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
284 wxASSERT( m_rawAccessCount
== 1 ) ;
288 #if !wxMAC_USE_CORE_GRAPHICS
293 bool wxBitmapRefData::HasNativeSize()
296 int h
= GetHeight() ;
297 int sz
= wxMax( w
, h
) ;
299 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
302 IconRef
wxBitmapRefData::GetIconRef()
304 if ( m_iconRef
== NULL
)
306 // Create Icon Family Handle
308 IconFamilyHandle iconFamily
= NULL
;
310 #ifdef WORDS_BIGENDIAN
311 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
312 (**iconFamily
).resourceType
= kIconFamilyType
;
313 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
315 // test this solution on big endian as well
316 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
320 int h
= GetHeight() ;
321 int sz
= wxMax( w
, h
) ;
323 OSType dataType
= 0 ;
324 OSType maskType
= 0 ;
329 dataType
= kThumbnail32BitData
;
330 maskType
= kThumbnail8BitMask
;
334 dataType
= kHuge32BitData
;
335 maskType
= kHuge8BitMask
;
339 dataType
= kLarge32BitData
;
340 maskType
= kLarge8BitMask
;
344 dataType
= kSmall32BitData
;
345 maskType
= kSmall8BitMask
;
354 // setup the header properly
357 Handle maskdata
= NULL
;
358 unsigned char * maskptr
= NULL
;
359 unsigned char * ptr
= NULL
;
360 size_t datasize
, masksize
;
362 datasize
= sz
* sz
* 4 ;
363 data
= NewHandle( datasize
) ;
365 ptr
= (unsigned char*) *data
;
366 memset( ptr
, 0, datasize
) ;
369 maskdata
= NewHandle( masksize
) ;
371 maskptr
= (unsigned char*) *maskdata
;
372 memset( maskptr
, 0 , masksize
) ;
374 bool hasAlpha
= HasAlpha() ;
375 wxMask
*mask
= m_bitmapMask
;
376 unsigned char * source
= (unsigned char*) GetRawAccess() ;
377 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
379 for ( int y
= 0 ; y
< h
; ++y
)
381 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
382 unsigned char * maskdest
= maskptr
+ y
* sz
;
383 unsigned char a
, r
, g
, b
;
385 for ( int x
= 0 ; x
< w
; ++x
)
399 *maskdest
++ = 0xFF - *masksource
++ ;
411 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
412 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
414 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
415 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
418 HUnlock( maskdata
) ;
419 DisposeHandle( data
) ;
420 DisposeHandle( maskdata
) ;
424 PicHandle pic
= GetPictHandle() ;
425 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
427 // transform into IconRef
428 #if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
429 // cleaner version existing from 10.3 upwards
430 HLock((Handle
) iconFamily
);
431 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
432 HUnlock((Handle
) iconFamily
);
433 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
435 static int iconCounter
= 2 ;
437 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
438 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
440 // we have to retain a reference, as Unregister will decrement it
441 AcquireIconRef( m_iconRef
) ;
442 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
445 DisposeHandle( (Handle
) iconFamily
) ;
451 PicHandle
wxBitmapRefData::GetPictHandle()
453 if ( m_pictHandle
== NULL
)
456 CGrafPtr origPort
= NULL
;
457 GDHandle origDev
= NULL
;
458 GWorldPtr wp
= NULL
;
459 GWorldPtr mask
= NULL
;
460 int height
= GetHeight() ;
461 int width
= GetWidth() ;
463 Rect rect
= { 0 , 0 , height
, width
} ;
464 RgnHandle clipRgn
= NULL
;
466 GetGWorld( &origPort
, &origDev
) ;
467 wp
= GetHBITMAP( &mask
) ;
471 GWorldPtr monoworld
;
473 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
475 LockPixels( GetGWorldPixMap( monoworld
) ) ;
476 LockPixels( GetGWorldPixMap( mask
) ) ;
477 SetGWorld( monoworld
, NULL
) ;
479 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
480 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
481 RGBForeColor( &black
) ;
482 RGBBackColor( &white
) ;
484 CopyBits(GetPortBitMapForCopyBits(mask
),
485 GetPortBitMapForCopyBits(monoworld
),
489 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
491 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
492 UnlockPixels( GetGWorldPixMap( mask
) ) ;
493 DisposeGWorld( monoworld
) ;
496 SetGWorld( wp
, NULL
) ;
498 GetPortBounds( wp
, &portRect
) ;
499 m_pictHandle
= OpenPicture(&portRect
);
503 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
504 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
506 RGBForeColor( &black
) ;
507 RGBBackColor( &white
) ;
512 LockPixels( GetGWorldPixMap( wp
) ) ;
513 CopyBits(GetPortBitMapForCopyBits(wp
),
514 GetPortBitMapForCopyBits(wp
),
518 UnlockPixels( GetGWorldPixMap( wp
) ) ;
522 SetGWorld( origPort
, origDev
) ;
524 DisposeRgn( clipRgn
) ;
528 return m_pictHandle
;
532 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t WXUNUSED(size
))
534 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
536 wxASSERT( data
== membuf
->GetData() ) ;
541 CGImageRef
wxBitmapRefData::CGImageCreate() const
544 wxASSERT( m_rawAccessCount
>= 0 ) ;
546 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
548 size_t imageSize
= m_width
* m_height
* 4 ;
549 void * dataBuffer
= m_memBuf
.GetData() ;
552 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
553 wxMemoryBuffer
* membuf
= NULL
;
557 alphaInfo
= kCGImageAlphaFirst
;
558 membuf
= new wxMemoryBuffer( imageSize
) ;
559 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
560 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
561 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
562 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
563 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
565 unsigned char *sourcemask
= sourcemaskstart
;
566 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 , destalpha
+= 4 )
568 *destalpha
= 0xFF - *sourcemask
;
576 #if wxMAC_USE_PREMULTIPLIED_ALPHA
577 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
579 alphaInfo
= kCGImageAlphaFirst
;
583 membuf
= new wxMemoryBuffer( m_memBuf
) ;
586 CGDataProviderRef dataProvider
= NULL
;
589 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
590 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
591 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
592 // copy one color component
593 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
594 maskBufData
[i
] = bufData
[i
*4+3];
596 CGDataProviderCreateWithData(
597 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
598 wxMacMemoryBufferReleaseProc
);
599 // as we are now passing the mask buffer to the data provider, we have
600 // to release the membuf ourselves
603 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
607 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
609 CGDataProviderCreateWithData(
610 membuf
, (const void *)membuf
->GetData() , imageSize
,
611 wxMacMemoryBufferReleaseProc
);
614 w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
615 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
617 CGDataProviderRelease( dataProvider
);
621 image
= m_cgImageRef
;
622 CGImageRetain( image
) ;
625 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
627 // we keep it for later use
628 m_cgImageRef
= image
;
629 CGImageRetain( image
) ;
636 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
638 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
644 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
646 else if ( m_hasAlpha
)
648 #if !wxMAC_USE_CORE_GRAPHICS
649 if ( m_rawAccessCount
> 0 )
652 // this structure is not kept in synch when using CG, so if something
653 // is really accessing the GrafPorts, we have to sync it
657 *mask
= m_hMaskBitmap
;
664 void wxBitmapRefData::UpdateAlphaMask() const
668 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
669 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
671 int h
= GetHeight() ;
674 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
676 unsigned char* destalpha
= destalphabase
;
678 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
680 // we must have 24 bit depth for non quartz smooth alpha
682 *destalpha
++ = 255 - *sourcemask
;
683 *destalpha
++ = 255 - *sourcemask
;
684 *destalpha
++ = 255 - *sourcemask
;
690 void wxBitmapRefData::Free()
692 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
697 CGImageRelease( m_cgImageRef
) ;
698 m_cgImageRef
= NULL
;
704 ReleaseIconRef( m_iconRef
) ;
711 KillPicture( m_pictHandle
) ;
712 m_pictHandle
= NULL
;
717 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
723 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
724 m_hMaskBitmap
= NULL
;
734 wxBitmapRefData::~wxBitmapRefData()
739 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
741 bool created
= false ;
742 int w
= icon
.GetWidth() ;
743 int h
= icon
.GetHeight() ;
745 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
747 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
749 IconFamilyHandle iconFamily
= NULL
;
750 Handle imagehandle
= NewHandle( 0 ) ;
751 Handle maskhandle
= NewHandle( 0 ) ;
755 IconSelectorValue selector
= 0 ;
760 dataType
= kThumbnail32BitData
;
761 maskType
= kThumbnail8BitMask
;
762 selector
= kSelectorAllAvailableData
;
766 dataType
= kHuge32BitData
;
767 maskType
= kHuge8BitMask
;
768 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
772 dataType
= kLarge32BitData
;
773 maskType
= kLarge8BitMask
;
774 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
778 dataType
= kSmall32BitData
;
779 maskType
= kSmall8BitMask
;
780 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
787 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
789 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
790 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
791 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
792 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
794 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
796 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
797 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
801 unsigned char *source
= (unsigned char *) *imagehandle
;
802 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
803 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
805 for ( int y
= 0 ; y
< h
; ++y
)
807 for ( int x
= 0 ; x
< w
; ++x
)
809 *destination
++ = *sourcemask
++ ;
811 *destination
++ = *source
++ ;
812 *destination
++ = *source
++ ;
813 *destination
++ = *source
++ ;
818 DisposeHandle( imagehandle
) ;
819 DisposeHandle( maskhandle
) ;
823 DisposeHandle( (Handle
) iconFamily
) ;
829 dc
.SelectObject( *this ) ;
830 dc
.DrawIcon( icon
, 0 , 0 ) ;
831 dc
.SelectObject( wxNullBitmap
) ;
841 wxBitmap::~wxBitmap()
845 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
847 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
851 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
852 if ( the_width
% (sizeof(unsigned char) * 8) )
853 linesize
+= sizeof(unsigned char);
855 unsigned char* linestart
= (unsigned char*) bits
;
856 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
858 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
860 int index
, bit
, mask
;
862 for ( int x
= 0 ; x
< the_width
; ++x
)
868 if ( linestart
[index
] & mask
)
870 *destination
++ = 0xFF ;
877 *destination
++ = 0xFF ;
878 *destination
++ = 0xFF ;
879 *destination
++ = 0xFF ;
880 *destination
++ = 0xFF ;
889 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
893 wxBitmap::wxBitmap(int w
, int h
, int d
)
895 (void)Create(w
, h
, d
);
898 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
900 (void) Create(data
, type
, width
, height
, depth
);
903 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
905 LoadFile(filename
, type
);
908 wxObjectRefData
* wxBitmap::CreateRefData() const
910 return new wxBitmapRefData
;
913 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
915 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
918 void * wxBitmap::GetRawAccess() const
920 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
922 return M_BITMAPDATA
->GetRawAccess() ;
925 void * wxBitmap::BeginRawAccess()
927 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
929 return M_BITMAPDATA
->BeginRawAccess() ;
932 void wxBitmap::EndRawAccess()
934 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
936 M_BITMAPDATA
->EndRawAccess() ;
940 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
942 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
944 return M_BITMAPDATA
->CGImageCreate() ;
948 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
951 (rect
.x
>= 0) && (rect
.y
>= 0) &&
952 (rect
.x
+rect
.width
<= GetWidth()) &&
953 (rect
.y
+rect
.height
<= GetHeight()),
954 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
956 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
957 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
959 int sourcewidth
= GetWidth() ;
960 int destwidth
= rect
.width
;
961 int destheight
= rect
.height
;
964 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
965 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
966 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
968 int sourcelinesize
= sourcewidth
* 4 ;
969 int destlinesize
= destwidth
* 4 ;
970 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
971 unsigned char *dest
= destdata
;
973 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
975 memcpy( dest
, source
, destlinesize
) ;
981 if ( M_BITMAPDATA
->m_bitmapMask
)
983 wxMemoryBuffer maskbuf
;
984 int rowBytes
= ( destwidth
* 4 + 3 ) & 0xFFFFFFC ;
985 size_t maskbufsize
= rowBytes
* destheight
;
987 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
988 int destlinesize
= rowBytes
;
990 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
991 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
992 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
994 source
+= rect
.x
* 4 + rect
.y
* sourcelinesize
;
995 unsigned char *dest
= destdata
;
997 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
999 memcpy( dest
, source
, destlinesize
) ;
1002 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1003 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1005 else if ( HasAlpha() )
1011 bool wxBitmap::Create(int w
, int h
, int d
)
1016 d
= wxDisplayDepth() ;
1018 m_refData
= new wxBitmapRefData( w
, h
, d
);
1020 return M_BITMAPDATA
->Ok() ;
1023 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1027 wxBitmapHandler
*handler
= FindHandler(type
);
1031 m_refData
= new wxBitmapRefData
;
1033 return handler
->LoadFile(this, filename
, type
, -1, -1);
1038 wxImage
loadimage(filename
, type
);
1048 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1053 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1057 m_refData
= new wxBitmapRefData
;
1059 wxBitmapHandler
*handler
= FindHandler(type
);
1061 if ( handler
== NULL
)
1063 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1068 return handler
->Create(this, data
, type
, width
, height
, depth
);
1073 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1075 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1077 // width and height of the device-dependent bitmap
1078 int width
= image
.GetWidth();
1079 int height
= image
.GetHeight();
1081 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1085 bool hasAlpha
= false ;
1087 if ( image
.HasMask() )
1089 // takes precedence, don't mix with alpha info
1093 hasAlpha
= image
.HasAlpha() ;
1099 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
1100 register unsigned char* data
= image
.GetData();
1101 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1103 for (int y
= 0; y
< height
; y
++)
1105 for (int x
= 0; x
< width
; x
++)
1109 const unsigned char a
= *alpha
++;
1110 *destination
++ = a
;
1112 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1113 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1114 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1115 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1117 *destination
++ = *data
++ ;
1118 *destination
++ = *data
++ ;
1119 *destination
++ = *data
++ ;
1124 *destination
++ = 0xFF ;
1125 *destination
++ = *data
++ ;
1126 *destination
++ = *data
++ ;
1127 *destination
++ = *data
++ ;
1133 if ( image
.HasMask() )
1134 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1137 wxImage
wxBitmap::ConvertToImage() const
1141 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1143 // create an wxImage object
1144 int width
= GetWidth();
1145 int height
= GetHeight();
1146 image
.Create( width
, height
);
1148 unsigned char *data
= image
.GetData();
1149 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1151 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1153 bool hasAlpha
= false ;
1154 bool hasMask
= false ;
1155 int maskBytesPerRow
= 0 ;
1156 unsigned char *alpha
= NULL
;
1157 unsigned char *mask
= NULL
;
1165 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1166 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1172 alpha
= image
.GetAlpha() ;
1177 // The following masking algorithm is the same as well in msw/gtk:
1178 // the colour used as transparent one in wxImage and the one it is
1179 // replaced with when it actually occurs in the bitmap
1180 static const int MASK_RED
= 1;
1181 static const int MASK_GREEN
= 2;
1182 static const int MASK_BLUE
= 3;
1183 static const int MASK_BLUE_REPLACEMENT
= 2;
1185 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1187 unsigned char * maskp
= mask
;
1188 unsigned char a
, r
, g
, b
;
1191 for (int xx
= 0; xx
< width
; xx
++)
1193 color
= *((long*) source
) ;
1194 #ifdef WORDS_BIGENDIAN
1195 a
= ((color
&0xFF000000) >> 24) ;
1196 r
= ((color
&0x00FF0000) >> 16) ;
1197 g
= ((color
&0x0000FF00) >> 8) ;
1198 b
= (color
&0x000000FF);
1200 b
= ((color
&0xFF000000) >> 24) ;
1201 g
= ((color
&0x00FF0000) >> 16) ;
1202 r
= ((color
&0x0000FF00) >> 8) ;
1203 a
= (color
&0x000000FF);
1207 if ( *maskp
++ == 0xFF )
1213 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1214 b
= MASK_BLUE_REPLACEMENT
;
1220 else if ( hasAlpha
)
1224 data
[index
+ 1] = g
;
1225 data
[index
+ 2] = b
;
1233 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1238 #endif //wxUSE_IMAGE
1240 bool wxBitmap::SaveFile( const wxString
& filename
,
1241 wxBitmapType type
, const wxPalette
*palette
) const
1243 bool success
= false;
1244 wxBitmapHandler
*handler
= FindHandler(type
);
1248 success
= handler
->SaveFile(this, filename
, type
, palette
);
1253 wxImage image
= ConvertToImage();
1254 success
= image
.SaveFile(filename
, type
);
1256 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1263 bool wxBitmap::IsOk() const
1265 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1268 int wxBitmap::GetHeight() const
1270 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1272 return M_BITMAPDATA
->GetHeight();
1275 int wxBitmap::GetWidth() const
1277 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1279 return M_BITMAPDATA
->GetWidth() ;
1282 int wxBitmap::GetDepth() const
1284 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1286 return M_BITMAPDATA
->GetDepth();
1289 wxMask
*wxBitmap::GetMask() const
1291 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1293 return M_BITMAPDATA
->m_bitmapMask
;
1296 bool wxBitmap::HasAlpha() const
1298 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1300 return M_BITMAPDATA
->HasAlpha() ;
1303 void wxBitmap::SetWidth(int w
)
1306 M_BITMAPDATA
->SetWidth(w
);
1309 void wxBitmap::SetHeight(int h
)
1312 M_BITMAPDATA
->SetHeight(h
);
1315 void wxBitmap::SetDepth(int d
)
1318 M_BITMAPDATA
->SetDepth(d
);
1321 void wxBitmap::SetOk(bool isOk
)
1324 M_BITMAPDATA
->SetOk(isOk
);
1328 wxPalette
*wxBitmap::GetPalette() const
1330 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1332 return &M_BITMAPDATA
->m_bitmapPalette
;
1335 void wxBitmap::SetPalette(const wxPalette
& palette
)
1338 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1340 #endif // wxUSE_PALETTE
1342 void wxBitmap::SetMask(wxMask
*mask
)
1345 // Remove existing mask if there is one.
1346 delete M_BITMAPDATA
->m_bitmapMask
;
1348 M_BITMAPDATA
->m_bitmapMask
= mask
;
1351 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1353 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1356 // ----------------------------------------------------------------------------
1358 // ----------------------------------------------------------------------------
1365 wxMask::wxMask(const wxMask
&tocopy
)
1369 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1370 m_width
= tocopy
.m_width
;
1371 m_height
= tocopy
.m_height
;
1373 size_t size
= m_bytesPerRow
* m_height
;
1374 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1375 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1376 for (size_t i
=0; i
<size
; i
++)
1378 *dest
++ = *source
++;
1381 m_memBuf
.UngetWriteBuf( size
) ;
1385 // Construct a mask from a bitmap and a colour indicating
1386 // the transparent area
1387 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1390 Create( bitmap
, colour
);
1393 // Construct a mask from a mono bitmap (copies the bitmap).
1394 wxMask::wxMask( const wxBitmap
& bitmap
)
1400 // Construct a mask from a mono bitmap (copies the bitmap).
1402 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1405 Create( data
, width
, height
, bytesPerRow
);
1413 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1414 m_maskBitmap
= NULL
;
1421 m_width
= m_height
= m_bytesPerRow
= 0 ;
1422 m_maskBitmap
= NULL
;
1425 void *wxMask::GetRawAccess() const
1427 return m_memBuf
.GetData() ;
1430 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1431 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1433 void wxMask::RealizeNative()
1438 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1439 m_maskBitmap
= NULL
;
1442 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1444 OSStatus err
= NewGWorldFromPtr(
1445 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1446 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1447 verify_noerr( err
) ;
1451 // Create a mask from a mono bitmap (copies the bitmap).
1453 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1458 m_bytesPerRow
= bytesPerRow
;
1460 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1467 // Create a mask from a mono bitmap (copies the bitmap).
1468 bool wxMask::Create(const wxBitmap
& bitmap
)
1470 m_width
= bitmap
.GetWidth() ;
1471 m_height
= bitmap
.GetHeight() ;
1472 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1474 size_t size
= m_bytesPerRow
* m_height
;
1475 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1476 wxASSERT( destdatabase
!= NULL
) ;
1478 memset( destdatabase
, 0 , size
) ;
1479 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1481 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1483 unsigned char *destdata
= destdatabase
;
1484 unsigned char r
, g
, b
;
1486 for ( int x
= 0 ; x
< m_width
; ++x
)
1493 if ( ( r
+ g
+ b
) > 0x10 )
1495 *destdata
++ = 0xFF ;
1496 *destdata
++ = 0xFF ;
1497 *destdata
++ = 0xFF ;
1498 *destdata
++ = 0xFF ;
1502 *destdata
++ = 0x00 ;
1503 *destdata
++ = 0x00 ;
1504 *destdata
++ = 0x00 ;
1505 *destdata
++ = 0x00 ;
1510 m_memBuf
.UngetWriteBuf( size
) ;
1516 // Create a mask from a bitmap and a colour indicating
1517 // the transparent area
1518 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1520 m_width
= bitmap
.GetWidth() ;
1521 m_height
= bitmap
.GetHeight() ;
1522 m_bytesPerRow
= ( m_width
* 4 + 3 ) & 0xFFFFFFC ;
1524 size_t size
= m_bytesPerRow
* m_height
;
1525 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1526 wxASSERT( destdatabase
!= NULL
) ;
1528 memset( destdatabase
, 0 , size
) ;
1529 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1531 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1533 unsigned char *destdata
= destdatabase
;
1534 unsigned char r
, g
, b
;
1536 for ( int x
= 0 ; x
< m_width
; ++x
)
1543 if ( colour
== wxColour( r
, g
, b
) )
1545 *destdata
++ = 0xFF ;
1546 *destdata
++ = 0xFF ;
1547 *destdata
++ = 0xFF ;
1548 *destdata
++ = 0xFF ;
1552 *destdata
++ = 0x00 ;
1553 *destdata
++ = 0x00 ;
1554 *destdata
++ = 0x00 ;
1555 *destdata
++ = 0x00 ;
1560 m_memBuf
.UngetWriteBuf( size
) ;
1566 WXHBITMAP
wxMask::GetHBITMAP() const
1568 return m_maskBitmap
;
1571 // ----------------------------------------------------------------------------
1573 // ----------------------------------------------------------------------------
1575 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1577 // ----------------------------------------------------------------------------
1578 // Standard Handlers
1579 // ----------------------------------------------------------------------------
1581 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1583 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1586 inline wxPICTResourceHandler()
1588 SetName(wxT("Macintosh Pict resource"));
1589 SetExtension(wxEmptyString
);
1590 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1593 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1594 int desiredWidth
, int desiredHeight
);
1597 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1600 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1601 const wxString
& name
,
1602 long WXUNUSED(flags
),
1603 int WXUNUSED(desiredWidth
),
1604 int WXUNUSED(desiredHeight
))
1608 wxMacStringToPascal( name
, theName
) ;
1610 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1615 mf
.SetHMETAFILE( (WXHMETAFILE
) thePict
) ;
1616 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1618 dc
.SelectObject( *bitmap
) ;
1620 dc
.SelectObject( wxNullBitmap
) ;
1629 void wxBitmap::InitStandardHandlers()
1631 AddHandler( new wxPICTResourceHandler
) ;
1632 AddHandler( new wxICONResourceHandler
) ;
1635 // ----------------------------------------------------------------------------
1636 // raw bitmap access support
1637 // ----------------------------------------------------------------------------
1639 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1642 // no bitmap, no data (raw or otherwise)
1645 data
.m_width
= GetWidth() ;
1646 data
.m_height
= GetHeight() ;
1647 data
.m_stride
= GetWidth() * 4 ;
1649 return BeginRawAccess() ;
1652 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1657 void wxBitmap::UseAlpha()
1659 // remember that we are using alpha channel:
1660 // we'll need to create a proper mask in UngetRawData()
1661 M_BITMAPDATA
->UseAlpha( true );