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
== kControlContentPictHandle
)
113 // owned by the bitmap, no release here
115 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
116 else if ( info
->contentType
== kControlContentCGImageRef
)
118 CGImageRelease( info
->u
.imageRef
) ;
123 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
127 #endif //wxUSE_BMPBUTTON
129 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
131 void wxBitmapRefData::Init()
137 m_bitmapMask
= NULL
;
138 #if wxMAC_USE_CORE_GRAPHICS
139 m_cgImageRef
= NULL
;
142 m_pictHandle
= NULL
;
144 m_hMaskBitmap
= NULL
;
145 m_maskBytesPerRow
= NULL
;
147 m_rawAccessCount
= 0 ;
151 wxBitmapRefData::wxBitmapRefData()
156 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
159 Create( w
, h
, d
) ;
162 bool wxBitmapRefData::Create( int w
, int h
, int d
)
168 m_bytesPerRow
= w
* 4 ;
169 size_t size
= m_bytesPerRow
* h
;
170 void* data
= m_memBuf
.GetWriteBuf(size
) ;
171 memset( data
, 0 , size
) ;
172 m_memBuf
.UngetWriteBuf(size
) ;
175 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
177 (char*) data
, m_bytesPerRow
) ) ;
178 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
179 m_ok
= ( m_hBitmap
!= NULL
) ;
184 void wxBitmapRefData::UseAlpha( bool use
)
186 if ( m_hasAlpha
== use
)
192 int width
= GetWidth() ;
193 int height
= GetHeight() ;
194 m_maskBytesPerRow
= ( width
* 4 + 3 ) & 0xFFFFFFC ;
195 size_t size
= height
* m_maskBytesPerRow
;
196 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
197 memset( data
, 0 , size
) ;
198 wxASSERT( m_hMaskBitmap
== NULL
) ;
199 Rect rect
= { 0 , 0 , height
, width
} ;
200 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
201 (char*) data
, m_maskBytesPerRow
) ) ;
202 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
203 m_maskMemBuf
.UngetWriteBuf(size
) ;
204 #if !wxMAC_USE_CORE_GRAPHICS
210 DisposeGWorld( m_hMaskBitmap
) ;
211 m_hMaskBitmap
= NULL
;
212 m_maskBytesPerRow
= 0 ;
216 void *wxBitmapRefData::GetRawAccess() const
218 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
219 return m_memBuf
.GetData() ;
222 void *wxBitmapRefData::BeginRawAccess()
224 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
225 wxASSERT( m_rawAccessCount
== 0 ) ;
227 // we must destroy an existing cached image, as
228 // the bitmap data may change now
229 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
230 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
231 #if wxMAC_USE_CORE_GRAPHICS
234 CGImageRelease( m_cgImageRef
) ;
235 m_cgImageRef
= NULL
;
238 return m_memBuf
.GetData() ;
241 void wxBitmapRefData::EndRawAccess()
243 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
244 wxASSERT( m_rawAccessCount
== 1 ) ;
246 #if !wxMAC_USE_CORE_GRAPHICS
251 bool wxBitmapRefData::HasNativeSize()
254 int h
= GetHeight() ;
255 int sz
= wxMax( w
, h
) ;
257 if ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 )
262 IconRef
wxBitmapRefData::GetIconRef()
264 if ( m_iconRef
== NULL
)
266 // Create Icon Family Handle
268 IconFamilyHandle iconFamily
= NULL
;
270 iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
271 (**iconFamily
).resourceType
= kIconFamilyType
;
272 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
275 int h
= GetHeight() ;
276 int sz
= wxMax( w
, h
) ;
278 OSType dataType
= 0 ;
279 OSType maskType
= 0 ;
283 dataType
= kThumbnail32BitData
;
284 maskType
= kThumbnail8BitMask
;
288 dataType
= kHuge32BitData
;
289 maskType
= kHuge8BitMask
;
293 dataType
= kLarge32BitData
;
294 maskType
= kLarge8BitMask
;
298 dataType
= kSmall32BitData
;
299 maskType
= kSmall8BitMask
;
304 // setup the header properly
307 Handle maskdata
= NULL
;
308 unsigned char * maskptr
= NULL
;
309 unsigned char * ptr
= NULL
;
314 data
= NewHandle( size
) ;
316 ptr
= (unsigned char*) *data
;
317 memset( ptr
, 0, size
) ;
320 maskdata
= NewHandle( masksize
) ;
322 maskptr
= (unsigned char*) *maskdata
;
323 memset( maskptr
, 0 , masksize
) ;
325 bool hasAlpha
= HasAlpha() ;
326 wxMask
*mask
= m_bitmapMask
;
327 unsigned char * source
= (unsigned char*) GetRawAccess() ;
328 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
329 for ( int y
= 0 ; y
< h
; ++y
)
331 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
332 unsigned char * maskdest
= maskptr
+ y
* sz
;
333 for ( int x
= 0 ; x
< w
; ++x
)
335 unsigned char a
= *source
++ ;
336 unsigned char r
= *source
++ ;
337 unsigned char g
= *source
++ ;
338 unsigned char b
= *source
++ ;
346 *maskdest
++ = *masksource
++ ;
354 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
355 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
357 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
358 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
360 HUnlock( maskdata
) ;
361 DisposeHandle( data
) ;
362 DisposeHandle( maskdata
) ;
366 PicHandle pic
= GetPictHandle() ;
367 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
370 // transform into IconRef
372 static int iconCounter
= 2 ;
376 RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &m_iconRef
) ;
377 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
378 // we have to retain a reference, as Unregister will decrement it
379 AcquireIconRef( m_iconRef
) ;
380 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
381 DisposeHandle( (Handle
) iconFamily
) ;
387 PicHandle
wxBitmapRefData::GetPictHandle()
389 if ( m_pictHandle
== NULL
)
391 CGrafPtr origPort
= NULL
;
392 GDHandle origDev
= NULL
;
393 GWorldPtr wp
= NULL
;
394 GWorldPtr mask
= NULL
;
395 int height
= GetHeight() ;
396 int width
= GetWidth() ;
398 Rect rect
= { 0 , 0 , height
, width
} ;
400 GetGWorld( &origPort
, &origDev
) ;
402 wp
= GetHBITMAP( &mask
) ;
404 RgnHandle clipRgn
= NULL
;
408 GWorldPtr monoworld
;
410 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
412 LockPixels( GetGWorldPixMap( monoworld
) ) ;
413 LockPixels( GetGWorldPixMap( mask
) ) ;
414 SetGWorld( monoworld
, NULL
) ;
415 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
416 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
417 RGBForeColor( &black
) ;
418 RGBBackColor( &white
) ;
419 CopyBits(GetPortBitMapForCopyBits(mask
),
420 GetPortBitMapForCopyBits(monoworld
),
424 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
425 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
426 UnlockPixels( GetGWorldPixMap( mask
) ) ;
427 DisposeGWorld( monoworld
) ;
430 SetGWorld( wp
, NULL
) ;
432 GetPortBounds( wp
, &portRect
) ;
433 m_pictHandle
= OpenPicture(&portRect
);
437 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
438 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
439 RGBForeColor( &black
) ;
440 RGBBackColor( &white
) ;
445 LockPixels( GetGWorldPixMap( wp
) ) ;
446 CopyBits(GetPortBitMapForCopyBits(wp
),
447 GetPortBitMapForCopyBits(wp
),
451 UnlockPixels( GetGWorldPixMap( wp
) ) ;
454 SetGWorld( origPort
, origDev
) ;
456 DisposeRgn( clipRgn
) ;
458 return m_pictHandle
;
462 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
464 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
465 wxASSERT( data
== membuf
->GetData() ) ;
469 CGImageRef
wxBitmapRefData::CGImageCreate() const
472 wxASSERT( m_rawAccessCount
>= 0 ) ;
474 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
476 size_t imageSize
= m_width
* m_height
* 4 ;
477 void * dataBuffer
= m_memBuf
.GetData() ;
480 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
481 wxMemoryBuffer
* membuf
= NULL
;
485 membuf
= new wxMemoryBuffer( imageSize
) ;
486 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
487 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
488 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
489 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
490 alphaInfo
= kCGImageAlphaFirst
;
491 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
493 unsigned char *sourcemask
= sourcemaskstart
;
494 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
496 *destalpha
= *sourcemask
;
500 else if ( m_hasAlpha
)
502 #if wxMAC_USE_PREMULTIPLIED_ALPHA
503 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
505 alphaInfo
= kCGImageAlphaFirst
;
507 membuf
= new wxMemoryBuffer( m_memBuf
) ;
511 membuf
= new wxMemoryBuffer( m_memBuf
) ;
513 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
514 CGDataProviderRef dataProvider
=
515 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
516 wxMacMemoryBufferReleaseProc
);
518 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
519 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
520 CGDataProviderRelease( dataProvider
);
524 image
= m_cgImageRef
;
525 CGImageRetain( image
) ;
527 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
529 // we keep it for later use
530 m_cgImageRef
= image
;
531 CGImageRetain( image
) ;
537 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
539 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
544 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
545 else if ( m_hasAlpha
)
547 #if !wxMAC_USE_CORE_GRAPHICS
548 if ( m_rawAccessCount
> 0 )
551 // this structure is not kept in synch when using CG, so if someone
552 // is really accessing the Graphports, we have to sync it
555 *mask
= m_hMaskBitmap
;
561 void wxBitmapRefData::UpdateAlphaMask() const
565 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
566 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
568 int h
= GetHeight() ;
571 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
573 unsigned char* destalpha
= destalphabase
;
574 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
576 // we must have 24 bit depth for non quartz smooth alpha
578 *destalpha
++ = 255 - *sourcemask
;
579 *destalpha
++ = 255 - *sourcemask
;
580 *destalpha
++ = 255 - *sourcemask
;
586 void wxBitmapRefData::Free()
588 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
590 #if wxMAC_USE_CORE_GRAPHICS
593 CGImageRelease( m_cgImageRef
) ;
594 m_cgImageRef
= NULL
;
599 ReleaseIconRef( m_iconRef
) ;
604 KillPicture( m_pictHandle
) ;
605 m_pictHandle
= NULL
;
609 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
614 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
615 m_hMaskBitmap
= NULL
;
625 wxBitmapRefData::~wxBitmapRefData()
630 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
632 int w
= icon
.GetWidth() ;
633 int h
= icon
.GetHeight() ;
636 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
638 bool created
= false ;
640 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
642 IconFamilyHandle iconFamily
= NULL
;
643 Handle imagehandle
= NewHandle(0) ;
644 Handle maskhandle
= NewHandle(0) ;
648 IconSelectorValue selector
= 0 ;
651 dataType
= kThumbnail32BitData
;
652 maskType
= kThumbnail8BitMask
;
653 selector
= kSelectorAllAvailableData
;
657 dataType
= kHuge32BitData
;
658 maskType
= kHuge8BitMask
;
659 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
663 dataType
= kLarge32BitData
;
664 maskType
= kLarge8BitMask
;
665 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
669 dataType
= kSmall32BitData
;
670 maskType
= kSmall8BitMask
;
671 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
675 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
677 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
678 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
679 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
680 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
682 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
684 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
685 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
687 unsigned char *source
= (unsigned char *) *imagehandle
;
688 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
690 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
691 for ( int y
= 0 ; y
< h
; ++y
)
693 for ( int x
= 0 ; x
< w
; ++x
)
695 *destination
++ = *sourcemask
++ ;
697 *destination
++ = *source
++ ;
698 *destination
++ = *source
++ ;
699 *destination
++ = *source
++ ;
703 DisposeHandle( imagehandle
) ;
704 DisposeHandle( maskhandle
) ;
706 DisposeHandle( (Handle
) iconFamily
) ;
713 dc
.SelectObject( *this ) ;
714 dc
.DrawIcon( icon
, 0 , 0 ) ;
715 dc
.SelectObject( wxNullBitmap
) ;
724 wxBitmap::~wxBitmap()
728 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
730 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
734 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
735 if ( the_width
% (sizeof(unsigned char) * 8) ) {
736 linesize
+= sizeof(unsigned char);
738 unsigned char* linestart
= (unsigned char*) bits
;
739 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
740 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
742 for ( int x
= 0 ; x
< the_width
; ++x
)
746 int mask
= 1 << bit
;
747 if ( linestart
[index
] & mask
)
749 *destination
++ = 0xFF ;
756 *destination
++ = 0xFF ;
757 *destination
++ = 0xFF ;
758 *destination
++ = 0xFF ;
759 *destination
++ = 0xFF ;
767 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
771 wxBitmap::wxBitmap(int w
, int h
, int d
)
773 (void)Create(w
, h
, d
);
776 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
778 (void) Create(data
, type
, width
, height
, depth
);
781 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
783 LoadFile(filename
, type
);
786 wxBitmap::wxBitmap(const char **bits
)
788 (void) CreateFromXpm(bits
);
791 wxBitmap::wxBitmap(char **bits
)
793 (void) CreateFromXpm((const char **)bits
);
796 void* wxBitmap::GetRawAccess() const
798 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
799 return M_BITMAPDATA
->GetRawAccess() ;
802 void* wxBitmap::BeginRawAccess()
804 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
805 return M_BITMAPDATA
->BeginRawAccess() ;
808 void wxBitmap::EndRawAccess()
810 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
811 M_BITMAPDATA
->EndRawAccess() ;
814 bool wxBitmap::CreateFromXpm(const char **bits
)
817 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
818 wxXPMDecoder decoder
;
819 wxImage img
= decoder
.ReadData(bits
);
820 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
821 *this = wxBitmap(img
);
829 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
831 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
832 return M_BITMAPDATA
->CGImageCreate() ;
836 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
839 (rect
.x
>= 0) && (rect
.y
>= 0) &&
840 (rect
.x
+rect
.width
<= GetWidth()) &&
841 (rect
.y
+rect
.height
<= GetHeight()),
842 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
845 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
846 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
849 int sourcewidth
= GetWidth() ;
850 int destwidth
= rect
.width
;
851 int destheight
= rect
.height
;
853 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
854 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
855 int sourcelinesize
= sourcewidth
* 4 ;
856 int destlinesize
= destwidth
* 4 ;
857 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
858 unsigned char *dest
= destdata
;
859 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
861 memcpy( dest
, source
, destlinesize
) ;
866 if ( M_BITMAPDATA
->m_bitmapMask
)
868 wxMemoryBuffer maskbuf
;
869 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
870 size_t maskbufsize
= rowBytes
* destheight
;
871 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
873 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
874 int destlinesize
= rowBytes
;
875 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
876 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
877 unsigned char *dest
= destdata
;
879 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
881 memcpy( dest
, source
, destlinesize
) ;
883 maskbuf
.UngetWriteBuf( maskbufsize
) ;
884 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
886 else if ( HasAlpha() )
892 bool wxBitmap::Create(int w
, int h
, int d
)
897 d
= wxDisplayDepth() ;
899 m_refData
= new wxBitmapRefData( w
, h
, d
);
901 return M_BITMAPDATA
->Ok() ;
904 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
908 wxBitmapHandler
*handler
= FindHandler(type
);
912 m_refData
= new wxBitmapRefData
;
914 return handler
->LoadFile(this, filename
, type
, -1, -1);
919 wxImage
loadimage(filename
, type
);
920 if (loadimage
.Ok()) {
926 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
930 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
934 m_refData
= new wxBitmapRefData
;
936 wxBitmapHandler
*handler
= FindHandler(type
);
938 if ( handler
== NULL
) {
939 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
944 return handler
->Create(this, data
, type
, width
, height
, depth
);
949 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
951 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
953 // width and height of the device-dependent bitmap
954 int width
= image
.GetWidth();
955 int height
= image
.GetHeight();
957 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
961 bool hasAlpha
= false ;
963 if ( image
.HasMask() )
965 // takes precedence, don't mix with alpha info
969 hasAlpha
= image
.HasAlpha() ;
975 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
977 register unsigned char* data
= image
.GetData();
978 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
979 for (int y
= 0; y
< height
; y
++)
981 for (int x
= 0; x
< width
; x
++)
985 const unsigned char a
= *alpha
++;
987 #if wxMAC_USE_PREMULTIPLIED_ALPHA
988 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
989 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
990 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
992 *destination
++ = *data
++ ;
993 *destination
++ = *data
++ ;
994 *destination
++ = *data
++ ;
999 *destination
++ = 0xFF ;
1000 *destination
++ = *data
++ ;
1001 *destination
++ = *data
++ ;
1002 *destination
++ = *data
++ ;
1007 if ( image
.HasMask() )
1009 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1013 wxImage
wxBitmap::ConvertToImage() const
1017 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1019 // create an wxImage object
1020 int width
= GetWidth();
1021 int height
= GetHeight();
1022 image
.Create( width
, height
);
1024 unsigned char *data
= image
.GetData();
1025 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1027 unsigned char* source
= (unsigned char*) GetRawAccess() ;
1029 bool hasAlpha
= false ;
1030 bool hasMask
= false ;
1031 int maskBytesPerRow
= 0 ;
1032 unsigned char *alpha
= NULL
;
1033 unsigned char *mask
= NULL
;
1042 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1043 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1049 alpha
= image
.GetAlpha() ;
1053 // The following masking algorithm is the same as well in msw/gtk:
1054 // the colour used as transparent one in wxImage and the one it is
1055 // replaced with when it really occurs in the bitmap
1056 static const int MASK_RED
= 1;
1057 static const int MASK_GREEN
= 2;
1058 static const int MASK_BLUE
= 3;
1059 static const int MASK_BLUE_REPLACEMENT
= 2;
1061 for (int yy
= 0; yy
< height
; yy
++ , mask
+= maskBytesPerRow
)
1063 unsigned char * maskp
= mask
;
1064 for (int xx
= 0; xx
< width
; xx
++)
1066 long color
= *((long*) source
) ;
1067 unsigned char a
= ((color
&0xFF000000) >> 24) ;
1068 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
1069 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
1070 unsigned char b
= (color
&0x000000FF);
1073 if ( *maskp
++ == 0 )
1075 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1076 b
= MASK_BLUE_REPLACEMENT
;
1085 else if ( hasAlpha
)
1089 data
[index
+ 1] = g
;
1090 data
[index
+ 2] = b
;
1096 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1100 #endif //wxUSE_IMAGE
1102 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
1103 const wxPalette
*palette
) const
1105 wxBitmapHandler
*handler
= FindHandler(type
);
1109 return handler
->SaveFile(this, filename
, type
, palette
);
1114 wxImage image
= ConvertToImage();
1115 return image
.SaveFile(filename
, type
);
1119 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1123 bool wxBitmap::Ok() const
1125 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1128 int wxBitmap::GetHeight() const
1130 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1132 return M_BITMAPDATA
->GetHeight();
1135 int wxBitmap::GetWidth() const
1137 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1139 return M_BITMAPDATA
->GetWidth() ;
1142 int wxBitmap::GetDepth() const
1144 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1146 return M_BITMAPDATA
->GetDepth();
1149 #if WXWIN_COMPATIBILITY_2_4
1151 int wxBitmap::GetQuality() const
1158 wxMask
*wxBitmap::GetMask() const
1160 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1162 return M_BITMAPDATA
->m_bitmapMask
;
1165 bool wxBitmap::HasAlpha() const
1167 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1169 return M_BITMAPDATA
->HasAlpha() ;
1172 void wxBitmap::SetWidth(int w
)
1175 m_refData
= new wxBitmapRefData
;
1177 M_BITMAPDATA
->SetWidth(w
);
1180 void wxBitmap::SetHeight(int h
)
1183 m_refData
= new wxBitmapRefData
;
1185 M_BITMAPDATA
->SetHeight(h
);
1188 void wxBitmap::SetDepth(int d
)
1191 m_refData
= new wxBitmapRefData
;
1193 M_BITMAPDATA
->SetDepth(d
);
1196 #if WXWIN_COMPATIBILITY_2_4
1198 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1204 void wxBitmap::SetOk(bool isOk
)
1207 m_refData
= new wxBitmapRefData
;
1209 M_BITMAPDATA
->SetOk(isOk
);
1213 wxPalette
*wxBitmap::GetPalette() const
1215 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1217 return &M_BITMAPDATA
->m_bitmapPalette
;
1220 void wxBitmap::SetPalette(const wxPalette
& palette
)
1223 m_refData
= new wxBitmapRefData
;
1225 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1227 #endif // wxUSE_PALETTE
1229 void wxBitmap::SetMask(wxMask
*mask
)
1232 m_refData
= new wxBitmapRefData
;
1234 // Remove existing mask if there is one.
1235 delete M_BITMAPDATA
->m_bitmapMask
;
1237 M_BITMAPDATA
->m_bitmapMask
= mask
;
1240 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1242 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1245 // ----------------------------------------------------------------------------
1247 // ----------------------------------------------------------------------------
1254 // Construct a mask from a bitmap and a colour indicating
1255 // the transparent area
1256 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1259 Create(bitmap
, colour
);
1262 // Construct a mask from a mono bitmap (copies the bitmap).
1263 wxMask::wxMask(const wxBitmap
& bitmap
)
1269 // Construct a mask from a mono bitmap (copies the bitmap).
1270 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1273 Create(data
, width
, height
, bytesPerRow
);
1280 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1281 m_maskBitmap
= NULL
;
1287 m_width
= m_height
= m_bytesPerRow
= 0 ;
1288 m_maskBitmap
= NULL
;
1291 void *wxMask::GetRawAccess() const
1293 return m_memBuf
.GetData() ;
1296 // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1297 // so no rainbox colors will be created by QD when blitting
1299 void wxMask::RealizeNative()
1303 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1304 m_maskBitmap
= NULL
;
1306 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1307 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1308 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1311 // Create a mask from a mono bitmap (copies the bitmap).
1312 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1317 m_bytesPerRow
= bytesPerRow
;
1318 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1323 // Create a mask from a mono bitmap (copies the bitmap).
1324 bool wxMask::Create(const wxBitmap
& bitmap
)
1326 m_width
= bitmap
.GetWidth() ;
1327 m_height
= bitmap
.GetHeight() ;
1328 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1329 size_t size
= m_bytesPerRow
* m_height
;
1330 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1331 memset( destdatabase
, 0 , size
) ;
1332 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1333 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1335 unsigned char *destdata
= destdatabase
;
1336 for( int x
= 0 ; x
< m_width
; ++x
)
1339 unsigned char r
= *srcdata
++ ;
1340 unsigned char g
= *srcdata
++ ;
1341 unsigned char b
= *srcdata
++ ;
1342 if ( ( r
+ g
+ b
) > 0x10 )
1343 *destdata
++ = 0x00 ;
1345 *destdata
++ = 0xFF ;
1348 m_memBuf
.UngetWriteBuf( size
) ;
1353 // Create a mask from a bitmap and a colour indicating
1354 // the transparent area
1355 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1357 m_width
= bitmap
.GetWidth() ;
1358 m_height
= bitmap
.GetHeight() ;
1359 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1360 size_t size
= m_bytesPerRow
* m_height
;
1362 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1363 memset( destdatabase
, 0 , size
) ;
1364 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1365 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1367 unsigned char *destdata
= destdatabase
;
1368 for( int x
= 0 ; x
< m_width
; ++x
)
1371 unsigned char r
= *srcdata
++ ;
1372 unsigned char g
= *srcdata
++ ;
1373 unsigned char b
= *srcdata
++ ;
1374 if ( colour
== wxColour( r
, g
, b
) )
1375 *destdata
++ = 0x00 ;
1377 *destdata
++ = 0xFF ;
1380 m_memBuf
.UngetWriteBuf( size
) ;
1385 WXHBITMAP
wxMask::GetHBITMAP() const
1387 return m_maskBitmap
;
1390 // ----------------------------------------------------------------------------
1392 // ----------------------------------------------------------------------------
1394 wxBitmapHandler::~wxBitmapHandler()
1398 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1403 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1404 int desiredWidth
, int desiredHeight
)
1409 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1414 // ----------------------------------------------------------------------------
1415 // Standard Handlers
1416 // ----------------------------------------------------------------------------
1418 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1420 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1422 inline wxPICTResourceHandler()
1424 SetName(wxT("Macintosh Pict resource"));
1425 SetExtension(wxEmptyString
);
1426 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1429 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1430 int desiredWidth
, int desiredHeight
);
1432 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1435 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1436 int desiredWidth
, int desiredHeight
)
1440 wxMacStringToPascal( name
, theName
) ;
1442 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1446 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1447 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1449 dc
.SelectObject( *bitmap
) ;
1451 dc
.SelectObject( wxNullBitmap
) ;
1454 #endif //wxUSE_METAFILE
1459 void wxBitmap::InitStandardHandlers()
1461 AddHandler(new wxPICTResourceHandler
) ;
1462 AddHandler(new wxICONResourceHandler
) ;
1465 // ----------------------------------------------------------------------------
1466 // raw bitmap access support
1467 // ----------------------------------------------------------------------------
1469 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1473 // no bitmap, no data (raw or otherwise)
1477 data
.m_width
= GetWidth() ;
1478 data
.m_height
= GetHeight() ;
1479 data
.m_stride
= GetWidth() * 4 ;
1480 return GetRawAccess() ;
1483 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1488 // TODO : if we have some information about the API we should check
1489 // this code looks strange...
1491 if ( M_BITMAPDATA
->HasAlpha() )
1493 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1495 int w
= data
.GetWidth(),
1496 h
= data
.GetHeight();
1498 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1499 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1500 wxAlphaPixelData::Iterator
pMask(dataMask
),
1502 for ( int y
= 0; y
< h
; y
++ )
1504 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1507 for ( int x
= 0; x
< w
; x
++ )
1509 const wxAlphaPixelData::Iterator::ChannelType
1512 pMask
.Red() = alpha
;
1513 pMask
.Green() = alpha
;
1514 pMask
.Blue() = alpha
;
1523 pMask
= rowStartMask
;
1524 pMask
.OffsetY(dataMask
, 1);
1527 SetMask(new wxMask(bmpMask
));
1531 void wxBitmap::UseAlpha()
1533 // remember that we are using alpha channel, we'll need to create a proper
1534 // mask in UngetRawData()
1535 M_BITMAPDATA
->UseAlpha( true );