1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/bitmap.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
18 #include "wx/dcmemory.h"
23 #include "wx/metafile.h"
24 #include "wx/xpmdecod.h"
26 #include "wx/rawbmp.h"
28 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
29 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
31 #include <ApplicationServices/ApplicationServices.h>
32 #include <QuickTime/QuickTime.h>
34 #include "wx/mac/uma.h"
36 // Implementation Notes
37 // --------------------
39 // we are always working with a 32 bit deep pixel buffer
40 // under QuickDraw its alpha parts are going to be ignored in the GWorld,
41 // therefore we have a separate GWorld there for blitting the mask in
43 // under Quartz then content is transformed into a CGImageRef representing the same data
44 // which can be transferred to the GPU by the OS for fast rendering
46 #if wxMAC_USE_CORE_GRAPHICS
47 #define wxMAC_USE_PREMULTIPLIED_ALPHA 1
48 static const int kBestByteAlignement
= 16;
49 static const int kMaskBytesPerPixel
= 1;
51 #define wxMAC_USE_PREMULTIPLIED_ALPHA 0
52 static const int kBestByteAlignement
= 4;
53 static const int kMaskBytesPerPixel
= 4;
56 static int GetBestBytesPerRow( int rawBytes
)
58 return (((rawBytes
)+kBestByteAlignement
-1) & ~(kBestByteAlignement
-1) );
63 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
65 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
68 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
74 // NOTE : For testing Panther behaviour under higher
75 // Systems make this always be false
76 if ( UMAGetSystemVersion() >= 0x1040 )
78 // as soon as it is supported, it's a better default
79 forceType
= kControlContentCGImageRef
;
81 else if ( bmap
->HasNativeSize() )
83 forceType
= kControlContentIconRef
;
87 if ( forceType
== kControlContentIconRef
)
90 wxBitmapRefData
* bmp
= bmap
;
92 if ( !bmap
->HasNativeSize() )
94 // as PICT conversion will only result in a 16x16 icon, let's attempt
95 // a few scales for better results
97 int w
= bitmap
.GetWidth() ;
98 int h
= bitmap
.GetHeight() ;
99 int sz
= wxMax( w
, h
) ;
100 if ( sz
== 24 || sz
== 64 )
102 scaleBmp
= wxBitmap( bitmap
.ConvertToImage().Scale( w
* 2 , h
* 2 ) ) ;
103 bmp
= scaleBmp
.GetBitmapData() ;
107 info
->contentType
= kControlContentIconRef
;
108 info
->u
.iconRef
= bmp
->GetIconRef() ;
109 AcquireIconRef( info
->u
.iconRef
) ;
111 else if ( forceType
== kControlContentCGImageRef
)
113 info
->contentType
= kControlContentCGImageRef
;
114 info
->u
.imageRef
= (CGImageRef
) bmap
->CGImageCreate() ;
119 info
->contentType
= kControlContentPictHandle
;
120 info
->u
.picture
= bmap
->GetPictHandle() ;
126 CGImageRef
wxMacCreateCGImageFromBitmap( const wxBitmap
& bitmap
)
128 wxBitmapRefData
* bmap
= bitmap
.GetBitmapData() ;
131 return (CGImageRef
) bmap
->CGImageCreate();
134 void wxMacReleaseBitmapButton( ControlButtonContentInfo
*info
)
136 if ( info
->contentType
== kControlContentIconRef
)
138 ReleaseIconRef( info
->u
.iconRef
) ;
140 else if ( info
->contentType
== kControlNoContent
)
142 // there's no bitmap at all, fall through silently
144 else if ( info
->contentType
== kControlContentPictHandle
)
146 // owned by the bitmap, no release here
148 else if ( info
->contentType
== kControlContentCGImageRef
)
150 CGImageRelease( info
->u
.imageRef
) ;
154 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
158 #endif //wxUSE_BMPBUTTON
160 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
162 void wxBitmapRefData::Init()
169 m_bitmapMask
= NULL
;
170 m_cgImageRef
= NULL
;
173 m_pictHandle
= NULL
;
175 #if! wxMAC_USE_CORE_GRAPHICS
176 m_hMaskBitmap
= NULL
;
177 m_maskBytesPerRow
= 0 ;
180 m_rawAccessCount
= 0 ;
184 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
&tocopy
)
187 Create(tocopy
.m_width
, tocopy
.m_height
, tocopy
.m_depth
);
189 if (tocopy
.m_bitmapMask
)
190 m_bitmapMask
= new wxMask(*tocopy
.m_bitmapMask
);
191 else if (tocopy
.m_hasAlpha
)
194 unsigned char* dest
= (unsigned char*)GetRawAccess();
195 unsigned char* source
= (unsigned char*)tocopy
.GetRawAccess();
196 size_t numbytes
= m_bytesPerRow
* m_height
;
197 memcpy( dest
, source
, numbytes
);
200 wxBitmapRefData::wxBitmapRefData()
205 wxBitmapRefData::wxBitmapRefData( int w
, int h
, int d
)
208 Create( w
, h
, d
) ;
211 bool wxBitmapRefData::Create( int w
, int h
, int d
)
213 m_width
= wxMax(1, w
);
214 m_height
= wxMax(1, h
);
217 m_bytesPerRow
= GetBestBytesPerRow( w
* 4 ) ;
218 size_t size
= m_bytesPerRow
* h
;
219 void* data
= m_memBuf
.GetWriteBuf( size
) ;
220 memset( data
, 0 , size
) ;
221 m_memBuf
.UngetWriteBuf( size
) ;
224 #if !wxMAC_USE_CORE_GRAPHICS
225 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
226 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
227 (char*) data
, m_bytesPerRow
) ) ;
228 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create GWorld context") ) ;
230 m_hBitmap
= CGBitmapContextCreate((char*) data
, m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst
);
231 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
232 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
233 CGContextScaleCTM( m_hBitmap
, 1, -1 );
235 m_ok
= ( m_hBitmap
!= NULL
) ;
240 void wxBitmapRefData::UseAlpha( bool use
)
242 if ( m_hasAlpha
== use
)
246 #if wxMAC_USE_CORE_GRAPHICS
247 CGContextRelease( m_hBitmap
);
248 m_hBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, wxMacGetGenericRGBColorSpace(), m_hasAlpha
? kCGImageAlphaPremultipliedFirst
: kCGImageAlphaNoneSkipFirst
);
249 wxASSERT_MSG( m_hBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
250 CGContextTranslateCTM( m_hBitmap
, 0, m_height
);
251 CGContextScaleCTM( m_hBitmap
, 1, -1 );
255 wxASSERT( m_hMaskBitmap
== NULL
) ;
257 int width
= GetWidth() ;
258 int height
= GetHeight() ;
259 m_maskBytesPerRow
= GetBestBytesPerRow( width
* kMaskBytesPerPixel
);
260 size_t size
= height
* m_maskBytesPerRow
;
261 unsigned char * data
= (unsigned char * ) m_maskMemBuf
.GetWriteBuf( size
) ;
262 wxASSERT( data
!= NULL
) ;
264 memset( data
, 0 , size
) ;
265 Rect rect
= { 0 , 0 , height
, width
} ;
267 verify_noerr( NewGWorldFromPtr( (GWorldPtr
*) &m_hMaskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
268 (char*) data
, m_maskBytesPerRow
) ) ;
269 wxASSERT_MSG( m_hMaskBitmap
, wxT("Unable to create GWorld context for alpha mask") ) ;
271 m_maskMemBuf
.UngetWriteBuf(size
) ;
278 DisposeGWorld( m_hMaskBitmap
) ;
279 m_hMaskBitmap
= NULL
;
280 m_maskBytesPerRow
= 0 ;
285 void *wxBitmapRefData::GetRawAccess() const
287 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
288 return m_memBuf
.GetData() ;
291 void *wxBitmapRefData::BeginRawAccess()
293 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
294 wxASSERT( m_rawAccessCount
== 0 ) ;
295 wxASSERT_MSG( m_pictHandle
== NULL
&& m_iconRef
== NULL
,
296 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
300 // we must destroy an existing cached image, as
301 // the bitmap data may change now
304 CGImageRelease( m_cgImageRef
) ;
305 m_cgImageRef
= NULL
;
308 return m_memBuf
.GetData() ;
311 void wxBitmapRefData::EndRawAccess()
313 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
314 wxASSERT( m_rawAccessCount
== 1 ) ;
318 #if !wxMAC_USE_CORE_GRAPHICS
323 bool wxBitmapRefData::HasNativeSize()
326 int h
= GetHeight() ;
327 int sz
= wxMax( w
, h
) ;
329 return ( sz
== 128 || sz
== 48 || sz
== 32 || sz
== 16 );
332 IconRef
wxBitmapRefData::GetIconRef()
334 if ( m_iconRef
== NULL
)
336 // Create Icon Family Handle
338 IconFamilyHandle iconFamily
= NULL
;
340 if ( UMAGetSystemVersion() < 0x1040 )
342 iconFamily
= (IconFamilyHandle
) NewHandle( 8 ) ;
343 (**iconFamily
).resourceType
= kIconFamilyType
;
344 (**iconFamily
).resourceSize
= sizeof(OSType
) + sizeof(Size
);
348 iconFamily
= (IconFamilyHandle
) NewHandle( 0 ) ;
352 int h
= GetHeight() ;
353 int sz
= wxMax( w
, h
) ;
355 OSType dataType
= 0 ;
356 OSType maskType
= 0 ;
361 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
362 if ( UMAGetSystemVersion() >= 0x1050 )
364 dataType
= kIconServices128PixelDataARGB
;
369 dataType
= kThumbnail32BitData
;
370 maskType
= kThumbnail8BitMask
;
375 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
376 if ( UMAGetSystemVersion() >= 0x1050 )
378 dataType
= kIconServices48PixelDataARGB
;
383 dataType
= kHuge32BitData
;
384 maskType
= kHuge8BitMask
;
389 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
390 if ( UMAGetSystemVersion() >= 0x1050 )
392 dataType
= kIconServices32PixelDataARGB
;
397 dataType
= kLarge32BitData
;
398 maskType
= kLarge8BitMask
;
403 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
404 if ( UMAGetSystemVersion() >= 0x1050 )
406 dataType
= kIconServices16PixelDataARGB
;
411 dataType
= kSmall32BitData
;
412 maskType
= kSmall8BitMask
;
422 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
423 if ( maskType
== 0 && UMAGetSystemVersion() >= 0x1050 )
425 size_t datasize
= sz
* sz
* 4 ;
426 Handle data
= NewHandle( datasize
) ;
428 unsigned char* ptr
= (unsigned char*) *data
;
429 memset( ptr
, 0, datasize
);
430 bool hasAlpha
= HasAlpha() ;
431 wxMask
*mask
= m_bitmapMask
;
432 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
433 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
435 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
437 unsigned char * source
= sourcePtr
;
438 unsigned char * masksource
= masksourcePtr
;
439 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
440 unsigned char a
, r
, g
, b
;
442 for ( int x
= 0 ; x
< w
; ++x
)
451 a
= 0xFF - *masksource
++ ;
453 else if ( !hasAlpha
)
457 #if wxMAC_USE_PREMULTIPLIED_ALPHA
458 // this must be non-premultiplied data
459 if ( a
!= 0xFF && a
!= 0 )
475 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
);
476 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") );
477 DisposeHandle( data
);
482 // setup the header properly
485 Handle maskdata
= NULL
;
486 unsigned char * maskptr
= NULL
;
487 unsigned char * ptr
= NULL
;
488 size_t datasize
, masksize
;
490 datasize
= sz
* sz
* 4 ;
491 data
= NewHandle( datasize
) ;
493 ptr
= (unsigned char*) *data
;
494 memset( ptr
, 0, datasize
) ;
497 maskdata
= NewHandle( masksize
) ;
499 maskptr
= (unsigned char*) *maskdata
;
500 memset( maskptr
, 0 , masksize
) ;
502 bool hasAlpha
= HasAlpha() ;
503 wxMask
*mask
= m_bitmapMask
;
504 unsigned char * sourcePtr
= (unsigned char*) GetRawAccess() ;
505 unsigned char * masksourcePtr
= mask
? (unsigned char*) mask
->GetRawAccess() : NULL
;
507 for ( int y
= 0 ; y
< h
; ++y
, sourcePtr
+= m_bytesPerRow
, masksourcePtr
+= mask
? mask
->GetBytesPerRow() : 0 )
509 unsigned char * source
= sourcePtr
;
510 unsigned char * masksource
= masksourcePtr
;
511 unsigned char * dest
= ptr
+ y
* sz
* 4 ;
512 unsigned char * maskdest
= maskptr
+ y
* sz
;
513 unsigned char a
, r
, g
, b
;
515 for ( int x
= 0 ; x
< w
; ++x
)
529 *maskdest
++ = 0xFF - *masksource
++ ;
530 #if !wxMAC_USE_CORE_GRAPHICS
543 OSStatus err
= SetIconFamilyData( iconFamily
, dataType
, data
) ;
544 wxASSERT_MSG( err
== noErr
, wxT("Error when adding bitmap") ) ;
546 err
= SetIconFamilyData( iconFamily
, maskType
, maskdata
) ;
547 wxASSERT_MSG( err
== noErr
, wxT("Error when adding mask") ) ;
550 HUnlock( maskdata
) ;
551 DisposeHandle( data
) ;
552 DisposeHandle( maskdata
) ;
557 PicHandle pic
= GetPictHandle() ;
558 SetIconFamilyData( iconFamily
, 'PICT' , (Handle
) pic
) ;
560 // transform into IconRef
562 // cleaner version existing from 10.3 upwards
563 HLock((Handle
) iconFamily
);
564 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &m_iconRef
);
565 HUnlock((Handle
) iconFamily
);
566 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
567 DisposeHandle( (Handle
) iconFamily
) ;
573 PicHandle
wxBitmapRefData::GetPictHandle()
575 if ( m_pictHandle
== NULL
)
577 #if !wxMAC_USE_CORE_GRAPHICS
578 CGrafPtr origPort
= NULL
;
579 GDHandle origDev
= NULL
;
580 GWorldPtr wp
= NULL
;
581 GWorldPtr mask
= NULL
;
582 int height
= GetHeight() ;
583 int width
= GetWidth() ;
585 Rect rect
= { 0 , 0 , height
, width
} ;
586 RgnHandle clipRgn
= NULL
;
588 GetGWorld( &origPort
, &origDev
) ;
589 wp
= GetHBITMAP( &mask
) ;
593 GWorldPtr monoworld
;
595 OSStatus err
= NewGWorld( &monoworld
, 1 , &rect
, NULL
, NULL
, 0 ) ;
597 LockPixels( GetGWorldPixMap( monoworld
) ) ;
598 LockPixels( GetGWorldPixMap( mask
) ) ;
599 SetGWorld( monoworld
, NULL
) ;
601 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
602 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
603 RGBForeColor( &black
) ;
604 RGBBackColor( &white
) ;
606 CopyBits(GetPortBitMapForCopyBits(mask
),
607 GetPortBitMapForCopyBits(monoworld
),
611 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( monoworld
) ) ;
613 UnlockPixels( GetGWorldPixMap( monoworld
) ) ;
614 UnlockPixels( GetGWorldPixMap( mask
) ) ;
615 DisposeGWorld( monoworld
) ;
618 SetGWorld( wp
, NULL
) ;
620 GetPortBounds( wp
, &portRect
) ;
621 m_pictHandle
= OpenPicture(&portRect
);
625 RGBColor white
= { 0xffff , 0xffff , 0xffff } ;
626 RGBColor black
= { 0x0000 , 0x0000 , 0x0000 } ;
628 RGBForeColor( &black
) ;
629 RGBBackColor( &white
) ;
634 LockPixels( GetGWorldPixMap( wp
) ) ;
635 CopyBits(GetPortBitMapForCopyBits(wp
),
636 GetPortBitMapForCopyBits(wp
),
640 UnlockPixels( GetGWorldPixMap( wp
) ) ;
644 SetGWorld( origPort
, origDev
) ;
646 DisposeRgn( clipRgn
) ;
649 GraphicsExportComponent exporter
= 0;
650 OSStatus err
= OpenADefaultComponent(GraphicsExporterComponentType
, kQTFileTypePicture
, &exporter
);
653 m_pictHandle
= (PicHandle
) NewHandle(0);
656 // QT does not correctly export the mask
657 // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
658 CGImageRef imageRef
= CGImageCreate();
659 err
= GraphicsExportSetInputCGImage( exporter
, imageRef
);
660 err
= GraphicsExportSetOutputHandle(exporter
, (Handle
)m_pictHandle
);
661 err
= GraphicsExportDoExport(exporter
, NULL
);
662 CGImageRelease( imageRef
);
664 size_t handleSize
= GetHandleSize( (Handle
) m_pictHandle
);
665 // the 512 bytes header is only needed for pict files, but not in memory
666 if ( handleSize
>= 512 )
668 memmove( *m_pictHandle
, (char*)(*m_pictHandle
)+512, handleSize
- 512 );
669 SetHandleSize( (Handle
) m_pictHandle
, handleSize
- 512 );
672 CloseComponent( exporter
);
678 return m_pictHandle
;
681 void wxMacMemoryBufferReleaseProc(void *info
, const void *data
, size_t WXUNUSED(size
))
683 wxMemoryBuffer
* membuf
= (wxMemoryBuffer
*) info
;
685 wxASSERT( data
== membuf
->GetData() ) ;
690 CGImageRef
wxBitmapRefData::CGImageCreate() const
693 wxASSERT( m_rawAccessCount
>= 0 ) ;
695 if ( m_rawAccessCount
> 0 || m_cgImageRef
== NULL
)
697 #if wxMAC_USE_CORE_GRAPHICS
698 if ( UMAGetSystemVersion() >= 0x1040 && m_depth
!= 1 && m_bitmapMask
== NULL
)
702 CGImageRef tempImage
= CGBitmapContextCreateImage( m_hBitmap
);
703 CGImageRef tempMask
= CGBitmapContextCreateImage((CGContextRef
) m_bitmapMask
->GetHBITMAP() );
704 image
= CGImageCreateWithMask( tempImage
, tempMask
);
705 CGImageRelease(tempMask
);
706 CGImageRelease(tempImage
);
709 image
= CGBitmapContextCreateImage( m_hBitmap
);
714 size_t imageSize
= m_height
* m_bytesPerRow
;
715 void * dataBuffer
= m_memBuf
.GetData() ;
718 CGImageAlphaInfo alphaInfo
= kCGImageAlphaNoneSkipFirst
;
719 wxMemoryBuffer
* membuf
= NULL
;
723 alphaInfo
= kCGImageAlphaFirst
;
724 membuf
= new wxMemoryBuffer( imageSize
) ;
725 memcpy( membuf
->GetData() , dataBuffer
, imageSize
) ;
726 unsigned char *sourcemaskstart
= (unsigned char *) m_bitmapMask
->GetRawAccess() ;
727 int maskrowbytes
= m_bitmapMask
->GetBytesPerRow() ;
728 unsigned char *destalphastart
= (unsigned char *) membuf
->GetData() ;
729 for ( int y
= 0 ; y
< h
; ++y
, destalphastart
+= m_bytesPerRow
, sourcemaskstart
+= maskrowbytes
)
731 unsigned char *sourcemask
= sourcemaskstart
;
732 unsigned char *destalpha
= destalphastart
;
733 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= kMaskBytesPerPixel
, destalpha
+= 4 )
735 *destalpha
= 0xFF - *sourcemask
;
743 #if wxMAC_USE_PREMULTIPLIED_ALPHA
744 alphaInfo
= kCGImageAlphaPremultipliedFirst
;
746 alphaInfo
= kCGImageAlphaFirst
;
750 membuf
= new wxMemoryBuffer( m_memBuf
) ;
753 CGDataProviderRef dataProvider
= NULL
;
756 wxMemoryBuffer
* maskBuf
= new wxMemoryBuffer( m_width
* m_height
);
757 unsigned char * maskBufData
= (unsigned char *) maskBuf
->GetData();
758 unsigned char * bufData
= (unsigned char *) membuf
->GetData() ;
759 // copy one color component
760 for( int i
= 0 ; i
< m_width
* m_height
; ++i
)
761 maskBufData
[i
] = bufData
[i
*4+3];
763 CGDataProviderCreateWithData(
764 maskBuf
, (const void *) maskBufData
, m_width
* m_height
,
765 wxMacMemoryBufferReleaseProc
);
766 // as we are now passing the mask buffer to the data provider, we have
767 // to release the membuf ourselves
770 image
= ::CGImageMaskCreate( w
, h
, 8, 8, m_width
, dataProvider
, NULL
, false );
774 CGColorSpaceRef colorSpace
= wxMacGetGenericRGBColorSpace();
776 CGDataProviderCreateWithData(
777 membuf
, (const void *)membuf
->GetData() , imageSize
,
778 wxMacMemoryBufferReleaseProc
);
781 w
, h
, 8 , 32 , m_bytesPerRow
, colorSpace
, alphaInfo
,
782 dataProvider
, NULL
, false , kCGRenderingIntentDefault
);
784 CGDataProviderRelease( dataProvider
);
789 image
= m_cgImageRef
;
790 CGImageRetain( image
) ;
793 if ( m_rawAccessCount
== 0 && m_cgImageRef
== NULL
)
795 // we keep it for later use
796 m_cgImageRef
= image
;
797 CGImageRetain( image
) ;
803 #if wxMAC_USE_CORE_GRAPHICS
804 CGContextRef
wxBitmapRefData::GetBitmapContext() const
809 GWorldPtr
wxBitmapRefData::GetHBITMAP(GWorldPtr
* mask
) const
811 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
817 *mask
= (GWorldPtr
) m_bitmapMask
->GetHBITMAP() ;
819 else if ( m_hasAlpha
)
821 if ( m_rawAccessCount
> 0 )
823 *mask
= m_hMaskBitmap
;
831 #if !wxMAC_USE_CORE_GRAPHICS
832 void wxBitmapRefData::UpdateAlphaMask() const
836 unsigned char *sourcemask
= (unsigned char *) GetRawAccess() ;
837 unsigned char *destalphabase
= (unsigned char *) m_maskMemBuf
.GetData() ;
839 int h
= GetHeight() ;
842 for ( int y
= 0 ; y
< h
; ++y
, destalphabase
+= m_maskBytesPerRow
)
844 unsigned char* destalpha
= destalphabase
;
846 for ( int x
= 0 ; x
< w
; ++x
, sourcemask
+= 4 )
848 // we must have 24 bit depth for non quartz smooth alpha
850 *destalpha
++ = 255 - *sourcemask
;
851 *destalpha
++ = 255 - *sourcemask
;
852 *destalpha
++ = 255 - *sourcemask
;
859 void wxBitmapRefData::Free()
861 wxASSERT_MSG( m_rawAccessCount
== 0 , wxT("Bitmap still selected when destroyed") ) ;
865 CGImageRelease( m_cgImageRef
) ;
866 m_cgImageRef
= NULL
;
871 ReleaseIconRef( m_iconRef
) ;
878 KillPicture( m_pictHandle
) ;
879 m_pictHandle
= NULL
;
885 #if !wxMAC_USE_CORE_GRAPHICS
886 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap
) ) ;
888 CGContextRelease(m_hBitmap
);
893 #if !wxMAC_USE_CORE_GRAPHICS
896 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap
) ) ;
897 m_hMaskBitmap
= NULL
;
907 wxBitmapRefData::~wxBitmapRefData()
912 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
914 bool created
= false ;
915 int w
= icon
.GetWidth() ;
916 int h
= icon
.GetHeight() ;
918 Create( icon
.GetWidth() , icon
.GetHeight() ) ;
920 if ( w
== h
&& ( w
== 16 || w
== 32 || w
== 48 || w
== 128 ) )
922 IconFamilyHandle iconFamily
= NULL
;
923 Handle imagehandle
= NewHandle( 0 ) ;
924 Handle maskhandle
= NewHandle( 0 ) ;
928 IconSelectorValue selector
= 0 ;
933 dataType
= kThumbnail32BitData
;
934 maskType
= kThumbnail8BitMask
;
935 selector
= kSelectorAllAvailableData
;
939 dataType
= kHuge32BitData
;
940 maskType
= kHuge8BitMask
;
941 selector
= kSelectorHuge32Bit
| kSelectorHuge8BitMask
;
945 dataType
= kLarge32BitData
;
946 maskType
= kLarge8BitMask
;
947 selector
= kSelectorLarge32Bit
| kSelectorLarge8BitMask
;
951 dataType
= kSmall32BitData
;
952 maskType
= kSmall8BitMask
;
953 selector
= kSelectorSmall32Bit
| kSelectorSmall8BitMask
;
960 OSStatus err
= IconRefToIconFamily( MAC_WXHICON(icon
.GetHICON()) , selector
, &iconFamily
) ;
962 err
= GetIconFamilyData( iconFamily
, dataType
, imagehandle
) ;
963 err
= GetIconFamilyData( iconFamily
, maskType
, maskhandle
) ;
964 size_t imagehandlesize
= GetHandleSize( imagehandle
) ;
965 size_t maskhandlesize
= GetHandleSize( maskhandle
) ;
967 if ( imagehandlesize
!= 0 && maskhandlesize
!= 0 )
969 wxASSERT( GetHandleSize( imagehandle
) == w
* 4 * h
) ;
970 wxASSERT( GetHandleSize( maskhandle
) == w
* h
) ;
974 unsigned char *source
= (unsigned char *) *imagehandle
;
975 unsigned char *sourcemask
= (unsigned char *) *maskhandle
;
976 unsigned char* destination
= (unsigned char*) BeginRawAccess() ;
978 for ( int y
= 0 ; y
< h
; ++y
)
980 for ( int x
= 0 ; x
< w
; ++x
)
982 unsigned char a
= *sourcemask
++;
985 #if wxMAC_USE_PREMULTIPLIED_ALPHA
986 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
987 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
988 *destination
++ = ( (*source
++) * a
+ 127 ) / 255;
990 *destination
++ = *source
++ ;
991 *destination
++ = *source
++ ;
992 *destination
++ = *source
++ ;
998 DisposeHandle( imagehandle
) ;
999 DisposeHandle( maskhandle
) ;
1003 DisposeHandle( (Handle
) iconFamily
) ;
1009 dc
.SelectObject( *this ) ;
1010 dc
.DrawIcon( icon
, 0 , 0 ) ;
1011 dc
.SelectObject( wxNullBitmap
) ;
1017 wxBitmap::wxBitmap()
1021 wxBitmap::~wxBitmap()
1025 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
1027 m_refData
= new wxBitmapRefData( the_width
, the_height
, no_bits
) ;
1031 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
1032 if ( the_width
% (sizeof(unsigned char) * 8) )
1033 linesize
+= sizeof(unsigned char);
1035 unsigned char* linestart
= (unsigned char*) bits
;
1036 unsigned char* destptr
= (unsigned char*) BeginRawAccess() ;
1038 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
, destptr
+= M_BITMAPDATA
->GetBytesPerRow() )
1040 unsigned char* destination
= destptr
;
1041 int index
, bit
, mask
;
1043 for ( int x
= 0 ; x
< the_width
; ++x
)
1049 if ( linestart
[index
] & mask
)
1051 *destination
++ = 0xFF ;
1052 *destination
++ = 0 ;
1053 *destination
++ = 0 ;
1054 *destination
++ = 0 ;
1058 *destination
++ = 0xFF ;
1059 *destination
++ = 0xFF ;
1060 *destination
++ = 0xFF ;
1061 *destination
++ = 0xFF ;
1070 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
1074 wxBitmap::wxBitmap(int w
, int h
, int d
)
1076 (void)Create(w
, h
, d
);
1079 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1081 (void) Create(data
, type
, width
, height
, depth
);
1084 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
1086 LoadFile(filename
, type
);
1089 wxObjectRefData
* wxBitmap::CreateRefData() const
1091 return new wxBitmapRefData
;
1094 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
1096 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
1099 void * wxBitmap::GetRawAccess() const
1101 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1103 return M_BITMAPDATA
->GetRawAccess() ;
1106 void * wxBitmap::BeginRawAccess()
1108 wxCHECK_MSG( Ok() , NULL
, wxT("invalid bitmap") ) ;
1110 return M_BITMAPDATA
->BeginRawAccess() ;
1113 void wxBitmap::EndRawAccess()
1115 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
1117 M_BITMAPDATA
->EndRawAccess() ;
1120 WXCGIMAGEREF
wxBitmap::CGImageCreate() const
1122 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ) ;
1124 return M_BITMAPDATA
->CGImageCreate() ;
1127 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
1129 wxCHECK_MSG( Ok() &&
1130 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1131 (rect
.x
+rect
.width
<= GetWidth()) &&
1132 (rect
.y
+rect
.height
<= GetHeight()),
1133 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1135 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1136 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1138 int destwidth
= rect
.width
;
1139 int destheight
= rect
.height
;
1142 unsigned char *sourcedata
= (unsigned char*) GetRawAccess() ;
1143 unsigned char *destdata
= (unsigned char*) ret
.BeginRawAccess() ;
1144 wxASSERT( (sourcedata
!= NULL
) && (destdata
!= NULL
) ) ;
1146 int sourcelinesize
= GetBitmapData()->GetBytesPerRow() ;
1147 int destlinesize
= ret
.GetBitmapData()->GetBytesPerRow() ;
1148 unsigned char *source
= sourcedata
+ rect
.x
* 4 + rect
.y
* sourcelinesize
;
1149 unsigned char *dest
= destdata
;
1151 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1153 memcpy( dest
, source
, destlinesize
) ;
1157 ret
.EndRawAccess() ;
1159 if ( M_BITMAPDATA
->m_bitmapMask
)
1161 wxMemoryBuffer maskbuf
;
1162 int rowBytes
= GetBestBytesPerRow( destwidth
* kMaskBytesPerPixel
);
1163 size_t maskbufsize
= rowBytes
* destheight
;
1165 int sourcelinesize
= M_BITMAPDATA
->m_bitmapMask
->GetBytesPerRow() ;
1166 int destlinesize
= rowBytes
;
1168 unsigned char *source
= (unsigned char *) M_BITMAPDATA
->m_bitmapMask
->GetRawAccess() ;
1169 unsigned char *destdata
= (unsigned char * ) maskbuf
.GetWriteBuf( maskbufsize
) ;
1170 wxASSERT( (source
!= NULL
) && (destdata
!= NULL
) ) ;
1172 source
+= rect
.x
* kMaskBytesPerPixel
+ rect
.y
* sourcelinesize
;
1173 unsigned char *dest
= destdata
;
1175 for (int yy
= 0; yy
< destheight
; ++yy
, source
+= sourcelinesize
, dest
+= destlinesize
)
1177 memcpy( dest
, source
, destlinesize
) ;
1180 maskbuf
.UngetWriteBuf( maskbufsize
) ;
1181 ret
.SetMask( new wxMask( maskbuf
, destwidth
, destheight
, rowBytes
) ) ;
1183 else if ( HasAlpha() )
1189 bool wxBitmap::Create(int w
, int h
, int d
)
1194 d
= wxDisplayDepth() ;
1196 m_refData
= new wxBitmapRefData( w
, h
, d
);
1198 return M_BITMAPDATA
->Ok() ;
1201 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1205 wxBitmapHandler
*handler
= FindHandler(type
);
1209 m_refData
= new wxBitmapRefData
;
1211 return handler
->LoadFile(this, filename
, type
, -1, -1);
1216 wxImage
loadimage(filename
, type
);
1226 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1231 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1235 m_refData
= new wxBitmapRefData
;
1237 wxBitmapHandler
*handler
= FindHandler(type
);
1239 if ( handler
== NULL
)
1241 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1246 return handler
->Create(this, data
, type
, width
, height
, depth
);
1251 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
1253 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
1255 // width and height of the device-dependent bitmap
1256 int width
= image
.GetWidth();
1257 int height
= image
.GetHeight();
1259 m_refData
= new wxBitmapRefData( width
, height
, depth
) ;
1263 bool hasAlpha
= false ;
1265 if ( image
.HasMask() )
1267 // takes precedence, don't mix with alpha info
1271 hasAlpha
= image
.HasAlpha() ;
1277 unsigned char* destinationstart
= (unsigned char*) BeginRawAccess() ;
1278 register unsigned char* data
= image
.GetData();
1279 if ( destinationstart
!= NULL
&& data
!= NULL
)
1281 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() : NULL
;
1282 for (int y
= 0; y
< height
; destinationstart
+= M_BITMAPDATA
->GetBytesPerRow(), y
++)
1284 unsigned char * destination
= destinationstart
;
1285 for (int x
= 0; x
< width
; x
++)
1289 const unsigned char a
= *alpha
++;
1290 *destination
++ = a
;
1292 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1293 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1294 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1295 *destination
++ = ((*data
++) * a
+ 127) / 255 ;
1297 *destination
++ = *data
++ ;
1298 *destination
++ = *data
++ ;
1299 *destination
++ = *data
++ ;
1304 *destination
++ = 0xFF ;
1305 *destination
++ = *data
++ ;
1306 *destination
++ = *data
++ ;
1307 *destination
++ = *data
++ ;
1314 if ( image
.HasMask() )
1315 SetMask( new wxMask( *this , wxColour( image
.GetMaskRed() , image
.GetMaskGreen() , image
.GetMaskBlue() ) ) ) ;
1318 wxImage
wxBitmap::ConvertToImage() const
1322 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1324 // create an wxImage object
1325 int width
= GetWidth();
1326 int height
= GetHeight();
1327 image
.Create( width
, height
);
1329 unsigned char *data
= image
.GetData();
1330 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
1332 unsigned char* sourcestart
= (unsigned char*) GetRawAccess() ;
1334 bool hasAlpha
= false ;
1335 bool hasMask
= false ;
1336 int maskBytesPerRow
= 0 ;
1337 unsigned char *alpha
= NULL
;
1338 unsigned char *mask
= NULL
;
1346 mask
= (unsigned char*) GetMask()->GetRawAccess() ;
1347 maskBytesPerRow
= GetMask()->GetBytesPerRow() ;
1353 alpha
= image
.GetAlpha() ;
1358 // The following masking algorithm is the same as well in msw/gtk:
1359 // the colour used as transparent one in wxImage and the one it is
1360 // replaced with when it actually occurs in the bitmap
1361 static const int MASK_RED
= 1;
1362 static const int MASK_GREEN
= 2;
1363 static const int MASK_BLUE
= 3;
1364 static const int MASK_BLUE_REPLACEMENT
= 2;
1366 for (int yy
= 0; yy
< height
; yy
++ , sourcestart
+= M_BITMAPDATA
->GetBytesPerRow() , mask
+= maskBytesPerRow
)
1368 unsigned char * maskp
= mask
;
1369 unsigned char * source
= sourcestart
;
1370 unsigned char a
, r
, g
, b
;
1373 for (int xx
= 0; xx
< width
; xx
++)
1375 color
= *((long*) source
) ;
1376 #ifdef WORDS_BIGENDIAN
1377 a
= ((color
&0xFF000000) >> 24) ;
1378 r
= ((color
&0x00FF0000) >> 16) ;
1379 g
= ((color
&0x0000FF00) >> 8) ;
1380 b
= (color
&0x000000FF);
1382 b
= ((color
&0xFF000000) >> 24) ;
1383 g
= ((color
&0x00FF0000) >> 16) ;
1384 r
= ((color
&0x0000FF00) >> 8) ;
1385 a
= (color
&0x000000FF);
1389 if ( *maskp
++ == 0xFF )
1395 else if ( r
== MASK_RED
&& g
== MASK_GREEN
&& b
== MASK_BLUE
)
1396 b
= MASK_BLUE_REPLACEMENT
;
1397 #if !wxMAC_USE_CORE_GRAPHICS
1403 else if ( hasAlpha
)
1406 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1407 // this must be non-premultiplied data
1408 if ( a
!= 0xFF && a
!= 0 )
1418 data
[index
+ 1] = g
;
1419 data
[index
+ 2] = b
;
1427 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1432 #endif //wxUSE_IMAGE
1434 bool wxBitmap::SaveFile( const wxString
& filename
,
1435 wxBitmapType type
, const wxPalette
*palette
) const
1437 bool success
= false;
1438 wxBitmapHandler
*handler
= FindHandler(type
);
1442 success
= handler
->SaveFile(this, filename
, type
, palette
);
1447 wxImage image
= ConvertToImage();
1448 success
= image
.SaveFile(filename
, type
);
1450 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
1457 bool wxBitmap::IsOk() const
1459 return (M_BITMAPDATA
&& M_BITMAPDATA
->Ok());
1462 int wxBitmap::GetHeight() const
1464 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1466 return M_BITMAPDATA
->GetHeight();
1469 int wxBitmap::GetWidth() const
1471 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1473 return M_BITMAPDATA
->GetWidth() ;
1476 int wxBitmap::GetDepth() const
1478 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1480 return M_BITMAPDATA
->GetDepth();
1483 wxMask
*wxBitmap::GetMask() const
1485 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1487 return M_BITMAPDATA
->m_bitmapMask
;
1490 bool wxBitmap::HasAlpha() const
1492 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1494 return M_BITMAPDATA
->HasAlpha() ;
1497 void wxBitmap::SetWidth(int w
)
1500 M_BITMAPDATA
->SetWidth(w
);
1503 void wxBitmap::SetHeight(int h
)
1506 M_BITMAPDATA
->SetHeight(h
);
1509 void wxBitmap::SetDepth(int d
)
1512 M_BITMAPDATA
->SetDepth(d
);
1515 void wxBitmap::SetOk(bool isOk
)
1518 M_BITMAPDATA
->SetOk(isOk
);
1522 wxPalette
*wxBitmap::GetPalette() const
1524 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1526 return &M_BITMAPDATA
->m_bitmapPalette
;
1529 void wxBitmap::SetPalette(const wxPalette
& palette
)
1532 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1534 #endif // wxUSE_PALETTE
1536 void wxBitmap::SetMask(wxMask
*mask
)
1539 // Remove existing mask if there is one.
1540 delete M_BITMAPDATA
->m_bitmapMask
;
1542 M_BITMAPDATA
->m_bitmapMask
= mask
;
1545 WXHBITMAP
wxBitmap::GetHBITMAP(WXHBITMAP
* mask
) const
1547 #if !wxMAC_USE_CORE_GRAPHICS
1548 return WXHBITMAP(M_BITMAPDATA
->GetHBITMAP((GWorldPtr
*)mask
));
1552 return WXHBITMAP(M_BITMAPDATA
->GetBitmapContext());
1556 // ----------------------------------------------------------------------------
1558 // ----------------------------------------------------------------------------
1565 wxMask::wxMask(const wxMask
&tocopy
)
1569 m_bytesPerRow
= tocopy
.m_bytesPerRow
;
1570 m_width
= tocopy
.m_width
;
1571 m_height
= tocopy
.m_height
;
1573 size_t size
= m_bytesPerRow
* m_height
;
1574 unsigned char* dest
= (unsigned char*)m_memBuf
.GetWriteBuf( size
);
1575 unsigned char* source
= (unsigned char*)tocopy
.m_memBuf
.GetData();
1576 memcpy( dest
, source
, size
);
1577 m_memBuf
.UngetWriteBuf( size
) ;
1581 // Construct a mask from a bitmap and a colour indicating
1582 // the transparent area
1583 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
1586 Create( bitmap
, colour
);
1589 // Construct a mask from a mono bitmap (copies the bitmap).
1590 wxMask::wxMask( const wxBitmap
& bitmap
)
1596 // Construct a mask from a mono bitmap (copies the bitmap).
1598 wxMask::wxMask( const wxMemoryBuffer
& data
, int width
, int height
, int bytesPerRow
)
1601 Create( data
, width
, height
, bytesPerRow
);
1608 #if wxMAC_USE_CORE_GRAPHICS
1609 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1611 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1613 m_maskBitmap
= NULL
;
1619 m_width
= m_height
= m_bytesPerRow
= 0 ;
1620 m_maskBitmap
= NULL
;
1623 void *wxMask::GetRawAccess() const
1625 return m_memBuf
.GetData() ;
1628 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1629 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1631 void wxMask::RealizeNative()
1635 #if wxMAC_USE_CORE_GRAPHICS
1636 CGContextRelease( (CGContextRef
) m_maskBitmap
);
1638 DisposeGWorld( (GWorldPtr
)m_maskBitmap
) ;
1640 m_maskBitmap
= NULL
;
1643 #if wxMAC_USE_CORE_GRAPHICS
1644 CGColorSpaceRef colorspace
= CGColorSpaceCreateDeviceGray();
1645 // from MouseTracking sample :
1646 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1647 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1649 m_maskBitmap
= CGBitmapContextCreate((char*) m_memBuf
.GetData(), m_width
, m_height
, 8, m_bytesPerRow
, colorspace
,
1650 kCGImageAlphaNone
);
1651 CGColorSpaceRelease( colorspace
);
1652 wxASSERT_MSG( m_maskBitmap
, wxT("Unable to create CGBitmapContext context") ) ;
1654 Rect rect
= { 0 , 0 , m_height
, m_width
} ;
1656 OSStatus err
= NewGWorldFromPtr(
1657 (GWorldPtr
*) &m_maskBitmap
, k32ARGBPixelFormat
, &rect
, NULL
, NULL
, 0 ,
1658 (char*) m_memBuf
.GetData() , m_bytesPerRow
) ;
1659 verify_noerr( err
) ;
1663 // Create a mask from a mono bitmap (copies the bitmap).
1665 bool wxMask::Create(const wxMemoryBuffer
& data
,int width
, int height
, int bytesPerRow
)
1670 m_bytesPerRow
= bytesPerRow
;
1672 wxASSERT( data
.GetDataLen() == (size_t)(height
* bytesPerRow
) ) ;
1679 // Create a mask from a mono bitmap (copies the bitmap).
1680 bool wxMask::Create(const wxBitmap
& bitmap
)
1682 m_width
= bitmap
.GetWidth() ;
1683 m_height
= bitmap
.GetHeight() ;
1684 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1686 size_t size
= m_bytesPerRow
* m_height
;
1687 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1688 wxASSERT( destdatabase
!= NULL
) ;
1690 memset( destdatabase
, 0 , size
) ;
1691 unsigned char * srcdata
= (unsigned char*) bitmap
.GetRawAccess() ;
1693 for ( int y
= 0 ; y
< m_height
; ++y
, destdatabase
+= m_bytesPerRow
)
1695 unsigned char *destdata
= destdatabase
;
1696 unsigned char r
, g
, b
;
1698 for ( int x
= 0 ; x
< m_width
; ++x
)
1705 if ( ( r
+ g
+ b
) > 0x10 )
1707 *destdata
++ = 0xFF ;
1708 #if !wxMAC_USE_CORE_GRAPHICS
1709 *destdata
++ = 0xFF ;
1710 *destdata
++ = 0xFF ;
1711 *destdata
++ = 0xFF ;
1716 *destdata
++ = 0x00 ;
1717 #if !wxMAC_USE_CORE_GRAPHICS
1718 *destdata
++ = 0x00 ;
1719 *destdata
++ = 0x00 ;
1720 *destdata
++ = 0x00 ;
1726 m_memBuf
.UngetWriteBuf( size
) ;
1732 // Create a mask from a bitmap and a colour indicating
1733 // the transparent area
1734 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1736 m_width
= bitmap
.GetWidth() ;
1737 m_height
= bitmap
.GetHeight() ;
1738 m_bytesPerRow
= GetBestBytesPerRow( m_width
* kMaskBytesPerPixel
) ;
1740 size_t size
= m_bytesPerRow
* m_height
;
1741 unsigned char * destdatabase
= (unsigned char*) m_memBuf
.GetWriteBuf( size
) ;
1742 wxASSERT( destdatabase
!= NULL
) ;
1744 memset( destdatabase
, 0 , size
) ;
1745 unsigned char * srcdatabase
= (unsigned char*) bitmap
.GetRawAccess() ;
1746 size_t sourceBytesRow
= bitmap
.GetBitmapData()->GetBytesPerRow();
1748 for ( int y
= 0 ; y
< m_height
; ++y
, srcdatabase
+= sourceBytesRow
, destdatabase
+= m_bytesPerRow
)
1750 unsigned char *srcdata
= srcdatabase
;
1751 unsigned char *destdata
= destdatabase
;
1752 unsigned char r
, g
, b
;
1754 for ( int x
= 0 ; x
< m_width
; ++x
)
1761 if ( colour
== wxColour( r
, g
, b
) )
1763 *destdata
++ = 0xFF ;
1764 #if !wxMAC_USE_CORE_GRAPHICS
1765 *destdata
++ = 0xFF ;
1766 *destdata
++ = 0xFF ;
1767 *destdata
++ = 0xFF ;
1772 *destdata
++ = 0x00 ;
1773 #if !wxMAC_USE_CORE_GRAPHICS
1774 *destdata
++ = 0x00 ;
1775 *destdata
++ = 0x00 ;
1776 *destdata
++ = 0x00 ;
1782 m_memBuf
.UngetWriteBuf( size
) ;
1788 WXHBITMAP
wxMask::GetHBITMAP() const
1790 return m_maskBitmap
;
1793 // ----------------------------------------------------------------------------
1795 // ----------------------------------------------------------------------------
1797 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1799 // ----------------------------------------------------------------------------
1800 // Standard Handlers
1801 // ----------------------------------------------------------------------------
1803 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1805 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1808 inline wxPICTResourceHandler()
1810 SetName(wxT("Macintosh Pict resource"));
1811 SetExtension(wxEmptyString
);
1812 SetType(wxBITMAP_TYPE_PICT_RESOURCE
);
1815 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1816 int desiredWidth
, int desiredHeight
);
1819 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1822 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
,
1823 const wxString
& name
,
1824 long WXUNUSED(flags
),
1825 int WXUNUSED(desiredWidth
),
1826 int WXUNUSED(desiredHeight
))
1830 wxMacStringToPascal( name
, theName
) ;
1832 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1837 mf
.SetPICT( thePict
) ;
1838 bitmap
->Create( mf
.GetWidth() , mf
.GetHeight() ) ;
1840 dc
.SelectObject( *bitmap
) ;
1842 dc
.SelectObject( wxNullBitmap
) ;
1851 void wxBitmap::InitStandardHandlers()
1853 AddHandler( new wxPICTResourceHandler
) ;
1854 AddHandler( new wxICONResourceHandler
) ;
1857 // ----------------------------------------------------------------------------
1858 // raw bitmap access support
1859 // ----------------------------------------------------------------------------
1861 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int WXUNUSED(bpp
))
1864 // no bitmap, no data (raw or otherwise)
1867 data
.m_width
= GetWidth() ;
1868 data
.m_height
= GetHeight() ;
1869 data
.m_stride
= GetBitmapData()->GetBytesPerRow() ;
1871 return BeginRawAccess() ;
1874 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(dataBase
))
1879 void wxBitmap::UseAlpha()
1881 // remember that we are using alpha channel:
1882 // we'll need to create a proper mask in UngetRawData()
1883 M_BITMAPDATA
->UseAlpha( true );