1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bitmap.h"
16 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
22 #include "wx/metafile.h"
23 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 #include "wx/dcmemory.h"
41 // Implementation Notes
42 // --------------------
44 // we are always working with a 32 bit deep pixel buffer
45 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
46 // therefore we have a separate GWorld there for blitting the mask in
48 // under Quartz then content is transformed into a CGImageRef representing the same data
49 // which can be transferred to the GPU by the OS for fast rendering
51 // we don't dare premultiplied alpha yet
52 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
54 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
56 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
59 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
63 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
67 wxBitmapRefData
* bmp
= bmap
;
69 if ( !bmap
->HasNativeSize() )
71 // as PICT conversion will only result in a 16x16 icon, let's attempt
72 // a few scales for better results
74 int w
= bitmap
.GetWidth() ;
75 int h
= bitmap
.GetHeight() ;
76 int sz
= wxMax( w
, h
) ;
77 if ( sz
== 24 || sz
== 64)
79 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
80 bmp
= scaleBmp
.GetBitmapData() ;
84 info
->contentType
= kControlContentIconRef
;
85 info
->u
.iconRef
= bmp
->GetIconRef() ;
86 AcquireIconRef( info
->u
.iconRef
) ;
88 #if wxMAC_USE_CORE_GRAPHICS
89 else if ( forceType
== kControlContentCGImageRef
)
91 info
->contentType
= kControlContentCGImageRef
;
92 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
97 info
->contentType
= kControlContentPictHandle
;
98 info
->u
.picture
= bmap
->GetPictHandle() ;
103 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
105 if ( info
->contentType
== kControlContentIconRef
)
107 ReleaseIconRef( info
->u
.iconRef
) ;
109 else if ( info
->contentType
== kControlContentPictHandle
)
111 // owned by the bitmap, no release here
113 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
114 else if ( info
->contentType
== kControlContentCGImageRef
)
116 CGImageRelease( info
->u
.imageRef
) ;
121 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
125 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
127 void wxBitmapRefData::Init()
133 m_bitmapMask
= NULL
;
134 #if wxMAC_USE_CORE_GRAPHICS
135 m_cgImageRef
= NULL
;
138 m_pictHandle
= NULL
;
140 m_hMaskBitmap
= NULL
;
141 m_maskBytesPerRow
= NULL
;
143 m_rawAccessCount
= 0 ;
147 wxBitmapRefData::wxBitmapRefData()
152 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
155 Create( w
, h
, d
) ;
158 bool wxBitmapRefData::Create( int w
, int h
, int d
)
164 m_bytesPerRow
= w
* 4 ;
165 size_t size
= m_bytesPerRow
* h
;
166 void* data
= m_memBuf
.GetWriteBuf(size
) ;
167 memset( data
, 0 , size
) ;
168 m_memBuf
.UngetWriteBuf(size
) ;
171 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
172 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
173 (char*) data
, m_bytesPerRow
) ) ;
174 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
175 m_ok
= ( m_hBitmap
!= NULL
) ;
180 void wxBitmapRefData::UseAlpha( bool use
)
182 if ( m_hasAlpha
== use
)
188 int width
= GetWidth() ;
189 int height
= GetHeight() ;
190 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
191 size_t size
= height
* m_maskBytesPerRow
;
192 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
193 memset( data
, 0 , size
) ;
194 wxASSERT( m_hMaskBitmap
== NULL
) ;
195 Rect rect
= { 0 , 0 , height
, width
} ;
196 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
197 (char*) data
, m_maskBytesPerRow
) ) ;
198 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
199 m_maskMemBuf
.UngetWriteBuf(size
) ;
200 #if !wxMAC_USE_CORE_GRAPHICS
206 DisposeGWorld( m_hMaskBitmap
) ;
207 m_hMaskBitmap
= NULL
;
208 m_maskBytesPerRow
= 0 ;
212 void *wxBitmapRefData::GetRawAccess() const
214 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
215 return m_memBuf
.GetData() ;
218 void *wxBitmapRefData::BeginRawAccess()
220 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
221 wxASSERT( m_rawAccessCount
== 0 ) ;
223 // we must destroy an existing cached image, as
224 // the bitmap data may change now
225 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
226 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
227 #if wxMAC_USE_CORE_GRAPHICS
230 CGImageRelease( m_cgImageRef
) ;
231 m_cgImageRef
= NULL
;
234 return m_memBuf
.GetData() ;
237 void wxBitmapRefData::EndRawAccess()
239 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
240 wxASSERT( m_rawAccessCount
== 1 ) ;
242 #if !wxMAC_USE_CORE_GRAPHICS
247 bool wxBitmapRefData::HasNativeSize()
250 int h
= GetHeight() ;
251 int sz
= wxMax( w
, h
) ;
253 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
258 IconRef
wxBitmapRefData::GetIconRef()
260 if ( m_iconRef
== NULL
)
262 // Create Icon Family Handle
264 IconFamilyHandle iconFamily
= NULL
;
266 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
267 (**iconFamily
).resourceType
= kIconFamilyType
;
268 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
271 int h
= GetHeight() ;
272 int sz
= wxMax( w
, h
) ;
274 OSType dataType
= 0 ;
275 OSType maskType
= 0 ;
279 dataType
= kThumbnail32BitData
;
280 maskType
= kThumbnail8BitMask
;
284 dataType
= kHuge32BitData
;
285 maskType
= kHuge8BitMask
;
289 dataType
= kLarge32BitData
;
290 maskType
= kLarge8BitMask
;
294 dataType
= kSmall32BitData
;
295 maskType
= kSmall8BitMask
;
300 // setup the header properly
303 Handle maskdata
= NULL
;
304 unsigned char * maskptr
= NULL
;
305 unsigned char * ptr
= NULL
;
310 data
= NewHandle( size
) ;
312 ptr
= (unsigned char*) *data
;
313 memset( ptr
, 0, size
) ;
316 maskdata
= NewHandle( masksize
) ;
318 maskptr
= (unsigned char*) *maskdata
;
319 memset( maskptr
, 0 , masksize
) ;
321 bool hasAlpha
= HasAlpha() ;
322 wxMask
*mask
= m_bitmapMask
;
323 unsigned char * source
= (unsigned char*) GetRawAccess() ;
324 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
325 for ( int y
= 0 ; y
< h
; ++y
)
327 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
328 unsigned char * maskdest
= maskptr
+ y
* sz
;
329 for ( int x
= 0 ; x
< w
; ++x
)
331 unsigned char a
= *source
++ ;
332 unsigned char r
= *source
++ ;
333 unsigned char g
= *source
++ ;
334 unsigned char b
= *source
++ ;
342 *maskdest
++ = *masksource
++ ;
350 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
351 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
353 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
354 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
356 HUnlock( maskdata
) ;
357 DisposeHandle( data
) ;
358 DisposeHandle( maskdata
) ;
362 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
363 (**iconFamily
).resourceType
= kIconFamilyType
;
364 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
365 PicHandle pic
= GetPictHandle() ;
366 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
369 // transform into IconRef
371 static int iconCounter
= 2 ;
372 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
373 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
374 // we have to retain a reference, as Unregister will decrement it
375 AcquireIconRef( m_iconRef
) ;
376 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
377 DisposeHandle( (Handle
) iconFamily
) ;
383 PicHandle
wxBitmapRefData::GetPictHandle()
385 if ( m_pictHandle
== NULL
)
387 CGrafPtr origPort
= NULL
;
388 GDHandle origDev
= NULL
;
389 GWorldPtr wp
= NULL
;
390 GWorldPtr mask
= NULL
;
391 int height
= GetHeight() ;
392 int width
= GetWidth() ;
394 Rect rect
= { 0 , 0 , height
, width
} ;
396 GetGWorld( &origPort
, &origDev
) ;
398 wp
= GetHBITMAP( &mask
) ;
400 RgnHandle clipRgn
= NULL
;
404 GWorldPtr monoworld
;
406 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
408 LockPixels( GetGWorldPixMap( monoworld
) ) ;
409 LockPixels( GetGWorldPixMap( mask
) ) ;
410 SetGWorld( monoworld
, NULL
) ;
411 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
412 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
413 RGBForeColor( &black
) ;
414 RGBBackColor( &white
) ;
415 CopyBits(GetPortBitMapForCopyBits(mask
),
416 GetPortBitMapForCopyBits(monoworld
),
420 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
421 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
422 UnlockPixels( GetGWorldPixMap( mask
) ) ;
423 DisposeGWorld( monoworld
) ;
426 SetGWorld( wp
, NULL
) ;
428 GetPortBounds( wp
, &portRect
) ;
429 m_pictHandle
= OpenPicture(&portRect
);
433 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
434 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
435 RGBForeColor( &black
) ;
436 RGBBackColor( &white
) ;
441 LockPixels( GetGWorldPixMap( wp
) ) ;
442 CopyBits(GetPortBitMapForCopyBits(wp
),
443 GetPortBitMapForCopyBits(wp
),
447 UnlockPixels( GetGWorldPixMap( wp
) ) ;
450 SetGWorld( origPort
, origDev
) ;
452 DisposeRgn( clipRgn
) ;
454 return m_pictHandle
;
457 #if wxMAC_USE_CORE_GRAPHICS
458 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
460 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
461 wxASSERT( data
== membuf
->GetData() ) ;
465 CGImageRef
wxBitmapRefData::CGImageCreate() const
468 wxASSERT( m_rawAccessCount
>= 0 ) ;
470 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
472 size_t imageSize
= m_width
* m_height
* 4 ;
473 void * dataBuffer
= m_memBuf
.GetData() ;
476 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
477 wxMemoryBuffer
* membuf
= NULL
;
481 membuf
= new wxMemoryBuffer( imageSize
) ;
482 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
483 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
484 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
485 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
486 alphaInfo
= kCGImageAlphaFirst
;
487 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
489 unsigned char *sourcemask
= sourcemaskstart
;
490 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
492 *destalpha
= *sourcemask
;
496 else if ( m_hasAlpha
)
498 #if wxMAC_USE_PREMULTIPLIED_ALPHA
499 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
501 alphaInfo
= kCGImageAlphaFirst
;
503 membuf
= new wxMemoryBuffer( m_memBuf
) ;
507 membuf
= new wxMemoryBuffer( m_memBuf
) ;
509 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
510 CGDataProviderRef dataProvider
=
511 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
512 wxMacMemoryBufferReleaseProc
);
514 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
515 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
516 CGDataProviderRelease( dataProvider
);
520 image
= m_cgImageRef
;
521 CGImageRetain( image
) ;
523 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
525 // we keep it for later use
526 m_cgImageRef
= image
;
527 CGImageRetain( image
) ;
533 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
535 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
540 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
541 else if ( m_hasAlpha
)
543 #if !wxMAC_USE_CORE_GRAPHICS
544 if ( m_rawAccessCount
> 0 )
547 // this structure is not kept in synch when using CG, so if someone
548 // is really accessing the Graphports, we have to sync it
551 *mask
= m_hMaskBitmap
;
557 void wxBitmapRefData::UpdateAlphaMask() const
561 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
562 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
564 int h
= GetHeight() ;
567 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
569 unsigned char* destalpha
= destalphabase
;
570 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
572 // we must have 24 bit depth for non quartz smooth alpha
574 *destalpha
++ = 255 - *sourcemask
;
575 *destalpha
++ = 255 - *sourcemask
;
576 *destalpha
++ = 255 - *sourcemask
;
582 void wxBitmapRefData::Free()
584 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
586 #if wxMAC_USE_CORE_GRAPHICS
589 CGImageRelease( m_cgImageRef
) ;
590 m_cgImageRef
= NULL
;
595 ReleaseIconRef( m_iconRef
) ;
600 KillPicture( m_pictHandle
) ;
601 m_pictHandle
= NULL
;
605 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
610 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
611 m_hMaskBitmap
= NULL
;
621 wxBitmapRefData::~wxBitmapRefData()
626 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
628 int w
= icon
.GetWidth() ;
629 int h
= icon
.GetHeight() ;
632 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
634 bool created
= false ;
636 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
638 IconFamilyHandle iconFamily
= NULL
;
639 Handle imagehandle
= NewHandle(0) ;
640 Handle maskhandle
= NewHandle(0) ;
644 IconSelectorValue selector
= 0 ;
647 dataType
= kThumbnail32BitData
;
648 maskType
= kThumbnail8BitMask
;
649 selector
= kSelectorAllAvailableData
;
653 dataType
= kHuge32BitData
;
654 maskType
= kHuge8BitMask
;
655 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
659 dataType
= kLarge32BitData
;
660 maskType
= kLarge8BitMask
;
661 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
665 dataType
= kSmall32BitData
;
666 maskType
= kSmall8BitMask
;
667 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
671 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
673 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
674 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
675 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
676 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
678 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
680 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
681 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
683 unsigned char *source
= (unsigned char *) *imagehandle
;
684 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
686 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
687 for ( int y
= 0 ; y
< h
; ++y
)
689 for ( int x
= 0 ; x
< w
; ++x
)
691 *destination
++ = *sourcemask
++ ;
693 *destination
++ = *source
++ ;
694 *destination
++ = *source
++ ;
695 *destination
++ = *source
++ ;
699 DisposeHandle( imagehandle
) ;
700 DisposeHandle( maskhandle
) ;
702 DisposeHandle( (Handle
) iconFamily
) ;
709 dc
.SelectObject( *this ) ;
710 dc
.DrawIcon( icon
, 0 , 0 ) ;
711 dc
.SelectObject( wxNullBitmap
) ;
720 wxBitmap::~wxBitmap()
724 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
726 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
730 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
731 if ( the_width
% (sizeof(unsigned char) * 8) ) {
732 linesize
+= sizeof(unsigned char);
734 unsigned char* linestart
= (unsigned char*) bits
;
735 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
736 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
738 for ( int x
= 0 ; x
< the_width
; ++x
)
742 int mask
= 1 << bit
;
743 if ( linestart
[index
] & mask
)
745 *destination
++ = 0xFF ;
752 *destination
++ = 0xFF ;
753 *destination
++ = 0xFF ;
754 *destination
++ = 0xFF ;
755 *destination
++ = 0xFF ;
763 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
767 wxBitmap::wxBitmap(int w
, int h
, int d
)
769 (void)Create(w
, h
, d
);
772 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
774 (void) Create(data
, type
, width
, height
, depth
);
777 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
779 LoadFile(filename
, type
);
782 wxBitmap::wxBitmap(const char **bits
)
784 (void) CreateFromXpm(bits
);
787 wxBitmap::wxBitmap(char **bits
)
789 (void) CreateFromXpm((const char **)bits
);
792 void* wxBitmap::GetRawAccess() const
794 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
795 return M_BITMAPDATA
->GetRawAccess() ;
798 void* wxBitmap::BeginRawAccess()
800 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
801 return M_BITMAPDATA
->BeginRawAccess() ;
804 void wxBitmap::EndRawAccess()
806 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
807 M_BITMAPDATA
->EndRawAccess() ;
810 bool wxBitmap::CreateFromXpm(const char **bits
)
812 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
813 wxXPMDecoder decoder
;
814 wxImage img
= decoder
.ReadData(bits
);
815 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
816 *this = wxBitmap(img
);
820 #if wxMAC_USE_CORE_GRAPHICS
821 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
823 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
824 return M_BITMAPDATA
->CGImageCreate() ;
828 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
831 (rect
.x
>= 0) && (rect
.y
>= 0) &&
832 (rect
.x
+rect
.width
<= GetWidth()) &&
833 (rect
.y
+rect
.height
<= GetHeight()),
834 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
837 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
838 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
841 int sourcewidth
= GetWidth() ;
842 int destwidth
= rect
.width
;
843 int destheight
= rect
.height
;
845 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
846 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
847 int sourcelinesize
= sourcewidth
* 4 ;
848 int destlinesize
= destwidth
* 4 ;
849 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
850 unsigned char *dest
= destdata
;
851 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
853 memcpy( dest
, source
, destlinesize
) ;
858 if ( M_BITMAPDATA
->m_bitmapMask
)
860 wxMemoryBuffer maskbuf
;
861 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
862 size_t maskbufsize
= rowBytes
* destheight
;
863 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
865 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
866 int destlinesize
= rowBytes
;
867 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
868 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
869 unsigned char *dest
= destdata
;
871 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
873 memcpy( dest
, source
, destlinesize
) ;
875 maskbuf
.UngetWriteBuf( maskbufsize
) ;
876 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
878 else if ( HasAlpha() )
884 bool wxBitmap::Create(int w
, int h
, int d
)
889 d
= wxDisplayDepth() ;
891 m_refData
= new wxBitmapRefData( w
, h
, d
);
893 return M_BITMAPDATA
->Ok() ;
896 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
900 wxBitmapHandler
*handler
= FindHandler(type
);
904 m_refData
= new wxBitmapRefData
;
906 return handler
->LoadFile(this, filename
, type
, -1, -1);
910 wxImage
loadimage(filename
, type
);
911 if (loadimage
.Ok()) {
916 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
920 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
924 m_refData
= new wxBitmapRefData
;
926 wxBitmapHandler
*handler
= FindHandler(type
);
928 if ( handler
== NULL
) {
929 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
934 return handler
->Create(this, data
, type
, width
, height
, depth
);
937 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
939 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
941 // width and height of the device-dependent bitmap
942 int width
= image
.GetWidth();
943 int height
= image
.GetHeight();
945 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
949 bool hasAlpha
= false ;
951 if ( image
.HasMask() )
953 // takes precedence, don't mix with alpha info
957 hasAlpha
= image
.HasAlpha() ;
963 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
965 register unsigned char* data
= image
.GetData();
966 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
967 for (int y
= 0; y
< height
; y
++)
969 for (int x
= 0; x
< width
; x
++)
973 const unsigned char a
= *alpha
++;
975 #if wxMAC_USE_PREMULTIPLIED_ALPHA
976 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
977 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
978 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
980 *destination
++ = *data
++ ;
981 *destination
++ = *data
++ ;
982 *destination
++ = *data
++ ;
987 *destination
++ = 0xFF ;
988 *destination
++ = *data
++ ;
989 *destination
++ = *data
++ ;
990 *destination
++ = *data
++ ;
995 if ( image
.HasMask() )
997 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1001 wxImage
wxBitmap::ConvertToImage() const
1005 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1007 // create an wxImage object
1008 int width
= GetWidth();
1009 int height
= GetHeight();
1010 image
.Create( width
, height
);
1012 unsigned char *data
= image
.GetData();
1013 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1015 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1017 bool hasAlpha
= false ;
1018 bool hasMask
= false ;
1019 unsigned char *alpha
= NULL
;
1020 unsigned char *mask
= NULL
;
1029 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1035 alpha
= image
.GetAlpha() ;
1039 // The following masking algorithm is the same as well in msw/gtk:
1040 // the colour used as transparent one in wxImage and the one it is
1041 // replaced with when it really occurs in the bitmap
1042 static const int MASK_RED
= 1;
1043 static const int MASK_GREEN
= 2;
1044 static const int MASK_BLUE
= 3;
1045 static const int MASK_BLUE_REPLACEMENT
= 2;
1047 for (int yy
= 0; yy
< height
; yy
++)
1049 for (int xx
= 0; xx
< width
; xx
++)
1051 long color
= *((long*) source
) ;
1052 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1053 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1054 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1055 unsigned char b
= (color
&0x000000FF);
1060 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1061 b
= MASK_BLUE_REPLACEMENT
;
1070 else if ( hasAlpha
)
1074 data
[index
+ 1] = g
;
1075 data
[index
+ 2] = b
;
1081 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1086 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1087 const wxPalette
*palette
) const
1089 wxBitmapHandler
*handler
= FindHandler(type
);
1093 return handler
->SaveFile(this, filename
, type
, palette
);
1097 wxImage image
= ConvertToImage();
1099 return image
.SaveFile(filename
, type
);
1102 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1106 bool wxBitmap::Ok() const
1108 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1111 int wxBitmap::GetHeight() const
1113 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1115 return M_BITMAPDATA
->GetHeight();
1118 int wxBitmap::GetWidth() const
1120 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1122 return M_BITMAPDATA
->GetWidth() ;
1125 int wxBitmap::GetDepth() const
1127 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1129 return M_BITMAPDATA
->GetDepth();
1132 #if WXWIN_COMPATIBILITY_2_4
1134 int wxBitmap::GetQuality() const
1141 wxMask
*wxBitmap::GetMask() const
1143 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1145 return M_BITMAPDATA
->m_bitmapMask
;
1148 bool wxBitmap::HasAlpha() const
1150 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1152 return M_BITMAPDATA
->HasAlpha() ;
1155 void wxBitmap::SetWidth(int w
)
1158 m_refData
= new wxBitmapRefData
;
1160 M_BITMAPDATA
->SetWidth(w
);
1163 void wxBitmap::SetHeight(int h
)
1166 m_refData
= new wxBitmapRefData
;
1168 M_BITMAPDATA
->SetHeight(h
);
1171 void wxBitmap::SetDepth(int d
)
1174 m_refData
= new wxBitmapRefData
;
1176 M_BITMAPDATA
->SetDepth(d
);
1179 #if WXWIN_COMPATIBILITY_2_4
1181 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1187 void wxBitmap::SetOk(bool isOk
)
1190 m_refData
= new wxBitmapRefData
;
1192 M_BITMAPDATA
->SetOk(isOk
);
1196 wxPalette
*wxBitmap::GetPalette() const
1198 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1200 return &M_BITMAPDATA
->m_bitmapPalette
;
1203 void wxBitmap::SetPalette(const wxPalette
& palette
)
1206 m_refData
= new wxBitmapRefData
;
1208 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1210 #endif // wxUSE_PALETTE
1212 void wxBitmap::SetMask(wxMask
*mask
)
1215 m_refData
= new wxBitmapRefData
;
1217 // Remove existing mask if there is one.
1218 delete M_BITMAPDATA
->m_bitmapMask
;
1220 M_BITMAPDATA
->m_bitmapMask
= mask
;
1223 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1225 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1228 // ----------------------------------------------------------------------------
1230 // ----------------------------------------------------------------------------
1237 // Construct a mask from a bitmap and a colour indicating
1238 // the transparent area
1239 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1242 Create(bitmap
, colour
);
1245 // Construct a mask from a mono bitmap (copies the bitmap).
1246 wxMask::wxMask(const wxBitmap
& bitmap
)
1252 // Construct a mask from a mono bitmap (copies the bitmap).
1253 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1256 Create(data
, width
, height
, bytesPerRow
);
1263 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1264 m_maskBitmap
= NULL
;
1270 m_width
= m_height
= m_bytesPerRow
= 0 ;
1271 m_maskBitmap
= NULL
;
1274 void *wxMask::GetRawAccess() const
1276 return m_memBuf
.GetData() ;
1279 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1280 // so no rainbox colors will be created by QD when blitting
1282 void wxMask::RealizeNative()
1286 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1287 m_maskBitmap
= NULL
;
1289 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1290 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1291 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1294 // Create a mask from a mono bitmap (copies the bitmap).
1295 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1300 m_bytesPerRow
= bytesPerRow
;
1301 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1306 // Create a mask from a mono bitmap (copies the bitmap).
1307 bool wxMask::Create(const wxBitmap
& bitmap
)
1309 m_width
= bitmap
.GetWidth() ;
1310 m_height
= bitmap
.GetHeight() ;
1311 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1312 size_t size
= m_bytesPerRow
* m_height
;
1313 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1314 memset( destdatabase
, 0 , size
) ;
1315 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1316 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1318 unsigned char *destdata
= destdatabase
;
1319 for( int x
= 0 ; x
< m_width
; ++x
)
1322 unsigned char r
= *srcdata
++ ;
1323 unsigned char g
= *srcdata
++ ;
1324 unsigned char b
= *srcdata
++ ;
1325 if ( ( r
+ g
+ b
) > 0x10 )
1326 *destdata
++ = 0x00 ;
1328 *destdata
++ = 0xFF ;
1331 m_memBuf
.UngetWriteBuf( size
) ;
1336 // Create a mask from a bitmap and a colour indicating
1337 // the transparent area
1338 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1340 m_width
= bitmap
.GetWidth() ;
1341 m_height
= bitmap
.GetHeight() ;
1342 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1343 size_t size
= m_bytesPerRow
* m_height
;
1345 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1346 memset( destdatabase
, 0 , size
) ;
1347 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1348 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1350 unsigned char *destdata
= destdatabase
;
1351 for( int x
= 0 ; x
< m_width
; ++x
)
1354 unsigned char r
= *srcdata
++ ;
1355 unsigned char g
= *srcdata
++ ;
1356 unsigned char b
= *srcdata
++ ;
1357 if ( colour
== wxColour( r
, g
, b
) )
1358 *destdata
++ = 0x00 ;
1360 *destdata
++ = 0xFF ;
1363 m_memBuf
.UngetWriteBuf( size
) ;
1368 WXHBITMAP
wxMask::GetHBITMAP() const
1370 return m_maskBitmap
;
1373 // ----------------------------------------------------------------------------
1375 // ----------------------------------------------------------------------------
1377 wxBitmapHandler::~wxBitmapHandler()
1381 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1386 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1387 int desiredWidth
, int desiredHeight
)
1392 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1397 // ----------------------------------------------------------------------------
1398 // Standard Handlers
1399 // ----------------------------------------------------------------------------
1401 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1403 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1405 inline wxPICTResourceHandler()
1407 SetName(wxT("Macintosh Pict resource"));
1408 SetExtension(wxEmptyString
);
1409 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1412 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1413 int desiredWidth
, int desiredHeight
);
1415 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1418 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1419 int desiredWidth
, int desiredHeight
)
1423 wxMacStringToPascal( name
, theName
) ;
1425 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1429 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1430 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1432 dc
.SelectObject( *bitmap
) ;
1434 dc
.SelectObject( wxNullBitmap
) ;
1437 #endif //wxUSE_METAFILE
1442 void wxBitmap::InitStandardHandlers()
1444 AddHandler(new wxPICTResourceHandler
) ;
1445 AddHandler(new wxICONResourceHandler
) ;
1448 // ----------------------------------------------------------------------------
1449 // raw bitmap access support
1450 // ----------------------------------------------------------------------------
1452 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1456 // no bitmap, no data (raw or otherwise)
1460 data
.m_width
= GetWidth() ;
1461 data
.m_height
= GetHeight() ;
1462 data
.m_stride
= GetWidth() * 4 ;
1463 return GetRawAccess() ;
1466 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1471 // TODO : if we have some information about the API we should check
1472 // this code looks strange...
1474 if ( M_BITMAPDATA
->HasAlpha() )
1476 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1478 int w
= data
.GetWidth(),
1479 h
= data
.GetHeight();
1481 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1482 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1483 wxAlphaPixelData::Iterator
pMask(dataMask
),
1485 for ( int y
= 0; y
< h
; y
++ )
1487 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1490 for ( int x
= 0; x
< w
; x
++ )
1492 const wxAlphaPixelData::Iterator::ChannelType
1495 pMask
.Red() = alpha
;
1496 pMask
.Green() = alpha
;
1497 pMask
.Blue() = alpha
;
1506 pMask
= rowStartMask
;
1507 pMask
.OffsetY(dataMask
, 1);
1510 SetMask(new wxMask(bmpMask
));
1514 void wxBitmap::UseAlpha()
1516 // remember that we are using alpha channel, we'll need to create a proper
1517 // mask in UngetRawData()
1518 M_BITMAPDATA
->UseAlpha( true );