1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bitmap.h"
16 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
22 #include "wx/metafile.h"
23 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 #include "wx/dcmemory.h"
41 // Implementation Notes
42 // --------------------
44 // we are always working with a 32 bit deep pixel buffer
45 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
46 // therefore we have a separate GWorld there for blitting the mask in
48 // under Quartz then content is transformed into a CGImageRef representing the same data
49 // which can be transferred to the GPU by the OS for fast rendering
51 // we don't dare premultiplied alpha yet
52 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
54 IconFamilyHandle
wxMacCreateIconFamily(const wxBitmap
& bmp
)
56 // setup the header properly
58 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
59 (**iconFamily
).resourceType
= kIconFamilyType
;
60 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
62 int w
= bmp
.GetWidth() ;
63 int h
= bmp
.GetHeight() ;
64 int sz
= wxMax( w
, h
) ;
67 wxFAIL_MSG( wxT("Currently only 128 pixels wide images are supported") ) ;
71 Handle maskdata
= NULL
;
72 unsigned char * maskptr
= NULL
;
73 unsigned char * ptr
= NULL
;
79 bool hasAlpha
= bmp
.HasAlpha() ;
80 wxMask
*mask
= bmp
.GetMask() ;
81 // thumbnail is 128 x 128 with 32 bits per pixel
86 dataType
= kThumbnail32BitData
;
87 maskType
= kThumbnail8BitMask
;
92 dataType
= kHuge32BitData
;
93 maskType
= kHuge8BitMask
;
98 dataType
= kLarge32BitData
;
99 maskType
= kLarge8BitMask
;
104 dataType
= kSmall32BitData
;
105 maskType
= kSmall8BitMask
;
109 data
= NewHandle( size
) ;
111 ptr
= (unsigned char*) *data
;
112 memset( ptr
, 0, size
) ;
115 maskdata
= NewHandle( masksize
) ;
117 maskptr
= (unsigned char*) *maskdata
;
118 memset( maskptr
, 0 , masksize
) ;
120 unsigned char * source
= (unsigned char*) bmp
.GetRawAccess() ;
121 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
122 for ( int y
= 0 ; y
< h
; ++y
)
124 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
125 unsigned char * maskdest
= maskptr
+ y
* sz
;
126 for ( int x
= 0 ; x
< w
; ++x
)
128 unsigned char a
= *source
++ ;
129 unsigned char r
= *source
++ ;
130 unsigned char g
= *source
++ ;
131 unsigned char b
= *source
++ ;
139 *maskdest
++ = *masksource
++ ;
147 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
148 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
150 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
151 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
153 HUnlock( maskdata
) ;
154 DisposeHandle( data
) ;
155 DisposeHandle( maskdata
) ;
159 IconRef
wxMacCreateIconRef(const wxBitmap
& bmp
)
161 IconFamilyHandle iconFamily
= wxMacCreateIconFamily( bmp
) ;
163 static int iconCounter
= 2 ;
165 OSStatus err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &iconRef
) ;
167 err
= GetIconRefOwners(iconRef
, &owners
) ;
169 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
170 // we have to retain a reference, as Unregister will decrement it
171 AcquireIconRef( iconRef
) ;
172 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
173 DisposeHandle( (Handle
) iconFamily
) ;
179 PicHandle
wxMacCreatePicHandle( const wxBitmap
&bmp
)
181 CGrafPtr origPort
= NULL
;
182 GDHandle origDev
= NULL
;
183 PicHandle pict
= NULL
;
184 GWorldPtr wp
= NULL
;
185 GWorldPtr mask
= NULL
;
187 GetGWorld( &origPort
, &origDev
) ;
189 wp
= (GWorldPtr
) bmp
.GetHBITMAP( (WXHBITMAP
*) &mask
) ;
191 SetGWorld( wp
, NULL
) ;
193 GetPortBounds( wp
, &portRect
) ;
194 pict
= OpenPicture(&portRect
);
198 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
199 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
200 RGBForeColor( &black
) ;
201 RGBBackColor( &white
) ;
203 LockPixels( GetGWorldPixMap( wp
) ) ;
204 CopyBits(GetPortBitMapForCopyBits(wp
),
205 GetPortBitMapForCopyBits(wp
),
209 UnlockPixels( GetGWorldPixMap( wp
) ) ;
212 SetGWorld( origPort
, origDev
) ;
217 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
219 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
222 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
225 info
->contentType
= kControlContentIconRef
;
226 info
->u
.iconRef
= wxMacCreateIconRef( bitmap
) ;
228 #if wxMAC_USE_CORE_GRAPHICS
230 // only on 10.4 more controls will accept a CGImage
232 info->contentType = kControlContentCGImageRef ;
233 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
239 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
241 if ( info
->contentType
== kControlContentIconRef
)
243 ReleaseIconRef(info
->u
.iconRef
) ;
245 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
246 else if ( info
->contentType
== kControlContentCGImageRef
)
248 CGImageRelease( info
->u
.imageRef
) ;
253 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
258 void wxBitmapRefData::Init()
264 m_bitmapMask
= NULL
;
265 #if wxMAC_USE_CORE_GRAPHICS
266 m_cgImageRef
= NULL
;
269 m_hMaskBitmap
= NULL
;
270 m_maskBytesPerRow
= NULL
;
272 m_rawAccessCount
= 0 ;
276 wxBitmapRefData::wxBitmapRefData()
281 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
284 Create( w
, h
, d
) ;
287 bool wxBitmapRefData::Create( int w
, int h
, int d
)
293 m_bytesPerRow
= w
* 4 ;
294 size_t size
= m_bytesPerRow
* h
;
295 void* data
= m_memBuf
.GetWriteBuf(size
) ;
296 memset( data
, 0 , size
) ;
297 m_memBuf
.UngetWriteBuf(size
) ;
300 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
301 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
302 (char*) data
, m_bytesPerRow
) ) ;
303 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
304 m_ok
= ( m_hBitmap
!= NULL
) ;
309 void wxBitmapRefData::UseAlpha( bool use
)
311 if ( m_hasAlpha
== use
)
317 int width
= GetWidth() ;
318 int height
= GetHeight() ;
319 m_maskBytesPerRow
= ( width
+ 3 ) & 0xFFFFFFC ;
320 size_t size
= height
* m_maskBytesPerRow
;
321 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
322 memset( data
, 0 , size
) ;
323 wxASSERT( m_hMaskBitmap
== NULL
) ;
324 Rect rect
= { 0 , 0 , height
, width
} ;
325 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
326 (char*) data
, m_maskBytesPerRow
) ) ;
327 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
328 m_maskMemBuf
.UngetWriteBuf(size
) ;
329 #if !wxMAC_USE_CORE_GRAPHICS
335 DisposeGWorld( m_hMaskBitmap
) ;
336 m_hMaskBitmap
= NULL
;
337 m_maskBytesPerRow
= 0 ;
341 void *wxBitmapRefData::GetRawAccess() const
343 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
344 return m_memBuf
.GetData() ;
347 void *wxBitmapRefData::BeginRawAccess()
349 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
350 wxASSERT( m_rawAccessCount
== 0 ) ;
352 #if wxMAC_USE_CORE_GRAPHICS
353 // we must destroy an existing cached image, as
354 // the bitmap data may change now
357 CGImageRelease( m_cgImageRef
) ;
358 m_cgImageRef
= NULL
;
361 return m_memBuf
.GetData() ;
364 void wxBitmapRefData::EndRawAccess()
366 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
367 wxASSERT( m_rawAccessCount
== 1 ) ;
369 #if !wxMAC_USE_CORE_GRAPHICS
375 #if wxMAC_USE_CORE_GRAPHICS
376 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
378 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
379 wxASSERT( data
== membuf
->GetData() ) ;
383 CGImageRef
wxBitmapRefData::CGImageCreate() const
386 wxASSERT( m_rawAccessCount
>= 0 ) ;
388 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
390 size_t imageSize
= m_width
* m_height
* 4 ;
391 void * dataBuffer
= m_memBuf
.GetData() ;
394 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
395 wxMemoryBuffer
* membuf
= NULL
;
399 membuf
= new wxMemoryBuffer( imageSize
) ;
400 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
401 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
402 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
403 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
404 alphaInfo
= kCGImageAlphaFirst
;
405 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
407 unsigned char *sourcemask
= sourcemaskstart
;
408 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
410 *destalpha
= *sourcemask
;
414 else if ( m_hasAlpha
)
416 #if wxMAC_USE_PREMULTIPLIED_ALPHA
417 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
419 alphaInfo
= kCGImageAlphaFirst
;
421 membuf
= new wxMemoryBuffer( m_memBuf
) ;
425 membuf
= new wxMemoryBuffer( m_memBuf
) ;
427 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
428 CGDataProviderRef dataProvider
=
429 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
430 wxMacMemoryBufferReleaseProc
);
432 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
433 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
434 CGDataProviderRelease( dataProvider
);
438 image
= m_cgImageRef
;
439 CGImageRetain( image
) ;
441 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
443 // we keep it for later use
444 m_cgImageRef
= image
;
445 CGImageRetain( image
) ;
451 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
453 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
458 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
459 else if ( m_hasAlpha
)
461 #if !wxMAC_USE_CORE_GRAPHICS
462 if ( m_rawAccessCount
> 0 )
465 // this structure is not kept in synch when using CG, so if someone
466 // is really accessing the Graphports, we have to sync it
469 *mask
= m_hMaskBitmap
;
475 void wxBitmapRefData::UpdateAlphaMask() const
479 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
480 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
482 int h
= GetHeight() ;
485 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
487 unsigned char* destalpha
= destalphabase
;
488 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4, ++destalpha
)
490 *destalpha
= *sourcemask
;
496 void wxBitmapRefData::Free()
498 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
500 #if wxMAC_USE_CORE_GRAPHICS
503 CGImageRelease( m_cgImageRef
) ;
504 m_cgImageRef
= NULL
;
509 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
514 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
515 m_hMaskBitmap
= NULL
;
525 wxBitmapRefData::~wxBitmapRefData()
530 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
532 int w
= icon
.GetWidth() ;
533 int h
= icon
.GetHeight() ;
534 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
536 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
538 IconFamilyHandle iconFamily
= NULL
;
539 Handle imagehandle
= NewHandle(0) ;
540 Handle maskhandle
= NewHandle(0) ;
544 IconSelectorValue selector
;
547 dataType
= kThumbnail32BitData
;
548 maskType
= kThumbnail8BitMask
;
549 selector
= kSelectorAllAvailableData
;
553 dataType
= kHuge32BitData
;
554 maskType
= kHuge8BitMask
;
555 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
559 dataType
= kLarge32BitData
;
560 maskType
= kLarge8BitMask
;
561 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
565 dataType
= kSmall32BitData
;
566 maskType
= kSmall8BitMask
;
567 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
571 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
575 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
577 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
578 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
579 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
580 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
582 unsigned char *source
= (unsigned char *) *imagehandle
;
583 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
585 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
586 for ( int y
= 0 ; y
< h
; ++y
)
588 for ( int x
= 0 ; x
< w
; ++x
)
590 *destination
++ = *sourcemask
++ ;
592 *destination
++ = *source
++ ;
593 *destination
++ = *source
++ ;
594 *destination
++ = *source
++ ;
598 DisposeHandle( imagehandle
) ;
599 DisposeHandle( maskhandle
) ;
604 dc
.SelectObject( *this ) ;
605 dc
.DrawIcon( icon
, 0 , 0 ) ;
606 dc
.SelectObject( wxNullBitmap
) ;
615 wxBitmap::~wxBitmap()
619 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
621 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
625 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
626 if ( the_width
% (sizeof(unsigned char) * 8) ) {
627 linesize
+= sizeof(unsigned char);
629 unsigned char* linestart
= (unsigned char*) bits
;
630 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
631 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
633 for ( int x
= 0 ; x
< the_width
; ++x
)
637 int mask
= 1 << bit
;
638 if ( linestart
[index
] & mask
)
640 *destination
++ = 0xFF ;
647 *destination
++ = 0xFF ;
648 *destination
++ = 0xFF ;
649 *destination
++ = 0xFF ;
650 *destination
++ = 0xFF ;
658 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
662 wxBitmap::wxBitmap(int w
, int h
, int d
)
664 (void)Create(w
, h
, d
);
667 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
669 (void) Create(data
, type
, width
, height
, depth
);
672 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
674 LoadFile(filename
, type
);
677 wxBitmap::wxBitmap(const char **bits
)
679 (void) CreateFromXpm(bits
);
682 wxBitmap::wxBitmap(char **bits
)
684 (void) CreateFromXpm((const char **)bits
);
687 void* wxBitmap::GetRawAccess() const
689 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
690 return M_BITMAPDATA
->GetRawAccess() ;
693 void* wxBitmap::BeginRawAccess()
695 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
696 return M_BITMAPDATA
->BeginRawAccess() ;
699 void wxBitmap::EndRawAccess()
701 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
702 M_BITMAPDATA
->EndRawAccess() ;
705 bool wxBitmap::CreateFromXpm(const char **bits
)
707 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
708 wxXPMDecoder decoder
;
709 wxImage img
= decoder
.ReadData(bits
);
710 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
711 *this = wxBitmap(img
);
715 #if wxMAC_USE_CORE_GRAPHICS
716 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
718 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
719 return M_BITMAPDATA
->CGImageCreate() ;
723 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
726 (rect
.x
>= 0) && (rect
.y
>= 0) &&
727 (rect
.x
+rect
.width
<= GetWidth()) &&
728 (rect
.y
+rect
.height
<= GetHeight()),
729 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
732 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
733 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
736 int sourcewidth
= GetWidth() ;
737 int destwidth
= rect
.width
;
738 int destheight
= rect
.height
;
740 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
741 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
742 int sourcelinesize
= sourcewidth
* 4 ;
743 int destlinesize
= destwidth
* 4 ;
744 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
745 unsigned char *dest
= destdata
;
746 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
748 memcpy( dest
, source
, destlinesize
) ;
753 if ( M_BITMAPDATA
->m_bitmapMask
)
755 wxMemoryBuffer maskbuf
;
756 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
757 size_t maskbufsize
= rowBytes
* destheight
;
758 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
760 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
761 int destlinesize
= rowBytes
;
762 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
763 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
764 unsigned char *dest
= destdata
;
766 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
768 memcpy( dest
, source
, destlinesize
) ;
770 maskbuf
.UngetWriteBuf( maskbufsize
) ;
771 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
773 else if ( HasAlpha() )
779 bool wxBitmap::Create(int w
, int h
, int d
)
784 d
= wxDisplayDepth() ;
786 m_refData
= new wxBitmapRefData( w
, h
, d
);
788 return M_BITMAPDATA
->Ok() ;
791 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
795 wxBitmapHandler
*handler
= FindHandler(type
);
799 m_refData
= new wxBitmapRefData
;
801 return handler
->LoadFile(this, filename
, type
, -1, -1);
805 wxImage
loadimage(filename
, type
);
806 if (loadimage
.Ok()) {
811 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
815 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
819 m_refData
= new wxBitmapRefData
;
821 wxBitmapHandler
*handler
= FindHandler(type
);
823 if ( handler
== NULL
) {
824 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
829 return handler
->Create(this, data
, type
, width
, height
, depth
);
832 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
834 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
836 // width and height of the device-dependent bitmap
837 int width
= image
.GetWidth();
838 int height
= image
.GetHeight();
840 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
844 bool hasAlpha
= false ;
846 if ( image
.HasMask() )
848 // takes precedence, don't mix with alpha info
852 hasAlpha
= image
.HasAlpha() ;
858 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
860 register unsigned char* data
= image
.GetData();
861 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
862 for (int y
= 0; y
< height
; y
++)
864 for (int x
= 0; x
< width
; x
++)
868 const unsigned char a
= *alpha
++;
870 #if wxMAC_USE_PREMULTIPLIED_ALPHA
871 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
872 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
873 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
875 *destination
++ = *data
++ ;
876 *destination
++ = *data
++ ;
877 *destination
++ = *data
++ ;
882 *destination
++ = 0xFF ;
883 *destination
++ = *data
++ ;
884 *destination
++ = *data
++ ;
885 *destination
++ = *data
++ ;
890 if ( image
.HasMask() )
892 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
896 wxImage
wxBitmap::ConvertToImage() const
900 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
902 // create an wxImage object
903 int width
= GetWidth();
904 int height
= GetHeight();
905 image
.Create( width
, height
);
907 unsigned char *data
= image
.GetData();
908 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
910 unsigned char* source
= (unsigned char*) GetRawAccess() ;
912 bool hasAlpha
= false ;
913 bool hasMask
= false ;
914 unsigned char *alpha
= NULL
;
915 unsigned char *mask
= NULL
;
924 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
930 alpha
= image
.GetAlpha() ;
934 // The following masking algorithm is the same as well in msw/gtk:
935 // the colour used as transparent one in wxImage and the one it is
936 // replaced with when it really occurs in the bitmap
937 static const int MASK_RED
= 1;
938 static const int MASK_GREEN
= 2;
939 static const int MASK_BLUE
= 3;
940 static const int MASK_BLUE_REPLACEMENT
= 2;
942 for (int yy
= 0; yy
< height
; yy
++)
944 for (int xx
= 0; xx
< width
; xx
++)
946 long color
= *((long*) source
) ;
947 unsigned char a
= ((color
&0xFF000000) >> 24) ;
948 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
949 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
950 unsigned char b
= (color
&0x000000FF);
955 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
956 b
= MASK_BLUE_REPLACEMENT
;
969 data
[index
+ 1] = g
;
970 data
[index
+ 2] = b
;
976 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
981 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
982 const wxPalette
*palette
) const
984 wxBitmapHandler
*handler
= FindHandler(type
);
988 return handler
->SaveFile(this, filename
, type
, palette
);
992 wxImage image
= ConvertToImage();
994 return image
.SaveFile(filename
, type
);
997 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1001 bool wxBitmap::Ok() const
1003 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1006 int wxBitmap::GetHeight() const
1008 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1010 return M_BITMAPDATA
->GetHeight();
1013 int wxBitmap::GetWidth() const
1015 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1017 return M_BITMAPDATA
->GetWidth() ;
1020 int wxBitmap::GetDepth() const
1022 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1024 return M_BITMAPDATA
->GetDepth();
1027 int wxBitmap::GetQuality() const
1032 wxMask
*wxBitmap::GetMask() const
1034 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1036 return M_BITMAPDATA
->m_bitmapMask
;
1039 bool wxBitmap::HasAlpha() const
1041 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1043 return M_BITMAPDATA
->HasAlpha() ;
1046 void wxBitmap::SetWidth(int w
)
1049 m_refData
= new wxBitmapRefData
;
1051 M_BITMAPDATA
->SetWidth(w
);
1054 void wxBitmap::SetHeight(int h
)
1057 m_refData
= new wxBitmapRefData
;
1059 M_BITMAPDATA
->SetHeight(h
);
1062 void wxBitmap::SetDepth(int d
)
1065 m_refData
= new wxBitmapRefData
;
1067 M_BITMAPDATA
->SetDepth(d
);
1070 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1074 void wxBitmap::SetOk(bool isOk
)
1077 m_refData
= new wxBitmapRefData
;
1079 M_BITMAPDATA
->SetOk(isOk
);
1083 wxPalette
*wxBitmap::GetPalette() const
1085 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1087 return &M_BITMAPDATA
->m_bitmapPalette
;
1090 void wxBitmap::SetPalette(const wxPalette
& palette
)
1093 m_refData
= new wxBitmapRefData
;
1095 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1097 #endif // wxUSE_PALETTE
1099 void wxBitmap::SetMask(wxMask
*mask
)
1102 m_refData
= new wxBitmapRefData
;
1104 // Remove existing mask if there is one.
1105 delete M_BITMAPDATA
->m_bitmapMask
;
1107 M_BITMAPDATA
->m_bitmapMask
= mask
;
1110 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1112 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1115 // ----------------------------------------------------------------------------
1117 // ----------------------------------------------------------------------------
1124 // Construct a mask from a bitmap and a colour indicating
1125 // the transparent area
1126 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1129 Create(bitmap
, colour
);
1132 // Construct a mask from a mono bitmap (copies the bitmap).
1133 wxMask::wxMask(const wxBitmap
& bitmap
)
1139 // Construct a mask from a mono bitmap (copies the bitmap).
1140 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1143 Create(data
, width
, height
, bytesPerRow
);
1150 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1151 m_maskBitmap
= NULL
;
1157 m_width
= m_height
= m_bytesPerRow
= 0 ;
1158 m_maskBitmap
= NULL
;
1161 void *wxMask::GetRawAccess() const
1163 return m_memBuf
.GetData() ;
1166 void wxMask::RealizeNative()
1170 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1171 m_maskBitmap
= NULL
;
1173 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1174 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1175 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1178 // Create a mask from a mono bitmap (copies the bitmap).
1179 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1184 m_bytesPerRow
= bytesPerRow
;
1185 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1190 // Create a mask from a mono bitmap (copies the bitmap).
1191 bool wxMask::Create(const wxBitmap
& bitmap
)
1193 m_width
= bitmap
.GetWidth() ;
1194 m_height
= bitmap
.GetHeight() ;
1195 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1196 size_t size
= m_bytesPerRow
* m_height
;
1197 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1198 memset( destdatabase
, 0 , size
) ;
1199 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1200 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1202 unsigned char *destdata
= destdatabase
;
1203 for( int x
= 0 ; x
< m_width
; ++x
)
1206 unsigned char r
= *srcdata
++ ;
1207 unsigned char g
= *srcdata
++ ;
1208 unsigned char b
= *srcdata
++ ;
1209 if ( ( r
+ g
+ b
) > 0x10 )
1210 *destdata
++ = 0x00 ;
1212 *destdata
++ = 0xFF ;
1215 m_memBuf
.UngetWriteBuf( size
) ;
1220 // Create a mask from a bitmap and a colour indicating
1221 // the transparent area
1222 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1224 m_width
= bitmap
.GetWidth() ;
1225 m_height
= bitmap
.GetHeight() ;
1226 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1227 size_t size
= m_bytesPerRow
* m_height
;
1229 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1230 memset( destdatabase
, 0 , size
) ;
1231 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1232 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1234 unsigned char *destdata
= destdatabase
;
1235 for( int x
= 0 ; x
< m_width
; ++x
)
1238 unsigned char r
= *srcdata
++ ;
1239 unsigned char g
= *srcdata
++ ;
1240 unsigned char b
= *srcdata
++ ;
1241 if ( colour
== wxColour( r
, g
, b
) )
1242 *destdata
++ = 0x00 ;
1244 *destdata
++ = 0xFF ;
1247 m_memBuf
.UngetWriteBuf( size
) ;
1252 WXHBITMAP
wxMask::GetHBITMAP() const
1254 return m_maskBitmap
;
1257 // ----------------------------------------------------------------------------
1259 // ----------------------------------------------------------------------------
1261 wxBitmapHandler::~wxBitmapHandler()
1265 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1270 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1271 int desiredWidth
, int desiredHeight
)
1276 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1281 // ----------------------------------------------------------------------------
1282 // Standard Handlers
1283 // ----------------------------------------------------------------------------
1285 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1287 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1289 inline wxPICTResourceHandler()
1291 SetName(wxT("Macintosh Pict resource"));
1292 SetExtension(wxEmptyString
);
1293 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1296 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1297 int desiredWidth
, int desiredHeight
);
1299 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1301 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1302 int desiredWidth
, int desiredHeight
)
1305 wxMacStringToPascal( name
, theName
) ;
1307 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1311 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1312 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1314 dc
.SelectObject( *bitmap
) ;
1316 dc
.SelectObject( wxNullBitmap
) ;
1322 void wxBitmap::InitStandardHandlers()
1324 AddHandler(new wxPICTResourceHandler
) ;
1325 AddHandler(new wxICONResourceHandler
) ;
1328 // ----------------------------------------------------------------------------
1329 // raw bitmap access support
1330 // ----------------------------------------------------------------------------
1332 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1336 // no bitmap, no data (raw or otherwise)
1340 data
.m_width
= GetWidth() ;
1341 data
.m_height
= GetHeight() ;
1342 data
.m_stride
= GetWidth() * 4 ;
1343 return GetRawAccess() ;
1346 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1351 // TODO : if we have some information about the API we should check
1352 // this code looks strange...
1354 if ( M_BITMAPDATA
->HasAlpha() )
1356 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1358 int w
= data
.GetWidth(),
1359 h
= data
.GetHeight();
1361 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1362 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1363 wxAlphaPixelData::Iterator
pMask(dataMask
),
1365 for ( int y
= 0; y
< h
; y
++ )
1367 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1370 for ( int x
= 0; x
< w
; x
++ )
1372 const wxAlphaPixelData::Iterator::ChannelType
1375 pMask
.Red() = alpha
;
1376 pMask
.Green() = alpha
;
1377 pMask
.Blue() = alpha
;
1386 pMask
= rowStartMask
;
1387 pMask
.OffsetY(dataMask
, 1);
1390 SetMask(new wxMask(bmpMask
));
1394 void wxBitmap::UseAlpha()
1396 // remember that we are using alpha channel, we'll need to create a proper
1397 // mask in UngetRawData()
1398 M_BITMAPDATA
->UseAlpha( true );