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 ) ;
220 LockPixels( GetGWorldPixMap( monoworld
) ) ;
221 LockPixels( GetGWorldPixMap( mask
) ) ;
222 SetGWorld( monoworld
, NULL
) ;
223 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
224 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
225 RGBForeColor( &black
) ;
226 RGBBackColor( &white
) ;
227 CopyBits(GetPortBitMapForCopyBits(mask
),
228 GetPortBitMapForCopyBits(monoworld
),
232 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
233 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
234 UnlockPixels( GetGWorldPixMap( mask
) ) ;
235 DisposeGWorld( monoworld
) ;
238 SetGWorld( wp
, NULL
) ;
240 GetPortBounds( wp
, &portRect
) ;
241 pict
= OpenPicture(&portRect
);
245 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
246 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
247 RGBForeColor( &black
) ;
248 RGBBackColor( &white
) ;
253 LockPixels( GetGWorldPixMap( wp
) ) ;
254 CopyBits(GetPortBitMapForCopyBits(wp
),
255 GetPortBitMapForCopyBits(wp
),
259 UnlockPixels( GetGWorldPixMap( wp
) ) ;
262 SetGWorld( origPort
, origDev
) ;
264 DisposeRgn( clipRgn
) ;
269 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
271 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
274 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
277 info
->contentType
= kControlContentIconRef
;
278 info
->u
.iconRef
= wxMacCreateIconRef( bitmap
) ;
279 wxASSERT_MSG( info
->u
.iconRef
, wxT("Converting to IconRef not possible") ) ;
280 #if wxMAC_USE_CORE_GRAPHICS
282 // only on 10.4 more controls will accept a CGImage
284 info->contentType = kControlContentCGImageRef ;
285 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
291 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
293 if ( info
->contentType
== kControlContentIconRef
)
295 ReleaseIconRef(info
->u
.iconRef
) ;
297 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
298 else if ( info
->contentType
== kControlContentCGImageRef
)
300 CGImageRelease( info
->u
.imageRef
) ;
305 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
309 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
311 void wxBitmapRefData::Init()
317 m_bitmapMask
= NULL
;
318 #if wxMAC_USE_CORE_GRAPHICS
319 m_cgImageRef
= NULL
;
322 m_hMaskBitmap
= NULL
;
323 m_maskBytesPerRow
= NULL
;
325 m_rawAccessCount
= 0 ;
329 wxBitmapRefData::wxBitmapRefData()
334 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
337 Create( w
, h
, d
) ;
340 bool wxBitmapRefData::Create( int w
, int h
, int d
)
346 m_bytesPerRow
= w
* 4 ;
347 size_t size
= m_bytesPerRow
* h
;
348 void* data
= m_memBuf
.GetWriteBuf(size
) ;
349 memset( data
, 0 , size
) ;
350 m_memBuf
.UngetWriteBuf(size
) ;
353 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
354 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
355 (char*) data
, m_bytesPerRow
) ) ;
356 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
357 m_ok
= ( m_hBitmap
!= NULL
) ;
362 void wxBitmapRefData::UseAlpha( bool use
)
364 if ( m_hasAlpha
== use
)
370 int width
= GetWidth() ;
371 int height
= GetHeight() ;
372 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
373 size_t size
= height
* m_maskBytesPerRow
;
374 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
375 memset( data
, 0 , size
) ;
376 wxASSERT( m_hMaskBitmap
== NULL
) ;
377 Rect rect
= { 0 , 0 , height
, width
} ;
378 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
379 (char*) data
, m_maskBytesPerRow
) ) ;
380 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
381 m_maskMemBuf
.UngetWriteBuf(size
) ;
382 #if !wxMAC_USE_CORE_GRAPHICS
388 DisposeGWorld( m_hMaskBitmap
) ;
389 m_hMaskBitmap
= NULL
;
390 m_maskBytesPerRow
= 0 ;
394 void *wxBitmapRefData::GetRawAccess() const
396 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
397 return m_memBuf
.GetData() ;
400 void *wxBitmapRefData::BeginRawAccess()
402 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
403 wxASSERT( m_rawAccessCount
== 0 ) ;
405 #if wxMAC_USE_CORE_GRAPHICS
406 // we must destroy an existing cached image, as
407 // the bitmap data may change now
410 CGImageRelease( m_cgImageRef
) ;
411 m_cgImageRef
= NULL
;
414 return m_memBuf
.GetData() ;
417 void wxBitmapRefData::EndRawAccess()
419 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
420 wxASSERT( m_rawAccessCount
== 1 ) ;
422 #if !wxMAC_USE_CORE_GRAPHICS
428 #if wxMAC_USE_CORE_GRAPHICS
429 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
431 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
432 wxASSERT( data
== membuf
->GetData() ) ;
436 CGImageRef
wxBitmapRefData::CGImageCreate() const
439 wxASSERT( m_rawAccessCount
>= 0 ) ;
441 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
443 size_t imageSize
= m_width
* m_height
* 4 ;
444 void * dataBuffer
= m_memBuf
.GetData() ;
447 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
448 wxMemoryBuffer
* membuf
= NULL
;
452 membuf
= new wxMemoryBuffer( imageSize
) ;
453 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
454 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
455 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
456 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
457 alphaInfo
= kCGImageAlphaFirst
;
458 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
460 unsigned char *sourcemask
= sourcemaskstart
;
461 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
463 *destalpha
= *sourcemask
;
467 else if ( m_hasAlpha
)
469 #if wxMAC_USE_PREMULTIPLIED_ALPHA
470 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
472 alphaInfo
= kCGImageAlphaFirst
;
474 membuf
= new wxMemoryBuffer( m_memBuf
) ;
478 membuf
= new wxMemoryBuffer( m_memBuf
) ;
480 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
481 CGDataProviderRef dataProvider
=
482 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
483 wxMacMemoryBufferReleaseProc
);
485 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
486 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
487 CGDataProviderRelease( dataProvider
);
491 image
= m_cgImageRef
;
492 CGImageRetain( image
) ;
494 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
496 // we keep it for later use
497 m_cgImageRef
= image
;
498 CGImageRetain( image
) ;
504 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
506 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
511 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
512 else if ( m_hasAlpha
)
514 #if !wxMAC_USE_CORE_GRAPHICS
515 if ( m_rawAccessCount
> 0 )
518 // this structure is not kept in synch when using CG, so if someone
519 // is really accessing the Graphports, we have to sync it
522 *mask
= m_hMaskBitmap
;
528 void wxBitmapRefData::UpdateAlphaMask() const
532 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
533 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
535 int h
= GetHeight() ;
538 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
540 unsigned char* destalpha
= destalphabase
;
541 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
543 // we must have 24 bit depth for non quartz smooth alpha
545 *destalpha
++ = 255 - *sourcemask
;
546 *destalpha
++ = 255 - *sourcemask
;
547 *destalpha
++ = 255 - *sourcemask
;
553 void wxBitmapRefData::Free()
555 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
557 #if wxMAC_USE_CORE_GRAPHICS
560 CGImageRelease( m_cgImageRef
) ;
561 m_cgImageRef
= NULL
;
566 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
571 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
572 m_hMaskBitmap
= NULL
;
582 wxBitmapRefData::~wxBitmapRefData()
587 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
589 int w
= icon
.GetWidth() ;
590 int h
= icon
.GetHeight() ;
591 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
593 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
595 IconFamilyHandle iconFamily
= NULL
;
596 Handle imagehandle
= NewHandle(0) ;
597 Handle maskhandle
= NewHandle(0) ;
601 IconSelectorValue selector
= 0 ;
604 dataType
= kThumbnail32BitData
;
605 maskType
= kThumbnail8BitMask
;
606 selector
= kSelectorAllAvailableData
;
610 dataType
= kHuge32BitData
;
611 maskType
= kHuge8BitMask
;
612 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
616 dataType
= kLarge32BitData
;
617 maskType
= kLarge8BitMask
;
618 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
622 dataType
= kSmall32BitData
;
623 maskType
= kSmall8BitMask
;
624 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
628 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
632 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
634 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
635 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
636 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
637 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
639 unsigned char *source
= (unsigned char *) *imagehandle
;
640 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
642 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
643 for ( int y
= 0 ; y
< h
; ++y
)
645 for ( int x
= 0 ; x
< w
; ++x
)
647 *destination
++ = *sourcemask
++ ;
649 *destination
++ = *source
++ ;
650 *destination
++ = *source
++ ;
651 *destination
++ = *source
++ ;
655 DisposeHandle( imagehandle
) ;
656 DisposeHandle( maskhandle
) ;
661 dc
.SelectObject( *this ) ;
662 dc
.DrawIcon( icon
, 0 , 0 ) ;
663 dc
.SelectObject( wxNullBitmap
) ;
672 wxBitmap::~wxBitmap()
676 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
678 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
682 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
683 if ( the_width
% (sizeof(unsigned char) * 8) ) {
684 linesize
+= sizeof(unsigned char);
686 unsigned char* linestart
= (unsigned char*) bits
;
687 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
688 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
690 for ( int x
= 0 ; x
< the_width
; ++x
)
694 int mask
= 1 << bit
;
695 if ( linestart
[index
] & mask
)
697 *destination
++ = 0xFF ;
704 *destination
++ = 0xFF ;
705 *destination
++ = 0xFF ;
706 *destination
++ = 0xFF ;
707 *destination
++ = 0xFF ;
715 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
719 wxBitmap::wxBitmap(int w
, int h
, int d
)
721 (void)Create(w
, h
, d
);
724 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
726 (void) Create(data
, type
, width
, height
, depth
);
729 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
731 LoadFile(filename
, type
);
734 wxBitmap::wxBitmap(const char **bits
)
736 (void) CreateFromXpm(bits
);
739 wxBitmap::wxBitmap(char **bits
)
741 (void) CreateFromXpm((const char **)bits
);
744 void* wxBitmap::GetRawAccess() const
746 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
747 return M_BITMAPDATA
->GetRawAccess() ;
750 void* wxBitmap::BeginRawAccess()
752 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
753 return M_BITMAPDATA
->BeginRawAccess() ;
756 void wxBitmap::EndRawAccess()
758 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
759 M_BITMAPDATA
->EndRawAccess() ;
762 bool wxBitmap::CreateFromXpm(const char **bits
)
764 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
765 wxXPMDecoder decoder
;
766 wxImage img
= decoder
.ReadData(bits
);
767 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
768 *this = wxBitmap(img
);
772 #if wxMAC_USE_CORE_GRAPHICS
773 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
775 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
776 return M_BITMAPDATA
->CGImageCreate() ;
780 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
783 (rect
.x
>= 0) && (rect
.y
>= 0) &&
784 (rect
.x
+rect
.width
<= GetWidth()) &&
785 (rect
.y
+rect
.height
<= GetHeight()),
786 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
789 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
790 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
793 int sourcewidth
= GetWidth() ;
794 int destwidth
= rect
.width
;
795 int destheight
= rect
.height
;
797 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
798 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
799 int sourcelinesize
= sourcewidth
* 4 ;
800 int destlinesize
= destwidth
* 4 ;
801 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
802 unsigned char *dest
= destdata
;
803 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
805 memcpy( dest
, source
, destlinesize
) ;
810 if ( M_BITMAPDATA
->m_bitmapMask
)
812 wxMemoryBuffer maskbuf
;
813 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
814 size_t maskbufsize
= rowBytes
* destheight
;
815 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
817 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
818 int destlinesize
= rowBytes
;
819 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
820 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
821 unsigned char *dest
= destdata
;
823 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
825 memcpy( dest
, source
, destlinesize
) ;
827 maskbuf
.UngetWriteBuf( maskbufsize
) ;
828 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
830 else if ( HasAlpha() )
836 bool wxBitmap::Create(int w
, int h
, int d
)
841 d
= wxDisplayDepth() ;
843 m_refData
= new wxBitmapRefData( w
, h
, d
);
845 return M_BITMAPDATA
->Ok() ;
848 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
852 wxBitmapHandler
*handler
= FindHandler(type
);
856 m_refData
= new wxBitmapRefData
;
858 return handler
->LoadFile(this, filename
, type
, -1, -1);
862 wxImage
loadimage(filename
, type
);
863 if (loadimage
.Ok()) {
868 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
872 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
876 m_refData
= new wxBitmapRefData
;
878 wxBitmapHandler
*handler
= FindHandler(type
);
880 if ( handler
== NULL
) {
881 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
886 return handler
->Create(this, data
, type
, width
, height
, depth
);
889 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
891 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
893 // width and height of the device-dependent bitmap
894 int width
= image
.GetWidth();
895 int height
= image
.GetHeight();
897 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
901 bool hasAlpha
= false ;
903 if ( image
.HasMask() )
905 // takes precedence, don't mix with alpha info
909 hasAlpha
= image
.HasAlpha() ;
915 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
917 register unsigned char* data
= image
.GetData();
918 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
919 for (int y
= 0; y
< height
; y
++)
921 for (int x
= 0; x
< width
; x
++)
925 const unsigned char a
= *alpha
++;
927 #if wxMAC_USE_PREMULTIPLIED_ALPHA
928 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
929 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
930 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
932 *destination
++ = *data
++ ;
933 *destination
++ = *data
++ ;
934 *destination
++ = *data
++ ;
939 *destination
++ = 0xFF ;
940 *destination
++ = *data
++ ;
941 *destination
++ = *data
++ ;
942 *destination
++ = *data
++ ;
947 if ( image
.HasMask() )
949 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
953 wxImage
wxBitmap::ConvertToImage() const
957 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
959 // create an wxImage object
960 int width
= GetWidth();
961 int height
= GetHeight();
962 image
.Create( width
, height
);
964 unsigned char *data
= image
.GetData();
965 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
967 unsigned char* source
= (unsigned char*) GetRawAccess() ;
969 bool hasAlpha
= false ;
970 bool hasMask
= false ;
971 unsigned char *alpha
= NULL
;
972 unsigned char *mask
= NULL
;
981 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
987 alpha
= image
.GetAlpha() ;
991 // The following masking algorithm is the same as well in msw/gtk:
992 // the colour used as transparent one in wxImage and the one it is
993 // replaced with when it really occurs in the bitmap
994 static const int MASK_RED
= 1;
995 static const int MASK_GREEN
= 2;
996 static const int MASK_BLUE
= 3;
997 static const int MASK_BLUE_REPLACEMENT
= 2;
999 for (int yy
= 0; yy
< height
; yy
++)
1001 for (int xx
= 0; xx
< width
; xx
++)
1003 long color
= *((long*) source
) ;
1004 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1005 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1006 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1007 unsigned char b
= (color
&0x000000FF);
1012 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1013 b
= MASK_BLUE_REPLACEMENT
;
1022 else if ( hasAlpha
)
1026 data
[index
+ 1] = g
;
1027 data
[index
+ 2] = b
;
1033 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1038 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1039 const wxPalette
*palette
) const
1041 wxBitmapHandler
*handler
= FindHandler(type
);
1045 return handler
->SaveFile(this, filename
, type
, palette
);
1049 wxImage image
= ConvertToImage();
1051 return image
.SaveFile(filename
, type
);
1054 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1058 bool wxBitmap::Ok() const
1060 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1063 int wxBitmap::GetHeight() const
1065 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1067 return M_BITMAPDATA
->GetHeight();
1070 int wxBitmap::GetWidth() const
1072 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1074 return M_BITMAPDATA
->GetWidth() ;
1077 int wxBitmap::GetDepth() const
1079 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1081 return M_BITMAPDATA
->GetDepth();
1084 #if WXWIN_COMPATIBILITY_2_4
1086 int wxBitmap::GetQuality() const
1093 wxMask
*wxBitmap::GetMask() const
1095 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1097 return M_BITMAPDATA
->m_bitmapMask
;
1100 bool wxBitmap::HasAlpha() const
1102 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1104 return M_BITMAPDATA
->HasAlpha() ;
1107 void wxBitmap::SetWidth(int w
)
1110 m_refData
= new wxBitmapRefData
;
1112 M_BITMAPDATA
->SetWidth(w
);
1115 void wxBitmap::SetHeight(int h
)
1118 m_refData
= new wxBitmapRefData
;
1120 M_BITMAPDATA
->SetHeight(h
);
1123 void wxBitmap::SetDepth(int d
)
1126 m_refData
= new wxBitmapRefData
;
1128 M_BITMAPDATA
->SetDepth(d
);
1131 #if WXWIN_COMPATIBILITY_2_4
1133 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1139 void wxBitmap::SetOk(bool isOk
)
1142 m_refData
= new wxBitmapRefData
;
1144 M_BITMAPDATA
->SetOk(isOk
);
1148 wxPalette
*wxBitmap::GetPalette() const
1150 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1152 return &M_BITMAPDATA
->m_bitmapPalette
;
1155 void wxBitmap::SetPalette(const wxPalette
& palette
)
1158 m_refData
= new wxBitmapRefData
;
1160 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1162 #endif // wxUSE_PALETTE
1164 void wxBitmap::SetMask(wxMask
*mask
)
1167 m_refData
= new wxBitmapRefData
;
1169 // Remove existing mask if there is one.
1170 delete M_BITMAPDATA
->m_bitmapMask
;
1172 M_BITMAPDATA
->m_bitmapMask
= mask
;
1175 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1177 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1180 // ----------------------------------------------------------------------------
1182 // ----------------------------------------------------------------------------
1189 // Construct a mask from a bitmap and a colour indicating
1190 // the transparent area
1191 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1194 Create(bitmap
, colour
);
1197 // Construct a mask from a mono bitmap (copies the bitmap).
1198 wxMask::wxMask(const wxBitmap
& bitmap
)
1204 // Construct a mask from a mono bitmap (copies the bitmap).
1205 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1208 Create(data
, width
, height
, bytesPerRow
);
1215 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1216 m_maskBitmap
= NULL
;
1222 m_width
= m_height
= m_bytesPerRow
= 0 ;
1223 m_maskBitmap
= NULL
;
1226 void *wxMask::GetRawAccess() const
1228 return m_memBuf
.GetData() ;
1231 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1232 // so no rainbox colors will be created by QD when blitting
1234 void wxMask::RealizeNative()
1238 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1239 m_maskBitmap
= NULL
;
1241 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1242 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1243 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1246 // Create a mask from a mono bitmap (copies the bitmap).
1247 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1252 m_bytesPerRow
= bytesPerRow
;
1253 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1258 // Create a mask from a mono bitmap (copies the bitmap).
1259 bool wxMask::Create(const wxBitmap
& bitmap
)
1261 m_width
= bitmap
.GetWidth() ;
1262 m_height
= bitmap
.GetHeight() ;
1263 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1264 size_t size
= m_bytesPerRow
* m_height
;
1265 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1266 memset( destdatabase
, 0 , size
) ;
1267 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1268 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1270 unsigned char *destdata
= destdatabase
;
1271 for( int x
= 0 ; x
< m_width
; ++x
)
1274 unsigned char r
= *srcdata
++ ;
1275 unsigned char g
= *srcdata
++ ;
1276 unsigned char b
= *srcdata
++ ;
1277 if ( ( r
+ g
+ b
) > 0x10 )
1278 *destdata
++ = 0x00 ;
1280 *destdata
++ = 0xFF ;
1283 m_memBuf
.UngetWriteBuf( size
) ;
1288 // Create a mask from a bitmap and a colour indicating
1289 // the transparent area
1290 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1292 m_width
= bitmap
.GetWidth() ;
1293 m_height
= bitmap
.GetHeight() ;
1294 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1295 size_t size
= m_bytesPerRow
* m_height
;
1297 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1298 memset( destdatabase
, 0 , size
) ;
1299 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1300 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1302 unsigned char *destdata
= destdatabase
;
1303 for( int x
= 0 ; x
< m_width
; ++x
)
1306 unsigned char r
= *srcdata
++ ;
1307 unsigned char g
= *srcdata
++ ;
1308 unsigned char b
= *srcdata
++ ;
1309 if ( colour
== wxColour( r
, g
, b
) )
1310 *destdata
++ = 0x00 ;
1312 *destdata
++ = 0xFF ;
1315 m_memBuf
.UngetWriteBuf( size
) ;
1320 WXHBITMAP
wxMask::GetHBITMAP() const
1322 return m_maskBitmap
;
1325 // ----------------------------------------------------------------------------
1327 // ----------------------------------------------------------------------------
1329 wxBitmapHandler::~wxBitmapHandler()
1333 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1338 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1339 int desiredWidth
, int desiredHeight
)
1344 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1349 // ----------------------------------------------------------------------------
1350 // Standard Handlers
1351 // ----------------------------------------------------------------------------
1353 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1355 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1357 inline wxPICTResourceHandler()
1359 SetName(wxT("Macintosh Pict resource"));
1360 SetExtension(wxEmptyString
);
1361 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1364 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1365 int desiredWidth
, int desiredHeight
);
1367 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1370 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1371 int desiredWidth
, int desiredHeight
)
1375 wxMacStringToPascal( name
, theName
) ;
1377 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1381 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1382 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1384 dc
.SelectObject( *bitmap
) ;
1386 dc
.SelectObject( wxNullBitmap
) ;
1389 #endif //wxUSE_METAFILE
1394 void wxBitmap::InitStandardHandlers()
1396 AddHandler(new wxPICTResourceHandler
) ;
1397 AddHandler(new wxICONResourceHandler
) ;
1400 // ----------------------------------------------------------------------------
1401 // raw bitmap access support
1402 // ----------------------------------------------------------------------------
1404 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1408 // no bitmap, no data (raw or otherwise)
1412 data
.m_width
= GetWidth() ;
1413 data
.m_height
= GetHeight() ;
1414 data
.m_stride
= GetWidth() * 4 ;
1415 return GetRawAccess() ;
1418 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1423 // TODO : if we have some information about the API we should check
1424 // this code looks strange...
1426 if ( M_BITMAPDATA
->HasAlpha() )
1428 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1430 int w
= data
.GetWidth(),
1431 h
= data
.GetHeight();
1433 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1434 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1435 wxAlphaPixelData::Iterator
pMask(dataMask
),
1437 for ( int y
= 0; y
< h
; y
++ )
1439 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1442 for ( int x
= 0; x
< w
; x
++ )
1444 const wxAlphaPixelData::Iterator::ChannelType
1447 pMask
.Red() = alpha
;
1448 pMask
.Green() = alpha
;
1449 pMask
.Blue() = alpha
;
1458 pMask
= rowStartMask
;
1459 pMask
.OffsetY(dataMask
, 1);
1462 SetMask(new wxMask(bmpMask
));
1466 void wxBitmap::UseAlpha()
1468 // remember that we are using alpha channel, we'll need to create a proper
1469 // mask in UngetRawData()
1470 M_BITMAPDATA
->UseAlpha( true );