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
== kControlContentPictHandle
)
113 // owned by the bitmap, no release here
115 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
116 else if ( info
->contentType
== kControlContentCGImageRef
)
118 CGImageRelease( info
->u
.imageRef
) ;
123 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
127 #endif //wxUSE_BMPBUTTON
129 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
131 void wxBitmapRefData::Init()
137 m_bitmapMask
= NULL
;
138 #if wxMAC_USE_CORE_GRAPHICS
139 m_cgImageRef
= NULL
;
142 m_pictHandle
= NULL
;
144 m_hMaskBitmap
= NULL
;
145 m_maskBytesPerRow
= NULL
;
147 m_rawAccessCount
= 0 ;
151 wxBitmapRefData::wxBitmapRefData()
156 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
159 Create( w
, h
, d
) ;
162 bool wxBitmapRefData::Create( int w
, int h
, int d
)
168 m_bytesPerRow
= w
* 4 ;
169 size_t size
= m_bytesPerRow
* h
;
170 void* data
= m_memBuf
.GetWriteBuf(size
) ;
171 memset( data
, 0 , size
) ;
172 m_memBuf
.UngetWriteBuf(size
) ;
175 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
177 (char*) data
, m_bytesPerRow
) ) ;
178 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
179 m_ok
= ( m_hBitmap
!= NULL
) ;
184 void wxBitmapRefData::UseAlpha( bool use
)
186 if ( m_hasAlpha
== use
)
192 int width
= GetWidth() ;
193 int height
= GetHeight() ;
194 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
195 size_t size
= height
* m_maskBytesPerRow
;
196 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
197 memset( data
, 0 , size
) ;
198 wxASSERT( m_hMaskBitmap
== NULL
) ;
199 Rect rect
= { 0 , 0 , height
, width
} ;
200 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
201 (char*) data
, m_maskBytesPerRow
) ) ;
202 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
203 m_maskMemBuf
.UngetWriteBuf(size
) ;
204 #if !wxMAC_USE_CORE_GRAPHICS
210 DisposeGWorld( m_hMaskBitmap
) ;
211 m_hMaskBitmap
= NULL
;
212 m_maskBytesPerRow
= 0 ;
216 void *wxBitmapRefData::GetRawAccess() const
218 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
219 return m_memBuf
.GetData() ;
222 void *wxBitmapRefData::BeginRawAccess()
224 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
225 wxASSERT( m_rawAccessCount
== 0 ) ;
227 // we must destroy an existing cached image, as
228 // the bitmap data may change now
229 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
230 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
231 #if wxMAC_USE_CORE_GRAPHICS
234 CGImageRelease( m_cgImageRef
) ;
235 m_cgImageRef
= NULL
;
238 return m_memBuf
.GetData() ;
241 void wxBitmapRefData::EndRawAccess()
243 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
244 wxASSERT( m_rawAccessCount
== 1 ) ;
246 #if !wxMAC_USE_CORE_GRAPHICS
251 bool wxBitmapRefData::HasNativeSize()
254 int h
= GetHeight() ;
255 int sz
= wxMax( w
, h
) ;
257 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
262 IconRef
wxBitmapRefData::GetIconRef()
264 if ( m_iconRef
== NULL
)
266 // Create Icon Family Handle
268 IconFamilyHandle iconFamily
= NULL
;
270 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
271 (**iconFamily
).resourceType
= kIconFamilyType
;
272 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
275 int h
= GetHeight() ;
276 int sz
= wxMax( w
, h
) ;
278 OSType dataType
= 0 ;
279 OSType maskType
= 0 ;
283 dataType
= kThumbnail32BitData
;
284 maskType
= kThumbnail8BitMask
;
288 dataType
= kHuge32BitData
;
289 maskType
= kHuge8BitMask
;
293 dataType
= kLarge32BitData
;
294 maskType
= kLarge8BitMask
;
298 dataType
= kSmall32BitData
;
299 maskType
= kSmall8BitMask
;
304 // setup the header properly
307 Handle maskdata
= NULL
;
308 unsigned char * maskptr
= NULL
;
309 unsigned char * ptr
= NULL
;
314 data
= NewHandle( size
) ;
316 ptr
= (unsigned char*) *data
;
317 memset( ptr
, 0, size
) ;
320 maskdata
= NewHandle( masksize
) ;
322 maskptr
= (unsigned char*) *maskdata
;
323 memset( maskptr
, 0 , masksize
) ;
325 bool hasAlpha
= HasAlpha() ;
326 wxMask
*mask
= m_bitmapMask
;
327 unsigned char * source
= (unsigned char*) GetRawAccess() ;
328 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
329 for ( int y
= 0 ; y
< h
; ++y
)
331 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
332 unsigned char * maskdest
= maskptr
+ y
* sz
;
333 for ( int x
= 0 ; x
< w
; ++x
)
335 unsigned char a
= *source
++ ;
336 unsigned char r
= *source
++ ;
337 unsigned char g
= *source
++ ;
338 unsigned char b
= *source
++ ;
346 *maskdest
++ = *masksource
++ ;
354 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
355 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
357 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
358 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
360 HUnlock( maskdata
) ;
361 DisposeHandle( data
) ;
362 DisposeHandle( maskdata
) ;
366 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
367 (**iconFamily
).resourceType
= kIconFamilyType
;
368 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
369 PicHandle pic
= GetPictHandle() ;
370 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
373 // transform into IconRef
375 static int iconCounter
= 2 ;
379 RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
380 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
381 // we have to retain a reference, as Unregister will decrement it
382 AcquireIconRef( m_iconRef
) ;
383 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
384 DisposeHandle( (Handle
) iconFamily
) ;
390 PicHandle
wxBitmapRefData::GetPictHandle()
392 if ( m_pictHandle
== NULL
)
394 CGrafPtr origPort
= NULL
;
395 GDHandle origDev
= NULL
;
396 GWorldPtr wp
= NULL
;
397 GWorldPtr mask
= NULL
;
398 int height
= GetHeight() ;
399 int width
= GetWidth() ;
401 Rect rect
= { 0 , 0 , height
, width
} ;
403 GetGWorld( &origPort
, &origDev
) ;
405 wp
= GetHBITMAP( &mask
) ;
407 RgnHandle clipRgn
= NULL
;
411 GWorldPtr monoworld
;
413 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
415 LockPixels( GetGWorldPixMap( monoworld
) ) ;
416 LockPixels( GetGWorldPixMap( mask
) ) ;
417 SetGWorld( monoworld
, NULL
) ;
418 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
419 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
420 RGBForeColor( &black
) ;
421 RGBBackColor( &white
) ;
422 CopyBits(GetPortBitMapForCopyBits(mask
),
423 GetPortBitMapForCopyBits(monoworld
),
427 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
428 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
429 UnlockPixels( GetGWorldPixMap( mask
) ) ;
430 DisposeGWorld( monoworld
) ;
433 SetGWorld( wp
, NULL
) ;
435 GetPortBounds( wp
, &portRect
) ;
436 m_pictHandle
= OpenPicture(&portRect
);
440 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
441 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
442 RGBForeColor( &black
) ;
443 RGBBackColor( &white
) ;
448 LockPixels( GetGWorldPixMap( wp
) ) ;
449 CopyBits(GetPortBitMapForCopyBits(wp
),
450 GetPortBitMapForCopyBits(wp
),
454 UnlockPixels( GetGWorldPixMap( wp
) ) ;
457 SetGWorld( origPort
, origDev
) ;
459 DisposeRgn( clipRgn
) ;
461 return m_pictHandle
;
465 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
467 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
468 wxASSERT( data
== membuf
->GetData() ) ;
472 CGImageRef
wxBitmapRefData::CGImageCreate() const
475 wxASSERT( m_rawAccessCount
>= 0 ) ;
477 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
479 size_t imageSize
= m_width
* m_height
* 4 ;
480 void * dataBuffer
= m_memBuf
.GetData() ;
483 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
484 wxMemoryBuffer
* membuf
= NULL
;
488 membuf
= new wxMemoryBuffer( imageSize
) ;
489 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
490 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
491 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
492 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
493 alphaInfo
= kCGImageAlphaFirst
;
494 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
496 unsigned char *sourcemask
= sourcemaskstart
;
497 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
499 *destalpha
= *sourcemask
;
503 else if ( m_hasAlpha
)
505 #if wxMAC_USE_PREMULTIPLIED_ALPHA
506 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
508 alphaInfo
= kCGImageAlphaFirst
;
510 membuf
= new wxMemoryBuffer( m_memBuf
) ;
514 membuf
= new wxMemoryBuffer( m_memBuf
) ;
516 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
517 CGDataProviderRef dataProvider
=
518 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
519 wxMacMemoryBufferReleaseProc
);
521 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
522 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
523 CGDataProviderRelease( dataProvider
);
527 image
= m_cgImageRef
;
528 CGImageRetain( image
) ;
530 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
532 // we keep it for later use
533 m_cgImageRef
= image
;
534 CGImageRetain( image
) ;
540 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
542 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
547 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
548 else if ( m_hasAlpha
)
550 #if !wxMAC_USE_CORE_GRAPHICS
551 if ( m_rawAccessCount
> 0 )
554 // this structure is not kept in synch when using CG, so if someone
555 // is really accessing the Graphports, we have to sync it
558 *mask
= m_hMaskBitmap
;
564 void wxBitmapRefData::UpdateAlphaMask() const
568 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
569 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
571 int h
= GetHeight() ;
574 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
576 unsigned char* destalpha
= destalphabase
;
577 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
579 // we must have 24 bit depth for non quartz smooth alpha
581 *destalpha
++ = 255 - *sourcemask
;
582 *destalpha
++ = 255 - *sourcemask
;
583 *destalpha
++ = 255 - *sourcemask
;
589 void wxBitmapRefData::Free()
591 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
593 #if wxMAC_USE_CORE_GRAPHICS
596 CGImageRelease( m_cgImageRef
) ;
597 m_cgImageRef
= NULL
;
602 ReleaseIconRef( m_iconRef
) ;
607 KillPicture( m_pictHandle
) ;
608 m_pictHandle
= NULL
;
612 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
617 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
618 m_hMaskBitmap
= NULL
;
628 wxBitmapRefData::~wxBitmapRefData()
633 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
635 int w
= icon
.GetWidth() ;
636 int h
= icon
.GetHeight() ;
639 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
641 bool created
= false ;
643 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
645 IconFamilyHandle iconFamily
= NULL
;
646 Handle imagehandle
= NewHandle(0) ;
647 Handle maskhandle
= NewHandle(0) ;
651 IconSelectorValue selector
= 0 ;
654 dataType
= kThumbnail32BitData
;
655 maskType
= kThumbnail8BitMask
;
656 selector
= kSelectorAllAvailableData
;
660 dataType
= kHuge32BitData
;
661 maskType
= kHuge8BitMask
;
662 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
666 dataType
= kLarge32BitData
;
667 maskType
= kLarge8BitMask
;
668 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
672 dataType
= kSmall32BitData
;
673 maskType
= kSmall8BitMask
;
674 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
678 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
680 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
681 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
682 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
683 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
685 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
687 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
688 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
690 unsigned char *source
= (unsigned char *) *imagehandle
;
691 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
693 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
694 for ( int y
= 0 ; y
< h
; ++y
)
696 for ( int x
= 0 ; x
< w
; ++x
)
698 *destination
++ = *sourcemask
++ ;
700 *destination
++ = *source
++ ;
701 *destination
++ = *source
++ ;
702 *destination
++ = *source
++ ;
706 DisposeHandle( imagehandle
) ;
707 DisposeHandle( maskhandle
) ;
709 DisposeHandle( (Handle
) iconFamily
) ;
716 dc
.SelectObject( *this ) ;
717 dc
.DrawIcon( icon
, 0 , 0 ) ;
718 dc
.SelectObject( wxNullBitmap
) ;
727 wxBitmap::~wxBitmap()
731 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
733 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
737 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
738 if ( the_width
% (sizeof(unsigned char) * 8) ) {
739 linesize
+= sizeof(unsigned char);
741 unsigned char* linestart
= (unsigned char*) bits
;
742 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
743 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
745 for ( int x
= 0 ; x
< the_width
; ++x
)
749 int mask
= 1 << bit
;
750 if ( linestart
[index
] & mask
)
752 *destination
++ = 0xFF ;
759 *destination
++ = 0xFF ;
760 *destination
++ = 0xFF ;
761 *destination
++ = 0xFF ;
762 *destination
++ = 0xFF ;
770 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
774 wxBitmap::wxBitmap(int w
, int h
, int d
)
776 (void)Create(w
, h
, d
);
779 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
781 (void) Create(data
, type
, width
, height
, depth
);
784 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
786 LoadFile(filename
, type
);
789 wxBitmap::wxBitmap(const char **bits
)
791 (void) CreateFromXpm(bits
);
794 wxBitmap::wxBitmap(char **bits
)
796 (void) CreateFromXpm((const char **)bits
);
799 void* wxBitmap::GetRawAccess() const
801 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
802 return M_BITMAPDATA
->GetRawAccess() ;
805 void* wxBitmap::BeginRawAccess()
807 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
808 return M_BITMAPDATA
->BeginRawAccess() ;
811 void wxBitmap::EndRawAccess()
813 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
814 M_BITMAPDATA
->EndRawAccess() ;
817 bool wxBitmap::CreateFromXpm(const char **bits
)
820 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
821 wxXPMDecoder decoder
;
822 wxImage img
= decoder
.ReadData(bits
);
823 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
824 *this = wxBitmap(img
);
832 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
834 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
835 return M_BITMAPDATA
->CGImageCreate() ;
839 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
842 (rect
.x
>= 0) && (rect
.y
>= 0) &&
843 (rect
.x
+rect
.width
<= GetWidth()) &&
844 (rect
.y
+rect
.height
<= GetHeight()),
845 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
848 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
849 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
852 int sourcewidth
= GetWidth() ;
853 int destwidth
= rect
.width
;
854 int destheight
= rect
.height
;
856 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
857 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
858 int sourcelinesize
= sourcewidth
* 4 ;
859 int destlinesize
= destwidth
* 4 ;
860 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
861 unsigned char *dest
= destdata
;
862 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
864 memcpy( dest
, source
, destlinesize
) ;
869 if ( M_BITMAPDATA
->m_bitmapMask
)
871 wxMemoryBuffer maskbuf
;
872 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
873 size_t maskbufsize
= rowBytes
* destheight
;
874 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
876 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
877 int destlinesize
= rowBytes
;
878 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
879 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
880 unsigned char *dest
= destdata
;
882 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
884 memcpy( dest
, source
, destlinesize
) ;
886 maskbuf
.UngetWriteBuf( maskbufsize
) ;
887 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
889 else if ( HasAlpha() )
895 bool wxBitmap::Create(int w
, int h
, int d
)
900 d
= wxDisplayDepth() ;
902 m_refData
= new wxBitmapRefData( w
, h
, d
);
904 return M_BITMAPDATA
->Ok() ;
907 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
911 wxBitmapHandler
*handler
= FindHandler(type
);
915 m_refData
= new wxBitmapRefData
;
917 return handler
->LoadFile(this, filename
, type
, -1, -1);
922 wxImage
loadimage(filename
, type
);
923 if (loadimage
.Ok()) {
929 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
933 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
937 m_refData
= new wxBitmapRefData
;
939 wxBitmapHandler
*handler
= FindHandler(type
);
941 if ( handler
== NULL
) {
942 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
947 return handler
->Create(this, data
, type
, width
, height
, depth
);
952 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
954 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
956 // width and height of the device-dependent bitmap
957 int width
= image
.GetWidth();
958 int height
= image
.GetHeight();
960 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
964 bool hasAlpha
= false ;
966 if ( image
.HasMask() )
968 // takes precedence, don't mix with alpha info
972 hasAlpha
= image
.HasAlpha() ;
978 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
980 register unsigned char* data
= image
.GetData();
981 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
982 for (int y
= 0; y
< height
; y
++)
984 for (int x
= 0; x
< width
; x
++)
988 const unsigned char a
= *alpha
++;
990 #if wxMAC_USE_PREMULTIPLIED_ALPHA
991 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
992 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
993 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
995 *destination
++ = *data
++ ;
996 *destination
++ = *data
++ ;
997 *destination
++ = *data
++ ;
1002 *destination
++ = 0xFF ;
1003 *destination
++ = *data
++ ;
1004 *destination
++ = *data
++ ;
1005 *destination
++ = *data
++ ;
1010 if ( image
.HasMask() )
1012 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1016 wxImage
wxBitmap::ConvertToImage() const
1020 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1022 // create an wxImage object
1023 int width
= GetWidth();
1024 int height
= GetHeight();
1025 image
.Create( width
, height
);
1027 unsigned char *data
= image
.GetData();
1028 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1030 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1032 bool hasAlpha
= false ;
1033 bool hasMask
= false ;
1034 int maskBytesPerRow
= 0 ;
1035 unsigned char *alpha
= NULL
;
1036 unsigned char *mask
= NULL
;
1045 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1046 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1052 alpha
= image
.GetAlpha() ;
1056 // The following masking algorithm is the same as well in msw/gtk:
1057 // the colour used as transparent one in wxImage and the one it is
1058 // replaced with when it really occurs in the bitmap
1059 static const int MASK_RED
= 1;
1060 static const int MASK_GREEN
= 2;
1061 static const int MASK_BLUE
= 3;
1062 static const int MASK_BLUE_REPLACEMENT
= 2;
1064 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1066 unsigned char * maskp
= mask
;
1067 for (int xx
= 0; xx
< width
; xx
++)
1069 long color
= *((long*) source
) ;
1070 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1071 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1072 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1073 unsigned char b
= (color
&0x000000FF);
1076 if ( *maskp
++ == 0 )
1078 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1079 b
= MASK_BLUE_REPLACEMENT
;
1088 else if ( hasAlpha
)
1092 data
[index
+ 1] = g
;
1093 data
[index
+ 2] = b
;
1099 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1103 #endif //wxUSE_IMAGE
1105 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1106 const wxPalette
*palette
) const
1108 wxBitmapHandler
*handler
= FindHandler(type
);
1112 return handler
->SaveFile(this, filename
, type
, palette
);
1117 wxImage image
= ConvertToImage();
1118 return image
.SaveFile(filename
, type
);
1122 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1126 bool wxBitmap::Ok() const
1128 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1131 int wxBitmap::GetHeight() const
1133 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1135 return M_BITMAPDATA
->GetHeight();
1138 int wxBitmap::GetWidth() const
1140 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1142 return M_BITMAPDATA
->GetWidth() ;
1145 int wxBitmap::GetDepth() const
1147 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1149 return M_BITMAPDATA
->GetDepth();
1152 #if WXWIN_COMPATIBILITY_2_4
1154 int wxBitmap::GetQuality() const
1161 wxMask
*wxBitmap::GetMask() const
1163 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1165 return M_BITMAPDATA
->m_bitmapMask
;
1168 bool wxBitmap::HasAlpha() const
1170 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1172 return M_BITMAPDATA
->HasAlpha() ;
1175 void wxBitmap::SetWidth(int w
)
1178 m_refData
= new wxBitmapRefData
;
1180 M_BITMAPDATA
->SetWidth(w
);
1183 void wxBitmap::SetHeight(int h
)
1186 m_refData
= new wxBitmapRefData
;
1188 M_BITMAPDATA
->SetHeight(h
);
1191 void wxBitmap::SetDepth(int d
)
1194 m_refData
= new wxBitmapRefData
;
1196 M_BITMAPDATA
->SetDepth(d
);
1199 #if WXWIN_COMPATIBILITY_2_4
1201 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1207 void wxBitmap::SetOk(bool isOk
)
1210 m_refData
= new wxBitmapRefData
;
1212 M_BITMAPDATA
->SetOk(isOk
);
1216 wxPalette
*wxBitmap::GetPalette() const
1218 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1220 return &M_BITMAPDATA
->m_bitmapPalette
;
1223 void wxBitmap::SetPalette(const wxPalette
& palette
)
1226 m_refData
= new wxBitmapRefData
;
1228 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1230 #endif // wxUSE_PALETTE
1232 void wxBitmap::SetMask(wxMask
*mask
)
1235 m_refData
= new wxBitmapRefData
;
1237 // Remove existing mask if there is one.
1238 delete M_BITMAPDATA
->m_bitmapMask
;
1240 M_BITMAPDATA
->m_bitmapMask
= mask
;
1243 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1245 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1248 // ----------------------------------------------------------------------------
1250 // ----------------------------------------------------------------------------
1257 // Construct a mask from a bitmap and a colour indicating
1258 // the transparent area
1259 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1262 Create(bitmap
, colour
);
1265 // Construct a mask from a mono bitmap (copies the bitmap).
1266 wxMask::wxMask(const wxBitmap
& bitmap
)
1272 // Construct a mask from a mono bitmap (copies the bitmap).
1273 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1276 Create(data
, width
, height
, bytesPerRow
);
1283 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1284 m_maskBitmap
= NULL
;
1290 m_width
= m_height
= m_bytesPerRow
= 0 ;
1291 m_maskBitmap
= NULL
;
1294 void *wxMask::GetRawAccess() const
1296 return m_memBuf
.GetData() ;
1299 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1300 // so no rainbox colors will be created by QD when blitting
1302 void wxMask::RealizeNative()
1306 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1307 m_maskBitmap
= NULL
;
1309 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1310 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1311 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1314 // Create a mask from a mono bitmap (copies the bitmap).
1315 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1320 m_bytesPerRow
= bytesPerRow
;
1321 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1326 // Create a mask from a mono bitmap (copies the bitmap).
1327 bool wxMask::Create(const wxBitmap
& bitmap
)
1329 m_width
= bitmap
.GetWidth() ;
1330 m_height
= bitmap
.GetHeight() ;
1331 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1332 size_t size
= m_bytesPerRow
* m_height
;
1333 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1334 memset( destdatabase
, 0 , size
) ;
1335 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1336 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1338 unsigned char *destdata
= destdatabase
;
1339 for( int x
= 0 ; x
< m_width
; ++x
)
1342 unsigned char r
= *srcdata
++ ;
1343 unsigned char g
= *srcdata
++ ;
1344 unsigned char b
= *srcdata
++ ;
1345 if ( ( r
+ g
+ b
) > 0x10 )
1346 *destdata
++ = 0x00 ;
1348 *destdata
++ = 0xFF ;
1351 m_memBuf
.UngetWriteBuf( size
) ;
1356 // Create a mask from a bitmap and a colour indicating
1357 // the transparent area
1358 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1360 m_width
= bitmap
.GetWidth() ;
1361 m_height
= bitmap
.GetHeight() ;
1362 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1363 size_t size
= m_bytesPerRow
* m_height
;
1365 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1366 memset( destdatabase
, 0 , size
) ;
1367 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1368 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1370 unsigned char *destdata
= destdatabase
;
1371 for( int x
= 0 ; x
< m_width
; ++x
)
1374 unsigned char r
= *srcdata
++ ;
1375 unsigned char g
= *srcdata
++ ;
1376 unsigned char b
= *srcdata
++ ;
1377 if ( colour
== wxColour( r
, g
, b
) )
1378 *destdata
++ = 0x00 ;
1380 *destdata
++ = 0xFF ;
1383 m_memBuf
.UngetWriteBuf( size
) ;
1388 WXHBITMAP
wxMask::GetHBITMAP() const
1390 return m_maskBitmap
;
1393 // ----------------------------------------------------------------------------
1395 // ----------------------------------------------------------------------------
1397 wxBitmapHandler::~wxBitmapHandler()
1401 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1406 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1407 int desiredWidth
, int desiredHeight
)
1412 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1417 // ----------------------------------------------------------------------------
1418 // Standard Handlers
1419 // ----------------------------------------------------------------------------
1421 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1423 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1425 inline wxPICTResourceHandler()
1427 SetName(wxT("Macintosh Pict resource"));
1428 SetExtension(wxEmptyString
);
1429 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1432 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1433 int desiredWidth
, int desiredHeight
);
1435 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1438 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1439 int desiredWidth
, int desiredHeight
)
1443 wxMacStringToPascal( name
, theName
) ;
1445 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1449 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1450 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1452 dc
.SelectObject( *bitmap
) ;
1454 dc
.SelectObject( wxNullBitmap
) ;
1457 #endif //wxUSE_METAFILE
1462 void wxBitmap::InitStandardHandlers()
1464 AddHandler(new wxPICTResourceHandler
) ;
1465 AddHandler(new wxICONResourceHandler
) ;
1468 // ----------------------------------------------------------------------------
1469 // raw bitmap access support
1470 // ----------------------------------------------------------------------------
1472 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1476 // no bitmap, no data (raw or otherwise)
1480 data
.m_width
= GetWidth() ;
1481 data
.m_height
= GetHeight() ;
1482 data
.m_stride
= GetWidth() * 4 ;
1483 return GetRawAccess() ;
1486 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1491 // TODO : if we have some information about the API we should check
1492 // this code looks strange...
1494 if ( M_BITMAPDATA
->HasAlpha() )
1496 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1498 int w
= data
.GetWidth(),
1499 h
= data
.GetHeight();
1501 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1502 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1503 wxAlphaPixelData::Iterator
pMask(dataMask
),
1505 for ( int y
= 0; y
< h
; y
++ )
1507 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1510 for ( int x
= 0; x
< w
; x
++ )
1512 const wxAlphaPixelData::Iterator::ChannelType
1515 pMask
.Red() = alpha
;
1516 pMask
.Green() = alpha
;
1517 pMask
.Blue() = alpha
;
1526 pMask
= rowStartMask
;
1527 pMask
.OffsetY(dataMask
, 1);
1530 SetMask(new wxMask(bmpMask
));
1534 void wxBitmap::UseAlpha()
1536 // remember that we are using alpha channel, we'll need to create a proper
1537 // mask in UngetRawData()
1538 M_BITMAPDATA
->UseAlpha( true );