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 IconFamilyHandle
wxMacCreateIconFamily(const wxBitmap
& bitmap
)
56 wxBitmap bmp
= bitmap
;
57 // setup the header properly
59 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
60 (**iconFamily
).resourceType
= kIconFamilyType
;
61 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
63 int w
= bmp
.GetWidth() ;
64 int h
= bmp
.GetHeight() ;
65 int sz
= wxMax( w
, h
) ;
72 bmp
= wxBitmap( bitmap
.ConvertToImage().Scale( 128 , 128 ) ) ;
78 bmp
= wxBitmap( bitmap
.ConvertToImage().Scale( 48 , 48 ) ) ;
85 dataType
= kThumbnail32BitData
;
86 maskType
= kThumbnail8BitMask
;
90 dataType
= kHuge32BitData
;
91 maskType
= kHuge8BitMask
;
95 dataType
= kLarge32BitData
;
96 maskType
= kLarge8BitMask
;
100 dataType
= kSmall32BitData
;
101 maskType
= kSmall8BitMask
;
109 Handle maskdata
= NULL
;
110 unsigned char * maskptr
= NULL
;
111 unsigned char * ptr
= NULL
;
116 data
= NewHandle( size
) ;
118 ptr
= (unsigned char*) *data
;
119 memset( ptr
, 0, size
) ;
122 maskdata
= NewHandle( masksize
) ;
124 maskptr
= (unsigned char*) *maskdata
;
125 memset( maskptr
, 0 , masksize
) ;
127 bool hasAlpha
= bmp
.HasAlpha() ;
128 wxMask
*mask
= bmp
.GetMask() ;
129 unsigned char * source
= (unsigned char*) bmp
.GetRawAccess() ;
130 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
131 for ( int y
= 0 ; y
< h
; ++y
)
133 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
134 unsigned char * maskdest
= maskptr
+ y
* sz
;
135 for ( int x
= 0 ; x
< w
; ++x
)
137 unsigned char a
= *source
++ ;
138 unsigned char r
= *source
++ ;
139 unsigned char g
= *source
++ ;
140 unsigned char b
= *source
++ ;
148 *maskdest
++ = *masksource
++ ;
156 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
157 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
159 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
160 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
162 HUnlock( maskdata
) ;
163 DisposeHandle( data
) ;
164 DisposeHandle( maskdata
) ;
168 IconRef
wxMacCreateIconRef(const wxBitmap
& bmp
)
170 IconFamilyHandle iconFamily
= wxMacCreateIconFamily( bmp
) ;
171 if ( iconFamily
== NULL
)
175 static int iconCounter
= 2 ;
177 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &iconRef
) ;
179 err
= GetIconRefOwners(iconRef
, &owners
) ;
181 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
182 // we have to retain a reference, as Unregister will decrement it
183 AcquireIconRef( iconRef
) ;
184 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
185 DisposeHandle( (Handle
) iconFamily
) ;
191 PicHandle
wxMacCreatePicHandle( const wxBitmap
&bmp
)
193 CGrafPtr origPort
= NULL
;
194 GDHandle origDev
= NULL
;
195 PicHandle pict
= NULL
;
196 GWorldPtr wp
= NULL
;
197 GWorldPtr mask
= NULL
;
199 GetGWorld( &origPort
, &origDev
) ;
201 wp
= (GWorldPtr
) bmp
.GetHBITMAP( (WXHBITMAP
*) &mask
) ;
203 SetGWorld( wp
, NULL
) ;
205 GetPortBounds( wp
, &portRect
) ;
206 pict
= OpenPicture(&portRect
);
210 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
211 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
212 RGBForeColor( &black
) ;
213 RGBBackColor( &white
) ;
215 LockPixels( GetGWorldPixMap( wp
) ) ;
216 CopyBits(GetPortBitMapForCopyBits(wp
),
217 GetPortBitMapForCopyBits(wp
),
221 UnlockPixels( GetGWorldPixMap( wp
) ) ;
224 SetGWorld( origPort
, origDev
) ;
229 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
231 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
234 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
237 info
->contentType
= kControlContentIconRef
;
238 info
->u
.iconRef
= wxMacCreateIconRef( bitmap
) ;
239 wxASSERT_MSG( info
->u
.iconRef
, wxT("Converting to IconRef not possible") ) ;
240 #if wxMAC_USE_CORE_GRAPHICS
242 // only on 10.4 more controls will accept a CGImage
244 info->contentType = kControlContentCGImageRef ;
245 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
251 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
253 if ( info
->contentType
== kControlContentIconRef
)
255 ReleaseIconRef(info
->u
.iconRef
) ;
257 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
258 else if ( info
->contentType
== kControlContentCGImageRef
)
260 CGImageRelease( info
->u
.imageRef
) ;
265 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
269 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
271 void wxBitmapRefData::Init()
277 m_bitmapMask
= NULL
;
278 #if wxMAC_USE_CORE_GRAPHICS
279 m_cgImageRef
= NULL
;
282 m_hMaskBitmap
= NULL
;
283 m_maskBytesPerRow
= NULL
;
285 m_rawAccessCount
= 0 ;
289 wxBitmapRefData::wxBitmapRefData()
294 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
297 Create( w
, h
, d
) ;
300 bool wxBitmapRefData::Create( int w
, int h
, int d
)
306 m_bytesPerRow
= w
* 4 ;
307 size_t size
= m_bytesPerRow
* h
;
308 void* data
= m_memBuf
.GetWriteBuf(size
) ;
309 memset( data
, 0 , size
) ;
310 m_memBuf
.UngetWriteBuf(size
) ;
313 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
314 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
315 (char*) data
, m_bytesPerRow
) ) ;
316 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
317 m_ok
= ( m_hBitmap
!= NULL
) ;
322 void wxBitmapRefData::UseAlpha( bool use
)
324 if ( m_hasAlpha
== use
)
330 int width
= GetWidth() ;
331 int height
= GetHeight() ;
332 m_maskBytesPerRow
= ( width
+ 3 ) & 0xFFFFFFC ;
333 size_t size
= height
* m_maskBytesPerRow
;
334 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
335 memset( data
, 0 , size
) ;
336 wxASSERT( m_hMaskBitmap
== NULL
) ;
337 Rect rect
= { 0 , 0 , height
, width
} ;
338 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
339 (char*) data
, m_maskBytesPerRow
) ) ;
340 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
341 m_maskMemBuf
.UngetWriteBuf(size
) ;
342 #if !wxMAC_USE_CORE_GRAPHICS
348 DisposeGWorld( m_hMaskBitmap
) ;
349 m_hMaskBitmap
= NULL
;
350 m_maskBytesPerRow
= 0 ;
354 void *wxBitmapRefData::GetRawAccess() const
356 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
357 return m_memBuf
.GetData() ;
360 void *wxBitmapRefData::BeginRawAccess()
362 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
363 wxASSERT( m_rawAccessCount
== 0 ) ;
365 #if wxMAC_USE_CORE_GRAPHICS
366 // we must destroy an existing cached image, as
367 // the bitmap data may change now
370 CGImageRelease( m_cgImageRef
) ;
371 m_cgImageRef
= NULL
;
374 return m_memBuf
.GetData() ;
377 void wxBitmapRefData::EndRawAccess()
379 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
380 wxASSERT( m_rawAccessCount
== 1 ) ;
382 #if !wxMAC_USE_CORE_GRAPHICS
388 #if wxMAC_USE_CORE_GRAPHICS
389 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
391 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
392 wxASSERT( data
== membuf
->GetData() ) ;
396 CGImageRef
wxBitmapRefData::CGImageCreate() const
399 wxASSERT( m_rawAccessCount
>= 0 ) ;
401 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
403 size_t imageSize
= m_width
* m_height
* 4 ;
404 void * dataBuffer
= m_memBuf
.GetData() ;
407 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
408 wxMemoryBuffer
* membuf
= NULL
;
412 membuf
= new wxMemoryBuffer( imageSize
) ;
413 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
414 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
415 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
416 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
417 alphaInfo
= kCGImageAlphaFirst
;
418 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
420 unsigned char *sourcemask
= sourcemaskstart
;
421 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
423 *destalpha
= *sourcemask
;
427 else if ( m_hasAlpha
)
429 #if wxMAC_USE_PREMULTIPLIED_ALPHA
430 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
432 alphaInfo
= kCGImageAlphaFirst
;
434 membuf
= new wxMemoryBuffer( m_memBuf
) ;
438 membuf
= new wxMemoryBuffer( m_memBuf
) ;
440 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
441 CGDataProviderRef dataProvider
=
442 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
443 wxMacMemoryBufferReleaseProc
);
445 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
446 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
447 CGDataProviderRelease( dataProvider
);
451 image
= m_cgImageRef
;
452 CGImageRetain( image
) ;
454 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
456 // we keep it for later use
457 m_cgImageRef
= image
;
458 CGImageRetain( image
) ;
464 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
466 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
471 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
472 else if ( m_hasAlpha
)
474 #if !wxMAC_USE_CORE_GRAPHICS
475 if ( m_rawAccessCount
> 0 )
478 // this structure is not kept in synch when using CG, so if someone
479 // is really accessing the Graphports, we have to sync it
482 *mask
= m_hMaskBitmap
;
488 void wxBitmapRefData::UpdateAlphaMask() const
492 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
493 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
495 int h
= GetHeight() ;
498 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
500 unsigned char* destalpha
= destalphabase
;
501 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4, ++destalpha
)
503 *destalpha
= *sourcemask
;
509 void wxBitmapRefData::Free()
511 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
513 #if wxMAC_USE_CORE_GRAPHICS
516 CGImageRelease( m_cgImageRef
) ;
517 m_cgImageRef
= NULL
;
522 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
527 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
528 m_hMaskBitmap
= NULL
;
538 wxBitmapRefData::~wxBitmapRefData()
543 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
545 int w
= icon
.GetWidth() ;
546 int h
= icon
.GetHeight() ;
547 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
549 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
551 IconFamilyHandle iconFamily
= NULL
;
552 Handle imagehandle
= NewHandle(0) ;
553 Handle maskhandle
= NewHandle(0) ;
557 IconSelectorValue selector
= 0 ;
560 dataType
= kThumbnail32BitData
;
561 maskType
= kThumbnail8BitMask
;
562 selector
= kSelectorAllAvailableData
;
566 dataType
= kHuge32BitData
;
567 maskType
= kHuge8BitMask
;
568 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
572 dataType
= kLarge32BitData
;
573 maskType
= kLarge8BitMask
;
574 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
578 dataType
= kSmall32BitData
;
579 maskType
= kSmall8BitMask
;
580 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
584 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
588 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
590 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
591 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
592 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
593 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
595 unsigned char *source
= (unsigned char *) *imagehandle
;
596 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
598 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
599 for ( int y
= 0 ; y
< h
; ++y
)
601 for ( int x
= 0 ; x
< w
; ++x
)
603 *destination
++ = *sourcemask
++ ;
605 *destination
++ = *source
++ ;
606 *destination
++ = *source
++ ;
607 *destination
++ = *source
++ ;
611 DisposeHandle( imagehandle
) ;
612 DisposeHandle( maskhandle
) ;
617 dc
.SelectObject( *this ) ;
618 dc
.DrawIcon( icon
, 0 , 0 ) ;
619 dc
.SelectObject( wxNullBitmap
) ;
628 wxBitmap::~wxBitmap()
632 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
634 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
638 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
639 if ( the_width
% (sizeof(unsigned char) * 8) ) {
640 linesize
+= sizeof(unsigned char);
642 unsigned char* linestart
= (unsigned char*) bits
;
643 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
644 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
646 for ( int x
= 0 ; x
< the_width
; ++x
)
650 int mask
= 1 << bit
;
651 if ( linestart
[index
] & mask
)
653 *destination
++ = 0xFF ;
660 *destination
++ = 0xFF ;
661 *destination
++ = 0xFF ;
662 *destination
++ = 0xFF ;
663 *destination
++ = 0xFF ;
671 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
675 wxBitmap::wxBitmap(int w
, int h
, int d
)
677 (void)Create(w
, h
, d
);
680 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
682 (void) Create(data
, type
, width
, height
, depth
);
685 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
687 LoadFile(filename
, type
);
690 wxBitmap::wxBitmap(const char **bits
)
692 (void) CreateFromXpm(bits
);
695 wxBitmap::wxBitmap(char **bits
)
697 (void) CreateFromXpm((const char **)bits
);
700 void* wxBitmap::GetRawAccess() const
702 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
703 return M_BITMAPDATA
->GetRawAccess() ;
706 void* wxBitmap::BeginRawAccess()
708 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
709 return M_BITMAPDATA
->BeginRawAccess() ;
712 void wxBitmap::EndRawAccess()
714 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
715 M_BITMAPDATA
->EndRawAccess() ;
718 bool wxBitmap::CreateFromXpm(const char **bits
)
720 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
721 wxXPMDecoder decoder
;
722 wxImage img
= decoder
.ReadData(bits
);
723 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
724 *this = wxBitmap(img
);
728 #if wxMAC_USE_CORE_GRAPHICS
729 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
731 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
732 return M_BITMAPDATA
->CGImageCreate() ;
736 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
739 (rect
.x
>= 0) && (rect
.y
>= 0) &&
740 (rect
.x
+rect
.width
<= GetWidth()) &&
741 (rect
.y
+rect
.height
<= GetHeight()),
742 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
745 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
746 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
749 int sourcewidth
= GetWidth() ;
750 int destwidth
= rect
.width
;
751 int destheight
= rect
.height
;
753 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
754 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
755 int sourcelinesize
= sourcewidth
* 4 ;
756 int destlinesize
= destwidth
* 4 ;
757 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
758 unsigned char *dest
= destdata
;
759 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
761 memcpy( dest
, source
, destlinesize
) ;
766 if ( M_BITMAPDATA
->m_bitmapMask
)
768 wxMemoryBuffer maskbuf
;
769 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
770 size_t maskbufsize
= rowBytes
* destheight
;
771 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
773 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
774 int destlinesize
= rowBytes
;
775 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
776 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
777 unsigned char *dest
= destdata
;
779 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
781 memcpy( dest
, source
, destlinesize
) ;
783 maskbuf
.UngetWriteBuf( maskbufsize
) ;
784 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
786 else if ( HasAlpha() )
792 bool wxBitmap::Create(int w
, int h
, int d
)
797 d
= wxDisplayDepth() ;
799 m_refData
= new wxBitmapRefData( w
, h
, d
);
801 return M_BITMAPDATA
->Ok() ;
804 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
808 wxBitmapHandler
*handler
= FindHandler(type
);
812 m_refData
= new wxBitmapRefData
;
814 return handler
->LoadFile(this, filename
, type
, -1, -1);
818 wxImage
loadimage(filename
, type
);
819 if (loadimage
.Ok()) {
824 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
828 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
832 m_refData
= new wxBitmapRefData
;
834 wxBitmapHandler
*handler
= FindHandler(type
);
836 if ( handler
== NULL
) {
837 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
842 return handler
->Create(this, data
, type
, width
, height
, depth
);
845 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
847 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
849 // width and height of the device-dependent bitmap
850 int width
= image
.GetWidth();
851 int height
= image
.GetHeight();
853 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
857 bool hasAlpha
= false ;
859 if ( image
.HasMask() )
861 // takes precedence, don't mix with alpha info
865 hasAlpha
= image
.HasAlpha() ;
871 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
873 register unsigned char* data
= image
.GetData();
874 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
875 for (int y
= 0; y
< height
; y
++)
877 for (int x
= 0; x
< width
; x
++)
881 const unsigned char a
= *alpha
++;
883 #if wxMAC_USE_PREMULTIPLIED_ALPHA
884 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
885 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
886 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
888 *destination
++ = *data
++ ;
889 *destination
++ = *data
++ ;
890 *destination
++ = *data
++ ;
895 *destination
++ = 0xFF ;
896 *destination
++ = *data
++ ;
897 *destination
++ = *data
++ ;
898 *destination
++ = *data
++ ;
903 if ( image
.HasMask() )
905 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
909 wxImage
wxBitmap::ConvertToImage() const
913 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
915 // create an wxImage object
916 int width
= GetWidth();
917 int height
= GetHeight();
918 image
.Create( width
, height
);
920 unsigned char *data
= image
.GetData();
921 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
923 unsigned char* source
= (unsigned char*) GetRawAccess() ;
925 bool hasAlpha
= false ;
926 bool hasMask
= false ;
927 unsigned char *alpha
= NULL
;
928 unsigned char *mask
= NULL
;
937 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
943 alpha
= image
.GetAlpha() ;
947 // The following masking algorithm is the same as well in msw/gtk:
948 // the colour used as transparent one in wxImage and the one it is
949 // replaced with when it really occurs in the bitmap
950 static const int MASK_RED
= 1;
951 static const int MASK_GREEN
= 2;
952 static const int MASK_BLUE
= 3;
953 static const int MASK_BLUE_REPLACEMENT
= 2;
955 for (int yy
= 0; yy
< height
; yy
++)
957 for (int xx
= 0; xx
< width
; xx
++)
959 long color
= *((long*) source
) ;
960 unsigned char a
= ((color
&0xFF000000) >> 24) ;
961 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
962 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
963 unsigned char b
= (color
&0x000000FF);
968 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
969 b
= MASK_BLUE_REPLACEMENT
;
982 data
[index
+ 1] = g
;
983 data
[index
+ 2] = b
;
989 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
994 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
995 const wxPalette
*palette
) const
997 wxBitmapHandler
*handler
= FindHandler(type
);
1001 return handler
->SaveFile(this, filename
, type
, palette
);
1005 wxImage image
= ConvertToImage();
1007 return image
.SaveFile(filename
, type
);
1010 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1014 bool wxBitmap::Ok() const
1016 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1019 int wxBitmap::GetHeight() const
1021 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1023 return M_BITMAPDATA
->GetHeight();
1026 int wxBitmap::GetWidth() const
1028 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1030 return M_BITMAPDATA
->GetWidth() ;
1033 int wxBitmap::GetDepth() const
1035 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1037 return M_BITMAPDATA
->GetDepth();
1040 #if WXWIN_COMPATIBILITY_2_4
1042 int wxBitmap::GetQuality() const
1049 wxMask
*wxBitmap::GetMask() const
1051 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1053 return M_BITMAPDATA
->m_bitmapMask
;
1056 bool wxBitmap::HasAlpha() const
1058 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1060 return M_BITMAPDATA
->HasAlpha() ;
1063 void wxBitmap::SetWidth(int w
)
1066 m_refData
= new wxBitmapRefData
;
1068 M_BITMAPDATA
->SetWidth(w
);
1071 void wxBitmap::SetHeight(int h
)
1074 m_refData
= new wxBitmapRefData
;
1076 M_BITMAPDATA
->SetHeight(h
);
1079 void wxBitmap::SetDepth(int d
)
1082 m_refData
= new wxBitmapRefData
;
1084 M_BITMAPDATA
->SetDepth(d
);
1087 #if WXWIN_COMPATIBILITY_2_4
1089 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1095 void wxBitmap::SetOk(bool isOk
)
1098 m_refData
= new wxBitmapRefData
;
1100 M_BITMAPDATA
->SetOk(isOk
);
1104 wxPalette
*wxBitmap::GetPalette() const
1106 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1108 return &M_BITMAPDATA
->m_bitmapPalette
;
1111 void wxBitmap::SetPalette(const wxPalette
& palette
)
1114 m_refData
= new wxBitmapRefData
;
1116 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1118 #endif // wxUSE_PALETTE
1120 void wxBitmap::SetMask(wxMask
*mask
)
1123 m_refData
= new wxBitmapRefData
;
1125 // Remove existing mask if there is one.
1126 delete M_BITMAPDATA
->m_bitmapMask
;
1128 M_BITMAPDATA
->m_bitmapMask
= mask
;
1131 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1133 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1136 // ----------------------------------------------------------------------------
1138 // ----------------------------------------------------------------------------
1145 // Construct a mask from a bitmap and a colour indicating
1146 // the transparent area
1147 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1150 Create(bitmap
, colour
);
1153 // Construct a mask from a mono bitmap (copies the bitmap).
1154 wxMask::wxMask(const wxBitmap
& bitmap
)
1160 // Construct a mask from a mono bitmap (copies the bitmap).
1161 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1164 Create(data
, width
, height
, bytesPerRow
);
1171 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1172 m_maskBitmap
= NULL
;
1178 m_width
= m_height
= m_bytesPerRow
= 0 ;
1179 m_maskBitmap
= NULL
;
1182 void *wxMask::GetRawAccess() const
1184 return m_memBuf
.GetData() ;
1187 void wxMask::RealizeNative()
1191 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1192 m_maskBitmap
= NULL
;
1194 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1195 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1196 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1199 // Create a mask from a mono bitmap (copies the bitmap).
1200 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1205 m_bytesPerRow
= bytesPerRow
;
1206 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1211 // Create a mask from a mono bitmap (copies the bitmap).
1212 bool wxMask::Create(const wxBitmap
& bitmap
)
1214 m_width
= bitmap
.GetWidth() ;
1215 m_height
= bitmap
.GetHeight() ;
1216 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1217 size_t size
= m_bytesPerRow
* m_height
;
1218 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1219 memset( destdatabase
, 0 , size
) ;
1220 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1221 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1223 unsigned char *destdata
= destdatabase
;
1224 for( int x
= 0 ; x
< m_width
; ++x
)
1227 unsigned char r
= *srcdata
++ ;
1228 unsigned char g
= *srcdata
++ ;
1229 unsigned char b
= *srcdata
++ ;
1230 if ( ( r
+ g
+ b
) > 0x10 )
1231 *destdata
++ = 0x00 ;
1233 *destdata
++ = 0xFF ;
1236 m_memBuf
.UngetWriteBuf( size
) ;
1241 // Create a mask from a bitmap and a colour indicating
1242 // the transparent area
1243 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1245 m_width
= bitmap
.GetWidth() ;
1246 m_height
= bitmap
.GetHeight() ;
1247 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1248 size_t size
= m_bytesPerRow
* m_height
;
1250 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1251 memset( destdatabase
, 0 , size
) ;
1252 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1253 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1255 unsigned char *destdata
= destdatabase
;
1256 for( int x
= 0 ; x
< m_width
; ++x
)
1259 unsigned char r
= *srcdata
++ ;
1260 unsigned char g
= *srcdata
++ ;
1261 unsigned char b
= *srcdata
++ ;
1262 if ( colour
== wxColour( r
, g
, b
) )
1263 *destdata
++ = 0x00 ;
1265 *destdata
++ = 0xFF ;
1268 m_memBuf
.UngetWriteBuf( size
) ;
1273 WXHBITMAP
wxMask::GetHBITMAP() const
1275 return m_maskBitmap
;
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1282 wxBitmapHandler::~wxBitmapHandler()
1286 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1291 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1292 int desiredWidth
, int desiredHeight
)
1297 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1302 // ----------------------------------------------------------------------------
1303 // Standard Handlers
1304 // ----------------------------------------------------------------------------
1306 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1308 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1310 inline wxPICTResourceHandler()
1312 SetName(wxT("Macintosh Pict resource"));
1313 SetExtension(wxEmptyString
);
1314 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1317 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1318 int desiredWidth
, int desiredHeight
);
1320 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1323 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1324 int desiredWidth
, int desiredHeight
)
1328 wxMacStringToPascal( name
, theName
) ;
1330 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1334 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1335 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1337 dc
.SelectObject( *bitmap
) ;
1339 dc
.SelectObject( wxNullBitmap
) ;
1342 #endif //wxUSE_METAFILE
1347 void wxBitmap::InitStandardHandlers()
1349 AddHandler(new wxPICTResourceHandler
) ;
1350 AddHandler(new wxICONResourceHandler
) ;
1353 // ----------------------------------------------------------------------------
1354 // raw bitmap access support
1355 // ----------------------------------------------------------------------------
1357 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1361 // no bitmap, no data (raw or otherwise)
1365 data
.m_width
= GetWidth() ;
1366 data
.m_height
= GetHeight() ;
1367 data
.m_stride
= GetWidth() * 4 ;
1368 return GetRawAccess() ;
1371 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1376 // TODO : if we have some information about the API we should check
1377 // this code looks strange...
1379 if ( M_BITMAPDATA
->HasAlpha() )
1381 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1383 int w
= data
.GetWidth(),
1384 h
= data
.GetHeight();
1386 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1387 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1388 wxAlphaPixelData::Iterator
pMask(dataMask
),
1390 for ( int y
= 0; y
< h
; y
++ )
1392 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1395 for ( int x
= 0; x
< w
; x
++ )
1397 const wxAlphaPixelData::Iterator::ChannelType
1400 pMask
.Red() = alpha
;
1401 pMask
.Green() = alpha
;
1402 pMask
.Blue() = alpha
;
1411 pMask
= rowStartMask
;
1412 pMask
.OffsetY(dataMask
, 1);
1415 SetMask(new wxMask(bmpMask
));
1419 void wxBitmap::UseAlpha()
1421 // remember that we are using alpha channel, we'll need to create a proper
1422 // mask in UngetRawData()
1423 M_BITMAPDATA
->UseAlpha( true );