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
)
31 #include <ApplicationServices/ApplicationServices.h>
32 #include <QuickTime/QuickTime.h>
34 #include "wx/mac/uma.h"
36 // Implementation Notes
37 // --------------------
39 // we are always working with a 32 bit deep pixel buffer
40 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
41 // therefore we have a separate GWorld there for blitting the mask in
43 // under Quartz then content is transformed into a CGImageRef representing the same data
44 // which can be transferred to the GPU by the OS for fast rendering
46 #define wxMAC_USE_PREMULTIPLIED_ALPHA 1
47 static const int kBestByteAlignement
= 16;
48 static const int kMaskBytesPerPixel
= 1;
50 static int GetBestBytesPerRow( int rawBytes
)
52 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
57 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
59 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
62 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
68 forceType
= kControlContentCGImageRef
;
71 if ( forceType
== kControlContentIconRef
)
74 wxBitmapRefData
* bmp
= bmap
;
76 if ( !bmap
->HasNativeSize() )
78 // as PICT conversion will only result in a 16x16 icon, let's attempt
79 // a few scales for better results
81 int w
= bitmap
.GetWidth() ;
82 int h
= bitmap
.GetHeight() ;
83 int sz
= wxMax( w
, h
) ;
84 if ( sz
== 24 || sz
== 64 )
86 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
87 bmp
= scaleBmp
.GetBitmapData() ;
91 info
->contentType
= kControlContentIconRef
;
92 info
->u
.iconRef
= bmp
->GetIconRef() ;
93 AcquireIconRef( info
->u
.iconRef
) ;
95 else if ( forceType
== kControlContentCGImageRef
)
97 info
->contentType
= kControlContentCGImageRef
;
98 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
103 info
->contentType
= kControlContentPictHandle
;
104 info
->u
.picture
= bmap
->GetPictHandle() ;
110 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
112 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
115 return (CGImageRef
) bmap
->CGImageCreate();
118 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
120 if ( info
->contentType
== kControlContentIconRef
)
122 ReleaseIconRef( info
->u
.iconRef
) ;
124 else if ( info
->contentType
== kControlNoContent
)
126 // there's no bitmap at all, fall through silently
128 else if ( info
->contentType
== kControlContentPictHandle
)
130 // owned by the bitmap, no release here
132 else if ( info
->contentType
== kControlContentCGImageRef
)
134 CGImageRelease( info
->u
.imageRef
) ;
138 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
142 #endif //wxUSE_BMPBUTTON
144 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
146 void wxBitmapRefData::Init()
153 m_bitmapMask
= NULL
;
154 m_cgImageRef
= NULL
;
157 m_pictHandle
= NULL
;
160 m_rawAccessCount
= 0 ;
164 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
167 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
169 if (tocopy
.m_bitmapMask
)
170 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
171 else if (tocopy
.m_hasAlpha
)
174 unsigned char* dest
= (unsigned char*)GetRawAccess();
175 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
176 size_t numbytes
= m_bytesPerRow
* m_height
;
177 memcpy( dest
, source
, numbytes
);
180 wxBitmapRefData::wxBitmapRefData()
185 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
188 Create( w
, h
, d
) ;
191 bool wxBitmapRefData::Create( int w
, int h
, int d
)
193 m_width
= wxMax(1, w
);
194 m_height
= wxMax(1, h
);
197 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
198 size_t size
= m_bytesPerRow
* h
;
199 void* data
= m_memBuf
.GetWriteBuf( size
) ;
200 memset( data
, 0 , size
) ;
201 m_memBuf
.UngetWriteBuf( size
) ;
204 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
205 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
206 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
207 CGContextScaleCTM( m_hBitmap
, 1, -1 );
209 m_ok
= ( m_hBitmap
!= NULL
) ;
214 void wxBitmapRefData::UseAlpha( bool use
)
216 if ( m_hasAlpha
== use
)
221 CGContextRelease( m_hBitmap
);
222 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
223 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
224 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
225 CGContextScaleCTM( m_hBitmap
, 1, -1 );
228 void *wxBitmapRefData::GetRawAccess() const
230 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
231 return m_memBuf
.GetData() ;
234 void *wxBitmapRefData::BeginRawAccess()
236 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
237 wxASSERT( m_rawAccessCount
== 0 ) ;
238 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
239 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
243 // we must destroy an existing cached image, as
244 // the bitmap data may change now
247 CGImageRelease( m_cgImageRef
) ;
248 m_cgImageRef
= NULL
;
251 return m_memBuf
.GetData() ;
254 void wxBitmapRefData::EndRawAccess()
256 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
257 wxASSERT( m_rawAccessCount
== 1 ) ;
262 bool wxBitmapRefData::HasNativeSize()
265 int h
= GetHeight() ;
266 int sz
= wxMax( w
, h
) ;
268 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
271 IconRef
wxBitmapRefData::GetIconRef()
273 if ( m_iconRef
== NULL
)
275 // Create Icon Family Handle
277 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
280 int h
= GetHeight() ;
281 int sz
= wxMax( w
, h
) ;
283 OSType dataType
= 0 ;
284 OSType maskType
= 0 ;
289 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
290 if ( UMAGetSystemVersion() >= 0x1050 )
292 dataType
= kIconServices128PixelDataARGB
;
297 dataType
= kThumbnail32BitData
;
298 maskType
= kThumbnail8BitMask
;
303 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
304 if ( UMAGetSystemVersion() >= 0x1050 )
306 dataType
= kIconServices48PixelDataARGB
;
311 dataType
= kHuge32BitData
;
312 maskType
= kHuge8BitMask
;
317 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
318 if ( UMAGetSystemVersion() >= 0x1050 )
320 dataType
= kIconServices32PixelDataARGB
;
325 dataType
= kLarge32BitData
;
326 maskType
= kLarge8BitMask
;
331 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
332 if ( UMAGetSystemVersion() >= 0x1050 )
334 dataType
= kIconServices16PixelDataARGB
;
339 dataType
= kSmall32BitData
;
340 maskType
= kSmall8BitMask
;
350 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
351 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
353 size_t datasize
= sz
* sz
* 4 ;
354 Handle data
= NewHandle( datasize
) ;
356 unsigned char* ptr
= (unsigned char*) *data
;
357 memset( ptr
, 0, datasize
);
358 bool hasAlpha
= HasAlpha() ;
359 wxMask
*mask
= m_bitmapMask
;
360 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
361 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
363 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
365 unsigned char * source
= sourcePtr
;
366 unsigned char * masksource
= masksourcePtr
;
367 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
368 unsigned char a
, r
, g
, b
;
370 for ( int x
= 0 ; x
< w
; ++x
)
379 a
= 0xFF - *masksource
++ ;
381 else if ( !hasAlpha
)
385 #if wxMAC_USE_PREMULTIPLIED_ALPHA
386 // this must be non-premultiplied data
387 if ( a
!= 0xFF && a
!= 0 )
403 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
404 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
405 DisposeHandle( data
);
410 // setup the header properly
413 Handle maskdata
= NULL
;
414 unsigned char * maskptr
= NULL
;
415 unsigned char * ptr
= NULL
;
416 size_t datasize
, masksize
;
418 datasize
= sz
* sz
* 4 ;
419 data
= NewHandle( datasize
) ;
421 ptr
= (unsigned char*) *data
;
422 memset( ptr
, 0, datasize
) ;
425 maskdata
= NewHandle( masksize
) ;
427 maskptr
= (unsigned char*) *maskdata
;
428 memset( maskptr
, 0 , masksize
) ;
430 bool hasAlpha
= HasAlpha() ;
431 wxMask
*mask
= m_bitmapMask
;
432 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
433 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
435 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
437 unsigned char * source
= sourcePtr
;
438 unsigned char * masksource
= masksourcePtr
;
439 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
440 unsigned char * maskdest
= maskptr
+ y
* sz
;
441 unsigned char a
, r
, g
, b
;
443 for ( int x
= 0 ; x
< w
; ++x
)
456 *maskdest
++ = 0xFF - *masksource
++ ;
464 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
465 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
467 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
468 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
471 HUnlock( maskdata
) ;
472 DisposeHandle( data
) ;
473 DisposeHandle( maskdata
) ;
478 PicHandle pic
= GetPictHandle() ;
479 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
481 // transform into IconRef
483 // cleaner version existing from 10.3 upwards
484 HLock((Handle
) iconFamily
);
485 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
486 HUnlock((Handle
) iconFamily
);
487 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
488 DisposeHandle( (Handle
) iconFamily
) ;
494 PicHandle
wxBitmapRefData::GetPictHandle()
496 if ( m_pictHandle
== NULL
)
499 GraphicsExportComponent exporter
= 0;
500 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
503 m_pictHandle
= (PicHandle
) NewHandle(0);
506 // QT does not correctly export the mask
507 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
508 CGImageRef imageRef
= CGImageCreate();
509 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
510 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
511 err
= GraphicsExportDoExport(exporter
, NULL
);
512 CGImageRelease( imageRef
);
514 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
515 // the 512 bytes header is only needed for pict files, but not in memory
516 if ( handleSize
>= 512 )
518 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
519 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
522 CloseComponent( exporter
);
527 return m_pictHandle
;
530 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t WXUNUSED(size
))
532 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
534 wxASSERT( data
== membuf
->GetData() ) ;
539 CGImageRef
wxBitmapRefData::CGImageCreate() const
542 wxASSERT( m_rawAccessCount
>= 0 ) ;
544 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
546 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
550 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
551 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
552 image
= CGImageCreateWithMask( tempImage
, tempMask
);
553 CGImageRelease(tempMask
);
554 CGImageRelease(tempImage
);
557 image
= CGBitmapContextCreateImage( m_hBitmap
);
561 size_t imageSize
= m_height
* m_bytesPerRow
;
562 void * dataBuffer
= m_memBuf
.GetData() ;
565 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
566 wxMemoryBuffer
* membuf
= NULL
;
570 alphaInfo
= kCGImageAlphaFirst
;
571 membuf
= new wxMemoryBuffer( imageSize
) ;
572 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
573 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
574 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
575 unsigned char *destalphastart
= (unsigned char *) membuf
->GetData() ;
576 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
578 unsigned char *sourcemask
= sourcemaskstart
;
579 unsigned char *destalpha
= destalphastart
;
580 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
582 *destalpha
= 0xFF - *sourcemask
;
590 #if wxMAC_USE_PREMULTIPLIED_ALPHA
591 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
593 alphaInfo
= kCGImageAlphaFirst
;
597 membuf
= new wxMemoryBuffer( m_memBuf
) ;
600 CGDataProviderRef dataProvider
= NULL
;
603 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
604 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
605 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
606 // copy one color component
607 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
608 maskBufData
[i
] = bufData
[i
*4+3];
610 CGDataProviderCreateWithData(
611 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
612 wxMacMemoryBufferReleaseProc
);
613 // as we are now passing the mask buffer to the data provider, we have
614 // to release the membuf ourselves
617 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
621 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
623 CGDataProviderCreateWithData(
624 membuf
, (const void *)membuf
->GetData() , imageSize
,
625 wxMacMemoryBufferReleaseProc
);
628 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
629 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
631 CGDataProviderRelease( dataProvider
);
636 image
= m_cgImageRef
;
637 CGImageRetain( image
) ;
640 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
642 // we keep it for later use
643 m_cgImageRef
= image
;
644 CGImageRetain( image
) ;
650 CGContextRef
wxBitmapRefData::GetBitmapContext() const
655 void wxBitmapRefData::Free()
657 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
661 CGImageRelease( m_cgImageRef
) ;
662 m_cgImageRef
= NULL
;
667 ReleaseIconRef( m_iconRef
) ;
674 KillPicture( m_pictHandle
) ;
675 m_pictHandle
= NULL
;
681 CGContextRelease(m_hBitmap
);
692 wxBitmapRefData::~wxBitmapRefData()
697 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
699 bool created
= false ;
700 int w
= icon
.GetWidth() ;
701 int h
= icon
.GetHeight() ;
703 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
705 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
707 IconFamilyHandle iconFamily
= NULL
;
708 Handle imagehandle
= NewHandle( 0 ) ;
709 Handle maskhandle
= NewHandle( 0 ) ;
713 IconSelectorValue selector
= 0 ;
718 dataType
= kThumbnail32BitData
;
719 maskType
= kThumbnail8BitMask
;
720 selector
= kSelectorAllAvailableData
;
724 dataType
= kHuge32BitData
;
725 maskType
= kHuge8BitMask
;
726 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
730 dataType
= kLarge32BitData
;
731 maskType
= kLarge8BitMask
;
732 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
736 dataType
= kSmall32BitData
;
737 maskType
= kSmall8BitMask
;
738 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
745 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
747 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
748 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
749 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
750 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
752 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
754 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
755 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
759 unsigned char *source
= (unsigned char *) *imagehandle
;
760 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
761 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
763 for ( int y
= 0 ; y
< h
; ++y
)
765 for ( int x
= 0 ; x
< w
; ++x
)
767 unsigned char a
= *sourcemask
++;
770 #if wxMAC_USE_PREMULTIPLIED_ALPHA
771 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
772 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
773 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
775 *destination
++ = *source
++ ;
776 *destination
++ = *source
++ ;
777 *destination
++ = *source
++ ;
783 DisposeHandle( imagehandle
) ;
784 DisposeHandle( maskhandle
) ;
788 DisposeHandle( (Handle
) iconFamily
) ;
794 dc
.SelectObject( *this ) ;
795 dc
.DrawIcon( icon
, 0 , 0 ) ;
796 dc
.SelectObject( wxNullBitmap
) ;
806 wxBitmap::~wxBitmap()
810 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
812 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
816 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
817 if ( the_width
% (sizeof(unsigned char) * 8) )
818 linesize
+= sizeof(unsigned char);
820 unsigned char* linestart
= (unsigned char*) bits
;
821 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
823 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
825 unsigned char* destination
= destptr
;
826 int index
, bit
, mask
;
828 for ( int x
= 0 ; x
< the_width
; ++x
)
834 if ( linestart
[index
] & mask
)
836 *destination
++ = 0xFF ;
843 *destination
++ = 0xFF ;
844 *destination
++ = 0xFF ;
845 *destination
++ = 0xFF ;
846 *destination
++ = 0xFF ;
855 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
859 wxBitmap::wxBitmap(int w
, int h
, int d
)
861 (void)Create(w
, h
, d
);
864 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
866 (void) Create(data
, type
, width
, height
, depth
);
869 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
871 LoadFile(filename
, type
);
874 wxObjectRefData
* wxBitmap::CreateRefData() const
876 return new wxBitmapRefData
;
879 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
881 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
884 void * wxBitmap::GetRawAccess() const
886 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
888 return M_BITMAPDATA
->GetRawAccess() ;
891 void * wxBitmap::BeginRawAccess()
893 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
895 return M_BITMAPDATA
->BeginRawAccess() ;
898 void wxBitmap::EndRawAccess()
900 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
902 M_BITMAPDATA
->EndRawAccess() ;
905 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
907 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
909 return M_BITMAPDATA
->CGImageCreate() ;
912 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
915 (rect
.x
>= 0) && (rect
.y
>= 0) &&
916 (rect
.x
+rect
.width
<= GetWidth()) &&
917 (rect
.y
+rect
.height
<= GetHeight()),
918 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
920 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
921 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
923 int destwidth
= rect
.width
;
924 int destheight
= rect
.height
;
927 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
928 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
929 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
931 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
932 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
933 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
934 unsigned char *dest
= destdata
;
936 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
938 memcpy( dest
, source
, destlinesize
) ;
944 if ( M_BITMAPDATA
->m_bitmapMask
)
946 wxMemoryBuffer maskbuf
;
947 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
948 size_t maskbufsize
= rowBytes
* destheight
;
950 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
951 int destlinesize
= rowBytes
;
953 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
954 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
955 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
957 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
958 unsigned char *dest
= destdata
;
960 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
962 memcpy( dest
, source
, destlinesize
) ;
965 maskbuf
.UngetWriteBuf( maskbufsize
) ;
966 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
968 else if ( HasAlpha() )
974 bool wxBitmap::Create(int w
, int h
, int d
)
979 d
= wxDisplayDepth() ;
981 m_refData
= new wxBitmapRefData( w
, h
, d
);
983 return M_BITMAPDATA
->Ok() ;
986 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
990 wxBitmapHandler
*handler
= FindHandler(type
);
994 m_refData
= new wxBitmapRefData
;
996 return handler
->LoadFile(this, filename
, type
, -1, -1);
1001 wxImage
loadimage(filename
, type
);
1011 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1016 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1020 m_refData
= new wxBitmapRefData
;
1022 wxBitmapHandler
*handler
= FindHandler(type
);
1024 if ( handler
== NULL
)
1026 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1031 return handler
->Create(this, data
, type
, width
, height
, depth
);
1036 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1038 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1040 // width and height of the device-dependent bitmap
1041 int width
= image
.GetWidth();
1042 int height
= image
.GetHeight();
1044 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1048 bool hasAlpha
= false ;
1050 if ( image
.HasMask() )
1052 // takes precedence, don't mix with alpha info
1056 hasAlpha
= image
.HasAlpha() ;
1062 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1063 register unsigned char* data
= image
.GetData();
1064 if ( destinationstart
!= NULL
&& data
!= NULL
)
1066 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1067 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1069 unsigned char * destination
= destinationstart
;
1070 for (int x
= 0; x
< width
; x
++)
1074 const unsigned char a
= *alpha
++;
1075 *destination
++ = a
;
1077 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1078 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1079 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1080 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1082 *destination
++ = *data
++ ;
1083 *destination
++ = *data
++ ;
1084 *destination
++ = *data
++ ;
1089 *destination
++ = 0xFF ;
1090 *destination
++ = *data
++ ;
1091 *destination
++ = *data
++ ;
1092 *destination
++ = *data
++ ;
1099 if ( image
.HasMask() )
1100 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1103 wxImage
wxBitmap::ConvertToImage() const
1107 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1109 // create an wxImage object
1110 int width
= GetWidth();
1111 int height
= GetHeight();
1112 image
.Create( width
, height
);
1114 unsigned char *data
= image
.GetData();
1115 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1117 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1119 bool hasAlpha
= false ;
1120 bool hasMask
= false ;
1121 int maskBytesPerRow
= 0 ;
1122 unsigned char *alpha
= NULL
;
1123 unsigned char *mask
= NULL
;
1131 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1132 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1138 alpha
= image
.GetAlpha() ;
1143 // The following masking algorithm is the same as well in msw/gtk:
1144 // the colour used as transparent one in wxImage and the one it is
1145 // replaced with when it actually occurs in the bitmap
1146 static const int MASK_RED
= 1;
1147 static const int MASK_GREEN
= 2;
1148 static const int MASK_BLUE
= 3;
1149 static const int MASK_BLUE_REPLACEMENT
= 2;
1151 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1153 unsigned char * maskp
= mask
;
1154 unsigned char * source
= sourcestart
;
1155 unsigned char a
, r
, g
, b
;
1158 for (int xx
= 0; xx
< width
; xx
++)
1160 color
= *((long*) source
) ;
1161 #ifdef WORDS_BIGENDIAN
1162 a
= ((color
&0xFF000000) >> 24) ;
1163 r
= ((color
&0x00FF0000) >> 16) ;
1164 g
= ((color
&0x0000FF00) >> 8) ;
1165 b
= (color
&0x000000FF);
1167 b
= ((color
&0xFF000000) >> 24) ;
1168 g
= ((color
&0x00FF0000) >> 16) ;
1169 r
= ((color
&0x0000FF00) >> 8) ;
1170 a
= (color
&0x000000FF);
1174 if ( *maskp
++ == 0xFF )
1180 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1181 b
= MASK_BLUE_REPLACEMENT
;
1183 else if ( hasAlpha
)
1186 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1187 // this must be non-premultiplied data
1188 if ( a
!= 0xFF && a
!= 0 )
1198 data
[index
+ 1] = g
;
1199 data
[index
+ 2] = b
;
1207 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1212 #endif //wxUSE_IMAGE
1214 bool wxBitmap::SaveFile( const wxString
& filename
,
1215 wxBitmapType type
, const wxPalette
*palette
) const
1217 bool success
= false;
1218 wxBitmapHandler
*handler
= FindHandler(type
);
1222 success
= handler
->SaveFile(this, filename
, type
, palette
);
1227 wxImage image
= ConvertToImage();
1228 success
= image
.SaveFile(filename
, type
);
1230 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1237 bool wxBitmap::IsOk() const
1239 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1242 int wxBitmap::GetHeight() const
1244 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1246 return M_BITMAPDATA
->GetHeight();
1249 int wxBitmap::GetWidth() const
1251 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1253 return M_BITMAPDATA
->GetWidth() ;
1256 int wxBitmap::GetDepth() const
1258 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1260 return M_BITMAPDATA
->GetDepth();
1263 wxMask
*wxBitmap::GetMask() const
1265 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1267 return M_BITMAPDATA
->m_bitmapMask
;
1270 bool wxBitmap::HasAlpha() const
1272 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1274 return M_BITMAPDATA
->HasAlpha() ;
1277 void wxBitmap::SetWidth(int w
)
1280 M_BITMAPDATA
->SetWidth(w
);
1283 void wxBitmap::SetHeight(int h
)
1286 M_BITMAPDATA
->SetHeight(h
);
1289 void wxBitmap::SetDepth(int d
)
1292 M_BITMAPDATA
->SetDepth(d
);
1295 void wxBitmap::SetOk(bool isOk
)
1298 M_BITMAPDATA
->SetOk(isOk
);
1302 wxPalette
*wxBitmap::GetPalette() const
1304 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1306 return &M_BITMAPDATA
->m_bitmapPalette
;
1309 void wxBitmap::SetPalette(const wxPalette
& palette
)
1312 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1314 #endif // wxUSE_PALETTE
1316 void wxBitmap::SetMask(wxMask
*mask
)
1319 // Remove existing mask if there is one.
1320 delete M_BITMAPDATA
->m_bitmapMask
;
1322 M_BITMAPDATA
->m_bitmapMask
= mask
;
1325 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1329 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1332 // ----------------------------------------------------------------------------
1334 // ----------------------------------------------------------------------------
1341 wxMask::wxMask(const wxMask
&tocopy
)
1345 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1346 m_width
= tocopy
.m_width
;
1347 m_height
= tocopy
.m_height
;
1349 size_t size
= m_bytesPerRow
* m_height
;
1350 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1351 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1352 memcpy( dest
, source
, size
);
1353 m_memBuf
.UngetWriteBuf( size
) ;
1357 // Construct a mask from a bitmap and a colour indicating
1358 // the transparent area
1359 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1362 Create( bitmap
, colour
);
1365 // Construct a mask from a mono bitmap (copies the bitmap).
1366 wxMask::wxMask( const wxBitmap
& bitmap
)
1372 // Construct a mask from a mono bitmap (copies the bitmap).
1374 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1377 Create( data
, width
, height
, bytesPerRow
);
1384 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1385 m_maskBitmap
= NULL
;
1391 m_width
= m_height
= m_bytesPerRow
= 0 ;
1392 m_maskBitmap
= NULL
;
1395 void *wxMask::GetRawAccess() const
1397 return m_memBuf
.GetData() ;
1400 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1401 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1403 void wxMask::RealizeNative()
1407 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1408 m_maskBitmap
= NULL
;
1411 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1412 // from MouseTracking sample :
1413 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1414 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1416 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1417 kCGImageAlphaNone
);
1418 CGColorSpaceRelease( colorspace
);
1419 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1422 // Create a mask from a mono bitmap (copies the bitmap).
1424 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1429 m_bytesPerRow
= bytesPerRow
;
1431 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1438 // Create a mask from a mono bitmap (copies the bitmap).
1439 bool wxMask::Create(const wxBitmap
& bitmap
)
1441 m_width
= bitmap
.GetWidth() ;
1442 m_height
= bitmap
.GetHeight() ;
1443 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1445 size_t size
= m_bytesPerRow
* m_height
;
1446 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1447 wxASSERT( destdatabase
!= NULL
) ;
1449 memset( destdatabase
, 0 , size
) ;
1450 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1452 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1454 unsigned char *destdata
= destdatabase
;
1455 unsigned char r
, g
, b
;
1457 for ( int x
= 0 ; x
< m_width
; ++x
)
1464 if ( ( r
+ g
+ b
) > 0x10 )
1465 *destdata
++ = 0xFF ;
1467 *destdata
++ = 0x00 ;
1471 m_memBuf
.UngetWriteBuf( size
) ;
1477 // Create a mask from a bitmap and a colour indicating
1478 // the transparent area
1479 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1481 m_width
= bitmap
.GetWidth() ;
1482 m_height
= bitmap
.GetHeight() ;
1483 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1485 size_t size
= m_bytesPerRow
* m_height
;
1486 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1487 wxASSERT( destdatabase
!= NULL
) ;
1489 memset( destdatabase
, 0 , size
) ;
1490 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1491 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1493 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1495 unsigned char *srcdata
= srcdatabase
;
1496 unsigned char *destdata
= destdatabase
;
1497 unsigned char r
, g
, b
;
1499 for ( int x
= 0 ; x
< m_width
; ++x
)
1506 if ( colour
== wxColour( r
, g
, b
) )
1507 *destdata
++ = 0xFF ;
1509 *destdata
++ = 0x00 ;
1513 m_memBuf
.UngetWriteBuf( size
) ;
1519 WXHBITMAP
wxMask::GetHBITMAP() const
1521 return m_maskBitmap
;
1524 // ----------------------------------------------------------------------------
1526 // ----------------------------------------------------------------------------
1528 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1530 // ----------------------------------------------------------------------------
1531 // Standard Handlers
1532 // ----------------------------------------------------------------------------
1534 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1536 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1539 inline wxPICTResourceHandler()
1541 SetName(wxT("Macintosh Pict resource"));
1542 SetExtension(wxEmptyString
);
1543 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1546 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1547 int desiredWidth
, int desiredHeight
);
1550 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1553 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1554 const wxString
& name
,
1555 long WXUNUSED(flags
),
1556 int WXUNUSED(desiredWidth
),
1557 int WXUNUSED(desiredHeight
))
1561 wxMacStringToPascal( name
, theName
) ;
1563 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1568 mf
.SetPICT( thePict
) ;
1569 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1571 dc
.SelectObject( *bitmap
) ;
1573 dc
.SelectObject( wxNullBitmap
) ;
1582 void wxBitmap::InitStandardHandlers()
1584 AddHandler( new wxPICTResourceHandler
) ;
1585 AddHandler( new wxICONResourceHandler
) ;
1588 // ----------------------------------------------------------------------------
1589 // raw bitmap access support
1590 // ----------------------------------------------------------------------------
1592 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1595 // no bitmap, no data (raw or otherwise)
1598 data
.m_width
= GetWidth() ;
1599 data
.m_height
= GetHeight() ;
1600 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1602 return BeginRawAccess() ;
1605 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1610 void wxBitmap::UseAlpha()
1612 // remember that we are using alpha channel:
1613 // we'll need to create a proper mask in UngetRawData()
1614 M_BITMAPDATA
->UseAlpha( true );