1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bitmap.h"
16 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
22 #include "wx/metafile.h"
23 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 #include "wx/dcmemory.h"
41 // Implementation Notes
42 // --------------------
44 // we are always working with a 32 bit deep pixel buffer
45 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
46 // therefore we have a separate GWorld there for blitting the mask in
48 // under Quartz then content is transformed into a CGImageRef representing the same data
49 // which can be transferred to the GPU by the OS for fast rendering
51 // we don't dare premultiplied alpha yet
52 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
56 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
58 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
61 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
65 if ( ( bmap
->HasNativeSize() && forceType
== 0 ) || forceType
== kControlContentIconRef
)
69 wxBitmapRefData
* bmp
= bmap
;
71 if ( !bmap
->HasNativeSize() )
73 // as PICT conversion will only result in a 16x16 icon, let's attempt
74 // a few scales for better results
76 int w
= bitmap
.GetWidth() ;
77 int h
= bitmap
.GetHeight() ;
78 int sz
= wxMax( w
, h
) ;
79 if ( sz
== 24 || sz
== 64)
81 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
82 bmp
= scaleBmp
.GetBitmapData() ;
86 info
->contentType
= kControlContentIconRef
;
87 info
->u
.iconRef
= bmp
->GetIconRef() ;
88 AcquireIconRef( info
->u
.iconRef
) ;
90 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
91 else if ( forceType
== kControlContentCGImageRef
)
93 info
->contentType
= kControlContentCGImageRef
;
94 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
99 info
->contentType
= kControlContentPictHandle
;
100 info
->u
.picture
= bmap
->GetPictHandle() ;
105 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
107 if ( info
->contentType
== kControlContentIconRef
)
109 ReleaseIconRef( info
->u
.iconRef
) ;
111 else if ( info
->contentType
== kControlNoContent
)
113 // there's no bitmap at all, fall through silently
115 else if ( info
->contentType
== kControlContentPictHandle
)
117 // owned by the bitmap, no release here
119 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
120 else if ( info
->contentType
== kControlContentCGImageRef
)
122 CGImageRelease( info
->u
.imageRef
) ;
127 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
131 #endif //wxUSE_BMPBUTTON
133 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
135 void wxBitmapRefData::Init()
141 m_bitmapMask
= NULL
;
142 #if wxMAC_USE_CORE_GRAPHICS
143 m_cgImageRef
= NULL
;
146 m_pictHandle
= NULL
;
148 m_hMaskBitmap
= NULL
;
149 m_maskBytesPerRow
= NULL
;
151 m_rawAccessCount
= 0 ;
155 wxBitmapRefData::wxBitmapRefData()
160 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
163 Create( w
, h
, d
) ;
166 bool wxBitmapRefData::Create( int w
, int h
, int d
)
172 m_bytesPerRow
= w
* 4 ;
173 size_t size
= m_bytesPerRow
* h
;
174 void* data
= m_memBuf
.GetWriteBuf(size
) ;
175 memset( data
, 0 , size
) ;
176 m_memBuf
.UngetWriteBuf(size
) ;
179 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
180 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
181 (char*) data
, m_bytesPerRow
) ) ;
182 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
183 m_ok
= ( m_hBitmap
!= NULL
) ;
188 void wxBitmapRefData::UseAlpha( bool use
)
190 if ( m_hasAlpha
== use
)
196 int width
= GetWidth() ;
197 int height
= GetHeight() ;
198 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
199 size_t size
= height
* m_maskBytesPerRow
;
200 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
201 memset( data
, 0 , size
) ;
202 wxASSERT( m_hMaskBitmap
== NULL
) ;
203 Rect rect
= { 0 , 0 , height
, width
} ;
204 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
205 (char*) data
, m_maskBytesPerRow
) ) ;
206 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
207 m_maskMemBuf
.UngetWriteBuf(size
) ;
208 #if !wxMAC_USE_CORE_GRAPHICS
214 DisposeGWorld( m_hMaskBitmap
) ;
215 m_hMaskBitmap
= NULL
;
216 m_maskBytesPerRow
= 0 ;
220 void *wxBitmapRefData::GetRawAccess() const
222 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
223 return m_memBuf
.GetData() ;
226 void *wxBitmapRefData::BeginRawAccess()
228 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
229 wxASSERT( m_rawAccessCount
== 0 ) ;
231 // we must destroy an existing cached image, as
232 // the bitmap data may change now
233 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
234 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
235 #if wxMAC_USE_CORE_GRAPHICS
238 CGImageRelease( m_cgImageRef
) ;
239 m_cgImageRef
= NULL
;
242 return m_memBuf
.GetData() ;
245 void wxBitmapRefData::EndRawAccess()
247 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
248 wxASSERT( m_rawAccessCount
== 1 ) ;
250 #if !wxMAC_USE_CORE_GRAPHICS
255 bool wxBitmapRefData::HasNativeSize()
258 int h
= GetHeight() ;
259 int sz
= wxMax( w
, h
) ;
261 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
266 IconRef
wxBitmapRefData::GetIconRef()
268 if ( m_iconRef
== NULL
)
270 // Create Icon Family Handle
272 IconFamilyHandle iconFamily
= NULL
;
274 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
275 (**iconFamily
).resourceType
= kIconFamilyType
;
276 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
279 int h
= GetHeight() ;
280 int sz
= wxMax( w
, h
) ;
282 OSType dataType
= 0 ;
283 OSType maskType
= 0 ;
287 dataType
= kThumbnail32BitData
;
288 maskType
= kThumbnail8BitMask
;
292 dataType
= kHuge32BitData
;
293 maskType
= kHuge8BitMask
;
297 dataType
= kLarge32BitData
;
298 maskType
= kLarge8BitMask
;
302 dataType
= kSmall32BitData
;
303 maskType
= kSmall8BitMask
;
308 // setup the header properly
311 Handle maskdata
= NULL
;
312 unsigned char * maskptr
= NULL
;
313 unsigned char * ptr
= NULL
;
318 data
= NewHandle( size
) ;
320 ptr
= (unsigned char*) *data
;
321 memset( ptr
, 0, size
) ;
324 maskdata
= NewHandle( masksize
) ;
326 maskptr
= (unsigned char*) *maskdata
;
327 memset( maskptr
, 0 , masksize
) ;
329 bool hasAlpha
= HasAlpha() ;
330 wxMask
*mask
= m_bitmapMask
;
331 unsigned char * source
= (unsigned char*) GetRawAccess() ;
332 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
333 for ( int y
= 0 ; y
< h
; ++y
)
335 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
336 unsigned char * maskdest
= maskptr
+ y
* sz
;
337 for ( int x
= 0 ; x
< w
; ++x
)
339 unsigned char a
= *source
++ ;
340 unsigned char r
= *source
++ ;
341 unsigned char g
= *source
++ ;
342 unsigned char b
= *source
++ ;
350 *maskdest
++ = *masksource
++ ;
358 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
359 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
361 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
362 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
364 HUnlock( maskdata
) ;
365 DisposeHandle( data
) ;
366 DisposeHandle( maskdata
) ;
370 PicHandle pic
= GetPictHandle() ;
371 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
374 // transform into IconRef
376 static int iconCounter
= 2 ;
380 RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
381 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
382 // we have to retain a reference, as Unregister will decrement it
383 AcquireIconRef( m_iconRef
) ;
384 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
385 DisposeHandle( (Handle
) iconFamily
) ;
391 PicHandle
wxBitmapRefData::GetPictHandle()
393 if ( m_pictHandle
== NULL
)
395 CGrafPtr origPort
= NULL
;
396 GDHandle origDev
= NULL
;
397 GWorldPtr wp
= NULL
;
398 GWorldPtr mask
= NULL
;
399 int height
= GetHeight() ;
400 int width
= GetWidth() ;
402 Rect rect
= { 0 , 0 , height
, width
} ;
404 GetGWorld( &origPort
, &origDev
) ;
406 wp
= GetHBITMAP( &mask
) ;
408 RgnHandle clipRgn
= NULL
;
412 GWorldPtr monoworld
;
414 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
416 LockPixels( GetGWorldPixMap( monoworld
) ) ;
417 LockPixels( GetGWorldPixMap( mask
) ) ;
418 SetGWorld( monoworld
, NULL
) ;
419 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
420 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
421 RGBForeColor( &black
) ;
422 RGBBackColor( &white
) ;
423 CopyBits(GetPortBitMapForCopyBits(mask
),
424 GetPortBitMapForCopyBits(monoworld
),
428 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
429 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
430 UnlockPixels( GetGWorldPixMap( mask
) ) ;
431 DisposeGWorld( monoworld
) ;
434 SetGWorld( wp
, NULL
) ;
436 GetPortBounds( wp
, &portRect
) ;
437 m_pictHandle
= OpenPicture(&portRect
);
441 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
442 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
443 RGBForeColor( &black
) ;
444 RGBBackColor( &white
) ;
449 LockPixels( GetGWorldPixMap( wp
) ) ;
450 CopyBits(GetPortBitMapForCopyBits(wp
),
451 GetPortBitMapForCopyBits(wp
),
455 UnlockPixels( GetGWorldPixMap( wp
) ) ;
458 SetGWorld( origPort
, origDev
) ;
460 DisposeRgn( clipRgn
) ;
462 return m_pictHandle
;
466 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
468 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
469 wxASSERT( data
== membuf
->GetData() ) ;
473 CGImageRef
wxBitmapRefData::CGImageCreate() const
476 wxASSERT( m_rawAccessCount
>= 0 ) ;
478 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
480 size_t imageSize
= m_width
* m_height
* 4 ;
481 void * dataBuffer
= m_memBuf
.GetData() ;
484 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
485 wxMemoryBuffer
* membuf
= NULL
;
489 membuf
= new wxMemoryBuffer( imageSize
) ;
490 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
491 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
492 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
493 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
494 alphaInfo
= kCGImageAlphaFirst
;
495 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
497 unsigned char *sourcemask
= sourcemaskstart
;
498 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
500 *destalpha
= *sourcemask
;
504 else if ( m_hasAlpha
)
506 #if wxMAC_USE_PREMULTIPLIED_ALPHA
507 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
509 alphaInfo
= kCGImageAlphaFirst
;
511 membuf
= new wxMemoryBuffer( m_memBuf
) ;
515 membuf
= new wxMemoryBuffer( m_memBuf
) ;
517 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
518 CGDataProviderRef dataProvider
=
519 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
520 wxMacMemoryBufferReleaseProc
);
522 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
523 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
524 CGDataProviderRelease( dataProvider
);
528 image
= m_cgImageRef
;
529 CGImageRetain( image
) ;
531 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
533 // we keep it for later use
534 m_cgImageRef
= image
;
535 CGImageRetain( image
) ;
541 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
543 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
548 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
549 else if ( m_hasAlpha
)
551 #if !wxMAC_USE_CORE_GRAPHICS
552 if ( m_rawAccessCount
> 0 )
555 // this structure is not kept in synch when using CG, so if someone
556 // is really accessing the Graphports, we have to sync it
559 *mask
= m_hMaskBitmap
;
565 void wxBitmapRefData::UpdateAlphaMask() const
569 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
570 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
572 int h
= GetHeight() ;
575 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
577 unsigned char* destalpha
= destalphabase
;
578 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
580 // we must have 24 bit depth for non quartz smooth alpha
582 *destalpha
++ = 255 - *sourcemask
;
583 *destalpha
++ = 255 - *sourcemask
;
584 *destalpha
++ = 255 - *sourcemask
;
590 void wxBitmapRefData::Free()
592 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
594 #if wxMAC_USE_CORE_GRAPHICS
597 CGImageRelease( m_cgImageRef
) ;
598 m_cgImageRef
= NULL
;
603 ReleaseIconRef( m_iconRef
) ;
608 KillPicture( m_pictHandle
) ;
609 m_pictHandle
= NULL
;
613 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
618 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
619 m_hMaskBitmap
= NULL
;
629 wxBitmapRefData::~wxBitmapRefData()
634 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
636 int w
= icon
.GetWidth() ;
637 int h
= icon
.GetHeight() ;
640 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
642 bool created
= false ;
644 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
646 IconFamilyHandle iconFamily
= NULL
;
647 Handle imagehandle
= NewHandle(0) ;
648 Handle maskhandle
= NewHandle(0) ;
652 IconSelectorValue selector
= 0 ;
655 dataType
= kThumbnail32BitData
;
656 maskType
= kThumbnail8BitMask
;
657 selector
= kSelectorAllAvailableData
;
661 dataType
= kHuge32BitData
;
662 maskType
= kHuge8BitMask
;
663 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
667 dataType
= kLarge32BitData
;
668 maskType
= kLarge8BitMask
;
669 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
673 dataType
= kSmall32BitData
;
674 maskType
= kSmall8BitMask
;
675 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
679 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
681 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
682 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
683 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
684 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
686 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
688 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
689 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
691 unsigned char *source
= (unsigned char *) *imagehandle
;
692 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
694 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
695 for ( int y
= 0 ; y
< h
; ++y
)
697 for ( int x
= 0 ; x
< w
; ++x
)
699 *destination
++ = *sourcemask
++ ;
701 *destination
++ = *source
++ ;
702 *destination
++ = *source
++ ;
703 *destination
++ = *source
++ ;
707 DisposeHandle( imagehandle
) ;
708 DisposeHandle( maskhandle
) ;
710 DisposeHandle( (Handle
) iconFamily
) ;
717 dc
.SelectObject( *this ) ;
718 dc
.DrawIcon( icon
, 0 , 0 ) ;
719 dc
.SelectObject( wxNullBitmap
) ;
728 wxBitmap::~wxBitmap()
732 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
734 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
738 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
739 if ( the_width
% (sizeof(unsigned char) * 8) ) {
740 linesize
+= sizeof(unsigned char);
742 unsigned char* linestart
= (unsigned char*) bits
;
743 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
744 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
746 for ( int x
= 0 ; x
< the_width
; ++x
)
750 int mask
= 1 << bit
;
751 if ( linestart
[index
] & mask
)
753 *destination
++ = 0xFF ;
760 *destination
++ = 0xFF ;
761 *destination
++ = 0xFF ;
762 *destination
++ = 0xFF ;
763 *destination
++ = 0xFF ;
771 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
775 wxBitmap::wxBitmap(int w
, int h
, int d
)
777 (void)Create(w
, h
, d
);
780 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
782 (void) Create(data
, type
, width
, height
, depth
);
785 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
787 LoadFile(filename
, type
);
790 wxBitmap::wxBitmap(const char **bits
)
792 (void) CreateFromXpm(bits
);
795 wxBitmap::wxBitmap(char **bits
)
797 (void) CreateFromXpm((const char **)bits
);
800 void* wxBitmap::GetRawAccess() const
802 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
803 return M_BITMAPDATA
->GetRawAccess() ;
806 void* wxBitmap::BeginRawAccess()
808 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
809 return M_BITMAPDATA
->BeginRawAccess() ;
812 void wxBitmap::EndRawAccess()
814 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
815 M_BITMAPDATA
->EndRawAccess() ;
818 bool wxBitmap::CreateFromXpm(const char **bits
)
821 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
822 wxXPMDecoder decoder
;
823 wxImage img
= decoder
.ReadData(bits
);
824 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
825 *this = wxBitmap(img
);
833 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
835 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
836 return M_BITMAPDATA
->CGImageCreate() ;
840 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
843 (rect
.x
>= 0) && (rect
.y
>= 0) &&
844 (rect
.x
+rect
.width
<= GetWidth()) &&
845 (rect
.y
+rect
.height
<= GetHeight()),
846 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
849 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
850 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
853 int sourcewidth
= GetWidth() ;
854 int destwidth
= rect
.width
;
855 int destheight
= rect
.height
;
857 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
858 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
859 int sourcelinesize
= sourcewidth
* 4 ;
860 int destlinesize
= destwidth
* 4 ;
861 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
862 unsigned char *dest
= destdata
;
863 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
865 memcpy( dest
, source
, destlinesize
) ;
870 if ( M_BITMAPDATA
->m_bitmapMask
)
872 wxMemoryBuffer maskbuf
;
873 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
874 size_t maskbufsize
= rowBytes
* destheight
;
875 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
877 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
878 int destlinesize
= rowBytes
;
879 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
880 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
881 unsigned char *dest
= destdata
;
883 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
885 memcpy( dest
, source
, destlinesize
) ;
887 maskbuf
.UngetWriteBuf( maskbufsize
) ;
888 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
890 else if ( HasAlpha() )
896 bool wxBitmap::Create(int w
, int h
, int d
)
901 d
= wxDisplayDepth() ;
903 m_refData
= new wxBitmapRefData( w
, h
, d
);
905 return M_BITMAPDATA
->Ok() ;
908 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
912 wxBitmapHandler
*handler
= FindHandler(type
);
916 m_refData
= new wxBitmapRefData
;
918 return handler
->LoadFile(this, filename
, type
, -1, -1);
923 wxImage
loadimage(filename
, type
);
924 if (loadimage
.Ok()) {
930 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
934 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
938 m_refData
= new wxBitmapRefData
;
940 wxBitmapHandler
*handler
= FindHandler(type
);
942 if ( handler
== NULL
) {
943 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
948 return handler
->Create(this, data
, type
, width
, height
, depth
);
953 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
955 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
957 // width and height of the device-dependent bitmap
958 int width
= image
.GetWidth();
959 int height
= image
.GetHeight();
961 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
965 bool hasAlpha
= false ;
967 if ( image
.HasMask() )
969 // takes precedence, don't mix with alpha info
973 hasAlpha
= image
.HasAlpha() ;
979 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
981 register unsigned char* data
= image
.GetData();
982 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
983 for (int y
= 0; y
< height
; y
++)
985 for (int x
= 0; x
< width
; x
++)
989 const unsigned char a
= *alpha
++;
991 #if wxMAC_USE_PREMULTIPLIED_ALPHA
992 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
993 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
994 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
996 *destination
++ = *data
++ ;
997 *destination
++ = *data
++ ;
998 *destination
++ = *data
++ ;
1003 *destination
++ = 0xFF ;
1004 *destination
++ = *data
++ ;
1005 *destination
++ = *data
++ ;
1006 *destination
++ = *data
++ ;
1011 if ( image
.HasMask() )
1013 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1017 wxImage
wxBitmap::ConvertToImage() const
1021 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1023 // create an wxImage object
1024 int width
= GetWidth();
1025 int height
= GetHeight();
1026 image
.Create( width
, height
);
1028 unsigned char *data
= image
.GetData();
1029 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1031 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1033 bool hasAlpha
= false ;
1034 bool hasMask
= false ;
1035 int maskBytesPerRow
= 0 ;
1036 unsigned char *alpha
= NULL
;
1037 unsigned char *mask
= NULL
;
1046 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1047 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1053 alpha
= image
.GetAlpha() ;
1057 // The following masking algorithm is the same as well in msw/gtk:
1058 // the colour used as transparent one in wxImage and the one it is
1059 // replaced with when it really occurs in the bitmap
1060 static const int MASK_RED
= 1;
1061 static const int MASK_GREEN
= 2;
1062 static const int MASK_BLUE
= 3;
1063 static const int MASK_BLUE_REPLACEMENT
= 2;
1065 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1067 unsigned char * maskp
= mask
;
1068 for (int xx
= 0; xx
< width
; xx
++)
1070 long color
= *((long*) source
) ;
1071 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1072 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1073 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1074 unsigned char b
= (color
&0x000000FF);
1077 if ( *maskp
++ == 0 )
1079 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1080 b
= MASK_BLUE_REPLACEMENT
;
1089 else if ( hasAlpha
)
1093 data
[index
+ 1] = g
;
1094 data
[index
+ 2] = b
;
1100 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1104 #endif //wxUSE_IMAGE
1106 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1107 const wxPalette
*palette
) const
1109 wxBitmapHandler
*handler
= FindHandler(type
);
1113 return handler
->SaveFile(this, filename
, type
, palette
);
1118 wxImage image
= ConvertToImage();
1119 return image
.SaveFile(filename
, type
);
1123 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1127 bool wxBitmap::Ok() const
1129 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1132 int wxBitmap::GetHeight() const
1134 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1136 return M_BITMAPDATA
->GetHeight();
1139 int wxBitmap::GetWidth() const
1141 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1143 return M_BITMAPDATA
->GetWidth() ;
1146 int wxBitmap::GetDepth() const
1148 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1150 return M_BITMAPDATA
->GetDepth();
1153 #if WXWIN_COMPATIBILITY_2_4
1155 int wxBitmap::GetQuality() const
1162 wxMask
*wxBitmap::GetMask() const
1164 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1166 return M_BITMAPDATA
->m_bitmapMask
;
1169 bool wxBitmap::HasAlpha() const
1171 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1173 return M_BITMAPDATA
->HasAlpha() ;
1176 void wxBitmap::SetWidth(int w
)
1179 m_refData
= new wxBitmapRefData
;
1181 M_BITMAPDATA
->SetWidth(w
);
1184 void wxBitmap::SetHeight(int h
)
1187 m_refData
= new wxBitmapRefData
;
1189 M_BITMAPDATA
->SetHeight(h
);
1192 void wxBitmap::SetDepth(int d
)
1195 m_refData
= new wxBitmapRefData
;
1197 M_BITMAPDATA
->SetDepth(d
);
1200 #if WXWIN_COMPATIBILITY_2_4
1202 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1208 void wxBitmap::SetOk(bool isOk
)
1211 m_refData
= new wxBitmapRefData
;
1213 M_BITMAPDATA
->SetOk(isOk
);
1217 wxPalette
*wxBitmap::GetPalette() const
1219 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1221 return &M_BITMAPDATA
->m_bitmapPalette
;
1224 void wxBitmap::SetPalette(const wxPalette
& palette
)
1227 m_refData
= new wxBitmapRefData
;
1229 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1231 #endif // wxUSE_PALETTE
1233 void wxBitmap::SetMask(wxMask
*mask
)
1236 m_refData
= new wxBitmapRefData
;
1238 // Remove existing mask if there is one.
1239 delete M_BITMAPDATA
->m_bitmapMask
;
1241 M_BITMAPDATA
->m_bitmapMask
= mask
;
1244 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1246 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1249 // ----------------------------------------------------------------------------
1251 // ----------------------------------------------------------------------------
1258 // Construct a mask from a bitmap and a colour indicating
1259 // the transparent area
1260 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1263 Create(bitmap
, colour
);
1266 // Construct a mask from a mono bitmap (copies the bitmap).
1267 wxMask::wxMask(const wxBitmap
& bitmap
)
1273 // Construct a mask from a mono bitmap (copies the bitmap).
1274 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1277 Create(data
, width
, height
, bytesPerRow
);
1284 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1285 m_maskBitmap
= NULL
;
1291 m_width
= m_height
= m_bytesPerRow
= 0 ;
1292 m_maskBitmap
= NULL
;
1295 void *wxMask::GetRawAccess() const
1297 return m_memBuf
.GetData() ;
1300 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1301 // so no rainbox colors will be created by QD when blitting
1303 void wxMask::RealizeNative()
1307 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1308 m_maskBitmap
= NULL
;
1310 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1311 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1312 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1315 // Create a mask from a mono bitmap (copies the bitmap).
1316 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1321 m_bytesPerRow
= bytesPerRow
;
1322 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1327 // Create a mask from a mono bitmap (copies the bitmap).
1328 bool wxMask::Create(const wxBitmap
& bitmap
)
1330 m_width
= bitmap
.GetWidth() ;
1331 m_height
= bitmap
.GetHeight() ;
1332 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1333 size_t size
= m_bytesPerRow
* m_height
;
1334 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1335 memset( destdatabase
, 0 , size
) ;
1336 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1337 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1339 unsigned char *destdata
= destdatabase
;
1340 for( int x
= 0 ; x
< m_width
; ++x
)
1343 unsigned char r
= *srcdata
++ ;
1344 unsigned char g
= *srcdata
++ ;
1345 unsigned char b
= *srcdata
++ ;
1346 if ( ( r
+ g
+ b
) > 0x10 )
1347 *destdata
++ = 0x00 ;
1349 *destdata
++ = 0xFF ;
1352 m_memBuf
.UngetWriteBuf( size
) ;
1357 // Create a mask from a bitmap and a colour indicating
1358 // the transparent area
1359 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1361 m_width
= bitmap
.GetWidth() ;
1362 m_height
= bitmap
.GetHeight() ;
1363 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1364 size_t size
= m_bytesPerRow
* m_height
;
1366 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1367 memset( destdatabase
, 0 , size
) ;
1368 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1369 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1371 unsigned char *destdata
= destdatabase
;
1372 for( int x
= 0 ; x
< m_width
; ++x
)
1375 unsigned char r
= *srcdata
++ ;
1376 unsigned char g
= *srcdata
++ ;
1377 unsigned char b
= *srcdata
++ ;
1378 if ( colour
== wxColour( r
, g
, b
) )
1379 *destdata
++ = 0x00 ;
1381 *destdata
++ = 0xFF ;
1384 m_memBuf
.UngetWriteBuf( size
) ;
1389 WXHBITMAP
wxMask::GetHBITMAP() const
1391 return m_maskBitmap
;
1394 // ----------------------------------------------------------------------------
1396 // ----------------------------------------------------------------------------
1398 wxBitmapHandler::~wxBitmapHandler()
1402 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1407 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1408 int desiredWidth
, int desiredHeight
)
1413 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1418 // ----------------------------------------------------------------------------
1419 // Standard Handlers
1420 // ----------------------------------------------------------------------------
1422 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1424 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1426 inline wxPICTResourceHandler()
1428 SetName(wxT("Macintosh Pict resource"));
1429 SetExtension(wxEmptyString
);
1430 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1433 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1434 int desiredWidth
, int desiredHeight
);
1436 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1439 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1440 int desiredWidth
, int desiredHeight
)
1444 wxMacStringToPascal( name
, theName
) ;
1446 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1450 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1451 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1453 dc
.SelectObject( *bitmap
) ;
1455 dc
.SelectObject( wxNullBitmap
) ;
1458 #endif //wxUSE_METAFILE
1463 void wxBitmap::InitStandardHandlers()
1465 AddHandler(new wxPICTResourceHandler
) ;
1466 AddHandler(new wxICONResourceHandler
) ;
1469 // ----------------------------------------------------------------------------
1470 // raw bitmap access support
1471 // ----------------------------------------------------------------------------
1473 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1477 // no bitmap, no data (raw or otherwise)
1481 data
.m_width
= GetWidth() ;
1482 data
.m_height
= GetHeight() ;
1483 data
.m_stride
= GetWidth() * 4 ;
1484 return GetRawAccess() ;
1487 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1492 // TODO : if we have some information about the API we should check
1493 // this code looks strange...
1495 if ( M_BITMAPDATA
->HasAlpha() )
1497 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1499 int w
= data
.GetWidth(),
1500 h
= data
.GetHeight();
1502 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1503 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1504 wxAlphaPixelData::Iterator
pMask(dataMask
),
1506 for ( int y
= 0; y
< h
; y
++ )
1508 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1511 for ( int x
= 0; x
< w
; x
++ )
1513 const wxAlphaPixelData::Iterator::ChannelType
1516 pMask
.Red() = alpha
;
1517 pMask
.Green() = alpha
;
1518 pMask
.Blue() = alpha
;
1527 pMask
= rowStartMask
;
1528 pMask
.OffsetY(dataMask
, 1);
1531 SetMask(new wxMask(bmpMask
));
1535 void wxBitmap::UseAlpha()
1537 // remember that we are using alpha channel, we'll need to create a proper
1538 // mask in UngetRawData()
1539 M_BITMAPDATA
->UseAlpha( true );