1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bitmap.h"
16 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
22 #include "wx/metafile.h"
23 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 #include "wx/dcmemory.h"
41 // Implementation Notes
42 // --------------------
44 // we are always working with a 32 bit deep pixel buffer
45 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
46 // therefore we have a separate GWorld there for blitting the mask in
48 // under Quartz then content is transformed into a CGImageRef representing the same data
49 // which can be transferred to the GPU by the OS for fast rendering
51 // we don't dare premultiplied alpha yet
52 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
56 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
58 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
61 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
65 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
69 wxBitmapRefData
* bmp
= bmap
;
71 if ( !bmap
->HasNativeSize() )
73 // as PICT conversion will only result in a 16x16 icon, let's attempt
74 // a few scales for better results
76 int w
= bitmap
.GetWidth() ;
77 int h
= bitmap
.GetHeight() ;
78 int sz
= wxMax( w
, h
) ;
79 if ( sz
== 24 || sz
== 64)
81 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
82 bmp
= scaleBmp
.GetBitmapData() ;
86 info
->contentType
= kControlContentIconRef
;
87 info
->u
.iconRef
= bmp
->GetIconRef() ;
88 AcquireIconRef( info
->u
.iconRef
) ;
90 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
91 else if ( forceType
== kControlContentCGImageRef
)
93 info
->contentType
= kControlContentCGImageRef
;
94 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
99 info
->contentType
= kControlContentPictHandle
;
100 info
->u
.picture
= bmap
->GetPictHandle() ;
105 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
107 if ( info
->contentType
== kControlContentIconRef
)
109 ReleaseIconRef( info
->u
.iconRef
) ;
111 else if ( info
->contentType
== kControlNoContent
)
113 // there's no bitmap at all, fall through silently
115 else if ( info
->contentType
== kControlContentPictHandle
)
117 // owned by the bitmap, no release here
119 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
120 else if ( info
->contentType
== kControlContentCGImageRef
)
122 CGImageRelease( info
->u
.imageRef
) ;
127 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
131 #endif //wxUSE_BMPBUTTON
133 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
135 void wxBitmapRefData::Init()
141 m_bitmapMask
= NULL
;
142 #if wxMAC_USE_CORE_GRAPHICS
143 m_cgImageRef
= NULL
;
146 m_pictHandle
= NULL
;
148 m_hMaskBitmap
= NULL
;
149 m_maskBytesPerRow
= 0 ;
151 m_rawAccessCount
= 0 ;
155 wxBitmapRefData::wxBitmapRefData()
160 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
163 Create( w
, h
, d
) ;
166 bool wxBitmapRefData::Create( int w
, int h
, int d
)
172 m_bytesPerRow
= w
* 4 ;
173 size_t size
= m_bytesPerRow
* h
;
174 void* data
= m_memBuf
.GetWriteBuf(size
) ;
175 memset( data
, 0 , size
) ;
176 m_memBuf
.UngetWriteBuf(size
) ;
179 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
180 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
181 (char*) data
, m_bytesPerRow
) ) ;
182 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
183 m_ok
= ( m_hBitmap
!= NULL
) ;
188 void wxBitmapRefData::UseAlpha( bool use
)
190 if ( m_hasAlpha
== use
)
196 int width
= GetWidth() ;
197 int height
= GetHeight() ;
198 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
199 size_t size
= height
* m_maskBytesPerRow
;
200 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
201 memset( data
, 0 , size
) ;
202 wxASSERT( m_hMaskBitmap
== NULL
) ;
203 Rect rect
= { 0 , 0 , height
, width
} ;
204 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
205 (char*) data
, m_maskBytesPerRow
) ) ;
206 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
207 m_maskMemBuf
.UngetWriteBuf(size
) ;
208 #if !wxMAC_USE_CORE_GRAPHICS
214 DisposeGWorld( m_hMaskBitmap
) ;
215 m_hMaskBitmap
= NULL
;
216 m_maskBytesPerRow
= 0 ;
220 void *wxBitmapRefData::GetRawAccess() const
222 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
223 return m_memBuf
.GetData() ;
226 void *wxBitmapRefData::BeginRawAccess()
228 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
229 wxASSERT( m_rawAccessCount
== 0 ) ;
231 // we must destroy an existing cached image, as
232 // the bitmap data may change now
233 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
234 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
235 #if wxMAC_USE_CORE_GRAPHICS
238 CGImageRelease( m_cgImageRef
) ;
239 m_cgImageRef
= NULL
;
242 return m_memBuf
.GetData() ;
245 void wxBitmapRefData::EndRawAccess()
247 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
248 wxASSERT( m_rawAccessCount
== 1 ) ;
250 #if !wxMAC_USE_CORE_GRAPHICS
255 bool wxBitmapRefData::HasNativeSize()
258 int h
= GetHeight() ;
259 int sz
= wxMax( w
, h
) ;
261 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
266 IconRef
wxBitmapRefData::GetIconRef()
268 if ( m_iconRef
== NULL
)
270 // Create Icon Family Handle
272 IconFamilyHandle iconFamily
= NULL
;
274 #ifdef WORDS_BIGENDIAN
275 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
276 (**iconFamily
).resourceType
= kIconFamilyType
;
277 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
279 // test this solution on big endian as well
280 iconFamily
= (IconFamilyHandle
) NewHandle(0) ;
284 int h
= GetHeight() ;
285 int sz
= wxMax( w
, h
) ;
287 OSType dataType
= 0 ;
288 OSType maskType
= 0 ;
292 dataType
= kThumbnail32BitData
;
293 maskType
= kThumbnail8BitMask
;
297 dataType
= kHuge32BitData
;
298 maskType
= kHuge8BitMask
;
302 dataType
= kLarge32BitData
;
303 maskType
= kLarge8BitMask
;
307 dataType
= kSmall32BitData
;
308 maskType
= kSmall8BitMask
;
313 // setup the header properly
316 Handle maskdata
= NULL
;
317 unsigned char * maskptr
= NULL
;
318 unsigned char * ptr
= NULL
;
323 data
= NewHandle( size
) ;
325 ptr
= (unsigned char*) *data
;
326 memset( ptr
, 0, size
) ;
329 maskdata
= NewHandle( masksize
) ;
331 maskptr
= (unsigned char*) *maskdata
;
332 memset( maskptr
, 0 , masksize
) ;
334 bool hasAlpha
= HasAlpha() ;
335 wxMask
*mask
= m_bitmapMask
;
336 unsigned char * source
= (unsigned char*) GetRawAccess() ;
337 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
338 for ( int y
= 0 ; y
< h
; ++y
)
340 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
341 unsigned char * maskdest
= maskptr
+ y
* sz
;
342 for ( int x
= 0 ; x
< w
; ++x
)
344 unsigned char a
= *source
++ ;
345 unsigned char r
= *source
++ ;
346 unsigned char g
= *source
++ ;
347 unsigned char b
= *source
++ ;
355 *maskdest
++ = *masksource
++ ;
363 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
364 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
366 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
367 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
369 HUnlock( maskdata
) ;
370 DisposeHandle( data
) ;
371 DisposeHandle( maskdata
) ;
375 PicHandle pic
= GetPictHandle() ;
376 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
379 // transform into IconRef
381 static int iconCounter
= 2 ;
385 RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
386 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
387 // we have to retain a reference, as Unregister will decrement it
388 AcquireIconRef( m_iconRef
) ;
389 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
390 DisposeHandle( (Handle
) iconFamily
) ;
396 PicHandle
wxBitmapRefData::GetPictHandle()
398 if ( m_pictHandle
== NULL
)
400 CGrafPtr origPort
= NULL
;
401 GDHandle origDev
= NULL
;
402 GWorldPtr wp
= NULL
;
403 GWorldPtr mask
= NULL
;
404 int height
= GetHeight() ;
405 int width
= GetWidth() ;
407 Rect rect
= { 0 , 0 , height
, width
} ;
409 GetGWorld( &origPort
, &origDev
) ;
411 wp
= GetHBITMAP( &mask
) ;
413 RgnHandle clipRgn
= NULL
;
417 GWorldPtr monoworld
;
419 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
421 LockPixels( GetGWorldPixMap( monoworld
) ) ;
422 LockPixels( GetGWorldPixMap( mask
) ) ;
423 SetGWorld( monoworld
, NULL
) ;
424 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
425 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
426 RGBForeColor( &black
) ;
427 RGBBackColor( &white
) ;
428 CopyBits(GetPortBitMapForCopyBits(mask
),
429 GetPortBitMapForCopyBits(monoworld
),
433 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
434 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
435 UnlockPixels( GetGWorldPixMap( mask
) ) ;
436 DisposeGWorld( monoworld
) ;
439 SetGWorld( wp
, NULL
) ;
441 GetPortBounds( wp
, &portRect
) ;
442 m_pictHandle
= OpenPicture(&portRect
);
446 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
447 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
448 RGBForeColor( &black
) ;
449 RGBBackColor( &white
) ;
454 LockPixels( GetGWorldPixMap( wp
) ) ;
455 CopyBits(GetPortBitMapForCopyBits(wp
),
456 GetPortBitMapForCopyBits(wp
),
460 UnlockPixels( GetGWorldPixMap( wp
) ) ;
463 SetGWorld( origPort
, origDev
) ;
465 DisposeRgn( clipRgn
) ;
467 return m_pictHandle
;
471 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
473 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
474 wxASSERT( data
== membuf
->GetData() ) ;
478 CGImageRef
wxBitmapRefData::CGImageCreate() const
481 wxASSERT( m_rawAccessCount
>= 0 ) ;
483 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
485 size_t imageSize
= m_width
* m_height
* 4 ;
486 void * dataBuffer
= m_memBuf
.GetData() ;
489 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
490 wxMemoryBuffer
* membuf
= NULL
;
494 membuf
= new wxMemoryBuffer( imageSize
) ;
495 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
496 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
497 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
498 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
499 alphaInfo
= kCGImageAlphaFirst
;
500 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
502 unsigned char *sourcemask
= sourcemaskstart
;
503 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
505 *destalpha
= *sourcemask
;
509 else if ( m_hasAlpha
)
511 #if wxMAC_USE_PREMULTIPLIED_ALPHA
512 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
514 alphaInfo
= kCGImageAlphaFirst
;
516 membuf
= new wxMemoryBuffer( m_memBuf
) ;
520 membuf
= new wxMemoryBuffer( m_memBuf
) ;
522 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
523 CGDataProviderRef dataProvider
=
524 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
525 wxMacMemoryBufferReleaseProc
);
527 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
528 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
529 CGDataProviderRelease( dataProvider
);
533 image
= m_cgImageRef
;
534 CGImageRetain( image
) ;
536 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
538 // we keep it for later use
539 m_cgImageRef
= image
;
540 CGImageRetain( image
) ;
546 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
548 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
553 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
554 else if ( m_hasAlpha
)
556 #if !wxMAC_USE_CORE_GRAPHICS
557 if ( m_rawAccessCount
> 0 )
560 // this structure is not kept in synch when using CG, so if someone
561 // is really accessing the Graphports, we have to sync it
564 *mask
= m_hMaskBitmap
;
570 void wxBitmapRefData::UpdateAlphaMask() const
574 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
575 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
577 int h
= GetHeight() ;
580 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
582 unsigned char* destalpha
= destalphabase
;
583 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
585 // we must have 24 bit depth for non quartz smooth alpha
587 *destalpha
++ = 255 - *sourcemask
;
588 *destalpha
++ = 255 - *sourcemask
;
589 *destalpha
++ = 255 - *sourcemask
;
595 void wxBitmapRefData::Free()
597 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
599 #if wxMAC_USE_CORE_GRAPHICS
602 CGImageRelease( m_cgImageRef
) ;
603 m_cgImageRef
= NULL
;
608 ReleaseIconRef( m_iconRef
) ;
613 KillPicture( m_pictHandle
) ;
614 m_pictHandle
= NULL
;
618 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
623 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
624 m_hMaskBitmap
= NULL
;
634 wxBitmapRefData::~wxBitmapRefData()
639 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
641 int w
= icon
.GetWidth() ;
642 int h
= icon
.GetHeight() ;
645 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
647 bool created
= false ;
649 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
651 IconFamilyHandle iconFamily
= NULL
;
652 Handle imagehandle
= NewHandle(0) ;
653 Handle maskhandle
= NewHandle(0) ;
657 IconSelectorValue selector
= 0 ;
660 dataType
= kThumbnail32BitData
;
661 maskType
= kThumbnail8BitMask
;
662 selector
= kSelectorAllAvailableData
;
666 dataType
= kHuge32BitData
;
667 maskType
= kHuge8BitMask
;
668 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
672 dataType
= kLarge32BitData
;
673 maskType
= kLarge8BitMask
;
674 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
678 dataType
= kSmall32BitData
;
679 maskType
= kSmall8BitMask
;
680 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
684 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
686 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
687 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
688 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
689 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
691 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
693 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
694 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
696 unsigned char *source
= (unsigned char *) *imagehandle
;
697 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
699 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
700 for ( int y
= 0 ; y
< h
; ++y
)
702 for ( int x
= 0 ; x
< w
; ++x
)
704 *destination
++ = *sourcemask
++ ;
706 *destination
++ = *source
++ ;
707 *destination
++ = *source
++ ;
708 *destination
++ = *source
++ ;
712 DisposeHandle( imagehandle
) ;
713 DisposeHandle( maskhandle
) ;
716 DisposeHandle( (Handle
) iconFamily
) ;
723 dc
.SelectObject( *this ) ;
724 dc
.DrawIcon( icon
, 0 , 0 ) ;
725 dc
.SelectObject( wxNullBitmap
) ;
734 wxBitmap::~wxBitmap()
738 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
740 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
744 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
745 if ( the_width
% (sizeof(unsigned char) * 8) ) {
746 linesize
+= sizeof(unsigned char);
748 unsigned char* linestart
= (unsigned char*) bits
;
749 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
750 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
752 for ( int x
= 0 ; x
< the_width
; ++x
)
756 int mask
= 1 << bit
;
757 if ( linestart
[index
] & mask
)
759 *destination
++ = 0xFF ;
766 *destination
++ = 0xFF ;
767 *destination
++ = 0xFF ;
768 *destination
++ = 0xFF ;
769 *destination
++ = 0xFF ;
777 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
781 wxBitmap::wxBitmap(int w
, int h
, int d
)
783 (void)Create(w
, h
, d
);
786 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
788 (void) Create(data
, type
, width
, height
, depth
);
791 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
793 LoadFile(filename
, type
);
796 wxBitmap::wxBitmap(const char **bits
)
798 (void) CreateFromXpm(bits
);
801 wxBitmap::wxBitmap(char **bits
)
803 (void) CreateFromXpm((const char **)bits
);
806 void* wxBitmap::GetRawAccess() const
808 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
809 return M_BITMAPDATA
->GetRawAccess() ;
812 void* wxBitmap::BeginRawAccess()
814 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
815 return M_BITMAPDATA
->BeginRawAccess() ;
818 void wxBitmap::EndRawAccess()
820 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
821 M_BITMAPDATA
->EndRawAccess() ;
824 bool wxBitmap::CreateFromXpm(const char **bits
)
827 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
828 wxXPMDecoder decoder
;
829 wxImage img
= decoder
.ReadData(bits
);
830 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
831 *this = wxBitmap(img
);
839 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
841 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
842 return M_BITMAPDATA
->CGImageCreate() ;
846 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
849 (rect
.x
>= 0) && (rect
.y
>= 0) &&
850 (rect
.x
+rect
.width
<= GetWidth()) &&
851 (rect
.y
+rect
.height
<= GetHeight()),
852 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
855 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
856 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
859 int sourcewidth
= GetWidth() ;
860 int destwidth
= rect
.width
;
861 int destheight
= rect
.height
;
863 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
864 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
865 int sourcelinesize
= sourcewidth
* 4 ;
866 int destlinesize
= destwidth
* 4 ;
867 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
868 unsigned char *dest
= destdata
;
869 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
871 memcpy( dest
, source
, destlinesize
) ;
876 if ( M_BITMAPDATA
->m_bitmapMask
)
878 wxMemoryBuffer maskbuf
;
879 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
880 size_t maskbufsize
= rowBytes
* destheight
;
881 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
883 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
884 int destlinesize
= rowBytes
;
885 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
886 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
887 unsigned char *dest
= destdata
;
889 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
891 memcpy( dest
, source
, destlinesize
) ;
893 maskbuf
.UngetWriteBuf( maskbufsize
) ;
894 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
896 else if ( HasAlpha() )
902 bool wxBitmap::Create(int w
, int h
, int d
)
907 d
= wxDisplayDepth() ;
909 m_refData
= new wxBitmapRefData( w
, h
, d
);
911 return M_BITMAPDATA
->Ok() ;
914 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
918 wxBitmapHandler
*handler
= FindHandler(type
);
922 m_refData
= new wxBitmapRefData
;
924 return handler
->LoadFile(this, filename
, type
, -1, -1);
929 wxImage
loadimage(filename
, type
);
930 if (loadimage
.Ok()) {
936 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
940 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
944 m_refData
= new wxBitmapRefData
;
946 wxBitmapHandler
*handler
= FindHandler(type
);
948 if ( handler
== NULL
) {
949 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
954 return handler
->Create(this, data
, type
, width
, height
, depth
);
959 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
961 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
963 // width and height of the device-dependent bitmap
964 int width
= image
.GetWidth();
965 int height
= image
.GetHeight();
967 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
971 bool hasAlpha
= false ;
973 if ( image
.HasMask() )
975 // takes precedence, don't mix with alpha info
979 hasAlpha
= image
.HasAlpha() ;
985 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
987 register unsigned char* data
= image
.GetData();
988 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
989 for (int y
= 0; y
< height
; y
++)
991 for (int x
= 0; x
< width
; x
++)
995 const unsigned char a
= *alpha
++;
997 #if wxMAC_USE_PREMULTIPLIED_ALPHA
998 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
999 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
1000 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
1002 *destination
++ = *data
++ ;
1003 *destination
++ = *data
++ ;
1004 *destination
++ = *data
++ ;
1009 *destination
++ = 0xFF ;
1010 *destination
++ = *data
++ ;
1011 *destination
++ = *data
++ ;
1012 *destination
++ = *data
++ ;
1017 if ( image
.HasMask() )
1019 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1023 wxImage
wxBitmap::ConvertToImage() const
1027 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1029 // create an wxImage object
1030 int width
= GetWidth();
1031 int height
= GetHeight();
1032 image
.Create( width
, height
);
1034 unsigned char *data
= image
.GetData();
1035 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1037 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1039 bool hasAlpha
= false ;
1040 bool hasMask
= false ;
1041 int maskBytesPerRow
= 0 ;
1042 unsigned char *alpha
= NULL
;
1043 unsigned char *mask
= NULL
;
1052 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1053 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1059 alpha
= image
.GetAlpha() ;
1063 // The following masking algorithm is the same as well in msw/gtk:
1064 // the colour used as transparent one in wxImage and the one it is
1065 // replaced with when it really occurs in the bitmap
1066 static const int MASK_RED
= 1;
1067 static const int MASK_GREEN
= 2;
1068 static const int MASK_BLUE
= 3;
1069 static const int MASK_BLUE_REPLACEMENT
= 2;
1071 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1073 unsigned char * maskp
= mask
;
1074 for (int xx
= 0; xx
< width
; xx
++)
1076 long color
= *((long*) source
) ;
1077 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1078 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1079 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1080 unsigned char b
= (color
&0x000000FF);
1083 if ( *maskp
++ == 0 )
1089 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1090 b
= MASK_BLUE_REPLACEMENT
;
1092 else if ( hasAlpha
)
1096 data
[index
+ 1] = g
;
1097 data
[index
+ 2] = b
;
1103 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1107 #endif //wxUSE_IMAGE
1109 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1110 const wxPalette
*palette
) const
1112 bool success
= false;
1113 wxBitmapHandler
*handler
= FindHandler(type
);
1117 success
= handler
->SaveFile(this, filename
, type
, palette
);
1122 wxImage image
= ConvertToImage();
1123 success
= image
.SaveFile(filename
, type
);
1125 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1132 bool wxBitmap::Ok() const
1134 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1137 int wxBitmap::GetHeight() const
1139 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1141 return M_BITMAPDATA
->GetHeight();
1144 int wxBitmap::GetWidth() const
1146 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1148 return M_BITMAPDATA
->GetWidth() ;
1151 int wxBitmap::GetDepth() const
1153 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1155 return M_BITMAPDATA
->GetDepth();
1158 #if WXWIN_COMPATIBILITY_2_4
1160 int wxBitmap::GetQuality() const
1167 wxMask
*wxBitmap::GetMask() const
1169 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1171 return M_BITMAPDATA
->m_bitmapMask
;
1174 bool wxBitmap::HasAlpha() const
1176 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1178 return M_BITMAPDATA
->HasAlpha() ;
1181 void wxBitmap::SetWidth(int w
)
1184 m_refData
= new wxBitmapRefData
;
1186 M_BITMAPDATA
->SetWidth(w
);
1189 void wxBitmap::SetHeight(int h
)
1192 m_refData
= new wxBitmapRefData
;
1194 M_BITMAPDATA
->SetHeight(h
);
1197 void wxBitmap::SetDepth(int d
)
1200 m_refData
= new wxBitmapRefData
;
1202 M_BITMAPDATA
->SetDepth(d
);
1205 #if WXWIN_COMPATIBILITY_2_4
1207 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1213 void wxBitmap::SetOk(bool isOk
)
1216 m_refData
= new wxBitmapRefData
;
1218 M_BITMAPDATA
->SetOk(isOk
);
1222 wxPalette
*wxBitmap::GetPalette() const
1224 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1226 return &M_BITMAPDATA
->m_bitmapPalette
;
1229 void wxBitmap::SetPalette(const wxPalette
& palette
)
1232 m_refData
= new wxBitmapRefData
;
1234 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1236 #endif // wxUSE_PALETTE
1238 void wxBitmap::SetMask(wxMask
*mask
)
1241 m_refData
= new wxBitmapRefData
;
1243 // Remove existing mask if there is one.
1244 delete M_BITMAPDATA
->m_bitmapMask
;
1246 M_BITMAPDATA
->m_bitmapMask
= mask
;
1249 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1251 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1254 // ----------------------------------------------------------------------------
1256 // ----------------------------------------------------------------------------
1263 // Construct a mask from a bitmap and a colour indicating
1264 // the transparent area
1265 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1268 Create(bitmap
, colour
);
1271 // Construct a mask from a mono bitmap (copies the bitmap).
1272 wxMask::wxMask(const wxBitmap
& bitmap
)
1278 // Construct a mask from a mono bitmap (copies the bitmap).
1279 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1282 Create(data
, width
, height
, bytesPerRow
);
1289 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1290 m_maskBitmap
= NULL
;
1296 m_width
= m_height
= m_bytesPerRow
= 0 ;
1297 m_maskBitmap
= NULL
;
1300 void *wxMask::GetRawAccess() const
1302 return m_memBuf
.GetData() ;
1305 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1306 // so no rainbox colors will be created by QD when blitting
1308 void wxMask::RealizeNative()
1312 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1313 m_maskBitmap
= NULL
;
1315 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1316 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1317 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1320 // Create a mask from a mono bitmap (copies the bitmap).
1321 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1326 m_bytesPerRow
= bytesPerRow
;
1327 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1332 // Create a mask from a mono bitmap (copies the bitmap).
1333 bool wxMask::Create(const wxBitmap
& bitmap
)
1335 m_width
= bitmap
.GetWidth() ;
1336 m_height
= bitmap
.GetHeight() ;
1337 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1338 size_t size
= m_bytesPerRow
* m_height
;
1339 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1340 memset( destdatabase
, 0 , size
) ;
1341 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1342 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1344 unsigned char *destdata
= destdatabase
;
1345 for( int x
= 0 ; x
< m_width
; ++x
)
1348 unsigned char r
= *srcdata
++ ;
1349 unsigned char g
= *srcdata
++ ;
1350 unsigned char b
= *srcdata
++ ;
1351 if ( ( r
+ g
+ b
) > 0x10 )
1352 *destdata
++ = 0x00 ;
1354 *destdata
++ = 0xFF ;
1357 m_memBuf
.UngetWriteBuf( size
) ;
1362 // Create a mask from a bitmap and a colour indicating
1363 // the transparent area
1364 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1366 m_width
= bitmap
.GetWidth() ;
1367 m_height
= bitmap
.GetHeight() ;
1368 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1369 size_t size
= m_bytesPerRow
* m_height
;
1371 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1372 memset( destdatabase
, 0 , size
) ;
1373 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1374 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1376 unsigned char *destdata
= destdatabase
;
1377 for( int x
= 0 ; x
< m_width
; ++x
)
1380 unsigned char r
= *srcdata
++ ;
1381 unsigned char g
= *srcdata
++ ;
1382 unsigned char b
= *srcdata
++ ;
1383 if ( colour
== wxColour( r
, g
, b
) )
1384 *destdata
++ = 0x00 ;
1386 *destdata
++ = 0xFF ;
1389 m_memBuf
.UngetWriteBuf( size
) ;
1394 WXHBITMAP
wxMask::GetHBITMAP() const
1396 return m_maskBitmap
;
1399 // ----------------------------------------------------------------------------
1401 // ----------------------------------------------------------------------------
1403 wxBitmapHandler::~wxBitmapHandler()
1407 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1412 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1413 int desiredWidth
, int desiredHeight
)
1418 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1423 // ----------------------------------------------------------------------------
1424 // Standard Handlers
1425 // ----------------------------------------------------------------------------
1427 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1429 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1431 inline wxPICTResourceHandler()
1433 SetName(wxT("Macintosh Pict resource"));
1434 SetExtension(wxEmptyString
);
1435 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1438 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1439 int desiredWidth
, int desiredHeight
);
1441 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1444 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1445 int desiredWidth
, int desiredHeight
)
1449 wxMacStringToPascal( name
, theName
) ;
1451 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1455 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1456 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1458 dc
.SelectObject( *bitmap
) ;
1460 dc
.SelectObject( wxNullBitmap
) ;
1463 #endif //wxUSE_METAFILE
1468 void wxBitmap::InitStandardHandlers()
1470 AddHandler(new wxPICTResourceHandler
) ;
1471 AddHandler(new wxICONResourceHandler
) ;
1474 // ----------------------------------------------------------------------------
1475 // raw bitmap access support
1476 // ----------------------------------------------------------------------------
1478 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1482 // no bitmap, no data (raw or otherwise)
1486 data
.m_width
= GetWidth() ;
1487 data
.m_height
= GetHeight() ;
1488 data
.m_stride
= GetWidth() * 4 ;
1489 return GetRawAccess() ;
1492 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1497 // TODO : if we have some information about the API we should check
1498 // this code looks strange...
1500 if ( M_BITMAPDATA
->HasAlpha() )
1502 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1504 int w
= data
.GetWidth(),
1505 h
= data
.GetHeight();
1507 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1508 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1509 wxAlphaPixelData::Iterator
pMask(dataMask
),
1511 for ( int y
= 0; y
< h
; y
++ )
1513 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1516 for ( int x
= 0; x
< w
; x
++ )
1518 const wxAlphaPixelData::Iterator::ChannelType
1521 pMask
.Red() = alpha
;
1522 pMask
.Green() = alpha
;
1523 pMask
.Blue() = alpha
;
1532 pMask
= rowStartMask
;
1533 pMask
.OffsetY(dataMask
, 1);
1536 SetMask(new wxMask(bmpMask
));
1540 void wxBitmap::UseAlpha()
1542 // remember that we are using alpha channel, we'll need to create a proper
1543 // mask in UngetRawData()
1544 M_BITMAPDATA
->UseAlpha( true );