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 PicHandle pict
= NULL
;
371 GWorldPtr wp
= NULL
;
372 GWorldPtr mask
= NULL
;
373 int height
= GetHeight() ;
374 int width
= GetWidth() ;
376 Rect rect
= { 0 , 0 , height
, width
} ;
378 GetGWorld( &origPort
, &origDev
) ;
380 wp
= GetHBITMAP( &mask
) ;
382 RgnHandle clipRgn
= NULL
;
386 GWorldPtr monoworld
;
388 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
390 LockPixels( GetGWorldPixMap( monoworld
) ) ;
391 LockPixels( GetGWorldPixMap( mask
) ) ;
392 SetGWorld( monoworld
, NULL
) ;
393 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
394 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
395 RGBForeColor( &black
) ;
396 RGBBackColor( &white
) ;
397 CopyBits(GetPortBitMapForCopyBits(mask
),
398 GetPortBitMapForCopyBits(monoworld
),
402 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
403 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
404 UnlockPixels( GetGWorldPixMap( mask
) ) ;
405 DisposeGWorld( monoworld
) ;
408 SetGWorld( wp
, NULL
) ;
410 GetPortBounds( wp
, &portRect
) ;
411 m_pictHandle
= OpenPicture(&portRect
);
415 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
416 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
417 RGBForeColor( &black
) ;
418 RGBBackColor( &white
) ;
423 LockPixels( GetGWorldPixMap( wp
) ) ;
424 CopyBits(GetPortBitMapForCopyBits(wp
),
425 GetPortBitMapForCopyBits(wp
),
429 UnlockPixels( GetGWorldPixMap( wp
) ) ;
432 SetGWorld( origPort
, origDev
) ;
434 DisposeRgn( clipRgn
) ;
436 return m_pictHandle
;
439 #if wxMAC_USE_CORE_GRAPHICS
440 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
442 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
443 wxASSERT( data
== membuf
->GetData() ) ;
447 CGImageRef
wxBitmapRefData::CGImageCreate() const
450 wxASSERT( m_rawAccessCount
>= 0 ) ;
452 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
454 size_t imageSize
= m_width
* m_height
* 4 ;
455 void * dataBuffer
= m_memBuf
.GetData() ;
458 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
459 wxMemoryBuffer
* membuf
= NULL
;
463 membuf
= new wxMemoryBuffer( imageSize
) ;
464 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
465 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
466 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
467 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
468 alphaInfo
= kCGImageAlphaFirst
;
469 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
471 unsigned char *sourcemask
= sourcemaskstart
;
472 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
474 *destalpha
= *sourcemask
;
478 else if ( m_hasAlpha
)
480 #if wxMAC_USE_PREMULTIPLIED_ALPHA
481 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
483 alphaInfo
= kCGImageAlphaFirst
;
485 membuf
= new wxMemoryBuffer( m_memBuf
) ;
489 membuf
= new wxMemoryBuffer( m_memBuf
) ;
491 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
492 CGDataProviderRef dataProvider
=
493 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
494 wxMacMemoryBufferReleaseProc
);
496 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
497 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
498 CGDataProviderRelease( dataProvider
);
502 image
= m_cgImageRef
;
503 CGImageRetain( image
) ;
505 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
507 // we keep it for later use
508 m_cgImageRef
= image
;
509 CGImageRetain( image
) ;
515 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
517 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
522 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
523 else if ( m_hasAlpha
)
525 #if !wxMAC_USE_CORE_GRAPHICS
526 if ( m_rawAccessCount
> 0 )
529 // this structure is not kept in synch when using CG, so if someone
530 // is really accessing the Graphports, we have to sync it
533 *mask
= m_hMaskBitmap
;
539 void wxBitmapRefData::UpdateAlphaMask() const
543 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
544 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
546 int h
= GetHeight() ;
549 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
551 unsigned char* destalpha
= destalphabase
;
552 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
554 // we must have 24 bit depth for non quartz smooth alpha
556 *destalpha
++ = 255 - *sourcemask
;
557 *destalpha
++ = 255 - *sourcemask
;
558 *destalpha
++ = 255 - *sourcemask
;
564 void wxBitmapRefData::Free()
566 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
568 #if wxMAC_USE_CORE_GRAPHICS
571 CGImageRelease( m_cgImageRef
) ;
572 m_cgImageRef
= NULL
;
577 ReleaseIconRef( m_iconRef
) ;
582 KillPicture( m_pictHandle
) ;
583 m_pictHandle
= NULL
;
587 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
592 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
593 m_hMaskBitmap
= NULL
;
603 wxBitmapRefData::~wxBitmapRefData()
608 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
610 int w
= icon
.GetWidth() ;
611 int h
= icon
.GetHeight() ;
614 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
616 bool created
= false ;
618 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
620 IconFamilyHandle iconFamily
= NULL
;
621 Handle imagehandle
= NewHandle(0) ;
622 Handle maskhandle
= NewHandle(0) ;
626 IconSelectorValue selector
= 0 ;
629 dataType
= kThumbnail32BitData
;
630 maskType
= kThumbnail8BitMask
;
631 selector
= kSelectorAllAvailableData
;
635 dataType
= kHuge32BitData
;
636 maskType
= kHuge8BitMask
;
637 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
641 dataType
= kLarge32BitData
;
642 maskType
= kLarge8BitMask
;
643 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
647 dataType
= kSmall32BitData
;
648 maskType
= kSmall8BitMask
;
649 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
653 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
655 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
656 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
657 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
658 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
660 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
662 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
663 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
665 unsigned char *source
= (unsigned char *) *imagehandle
;
666 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
668 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
669 for ( int y
= 0 ; y
< h
; ++y
)
671 for ( int x
= 0 ; x
< w
; ++x
)
673 *destination
++ = *sourcemask
++ ;
675 *destination
++ = *source
++ ;
676 *destination
++ = *source
++ ;
677 *destination
++ = *source
++ ;
681 DisposeHandle( imagehandle
) ;
682 DisposeHandle( maskhandle
) ;
684 DisposeHandle( (Handle
) iconFamily
) ;
691 dc
.SelectObject( *this ) ;
692 dc
.DrawIcon( icon
, 0 , 0 ) ;
693 dc
.SelectObject( wxNullBitmap
) ;
702 wxBitmap::~wxBitmap()
706 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
708 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
712 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
713 if ( the_width
% (sizeof(unsigned char) * 8) ) {
714 linesize
+= sizeof(unsigned char);
716 unsigned char* linestart
= (unsigned char*) bits
;
717 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
718 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
720 for ( int x
= 0 ; x
< the_width
; ++x
)
724 int mask
= 1 << bit
;
725 if ( linestart
[index
] & mask
)
727 *destination
++ = 0xFF ;
734 *destination
++ = 0xFF ;
735 *destination
++ = 0xFF ;
736 *destination
++ = 0xFF ;
737 *destination
++ = 0xFF ;
745 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
749 wxBitmap::wxBitmap(int w
, int h
, int d
)
751 (void)Create(w
, h
, d
);
754 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
756 (void) Create(data
, type
, width
, height
, depth
);
759 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
761 LoadFile(filename
, type
);
764 wxBitmap::wxBitmap(const char **bits
)
766 (void) CreateFromXpm(bits
);
769 wxBitmap::wxBitmap(char **bits
)
771 (void) CreateFromXpm((const char **)bits
);
774 void* wxBitmap::GetRawAccess() const
776 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
777 return M_BITMAPDATA
->GetRawAccess() ;
780 void* wxBitmap::BeginRawAccess()
782 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
783 return M_BITMAPDATA
->BeginRawAccess() ;
786 void wxBitmap::EndRawAccess()
788 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
789 M_BITMAPDATA
->EndRawAccess() ;
792 bool wxBitmap::CreateFromXpm(const char **bits
)
794 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
795 wxXPMDecoder decoder
;
796 wxImage img
= decoder
.ReadData(bits
);
797 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
798 *this = wxBitmap(img
);
802 #if wxMAC_USE_CORE_GRAPHICS
803 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
805 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
806 return M_BITMAPDATA
->CGImageCreate() ;
810 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
813 (rect
.x
>= 0) && (rect
.y
>= 0) &&
814 (rect
.x
+rect
.width
<= GetWidth()) &&
815 (rect
.y
+rect
.height
<= GetHeight()),
816 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
819 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
820 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
823 int sourcewidth
= GetWidth() ;
824 int destwidth
= rect
.width
;
825 int destheight
= rect
.height
;
827 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
828 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
829 int sourcelinesize
= sourcewidth
* 4 ;
830 int destlinesize
= destwidth
* 4 ;
831 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
832 unsigned char *dest
= destdata
;
833 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
835 memcpy( dest
, source
, destlinesize
) ;
840 if ( M_BITMAPDATA
->m_bitmapMask
)
842 wxMemoryBuffer maskbuf
;
843 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
844 size_t maskbufsize
= rowBytes
* destheight
;
845 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
847 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
848 int destlinesize
= rowBytes
;
849 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
850 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
851 unsigned char *dest
= destdata
;
853 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
855 memcpy( dest
, source
, destlinesize
) ;
857 maskbuf
.UngetWriteBuf( maskbufsize
) ;
858 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
860 else if ( HasAlpha() )
866 bool wxBitmap::Create(int w
, int h
, int d
)
871 d
= wxDisplayDepth() ;
873 m_refData
= new wxBitmapRefData( w
, h
, d
);
875 return M_BITMAPDATA
->Ok() ;
878 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
882 wxBitmapHandler
*handler
= FindHandler(type
);
886 m_refData
= new wxBitmapRefData
;
888 return handler
->LoadFile(this, filename
, type
, -1, -1);
892 wxImage
loadimage(filename
, type
);
893 if (loadimage
.Ok()) {
898 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
902 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
906 m_refData
= new wxBitmapRefData
;
908 wxBitmapHandler
*handler
= FindHandler(type
);
910 if ( handler
== NULL
) {
911 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
916 return handler
->Create(this, data
, type
, width
, height
, depth
);
919 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
921 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
923 // width and height of the device-dependent bitmap
924 int width
= image
.GetWidth();
925 int height
= image
.GetHeight();
927 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
931 bool hasAlpha
= false ;
933 if ( image
.HasMask() )
935 // takes precedence, don't mix with alpha info
939 hasAlpha
= image
.HasAlpha() ;
945 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
947 register unsigned char* data
= image
.GetData();
948 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
949 for (int y
= 0; y
< height
; y
++)
951 for (int x
= 0; x
< width
; x
++)
955 const unsigned char a
= *alpha
++;
957 #if wxMAC_USE_PREMULTIPLIED_ALPHA
958 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
959 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
960 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
962 *destination
++ = *data
++ ;
963 *destination
++ = *data
++ ;
964 *destination
++ = *data
++ ;
969 *destination
++ = 0xFF ;
970 *destination
++ = *data
++ ;
971 *destination
++ = *data
++ ;
972 *destination
++ = *data
++ ;
977 if ( image
.HasMask() )
979 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
983 wxImage
wxBitmap::ConvertToImage() const
987 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
989 // create an wxImage object
990 int width
= GetWidth();
991 int height
= GetHeight();
992 image
.Create( width
, height
);
994 unsigned char *data
= image
.GetData();
995 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
997 unsigned char* source
= (unsigned char*) GetRawAccess() ;
999 bool hasAlpha
= false ;
1000 bool hasMask
= false ;
1001 unsigned char *alpha
= NULL
;
1002 unsigned char *mask
= NULL
;
1011 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1017 alpha
= image
.GetAlpha() ;
1021 // The following masking algorithm is the same as well in msw/gtk:
1022 // the colour used as transparent one in wxImage and the one it is
1023 // replaced with when it really occurs in the bitmap
1024 static const int MASK_RED
= 1;
1025 static const int MASK_GREEN
= 2;
1026 static const int MASK_BLUE
= 3;
1027 static const int MASK_BLUE_REPLACEMENT
= 2;
1029 for (int yy
= 0; yy
< height
; yy
++)
1031 for (int xx
= 0; xx
< width
; xx
++)
1033 long color
= *((long*) source
) ;
1034 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1035 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1036 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1037 unsigned char b
= (color
&0x000000FF);
1042 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1043 b
= MASK_BLUE_REPLACEMENT
;
1052 else if ( hasAlpha
)
1056 data
[index
+ 1] = g
;
1057 data
[index
+ 2] = b
;
1063 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1068 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1069 const wxPalette
*palette
) const
1071 wxBitmapHandler
*handler
= FindHandler(type
);
1075 return handler
->SaveFile(this, filename
, type
, palette
);
1079 wxImage image
= ConvertToImage();
1081 return image
.SaveFile(filename
, type
);
1084 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1088 bool wxBitmap::Ok() const
1090 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1093 int wxBitmap::GetHeight() const
1095 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1097 return M_BITMAPDATA
->GetHeight();
1100 int wxBitmap::GetWidth() const
1102 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1104 return M_BITMAPDATA
->GetWidth() ;
1107 int wxBitmap::GetDepth() const
1109 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1111 return M_BITMAPDATA
->GetDepth();
1114 #if WXWIN_COMPATIBILITY_2_4
1116 int wxBitmap::GetQuality() const
1123 wxMask
*wxBitmap::GetMask() const
1125 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1127 return M_BITMAPDATA
->m_bitmapMask
;
1130 bool wxBitmap::HasAlpha() const
1132 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1134 return M_BITMAPDATA
->HasAlpha() ;
1137 void wxBitmap::SetWidth(int w
)
1140 m_refData
= new wxBitmapRefData
;
1142 M_BITMAPDATA
->SetWidth(w
);
1145 void wxBitmap::SetHeight(int h
)
1148 m_refData
= new wxBitmapRefData
;
1150 M_BITMAPDATA
->SetHeight(h
);
1153 void wxBitmap::SetDepth(int d
)
1156 m_refData
= new wxBitmapRefData
;
1158 M_BITMAPDATA
->SetDepth(d
);
1161 #if WXWIN_COMPATIBILITY_2_4
1163 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1169 void wxBitmap::SetOk(bool isOk
)
1172 m_refData
= new wxBitmapRefData
;
1174 M_BITMAPDATA
->SetOk(isOk
);
1178 wxPalette
*wxBitmap::GetPalette() const
1180 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1182 return &M_BITMAPDATA
->m_bitmapPalette
;
1185 void wxBitmap::SetPalette(const wxPalette
& palette
)
1188 m_refData
= new wxBitmapRefData
;
1190 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1192 #endif // wxUSE_PALETTE
1194 void wxBitmap::SetMask(wxMask
*mask
)
1197 m_refData
= new wxBitmapRefData
;
1199 // Remove existing mask if there is one.
1200 delete M_BITMAPDATA
->m_bitmapMask
;
1202 M_BITMAPDATA
->m_bitmapMask
= mask
;
1205 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1207 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1219 // Construct a mask from a bitmap and a colour indicating
1220 // the transparent area
1221 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1224 Create(bitmap
, colour
);
1227 // Construct a mask from a mono bitmap (copies the bitmap).
1228 wxMask::wxMask(const wxBitmap
& bitmap
)
1234 // Construct a mask from a mono bitmap (copies the bitmap).
1235 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1238 Create(data
, width
, height
, bytesPerRow
);
1245 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1246 m_maskBitmap
= NULL
;
1252 m_width
= m_height
= m_bytesPerRow
= 0 ;
1253 m_maskBitmap
= NULL
;
1256 void *wxMask::GetRawAccess() const
1258 return m_memBuf
.GetData() ;
1261 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1262 // so no rainbox colors will be created by QD when blitting
1264 void wxMask::RealizeNative()
1268 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1269 m_maskBitmap
= NULL
;
1271 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1272 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1273 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1276 // Create a mask from a mono bitmap (copies the bitmap).
1277 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1282 m_bytesPerRow
= bytesPerRow
;
1283 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1288 // Create a mask from a mono bitmap (copies the bitmap).
1289 bool wxMask::Create(const wxBitmap
& bitmap
)
1291 m_width
= bitmap
.GetWidth() ;
1292 m_height
= bitmap
.GetHeight() ;
1293 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1294 size_t size
= m_bytesPerRow
* m_height
;
1295 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1296 memset( destdatabase
, 0 , size
) ;
1297 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1298 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1300 unsigned char *destdata
= destdatabase
;
1301 for( int x
= 0 ; x
< m_width
; ++x
)
1304 unsigned char r
= *srcdata
++ ;
1305 unsigned char g
= *srcdata
++ ;
1306 unsigned char b
= *srcdata
++ ;
1307 if ( ( r
+ g
+ b
) > 0x10 )
1308 *destdata
++ = 0x00 ;
1310 *destdata
++ = 0xFF ;
1313 m_memBuf
.UngetWriteBuf( size
) ;
1318 // Create a mask from a bitmap and a colour indicating
1319 // the transparent area
1320 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1322 m_width
= bitmap
.GetWidth() ;
1323 m_height
= bitmap
.GetHeight() ;
1324 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1325 size_t size
= m_bytesPerRow
* m_height
;
1327 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1328 memset( destdatabase
, 0 , size
) ;
1329 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1330 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1332 unsigned char *destdata
= destdatabase
;
1333 for( int x
= 0 ; x
< m_width
; ++x
)
1336 unsigned char r
= *srcdata
++ ;
1337 unsigned char g
= *srcdata
++ ;
1338 unsigned char b
= *srcdata
++ ;
1339 if ( colour
== wxColour( r
, g
, b
) )
1340 *destdata
++ = 0x00 ;
1342 *destdata
++ = 0xFF ;
1345 m_memBuf
.UngetWriteBuf( size
) ;
1350 WXHBITMAP
wxMask::GetHBITMAP() const
1352 return m_maskBitmap
;
1355 // ----------------------------------------------------------------------------
1357 // ----------------------------------------------------------------------------
1359 wxBitmapHandler::~wxBitmapHandler()
1363 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1368 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1369 int desiredWidth
, int desiredHeight
)
1374 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1379 // ----------------------------------------------------------------------------
1380 // Standard Handlers
1381 // ----------------------------------------------------------------------------
1383 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1385 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1387 inline wxPICTResourceHandler()
1389 SetName(wxT("Macintosh Pict resource"));
1390 SetExtension(wxEmptyString
);
1391 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1394 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1395 int desiredWidth
, int desiredHeight
);
1397 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1400 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1401 int desiredWidth
, int desiredHeight
)
1405 wxMacStringToPascal( name
, theName
) ;
1407 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1411 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1412 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1414 dc
.SelectObject( *bitmap
) ;
1416 dc
.SelectObject( wxNullBitmap
) ;
1419 #endif //wxUSE_METAFILE
1424 void wxBitmap::InitStandardHandlers()
1426 AddHandler(new wxPICTResourceHandler
) ;
1427 AddHandler(new wxICONResourceHandler
) ;
1430 // ----------------------------------------------------------------------------
1431 // raw bitmap access support
1432 // ----------------------------------------------------------------------------
1434 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1438 // no bitmap, no data (raw or otherwise)
1442 data
.m_width
= GetWidth() ;
1443 data
.m_height
= GetHeight() ;
1444 data
.m_stride
= GetWidth() * 4 ;
1445 return GetRawAccess() ;
1448 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1453 // TODO : if we have some information about the API we should check
1454 // this code looks strange...
1456 if ( M_BITMAPDATA
->HasAlpha() )
1458 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1460 int w
= data
.GetWidth(),
1461 h
= data
.GetHeight();
1463 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1464 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1465 wxAlphaPixelData::Iterator
pMask(dataMask
),
1467 for ( int y
= 0; y
< h
; y
++ )
1469 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1472 for ( int x
= 0; x
< w
; x
++ )
1474 const wxAlphaPixelData::Iterator::ChannelType
1477 pMask
.Red() = alpha
;
1478 pMask
.Green() = alpha
;
1479 pMask
.Blue() = alpha
;
1488 pMask
= rowStartMask
;
1489 pMask
.OffsetY(dataMask
, 1);
1492 SetMask(new wxMask(bmpMask
));
1496 void wxBitmap::UseAlpha()
1498 // remember that we are using alpha channel, we'll need to create a proper
1499 // mask in UngetRawData()
1500 M_BITMAPDATA
->UseAlpha( true );