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 // Implementation Notes
40 // --------------------
42 // we are always working with a 32 bit deep pixel buffer
43 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
44 // therefore we have a separate GWorld there for blitting the mask in
46 // under Quartz then content is transformed into a CGImageRef representing the same data
47 // which can be transferred to the GPU by the OS for fast rendering
49 // we don't dare premultiplied alpha yet
50 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
52 IconRef
wxMacCreateIconRef(const wxBitmap
& bmp
)
54 // setup the header properly
56 IconFamilyHandle iconFamily
= (IconFamilyHandle
) NewHandle(8) ;
57 (**iconFamily
).resourceType
= kIconFamilyType
;
58 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
60 int w
= bmp
.GetWidth() ;
61 int h
= bmp
.GetHeight() ;
62 int sz
= wxMax( w
, h
) ;
65 wxFAIL_MSG( wxT("Currently only 128 pixels wide images are supported") ) ;
69 Handle maskdata
= NULL
;
70 unsigned char * maskptr
= NULL
;
71 unsigned char * ptr
= NULL
;
77 bool hasAlpha
= bmp
.HasAlpha() ;
78 wxMask
*mask
= bmp
.GetMask() ;
79 // thumbnail is 128 x 128 with 32 bits per pixel
84 dataType
= kThumbnail32BitData
;
85 maskType
= kThumbnail8BitMask
;
90 dataType
= kHuge32BitData
;
91 maskType
= kHuge8BitMask
;
96 dataType
= kLarge32BitData
;
97 maskType
= kLarge8BitMask
;
102 dataType
= kSmall32BitData
;
103 maskType
= kSmall8BitMask
;
107 data
= NewHandle( size
) ;
109 ptr
= (unsigned char*) *data
;
110 memset( ptr
, 0, size
) ;
113 maskdata
= NewHandle( masksize
) ;
115 maskptr
= (unsigned char*) *maskdata
;
116 memset( maskptr
, 0 , masksize
) ;
118 unsigned char * source
= (unsigned char*) bmp
.GetRawAccess() ;
119 unsigned char * masksource
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
120 for ( int y
= 0 ; y
< h
; ++y
)
122 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
123 unsigned char * maskdest
= maskptr
+ y
* sz
;
124 for ( int x
= 0 ; x
< w
; ++x
)
126 unsigned char a
= *source
++ ;
127 unsigned char r
= *source
++ ;
128 unsigned char g
= *source
++ ;
129 unsigned char b
= *source
++ ;
137 *maskdest
++ = *masksource
++ ;
145 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
146 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
148 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
149 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
151 HUnlock( maskdata
) ;
152 DisposeHandle( data
) ;
153 DisposeHandle( maskdata
) ;
156 static int iconCounter
= 2 ;
158 err
= RegisterIconRefFromIconFamily( 'WXNG' , (OSType
) iconCounter
, iconFamily
, &iconRef
) ;
160 err
= GetIconRefOwners(iconRef
, &owners
) ;
162 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
163 // we have to retain a reference, as Unregister will decrement it
164 AcquireIconRef( iconRef
) ;
165 UnregisterIconRef( 'WXNG' , (OSType
) iconCounter
) ;
166 DisposeHandle( (Handle
) iconFamily
) ;
172 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
174 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
177 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
180 info
->contentType
= kControlContentIconRef
;
181 info
->u
.iconRef
= wxMacCreateIconRef( bitmap
) ;
183 #if wxMAC_USE_CORE_GRAPHICS
185 // only on 10.4 more controls will accept a CGImage
187 info->contentType = kControlContentCGImageRef ;
188 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
194 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
196 if ( info
->contentType
== kControlContentIconRef
)
198 ReleaseIconRef(info
->u
.iconRef
) ;
200 #if wxMAC_USE_CORE_GRAPHICS
201 else if ( info
->contentType
== kControlContentCGImageRef
)
203 CGImageRelease( info
->u
.imageRef
) ;
208 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
213 void wxBitmapRefData::Init()
219 m_bitmapMask
= NULL
;
220 #if wxMAC_USE_CORE_GRAPHICS
221 m_cgImageRef
= NULL
;
224 m_hMaskBitmap
= NULL
;
225 m_maskBytesPerRow
= NULL
;
227 m_rawAccessCount
= 0 ;
231 wxBitmapRefData::wxBitmapRefData()
236 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
239 Create( w
, h
, d
) ;
242 bool wxBitmapRefData::Create( int w
, int h
, int d
)
248 m_bytesPerRow
= w
* 4 ;
249 size_t size
= m_bytesPerRow
* h
;
250 void* data
= m_memBuf
.GetWriteBuf(size
) ;
251 memset( data
, 0 , size
) ;
252 m_memBuf
.UngetWriteBuf(size
) ;
253 #if wxMAC_USE_CORE_GRAPHICS
257 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
258 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
259 (char*) data
, m_bytesPerRow
) ) ;
260 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
261 m_ok
= ( m_hBitmap
!= NULL
) ;
266 void wxBitmapRefData::UseAlpha( bool use
)
268 if ( m_hasAlpha
== use
)
272 #if !wxMAC_USE_CORE_GRAPHICS
275 int width
= GetWidth() ;
276 int height
= GetHeight() ;
277 m_maskBytesPerRow
= ( width
+ 3 ) & 0xFFFFFFC ;
278 size_t size
= height
* m_maskBytesPerRow
;
279 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
280 memset( data
, 0 , size
) ;
281 wxASSERT( m_hMaskBitmap
== NULL
) ;
282 Rect rect
= { 0 , 0 , height
, width
} ;
283 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
284 (char*) data
, m_maskBytesPerRow
) ) ;
285 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
286 m_maskMemBuf
.UngetWriteBuf(size
) ;
291 DisposeGWorld( m_hMaskBitmap
) ;
292 m_hMaskBitmap
= NULL
;
293 m_maskBytesPerRow
= 0 ;
298 void *wxBitmapRefData::GetRawAccess() const
300 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
301 return m_memBuf
.GetData() ;
304 void *wxBitmapRefData::BeginRawAccess()
306 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
307 wxASSERT( m_rawAccessCount
== 0 ) ;
309 #if wxMAC_USE_CORE_GRAPHICS
310 // we must destroy an existing cached image, as
311 // the bitmap data may change now
314 CGImageRelease( m_cgImageRef
) ;
315 m_cgImageRef
= NULL
;
318 return m_memBuf
.GetData() ;
321 void wxBitmapRefData::EndRawAccess()
323 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
324 wxASSERT( m_rawAccessCount
== 1 ) ;
326 #if !wxMAC_USE_CORE_GRAPHICS
332 #if wxMAC_USE_CORE_GRAPHICS
333 static void FreeImageMemoryBufferInstance(void *info
, const void *data
, size_t size
)
335 delete ((wxMemoryBuffer
*)info
) ;
338 CGImageRef
wxBitmapRefData::CGImageCreate() const
341 wxASSERT( m_rawAccessCount
>= 0 ) ;
343 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
345 size_t imageSize
= m_width
* m_height
* 4 ;
346 void * dataBuffer
= m_memBuf
.GetData() ;
349 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
350 wxMemoryBuffer
* membuf
= NULL
;
354 membuf
= new wxMemoryBuffer( imageSize
) ;
355 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
356 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
357 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
358 unsigned char *destalpha
= (unsigned char *) membuf
->GetData() ;
359 alphaInfo
= kCGImageAlphaFirst
;
360 for ( int y
= 0 ; y
< h
; ++y
, sourcemaskstart
+= maskrowbytes
)
362 unsigned char *sourcemask
= sourcemaskstart
;
363 for( int x
= 0 ; x
< w
; ++x
, sourcemask
++ , destalpha
+= 4 )
365 *destalpha
= *sourcemask
;
369 else if ( m_hasAlpha
)
371 #if wxMAC_USE_PREMULTIPLIED_ALPHA
372 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
374 alphaInfo
= kCGImageAlphaFirst
;
376 membuf
= new wxMemoryBuffer( m_memBuf
) ;
380 membuf
= new wxMemoryBuffer( m_memBuf
) ;
382 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
383 CGDataProviderRef dataProvider
=
384 CGDataProviderCreateWithData( membuf
, (const void *)membuf
->GetData() , imageSize
, FreeImageMemoryBufferInstance
);
386 ::CGImageCreate( w
, h
, 8 , 32 , 4 * m_width
, colorSpace
, alphaInfo
,
387 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
388 CGDataProviderRelease( dataProvider
);
392 image
= m_cgImageRef
;
393 CGImageRetain( image
) ;
395 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
397 // we keep it for later use
398 m_cgImageRef
= image
;
399 CGImageRetain( image
) ;
405 #if !wxMAC_USE_CORE_GRAPHICS
406 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
408 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
413 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
414 else if ( m_hasAlpha
)
416 if ( m_rawAccessCount
> 0 )
418 *mask
= m_hMaskBitmap
;
424 void wxBitmapRefData::UpdateAlphaMask() const
428 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
429 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
431 int h
= GetHeight() ;
434 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
436 unsigned char* destalpha
= destalphabase
;
437 for( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4, ++destalpha
)
439 *destalpha
= *sourcemask
;
447 void wxBitmapRefData::Free()
449 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
451 #if wxMAC_USE_CORE_GRAPHICS
454 CGImageRelease( m_cgImageRef
) ;
455 m_cgImageRef
= NULL
;
460 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
465 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
466 m_hMaskBitmap
= NULL
;
477 wxBitmapRefData::~wxBitmapRefData()
482 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
484 int w
= icon
.GetWidth() ;
485 int h
= icon
.GetHeight() ;
486 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
488 if ( w
== h
&& w
== 32 )
490 IconFamilyHandle iconFamily
= NULL
;
491 Handle imagehandle
= NewHandle(0) ;
492 Handle maskhandle
= NewHandle(0) ;
493 OSStatus err
= ( IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , kSelectorLarge32Bit
| kSelectorLarge8BitMask
, &iconFamily
) ) ;
494 err
=( GetIconFamilyData( iconFamily
, kLarge32BitData
, imagehandle
) ) ;
495 err
=( GetIconFamilyData( iconFamily
, kLarge8BitMask
, maskhandle
) ) ;
496 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
497 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
499 unsigned char *source
= (unsigned char *) *imagehandle
;
500 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
502 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
503 for ( int y
= 0 ; y
< h
; ++y
)
505 for ( int x
= 0 ; x
< w
; ++x
)
507 *destination
++ = *sourcemask
++ ;
509 *destination
++ = *source
++ ;
510 *destination
++ = *source
++ ;
511 *destination
++ = *source
++ ;
515 DisposeHandle( imagehandle
) ;
516 DisposeHandle( maskhandle
) ;
521 dc
.SelectObject( *this ) ;
522 dc
.DrawIcon( icon
, 0 , 0 ) ;
523 dc
.SelectObject( wxNullBitmap
) ;
532 wxBitmap::~wxBitmap()
536 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
538 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
542 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
543 if ( the_width
% (sizeof(unsigned char) * 8) ) {
544 linesize
+= sizeof(unsigned char);
546 unsigned char* linestart
= (unsigned char*) bits
;
547 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
548 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
550 for ( int x
= 0 ; x
< the_width
; ++x
)
554 int mask
= 1 << bit
;
555 if ( linestart
[index
] & mask
)
557 *destination
++ = 0xFF ;
564 *destination
++ = 0xFF ;
565 *destination
++ = 0xFF ;
566 *destination
++ = 0xFF ;
567 *destination
++ = 0xFF ;
575 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
579 wxBitmap::wxBitmap(int w
, int h
, int d
)
581 (void)Create(w
, h
, d
);
584 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
586 (void) Create(data
, type
, width
, height
, depth
);
589 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
591 LoadFile(filename
, type
);
594 wxBitmap::wxBitmap(const char **bits
)
596 (void) CreateFromXpm(bits
);
599 wxBitmap::wxBitmap(char **bits
)
601 (void) CreateFromXpm((const char **)bits
);
604 void* wxBitmap::GetRawAccess() const
606 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
607 return M_BITMAPDATA
->GetRawAccess() ;
610 void* wxBitmap::BeginRawAccess()
612 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
613 return M_BITMAPDATA
->BeginRawAccess() ;
616 void wxBitmap::EndRawAccess()
618 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
619 M_BITMAPDATA
->EndRawAccess() ;
622 bool wxBitmap::CreateFromXpm(const char **bits
)
624 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
625 wxXPMDecoder decoder
;
626 wxImage img
= decoder
.ReadData(bits
);
627 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
628 *this = wxBitmap(img
);
632 #if wxMAC_USE_CORE_GRAPHICS
633 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
635 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
636 return M_BITMAPDATA
->CGImageCreate() ;
640 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
643 (rect
.x
>= 0) && (rect
.y
>= 0) &&
644 (rect
.x
+rect
.width
<= GetWidth()) &&
645 (rect
.y
+rect
.height
<= GetHeight()),
646 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
649 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
650 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
653 int sourcewidth
= GetWidth() ;
654 int destwidth
= rect
.width
;
655 int destheight
= rect
.height
;
657 unsigned char * sourcedata
= (unsigned char*) GetRawAccess() ;
658 unsigned char * destdata
= (unsigned char*) ret
.BeginRawAccess() ;
659 int sourcelinesize
= sourcewidth
* 4 ;
660 int destlinesize
= destwidth
* 4 ;
661 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
662 unsigned char *dest
= destdata
;
663 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
665 memcpy( dest
, source
, destlinesize
) ;
670 if ( M_BITMAPDATA
->m_bitmapMask
)
672 wxMemoryBuffer maskbuf
;
673 int rowBytes
= ( destwidth
+ 3 ) & 0xFFFFFFC ;
674 size_t maskbufsize
= rowBytes
* destheight
;
675 unsigned char * destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
677 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
678 int destlinesize
= rowBytes
;
679 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
680 source
+= rect
.x
+ rect
.y
* sourcelinesize
;
681 unsigned char *dest
= destdata
;
683 for(int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
685 memcpy( dest
, source
, destlinesize
) ;
687 maskbuf
.UngetWriteBuf( maskbufsize
) ;
688 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
690 else if ( HasAlpha() )
696 bool wxBitmap::Create(int w
, int h
, int d
)
701 d
= wxDisplayDepth() ;
703 m_refData
= new wxBitmapRefData( w
, h
, d
);
705 return M_BITMAPDATA
->Ok() ;
708 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
712 wxBitmapHandler
*handler
= FindHandler(type
);
716 m_refData
= new wxBitmapRefData
;
718 return handler
->LoadFile(this, filename
, type
, -1, -1);
722 wxImage
loadimage(filename
, type
);
723 if (loadimage
.Ok()) {
728 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
732 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
736 m_refData
= new wxBitmapRefData
;
738 wxBitmapHandler
*handler
= FindHandler(type
);
740 if ( handler
== NULL
) {
741 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
746 return handler
->Create(this, data
, type
, width
, height
, depth
);
749 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
751 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
753 // width and height of the device-dependent bitmap
754 int width
= image
.GetWidth();
755 int height
= image
.GetHeight();
757 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;;
761 bool hasAlpha
= false ;
763 if ( image
.HasMask() )
765 // takes precedence, don't mix with alpha info
769 hasAlpha
= image
.HasAlpha() ;
775 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
777 register unsigned char* data
= image
.GetData();
778 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
779 for (int y
= 0; y
< height
; y
++)
781 for (int x
= 0; x
< width
; x
++)
785 const unsigned char a
= *alpha
++;
787 #if wxMAC_USE_PREMULTIPLIED_ALPHA
788 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
789 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
790 *destination
++ = ((*data
++) * a
+ 127 ) / 255 ;
792 *destination
++ = *data
++ ;
793 *destination
++ = *data
++ ;
794 *destination
++ = *data
++ ;
799 *destination
++ = 0xFF ;
800 *destination
++ = *data
++ ;
801 *destination
++ = *data
++ ;
802 *destination
++ = *data
++ ;
807 if ( image
.HasMask() )
809 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
813 wxImage
wxBitmap::ConvertToImage() const
817 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
819 // create an wxImage object
820 int width
= GetWidth();
821 int height
= GetHeight();
822 image
.Create( width
, height
);
824 unsigned char *data
= image
.GetData();
825 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
827 unsigned char* source
= (unsigned char*) GetRawAccess() ;
829 bool hasAlpha
= false ;
830 bool hasMask
= false ;
831 unsigned char *alpha
= NULL
;
832 unsigned char *mask
= NULL
;
841 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
847 alpha
= image
.GetAlpha() ;
851 // The following masking algorithm is the same as well in msw/gtk:
852 // the colour used as transparent one in wxImage and the one it is
853 // replaced with when it really occurs in the bitmap
854 static const int MASK_RED
= 1;
855 static const int MASK_GREEN
= 2;
856 static const int MASK_BLUE
= 3;
857 static const int MASK_BLUE_REPLACEMENT
= 2;
859 for (int yy
= 0; yy
< height
; yy
++)
861 for (int xx
= 0; xx
< width
; xx
++)
863 long color
= *((long*) source
) ;
864 unsigned char a
= ((color
&0xFF000000) >> 24) ;
865 unsigned char r
= ((color
&0x00FF0000) >> 16) ;
866 unsigned char g
= ((color
&0x0000FF00) >> 8) ;
867 unsigned char b
= (color
&0x000000FF);
872 if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
873 b
= MASK_BLUE_REPLACEMENT
;
886 data
[index
+ 1] = g
;
887 data
[index
+ 2] = b
;
893 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
898 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
899 const wxPalette
*palette
) const
901 wxBitmapHandler
*handler
= FindHandler(type
);
905 return handler
->SaveFile(this, filename
, type
, palette
);
909 wxImage image
= ConvertToImage();
911 return image
.SaveFile(filename
, type
);
914 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
918 bool wxBitmap::Ok() const
920 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
923 int wxBitmap::GetHeight() const
925 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
927 return M_BITMAPDATA
->GetHeight();
930 int wxBitmap::GetWidth() const
932 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
934 return M_BITMAPDATA
->GetWidth() ;
937 int wxBitmap::GetDepth() const
939 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
941 return M_BITMAPDATA
->GetDepth();
944 int wxBitmap::GetQuality() const
949 wxMask
*wxBitmap::GetMask() const
951 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
953 return M_BITMAPDATA
->m_bitmapMask
;
956 bool wxBitmap::HasAlpha() const
958 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
960 return M_BITMAPDATA
->HasAlpha() ;
963 void wxBitmap::SetWidth(int w
)
966 m_refData
= new wxBitmapRefData
;
968 M_BITMAPDATA
->SetWidth(w
);
971 void wxBitmap::SetHeight(int h
)
974 m_refData
= new wxBitmapRefData
;
976 M_BITMAPDATA
->SetHeight(h
);
979 void wxBitmap::SetDepth(int d
)
982 m_refData
= new wxBitmapRefData
;
984 M_BITMAPDATA
->SetDepth(d
);
987 void wxBitmap::SetQuality(int WXUNUSED(quality
))
991 void wxBitmap::SetOk(bool isOk
)
994 m_refData
= new wxBitmapRefData
;
996 M_BITMAPDATA
->SetOk(isOk
);
1000 wxPalette
*wxBitmap::GetPalette() const
1002 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1004 return &M_BITMAPDATA
->m_bitmapPalette
;
1007 void wxBitmap::SetPalette(const wxPalette
& palette
)
1010 m_refData
= new wxBitmapRefData
;
1012 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1014 #endif // wxUSE_PALETTE
1016 void wxBitmap::SetMask(wxMask
*mask
)
1019 m_refData
= new wxBitmapRefData
;
1021 // Remove existing mask if there is one.
1022 delete M_BITMAPDATA
->m_bitmapMask
;
1024 M_BITMAPDATA
->m_bitmapMask
= mask
;
1027 #if !wxMAC_USE_CORE_GRAPHICS
1028 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1030 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1034 // ----------------------------------------------------------------------------
1036 // ----------------------------------------------------------------------------
1043 // Construct a mask from a bitmap and a colour indicating
1044 // the transparent area
1045 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1048 Create(bitmap
, colour
);
1051 // Construct a mask from a mono bitmap (copies the bitmap).
1052 wxMask::wxMask(const wxBitmap
& bitmap
)
1058 // Construct a mask from a mono bitmap (copies the bitmap).
1059 wxMask::wxMask(const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1062 Create(data
, width
, height
, bytesPerRow
);
1067 #if !wxMAC_USE_CORE_GRAPHICS
1070 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1071 m_maskBitmap
= NULL
;
1078 m_width
= m_height
= m_bytesPerRow
= 0 ;
1079 #if !wxMAC_USE_CORE_GRAPHICS
1080 m_maskBitmap
= NULL
;
1085 void *wxMask::GetRawAccess() const
1087 return m_memBuf
.GetData() ;
1090 void wxMask::RealizeNative()
1092 #if !wxMAC_USE_CORE_GRAPHICS
1095 DisposeGWorld( (GWorldPtr
) m_maskBitmap
) ;
1096 m_maskBitmap
= NULL
;
1098 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1099 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_maskBitmap
, k8IndexedGrayPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1100 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ) ;
1104 // Create a mask from a mono bitmap (copies the bitmap).
1105 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1110 m_bytesPerRow
= bytesPerRow
;
1111 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1116 // Create a mask from a mono bitmap (copies the bitmap).
1117 bool wxMask::Create(const wxBitmap
& bitmap
)
1119 m_width
= bitmap
.GetWidth() ;
1120 m_height
= bitmap
.GetHeight() ;
1121 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1122 size_t size
= m_bytesPerRow
* m_height
;
1123 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1124 memset( destdatabase
, 0 , size
) ;
1125 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1126 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1128 unsigned char *destdata
= destdatabase
;
1129 for( int x
= 0 ; x
< m_width
; ++x
)
1132 unsigned char r
= *srcdata
++ ;
1133 unsigned char g
= *srcdata
++ ;
1134 unsigned char b
= *srcdata
++ ;
1135 if ( ( r
+ g
+ b
) > 0x10 )
1136 *destdata
++ = 0x00 ;
1138 *destdata
++ = 0xFF ;
1141 m_memBuf
.UngetWriteBuf( size
) ;
1146 // Create a mask from a bitmap and a colour indicating
1147 // the transparent area
1148 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1150 m_width
= bitmap
.GetWidth() ;
1151 m_height
= bitmap
.GetHeight() ;
1152 m_bytesPerRow
= ( m_width
+ 3 ) & 0xFFFFFFC ;
1153 size_t size
= m_bytesPerRow
* m_height
;
1155 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1156 memset( destdatabase
, 0 , size
) ;
1157 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1158 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1160 unsigned char *destdata
= destdatabase
;
1161 for( int x
= 0 ; x
< m_width
; ++x
)
1164 unsigned char r
= *srcdata
++ ;
1165 unsigned char g
= *srcdata
++ ;
1166 unsigned char b
= *srcdata
++ ;
1167 if ( colour
== wxColour( r
, g
, b
) )
1168 *destdata
++ = 0x00 ;
1170 *destdata
++ = 0xFF ;
1173 m_memBuf
.UngetWriteBuf( size
) ;
1178 #if !wxMAC_USE_CORE_GRAPHICS
1179 WXHBITMAP
wxMask::GetHBITMAP() const
1181 return m_maskBitmap
;
1185 // ----------------------------------------------------------------------------
1187 // ----------------------------------------------------------------------------
1189 wxBitmapHandler::~wxBitmapHandler()
1193 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1198 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1199 int desiredWidth
, int desiredHeight
)
1204 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1209 // ----------------------------------------------------------------------------
1210 // Standard Handlers
1211 // ----------------------------------------------------------------------------
1213 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1215 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1217 inline wxPICTResourceHandler()
1219 SetName(wxT("Macintosh Pict resource"));
1220 SetExtension(wxEmptyString
);
1221 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1224 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1225 int desiredWidth
, int desiredHeight
);
1227 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1229 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1230 int desiredWidth
, int desiredHeight
)
1233 wxMacStringToPascal( name
, theName
) ;
1235 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1239 mf
.SetHMETAFILE((WXHMETAFILE
) thePict
) ;
1240 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1242 dc
.SelectObject( *bitmap
) ;
1244 dc
.SelectObject( wxNullBitmap
) ;
1250 void wxBitmap::InitStandardHandlers()
1252 AddHandler(new wxPICTResourceHandler
) ;
1253 AddHandler(new wxICONResourceHandler
) ;
1256 // ----------------------------------------------------------------------------
1257 // raw bitmap access support
1258 // ----------------------------------------------------------------------------
1260 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1264 // no bitmap, no data (raw or otherwise)
1268 data
.m_width
= GetWidth() ;
1269 data
.m_height
= GetHeight() ;
1270 data
.m_stride
= GetWidth() * 4 ;
1271 return GetRawAccess() ;
1274 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1279 // TODO : if we have some information about the API we should check
1280 // this code looks strange...
1282 if ( M_BITMAPDATA
->HasAlpha() )
1284 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1286 int w
= data
.GetWidth(),
1287 h
= data
.GetHeight();
1289 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1290 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1291 wxAlphaPixelData::Iterator
pMask(dataMask
),
1293 for ( int y
= 0; y
< h
; y
++ )
1295 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1298 for ( int x
= 0; x
< w
; x
++ )
1300 const wxAlphaPixelData::Iterator::ChannelType
1303 pMask
.Red() = alpha
;
1304 pMask
.Green() = alpha
;
1305 pMask
.Blue() = alpha
;
1314 pMask
= rowStartMask
;
1315 pMask
.OffsetY(dataMask
, 1);
1318 SetMask(new wxMask(bmpMask
));
1322 void wxBitmap::UseAlpha()
1324 // remember that we are using alpha channel, we'll need to create a proper
1325 // mask in UngetRawData()
1326 M_BITMAPDATA
->UseAlpha( true );