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
54 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
56 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
59 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
63 if ( bmap
->HasNativeSize() )
65 info
->contentType
= kControlContentIconRef
;
66 info
->u
.iconRef
= bmap
->GetIconRef() ;
70 info
->contentType
= kControlContentPictHandle
;
71 info
->u
.picture
= bmap
->GetPictHandle() ;
73 #if wxMAC_USE_CORE_GRAPHICS
75 // only on 10.4 more controls will accept a CGImage
77 info->contentType = kControlContentCGImageRef ;
78 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
84 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
86 if ( info
->contentType
== kControlContentIconRef
)
88 // as the bitmap is now the owner, no need to release here
90 else if ( info
->contentType
== kControlContentPictHandle
)
92 // owned by the bitma, no release here
94 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
95 else if ( info
->contentType
== kControlContentCGImageRef
)
97 CGImageRelease( info
->u
.imageRef
) ;
102 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
106 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
108 void wxBitmapRefData::Init()
114 m_bitmapMask
= NULL
;
115 #if wxMAC_USE_CORE_GRAPHICS
116 m_cgImageRef
= NULL
;
119 m_pictHandle
= NULL
;
121 m_hMaskBitmap
= NULL
;
122 m_maskBytesPerRow
= NULL
;
124 m_rawAccessCount
= 0 ;
128 wxBitmapRefData::wxBitmapRefData()
133 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
136 Create( w
, h
, d
) ;
139 bool wxBitmapRefData::Create( int w
, int h
, int d
)
145 m_bytesPerRow
= w
* 4 ;
146 size_t size
= m_bytesPerRow
* h
;
147 void* data
= m_memBuf
.GetWriteBuf(size
) ;
148 memset( data
, 0 , size
) ;
149 m_memBuf
.UngetWriteBuf(size
) ;
152 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
153 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
154 (char*) data
, m_bytesPerRow
) ) ;
155 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
156 m_ok
= ( m_hBitmap
!= NULL
) ;
161 void wxBitmapRefData::UseAlpha( bool use
)
163 if ( m_hasAlpha
== use
)
169 int width
= GetWidth() ;
170 int height
= GetHeight() ;
171 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
172 size_t size
= height
* m_maskBytesPerRow
;
173 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
174 memset( data
, 0 , size
) ;
175 wxASSERT( m_hMaskBitmap
== NULL
) ;
176 Rect rect
= { 0 , 0 , height
, width
} ;
177 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
178 (char*) data
, m_maskBytesPerRow
) ) ;
179 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
180 m_maskMemBuf
.UngetWriteBuf(size
) ;
181 #if !wxMAC_USE_CORE_GRAPHICS
187 DisposeGWorld( m_hMaskBitmap
) ;
188 m_hMaskBitmap
= NULL
;
189 m_maskBytesPerRow
= 0 ;
193 void *wxBitmapRefData::GetRawAccess() const
195 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
196 return m_memBuf
.GetData() ;
199 void *wxBitmapRefData::BeginRawAccess()
201 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
202 wxASSERT( m_rawAccessCount
== 0 ) ;
204 // we must destroy an existing cached image, as
205 // the bitmap data may change now
206 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
207 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
208 #if wxMAC_USE_CORE_GRAPHICS
211 CGImageRelease( m_cgImageRef
) ;
212 m_cgImageRef
= NULL
;
215 return m_memBuf
.GetData() ;
218 void wxBitmapRefData::EndRawAccess()
220 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
221 wxASSERT( m_rawAccessCount
== 1 ) ;
223 #if !wxMAC_USE_CORE_GRAPHICS
228 bool wxBitmapRefData::HasNativeSize()
231 int h
= GetHeight() ;
232 int sz
= wxMax( w
, h
) ;
234 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
239 IconRef
wxBitmapRefData::GetIconRef()
241 if ( m_iconRef
== NULL
)
243 // Create Icon Family Handle
245 IconFamilyHandle iconFamily
= NULL
;
247 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
248 (**iconFamily
).resourceType
= kIconFamilyType
;
249 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
252 int h
= GetHeight() ;
253 int sz
= wxMax( w
, h
) ;
255 OSType dataType
= 0 ;
256 OSType maskType
= 0 ;
260 dataType
= kThumbnail32BitData
;
261 maskType
= kThumbnail8BitMask
;
265 dataType
= kHuge32BitData
;
266 maskType
= kHuge8BitMask
;
270 dataType
= kLarge32BitData
;
271 maskType
= kLarge8BitMask
;
275 dataType
= kSmall32BitData
;
276 maskType
= kSmall8BitMask
;
281 // setup the header properly
284 Handle maskdata
= NULL
;
285 unsigned char * maskptr
= NULL
;
286 unsigned char * ptr
= NULL
;
291 data
= NewHandle( size
) ;
293 ptr
= (unsigned char*) *data
;
294 memset( ptr
, 0, size
) ;
297 maskdata
= NewHandle( masksize
) ;
299 maskptr
= (unsigned char*) *maskdata
;
300 memset( maskptr
, 0 , masksize
) ;
302 bool hasAlpha
= HasAlpha() ;
303 wxMask
*mask
= m_bitmapMask
;
304 unsigned char * source
= (unsigned char*) GetRawAccess() ;
305 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
306 for ( int y
= 0 ; y
< h
; ++y
)
308 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
309 unsigned char * maskdest
= maskptr
+ y
* sz
;
310 for ( int x
= 0 ; x
< w
; ++x
)
312 unsigned char a
= *source
++ ;
313 unsigned char r
= *source
++ ;
314 unsigned char g
= *source
++ ;
315 unsigned char b
= *source
++ ;
323 *maskdest
++ = *masksource
++ ;
331 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
332 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
334 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
335 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
337 HUnlock( maskdata
) ;
338 DisposeHandle( data
) ;
339 DisposeHandle( maskdata
) ;
343 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
344 (**iconFamily
).resourceType
= kIconFamilyType
;
345 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
346 PicHandle pic
= GetPictHandle() ;
347 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
350 // transform into IconRef
352 static int iconCounter
= 2 ;
353 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
354 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
355 // we have to retain a reference, as Unregister will decrement it
356 AcquireIconRef( m_iconRef
) ;
357 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
358 DisposeHandle( (Handle
) iconFamily
) ;
364 PicHandle
wxBitmapRefData::GetPictHandle()
366 if ( m_pictHandle
== NULL
)
368 CGrafPtr origPort
= NULL
;
369 GDHandle origDev
= NULL
;
370 GWorldPtr wp
= NULL
;
371 GWorldPtr mask
= NULL
;
372 int height
= GetHeight() ;
373 int width
= GetWidth() ;
375 Rect rect
= { 0 , 0 , height
, width
} ;
377 GetGWorld( &origPort
, &origDev
) ;
379 wp
= GetHBITMAP( &mask
) ;
381 RgnHandle clipRgn
= NULL
;
385 GWorldPtr monoworld
;
387 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
389 LockPixels( GetGWorldPixMap( monoworld
) ) ;
390 LockPixels( GetGWorldPixMap( mask
) ) ;
391 SetGWorld( monoworld
, NULL
) ;
392 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
393 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
394 RGBForeColor( &black
) ;
395 RGBBackColor( &white
) ;
396 CopyBits(GetPortBitMapForCopyBits(mask
),
397 GetPortBitMapForCopyBits(monoworld
),
401 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
402 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
403 UnlockPixels( GetGWorldPixMap( mask
) ) ;
404 DisposeGWorld( monoworld
) ;
407 SetGWorld( wp
, NULL
) ;
409 GetPortBounds( wp
, &portRect
) ;
410 m_pictHandle
= OpenPicture(&portRect
);
414 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
415 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
416 RGBForeColor( &black
) ;
417 RGBBackColor( &white
) ;
422 LockPixels( GetGWorldPixMap( wp
) ) ;
423 CopyBits(GetPortBitMapForCopyBits(wp
),
424 GetPortBitMapForCopyBits(wp
),
428 UnlockPixels( GetGWorldPixMap( wp
) ) ;
431 SetGWorld( origPort
, origDev
) ;
433 DisposeRgn( clipRgn
) ;
435 return m_pictHandle
;
438 #if wxMAC_USE_CORE_GRAPHICS
439 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
441 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
442 wxASSERT( data
== membuf
->GetData() ) ;
446 CGImageRef
wxBitmapRefData::CGImageCreate() const
449 wxASSERT( m_rawAccessCount
>= 0 ) ;
451 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
453 size_t imageSize
= m_width
* m_height
* 4 ;
454 void * dataBuffer
= m_memBuf
.GetData() ;
457 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
458 wxMemoryBuffer
* membuf
= NULL
;
462 membuf
= new wxMemoryBuffer( imageSize
) ;
463 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
464 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
465 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
466 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
467 alphaInfo
= kCGImageAlphaFirst
;
468 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
470 unsigned char *sourcemask
= sourcemaskstart
;
471 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
473 *destalpha
= *sourcemask
;
477 else if ( m_hasAlpha
)
479 #if wxMAC_USE_PREMULTIPLIED_ALPHA
480 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
482 alphaInfo
= kCGImageAlphaFirst
;
484 membuf
= new wxMemoryBuffer( m_memBuf
) ;
488 membuf
= new wxMemoryBuffer( m_memBuf
) ;
490 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
491 CGDataProviderRef dataProvider
=
492 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
493 wxMacMemoryBufferReleaseProc
);
495 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
496 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
497 CGDataProviderRelease( dataProvider
);
501 image
= m_cgImageRef
;
502 CGImageRetain( image
) ;
504 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
506 // we keep it for later use
507 m_cgImageRef
= image
;
508 CGImageRetain( image
) ;
514 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
516 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
521 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
522 else if ( m_hasAlpha
)
524 #if !wxMAC_USE_CORE_GRAPHICS
525 if ( m_rawAccessCount
> 0 )
528 // this structure is not kept in synch when using CG, so if someone
529 // is really accessing the Graphports, we have to sync it
532 *mask
= m_hMaskBitmap
;
538 void wxBitmapRefData::UpdateAlphaMask() const
542 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
543 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
545 int h
= GetHeight() ;
548 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
550 unsigned char* destalpha
= destalphabase
;
551 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
553 // we must have 24 bit depth for non quartz smooth alpha
555 *destalpha
++ = 255 - *sourcemask
;
556 *destalpha
++ = 255 - *sourcemask
;
557 *destalpha
++ = 255 - *sourcemask
;
563 void wxBitmapRefData::Free()
565 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
567 #if wxMAC_USE_CORE_GRAPHICS
570 CGImageRelease( m_cgImageRef
) ;
571 m_cgImageRef
= NULL
;
576 ReleaseIconRef( m_iconRef
) ;
581 KillPicture( m_pictHandle
) ;
582 m_pictHandle
= NULL
;
586 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
591 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
592 m_hMaskBitmap
= NULL
;
602 wxBitmapRefData::~wxBitmapRefData()
607 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
609 int w
= icon
.GetWidth() ;
610 int h
= icon
.GetHeight() ;
613 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
615 bool created
= false ;
617 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
619 IconFamilyHandle iconFamily
= NULL
;
620 Handle imagehandle
= NewHandle(0) ;
621 Handle maskhandle
= NewHandle(0) ;
625 IconSelectorValue selector
= 0 ;
628 dataType
= kThumbnail32BitData
;
629 maskType
= kThumbnail8BitMask
;
630 selector
= kSelectorAllAvailableData
;
634 dataType
= kHuge32BitData
;
635 maskType
= kHuge8BitMask
;
636 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
640 dataType
= kLarge32BitData
;
641 maskType
= kLarge8BitMask
;
642 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
646 dataType
= kSmall32BitData
;
647 maskType
= kSmall8BitMask
;
648 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
652 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
654 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
655 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
656 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
657 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
659 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
661 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
662 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
664 unsigned char *source
= (unsigned char *) *imagehandle
;
665 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
667 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
668 for ( int y
= 0 ; y
< h
; ++y
)
670 for ( int x
= 0 ; x
< w
; ++x
)
672 *destination
++ = *sourcemask
++ ;
674 *destination
++ = *source
++ ;
675 *destination
++ = *source
++ ;
676 *destination
++ = *source
++ ;
680 DisposeHandle( imagehandle
) ;
681 DisposeHandle( maskhandle
) ;
683 DisposeHandle( (Handle
) iconFamily
) ;
690 dc
.SelectObject( *this ) ;
691 dc
.DrawIcon( icon
, 0 , 0 ) ;
692 dc
.SelectObject( wxNullBitmap
) ;
701 wxBitmap::~wxBitmap()
705 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
707 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
711 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
712 if ( the_width
% (sizeof(unsigned char) * 8) ) {
713 linesize
+= sizeof(unsigned char);
715 unsigned char* linestart
= (unsigned char*) bits
;
716 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
717 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
719 for ( int x
= 0 ; x
< the_width
; ++x
)
723 int mask
= 1 << bit
;
724 if ( linestart
[index
] & mask
)
726 *destination
++ = 0xFF ;
733 *destination
++ = 0xFF ;
734 *destination
++ = 0xFF ;
735 *destination
++ = 0xFF ;
736 *destination
++ = 0xFF ;
744 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
748 wxBitmap::wxBitmap(int w
, int h
, int d
)
750 (void)Create(w
, h
, d
);
753 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
755 (void) Create(data
, type
, width
, height
, depth
);
758 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
760 LoadFile(filename
, type
);
763 wxBitmap::wxBitmap(const char **bits
)
765 (void) CreateFromXpm(bits
);
768 wxBitmap::wxBitmap(char **bits
)
770 (void) CreateFromXpm((const char **)bits
);
773 void* wxBitmap::GetRawAccess() const
775 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
776 return M_BITMAPDATA
->GetRawAccess() ;
779 void* wxBitmap::BeginRawAccess()
781 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
782 return M_BITMAPDATA
->BeginRawAccess() ;
785 void wxBitmap::EndRawAccess()
787 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
788 M_BITMAPDATA
->EndRawAccess() ;
791 bool wxBitmap::CreateFromXpm(const char **bits
)
793 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
794 wxXPMDecoder decoder
;
795 wxImage img
= decoder
.ReadData(bits
);
796 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
797 *this = wxBitmap(img
);
801 #if wxMAC_USE_CORE_GRAPHICS
802 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
804 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
805 return M_BITMAPDATA
->CGImageCreate() ;
809 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
812 (rect
.x
>= 0) && (rect
.y
>= 0) &&
813 (rect
.x
+rect
.width
<= GetWidth()) &&
814 (rect
.y
+rect
.height
<= GetHeight()),
815 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
818 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
819 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
822 int sourcewidth
= GetWidth() ;
823 int destwidth
= rect
.width
;
824 int destheight
= rect
.height
;
826 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
827 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
828 int sourcelinesize
= sourcewidth
* 4 ;
829 int destlinesize
= destwidth
* 4 ;
830 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
831 unsigned char *dest
= destdata
;
832 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
834 memcpy( dest
, source
, destlinesize
) ;
839 if ( M_BITMAPDATA
->m_bitmapMask
)
841 wxMemoryBuffer maskbuf
;
842 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
843 size_t maskbufsize
= rowBytes
* destheight
;
844 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
846 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
847 int destlinesize
= rowBytes
;
848 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
849 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
850 unsigned char *dest
= destdata
;
852 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
854 memcpy( dest
, source
, destlinesize
) ;
856 maskbuf
.UngetWriteBuf( maskbufsize
) ;
857 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
859 else if ( HasAlpha() )
865 bool wxBitmap::Create(int w
, int h
, int d
)
870 d
= wxDisplayDepth() ;
872 m_refData
= new wxBitmapRefData( w
, h
, d
);
874 return M_BITMAPDATA
->Ok() ;
877 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
881 wxBitmapHandler
*handler
= FindHandler(type
);
885 m_refData
= new wxBitmapRefData
;
887 return handler
->LoadFile(this, filename
, type
, -1, -1);
891 wxImage
loadimage(filename
, type
);
892 if (loadimage
.Ok()) {
897 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
901 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
905 m_refData
= new wxBitmapRefData
;
907 wxBitmapHandler
*handler
= FindHandler(type
);
909 if ( handler
== NULL
) {
910 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
915 return handler
->Create(this, data
, type
, width
, height
, depth
);
918 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
920 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
922 // width and height of the device-dependent bitmap
923 int width
= image
.GetWidth();
924 int height
= image
.GetHeight();
926 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
930 bool hasAlpha
= false ;
932 if ( image
.HasMask() )
934 // takes precedence, don't mix with alpha info
938 hasAlpha
= image
.HasAlpha() ;
944 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
946 register unsigned char* data
= image
.GetData();
947 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
948 for (int y
= 0; y
< height
; y
++)
950 for (int x
= 0; x
< width
; x
++)
954 const unsigned char a
= *alpha
++;
956 #if wxMAC_USE_PREMULTIPLIED_ALPHA
957 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
958 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
959 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
961 *destination
++ = *data
++ ;
962 *destination
++ = *data
++ ;
963 *destination
++ = *data
++ ;
968 *destination
++ = 0xFF ;
969 *destination
++ = *data
++ ;
970 *destination
++ = *data
++ ;
971 *destination
++ = *data
++ ;
976 if ( image
.HasMask() )
978 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
982 wxImage
wxBitmap::ConvertToImage() const
986 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
988 // create an wxImage object
989 int width
= GetWidth();
990 int height
= GetHeight();
991 image
.Create( width
, height
);
993 unsigned char *data
= image
.GetData();
994 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
996 unsigned char* source
= (unsigned char*) GetRawAccess() ;
998 bool hasAlpha
= false ;
999 bool hasMask
= false ;
1000 unsigned char *alpha
= NULL
;
1001 unsigned char *mask
= NULL
;
1010 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1016 alpha
= image
.GetAlpha() ;
1020 // The following masking algorithm is the same as well in msw/gtk:
1021 // the colour used as transparent one in wxImage and the one it is
1022 // replaced with when it really occurs in the bitmap
1023 static const int MASK_RED
= 1;
1024 static const int MASK_GREEN
= 2;
1025 static const int MASK_BLUE
= 3;
1026 static const int MASK_BLUE_REPLACEMENT
= 2;
1028 for (int yy
= 0; yy
< height
; yy
++)
1030 for (int xx
= 0; xx
< width
; xx
++)
1032 long color
= *((long*) source
) ;
1033 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1034 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1035 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1036 unsigned char b
= (color
&0x000000FF);
1041 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1042 b
= MASK_BLUE_REPLACEMENT
;
1051 else if ( hasAlpha
)
1055 data
[index
+ 1] = g
;
1056 data
[index
+ 2] = b
;
1062 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1067 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1068 const wxPalette
*palette
) const
1070 wxBitmapHandler
*handler
= FindHandler(type
);
1074 return handler
->SaveFile(this, filename
, type
, palette
);
1078 wxImage image
= ConvertToImage();
1080 return image
.SaveFile(filename
, type
);
1083 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1087 bool wxBitmap::Ok() const
1089 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1092 int wxBitmap::GetHeight() const
1094 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1096 return M_BITMAPDATA
->GetHeight();
1099 int wxBitmap::GetWidth() const
1101 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1103 return M_BITMAPDATA
->GetWidth() ;
1106 int wxBitmap::GetDepth() const
1108 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1110 return M_BITMAPDATA
->GetDepth();
1113 #if WXWIN_COMPATIBILITY_2_4
1115 int wxBitmap::GetQuality() const
1122 wxMask
*wxBitmap::GetMask() const
1124 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1126 return M_BITMAPDATA
->m_bitmapMask
;
1129 bool wxBitmap::HasAlpha() const
1131 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1133 return M_BITMAPDATA
->HasAlpha() ;
1136 void wxBitmap::SetWidth(int w
)
1139 m_refData
= new wxBitmapRefData
;
1141 M_BITMAPDATA
->SetWidth(w
);
1144 void wxBitmap::SetHeight(int h
)
1147 m_refData
= new wxBitmapRefData
;
1149 M_BITMAPDATA
->SetHeight(h
);
1152 void wxBitmap::SetDepth(int d
)
1155 m_refData
= new wxBitmapRefData
;
1157 M_BITMAPDATA
->SetDepth(d
);
1160 #if WXWIN_COMPATIBILITY_2_4
1162 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1168 void wxBitmap::SetOk(bool isOk
)
1171 m_refData
= new wxBitmapRefData
;
1173 M_BITMAPDATA
->SetOk(isOk
);
1177 wxPalette
*wxBitmap::GetPalette() const
1179 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1181 return &M_BITMAPDATA
->m_bitmapPalette
;
1184 void wxBitmap::SetPalette(const wxPalette
& palette
)
1187 m_refData
= new wxBitmapRefData
;
1189 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1191 #endif // wxUSE_PALETTE
1193 void wxBitmap::SetMask(wxMask
*mask
)
1196 m_refData
= new wxBitmapRefData
;
1198 // Remove existing mask if there is one.
1199 delete M_BITMAPDATA
->m_bitmapMask
;
1201 M_BITMAPDATA
->m_bitmapMask
= mask
;
1204 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1206 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1209 // ----------------------------------------------------------------------------
1211 // ----------------------------------------------------------------------------
1218 // Construct a mask from a bitmap and a colour indicating
1219 // the transparent area
1220 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1223 Create(bitmap
, colour
);
1226 // Construct a mask from a mono bitmap (copies the bitmap).
1227 wxMask::wxMask(const wxBitmap
& bitmap
)
1233 // Construct a mask from a mono bitmap (copies the bitmap).
1234 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1237 Create(data
, width
, height
, bytesPerRow
);
1244 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1245 m_maskBitmap
= NULL
;
1251 m_width
= m_height
= m_bytesPerRow
= 0 ;
1252 m_maskBitmap
= NULL
;
1255 void *wxMask::GetRawAccess() const
1257 return m_memBuf
.GetData() ;
1260 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1261 // so no rainbox colors will be created by QD when blitting
1263 void wxMask::RealizeNative()
1267 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1268 m_maskBitmap
= NULL
;
1270 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1271 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1272 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1275 // Create a mask from a mono bitmap (copies the bitmap).
1276 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1281 m_bytesPerRow
= bytesPerRow
;
1282 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1287 // Create a mask from a mono bitmap (copies the bitmap).
1288 bool wxMask::Create(const wxBitmap
& bitmap
)
1290 m_width
= bitmap
.GetWidth() ;
1291 m_height
= bitmap
.GetHeight() ;
1292 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1293 size_t size
= m_bytesPerRow
* m_height
;
1294 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1295 memset( destdatabase
, 0 , size
) ;
1296 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1297 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1299 unsigned char *destdata
= destdatabase
;
1300 for( int x
= 0 ; x
< m_width
; ++x
)
1303 unsigned char r
= *srcdata
++ ;
1304 unsigned char g
= *srcdata
++ ;
1305 unsigned char b
= *srcdata
++ ;
1306 if ( ( r
+ g
+ b
) > 0x10 )
1307 *destdata
++ = 0x00 ;
1309 *destdata
++ = 0xFF ;
1312 m_memBuf
.UngetWriteBuf( size
) ;
1317 // Create a mask from a bitmap and a colour indicating
1318 // the transparent area
1319 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1321 m_width
= bitmap
.GetWidth() ;
1322 m_height
= bitmap
.GetHeight() ;
1323 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1324 size_t size
= m_bytesPerRow
* m_height
;
1326 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1327 memset( destdatabase
, 0 , size
) ;
1328 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1329 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1331 unsigned char *destdata
= destdatabase
;
1332 for( int x
= 0 ; x
< m_width
; ++x
)
1335 unsigned char r
= *srcdata
++ ;
1336 unsigned char g
= *srcdata
++ ;
1337 unsigned char b
= *srcdata
++ ;
1338 if ( colour
== wxColour( r
, g
, b
) )
1339 *destdata
++ = 0x00 ;
1341 *destdata
++ = 0xFF ;
1344 m_memBuf
.UngetWriteBuf( size
) ;
1349 WXHBITMAP
wxMask::GetHBITMAP() const
1351 return m_maskBitmap
;
1354 // ----------------------------------------------------------------------------
1356 // ----------------------------------------------------------------------------
1358 wxBitmapHandler::~wxBitmapHandler()
1362 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1367 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1368 int desiredWidth
, int desiredHeight
)
1373 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1378 // ----------------------------------------------------------------------------
1379 // Standard Handlers
1380 // ----------------------------------------------------------------------------
1382 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1384 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1386 inline wxPICTResourceHandler()
1388 SetName(wxT("Macintosh Pict resource"));
1389 SetExtension(wxEmptyString
);
1390 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1393 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1394 int desiredWidth
, int desiredHeight
);
1396 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1399 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1400 int desiredWidth
, int desiredHeight
)
1404 wxMacStringToPascal( name
, theName
) ;
1406 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1410 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1411 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1413 dc
.SelectObject( *bitmap
) ;
1415 dc
.SelectObject( wxNullBitmap
) ;
1418 #endif //wxUSE_METAFILE
1423 void wxBitmap::InitStandardHandlers()
1425 AddHandler(new wxPICTResourceHandler
) ;
1426 AddHandler(new wxICONResourceHandler
) ;
1429 // ----------------------------------------------------------------------------
1430 // raw bitmap access support
1431 // ----------------------------------------------------------------------------
1433 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1437 // no bitmap, no data (raw or otherwise)
1441 data
.m_width
= GetWidth() ;
1442 data
.m_height
= GetHeight() ;
1443 data
.m_stride
= GetWidth() * 4 ;
1444 return GetRawAccess() ;
1447 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1452 // TODO : if we have some information about the API we should check
1453 // this code looks strange...
1455 if ( M_BITMAPDATA
->HasAlpha() )
1457 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1459 int w
= data
.GetWidth(),
1460 h
= data
.GetHeight();
1462 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1463 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1464 wxAlphaPixelData::Iterator
pMask(dataMask
),
1466 for ( int y
= 0; y
< h
; y
++ )
1468 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1471 for ( int x
= 0; x
< w
; x
++ )
1473 const wxAlphaPixelData::Iterator::ChannelType
1476 pMask
.Red() = alpha
;
1477 pMask
.Green() = alpha
;
1478 pMask
.Blue() = alpha
;
1487 pMask
= rowStartMask
;
1488 pMask
.OffsetY(dataMask
, 1);
1491 SetMask(new wxMask(bmpMask
));
1495 void wxBitmap::UseAlpha()
1497 // remember that we are using alpha channel, we'll need to create a proper
1498 // mask in UngetRawData()
1499 M_BITMAPDATA
->UseAlpha( true );