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();
57 virtual 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
);
277 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
278 size_t size
= m_bytesPerRow
* h
;
279 void* data
= m_memBuf
.GetWriteBuf( size
) ;
282 memset( data
, 0 , size
) ;
283 m_memBuf
.UngetWriteBuf( size
) ;
285 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
286 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
287 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
288 CGContextScaleCTM( m_hBitmap
, 1, -1 );
290 m_ok
= ( m_hBitmap
!= NULL
) ;
295 void wxBitmapRefData::UseAlpha( bool use
)
297 if ( m_hasAlpha
== use
)
302 CGContextRelease( m_hBitmap
);
303 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
304 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
305 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
306 CGContextScaleCTM( m_hBitmap
, 1, -1 );
309 void *wxBitmapRefData::GetRawAccess() const
311 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
312 return m_memBuf
.GetData() ;
315 void *wxBitmapRefData::BeginRawAccess()
317 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
318 wxASSERT( m_rawAccessCount
== 0 ) ;
319 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
320 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
324 // we must destroy an existing cached image, as
325 // the bitmap data may change now
328 CGImageRelease( m_cgImageRef
) ;
329 m_cgImageRef
= NULL
;
332 return m_memBuf
.GetData() ;
335 void wxBitmapRefData::EndRawAccess()
337 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
338 wxASSERT( m_rawAccessCount
== 1 ) ;
343 bool wxBitmapRefData::HasNativeSize()
346 int h
= GetHeight() ;
347 int sz
= wxMax( w
, h
) ;
349 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
352 IconRef
wxBitmapRefData::GetIconRef()
354 if ( m_iconRef
== NULL
)
356 // Create Icon Family Handle
358 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
361 int h
= GetHeight() ;
362 int sz
= wxMax( w
, h
) ;
364 OSType dataType
= 0 ;
365 OSType maskType
= 0 ;
370 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
371 if ( UMAGetSystemVersion() >= 0x1050 )
373 dataType
= kIconServices128PixelDataARGB
;
378 dataType
= kThumbnail32BitData
;
379 maskType
= kThumbnail8BitMask
;
384 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
385 if ( UMAGetSystemVersion() >= 0x1050 )
387 dataType
= kIconServices48PixelDataARGB
;
392 dataType
= kHuge32BitData
;
393 maskType
= kHuge8BitMask
;
398 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
399 if ( UMAGetSystemVersion() >= 0x1050 )
401 dataType
= kIconServices32PixelDataARGB
;
406 dataType
= kLarge32BitData
;
407 maskType
= kLarge8BitMask
;
412 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
413 if ( UMAGetSystemVersion() >= 0x1050 )
415 dataType
= kIconServices16PixelDataARGB
;
420 dataType
= kSmall32BitData
;
421 maskType
= kSmall8BitMask
;
431 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
432 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
434 size_t datasize
= sz
* sz
* 4 ;
435 Handle data
= NewHandle( datasize
) ;
437 unsigned char* ptr
= (unsigned char*) *data
;
438 memset( ptr
, 0, datasize
);
439 bool hasAlpha
= HasAlpha() ;
440 wxMask
*mask
= m_bitmapMask
;
441 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
442 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
444 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
446 unsigned char * source
= sourcePtr
;
447 unsigned char * masksource
= masksourcePtr
;
448 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
449 unsigned char a
, r
, g
, b
;
451 for ( int x
= 0 ; x
< w
; ++x
)
460 a
= 0xFF - *masksource
++ ;
462 else if ( !hasAlpha
)
466 #if wxMAC_USE_PREMULTIPLIED_ALPHA
467 // this must be non-premultiplied data
468 if ( a
!= 0xFF && a
!= 0 )
484 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
485 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
486 DisposeHandle( data
);
491 // setup the header properly
494 Handle maskdata
= NULL
;
495 unsigned char * maskptr
= NULL
;
496 unsigned char * ptr
= NULL
;
497 size_t datasize
, masksize
;
499 datasize
= sz
* sz
* 4 ;
500 data
= NewHandle( datasize
) ;
502 ptr
= (unsigned char*) *data
;
503 memset( ptr
, 0, datasize
) ;
506 maskdata
= NewHandle( masksize
) ;
508 maskptr
= (unsigned char*) *maskdata
;
509 memset( maskptr
, 0 , masksize
) ;
511 bool hasAlpha
= HasAlpha() ;
512 wxMask
*mask
= m_bitmapMask
;
513 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
514 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
516 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
518 unsigned char * source
= sourcePtr
;
519 unsigned char * masksource
= masksourcePtr
;
520 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
521 unsigned char * maskdest
= maskptr
+ y
* sz
;
522 unsigned char a
, r
, g
, b
;
524 for ( int x
= 0 ; x
< w
; ++x
)
537 *maskdest
++ = 0xFF - *masksource
++ ;
545 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
546 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
548 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
549 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
552 HUnlock( maskdata
) ;
553 DisposeHandle( data
) ;
554 DisposeHandle( maskdata
) ;
559 PicHandle pic
= GetPictHandle() ;
560 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
562 // transform into IconRef
564 // cleaner version existing from 10.3 upwards
565 HLock((Handle
) iconFamily
);
566 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
567 HUnlock((Handle
) iconFamily
);
568 DisposeHandle( (Handle
) iconFamily
) ;
570 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
576 PicHandle
wxBitmapRefData::GetPictHandle()
578 if ( m_pictHandle
== NULL
)
581 GraphicsExportComponent exporter
= 0;
582 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
585 m_pictHandle
= (PicHandle
) NewHandle(0);
588 // QT does not correctly export the mask
589 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
590 CGImageRef imageRef
= CreateCGImage();
591 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
592 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
593 err
= GraphicsExportDoExport(exporter
, NULL
);
594 CGImageRelease( imageRef
);
596 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
597 // the 512 bytes header is only needed for pict files, but not in memory
598 if ( handleSize
>= 512 )
600 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
601 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
604 CloseComponent( exporter
);
609 return m_pictHandle
;
612 CGImageRef
wxBitmapRefData::CreateCGImage() const
615 wxASSERT( m_rawAccessCount
>= 0 ) ;
617 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
619 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
623 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
624 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
625 image
= CGImageCreateWithMask( tempImage
, tempMask
);
626 CGImageRelease(tempMask
);
627 CGImageRelease(tempImage
);
630 image
= CGBitmapContextCreateImage( m_hBitmap
);
634 size_t imageSize
= m_height
* m_bytesPerRow
;
635 void * dataBuffer
= m_memBuf
.GetData() ;
638 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
639 wxMemoryBuffer membuf
;
643 alphaInfo
= kCGImageAlphaFirst
;
644 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
645 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
646 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
647 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
648 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
650 unsigned char *sourcemask
= sourcemaskstart
;
651 unsigned char *destalpha
= destalphastart
;
652 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
654 *destalpha
= 0xFF - *sourcemask
;
657 membuf
.UngetWriteBuf( imageSize
);
663 #if wxMAC_USE_PREMULTIPLIED_ALPHA
664 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
666 alphaInfo
= kCGImageAlphaFirst
;
673 CGDataProviderRef dataProvider
= NULL
;
676 // TODO CHECK ALIGNMENT
677 wxMemoryBuffer maskBuf
;
678 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
679 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
680 // copy one color component
682 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
684 unsigned char *bufDataIter
= bufData
+3;
685 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
687 maskBufData
[i
] = *bufDataIter
;
690 maskBuf
.UngetWriteBuf( m_width
* m_height
);
693 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
695 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
699 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
700 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
703 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
704 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
706 CGDataProviderRelease( dataProvider
);
711 image
= m_cgImageRef
;
712 CGImageRetain( image
) ;
715 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
717 // we keep it for later use
718 m_cgImageRef
= image
;
719 CGImageRetain( image
) ;
725 CGContextRef
wxBitmapRefData::GetBitmapContext() const
730 void wxBitmapRefData::Free()
732 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
736 CGImageRelease( m_cgImageRef
) ;
737 m_cgImageRef
= NULL
;
742 ReleaseIconRef( m_iconRef
) ;
749 KillPicture( m_pictHandle
) ;
750 m_pictHandle
= NULL
;
756 CGContextRelease(m_hBitmap
);
767 wxBitmapRefData::~wxBitmapRefData()
772 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
774 bool created
= false ;
775 int w
= icon
.GetWidth() ;
776 int h
= icon
.GetHeight() ;
778 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
780 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
782 IconFamilyHandle iconFamily
= NULL
;
783 Handle imagehandle
= NewHandle( 0 ) ;
784 Handle maskhandle
= NewHandle( 0 ) ;
788 IconSelectorValue selector
= 0 ;
793 dataType
= kThumbnail32BitData
;
794 maskType
= kThumbnail8BitMask
;
795 selector
= kSelectorAllAvailableData
;
799 dataType
= kHuge32BitData
;
800 maskType
= kHuge8BitMask
;
801 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
805 dataType
= kLarge32BitData
;
806 maskType
= kLarge8BitMask
;
807 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
811 dataType
= kSmall32BitData
;
812 maskType
= kSmall8BitMask
;
813 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
820 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
822 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
823 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
824 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
825 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
827 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
829 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
830 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
834 unsigned char *source
= (unsigned char *) *imagehandle
;
835 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
836 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
838 for ( int y
= 0 ; y
< h
; ++y
)
840 for ( int x
= 0 ; x
< w
; ++x
)
842 unsigned char a
= *sourcemask
++;
845 #if wxMAC_USE_PREMULTIPLIED_ALPHA
846 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
847 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
848 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
850 *destination
++ = *source
++ ;
851 *destination
++ = *source
++ ;
852 *destination
++ = *source
++ ;
858 DisposeHandle( imagehandle
) ;
859 DisposeHandle( maskhandle
) ;
863 DisposeHandle( (Handle
) iconFamily
) ;
869 dc
.SelectObject( *this ) ;
870 dc
.DrawIcon( icon
, 0 , 0 ) ;
871 dc
.SelectObject( wxNullBitmap
) ;
881 wxBitmap::~wxBitmap()
885 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
887 wxBitmapRefData
* bitmapRefData
;
889 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
891 if (bitmapRefData
->IsOk())
895 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
896 if ( the_width
% (sizeof(unsigned char) * 8) )
897 linesize
+= sizeof(unsigned char);
899 unsigned char* linestart
= (unsigned char*) bits
;
900 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
902 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
904 unsigned char* destination
= destptr
;
905 int index
, bit
, mask
;
907 for ( int x
= 0 ; x
< the_width
; ++x
)
913 if ( linestart
[index
] & mask
)
915 *destination
++ = 0xFF ;
922 *destination
++ = 0xFF ;
923 *destination
++ = 0xFF ;
924 *destination
++ = 0xFF ;
925 *destination
++ = 0xFF ;
934 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
936 } /* bitmapRefData->IsOk() */
939 wxBitmap::wxBitmap(int w
, int h
, int d
)
941 (void)Create(w
, h
, d
);
944 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
946 (void) Create(data
, type
, width
, height
, depth
);
949 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
951 LoadFile(filename
, type
);
954 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
956 return new wxBitmapRefData
;
959 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
961 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
964 void * wxBitmap::GetRawAccess() const
966 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
968 return M_BITMAPDATA
->GetRawAccess() ;
971 void * wxBitmap::BeginRawAccess()
973 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
975 return M_BITMAPDATA
->BeginRawAccess() ;
978 void wxBitmap::EndRawAccess()
980 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
982 M_BITMAPDATA
->EndRawAccess() ;
985 CGImageRef
wxBitmap::CreateCGImage() const
987 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
989 return M_BITMAPDATA
->CreateCGImage() ;
992 IconRef
wxBitmap::GetIconRef() const
994 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
996 return M_BITMAPDATA
->GetIconRef() ;
999 IconRef
wxBitmap::CreateIconRef() const
1001 IconRef icon
= GetIconRef();
1002 verify_noerr( AcquireIconRef(icon
) );
1006 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1008 wxCHECK_MSG( Ok() &&
1009 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1010 (rect
.x
+rect
.width
<= GetWidth()) &&
1011 (rect
.y
+rect
.height
<= GetHeight()),
1012 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1014 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1015 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1017 int destwidth
= rect
.width
;
1018 int destheight
= rect
.height
;
1021 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1022 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1023 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1025 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1026 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1027 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1028 unsigned char *dest
= destdata
;
1030 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1032 memcpy( dest
, source
, destlinesize
) ;
1036 ret
.EndRawAccess() ;
1038 if ( M_BITMAPDATA
->m_bitmapMask
)
1040 wxMemoryBuffer maskbuf
;
1041 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1042 size_t maskbufsize
= rowBytes
* destheight
;
1044 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1045 int destlinesize
= rowBytes
;
1047 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1048 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1049 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1051 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1052 unsigned char *dest
= destdata
;
1054 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1056 memcpy( dest
, source
, destlinesize
) ;
1059 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1060 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1062 else if ( HasAlpha() )
1068 bool wxBitmap::Create(int w
, int h
, int d
)
1073 d
= wxDisplayDepth() ;
1075 m_refData
= new wxBitmapRefData( w
, h
, d
);
1077 return M_BITMAPDATA
->IsOk() ;
1080 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1084 wxBitmapHandler
*handler
= FindHandler(type
);
1088 m_refData
= new wxBitmapRefData
;
1090 return handler
->LoadFile(this, filename
, type
, -1, -1);
1095 wxImage
loadimage(filename
, type
);
1105 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1110 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1114 m_refData
= new wxBitmapRefData
;
1116 wxBitmapHandler
*handler
= FindHandler(type
);
1118 if ( handler
== NULL
)
1120 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1125 return handler
->Create(this, data
, type
, width
, height
, depth
);
1130 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1132 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1134 // width and height of the device-dependent bitmap
1135 int width
= image
.GetWidth();
1136 int height
= image
.GetHeight();
1138 wxBitmapRefData
* bitmapRefData
;
1140 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1142 if ( bitmapRefData
->IsOk())
1146 bool hasAlpha
= false ;
1148 if ( image
.HasMask() )
1150 // takes precedence, don't mix with alpha info
1154 hasAlpha
= image
.HasAlpha() ;
1160 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1161 register unsigned char* data
= image
.GetData();
1162 if ( destinationstart
!= NULL
&& data
!= NULL
)
1164 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1165 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1167 unsigned char * destination
= destinationstart
;
1168 for (int x
= 0; x
< width
; x
++)
1172 const unsigned char a
= *alpha
++;
1173 *destination
++ = a
;
1175 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1176 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1177 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1178 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1180 *destination
++ = *data
++ ;
1181 *destination
++ = *data
++ ;
1182 *destination
++ = *data
++ ;
1187 *destination
++ = 0xFF ;
1188 *destination
++ = *data
++ ;
1189 *destination
++ = *data
++ ;
1190 *destination
++ = *data
++ ;
1197 if ( image
.HasMask() )
1198 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1199 } /* bitmapRefData->IsOk() */
1202 wxImage
wxBitmap::ConvertToImage() const
1206 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1208 // create an wxImage object
1209 int width
= GetWidth();
1210 int height
= GetHeight();
1211 image
.Create( width
, height
);
1213 unsigned char *data
= image
.GetData();
1214 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1216 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1218 bool hasAlpha
= false ;
1219 bool hasMask
= false ;
1220 int maskBytesPerRow
= 0 ;
1221 unsigned char *alpha
= NULL
;
1222 unsigned char *mask
= NULL
;
1230 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1231 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1237 alpha
= image
.GetAlpha() ;
1242 // The following masking algorithm is the same as well in msw/gtk:
1243 // the colour used as transparent one in wxImage and the one it is
1244 // replaced with when it actually occurs in the bitmap
1245 static const int MASK_RED
= 1;
1246 static const int MASK_GREEN
= 2;
1247 static const int MASK_BLUE
= 3;
1248 static const int MASK_BLUE_REPLACEMENT
= 2;
1250 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1252 unsigned char * maskp
= mask
;
1253 unsigned char * source
= sourcestart
;
1254 unsigned char a
, r
, g
, b
;
1257 for (int xx
= 0; xx
< width
; xx
++)
1259 color
= *((long*) source
) ;
1260 #ifdef WORDS_BIGENDIAN
1261 a
= ((color
&0xFF000000) >> 24) ;
1262 r
= ((color
&0x00FF0000) >> 16) ;
1263 g
= ((color
&0x0000FF00) >> 8) ;
1264 b
= (color
&0x000000FF);
1266 b
= ((color
&0xFF000000) >> 24) ;
1267 g
= ((color
&0x00FF0000) >> 16) ;
1268 r
= ((color
&0x0000FF00) >> 8) ;
1269 a
= (color
&0x000000FF);
1273 if ( *maskp
++ == 0xFF )
1279 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1280 b
= MASK_BLUE_REPLACEMENT
;
1282 else if ( hasAlpha
)
1285 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1286 // this must be non-premultiplied data
1287 if ( a
!= 0xFF && a
!= 0 )
1297 data
[index
+ 1] = g
;
1298 data
[index
+ 2] = b
;
1306 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1311 #endif //wxUSE_IMAGE
1313 bool wxBitmap::SaveFile( const wxString
& filename
,
1314 wxBitmapType type
, const wxPalette
*palette
) const
1316 bool success
= false;
1317 wxBitmapHandler
*handler
= FindHandler(type
);
1321 success
= handler
->SaveFile(this, filename
, type
, palette
);
1326 wxImage image
= ConvertToImage();
1327 success
= image
.SaveFile(filename
, type
);
1329 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1336 int wxBitmap::GetHeight() const
1338 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1340 return M_BITMAPDATA
->GetHeight();
1343 int wxBitmap::GetWidth() const
1345 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1347 return M_BITMAPDATA
->GetWidth() ;
1350 int wxBitmap::GetDepth() const
1352 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1354 return M_BITMAPDATA
->GetDepth();
1357 wxMask
*wxBitmap::GetMask() const
1359 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1361 return M_BITMAPDATA
->m_bitmapMask
;
1364 bool wxBitmap::HasAlpha() const
1366 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1368 return M_BITMAPDATA
->HasAlpha() ;
1371 void wxBitmap::SetWidth(int w
)
1374 M_BITMAPDATA
->SetWidth(w
);
1377 void wxBitmap::SetHeight(int h
)
1380 M_BITMAPDATA
->SetHeight(h
);
1383 void wxBitmap::SetDepth(int d
)
1386 M_BITMAPDATA
->SetDepth(d
);
1389 void wxBitmap::SetOk(bool isOk
)
1392 M_BITMAPDATA
->SetOk(isOk
);
1396 wxPalette
*wxBitmap::GetPalette() const
1398 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1400 return &M_BITMAPDATA
->m_bitmapPalette
;
1403 void wxBitmap::SetPalette(const wxPalette
& palette
)
1406 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1408 #endif // wxUSE_PALETTE
1410 void wxBitmap::SetMask(wxMask
*mask
)
1413 // Remove existing mask if there is one.
1414 delete M_BITMAPDATA
->m_bitmapMask
;
1416 M_BITMAPDATA
->m_bitmapMask
= mask
;
1419 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1423 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1426 // ----------------------------------------------------------------------------
1428 // ----------------------------------------------------------------------------
1435 wxMask::wxMask(const wxMask
&tocopy
)
1439 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1440 m_width
= tocopy
.m_width
;
1441 m_height
= tocopy
.m_height
;
1443 size_t size
= m_bytesPerRow
* m_height
;
1444 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1445 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1446 memcpy( dest
, source
, size
);
1447 m_memBuf
.UngetWriteBuf( size
) ;
1451 // Construct a mask from a bitmap and a colour indicating
1452 // the transparent area
1453 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1456 Create( bitmap
, colour
);
1459 // Construct a mask from a mono bitmap (copies the bitmap).
1460 wxMask::wxMask( const wxBitmap
& bitmap
)
1466 // Construct a mask from a mono bitmap (copies the bitmap).
1468 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1471 Create( data
, width
, height
, bytesPerRow
);
1478 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1479 m_maskBitmap
= NULL
;
1485 m_width
= m_height
= m_bytesPerRow
= 0 ;
1486 m_maskBitmap
= NULL
;
1489 void *wxMask::GetRawAccess() const
1491 return m_memBuf
.GetData() ;
1494 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1495 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1497 void wxMask::RealizeNative()
1501 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1502 m_maskBitmap
= NULL
;
1505 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1506 // from MouseTracking sample :
1507 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1508 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1510 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1511 kCGImageAlphaNone
);
1512 CGColorSpaceRelease( colorspace
);
1513 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1516 // Create a mask from a mono bitmap (copies the bitmap).
1518 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1523 m_bytesPerRow
= bytesPerRow
;
1525 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1532 // Create a mask from a mono bitmap (copies the bitmap).
1533 bool wxMask::Create(const wxBitmap
& bitmap
)
1535 m_width
= bitmap
.GetWidth() ;
1536 m_height
= bitmap
.GetHeight() ;
1537 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1539 size_t size
= m_bytesPerRow
* m_height
;
1540 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1541 wxASSERT( destdatabase
!= NULL
) ;
1543 memset( destdatabase
, 0 , size
) ;
1544 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1546 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1548 unsigned char *destdata
= destdatabase
;
1549 unsigned char r
, g
, b
;
1551 for ( int x
= 0 ; x
< m_width
; ++x
)
1558 if ( ( r
+ g
+ b
) > 0x10 )
1559 *destdata
++ = 0xFF ;
1561 *destdata
++ = 0x00 ;
1565 m_memBuf
.UngetWriteBuf( size
) ;
1571 // Create a mask from a bitmap and a colour indicating
1572 // the transparent area
1573 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1575 m_width
= bitmap
.GetWidth() ;
1576 m_height
= bitmap
.GetHeight() ;
1577 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1579 size_t size
= m_bytesPerRow
* m_height
;
1580 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1581 wxASSERT( destdatabase
!= NULL
) ;
1583 memset( destdatabase
, 0 , size
) ;
1584 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1585 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1587 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1589 unsigned char *srcdata
= srcdatabase
;
1590 unsigned char *destdata
= destdatabase
;
1591 unsigned char r
, g
, b
;
1593 for ( int x
= 0 ; x
< m_width
; ++x
)
1600 if ( colour
== wxColour( r
, g
, b
) )
1601 *destdata
++ = 0xFF ;
1603 *destdata
++ = 0x00 ;
1607 m_memBuf
.UngetWriteBuf( size
) ;
1613 WXHBITMAP
wxMask::GetHBITMAP() const
1615 return m_maskBitmap
;
1618 // ----------------------------------------------------------------------------
1619 // Standard Handlers
1620 // ----------------------------------------------------------------------------
1624 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1626 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1629 inline wxPICTResourceHandler()
1631 SetName(wxT("Macintosh Pict resource"));
1632 SetExtension(wxEmptyString
);
1633 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1636 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1637 int desiredWidth
, int desiredHeight
);
1640 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1643 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1644 const wxString
& name
,
1645 long WXUNUSED(flags
),
1646 int WXUNUSED(desiredWidth
),
1647 int WXUNUSED(desiredHeight
))
1651 wxMacStringToPascal( name
, theName
) ;
1653 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1658 mf
.SetPICT( thePict
) ;
1659 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1661 dc
.SelectObject( *bitmap
) ;
1663 dc
.SelectObject( wxNullBitmap
) ;
1673 void wxBitmap::InitStandardHandlers()
1676 AddHandler( new wxPICTResourceHandler
) ;
1678 AddHandler( new wxICONResourceHandler
) ;
1681 // ----------------------------------------------------------------------------
1682 // raw bitmap access support
1683 // ----------------------------------------------------------------------------
1685 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1688 // no bitmap, no data (raw or otherwise)
1691 data
.m_width
= GetWidth() ;
1692 data
.m_height
= GetHeight() ;
1693 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1695 return BeginRawAccess() ;
1698 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1703 void wxBitmap::UseAlpha()
1705 // remember that we are using alpha channel:
1706 // we'll need to create a proper mask in UngetRawData()
1707 M_BITMAPDATA
->UseAlpha( true );