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 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
48 friend class WXDLLIMPEXP_FWD_CORE wxIcon
;
49 friend class WXDLLIMPEXP_FWD_CORE wxCursor
;
51 wxBitmapRefData(int width
, int height
, int depth
);
53 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
55 virtual ~wxBitmapRefData();
58 bool Ok() const { return IsOk(); }
59 bool IsOk() const { return m_ok
; }
60 void SetOk( bool isOk
) { m_ok
= isOk
; }
62 void SetWidth( int width
) { m_width
= width
; }
63 void SetHeight( int height
) { m_height
= height
; }
64 void SetDepth( int depth
) { m_depth
= depth
; }
66 int GetWidth() const { return m_width
; }
67 int GetHeight() const { return m_height
; }
68 int GetDepth() const { return m_depth
; }
70 void *GetRawAccess() const;
71 void *BeginRawAccess();
74 bool HasAlpha() const { return m_hasAlpha
; }
75 void UseAlpha( bool useAlpha
);
79 wxPalette m_bitmapPalette
;
80 #endif // wxUSE_PALETTE
82 wxMask
* m_bitmapMask
; // Optional mask
83 CGImageRef
CreateCGImage() const;
85 // returns true if the bitmap has a size that
86 // can be natively transferred into a true icon
87 // if no is returned GetIconRef will still produce
88 // an icon but it will be generated via a PICT and
89 // rescaled to 16 x 16
92 // caller should increase ref count if needed longer
93 // than the bitmap exists
96 // returns a Pict from the bitmap content
97 PicHandle
GetPictHandle();
99 CGContextRef
GetBitmapContext() const;
101 int GetBytesPerRow() const { return m_bytesPerRow
; }
103 bool Create(int width
, int height
, int depth
);
111 wxMemoryBuffer m_memBuf
;
112 int m_rawAccessCount
;
114 mutable CGImageRef m_cgImageRef
;
117 PicHandle m_pictHandle
;
119 CGContextRef m_hBitmap
;
123 #define wxMAC_USE_PREMULTIPLIED_ALPHA 1
124 static const int kBestByteAlignement
= 16;
125 static const int kMaskBytesPerPixel
= 1;
127 static int GetBestBytesPerRow( int rawBytes
)
129 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
134 // this is used for more controls than just the wxBitmap button, also for notebooks etc
136 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
138 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
141 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
145 if ( forceType
== 0 )
147 forceType
= kControlContentCGImageRef
;
150 if ( forceType
== kControlContentIconRef
)
153 wxBitmapRefData
* bmp
= bmap
;
155 if ( !bmap
->HasNativeSize() )
157 // as PICT conversion will only result in a 16x16 icon, let's attempt
158 // a few scales for better results
160 int w
= bitmap
.GetWidth() ;
161 int h
= bitmap
.GetHeight() ;
162 int sz
= wxMax( w
, h
) ;
163 if ( sz
== 24 || sz
== 64 )
165 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
166 bmp
= scaleBmp
.GetBitmapData() ;
170 info
->contentType
= kControlContentIconRef
;
171 info
->u
.iconRef
= bmp
->GetIconRef() ;
172 AcquireIconRef( info
->u
.iconRef
) ;
174 else if ( forceType
== kControlContentCGImageRef
)
176 info
->contentType
= kControlContentCGImageRef
;
177 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
182 info
->contentType
= kControlContentPictHandle
;
183 info
->u
.picture
= bmap
->GetPictHandle() ;
189 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
191 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
194 return (CGImageRef
) bmap
->CreateCGImage();
197 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
199 if ( info
->contentType
== kControlContentIconRef
)
201 ReleaseIconRef( info
->u
.iconRef
) ;
203 else if ( info
->contentType
== kControlNoContent
)
205 // there's no bitmap at all, fall through silently
207 else if ( info
->contentType
== kControlContentPictHandle
)
209 // owned by the bitmap, no release here
211 else if ( info
->contentType
== kControlContentCGImageRef
)
213 CGImageRelease( info
->u
.imageRef
) ;
217 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
221 #endif //wxUSE_BMPBUTTON
223 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
225 void wxBitmapRefData::Init()
232 m_bitmapMask
= NULL
;
233 m_cgImageRef
= NULL
;
236 m_pictHandle
= NULL
;
239 m_rawAccessCount
= 0 ;
243 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
246 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
248 if (tocopy
.m_bitmapMask
)
249 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
250 else if (tocopy
.m_hasAlpha
)
253 unsigned char* dest
= (unsigned char*)GetRawAccess();
254 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
255 size_t numbytes
= m_bytesPerRow
* m_height
;
256 memcpy( dest
, source
, numbytes
);
259 wxBitmapRefData::wxBitmapRefData()
264 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
267 Create( w
, h
, d
) ;
270 bool wxBitmapRefData::Create( int w
, int h
, int d
)
272 m_width
= wxMax(1, w
);
273 m_height
= wxMax(1, h
);
276 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
277 size_t size
= m_bytesPerRow
* h
;
278 void* data
= m_memBuf
.GetWriteBuf( size
) ;
279 memset( data
, 0 , size
) ;
280 m_memBuf
.UngetWriteBuf( size
) ;
283 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
284 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
285 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
286 CGContextScaleCTM( m_hBitmap
, 1, -1 );
288 m_ok
= ( m_hBitmap
!= NULL
) ;
293 void wxBitmapRefData::UseAlpha( bool use
)
295 if ( m_hasAlpha
== use
)
300 CGContextRelease( m_hBitmap
);
301 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
302 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
303 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
304 CGContextScaleCTM( m_hBitmap
, 1, -1 );
307 void *wxBitmapRefData::GetRawAccess() const
309 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
310 return m_memBuf
.GetData() ;
313 void *wxBitmapRefData::BeginRawAccess()
315 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
316 wxASSERT( m_rawAccessCount
== 0 ) ;
317 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
318 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
322 // we must destroy an existing cached image, as
323 // the bitmap data may change now
326 CGImageRelease( m_cgImageRef
) ;
327 m_cgImageRef
= NULL
;
330 return m_memBuf
.GetData() ;
333 void wxBitmapRefData::EndRawAccess()
335 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
336 wxASSERT( m_rawAccessCount
== 1 ) ;
341 bool wxBitmapRefData::HasNativeSize()
344 int h
= GetHeight() ;
345 int sz
= wxMax( w
, h
) ;
347 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
350 IconRef
wxBitmapRefData::GetIconRef()
352 if ( m_iconRef
== NULL
)
354 // Create Icon Family Handle
356 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
359 int h
= GetHeight() ;
360 int sz
= wxMax( w
, h
) ;
362 OSType dataType
= 0 ;
363 OSType maskType
= 0 ;
368 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
369 if ( UMAGetSystemVersion() >= 0x1050 )
371 dataType
= kIconServices128PixelDataARGB
;
376 dataType
= kThumbnail32BitData
;
377 maskType
= kThumbnail8BitMask
;
382 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
383 if ( UMAGetSystemVersion() >= 0x1050 )
385 dataType
= kIconServices48PixelDataARGB
;
390 dataType
= kHuge32BitData
;
391 maskType
= kHuge8BitMask
;
396 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
397 if ( UMAGetSystemVersion() >= 0x1050 )
399 dataType
= kIconServices32PixelDataARGB
;
404 dataType
= kLarge32BitData
;
405 maskType
= kLarge8BitMask
;
410 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
411 if ( UMAGetSystemVersion() >= 0x1050 )
413 dataType
= kIconServices16PixelDataARGB
;
418 dataType
= kSmall32BitData
;
419 maskType
= kSmall8BitMask
;
429 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
430 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
432 size_t datasize
= sz
* sz
* 4 ;
433 Handle data
= NewHandle( datasize
) ;
435 unsigned char* ptr
= (unsigned char*) *data
;
436 memset( ptr
, 0, datasize
);
437 bool hasAlpha
= HasAlpha() ;
438 wxMask
*mask
= m_bitmapMask
;
439 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
440 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
442 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
444 unsigned char * source
= sourcePtr
;
445 unsigned char * masksource
= masksourcePtr
;
446 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
447 unsigned char a
, r
, g
, b
;
449 for ( int x
= 0 ; x
< w
; ++x
)
458 a
= 0xFF - *masksource
++ ;
460 else if ( !hasAlpha
)
464 #if wxMAC_USE_PREMULTIPLIED_ALPHA
465 // this must be non-premultiplied data
466 if ( a
!= 0xFF && a
!= 0 )
482 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
483 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
484 DisposeHandle( data
);
489 // setup the header properly
492 Handle maskdata
= NULL
;
493 unsigned char * maskptr
= NULL
;
494 unsigned char * ptr
= NULL
;
495 size_t datasize
, masksize
;
497 datasize
= sz
* sz
* 4 ;
498 data
= NewHandle( datasize
) ;
500 ptr
= (unsigned char*) *data
;
501 memset( ptr
, 0, datasize
) ;
504 maskdata
= NewHandle( masksize
) ;
506 maskptr
= (unsigned char*) *maskdata
;
507 memset( maskptr
, 0 , masksize
) ;
509 bool hasAlpha
= HasAlpha() ;
510 wxMask
*mask
= m_bitmapMask
;
511 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
512 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
514 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
516 unsigned char * source
= sourcePtr
;
517 unsigned char * masksource
= masksourcePtr
;
518 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
519 unsigned char * maskdest
= maskptr
+ y
* sz
;
520 unsigned char a
, r
, g
, b
;
522 for ( int x
= 0 ; x
< w
; ++x
)
535 *maskdest
++ = 0xFF - *masksource
++ ;
543 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
544 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
546 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
547 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
550 HUnlock( maskdata
) ;
551 DisposeHandle( data
) ;
552 DisposeHandle( maskdata
) ;
557 PicHandle pic
= GetPictHandle() ;
558 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
560 // transform into IconRef
562 // cleaner version existing from 10.3 upwards
563 HLock((Handle
) iconFamily
);
564 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
565 HUnlock((Handle
) iconFamily
);
566 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
567 DisposeHandle( (Handle
) iconFamily
) ;
573 PicHandle
wxBitmapRefData::GetPictHandle()
575 if ( m_pictHandle
== NULL
)
578 GraphicsExportComponent exporter
= 0;
579 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
582 m_pictHandle
= (PicHandle
) NewHandle(0);
585 // QT does not correctly export the mask
586 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
587 CGImageRef imageRef
= CreateCGImage();
588 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
589 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
590 err
= GraphicsExportDoExport(exporter
, NULL
);
591 CGImageRelease( imageRef
);
593 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
594 // the 512 bytes header is only needed for pict files, but not in memory
595 if ( handleSize
>= 512 )
597 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
598 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
601 CloseComponent( exporter
);
606 return m_pictHandle
;
609 CGImageRef
wxBitmapRefData::CreateCGImage() const
612 wxASSERT( m_rawAccessCount
>= 0 ) ;
614 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
616 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
620 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
621 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
622 image
= CGImageCreateWithMask( tempImage
, tempMask
);
623 CGImageRelease(tempMask
);
624 CGImageRelease(tempImage
);
627 image
= CGBitmapContextCreateImage( m_hBitmap
);
631 size_t imageSize
= m_height
* m_bytesPerRow
;
632 void * dataBuffer
= m_memBuf
.GetData() ;
635 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
636 wxMemoryBuffer membuf
;
640 alphaInfo
= kCGImageAlphaFirst
;
641 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
642 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
643 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
644 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
645 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
647 unsigned char *sourcemask
= sourcemaskstart
;
648 unsigned char *destalpha
= destalphastart
;
649 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
651 *destalpha
= 0xFF - *sourcemask
;
654 membuf
.UngetWriteBuf( imageSize
);
660 #if wxMAC_USE_PREMULTIPLIED_ALPHA
661 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
663 alphaInfo
= kCGImageAlphaFirst
;
670 CGDataProviderRef dataProvider
= NULL
;
673 // TODO CHECK ALIGNMENT
674 wxMemoryBuffer maskBuf
;
675 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
676 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
677 // copy one color component
679 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
681 unsigned char *bufDataIter
= bufData
+3;
682 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
684 maskBufData
[i
] = *bufDataIter
;
687 maskBuf
.UngetWriteBuf( m_width
* m_height
);
690 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
692 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
696 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
697 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
700 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
701 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
703 CGDataProviderRelease( dataProvider
);
708 image
= m_cgImageRef
;
709 CGImageRetain( image
) ;
712 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
714 // we keep it for later use
715 m_cgImageRef
= image
;
716 CGImageRetain( image
) ;
722 CGContextRef
wxBitmapRefData::GetBitmapContext() const
727 void wxBitmapRefData::Free()
729 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
733 CGImageRelease( m_cgImageRef
) ;
734 m_cgImageRef
= NULL
;
739 ReleaseIconRef( m_iconRef
) ;
746 KillPicture( m_pictHandle
) ;
747 m_pictHandle
= NULL
;
753 CGContextRelease(m_hBitmap
);
764 wxBitmapRefData::~wxBitmapRefData()
769 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
771 bool created
= false ;
772 int w
= icon
.GetWidth() ;
773 int h
= icon
.GetHeight() ;
775 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
777 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
779 IconFamilyHandle iconFamily
= NULL
;
780 Handle imagehandle
= NewHandle( 0 ) ;
781 Handle maskhandle
= NewHandle( 0 ) ;
785 IconSelectorValue selector
= 0 ;
790 dataType
= kThumbnail32BitData
;
791 maskType
= kThumbnail8BitMask
;
792 selector
= kSelectorAllAvailableData
;
796 dataType
= kHuge32BitData
;
797 maskType
= kHuge8BitMask
;
798 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
802 dataType
= kLarge32BitData
;
803 maskType
= kLarge8BitMask
;
804 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
808 dataType
= kSmall32BitData
;
809 maskType
= kSmall8BitMask
;
810 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
817 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
819 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
820 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
821 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
822 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
824 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
826 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
827 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
831 unsigned char *source
= (unsigned char *) *imagehandle
;
832 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
833 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
835 for ( int y
= 0 ; y
< h
; ++y
)
837 for ( int x
= 0 ; x
< w
; ++x
)
839 unsigned char a
= *sourcemask
++;
842 #if wxMAC_USE_PREMULTIPLIED_ALPHA
843 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
844 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
845 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
847 *destination
++ = *source
++ ;
848 *destination
++ = *source
++ ;
849 *destination
++ = *source
++ ;
855 DisposeHandle( imagehandle
) ;
856 DisposeHandle( maskhandle
) ;
860 DisposeHandle( (Handle
) iconFamily
) ;
866 dc
.SelectObject( *this ) ;
867 dc
.DrawIcon( icon
, 0 , 0 ) ;
868 dc
.SelectObject( wxNullBitmap
) ;
878 wxBitmap::~wxBitmap()
882 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
884 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
888 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
889 if ( the_width
% (sizeof(unsigned char) * 8) )
890 linesize
+= sizeof(unsigned char);
892 unsigned char* linestart
= (unsigned char*) bits
;
893 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
895 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
897 unsigned char* destination
= destptr
;
898 int index
, bit
, mask
;
900 for ( int x
= 0 ; x
< the_width
; ++x
)
906 if ( linestart
[index
] & mask
)
908 *destination
++ = 0xFF ;
915 *destination
++ = 0xFF ;
916 *destination
++ = 0xFF ;
917 *destination
++ = 0xFF ;
918 *destination
++ = 0xFF ;
927 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
931 wxBitmap::wxBitmap(int w
, int h
, int d
)
933 (void)Create(w
, h
, d
);
936 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
938 (void) Create(data
, type
, width
, height
, depth
);
941 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
943 LoadFile(filename
, type
);
946 wxObjectRefData
* wxBitmap::CreateRefData() const
948 return new wxBitmapRefData
;
951 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
953 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
956 void * wxBitmap::GetRawAccess() const
958 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
960 return M_BITMAPDATA
->GetRawAccess() ;
963 void * wxBitmap::BeginRawAccess()
965 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
967 return M_BITMAPDATA
->BeginRawAccess() ;
970 void wxBitmap::EndRawAccess()
972 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
974 M_BITMAPDATA
->EndRawAccess() ;
977 CGImageRef
wxBitmap::CreateCGImage() const
979 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
981 return M_BITMAPDATA
->CreateCGImage() ;
984 IconRef
wxBitmap::GetIconRef() const
986 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
988 return M_BITMAPDATA
->GetIconRef() ;
991 IconRef
wxBitmap::CreateIconRef() const
993 IconRef icon
= GetIconRef();
994 verify_noerr( AcquireIconRef(icon
) );
998 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1000 wxCHECK_MSG( Ok() &&
1001 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1002 (rect
.x
+rect
.width
<= GetWidth()) &&
1003 (rect
.y
+rect
.height
<= GetHeight()),
1004 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1006 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1007 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1009 int destwidth
= rect
.width
;
1010 int destheight
= rect
.height
;
1013 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1014 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1015 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1017 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1018 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1019 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1020 unsigned char *dest
= destdata
;
1022 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1024 memcpy( dest
, source
, destlinesize
) ;
1028 ret
.EndRawAccess() ;
1030 if ( M_BITMAPDATA
->m_bitmapMask
)
1032 wxMemoryBuffer maskbuf
;
1033 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1034 size_t maskbufsize
= rowBytes
* destheight
;
1036 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1037 int destlinesize
= rowBytes
;
1039 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1040 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1041 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1043 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1044 unsigned char *dest
= destdata
;
1046 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1048 memcpy( dest
, source
, destlinesize
) ;
1051 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1052 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1054 else if ( HasAlpha() )
1060 bool wxBitmap::Create(int w
, int h
, int d
)
1065 d
= wxDisplayDepth() ;
1067 m_refData
= new wxBitmapRefData( w
, h
, d
);
1069 return M_BITMAPDATA
->Ok() ;
1072 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1076 wxBitmapHandler
*handler
= FindHandler(type
);
1080 m_refData
= new wxBitmapRefData
;
1082 return handler
->LoadFile(this, filename
, type
, -1, -1);
1087 wxImage
loadimage(filename
, type
);
1097 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1102 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1106 m_refData
= new wxBitmapRefData
;
1108 wxBitmapHandler
*handler
= FindHandler(type
);
1110 if ( handler
== NULL
)
1112 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1117 return handler
->Create(this, data
, type
, width
, height
, depth
);
1122 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1124 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1126 // width and height of the device-dependent bitmap
1127 int width
= image
.GetWidth();
1128 int height
= image
.GetHeight();
1130 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1134 bool hasAlpha
= false ;
1136 if ( image
.HasMask() )
1138 // takes precedence, don't mix with alpha info
1142 hasAlpha
= image
.HasAlpha() ;
1148 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1149 register unsigned char* data
= image
.GetData();
1150 if ( destinationstart
!= NULL
&& data
!= NULL
)
1152 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1153 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1155 unsigned char * destination
= destinationstart
;
1156 for (int x
= 0; x
< width
; x
++)
1160 const unsigned char a
= *alpha
++;
1161 *destination
++ = a
;
1163 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1164 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1165 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1166 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1168 *destination
++ = *data
++ ;
1169 *destination
++ = *data
++ ;
1170 *destination
++ = *data
++ ;
1175 *destination
++ = 0xFF ;
1176 *destination
++ = *data
++ ;
1177 *destination
++ = *data
++ ;
1178 *destination
++ = *data
++ ;
1185 if ( image
.HasMask() )
1186 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1189 wxImage
wxBitmap::ConvertToImage() const
1193 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1195 // create an wxImage object
1196 int width
= GetWidth();
1197 int height
= GetHeight();
1198 image
.Create( width
, height
);
1200 unsigned char *data
= image
.GetData();
1201 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1203 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1205 bool hasAlpha
= false ;
1206 bool hasMask
= false ;
1207 int maskBytesPerRow
= 0 ;
1208 unsigned char *alpha
= NULL
;
1209 unsigned char *mask
= NULL
;
1217 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1218 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1224 alpha
= image
.GetAlpha() ;
1229 // The following masking algorithm is the same as well in msw/gtk:
1230 // the colour used as transparent one in wxImage and the one it is
1231 // replaced with when it actually occurs in the bitmap
1232 static const int MASK_RED
= 1;
1233 static const int MASK_GREEN
= 2;
1234 static const int MASK_BLUE
= 3;
1235 static const int MASK_BLUE_REPLACEMENT
= 2;
1237 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1239 unsigned char * maskp
= mask
;
1240 unsigned char * source
= sourcestart
;
1241 unsigned char a
, r
, g
, b
;
1244 for (int xx
= 0; xx
< width
; xx
++)
1246 color
= *((long*) source
) ;
1247 #ifdef WORDS_BIGENDIAN
1248 a
= ((color
&0xFF000000) >> 24) ;
1249 r
= ((color
&0x00FF0000) >> 16) ;
1250 g
= ((color
&0x0000FF00) >> 8) ;
1251 b
= (color
&0x000000FF);
1253 b
= ((color
&0xFF000000) >> 24) ;
1254 g
= ((color
&0x00FF0000) >> 16) ;
1255 r
= ((color
&0x0000FF00) >> 8) ;
1256 a
= (color
&0x000000FF);
1260 if ( *maskp
++ == 0xFF )
1266 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1267 b
= MASK_BLUE_REPLACEMENT
;
1269 else if ( hasAlpha
)
1272 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1273 // this must be non-premultiplied data
1274 if ( a
!= 0xFF && a
!= 0 )
1284 data
[index
+ 1] = g
;
1285 data
[index
+ 2] = b
;
1293 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1298 #endif //wxUSE_IMAGE
1300 bool wxBitmap::SaveFile( const wxString
& filename
,
1301 wxBitmapType type
, const wxPalette
*palette
) const
1303 bool success
= false;
1304 wxBitmapHandler
*handler
= FindHandler(type
);
1308 success
= handler
->SaveFile(this, filename
, type
, palette
);
1313 wxImage image
= ConvertToImage();
1314 success
= image
.SaveFile(filename
, type
);
1316 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1323 bool wxBitmap::IsOk() const
1325 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1328 int wxBitmap::GetHeight() const
1330 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1332 return M_BITMAPDATA
->GetHeight();
1335 int wxBitmap::GetWidth() const
1337 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1339 return M_BITMAPDATA
->GetWidth() ;
1342 int wxBitmap::GetDepth() const
1344 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1346 return M_BITMAPDATA
->GetDepth();
1349 wxMask
*wxBitmap::GetMask() const
1351 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1353 return M_BITMAPDATA
->m_bitmapMask
;
1356 bool wxBitmap::HasAlpha() const
1358 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1360 return M_BITMAPDATA
->HasAlpha() ;
1363 void wxBitmap::SetWidth(int w
)
1366 M_BITMAPDATA
->SetWidth(w
);
1369 void wxBitmap::SetHeight(int h
)
1372 M_BITMAPDATA
->SetHeight(h
);
1375 void wxBitmap::SetDepth(int d
)
1378 M_BITMAPDATA
->SetDepth(d
);
1381 void wxBitmap::SetOk(bool isOk
)
1384 M_BITMAPDATA
->SetOk(isOk
);
1388 wxPalette
*wxBitmap::GetPalette() const
1390 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1392 return &M_BITMAPDATA
->m_bitmapPalette
;
1395 void wxBitmap::SetPalette(const wxPalette
& palette
)
1398 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1400 #endif // wxUSE_PALETTE
1402 void wxBitmap::SetMask(wxMask
*mask
)
1405 // Remove existing mask if there is one.
1406 delete M_BITMAPDATA
->m_bitmapMask
;
1408 M_BITMAPDATA
->m_bitmapMask
= mask
;
1411 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1415 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1418 // ----------------------------------------------------------------------------
1420 // ----------------------------------------------------------------------------
1427 wxMask::wxMask(const wxMask
&tocopy
)
1431 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1432 m_width
= tocopy
.m_width
;
1433 m_height
= tocopy
.m_height
;
1435 size_t size
= m_bytesPerRow
* m_height
;
1436 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1437 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1438 memcpy( dest
, source
, size
);
1439 m_memBuf
.UngetWriteBuf( size
) ;
1443 // Construct a mask from a bitmap and a colour indicating
1444 // the transparent area
1445 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1448 Create( bitmap
, colour
);
1451 // Construct a mask from a mono bitmap (copies the bitmap).
1452 wxMask::wxMask( const wxBitmap
& bitmap
)
1458 // Construct a mask from a mono bitmap (copies the bitmap).
1460 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1463 Create( data
, width
, height
, bytesPerRow
);
1470 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1471 m_maskBitmap
= NULL
;
1477 m_width
= m_height
= m_bytesPerRow
= 0 ;
1478 m_maskBitmap
= NULL
;
1481 void *wxMask::GetRawAccess() const
1483 return m_memBuf
.GetData() ;
1486 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1487 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1489 void wxMask::RealizeNative()
1493 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1494 m_maskBitmap
= NULL
;
1497 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1498 // from MouseTracking sample :
1499 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1500 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1502 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1503 kCGImageAlphaNone
);
1504 CGColorSpaceRelease( colorspace
);
1505 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1508 // Create a mask from a mono bitmap (copies the bitmap).
1510 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1515 m_bytesPerRow
= bytesPerRow
;
1517 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1524 // Create a mask from a mono bitmap (copies the bitmap).
1525 bool wxMask::Create(const wxBitmap
& bitmap
)
1527 m_width
= bitmap
.GetWidth() ;
1528 m_height
= bitmap
.GetHeight() ;
1529 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1531 size_t size
= m_bytesPerRow
* m_height
;
1532 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1533 wxASSERT( destdatabase
!= NULL
) ;
1535 memset( destdatabase
, 0 , size
) ;
1536 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1538 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1540 unsigned char *destdata
= destdatabase
;
1541 unsigned char r
, g
, b
;
1543 for ( int x
= 0 ; x
< m_width
; ++x
)
1550 if ( ( r
+ g
+ b
) > 0x10 )
1551 *destdata
++ = 0xFF ;
1553 *destdata
++ = 0x00 ;
1557 m_memBuf
.UngetWriteBuf( size
) ;
1563 // Create a mask from a bitmap and a colour indicating
1564 // the transparent area
1565 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1567 m_width
= bitmap
.GetWidth() ;
1568 m_height
= bitmap
.GetHeight() ;
1569 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1571 size_t size
= m_bytesPerRow
* m_height
;
1572 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1573 wxASSERT( destdatabase
!= NULL
) ;
1575 memset( destdatabase
, 0 , size
) ;
1576 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1577 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1579 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1581 unsigned char *srcdata
= srcdatabase
;
1582 unsigned char *destdata
= destdatabase
;
1583 unsigned char r
, g
, b
;
1585 for ( int x
= 0 ; x
< m_width
; ++x
)
1592 if ( colour
== wxColour( r
, g
, b
) )
1593 *destdata
++ = 0xFF ;
1595 *destdata
++ = 0x00 ;
1599 m_memBuf
.UngetWriteBuf( size
) ;
1605 WXHBITMAP
wxMask::GetHBITMAP() const
1607 return m_maskBitmap
;
1610 // ----------------------------------------------------------------------------
1612 // ----------------------------------------------------------------------------
1614 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1616 // ----------------------------------------------------------------------------
1617 // Standard Handlers
1618 // ----------------------------------------------------------------------------
1622 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1624 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1627 inline wxPICTResourceHandler()
1629 SetName(wxT("Macintosh Pict resource"));
1630 SetExtension(wxEmptyString
);
1631 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1634 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1635 int desiredWidth
, int desiredHeight
);
1638 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1641 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1642 const wxString
& name
,
1643 long WXUNUSED(flags
),
1644 int WXUNUSED(desiredWidth
),
1645 int WXUNUSED(desiredHeight
))
1649 wxMacStringToPascal( name
, theName
) ;
1651 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1656 mf
.SetPICT( thePict
) ;
1657 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1659 dc
.SelectObject( *bitmap
) ;
1661 dc
.SelectObject( wxNullBitmap
) ;
1671 void wxBitmap::InitStandardHandlers()
1674 AddHandler( new wxPICTResourceHandler
) ;
1676 AddHandler( new wxICONResourceHandler
) ;
1679 // ----------------------------------------------------------------------------
1680 // raw bitmap access support
1681 // ----------------------------------------------------------------------------
1683 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1686 // no bitmap, no data (raw or otherwise)
1689 data
.m_width
= GetWidth() ;
1690 data
.m_height
= GetHeight() ;
1691 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1693 return BeginRawAccess() ;
1696 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1701 void wxBitmap::UseAlpha()
1703 // remember that we are using alpha channel:
1704 // we'll need to create a proper mask in UngetRawData()
1705 M_BITMAPDATA
->UseAlpha( true );