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") ) ;
257 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
259 void wxBitmapRefData::Init()
265 m_bitmapMask
= NULL
;
266 #if wxMAC_USE_CORE_GRAPHICS
267 m_cgImageRef
= NULL
;
270 m_hMaskBitmap
= NULL
;
271 m_maskBytesPerRow
= NULL
;
273 m_rawAccessCount
= 0 ;
277 wxBitmapRefData::wxBitmapRefData()
282 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
285 Create( w
, h
, d
) ;
288 bool wxBitmapRefData::Create( int w
, int h
, int d
)
294 m_bytesPerRow
= w
* 4 ;
295 size_t size
= m_bytesPerRow
* h
;
296 void* data
= m_memBuf
.GetWriteBuf(size
) ;
297 memset( data
, 0 , size
) ;
298 m_memBuf
.UngetWriteBuf(size
) ;
301 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
302 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
303 (char*) data
, m_bytesPerRow
) ) ;
304 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
305 m_ok
= ( m_hBitmap
!= NULL
) ;
310 void wxBitmapRefData::UseAlpha( bool use
)
312 if ( m_hasAlpha
== use
)
318 int width
= GetWidth() ;
319 int height
= GetHeight() ;
320 m_maskBytesPerRow
= ( width
+ 3 ) & 0xFFFFFFC ;
321 size_t size
= height
* m_maskBytesPerRow
;
322 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
323 memset( data
, 0 , size
) ;
324 wxASSERT( m_hMaskBitmap
== NULL
) ;
325 Rect rect
= { 0 , 0 , height
, width
} ;
326 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
327 (char*) data
, m_maskBytesPerRow
) ) ;
328 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
329 m_maskMemBuf
.UngetWriteBuf(size
) ;
330 #if !wxMAC_USE_CORE_GRAPHICS
336 DisposeGWorld( m_hMaskBitmap
) ;
337 m_hMaskBitmap
= NULL
;
338 m_maskBytesPerRow
= 0 ;
342 void *wxBitmapRefData::GetRawAccess() const
344 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
345 return m_memBuf
.GetData() ;
348 void *wxBitmapRefData::BeginRawAccess()
350 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
351 wxASSERT( m_rawAccessCount
== 0 ) ;
353 #if wxMAC_USE_CORE_GRAPHICS
354 // we must destroy an existing cached image, as
355 // the bitmap data may change now
358 CGImageRelease( m_cgImageRef
) ;
359 m_cgImageRef
= NULL
;
362 return m_memBuf
.GetData() ;
365 void wxBitmapRefData::EndRawAccess()
367 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
368 wxASSERT( m_rawAccessCount
== 1 ) ;
370 #if !wxMAC_USE_CORE_GRAPHICS
376 #if wxMAC_USE_CORE_GRAPHICS
377 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t size
)
379 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
380 wxASSERT( data
== membuf
->GetData() ) ;
384 CGImageRef
wxBitmapRefData::CGImageCreate() const
387 wxASSERT( m_rawAccessCount
>= 0 ) ;
389 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
391 size_t imageSize
= m_width
* m_height
* 4 ;
392 void * dataBuffer
= m_memBuf
.GetData() ;
395 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
396 wxMemoryBuffer
* membuf
= NULL
;
400 membuf
= new wxMemoryBuffer( imageSize
) ;
401 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
402 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
403 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
404 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
405 alphaInfo
= kCGImageAlphaFirst
;
406 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
408 unsigned char *sourcemask
= sourcemaskstart
;
409 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
411 *destalpha
= *sourcemask
;
415 else if ( m_hasAlpha
)
417 #if wxMAC_USE_PREMULTIPLIED_ALPHA
418 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
420 alphaInfo
= kCGImageAlphaFirst
;
422 membuf
= new wxMemoryBuffer( m_memBuf
) ;
426 membuf
= new wxMemoryBuffer( m_memBuf
) ;
428 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
429 CGDataProviderRef dataProvider
=
430 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
,
431 wxMacMemoryBufferReleaseProc
);
433 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
434 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
435 CGDataProviderRelease( dataProvider
);
439 image
= m_cgImageRef
;
440 CGImageRetain( image
) ;
442 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
444 // we keep it for later use
445 m_cgImageRef
= image
;
446 CGImageRetain( image
) ;
452 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
454 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
459 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
460 else if ( m_hasAlpha
)
462 #if !wxMAC_USE_CORE_GRAPHICS
463 if ( m_rawAccessCount
> 0 )
466 // this structure is not kept in synch when using CG, so if someone
467 // is really accessing the Graphports, we have to sync it
470 *mask
= m_hMaskBitmap
;
476 void wxBitmapRefData::UpdateAlphaMask() const
480 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
481 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
483 int h
= GetHeight() ;
486 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
488 unsigned char* destalpha
= destalphabase
;
489 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4, ++destalpha
)
491 *destalpha
= *sourcemask
;
497 void wxBitmapRefData::Free()
499 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
501 #if wxMAC_USE_CORE_GRAPHICS
504 CGImageRelease( m_cgImageRef
) ;
505 m_cgImageRef
= NULL
;
510 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
515 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
516 m_hMaskBitmap
= NULL
;
526 wxBitmapRefData::~wxBitmapRefData()
531 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
533 int w
= icon
.GetWidth() ;
534 int h
= icon
.GetHeight() ;
535 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
537 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
539 IconFamilyHandle iconFamily
= NULL
;
540 Handle imagehandle
= NewHandle(0) ;
541 Handle maskhandle
= NewHandle(0) ;
545 IconSelectorValue selector
= 0 ;
548 dataType
= kThumbnail32BitData
;
549 maskType
= kThumbnail8BitMask
;
550 selector
= kSelectorAllAvailableData
;
554 dataType
= kHuge32BitData
;
555 maskType
= kHuge8BitMask
;
556 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
560 dataType
= kLarge32BitData
;
561 maskType
= kLarge8BitMask
;
562 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
566 dataType
= kSmall32BitData
;
567 maskType
= kSmall8BitMask
;
568 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
572 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
576 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ) ;
578 err
=( GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ) ;
579 err
=( GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ) ;
580 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
581 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
583 unsigned char *source
= (unsigned char *) *imagehandle
;
584 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
586 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
587 for ( int y
= 0 ; y
< h
; ++y
)
589 for ( int x
= 0 ; x
< w
; ++x
)
591 *destination
++ = *sourcemask
++ ;
593 *destination
++ = *source
++ ;
594 *destination
++ = *source
++ ;
595 *destination
++ = *source
++ ;
599 DisposeHandle( imagehandle
) ;
600 DisposeHandle( maskhandle
) ;
605 dc
.SelectObject( *this ) ;
606 dc
.DrawIcon( icon
, 0 , 0 ) ;
607 dc
.SelectObject( wxNullBitmap
) ;
616 wxBitmap::~wxBitmap()
620 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
622 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
626 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
627 if ( the_width
% (sizeof(unsigned char) * 8) ) {
628 linesize
+= sizeof(unsigned char);
630 unsigned char* linestart
= (unsigned char*) bits
;
631 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
632 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
634 for ( int x
= 0 ; x
< the_width
; ++x
)
638 int mask
= 1 << bit
;
639 if ( linestart
[index
] & mask
)
641 *destination
++ = 0xFF ;
648 *destination
++ = 0xFF ;
649 *destination
++ = 0xFF ;
650 *destination
++ = 0xFF ;
651 *destination
++ = 0xFF ;
659 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
663 wxBitmap::wxBitmap(int w
, int h
, int d
)
665 (void)Create(w
, h
, d
);
668 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
670 (void) Create(data
, type
, width
, height
, depth
);
673 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
675 LoadFile(filename
, type
);
678 wxBitmap::wxBitmap(const char **bits
)
680 (void) CreateFromXpm(bits
);
683 wxBitmap::wxBitmap(char **bits
)
685 (void) CreateFromXpm((const char **)bits
);
688 void* wxBitmap::GetRawAccess() const
690 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
691 return M_BITMAPDATA
->GetRawAccess() ;
694 void* wxBitmap::BeginRawAccess()
696 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
697 return M_BITMAPDATA
->BeginRawAccess() ;
700 void wxBitmap::EndRawAccess()
702 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
703 M_BITMAPDATA
->EndRawAccess() ;
706 bool wxBitmap::CreateFromXpm(const char **bits
)
708 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
709 wxXPMDecoder decoder
;
710 wxImage img
= decoder
.ReadData(bits
);
711 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
712 *this = wxBitmap(img
);
716 #if wxMAC_USE_CORE_GRAPHICS
717 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
719 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
720 return M_BITMAPDATA
->CGImageCreate() ;
724 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
727 (rect
.x
>= 0) && (rect
.y
>= 0) &&
728 (rect
.x
+rect
.width
<= GetWidth()) &&
729 (rect
.y
+rect
.height
<= GetHeight()),
730 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
733 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
734 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
737 int sourcewidth
= GetWidth() ;
738 int destwidth
= rect
.width
;
739 int destheight
= rect
.height
;
741 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
742 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
743 int sourcelinesize
= sourcewidth
* 4 ;
744 int destlinesize
= destwidth
* 4 ;
745 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
746 unsigned char *dest
= destdata
;
747 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
749 memcpy( dest
, source
, destlinesize
) ;
754 if ( M_BITMAPDATA
->m_bitmapMask
)
756 wxMemoryBuffer maskbuf
;
757 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
758 size_t maskbufsize
= rowBytes
* destheight
;
759 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
761 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
762 int destlinesize
= rowBytes
;
763 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
764 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
765 unsigned char *dest
= destdata
;
767 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
769 memcpy( dest
, source
, destlinesize
) ;
771 maskbuf
.UngetWriteBuf( maskbufsize
) ;
772 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
774 else if ( HasAlpha() )
780 bool wxBitmap::Create(int w
, int h
, int d
)
785 d
= wxDisplayDepth() ;
787 m_refData
= new wxBitmapRefData( w
, h
, d
);
789 return M_BITMAPDATA
->Ok() ;
792 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
796 wxBitmapHandler
*handler
= FindHandler(type
);
800 m_refData
= new wxBitmapRefData
;
802 return handler
->LoadFile(this, filename
, type
, -1, -1);
806 wxImage
loadimage(filename
, type
);
807 if (loadimage
.Ok()) {
812 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
816 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
820 m_refData
= new wxBitmapRefData
;
822 wxBitmapHandler
*handler
= FindHandler(type
);
824 if ( handler
== NULL
) {
825 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
830 return handler
->Create(this, data
, type
, width
, height
, depth
);
833 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
835 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
837 // width and height of the device-dependent bitmap
838 int width
= image
.GetWidth();
839 int height
= image
.GetHeight();
841 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
845 bool hasAlpha
= false ;
847 if ( image
.HasMask() )
849 // takes precedence, don't mix with alpha info
853 hasAlpha
= image
.HasAlpha() ;
859 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
861 register unsigned char* data
= image
.GetData();
862 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
863 for (int y
= 0; y
< height
; y
++)
865 for (int x
= 0; x
< width
; x
++)
869 const unsigned char a
= *alpha
++;
871 #if wxMAC_USE_PREMULTIPLIED_ALPHA
872 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
873 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
874 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
876 *destination
++ = *data
++ ;
877 *destination
++ = *data
++ ;
878 *destination
++ = *data
++ ;
883 *destination
++ = 0xFF ;
884 *destination
++ = *data
++ ;
885 *destination
++ = *data
++ ;
886 *destination
++ = *data
++ ;
891 if ( image
.HasMask() )
893 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
897 wxImage
wxBitmap::ConvertToImage() const
901 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
903 // create an wxImage object
904 int width
= GetWidth();
905 int height
= GetHeight();
906 image
.Create( width
, height
);
908 unsigned char *data
= image
.GetData();
909 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
911 unsigned char* source
= (unsigned char*) GetRawAccess() ;
913 bool hasAlpha
= false ;
914 bool hasMask
= false ;
915 unsigned char *alpha
= NULL
;
916 unsigned char *mask
= NULL
;
925 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
931 alpha
= image
.GetAlpha() ;
935 // The following masking algorithm is the same as well in msw/gtk:
936 // the colour used as transparent one in wxImage and the one it is
937 // replaced with when it really occurs in the bitmap
938 static const int MASK_RED
= 1;
939 static const int MASK_GREEN
= 2;
940 static const int MASK_BLUE
= 3;
941 static const int MASK_BLUE_REPLACEMENT
= 2;
943 for (int yy
= 0; yy
< height
; yy
++)
945 for (int xx
= 0; xx
< width
; xx
++)
947 long color
= *((long*) source
) ;
948 unsigned char a
= ((color
&0xFF000000) >> 24) ;
949 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
950 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
951 unsigned char b
= (color
&0x000000FF);
956 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
957 b
= MASK_BLUE_REPLACEMENT
;
970 data
[index
+ 1] = g
;
971 data
[index
+ 2] = b
;
977 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
982 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
983 const wxPalette
*palette
) const
985 wxBitmapHandler
*handler
= FindHandler(type
);
989 return handler
->SaveFile(this, filename
, type
, palette
);
993 wxImage image
= ConvertToImage();
995 return image
.SaveFile(filename
, type
);
998 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1002 bool wxBitmap::Ok() const
1004 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1007 int wxBitmap::GetHeight() const
1009 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1011 return M_BITMAPDATA
->GetHeight();
1014 int wxBitmap::GetWidth() const
1016 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1018 return M_BITMAPDATA
->GetWidth() ;
1021 int wxBitmap::GetDepth() const
1023 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1025 return M_BITMAPDATA
->GetDepth();
1028 int wxBitmap::GetQuality() const
1033 wxMask
*wxBitmap::GetMask() const
1035 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1037 return M_BITMAPDATA
->m_bitmapMask
;
1040 bool wxBitmap::HasAlpha() const
1042 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1044 return M_BITMAPDATA
->HasAlpha() ;
1047 void wxBitmap::SetWidth(int w
)
1050 m_refData
= new wxBitmapRefData
;
1052 M_BITMAPDATA
->SetWidth(w
);
1055 void wxBitmap::SetHeight(int h
)
1058 m_refData
= new wxBitmapRefData
;
1060 M_BITMAPDATA
->SetHeight(h
);
1063 void wxBitmap::SetDepth(int d
)
1066 m_refData
= new wxBitmapRefData
;
1068 M_BITMAPDATA
->SetDepth(d
);
1071 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1075 void wxBitmap::SetOk(bool isOk
)
1078 m_refData
= new wxBitmapRefData
;
1080 M_BITMAPDATA
->SetOk(isOk
);
1084 wxPalette
*wxBitmap::GetPalette() const
1086 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1088 return &M_BITMAPDATA
->m_bitmapPalette
;
1091 void wxBitmap::SetPalette(const wxPalette
& palette
)
1094 m_refData
= new wxBitmapRefData
;
1096 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1098 #endif // wxUSE_PALETTE
1100 void wxBitmap::SetMask(wxMask
*mask
)
1103 m_refData
= new wxBitmapRefData
;
1105 // Remove existing mask if there is one.
1106 delete M_BITMAPDATA
->m_bitmapMask
;
1108 M_BITMAPDATA
->m_bitmapMask
= mask
;
1111 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1113 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1116 // ----------------------------------------------------------------------------
1118 // ----------------------------------------------------------------------------
1125 // Construct a mask from a bitmap and a colour indicating
1126 // the transparent area
1127 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1130 Create(bitmap
, colour
);
1133 // Construct a mask from a mono bitmap (copies the bitmap).
1134 wxMask::wxMask(const wxBitmap
& bitmap
)
1140 // Construct a mask from a mono bitmap (copies the bitmap).
1141 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1144 Create(data
, width
, height
, bytesPerRow
);
1151 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1152 m_maskBitmap
= NULL
;
1158 m_width
= m_height
= m_bytesPerRow
= 0 ;
1159 m_maskBitmap
= NULL
;
1162 void *wxMask::GetRawAccess() const
1164 return m_memBuf
.GetData() ;
1167 void wxMask::RealizeNative()
1171 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1172 m_maskBitmap
= NULL
;
1174 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1175 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1176 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1179 // Create a mask from a mono bitmap (copies the bitmap).
1180 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1185 m_bytesPerRow
= bytesPerRow
;
1186 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1191 // Create a mask from a mono bitmap (copies the bitmap).
1192 bool wxMask::Create(const wxBitmap
& bitmap
)
1194 m_width
= bitmap
.GetWidth() ;
1195 m_height
= bitmap
.GetHeight() ;
1196 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1197 size_t size
= m_bytesPerRow
* m_height
;
1198 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1199 memset( destdatabase
, 0 , size
) ;
1200 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1201 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1203 unsigned char *destdata
= destdatabase
;
1204 for( int x
= 0 ; x
< m_width
; ++x
)
1207 unsigned char r
= *srcdata
++ ;
1208 unsigned char g
= *srcdata
++ ;
1209 unsigned char b
= *srcdata
++ ;
1210 if ( ( r
+ g
+ b
) > 0x10 )
1211 *destdata
++ = 0x00 ;
1213 *destdata
++ = 0xFF ;
1216 m_memBuf
.UngetWriteBuf( size
) ;
1221 // Create a mask from a bitmap and a colour indicating
1222 // the transparent area
1223 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1225 m_width
= bitmap
.GetWidth() ;
1226 m_height
= bitmap
.GetHeight() ;
1227 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1228 size_t size
= m_bytesPerRow
* m_height
;
1230 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1231 memset( destdatabase
, 0 , size
) ;
1232 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1233 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1235 unsigned char *destdata
= destdatabase
;
1236 for( int x
= 0 ; x
< m_width
; ++x
)
1239 unsigned char r
= *srcdata
++ ;
1240 unsigned char g
= *srcdata
++ ;
1241 unsigned char b
= *srcdata
++ ;
1242 if ( colour
== wxColour( r
, g
, b
) )
1243 *destdata
++ = 0x00 ;
1245 *destdata
++ = 0xFF ;
1248 m_memBuf
.UngetWriteBuf( size
) ;
1253 WXHBITMAP
wxMask::GetHBITMAP() const
1255 return m_maskBitmap
;
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1262 wxBitmapHandler::~wxBitmapHandler()
1266 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1271 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1272 int desiredWidth
, int desiredHeight
)
1277 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1282 // ----------------------------------------------------------------------------
1283 // Standard Handlers
1284 // ----------------------------------------------------------------------------
1286 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1288 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1290 inline wxPICTResourceHandler()
1292 SetName(wxT("Macintosh Pict resource"));
1293 SetExtension(wxEmptyString
);
1294 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1297 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1298 int desiredWidth
, int desiredHeight
);
1300 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1302 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1303 int desiredWidth
, int desiredHeight
)
1306 wxMacStringToPascal( name
, theName
) ;
1308 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1312 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1313 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1315 dc
.SelectObject( *bitmap
) ;
1317 dc
.SelectObject( wxNullBitmap
) ;
1323 void wxBitmap::InitStandardHandlers()
1325 AddHandler(new wxPICTResourceHandler
) ;
1326 AddHandler(new wxICONResourceHandler
) ;
1329 // ----------------------------------------------------------------------------
1330 // raw bitmap access support
1331 // ----------------------------------------------------------------------------
1333 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1337 // no bitmap, no data (raw or otherwise)
1341 data
.m_width
= GetWidth() ;
1342 data
.m_height
= GetHeight() ;
1343 data
.m_stride
= GetWidth() * 4 ;
1344 return GetRawAccess() ;
1347 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1352 // TODO : if we have some information about the API we should check
1353 // this code looks strange...
1355 if ( M_BITMAPDATA
->HasAlpha() )
1357 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1359 int w
= data
.GetWidth(),
1360 h
= data
.GetHeight();
1362 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1363 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1364 wxAlphaPixelData::Iterator
pMask(dataMask
),
1366 for ( int y
= 0; y
< h
; y
++ )
1368 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1371 for ( int x
= 0; x
< w
; x
++ )
1373 const wxAlphaPixelData::Iterator::ChannelType
1376 pMask
.Red() = alpha
;
1377 pMask
.Green() = alpha
;
1378 pMask
.Blue() = alpha
;
1387 pMask
= rowStartMask
;
1388 pMask
.OffsetY(dataMask
, 1);
1391 SetMask(new wxMask(bmpMask
));
1395 void wxBitmap::UseAlpha()
1397 // remember that we are using alpha channel, we'll need to create a proper
1398 // mask in UngetRawData()
1399 M_BITMAPDATA
->UseAlpha( true );