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
;
107 Handle maskdata
= NULL
;
108 unsigned char * maskptr
= NULL
;
109 unsigned char * ptr
= NULL
;
114 data
= NewHandle( size
) ;
116 ptr
= (unsigned char*) *data
;
117 memset( ptr
, 0, size
) ;
120 maskdata
= NewHandle( masksize
) ;
122 maskptr
= (unsigned char*) *maskdata
;
123 memset( maskptr
, 0 , masksize
) ;
125 bool hasAlpha
= bmp
.HasAlpha() ;
126 wxMask
*mask
= bmp
.GetMask() ;
127 unsigned char * source
= (unsigned char*) bmp
.GetRawAccess() ;
128 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
129 for ( int y
= 0 ; y
< h
; ++y
)
131 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
132 unsigned char * maskdest
= maskptr
+ y
* sz
;
133 for ( int x
= 0 ; x
< w
; ++x
)
135 unsigned char a
= *source
++ ;
136 unsigned char r
= *source
++ ;
137 unsigned char g
= *source
++ ;
138 unsigned char b
= *source
++ ;
146 *maskdest
++ = *masksource
++ ;
154 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
155 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
157 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
158 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
160 HUnlock( maskdata
) ;
161 DisposeHandle( data
) ;
162 DisposeHandle( maskdata
) ;
166 PicHandle pic
= wxMacCreatePicHandle( bitmap
) ;
167 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
173 IconRef
wxMacCreateIconRef(const wxBitmap
& bmp
)
175 IconFamilyHandle iconFamily
= wxMacCreateIconFamily( bmp
) ;
176 if ( iconFamily
== NULL
)
180 static int iconCounter
= 2 ;
182 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &iconRef
) ;
184 err
= GetIconRefOwners(iconRef
, &owners
) ;
186 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
187 // we have to retain a reference, as Unregister will decrement it
188 AcquireIconRef( iconRef
) ;
189 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
190 DisposeHandle( (Handle
) iconFamily
) ;
196 PicHandle
wxMacCreatePicHandle( const wxBitmap
&bmp
)
198 CGrafPtr origPort
= NULL
;
199 GDHandle origDev
= NULL
;
200 PicHandle pict
= NULL
;
201 GWorldPtr wp
= NULL
;
202 GWorldPtr mask
= NULL
;
203 int height
= bmp
.GetHeight() ;
204 int width
= bmp
.GetWidth() ;
206 Rect rect
= { 0 , 0 , height
, width
} ;
208 GetGWorld( &origPort
, &origDev
) ;
210 wp
= (GWorldPtr
) bmp
.GetHBITMAP( (WXHBITMAP
*) &mask
) ;
212 RgnHandle clipRgn
= NULL
;
216 GWorldPtr monoworld
;
218 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
219 LockPixels( GetGWorldPixMap( monoworld
) ) ;
220 LockPixels( GetGWorldPixMap( mask
) ) ;
221 SetGWorld( monoworld
, NULL
) ;
222 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
223 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
224 RGBForeColor( &black
) ;
225 RGBBackColor( &white
) ;
226 CopyBits(GetPortBitMapForCopyBits(mask
),
227 GetPortBitMapForCopyBits(monoworld
),
231 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
232 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
233 UnlockPixels( GetGWorldPixMap( mask
) ) ;
234 DisposeGWorld( monoworld
) ;
237 SetGWorld( wp
, NULL
) ;
239 GetPortBounds( wp
, &portRect
) ;
240 pict
= OpenPicture(&portRect
);
244 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
245 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
246 RGBForeColor( &black
) ;
247 RGBBackColor( &white
) ;
252 LockPixels( GetGWorldPixMap( wp
) ) ;
253 CopyBits(GetPortBitMapForCopyBits(wp
),
254 GetPortBitMapForCopyBits(wp
),
258 UnlockPixels( GetGWorldPixMap( wp
) ) ;
261 SetGWorld( origPort
, origDev
) ;
263 DisposeRgn( clipRgn
) ;
268 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
270 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
273 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
276 info
->contentType
= kControlContentIconRef
;
277 info
->u
.iconRef
= wxMacCreateIconRef( bitmap
) ;
278 wxASSERT_MSG( info
->u
.iconRef
, wxT("Converting to IconRef not possible") ) ;
279 #if wxMAC_USE_CORE_GRAPHICS
281 // only on 10.4 more controls will accept a CGImage
283 info->contentType = kControlContentCGImageRef ;
284 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
290 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
292 if ( info
->contentType
== kControlContentIconRef
)
294 ReleaseIconRef(info
->u
.iconRef
) ;
296 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
297 else if ( info
->contentType
== kControlContentCGImageRef
)
299 CGImageRelease( info
->u
.imageRef
) ;
304 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
308 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
310 void wxBitmapRefData::Init()
316 m_bitmapMask
= NULL
;
317 #if wxMAC_USE_CORE_GRAPHICS
318 m_cgImageRef
= NULL
;
321 m_hMaskBitmap
= NULL
;
322 m_maskBytesPerRow
= NULL
;
324 m_rawAccessCount
= 0 ;
328 wxBitmapRefData::wxBitmapRefData()
333 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
336 Create( w
, h
, d
) ;
339 bool wxBitmapRefData::Create( int w
, int h
, int d
)
345 m_bytesPerRow
= w
* 4 ;
346 size_t size
= m_bytesPerRow
* h
;
347 void* data
= m_memBuf
.GetWriteBuf(size
) ;
348 memset( data
, 0 , size
) ;
349 m_memBuf
.UngetWriteBuf(size
) ;
352 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
353 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
354 (char*) data
, m_bytesPerRow
) ) ;
355 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
356 m_ok
= ( m_hBitmap
!= NULL
) ;
361 void wxBitmapRefData::UseAlpha( bool use
)
363 if ( m_hasAlpha
== use
)
369 int width
= GetWidth() ;
370 int height
= GetHeight() ;
371 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
372 size_t size
= height
* m_maskBytesPerRow
;
373 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
374 memset( data
, 0 , size
) ;
375 wxASSERT( m_hMaskBitmap
== NULL
) ;
376 Rect rect
= { 0 , 0 , height
, width
} ;
377 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
378 (char*) data
, m_maskBytesPerRow
) ) ;
379 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
380 m_maskMemBuf
.UngetWriteBuf(size
) ;
381 #if !wxMAC_USE_CORE_GRAPHICS
387 DisposeGWorld( m_hMaskBitmap
) ;
388 m_hMaskBitmap
= NULL
;
389 m_maskBytesPerRow
= 0 ;
393 void *wxBitmapRefData::GetRawAccess() const
395 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
396 return m_memBuf
.GetData() ;
399 void *wxBitmapRefData::BeginRawAccess()
401 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
402 wxASSERT( m_rawAccessCount
== 0 ) ;
404 #if wxMAC_USE_CORE_GRAPHICS
405 // we must destroy an existing cached image, as
406 // the bitmap data may change now
409 CGImageRelease( m_cgImageRef
) ;
410 m_cgImageRef
= NULL
;
413 return m_memBuf
.GetData() ;
416 void wxBitmapRefData::EndRawAccess()
418 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
419 wxASSERT( m_rawAccessCount
== 1 ) ;
421 #if !wxMAC_USE_CORE_GRAPHICS
427 #if wxMAC_USE_CORE_GRAPHICS
428 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
430 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
431 wxASSERT( data
== membuf
->GetData() ) ;
435 CGImageRef
wxBitmapRefData::CGImageCreate() const
438 wxASSERT( m_rawAccessCount
>= 0 ) ;
440 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
442 size_t imageSize
= m_width
* m_height
* 4 ;
443 void * dataBuffer
= m_memBuf
.GetData() ;
446 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
447 wxMemoryBuffer
* membuf
= NULL
;
451 membuf
= new wxMemoryBuffer( imageSize
) ;
452 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
453 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
454 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
455 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
456 alphaInfo
= kCGImageAlphaFirst
;
457 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
459 unsigned char *sourcemask
= sourcemaskstart
;
460 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
462 *destalpha
= *sourcemask
;
466 else if ( m_hasAlpha
)
468 #if wxMAC_USE_PREMULTIPLIED_ALPHA
469 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
471 alphaInfo
= kCGImageAlphaFirst
;
473 membuf
= new wxMemoryBuffer( m_memBuf
) ;
477 membuf
= new wxMemoryBuffer( m_memBuf
) ;
479 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
480 CGDataProviderRef dataProvider
=
481 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
482 wxMacMemoryBufferReleaseProc
);
484 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
485 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
486 CGDataProviderRelease( dataProvider
);
490 image
= m_cgImageRef
;
491 CGImageRetain( image
) ;
493 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
495 // we keep it for later use
496 m_cgImageRef
= image
;
497 CGImageRetain( image
) ;
503 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
505 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
510 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
511 else if ( m_hasAlpha
)
513 #if !wxMAC_USE_CORE_GRAPHICS
514 if ( m_rawAccessCount
> 0 )
517 // this structure is not kept in synch when using CG, so if someone
518 // is really accessing the Graphports, we have to sync it
521 *mask
= m_hMaskBitmap
;
527 void wxBitmapRefData::UpdateAlphaMask() const
531 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
532 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
534 int h
= GetHeight() ;
537 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
539 unsigned char* destalpha
= destalphabase
;
540 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
542 // we must have 24 bit depth for non quartz smooth alpha
544 *destalpha
++ = 255 - *sourcemask
;
545 *destalpha
++ = 255 - *sourcemask
;
546 *destalpha
++ = 255 - *sourcemask
;
552 void wxBitmapRefData::Free()
554 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
556 #if wxMAC_USE_CORE_GRAPHICS
559 CGImageRelease( m_cgImageRef
) ;
560 m_cgImageRef
= NULL
;
565 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
570 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
571 m_hMaskBitmap
= NULL
;
581 wxBitmapRefData::~wxBitmapRefData()
586 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
588 int w
= icon
.GetWidth() ;
589 int h
= icon
.GetHeight() ;
590 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
592 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
594 IconFamilyHandle iconFamily
= NULL
;
595 Handle imagehandle
= NewHandle(0) ;
596 Handle maskhandle
= NewHandle(0) ;
600 IconSelectorValue selector
= 0 ;
603 dataType
= kThumbnail32BitData
;
604 maskType
= kThumbnail8BitMask
;
605 selector
= kSelectorAllAvailableData
;
609 dataType
= kHuge32BitData
;
610 maskType
= kHuge8BitMask
;
611 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
615 dataType
= kLarge32BitData
;
616 maskType
= kLarge8BitMask
;
617 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
621 dataType
= kSmall32BitData
;
622 maskType
= kSmall8BitMask
;
623 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
627 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
631 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
633 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
634 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
635 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
636 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
638 unsigned char *source
= (unsigned char *) *imagehandle
;
639 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
641 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
642 for ( int y
= 0 ; y
< h
; ++y
)
644 for ( int x
= 0 ; x
< w
; ++x
)
646 *destination
++ = *sourcemask
++ ;
648 *destination
++ = *source
++ ;
649 *destination
++ = *source
++ ;
650 *destination
++ = *source
++ ;
654 DisposeHandle( imagehandle
) ;
655 DisposeHandle( maskhandle
) ;
660 dc
.SelectObject( *this ) ;
661 dc
.DrawIcon( icon
, 0 , 0 ) ;
662 dc
.SelectObject( wxNullBitmap
) ;
671 wxBitmap::~wxBitmap()
675 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
677 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
681 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
682 if ( the_width
% (sizeof(unsigned char) * 8) ) {
683 linesize
+= sizeof(unsigned char);
685 unsigned char* linestart
= (unsigned char*) bits
;
686 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
687 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
689 for ( int x
= 0 ; x
< the_width
; ++x
)
693 int mask
= 1 << bit
;
694 if ( linestart
[index
] & mask
)
696 *destination
++ = 0xFF ;
703 *destination
++ = 0xFF ;
704 *destination
++ = 0xFF ;
705 *destination
++ = 0xFF ;
706 *destination
++ = 0xFF ;
714 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
718 wxBitmap::wxBitmap(int w
, int h
, int d
)
720 (void)Create(w
, h
, d
);
723 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
725 (void) Create(data
, type
, width
, height
, depth
);
728 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
730 LoadFile(filename
, type
);
733 wxBitmap::wxBitmap(const char **bits
)
735 (void) CreateFromXpm(bits
);
738 wxBitmap::wxBitmap(char **bits
)
740 (void) CreateFromXpm((const char **)bits
);
743 void* wxBitmap::GetRawAccess() const
745 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
746 return M_BITMAPDATA
->GetRawAccess() ;
749 void* wxBitmap::BeginRawAccess()
751 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
752 return M_BITMAPDATA
->BeginRawAccess() ;
755 void wxBitmap::EndRawAccess()
757 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
758 M_BITMAPDATA
->EndRawAccess() ;
761 bool wxBitmap::CreateFromXpm(const char **bits
)
763 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
764 wxXPMDecoder decoder
;
765 wxImage img
= decoder
.ReadData(bits
);
766 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
767 *this = wxBitmap(img
);
771 #if wxMAC_USE_CORE_GRAPHICS
772 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
774 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
775 return M_BITMAPDATA
->CGImageCreate() ;
779 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
782 (rect
.x
>= 0) && (rect
.y
>= 0) &&
783 (rect
.x
+rect
.width
<= GetWidth()) &&
784 (rect
.y
+rect
.height
<= GetHeight()),
785 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
788 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
789 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
792 int sourcewidth
= GetWidth() ;
793 int destwidth
= rect
.width
;
794 int destheight
= rect
.height
;
796 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
797 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
798 int sourcelinesize
= sourcewidth
* 4 ;
799 int destlinesize
= destwidth
* 4 ;
800 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
801 unsigned char *dest
= destdata
;
802 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
804 memcpy( dest
, source
, destlinesize
) ;
809 if ( M_BITMAPDATA
->m_bitmapMask
)
811 wxMemoryBuffer maskbuf
;
812 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
813 size_t maskbufsize
= rowBytes
* destheight
;
814 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
816 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
817 int destlinesize
= rowBytes
;
818 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
819 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
820 unsigned char *dest
= destdata
;
822 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
824 memcpy( dest
, source
, destlinesize
) ;
826 maskbuf
.UngetWriteBuf( maskbufsize
) ;
827 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
829 else if ( HasAlpha() )
835 bool wxBitmap::Create(int w
, int h
, int d
)
840 d
= wxDisplayDepth() ;
842 m_refData
= new wxBitmapRefData( w
, h
, d
);
844 return M_BITMAPDATA
->Ok() ;
847 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
851 wxBitmapHandler
*handler
= FindHandler(type
);
855 m_refData
= new wxBitmapRefData
;
857 return handler
->LoadFile(this, filename
, type
, -1, -1);
861 wxImage
loadimage(filename
, type
);
862 if (loadimage
.Ok()) {
867 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
871 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
875 m_refData
= new wxBitmapRefData
;
877 wxBitmapHandler
*handler
= FindHandler(type
);
879 if ( handler
== NULL
) {
880 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
885 return handler
->Create(this, data
, type
, width
, height
, depth
);
888 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
890 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
892 // width and height of the device-dependent bitmap
893 int width
= image
.GetWidth();
894 int height
= image
.GetHeight();
896 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
900 bool hasAlpha
= false ;
902 if ( image
.HasMask() )
904 // takes precedence, don't mix with alpha info
908 hasAlpha
= image
.HasAlpha() ;
914 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
916 register unsigned char* data
= image
.GetData();
917 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
918 for (int y
= 0; y
< height
; y
++)
920 for (int x
= 0; x
< width
; x
++)
924 const unsigned char a
= *alpha
++;
926 #if wxMAC_USE_PREMULTIPLIED_ALPHA
927 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
928 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
929 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
931 *destination
++ = *data
++ ;
932 *destination
++ = *data
++ ;
933 *destination
++ = *data
++ ;
938 *destination
++ = 0xFF ;
939 *destination
++ = *data
++ ;
940 *destination
++ = *data
++ ;
941 *destination
++ = *data
++ ;
946 if ( image
.HasMask() )
948 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
952 wxImage
wxBitmap::ConvertToImage() const
956 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
958 // create an wxImage object
959 int width
= GetWidth();
960 int height
= GetHeight();
961 image
.Create( width
, height
);
963 unsigned char *data
= image
.GetData();
964 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
966 unsigned char* source
= (unsigned char*) GetRawAccess() ;
968 bool hasAlpha
= false ;
969 bool hasMask
= false ;
970 unsigned char *alpha
= NULL
;
971 unsigned char *mask
= NULL
;
980 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
986 alpha
= image
.GetAlpha() ;
990 // The following masking algorithm is the same as well in msw/gtk:
991 // the colour used as transparent one in wxImage and the one it is
992 // replaced with when it really occurs in the bitmap
993 static const int MASK_RED
= 1;
994 static const int MASK_GREEN
= 2;
995 static const int MASK_BLUE
= 3;
996 static const int MASK_BLUE_REPLACEMENT
= 2;
998 for (int yy
= 0; yy
< height
; yy
++)
1000 for (int xx
= 0; xx
< width
; xx
++)
1002 long color
= *((long*) source
) ;
1003 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1004 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1005 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1006 unsigned char b
= (color
&0x000000FF);
1011 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1012 b
= MASK_BLUE_REPLACEMENT
;
1021 else if ( hasAlpha
)
1025 data
[index
+ 1] = g
;
1026 data
[index
+ 2] = b
;
1032 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1037 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1038 const wxPalette
*palette
) const
1040 wxBitmapHandler
*handler
= FindHandler(type
);
1044 return handler
->SaveFile(this, filename
, type
, palette
);
1048 wxImage image
= ConvertToImage();
1050 return image
.SaveFile(filename
, type
);
1053 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1057 bool wxBitmap::Ok() const
1059 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1062 int wxBitmap::GetHeight() const
1064 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1066 return M_BITMAPDATA
->GetHeight();
1069 int wxBitmap::GetWidth() const
1071 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1073 return M_BITMAPDATA
->GetWidth() ;
1076 int wxBitmap::GetDepth() const
1078 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1080 return M_BITMAPDATA
->GetDepth();
1083 #if WXWIN_COMPATIBILITY_2_4
1085 int wxBitmap::GetQuality() const
1092 wxMask
*wxBitmap::GetMask() const
1094 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1096 return M_BITMAPDATA
->m_bitmapMask
;
1099 bool wxBitmap::HasAlpha() const
1101 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1103 return M_BITMAPDATA
->HasAlpha() ;
1106 void wxBitmap::SetWidth(int w
)
1109 m_refData
= new wxBitmapRefData
;
1111 M_BITMAPDATA
->SetWidth(w
);
1114 void wxBitmap::SetHeight(int h
)
1117 m_refData
= new wxBitmapRefData
;
1119 M_BITMAPDATA
->SetHeight(h
);
1122 void wxBitmap::SetDepth(int d
)
1125 m_refData
= new wxBitmapRefData
;
1127 M_BITMAPDATA
->SetDepth(d
);
1130 #if WXWIN_COMPATIBILITY_2_4
1132 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1138 void wxBitmap::SetOk(bool isOk
)
1141 m_refData
= new wxBitmapRefData
;
1143 M_BITMAPDATA
->SetOk(isOk
);
1147 wxPalette
*wxBitmap::GetPalette() const
1149 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1151 return &M_BITMAPDATA
->m_bitmapPalette
;
1154 void wxBitmap::SetPalette(const wxPalette
& palette
)
1157 m_refData
= new wxBitmapRefData
;
1159 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1161 #endif // wxUSE_PALETTE
1163 void wxBitmap::SetMask(wxMask
*mask
)
1166 m_refData
= new wxBitmapRefData
;
1168 // Remove existing mask if there is one.
1169 delete M_BITMAPDATA
->m_bitmapMask
;
1171 M_BITMAPDATA
->m_bitmapMask
= mask
;
1174 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1176 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1179 // ----------------------------------------------------------------------------
1181 // ----------------------------------------------------------------------------
1188 // Construct a mask from a bitmap and a colour indicating
1189 // the transparent area
1190 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1193 Create(bitmap
, colour
);
1196 // Construct a mask from a mono bitmap (copies the bitmap).
1197 wxMask::wxMask(const wxBitmap
& bitmap
)
1203 // Construct a mask from a mono bitmap (copies the bitmap).
1204 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1207 Create(data
, width
, height
, bytesPerRow
);
1214 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1215 m_maskBitmap
= NULL
;
1221 m_width
= m_height
= m_bytesPerRow
= 0 ;
1222 m_maskBitmap
= NULL
;
1225 void *wxMask::GetRawAccess() const
1227 return m_memBuf
.GetData() ;
1230 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1231 // so no rainbox colors will be created by QD when blitting
1233 void wxMask::RealizeNative()
1237 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1238 m_maskBitmap
= NULL
;
1240 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1241 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1242 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1245 // Create a mask from a mono bitmap (copies the bitmap).
1246 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1251 m_bytesPerRow
= bytesPerRow
;
1252 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1257 // Create a mask from a mono bitmap (copies the bitmap).
1258 bool wxMask::Create(const wxBitmap
& bitmap
)
1260 m_width
= bitmap
.GetWidth() ;
1261 m_height
= bitmap
.GetHeight() ;
1262 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1263 size_t size
= m_bytesPerRow
* m_height
;
1264 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1265 memset( destdatabase
, 0 , size
) ;
1266 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1267 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1269 unsigned char *destdata
= destdatabase
;
1270 for( int x
= 0 ; x
< m_width
; ++x
)
1273 unsigned char r
= *srcdata
++ ;
1274 unsigned char g
= *srcdata
++ ;
1275 unsigned char b
= *srcdata
++ ;
1276 if ( ( r
+ g
+ b
) > 0x10 )
1277 *destdata
++ = 0x00 ;
1279 *destdata
++ = 0xFF ;
1282 m_memBuf
.UngetWriteBuf( size
) ;
1287 // Create a mask from a bitmap and a colour indicating
1288 // the transparent area
1289 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
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
;
1296 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1297 memset( destdatabase
, 0 , size
) ;
1298 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1299 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1301 unsigned char *destdata
= destdatabase
;
1302 for( int x
= 0 ; x
< m_width
; ++x
)
1305 unsigned char r
= *srcdata
++ ;
1306 unsigned char g
= *srcdata
++ ;
1307 unsigned char b
= *srcdata
++ ;
1308 if ( colour
== wxColour( r
, g
, b
) )
1309 *destdata
++ = 0x00 ;
1311 *destdata
++ = 0xFF ;
1314 m_memBuf
.UngetWriteBuf( size
) ;
1319 WXHBITMAP
wxMask::GetHBITMAP() const
1321 return m_maskBitmap
;
1324 // ----------------------------------------------------------------------------
1326 // ----------------------------------------------------------------------------
1328 wxBitmapHandler::~wxBitmapHandler()
1332 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1337 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1338 int desiredWidth
, int desiredHeight
)
1343 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1348 // ----------------------------------------------------------------------------
1349 // Standard Handlers
1350 // ----------------------------------------------------------------------------
1352 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1354 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1356 inline wxPICTResourceHandler()
1358 SetName(wxT("Macintosh Pict resource"));
1359 SetExtension(wxEmptyString
);
1360 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1363 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1364 int desiredWidth
, int desiredHeight
);
1366 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1369 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1370 int desiredWidth
, int desiredHeight
)
1374 wxMacStringToPascal( name
, theName
) ;
1376 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1380 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1381 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1383 dc
.SelectObject( *bitmap
) ;
1385 dc
.SelectObject( wxNullBitmap
) ;
1388 #endif //wxUSE_METAFILE
1393 void wxBitmap::InitStandardHandlers()
1395 AddHandler(new wxPICTResourceHandler
) ;
1396 AddHandler(new wxICONResourceHandler
) ;
1399 // ----------------------------------------------------------------------------
1400 // raw bitmap access support
1401 // ----------------------------------------------------------------------------
1403 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1407 // no bitmap, no data (raw or otherwise)
1411 data
.m_width
= GetWidth() ;
1412 data
.m_height
= GetHeight() ;
1413 data
.m_stride
= GetWidth() * 4 ;
1414 return GetRawAccess() ;
1417 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1422 // TODO : if we have some information about the API we should check
1423 // this code looks strange...
1425 if ( M_BITMAPDATA
->HasAlpha() )
1427 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1429 int w
= data
.GetWidth(),
1430 h
= data
.GetHeight();
1432 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1433 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1434 wxAlphaPixelData::Iterator
pMask(dataMask
),
1436 for ( int y
= 0; y
< h
; y
++ )
1438 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1441 for ( int x
= 0; x
< w
; x
++ )
1443 const wxAlphaPixelData::Iterator::ChannelType
1446 pMask
.Red() = alpha
;
1447 pMask
.Green() = alpha
;
1448 pMask
.Blue() = alpha
;
1457 pMask
= rowStartMask
;
1458 pMask
.OffsetY(dataMask
, 1);
1461 SetMask(new wxMask(bmpMask
));
1465 void wxBitmap::UseAlpha()
1467 // remember that we are using alpha channel, we'll need to create a proper
1468 // mask in UngetRawData()
1469 M_BITMAPDATA
->UseAlpha( true );