1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/bitmap.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
18 #include "wx/dcmemory.h"
23 #include "wx/metafile.h"
24 #include "wx/xpmdecod.h"
26 #include "wx/rawbmp.h"
28 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
32 #include "wx/osx/uma.h"
34 #include "wx/osx/private.h"
37 #ifndef __WXOSX_IPHONE__
38 #include <QuickTime/QuickTime.h>
41 CGColorSpaceRef
wxMacGetGenericRGBColorSpace();
42 CGDataProviderRef
wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer
& buf
);
44 // Implementation Notes
45 // --------------------
47 // we are always working with a 32 bit deep pixel buffer
48 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
49 // therefore we have a separate GWorld there for blitting the mask in
51 // under Quartz then content is transformed into a CGImageRef representing the same data
52 // which can be transferred to the GPU by the OS for fast rendering
54 class WXDLLEXPORT wxBitmapRefData
: public wxGDIRefData
56 friend class WXDLLIMPEXP_FWD_CORE wxIcon
;
57 friend class WXDLLIMPEXP_FWD_CORE wxCursor
;
59 wxBitmapRefData(int width
, int height
, int depth
);
60 wxBitmapRefData(CGImageRef image
);
62 wxBitmapRefData(const wxBitmapRefData
&tocopy
);
64 virtual ~wxBitmapRefData();
66 virtual bool IsOk() const { return m_ok
; }
69 void SetOk( bool isOk
) { m_ok
= isOk
; }
71 void SetWidth( int width
) { m_width
= width
; }
72 void SetHeight( int height
) { m_height
= height
; }
73 void SetDepth( int depth
) { m_depth
= depth
; }
75 int GetWidth() const { return m_width
; }
76 int GetHeight() const { return m_height
; }
77 int GetDepth() const { return m_depth
; }
79 void *GetRawAccess() const;
80 void *BeginRawAccess();
83 bool HasAlpha() const { return m_hasAlpha
; }
84 void UseAlpha( bool useAlpha
);
88 wxPalette m_bitmapPalette
;
89 #endif // wxUSE_PALETTE
91 wxMask
* m_bitmapMask
; // Optional mask
92 CGImageRef
CreateCGImage() const;
94 // returns true if the bitmap has a size that
95 // can be natively transferred into a true icon
96 // if no is returned GetIconRef will still produce
97 // an icon but it will be generated via a PICT and
98 // rescaled to 16 x 16
101 // caller should increase ref count if needed longer
102 // than the bitmap exists
103 IconRef
GetIconRef();
105 #ifndef __WXOSX_IPHONE__
106 // returns a Pict from the bitmap content
107 PicHandle
GetPictHandle();
110 CGContextRef
GetBitmapContext() const;
112 int GetBytesPerRow() const { return m_bytesPerRow
; }
114 bool Create(int width
, int height
, int depth
);
115 bool Create( CGImageRef image
);
123 wxMemoryBuffer m_memBuf
;
124 int m_rawAccessCount
;
126 mutable CGImageRef m_cgImageRef
;
129 #ifndef __WXOSX_IPHONE__
130 PicHandle m_pictHandle
;
132 CGContextRef m_hBitmap
;
136 #define wxOSX_USE_PREMULTIPLIED_ALPHA 1
137 static const int kBestByteAlignement
= 16;
138 static const int kMaskBytesPerPixel
= 1;
140 static int GetBestBytesPerRow( int rawBytes
)
142 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
145 #if wxUSE_GUI && !defined(__WXOSX_IPHONE__)
147 // this is used for more controls than just the wxBitmap button, also for notebooks etc
149 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
151 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
154 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
158 if ( forceType
== 0 )
160 forceType
= kControlContentCGImageRef
;
163 if ( forceType
== kControlContentIconRef
)
166 wxBitmapRefData
* bmp
= bmap
;
168 if ( !bmap
->HasNativeSize() )
170 // as PICT conversion will only result in a 16x16 icon, let's attempt
171 // a few scales for better results
173 int w
= bitmap
.GetWidth() ;
174 int h
= bitmap
.GetHeight() ;
175 int sz
= wxMax( w
, h
) ;
176 if ( sz
== 24 || sz
== 64 )
178 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
179 bmp
= scaleBmp
.GetBitmapData() ;
183 info
->contentType
= kControlContentIconRef
;
184 info
->u
.iconRef
= bmp
->GetIconRef() ;
185 AcquireIconRef( info
->u
.iconRef
) ;
187 else if ( forceType
== kControlContentCGImageRef
)
189 info
->contentType
= kControlContentCGImageRef
;
190 info
->u
.imageRef
= (CGImageRef
) bmap
->CreateCGImage() ;
195 info
->contentType
= kControlContentPictHandle
;
196 info
->u
.picture
= bmap
->GetPictHandle() ;
202 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
204 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
207 return (CGImageRef
) bmap
->CreateCGImage();
210 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
212 if ( info
->contentType
== kControlContentIconRef
)
214 ReleaseIconRef( info
->u
.iconRef
) ;
216 else if ( info
->contentType
== kControlNoContent
)
218 // there's no bitmap at all, fall through silently
220 else if ( info
->contentType
== kControlContentPictHandle
)
222 // owned by the bitmap, no release here
224 else if ( info
->contentType
== kControlContentCGImageRef
)
226 CGImageRelease( info
->u
.imageRef
) ;
230 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
234 #endif //wxUSE_BMPBUTTON
236 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
238 void wxBitmapRefData::Init()
245 m_bitmapMask
= NULL
;
246 m_cgImageRef
= NULL
;
248 #ifndef __WXOSX_IPHONE__
250 m_pictHandle
= NULL
;
254 m_rawAccessCount
= 0 ;
258 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
261 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
263 if (tocopy
.m_bitmapMask
)
264 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
265 else if (tocopy
.m_hasAlpha
)
268 unsigned char* dest
= (unsigned char*)GetRawAccess();
269 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
270 size_t numbytes
= m_bytesPerRow
* m_height
;
271 memcpy( dest
, source
, numbytes
);
274 wxBitmapRefData::wxBitmapRefData()
279 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
282 Create( w
, h
, d
) ;
285 wxBitmapRefData::wxBitmapRefData(CGImageRef image
)
290 // code from Technical Q&A QA1509
292 bool wxBitmapRefData::Create(CGImageRef image
)
294 m_width
= CGImageGetWidth(image
);
295 m_height
= CGImageGetHeight(image
);
299 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
300 size_t size
= m_bytesPerRow
* m_height
;
301 void* data
= m_memBuf
.GetWriteBuf( size
) ;
304 memset( data
, 0 , size
) ;
305 m_memBuf
.UngetWriteBuf( size
) ;
306 CGImageAlphaInfo alpha
= CGImageGetAlphaInfo(image
);
307 if ( alpha
== kCGImageAlphaNone
|| alpha
== kCGImageAlphaNoneSkipLast
|| alpha
== kCGImageAlphaNoneSkipLast
)
309 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
314 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst
);
316 CGRect rect
= {{0,0},{m_width
,m_height
}};
317 CGContextDrawImage(m_hBitmap
, rect
, image
);
319 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
320 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
321 CGContextScaleCTM( m_hBitmap
, 1, -1 );
323 m_ok
= ( m_hBitmap
!= NULL
) ;
329 bool wxBitmapRefData::Create( int w
, int h
, int d
)
331 m_width
= wxMax(1, w
);
332 m_height
= wxMax(1, h
);
336 m_bytesPerRow
= GetBestBytesPerRow( m_width
* 4 ) ;
337 size_t size
= m_bytesPerRow
* m_height
;
338 void* data
= m_memBuf
.GetWriteBuf( size
) ;
341 memset( data
, 0 , size
) ;
342 m_memBuf
.UngetWriteBuf( size
) ;
344 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
345 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
346 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
347 CGContextScaleCTM( m_hBitmap
, 1, -1 );
349 m_ok
= ( m_hBitmap
!= NULL
) ;
354 void wxBitmapRefData::UseAlpha( bool use
)
356 if ( m_hasAlpha
== use
)
361 CGContextRelease( m_hBitmap
);
362 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
363 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
364 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
365 CGContextScaleCTM( m_hBitmap
, 1, -1 );
368 void *wxBitmapRefData::GetRawAccess() const
370 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
371 return m_memBuf
.GetData() ;
374 void *wxBitmapRefData::BeginRawAccess()
376 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") ) ;
377 wxASSERT( m_rawAccessCount
== 0 ) ;
378 #ifndef __WXOSX_IPHONE__
379 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
380 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
384 // we must destroy an existing cached image, as
385 // the bitmap data may change now
388 CGImageRelease( m_cgImageRef
) ;
389 m_cgImageRef
= NULL
;
392 return m_memBuf
.GetData() ;
395 void wxBitmapRefData::EndRawAccess()
397 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
398 wxASSERT( m_rawAccessCount
== 1 ) ;
403 bool wxBitmapRefData::HasNativeSize()
406 int h
= GetHeight() ;
407 int sz
= wxMax( w
, h
) ;
409 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
412 #ifndef __WXOSX_IPHONE__
413 IconRef
wxBitmapRefData::GetIconRef()
415 if ( m_iconRef
== NULL
)
417 // Create Icon Family Handle
419 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle( 0 );
422 int h
= GetHeight() ;
423 int sz
= wxMax( w
, h
) ;
425 OSType dataType
= 0 ;
426 OSType maskType
= 0 ;
431 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
432 if ( UMAGetSystemVersion() >= 0x1050 )
434 dataType
= kIconServices128PixelDataARGB
;
439 dataType
= kThumbnail32BitData
;
440 maskType
= kThumbnail8BitMask
;
445 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
446 if ( UMAGetSystemVersion() >= 0x1050 )
448 dataType
= kIconServices48PixelDataARGB
;
453 dataType
= kHuge32BitData
;
454 maskType
= kHuge8BitMask
;
459 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
460 if ( UMAGetSystemVersion() >= 0x1050 )
462 dataType
= kIconServices32PixelDataARGB
;
467 dataType
= kLarge32BitData
;
468 maskType
= kLarge8BitMask
;
473 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
474 if ( UMAGetSystemVersion() >= 0x1050 )
476 dataType
= kIconServices16PixelDataARGB
;
481 dataType
= kSmall32BitData
;
482 maskType
= kSmall8BitMask
;
492 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
493 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
495 size_t datasize
= sz
* sz
* 4 ;
496 Handle data
= NewHandle( datasize
) ;
498 unsigned char* ptr
= (unsigned char*) *data
;
499 memset( ptr
, 0, datasize
);
500 bool hasAlpha
= HasAlpha() ;
501 wxMask
*mask
= m_bitmapMask
;
502 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
503 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
505 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
507 unsigned char * source
= sourcePtr
;
508 unsigned char * masksource
= masksourcePtr
;
509 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
510 unsigned char a
, r
, g
, b
;
512 for ( int x
= 0 ; x
< w
; ++x
)
521 a
= 0xFF - *masksource
++ ;
523 else if ( !hasAlpha
)
527 #if wxOSX_USE_PREMULTIPLIED_ALPHA
528 // this must be non-premultiplied data
529 if ( a
!= 0xFF && a
!= 0 )
546 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
549 wxFAIL_MSG("Error when adding bitmap");
552 DisposeHandle( data
);
557 // setup the header properly
560 Handle maskdata
= NULL
;
561 unsigned char * maskptr
= NULL
;
562 unsigned char * ptr
= NULL
;
563 size_t datasize
, masksize
;
565 datasize
= sz
* sz
* 4 ;
566 data
= NewHandle( datasize
) ;
568 ptr
= (unsigned char*) *data
;
569 memset( ptr
, 0, datasize
) ;
572 maskdata
= NewHandle( masksize
) ;
574 maskptr
= (unsigned char*) *maskdata
;
575 memset( maskptr
, 0 , masksize
) ;
577 bool hasAlpha
= HasAlpha() ;
578 wxMask
*mask
= m_bitmapMask
;
579 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
580 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
582 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
584 unsigned char * source
= sourcePtr
;
585 unsigned char * masksource
= masksourcePtr
;
586 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
587 unsigned char * maskdest
= maskptr
+ y
* sz
;
588 unsigned char a
, r
, g
, b
;
590 for ( int x
= 0 ; x
< w
; ++x
)
603 *maskdest
++ = 0xFF - *masksource
++ ;
611 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
612 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
614 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
615 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
618 HUnlock( maskdata
) ;
619 DisposeHandle( data
) ;
620 DisposeHandle( maskdata
) ;
625 PicHandle pic
= GetPictHandle() ;
626 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
628 // transform into IconRef
630 // cleaner version existing from 10.3 upwards
631 HLock((Handle
) iconFamily
);
632 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
633 HUnlock((Handle
) iconFamily
);
634 DisposeHandle( (Handle
) iconFamily
) ;
636 wxCHECK_MSG( err
== noErr
, NULL
, wxT("Error when constructing icon ref") );
642 PicHandle
wxBitmapRefData::GetPictHandle()
644 if ( m_pictHandle
== NULL
)
647 GraphicsExportComponent exporter
= 0;
648 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
651 m_pictHandle
= (PicHandle
) NewHandle(0);
654 // QT does not correctly export the mask
655 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
656 CGImageRef imageRef
= CreateCGImage();
657 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
658 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
659 err
= GraphicsExportDoExport(exporter
, NULL
);
660 CGImageRelease( imageRef
);
662 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
663 // the 512 bytes header is only needed for pict files, but not in memory
664 if ( handleSize
>= 512 )
666 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
667 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
670 CloseComponent( exporter
);
675 return m_pictHandle
;
679 CGImageRef
wxBitmapRefData::CreateCGImage() const
682 wxASSERT( m_rawAccessCount
>= 0 ) ;
684 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
686 if ( m_depth
!= 1 && m_bitmapMask
== NULL
)
689 // in order for this code to work properly, wxMask would have to invert black and white
690 // in the native bitmap
693 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
694 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
695 image
= CGImageCreateWithMask( tempImage
, tempMask
);
696 CGImageRelease(tempMask
);
697 CGImageRelease(tempImage
);
701 image
= CGBitmapContextCreateImage( m_hBitmap
);
705 size_t imageSize
= m_height
* m_bytesPerRow
;
706 void * dataBuffer
= m_memBuf
.GetData() ;
709 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
710 wxMemoryBuffer membuf
;
714 alphaInfo
= kCGImageAlphaFirst
;
715 unsigned char *destalphastart
= (unsigned char*) membuf
.GetWriteBuf( imageSize
) ;
716 memcpy( destalphastart
, dataBuffer
, imageSize
) ;
717 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
718 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
719 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
721 unsigned char *sourcemask
= sourcemaskstart
;
722 unsigned char *destalpha
= destalphastart
;
723 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
725 *destalpha
= 0xFF - *sourcemask
;
728 membuf
.UngetWriteBuf( imageSize
);
734 #if wxOSX_USE_PREMULTIPLIED_ALPHA
735 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
737 alphaInfo
= kCGImageAlphaFirst
;
744 CGDataProviderRef dataProvider
= NULL
;
747 // TODO CHECK ALIGNMENT
748 wxMemoryBuffer maskBuf
;
749 unsigned char * maskBufData
= (unsigned char*) maskBuf
.GetWriteBuf( m_width
* m_height
);
750 unsigned char * bufData
= (unsigned char *) membuf
.GetData() ;
751 // copy one color component
753 for( int y
= 0 ; y
< m_height
; bufData
+= m_bytesPerRow
, ++y
)
755 unsigned char *bufDataIter
= bufData
+3;
756 for ( int x
= 0 ; x
< m_width
; bufDataIter
+= 4, ++x
, ++i
)
758 maskBufData
[i
] = *bufDataIter
;
761 maskBuf
.UngetWriteBuf( m_width
* m_height
);
764 wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf
);
766 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
770 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
771 dataProvider
= wxMacCGDataProviderCreateWithMemoryBuffer( membuf
);
774 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
775 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
777 CGDataProviderRelease( dataProvider
);
782 image
= m_cgImageRef
;
783 CGImageRetain( image
) ;
786 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
788 // we keep it for later use
789 m_cgImageRef
= image
;
790 CGImageRetain( image
) ;
796 CGContextRef
wxBitmapRefData::GetBitmapContext() const
801 void wxBitmapRefData::Free()
803 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
807 CGImageRelease( m_cgImageRef
) ;
808 m_cgImageRef
= NULL
;
810 #ifndef __WXOSX_IPHONE__
813 ReleaseIconRef( m_iconRef
) ;
820 KillPicture( m_pictHandle
) ;
821 m_pictHandle
= NULL
;
827 CGContextRelease(m_hBitmap
);
831 wxDELETE(m_bitmapMask
);
834 wxBitmapRefData::~wxBitmapRefData()
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
845 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
847 bool created
= false ;
848 int w
= icon
.GetWidth() ;
849 int h
= icon
.GetHeight() ;
852 #ifdef __WXOSX_CARBON__
853 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
855 IconFamilyHandle iconFamily
= NULL
;
856 Handle imagehandle
= NewHandle( 0 ) ;
857 Handle maskhandle
= NewHandle( 0 ) ;
861 IconSelectorValue selector
= 0 ;
866 dataType
= kThumbnail32BitData
;
867 maskType
= kThumbnail8BitMask
;
868 selector
= kSelectorAllAvailableData
;
872 dataType
= kHuge32BitData
;
873 maskType
= kHuge8BitMask
;
874 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
878 dataType
= kLarge32BitData
;
879 maskType
= kLarge8BitMask
;
880 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
884 dataType
= kSmall32BitData
;
885 maskType
= kSmall8BitMask
;
886 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
893 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
895 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
896 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
897 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
898 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
900 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
902 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
903 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
907 unsigned char *source
= (unsigned char *) *imagehandle
;
908 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
909 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
911 for ( int y
= 0 ; y
< h
; ++y
)
913 for ( int x
= 0 ; x
< w
; ++x
)
915 unsigned char a
= *sourcemask
++;
918 #if wxOSX_USE_PREMULTIPLIED_ALPHA
919 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
920 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
921 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
923 *destination
++ = *source
++ ;
924 *destination
++ = *source
++ ;
925 *destination
++ = *source
++ ;
931 DisposeHandle( imagehandle
) ;
932 DisposeHandle( maskhandle
) ;
936 DisposeHandle( (Handle
) iconFamily
) ;
942 dc
.SelectObject( *this ) ;
943 dc
.DrawIcon( icon
, 0 , 0 ) ;
944 dc
.SelectObject( wxNullBitmap
) ;
950 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
952 wxBitmapRefData
* bitmapRefData
;
954 m_refData
= bitmapRefData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
956 if (bitmapRefData
->IsOk())
960 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
961 if ( the_width
% (sizeof(unsigned char) * 8) )
962 linesize
+= sizeof(unsigned char);
964 unsigned char* linestart
= (unsigned char*) bits
;
965 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
967 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
969 unsigned char* destination
= destptr
;
970 int index
, bit
, mask
;
972 for ( int x
= 0 ; x
< the_width
; ++x
)
978 if ( linestart
[index
] & mask
)
980 *destination
++ = 0xFF ;
987 *destination
++ = 0xFF ;
988 *destination
++ = 0xFF ;
989 *destination
++ = 0xFF ;
990 *destination
++ = 0xFF ;
999 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
1001 } /* bitmapRefData->IsOk() */
1004 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1006 (void) Create(data
, type
, width
, height
, depth
);
1009 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1011 LoadFile(filename
, type
);
1014 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
1016 return new wxBitmapRefData
;
1019 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
1021 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
1024 void * wxBitmap::GetRawAccess() const
1026 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1028 return M_BITMAPDATA
->GetRawAccess() ;
1031 void * wxBitmap::BeginRawAccess()
1033 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1035 return M_BITMAPDATA
->BeginRawAccess() ;
1038 void wxBitmap::EndRawAccess()
1040 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
1042 M_BITMAPDATA
->EndRawAccess() ;
1045 CGImageRef
wxBitmap::CreateCGImage() const
1047 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
1049 return M_BITMAPDATA
->CreateCGImage() ;
1052 #ifndef __WXOSX_IPHONE__
1053 IconRef
wxBitmap::GetIconRef() const
1055 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
1057 return M_BITMAPDATA
->GetIconRef() ;
1060 IconRef
wxBitmap::CreateIconRef() const
1062 IconRef icon
= GetIconRef();
1063 verify_noerr( AcquireIconRef(icon
) );
1070 WX_NSImage
wxBitmap::GetNSImage() const
1072 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1073 return wxOSXGetNSImageFromCGImage( cgimage
);
1078 #if wxOSX_USE_IPHONE
1080 WX_UIImage
wxBitmap::GetUIImage() const
1082 wxCFRef
< CGImageRef
> cgimage(CreateCGImage());
1083 return wxOSXGetUIImageFromCGImage( cgimage
);
1087 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1089 wxCHECK_MSG( Ok() &&
1090 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1091 (rect
.x
+rect
.width
<= GetWidth()) &&
1092 (rect
.y
+rect
.height
<= GetHeight()),
1093 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1095 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1096 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1098 int destwidth
= rect
.width
;
1099 int destheight
= rect
.height
;
1102 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1103 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1104 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1106 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1107 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1108 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1109 unsigned char *dest
= destdata
;
1111 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1113 memcpy( dest
, source
, destlinesize
) ;
1117 ret
.EndRawAccess() ;
1119 if ( M_BITMAPDATA
->m_bitmapMask
)
1121 wxMemoryBuffer maskbuf
;
1122 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1123 size_t maskbufsize
= rowBytes
* destheight
;
1125 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1126 int destlinesize
= rowBytes
;
1128 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1129 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1130 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1132 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1133 unsigned char *dest
= destdata
;
1135 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1137 memcpy( dest
, source
, destlinesize
) ;
1140 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1141 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1143 else if ( HasAlpha() )
1149 bool wxBitmap::Create(int w
, int h
, int d
)
1154 d
= wxDisplayDepth() ;
1156 m_refData
= new wxBitmapRefData( w
, h
, d
);
1158 return M_BITMAPDATA
->IsOk() ;
1162 bool wxBitmap::Create(CGImageRef image
)
1166 m_refData
= new wxBitmapRefData( image
);
1168 return M_BITMAPDATA
->IsOk() ;
1171 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1175 wxBitmapHandler
*handler
= FindHandler(type
);
1179 m_refData
= new wxBitmapRefData
;
1181 return handler
->LoadFile(this, filename
, type
, -1, -1);
1186 wxImage
loadimage(filename
, type
);
1196 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1201 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1205 m_refData
= new wxBitmapRefData
;
1207 wxBitmapHandler
*handler
= FindHandler(type
);
1209 if ( handler
== NULL
)
1211 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1216 return handler
->Create(this, data
, type
, width
, height
, depth
);
1221 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1223 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1225 // width and height of the device-dependent bitmap
1226 int width
= image
.GetWidth();
1227 int height
= image
.GetHeight();
1229 wxBitmapRefData
* bitmapRefData
;
1231 m_refData
= bitmapRefData
= new wxBitmapRefData( width
, height
, depth
) ;
1233 if ( bitmapRefData
->IsOk())
1237 bool hasAlpha
= false ;
1239 if ( image
.HasMask() )
1241 // takes precedence, don't mix with alpha info
1245 hasAlpha
= image
.HasAlpha() ;
1251 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1252 register unsigned char* data
= image
.GetData();
1253 if ( destinationstart
!= NULL
&& data
!= NULL
)
1255 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1256 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1258 unsigned char * destination
= destinationstart
;
1259 for (int x
= 0; x
< width
; x
++)
1263 const unsigned char a
= *alpha
++;
1264 *destination
++ = a
;
1266 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1267 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1268 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1269 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1271 *destination
++ = *data
++ ;
1272 *destination
++ = *data
++ ;
1273 *destination
++ = *data
++ ;
1278 *destination
++ = 0xFF ;
1279 *destination
++ = *data
++ ;
1280 *destination
++ = *data
++ ;
1281 *destination
++ = *data
++ ;
1288 if ( image
.HasMask() )
1289 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1290 } /* bitmapRefData->IsOk() */
1293 wxImage
wxBitmap::ConvertToImage() const
1297 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1299 // create an wxImage object
1300 int width
= GetWidth();
1301 int height
= GetHeight();
1302 image
.Create( width
, height
);
1304 unsigned char *data
= image
.GetData();
1305 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1307 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1309 bool hasAlpha
= false ;
1310 bool hasMask
= false ;
1311 int maskBytesPerRow
= 0 ;
1312 unsigned char *alpha
= NULL
;
1313 unsigned char *mask
= NULL
;
1321 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1322 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1328 alpha
= image
.GetAlpha() ;
1333 // The following masking algorithm is the same as well in msw/gtk:
1334 // the colour used as transparent one in wxImage and the one it is
1335 // replaced with when it actually occurs in the bitmap
1336 static const int MASK_RED
= 1;
1337 static const int MASK_GREEN
= 2;
1338 static const int MASK_BLUE
= 3;
1339 static const int MASK_BLUE_REPLACEMENT
= 2;
1341 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1343 unsigned char * maskp
= mask
;
1344 unsigned char * source
= sourcestart
;
1345 unsigned char a
, r
, g
, b
;
1348 for (int xx
= 0; xx
< width
; xx
++)
1350 color
= *((long*) source
) ;
1351 #ifdef WORDS_BIGENDIAN
1352 a
= ((color
&0xFF000000) >> 24) ;
1353 r
= ((color
&0x00FF0000) >> 16) ;
1354 g
= ((color
&0x0000FF00) >> 8) ;
1355 b
= (color
&0x000000FF);
1357 b
= ((color
&0xFF000000) >> 24) ;
1358 g
= ((color
&0x00FF0000) >> 16) ;
1359 r
= ((color
&0x0000FF00) >> 8) ;
1360 a
= (color
&0x000000FF);
1364 if ( *maskp
++ == 0xFF )
1370 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1371 b
= MASK_BLUE_REPLACEMENT
;
1373 else if ( hasAlpha
)
1376 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1377 // this must be non-premultiplied data
1378 if ( a
!= 0xFF && a
!= 0 )
1388 data
[index
+ 1] = g
;
1389 data
[index
+ 2] = b
;
1397 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1402 #endif //wxUSE_IMAGE
1404 bool wxBitmap::SaveFile( const wxString
& filename
,
1405 wxBitmapType type
, const wxPalette
*palette
) const
1407 bool success
= false;
1408 wxBitmapHandler
*handler
= FindHandler(type
);
1412 success
= handler
->SaveFile(this, filename
, type
, palette
);
1417 wxImage image
= ConvertToImage();
1418 success
= image
.SaveFile(filename
, type
);
1420 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1427 int wxBitmap::GetHeight() const
1429 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1431 return M_BITMAPDATA
->GetHeight();
1434 int wxBitmap::GetWidth() const
1436 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1438 return M_BITMAPDATA
->GetWidth() ;
1441 int wxBitmap::GetDepth() const
1443 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1445 return M_BITMAPDATA
->GetDepth();
1448 wxMask
*wxBitmap::GetMask() const
1450 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1452 return M_BITMAPDATA
->m_bitmapMask
;
1455 bool wxBitmap::HasAlpha() const
1457 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1459 return M_BITMAPDATA
->HasAlpha() ;
1462 void wxBitmap::SetWidth(int w
)
1465 M_BITMAPDATA
->SetWidth(w
);
1468 void wxBitmap::SetHeight(int h
)
1471 M_BITMAPDATA
->SetHeight(h
);
1474 void wxBitmap::SetDepth(int d
)
1477 M_BITMAPDATA
->SetDepth(d
);
1480 void wxBitmap::SetOk(bool isOk
)
1483 M_BITMAPDATA
->SetOk(isOk
);
1487 wxPalette
*wxBitmap::GetPalette() const
1489 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1491 return &M_BITMAPDATA
->m_bitmapPalette
;
1494 void wxBitmap::SetPalette(const wxPalette
& palette
)
1497 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1499 #endif // wxUSE_PALETTE
1501 void wxBitmap::SetMask(wxMask
*mask
)
1504 // Remove existing mask if there is one.
1505 delete M_BITMAPDATA
->m_bitmapMask
;
1507 M_BITMAPDATA
->m_bitmapMask
= mask
;
1510 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1514 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1517 // ----------------------------------------------------------------------------
1519 // ----------------------------------------------------------------------------
1526 wxMask::wxMask(const wxMask
&tocopy
)
1530 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1531 m_width
= tocopy
.m_width
;
1532 m_height
= tocopy
.m_height
;
1534 size_t size
= m_bytesPerRow
* m_height
;
1535 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1536 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1537 memcpy( dest
, source
, size
);
1538 m_memBuf
.UngetWriteBuf( size
) ;
1542 // Construct a mask from a bitmap and a colour indicating
1543 // the transparent area
1544 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1547 Create( bitmap
, colour
);
1550 // Construct a mask from a mono bitmap (copies the bitmap).
1551 wxMask::wxMask( const wxBitmap
& bitmap
)
1557 // Construct a mask from a mono bitmap (copies the bitmap).
1559 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1562 Create( data
, width
, height
, bytesPerRow
);
1569 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1570 m_maskBitmap
= NULL
;
1576 m_width
= m_height
= m_bytesPerRow
= 0 ;
1577 m_maskBitmap
= NULL
;
1580 void *wxMask::GetRawAccess() const
1582 return m_memBuf
.GetData() ;
1585 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1586 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1588 void wxMask::RealizeNative()
1592 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1593 m_maskBitmap
= NULL
;
1596 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1597 // from MouseTracking sample :
1598 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1599 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1601 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1602 kCGImageAlphaNone
);
1603 CGColorSpaceRelease( colorspace
);
1604 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1607 // Create a mask from a mono bitmap (copies the bitmap).
1609 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1614 m_bytesPerRow
= bytesPerRow
;
1616 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1623 // Create a mask from a mono bitmap (copies the bitmap).
1624 bool wxMask::Create(const wxBitmap
& bitmap
)
1626 m_width
= bitmap
.GetWidth() ;
1627 m_height
= bitmap
.GetHeight() ;
1628 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1630 size_t size
= m_bytesPerRow
* m_height
;
1631 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1632 wxASSERT( destdatabase
!= NULL
) ;
1634 memset( destdatabase
, 0 , size
) ;
1635 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1637 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1639 unsigned char *destdata
= destdatabase
;
1640 unsigned char r
, g
, b
;
1642 for ( int x
= 0 ; x
< m_width
; ++x
)
1649 if ( ( r
+ g
+ b
) > 0x10 )
1650 *destdata
++ = 0xFF ;
1652 *destdata
++ = 0x00 ;
1656 m_memBuf
.UngetWriteBuf( size
) ;
1662 // Create a mask from a bitmap and a colour indicating
1663 // the transparent area
1664 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1666 m_width
= bitmap
.GetWidth() ;
1667 m_height
= bitmap
.GetHeight() ;
1668 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1670 size_t size
= m_bytesPerRow
* m_height
;
1671 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1672 wxASSERT( destdatabase
!= NULL
) ;
1674 memset( destdatabase
, 0 , size
) ;
1675 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1676 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1678 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1680 unsigned char *srcdata
= srcdatabase
;
1681 unsigned char *destdata
= destdatabase
;
1682 unsigned char r
, g
, b
;
1684 for ( int x
= 0 ; x
< m_width
; ++x
)
1691 if ( colour
== wxColour( r
, g
, b
) )
1692 *destdata
++ = 0xFF ;
1694 *destdata
++ = 0x00 ;
1698 m_memBuf
.UngetWriteBuf( size
) ;
1704 WXHBITMAP
wxMask::GetHBITMAP() const
1706 return m_maskBitmap
;
1709 // ----------------------------------------------------------------------------
1710 // Standard Handlers
1711 // ----------------------------------------------------------------------------
1713 class WXDLLEXPORT wxBundleResourceHandler
: public wxBitmapHandler
1715 DECLARE_ABSTRACT_CLASS(wxPNGResourceHandler
)
1718 inline wxBundleResourceHandler()
1722 virtual bool LoadFile(wxBitmap
*bitmap
,
1723 const wxString
& name
,
1729 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler
, wxBitmapHandler
);
1731 class WXDLLEXPORT wxPNGResourceHandler
: public wxBundleResourceHandler
1733 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1736 inline wxPNGResourceHandler()
1738 SetName(wxT("PNG resource"));
1739 SetExtension("PNG");
1740 SetType(wxBITMAP_TYPE_PNG_RESOURCE
);
1744 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler
, wxBundleResourceHandler
)
1746 class WXDLLEXPORT wxJPEGResourceHandler
: public wxBundleResourceHandler
1748 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler
)
1751 inline wxJPEGResourceHandler()
1753 SetName(wxT("JPEG resource"));
1754 SetExtension("JPEG");
1755 SetType(wxBITMAP_TYPE_JPEG_RESOURCE
);
1759 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler
, wxBundleResourceHandler
)
1761 bool wxBundleResourceHandler::LoadFile(wxBitmap
*bitmap
,
1762 const wxString
& name
,
1763 wxBitmapType
WXUNUSED(type
),
1764 int WXUNUSED(desiredWidth
),
1765 int WXUNUSED(desiredHeight
))
1767 wxString ext
= GetExtension().Lower();
1768 wxCFStringRef
resname(name
);
1769 wxCFStringRef
restype(ext
);
1771 wxCFRef
<CFURLRef
> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname
, restype
, NULL
));
1773 if ( imageURL
.get() != NULL
)
1775 // Create the data provider object
1776 wxCFRef
<CGDataProviderRef
> provider(CGDataProviderCreateWithURL (imageURL
) );
1777 CGImageRef image
= NULL
;
1779 if ( ext
== "jpeg" )
1780 image
= CGImageCreateWithJPEGDataProvider (provider
, NULL
, true,
1781 kCGRenderingIntentDefault
);
1782 else if ( ext
== "png" )
1783 image
= CGImageCreateWithPNGDataProvider (provider
, NULL
, true,
1784 kCGRenderingIntentDefault
);
1785 if ( image
!= NULL
)
1787 bitmap
->Create(image
);
1788 CGImageRelease(image
);
1795 #if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__)
1797 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1799 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1802 inline wxPICTResourceHandler()
1804 SetName(wxT("Macintosh Pict resource"));
1805 SetExtension(wxEmptyString
);
1806 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1809 virtual bool LoadFile(wxBitmap
*bitmap
,
1810 const wxString
& name
,
1816 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1819 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1820 const wxString
& name
,
1821 wxBitmapType
WXUNUSED(type
),
1822 int WXUNUSED(desiredWidth
),
1823 int WXUNUSED(desiredHeight
))
1827 wxMacStringToPascal( name
, theName
) ;
1829 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1834 mf
.SetPICT( thePict
) ;
1835 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1837 dc
.SelectObject( *bitmap
) ;
1839 dc
.SelectObject( wxNullBitmap
) ;
1849 void wxBitmap::InitStandardHandlers()
1851 #if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__)
1852 AddHandler( new wxPICTResourceHandler
) ;
1854 #if wxOSX_USE_COCOA_OR_CARBON
1855 AddHandler( new wxICONResourceHandler
) ;
1857 AddHandler( new wxPNGResourceHandler
);
1858 AddHandler( new wxJPEGResourceHandler
);
1861 // ----------------------------------------------------------------------------
1862 // raw bitmap access support
1863 // ----------------------------------------------------------------------------
1865 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1868 // no bitmap, no data (raw or otherwise)
1871 data
.m_width
= GetWidth() ;
1872 data
.m_height
= GetHeight() ;
1873 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1875 return BeginRawAccess() ;
1878 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1883 void wxBitmap::UseAlpha()
1885 // remember that we are using alpha channel:
1886 // we'll need to create a proper mask in UngetRawData()
1887 M_BITMAPDATA
->UseAlpha( true );