]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bitmap.cpp
alpha support for Scale added
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose: wxBitmap
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "bitmap.h"
14#endif
15
a8e9860d 16#include "wx/wxprec.h"
b698c8e9 17
e9576ca5
SC
18#include "wx/bitmap.h"
19#include "wx/icon.h"
20#include "wx/log.h"
fec19ea9 21#include "wx/image.h"
20b69855 22#include "wx/metafile.h"
973b0afb 23#include "wx/xpmdecod.h"
e9576ca5 24
55e18dbe
VZ
25#include "wx/rawbmp.h"
26
e9576ca5
SC
27IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
28IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
55e18dbe 29IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )
e9576ca5 30
f5c6eb5c 31#ifdef __DARWIN__
5fde6fcc 32 #include <ApplicationServices/ApplicationServices.h>
03e11df5
GD
33#else
34 #include <PictUtils.h>
35#endif
519cb848 36
31d30995
SC
37#include "wx/mac/uma.h"
38
05d8deda
RN
39#include "wx/dcmemory.h"
40
20b69855
SC
41// Implementation Notes
42// --------------------
43//
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
47
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
50
51// we don't dare premultiplied alpha yet
52#define wxMAC_USE_PREMULTIPLIED_ALPHA 0
53
71cc158e 54IconFamilyHandle wxMacCreateIconFamily(const wxBitmap& bmp)
20b69855
SC
55{
56 // setup the header properly
57
58 IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(8) ;
59 (**iconFamily).resourceType = kIconFamilyType ;
60 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
61
62 int w = bmp.GetWidth() ;
63 int h = bmp.GetHeight() ;
64 int sz = wxMax( w , h ) ;
65 if ( sz > 128 )
4e9ed364 66 {
20b69855
SC
67 wxFAIL_MSG( wxT("Currently only 128 pixels wide images are supported") ) ;
68 }
69
70 Handle data = NULL ;
71 Handle maskdata = NULL ;
72 unsigned char * maskptr = NULL ;
73 unsigned char * ptr = NULL ;
74 size_t size ;
75 size_t masksize ;
76 OSType dataType ;
77 OSType maskType ;
78
79 bool hasAlpha = bmp.HasAlpha() ;
80 wxMask *mask = bmp.GetMask() ;
81 // thumbnail is 128 x 128 with 32 bits per pixel
82
83 if ( sz > 48 )
84 {
85 sz = 128 ;
86 dataType = kThumbnail32BitData ;
87 maskType = kThumbnail8BitMask ;
88 }
89 else if ( sz > 32 )
90 {
91 sz = 48 ;
92 dataType = kHuge32BitData ;
93 maskType = kHuge8BitMask ;
94 }
95 else if ( sz > 16 )
96 {
97 sz = 32 ;
98 dataType = kLarge32BitData ;
99 maskType = kLarge8BitMask ;
100 }
101 else
102 {
103 sz = 16 ;
104 dataType = kSmall32BitData ;
105 maskType = kSmall8BitMask ;
4e9ed364 106 }
519cb848 107
20b69855
SC
108 size = sz * sz * 4 ;
109 data = NewHandle( size) ;
110 HLock( data ) ;
111 ptr = (unsigned char*) *data ;
112 memset( ptr , 0, size ) ;
113
114 masksize = sz * sz ;
115 maskdata = NewHandle( masksize ) ;
116 HLock( maskdata ) ;
117 maskptr = (unsigned char*) *maskdata ;
118 memset( maskptr , 0 , masksize ) ;
119
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 )
123 {
124 unsigned char * dest = ptr + y * sz * 4 ;
125 unsigned char * maskdest = maskptr + y * sz ;
126 for ( int x = 0 ; x < w ; ++x )
127 {
128 unsigned char a = *source ++ ;
129 unsigned char r = *source ++ ;
130 unsigned char g = *source ++ ;
131 unsigned char b = *source ++ ;
132
133 *dest++ = 0 ;
134 *dest++ = r ;
135 *dest++ = g ;
136 *dest++ = b ;
137
138 if ( mask )
139 *maskdest++ = *masksource++ ;
140 else if ( hasAlpha )
141 *maskdest++ = a ;
142 else
143 *maskdest++ = 0xFF ;
144 }
145 }
146
147 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
148 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
149
150 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
151 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
152 HUnlock( data ) ;
153 HUnlock( maskdata ) ;
154 DisposeHandle( data ) ;
155 DisposeHandle( maskdata ) ;
71cc158e
SC
156 return iconFamily ;
157}
20b69855 158
71cc158e
SC
159IconRef wxMacCreateIconRef(const wxBitmap& bmp)
160{
161 IconFamilyHandle iconFamily = wxMacCreateIconFamily( bmp ) ;
20b69855
SC
162 IconRef iconRef ;
163 static int iconCounter = 2 ;
164
71cc158e 165 OSStatus err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &iconRef ) ;
20b69855
SC
166 UInt16 owners ;
167 err = GetIconRefOwners(iconRef , &owners ) ;
168
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 ) ;
174 ++iconCounter ;
175
176 return iconRef ;
519cb848
SC
177}
178
71cc158e
SC
179PicHandle wxMacCreatePicHandle( const wxBitmap &bmp )
180{
181 CGrafPtr origPort = NULL ;
182 GDHandle origDev = NULL ;
183 PicHandle pict = NULL ;
184 GWorldPtr wp = NULL ;
185 GWorldPtr mask = NULL ;
186
187 GetGWorld( &origPort , &origDev ) ;
188
189 wp = (GWorldPtr) bmp.GetHBITMAP( (WXHBITMAP*) &mask ) ;
190
191 SetGWorld( wp , NULL ) ;
192 Rect portRect ;
193 GetPortBounds( wp , &portRect ) ;
194 pict = OpenPicture(&portRect);
195
196 if(pict)
197 {
198 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
199 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
200 RGBForeColor( &black ) ;
201 RGBBackColor( &white ) ;
202
203 LockPixels( GetGWorldPixMap( wp ) ) ;
204 CopyBits(GetPortBitMapForCopyBits(wp),
205 GetPortBitMapForCopyBits(wp),
206 &portRect,
207 &portRect,
208 srcCopy,NULL);
209 UnlockPixels( GetGWorldPixMap( wp ) ) ;
210 ClosePicture();
211 }
212 SetGWorld( origPort , origDev ) ;
213
214 return pict;
215}
216
20b69855 217void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
519cb848 218{
20b69855
SC
219 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
220 if ( bitmap.Ok() )
221 {
222 wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ;
223 if ( bmap == NULL )
224 return ;
225 info->contentType = kControlContentIconRef ;
226 info->u.iconRef = wxMacCreateIconRef( bitmap ) ;
227
228#if wxMAC_USE_CORE_GRAPHICS
229 /*
230 // only on 10.4 more controls will accept a CGImage
231
232 info->contentType = kControlContentCGImageRef ;
233 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
234 */
235#endif
236 }
519cb848
SC
237}
238
20b69855 239void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
519cb848 240{
20b69855 241 if ( info->contentType == kControlContentIconRef )
4e9ed364 242 {
20b69855 243 ReleaseIconRef(info->u.iconRef) ;
4e9ed364 244 }
df21986c 245#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
20b69855
SC
246 else if ( info->contentType == kControlContentCGImageRef )
247 {
248 CGImageRelease( info->u.imageRef ) ;
249 }
250#endif
251 else
4e9ed364 252 {
20b69855 253 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
4e9ed364 254 }
03e11df5 255}
519cb848 256
9cc62fc8 257#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
20b69855
SC
258
259void wxBitmapRefData::Init()
519cb848 260{
20b69855
SC
261 m_width = 0 ;
262 m_height = 0 ;
263 m_depth = 0 ;
264 m_ok = false ;
265 m_bitmapMask = NULL ;
266#if wxMAC_USE_CORE_GRAPHICS
267 m_cgImageRef = NULL ;
71cc158e 268#endif
20b69855
SC
269 m_hBitmap = NULL ;
270 m_hMaskBitmap = NULL;
271 m_maskBytesPerRow = NULL ;
71cc158e 272
20b69855
SC
273 m_rawAccessCount = 0 ;
274 m_hasAlpha = false;
519cb848
SC
275}
276
20b69855 277wxBitmapRefData::wxBitmapRefData()
5fde6fcc 278{
20b69855 279 Init() ;
72055702
SC
280}
281
20b69855 282wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
72055702 283{
20b69855
SC
284 Init() ;
285 Create( w , h , d ) ;
286}
55e18dbe 287
20b69855
SC
288bool wxBitmapRefData::Create( int w , int h , int d )
289{
290 m_width = w ;
291 m_height = h ;
292 m_depth = d ;
72055702 293
20b69855
SC
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) ;
71cc158e 299
20b69855
SC
300 m_hBitmap = NULL ;
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 ) ;
71cc158e 306
20b69855
SC
307 return m_ok ;
308}
72055702 309
20b69855
SC
310void wxBitmapRefData::UseAlpha( bool use )
311{
312 if ( m_hasAlpha == use )
313 return ;
314
315 m_hasAlpha = use ;
20b69855 316 if ( m_hasAlpha )
72055702 317 {
20b69855
SC
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) ;
71cc158e 330#if !wxMAC_USE_CORE_GRAPHICS
20b69855 331 UpdateAlphaMask() ;
71cc158e 332#endif
72055702
SC
333 }
334 else
335 {
20b69855
SC
336 DisposeGWorld( m_hMaskBitmap ) ;
337 m_hMaskBitmap = NULL ;
338 m_maskBytesPerRow = 0 ;
72055702 339 }
72055702
SC
340}
341
20b69855 342void *wxBitmapRefData::GetRawAccess() const
72055702 343{
20b69855
SC
344 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
345 return m_memBuf.GetData() ;
346}
72055702 347
20b69855
SC
348void *wxBitmapRefData::BeginRawAccess()
349{
350 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
351 wxASSERT( m_rawAccessCount == 0 ) ;
352 ++m_rawAccessCount ;
353#if wxMAC_USE_CORE_GRAPHICS
354 // we must destroy an existing cached image, as
355 // the bitmap data may change now
356 if ( m_cgImageRef )
357 {
358 CGImageRelease( m_cgImageRef ) ;
359 m_cgImageRef = NULL ;
360 }
361#endif
362 return m_memBuf.GetData() ;
363}
55e18dbe 364
20b69855
SC
365void wxBitmapRefData::EndRawAccess()
366{
367 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
368 wxASSERT( m_rawAccessCount == 1 ) ;
369 --m_rawAccessCount ;
370#if !wxMAC_USE_CORE_GRAPHICS
371 UpdateAlphaMask() ;
372#endif
373}
55e18dbe 374
55e18dbe 375
20b69855 376#if wxMAC_USE_CORE_GRAPHICS
71cc158e 377void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
20b69855 378{
71cc158e
SC
379 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
380 wxASSERT( data == membuf->GetData() ) ;
381 delete membuf ;
5fde6fcc
GD
382}
383
20b69855 384CGImageRef wxBitmapRefData::CGImageCreate() const
be295828 385{
20b69855
SC
386 wxASSERT( m_ok ) ;
387 wxASSERT( m_rawAccessCount >= 0 ) ;
388 CGImageRef image ;
389 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
be295828 390 {
20b69855
SC
391 size_t imageSize = m_width * m_height * 4 ;
392 void * dataBuffer = m_memBuf.GetData() ;
393 int w = m_width ;
394 int h = m_height ;
395 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
396 wxMemoryBuffer* membuf = NULL ;
397
398 if ( m_bitmapMask )
be295828 399 {
20b69855
SC
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)
be295828 407 {
20b69855
SC
408 unsigned char *sourcemask = sourcemaskstart ;
409 for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
be295828 410 {
20b69855 411 *destalpha = *sourcemask ;
be295828
SC
412 }
413 }
414 }
20b69855 415 else if ( m_hasAlpha )
e40298d5 416 {
20b69855
SC
417#if wxMAC_USE_PREMULTIPLIED_ALPHA
418 alphaInfo = kCGImageAlphaPremultipliedFirst ;
419#else
420 alphaInfo = kCGImageAlphaFirst ;
421#endif
422 membuf = new wxMemoryBuffer( m_memBuf ) ;
e40298d5 423 }
20b69855
SC
424 else
425 {
426 membuf = new wxMemoryBuffer( m_memBuf ) ;
427 }
428 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
429 CGDataProviderRef dataProvider =
71cc158e
SC
430 CGDataProviderCreateWithData( membuf , (const void *)membuf->GetData() , imageSize,
431 wxMacMemoryBufferReleaseProc );
20b69855
SC
432 image =
433 ::CGImageCreate( w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
434 dataProvider, NULL , false , kCGRenderingIntentDefault );
435 CGDataProviderRelease( dataProvider);
436 }
437 else
438 {
439 image = m_cgImageRef ;
440 CGImageRetain( image ) ;
be295828 441 }
20b69855
SC
442 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
443 {
444 // we keep it for later use
445 m_cgImageRef = image ;
446 CGImageRetain( image ) ;
447 }
448 return image ;
be295828 449}
20b69855 450#endif
be295828 451
20b69855
SC
452GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
453{
454 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
455 if ( mask )
456 {
457 *mask = NULL ;
458 if ( m_bitmapMask )
459 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
460 else if ( m_hasAlpha )
461 {
71cc158e 462#if !wxMAC_USE_CORE_GRAPHICS
20b69855
SC
463 if ( m_rawAccessCount > 0 )
464 UpdateAlphaMask() ;
71cc158e
SC
465#else
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
468 UpdateAlphaMask() ;
469#endif
20b69855
SC
470 *mask = m_hMaskBitmap ;
471 }
472 }
473 return m_hBitmap ;
e9576ca5
SC
474}
475
20b69855 476void wxBitmapRefData::UpdateAlphaMask() const
e9576ca5 477{
20b69855 478 if ( m_hasAlpha )
e40298d5 479 {
20b69855
SC
480 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
481 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
482
483 int h = GetHeight() ;
484 int w = GetWidth() ;
485
486 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
487 {
488 unsigned char* destalpha = destalphabase ;
489 for( int x = 0 ; x < w ; ++x , sourcemask += 4, ++destalpha )
e40298d5 490 {
20b69855 491 *destalpha = *sourcemask ;
e40298d5 492 }
20b69855
SC
493 }
494 }
495}
496
20b69855
SC
497void wxBitmapRefData::Free()
498{
499 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
500
501#if wxMAC_USE_CORE_GRAPHICS
502 if ( m_cgImageRef )
503 {
504 CGImageRelease( m_cgImageRef ) ;
505 m_cgImageRef = NULL ;
e40298d5 506 }
71cc158e 507#endif
20b69855
SC
508 if ( m_hBitmap )
509 {
510 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
511 m_hBitmap = NULL ;
512 }
513 if ( m_hMaskBitmap )
514 {
515 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
516 m_hMaskBitmap = NULL ;
517 }
55e18dbe 518
20b69855 519 if (m_bitmapMask)
e40298d5 520 {
20b69855
SC
521 delete m_bitmapMask;
522 m_bitmapMask = NULL;
e40298d5 523 }
e9576ca5
SC
524}
525
85f296a3
SC
526wxBitmapRefData::~wxBitmapRefData()
527{
20b69855 528 Free() ;
85f296a3
SC
529}
530
90b959ae
SC
531bool wxBitmap::CopyFromIcon(const wxIcon& icon)
532{
20b69855
SC
533 int w = icon.GetWidth() ;
534 int h = icon.GetHeight() ;
535 Create( icon.GetWidth() , icon.GetHeight() ) ;
536
71cc158e 537 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
20b69855
SC
538 {
539 IconFamilyHandle iconFamily = NULL ;
540 Handle imagehandle = NewHandle(0) ;
541 Handle maskhandle = NewHandle(0) ;
71cc158e 542
9cc62fc8
SC
543 OSType maskType = 0;
544 OSType dataType = 0;
545 IconSelectorValue selector = 0 ;
71cc158e
SC
546 if ( w == 128 )
547 {
548 dataType = kThumbnail32BitData ;
549 maskType = kThumbnail8BitMask ;
550 selector = kSelectorAllAvailableData ;
551 }
552 else if ( w == 48 )
553 {
554 dataType = kHuge32BitData ;
555 maskType = kHuge8BitMask ;
556 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
557 }
558 else if ( w == 32 )
559 {
560 dataType = kLarge32BitData ;
561 maskType = kLarge8BitMask ;
562 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
563 }
564 else if ( w == 16 )
565 {
566 dataType = kSmall32BitData ;
567 maskType = kSmall8BitMask ;
568 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
569 }
570 else
571 {
572 wxFAIL_MSG(wxT("Illegal icon size for conversion") ) ;
573 }
574
575
576 OSStatus err = ( IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ) ;
577
578 err =( GetIconFamilyData( iconFamily , dataType , imagehandle ) ) ;
579 err =( GetIconFamilyData( iconFamily , maskType , maskhandle ) ) ;
20b69855
SC
580 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
581 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
582 UseAlpha() ;
583 unsigned char *source = (unsigned char *) *imagehandle ;
584 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
585
586 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
587 for ( int y = 0 ; y < h ; ++y )
588 {
589 for ( int x = 0 ; x < w ; ++x )
590 {
591 *destination++ = *sourcemask++ ;
592 source++ ;
593 *destination++ = *source++ ;
594 *destination++ = *source++ ;
595 *destination++ = *source++ ;
596 }
597 }
598 EndRawAccess() ;
599 DisposeHandle( imagehandle ) ;
600 DisposeHandle( maskhandle ) ;
601 }
602 else
603 {
604 wxMemoryDC dc ;
605 dc.SelectObject( *this ) ;
606 dc.DrawIcon( icon , 0 , 0 ) ;
607 dc.SelectObject( wxNullBitmap ) ;
608 }
125c389e 609 return true;
90b959ae
SC
610}
611
e9576ca5
SC
612wxBitmap::wxBitmap()
613{
e9576ca5
SC
614}
615
616wxBitmap::~wxBitmap()
617{
e9576ca5
SC
618}
619
620wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
621{
20b69855 622 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
e9576ca5 623
d2c6d549
GD
624 if ( no_bits == 1 )
625 {
973b0afb
GD
626 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
627 if ( the_width % (sizeof(unsigned char) * 8) ) {
628 linesize += sizeof(unsigned char);
629 }
20b69855
SC
630 unsigned char* linestart = (unsigned char*) bits ;
631 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
973b0afb
GD
632 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
633 {
634 for ( int x = 0 ; x < the_width ; ++x )
635 {
636 int index = x / 8 ;
637 int bit = x % 8 ;
638 int mask = 1 << bit ;
639 if ( linestart[index] & mask )
640 {
20b69855
SC
641 *destination++ = 0xFF ;
642 *destination++ = 0 ;
643 *destination++ = 0 ;
644 *destination++ = 0 ;
973b0afb
GD
645 }
646 else
647 {
20b69855
SC
648 *destination++ = 0xFF ;
649 *destination++ = 0xFF ;
650 *destination++ = 0xFF ;
651 *destination++ = 0xFF ;
973b0afb
GD
652 }
653 }
654 }
20b69855 655 EndRawAccess() ;
d2c6d549
GD
656 }
657 else
658 {
973b0afb 659 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
d2c6d549 660 }
e9576ca5
SC
661}
662
663wxBitmap::wxBitmap(int w, int h, int d)
664{
665 (void)Create(w, h, d);
e9576ca5
SC
666}
667
a8562f55 668wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
669{
670 (void) Create(data, type, width, height, depth);
e9576ca5
SC
671}
672
a8562f55 673wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
e9576ca5 674{
a8562f55 675 LoadFile(filename, type);
e9576ca5
SC
676}
677
20b69855
SC
678wxBitmap::wxBitmap(const char **bits)
679{
680 (void) CreateFromXpm(bits);
681}
682
683wxBitmap::wxBitmap(char **bits)
684{
685 (void) CreateFromXpm((const char **)bits);
686}
687
688void* wxBitmap::GetRawAccess() const
689{
690 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
691 return M_BITMAPDATA->GetRawAccess() ;
692}
693
694void* wxBitmap::BeginRawAccess()
695{
696 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
697 return M_BITMAPDATA->BeginRawAccess() ;
698}
699
700void wxBitmap::EndRawAccess()
701{
702 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
703 M_BITMAPDATA->EndRawAccess() ;
704}
705
973b0afb 706bool wxBitmap::CreateFromXpm(const char **bits)
e9576ca5 707{
973b0afb
GD
708 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
709 wxXPMDecoder decoder;
710 wxImage img = decoder.ReadData(bits);
55e18dbe
VZ
711 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
712 *this = wxBitmap(img);
973b0afb
GD
713 return TRUE;
714}
715
20b69855
SC
716#if wxMAC_USE_CORE_GRAPHICS
717WXCGIMAGEREF wxBitmap::CGImageCreate() const
03e11df5 718{
20b69855
SC
719 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
720 return M_BITMAPDATA->CGImageCreate() ;
03e11df5 721}
20b69855 722#endif
03e11df5 723
5fde6fcc
GD
724wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
725{
20b69855 726 wxCHECK_MSG( Ok() &&
5fde6fcc
GD
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") );
731
55e18dbe 732
20b69855
SC
733 wxBitmap ret( rect.width, rect.height, GetDepth() );
734 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
5fde6fcc 735
5fde6fcc 736
20b69855
SC
737 int sourcewidth = GetWidth() ;
738 int destwidth = rect.width ;
739 int destheight = rect.height ;
740 {
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)
748 {
749 memcpy( dest , source , destlinesize ) ;
750 }
751 }
752 ret.EndRawAccess() ;
753
754 if ( M_BITMAPDATA->m_bitmapMask )
755 {
756 wxMemoryBuffer maskbuf ;
757 int rowBytes = ( destwidth + 3 ) & 0xFFFFFFC ;
758 size_t maskbufsize = rowBytes * destheight ;
759 unsigned char * destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
760
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 ;
766
767 for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
768 {
769 memcpy( dest , source , destlinesize ) ;
770 }
771 maskbuf.UngetWriteBuf( maskbufsize ) ;
772 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
773 }
774 else if ( HasAlpha() )
775 ret.UseAlpha() ;
5fde6fcc 776
20b69855 777 return ret;
5fde6fcc
GD
778}
779
e9576ca5
SC
780bool wxBitmap::Create(int w, int h, int d)
781{
782 UnRef();
783
20b69855
SC
784 if ( d < 0 )
785 d = wxDisplayDepth() ;
2a391f87 786
20b69855 787 m_refData = new wxBitmapRefData( w , h , d );
55e18dbe 788
20b69855 789 return M_BITMAPDATA->Ok() ;
519cb848
SC
790}
791
a8562f55 792bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
e9576ca5
SC
793{
794 UnRef();
795
e9576ca5
SC
796 wxBitmapHandler *handler = FindHandler(type);
797
a8562f55
GD
798 if ( handler )
799 {
4e9ed364 800 m_refData = new wxBitmapRefData;
e9576ca5 801
a8562f55 802 return handler->LoadFile(this, filename, type, -1, -1);
e9576ca5 803 }
a8562f55
GD
804 else
805 {
806 wxImage loadimage(filename, type);
807 if (loadimage.Ok()) {
808 *this = loadimage;
809 return true;
810 }
811 }
427ff662 812 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 813 return false;
e9576ca5
SC
814}
815
a8562f55 816bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
817{
818 UnRef();
819
820 m_refData = new wxBitmapRefData;
821
822 wxBitmapHandler *handler = FindHandler(type);
823
824 if ( handler == NULL ) {
427ff662 825 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
e9576ca5
SC
826
827 return FALSE;
828 }
829
830 return handler->Create(this, data, type, width, height, depth);
831}
832
fec19ea9
VS
833wxBitmap::wxBitmap(const wxImage& image, int depth)
834{
835 wxCHECK_RET( image.Ok(), wxT("invalid image") )
fec19ea9 836
fec19ea9
VS
837 // width and height of the device-dependent bitmap
838 int width = image.GetWidth();
839 int height = image.GetHeight();
840
20b69855 841 m_refData = new wxBitmapRefData( width , height , depth ) ;;
55e18dbe 842
20b69855 843 // Create picture
fec19ea9 844
20b69855
SC
845 bool hasAlpha = false ;
846
847 if ( image.HasMask() )
848 {
849 // takes precedence, don't mix with alpha info
850 }
851 else
852 {
853 hasAlpha = image.HasAlpha() ;
854 }
855
856 if ( hasAlpha )
857 UseAlpha() ;
858
859 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
55e18dbe 860
fec19ea9 861 register unsigned char* data = image.GetData();
20b69855 862 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
fec19ea9
VS
863 for (int y = 0; y < height; y++)
864 {
865 for (int x = 0; x < width; x++)
866 {
20b69855
SC
867 if ( hasAlpha )
868 {
869 const unsigned char a = *alpha++;
870 *destination++ = a ;
871#if wxMAC_USE_PREMULTIPLIED_ALPHA
872 *destination++ = ((*data++) * a + 127 ) / 255 ;
873 *destination++ = ((*data++) * a + 127 ) / 255 ;
874 *destination++ = ((*data++) * a + 127 ) / 255 ;
875#else
876 *destination++ = *data++ ;
877 *destination++ = *data++ ;
878 *destination++ = *data++ ;
879#endif
880 }
881 else
882 {
883 *destination++ = 0xFF ;
884 *destination++ = *data++ ;
885 *destination++ = *data++ ;
886 *destination++ = *data++ ;
887 }
fec19ea9 888 }
55e18dbe 889 }
20b69855
SC
890 EndRawAccess() ;
891 if ( image.HasMask() )
8dd336cb 892 {
20b69855 893 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
8dd336cb 894 }
fec19ea9
VS
895}
896
897wxImage wxBitmap::ConvertToImage() const
898{
899 wxImage image;
55e18dbe 900
fec19ea9
VS
901 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
902
903 // create an wxImage object
904 int width = GetWidth();
905 int height = GetHeight();
906 image.Create( width, height );
907
908 unsigned char *data = image.GetData();
fec19ea9
VS
909 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
910
20b69855
SC
911 unsigned char* source = (unsigned char*) GetRawAccess() ;
912
913 bool hasAlpha = false ;
914 bool hasMask = false ;
915 unsigned char *alpha = NULL ;
916 unsigned char *mask = NULL ;
917 if ( HasAlpha() )
2b5f62a0 918 {
20b69855 919 hasAlpha = true ;
2b5f62a0 920 }
20b69855
SC
921
922 if ( GetMask() )
2b5f62a0 923 {
20b69855
SC
924 hasMask = true ;
925 mask = (unsigned char*) GetMask()->GetRawAccess() ;
e40298d5 926 }
20b69855
SC
927
928 if ( hasAlpha )
e40298d5 929 {
20b69855
SC
930 image.SetAlpha() ;
931 alpha = image.GetAlpha() ;
e40298d5 932 }
20b69855
SC
933 int index = 0;
934
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;
942
fec19ea9
VS
943 for (int yy = 0; yy < height; yy++)
944 {
945 for (int xx = 0; xx < width; xx++)
946 {
20b69855
SC
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);
952 if ( hasMask )
55e18dbe 953 {
20b69855 954 if ( *mask++ == 0 )
e40298d5 955 {
20b69855
SC
956 if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
957 b = MASK_BLUE_REPLACEMENT ;
e40298d5 958 }
20b69855 959 else
e40298d5 960 {
20b69855
SC
961 r = MASK_RED ;
962 g = MASK_GREEN ;
963 b = MASK_BLUE ;
e40298d5 964 }
fec19ea9 965 }
20b69855
SC
966 else if ( hasAlpha )
967 *alpha++ = a ;
968
969 data[index ] = r ;
970 data[index + 1] = g ;
971 data[index + 2] = b ;
fec19ea9 972 index += 3;
20b69855 973 source += 4 ;
fec19ea9
VS
974 }
975 }
20b69855
SC
976 if ( hasMask )
977 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
fec19ea9
VS
978 return image;
979}
980
981
a8562f55
GD
982bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
983 const wxPalette *palette) const
e9576ca5
SC
984{
985 wxBitmapHandler *handler = FindHandler(type);
986
a8562f55
GD
987 if ( handler )
988 {
989 return handler->SaveFile(this, filename, type, palette);
990 }
991 else
992 {
993 wxImage image = ConvertToImage();
e9576ca5 994
a8562f55
GD
995 return image.SaveFile(filename, type);
996 }
55e18dbe 997
427ff662 998 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 999 return false;
e9576ca5
SC
1000}
1001
5fde6fcc
GD
1002bool wxBitmap::Ok() const
1003{
20b69855 1004 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
5fde6fcc
GD
1005}
1006
1007int wxBitmap::GetHeight() const
1008{
1009 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1010
20b69855 1011 return M_BITMAPDATA->GetHeight();
5fde6fcc
GD
1012}
1013
1014int wxBitmap::GetWidth() const
1015{
1016 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1017
20b69855 1018 return M_BITMAPDATA->GetWidth() ;
5fde6fcc
GD
1019}
1020
1021int wxBitmap::GetDepth() const
1022{
1023 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1024
20b69855 1025 return M_BITMAPDATA->GetDepth();
5fde6fcc
GD
1026}
1027
179e085f
RN
1028#if WXWIN_COMPATIBILITY_2_4
1029
5fde6fcc
GD
1030int wxBitmap::GetQuality() const
1031{
20b69855 1032 return 0;
5fde6fcc
GD
1033}
1034
179e085f
RN
1035#endif
1036
5fde6fcc
GD
1037wxMask *wxBitmap::GetMask() const
1038{
1039 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1040
1041 return M_BITMAPDATA->m_bitmapMask;
1042}
1043
20b69855
SC
1044bool wxBitmap::HasAlpha() const
1045{
1046 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1047
1048 return M_BITMAPDATA->HasAlpha() ;
1049}
1050
e9576ca5
SC
1051void wxBitmap::SetWidth(int w)
1052{
1053 if (!M_BITMAPDATA)
1054 m_refData = new wxBitmapRefData;
1055
20b69855 1056 M_BITMAPDATA->SetWidth(w);
e9576ca5
SC
1057}
1058
1059void wxBitmap::SetHeight(int h)
1060{
1061 if (!M_BITMAPDATA)
1062 m_refData = new wxBitmapRefData;
1063
20b69855 1064 M_BITMAPDATA->SetHeight(h);
e9576ca5
SC
1065}
1066
1067void wxBitmap::SetDepth(int d)
1068{
1069 if (!M_BITMAPDATA)
1070 m_refData = new wxBitmapRefData;
1071
20b69855 1072 M_BITMAPDATA->SetDepth(d);
e9576ca5
SC
1073}
1074
179e085f
RN
1075#if WXWIN_COMPATIBILITY_2_4
1076
20b69855 1077void wxBitmap::SetQuality(int WXUNUSED(quality))
e9576ca5 1078{
e9576ca5
SC
1079}
1080
179e085f
RN
1081#endif
1082
e9576ca5
SC
1083void wxBitmap::SetOk(bool isOk)
1084{
1085 if (!M_BITMAPDATA)
1086 m_refData = new wxBitmapRefData;
1087
20b69855 1088 M_BITMAPDATA->SetOk(isOk);
e9576ca5
SC
1089}
1090
a6de86fa 1091#if wxUSE_PALETTE
5fde6fcc
GD
1092wxPalette *wxBitmap::GetPalette() const
1093{
1094 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1095
1096 return &M_BITMAPDATA->m_bitmapPalette;
1097}
1098
e9576ca5
SC
1099void wxBitmap::SetPalette(const wxPalette& palette)
1100{
1101 if (!M_BITMAPDATA)
1102 m_refData = new wxBitmapRefData;
1103
1104 M_BITMAPDATA->m_bitmapPalette = palette ;
1105}
a6de86fa 1106#endif // wxUSE_PALETTE
e9576ca5
SC
1107
1108void wxBitmap::SetMask(wxMask *mask)
1109{
1110 if (!M_BITMAPDATA)
1111 m_refData = new wxBitmapRefData;
1112
a8562f55 1113 // Remove existing mask if there is one.
1e74d03b 1114 delete M_BITMAPDATA->m_bitmapMask;
a8562f55 1115
e9576ca5
SC
1116 M_BITMAPDATA->m_bitmapMask = mask ;
1117}
1118
20b69855 1119WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
5fde6fcc 1120{
20b69855 1121 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
5fde6fcc
GD
1122}
1123
20b69855
SC
1124// ----------------------------------------------------------------------------
1125// wxMask
1126// ----------------------------------------------------------------------------
e9576ca5
SC
1127
1128wxMask::wxMask()
1129{
20b69855 1130 Init() ;
e9576ca5
SC
1131}
1132
1133// Construct a mask from a bitmap and a colour indicating
1134// the transparent area
1135wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1136{
20b69855 1137 Init() ;
e9576ca5
SC
1138 Create(bitmap, colour);
1139}
1140
20b69855
SC
1141// Construct a mask from a mono bitmap (copies the bitmap).
1142wxMask::wxMask(const wxBitmap& bitmap)
e9576ca5 1143{
20b69855
SC
1144 Init() ;
1145 Create(bitmap);
e9576ca5
SC
1146}
1147
1148// Construct a mask from a mono bitmap (copies the bitmap).
20b69855 1149wxMask::wxMask(const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
e9576ca5 1150{
20b69855
SC
1151 Init() ;
1152 Create(data, width , height , bytesPerRow );
e9576ca5
SC
1153}
1154
1155wxMask::~wxMask()
1156{
4e9ed364
RR
1157 if ( m_maskBitmap )
1158 {
20b69855 1159 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
4e9ed364
RR
1160 m_maskBitmap = NULL ;
1161 }
e9576ca5
SC
1162}
1163
20b69855 1164void wxMask::Init()
e9576ca5 1165{
20b69855 1166 m_width = m_height = m_bytesPerRow = 0 ;
20b69855 1167 m_maskBitmap = NULL ;
20b69855 1168}
5fde6fcc 1169
20b69855
SC
1170void *wxMask::GetRawAccess() const
1171{
1172 return m_memBuf.GetData() ;
1173}
5fde6fcc 1174
20b69855
SC
1175void wxMask::RealizeNative()
1176{
20b69855
SC
1177 if ( m_maskBitmap )
1178 {
1179 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1180 m_maskBitmap = NULL ;
1181 }
1182 Rect rect = { 0 , 0 , m_height , m_width } ;
1183 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_maskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 ,
1184 (char*) m_memBuf.GetData() , m_bytesPerRow ) ) ;
20b69855 1185}
5fde6fcc 1186
20b69855
SC
1187// Create a mask from a mono bitmap (copies the bitmap).
1188bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1189{
1190 m_memBuf = data ;
1191 m_width = width ;
1192 m_height = height ;
1193 m_bytesPerRow = bytesPerRow ;
1194 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1195 RealizeNative() ;
1196 return true ;
e9576ca5
SC
1197}
1198
20b69855
SC
1199// Create a mask from a mono bitmap (copies the bitmap).
1200bool wxMask::Create(const wxBitmap& bitmap)
e9576ca5 1201{
20b69855
SC
1202 m_width = bitmap.GetWidth() ;
1203 m_height = bitmap.GetHeight() ;
1204 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1205 size_t size = m_bytesPerRow * m_height ;
1206 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1207 memset( destdatabase , 0 , size ) ;
1208 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1209 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1210 {
1211 unsigned char *destdata= destdatabase ;
1212 for( int x = 0 ; x < m_width ; ++x )
1213 {
1214 srcdata++ ;
1215 unsigned char r = *srcdata++ ;
1216 unsigned char g = *srcdata++ ;
1217 unsigned char b = *srcdata++ ;
1218 if ( ( r + g + b ) > 0x10 )
1219 *destdata++ = 0x00 ;
1220 else
1221 *destdata++ = 0xFF ;
1222 }
1223 }
1224 m_memBuf.UngetWriteBuf( size ) ;
1225 RealizeNative() ;
1226 return TRUE;
e9576ca5
SC
1227}
1228
1229// Create a mask from a bitmap and a colour indicating
1230// the transparent area
1231bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1232{
20b69855
SC
1233 m_width = bitmap.GetWidth() ;
1234 m_height = bitmap.GetHeight() ;
1235 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1236 size_t size = m_bytesPerRow * m_height ;
1237
1238 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1239 memset( destdatabase , 0 , size ) ;
1240 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1241 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
8208e181 1242 {
20b69855
SC
1243 unsigned char *destdata= destdatabase ;
1244 for( int x = 0 ; x < m_width ; ++x )
55e18dbe 1245 {
20b69855
SC
1246 srcdata++ ;
1247 unsigned char r = *srcdata++ ;
1248 unsigned char g = *srcdata++ ;
1249 unsigned char b = *srcdata++ ;
1250 if ( colour == wxColour( r , g , b) )
1251 *destdata++ = 0x00 ;
8208e181 1252 else
20b69855 1253 *destdata++ = 0xFF ;
8208e181
SC
1254 }
1255 }
20b69855
SC
1256 m_memBuf.UngetWriteBuf( size ) ;
1257 RealizeNative() ;
8208e181 1258 return TRUE;
e9576ca5
SC
1259}
1260
20b69855 1261WXHBITMAP wxMask::GetHBITMAP() const
5fde6fcc 1262{
20b69855 1263 return m_maskBitmap ;
5fde6fcc
GD
1264}
1265
20b69855
SC
1266// ----------------------------------------------------------------------------
1267// wxBitmapHandler
1268// ----------------------------------------------------------------------------
e9576ca5 1269
be52b341
GD
1270wxBitmapHandler::~wxBitmapHandler()
1271{
1272}
1273
e9576ca5
SC
1274bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1275{
1276 return FALSE;
1277}
1278
a8562f55 1279bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1280 int desiredWidth, int desiredHeight)
1281{
1282 return FALSE;
1283}
1284
a8562f55 1285bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5
SC
1286{
1287 return FALSE;
1288}
1289
20b69855
SC
1290// ----------------------------------------------------------------------------
1291// Standard Handlers
1292// ----------------------------------------------------------------------------
e9576ca5 1293
519cb848
SC
1294class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1295{
1296 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1297public:
1298 inline wxPICTResourceHandler()
1299 {
3bf2bdfb
GD
1300 SetName(wxT("Macintosh Pict resource"));
1301 SetExtension(wxEmptyString);
1302 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
519cb848
SC
1303 };
1304
1305 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1306 int desiredWidth, int desiredHeight);
1307};
1308IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1309
179e085f 1310
519cb848
SC
1311bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1312 int desiredWidth, int desiredHeight)
1313{
179e085f 1314#if wxUSE_METAFILE
4e9ed364 1315 Str255 theName ;
427ff662 1316 wxMacStringToPascal( name , theName ) ;
55e18dbe 1317
4e9ed364
RR
1318 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1319 if ( thePict )
1320 {
20b69855
SC
1321 wxMetafile mf ;
1322 mf.SetHMETAFILE((WXHMETAFILE) thePict ) ;
1323 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1324 wxMemoryDC dc ;
1325 dc.SelectObject( *bitmap ) ;
1326 mf.Play( &dc ) ;
1327 dc.SelectObject( wxNullBitmap ) ;
4e9ed364
RR
1328 return TRUE ;
1329 }
179e085f 1330#endif //wxUSE_METAFILE
4e9ed364 1331 return FALSE ;
519cb848
SC
1332}
1333
179e085f 1334
e9576ca5
SC
1335void wxBitmap::InitStandardHandlers()
1336{
973b0afb
GD
1337 AddHandler(new wxPICTResourceHandler) ;
1338 AddHandler(new wxICONResourceHandler) ;
e9576ca5 1339}
55e18dbe
VZ
1340
1341// ----------------------------------------------------------------------------
1342// raw bitmap access support
1343// ----------------------------------------------------------------------------
1344
1345void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1346{
1347 if ( !Ok() )
1348 {
1349 // no bitmap, no data (raw or otherwise)
1350 return NULL;
1351 }
1352
20b69855
SC
1353 data.m_width = GetWidth() ;
1354 data.m_height = GetHeight() ;
1355 data.m_stride = GetWidth() * 4 ;
1356 return GetRawAccess() ;
55e18dbe
VZ
1357}
1358
1e74d03b 1359void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
55e18dbe
VZ
1360{
1361 if ( !Ok() )
1362 return;
1363
20b69855
SC
1364 // TODO : if we have some information about the API we should check
1365 // this code looks strange...
1366
1367 if ( M_BITMAPDATA->HasAlpha() )
1e74d03b
VZ
1368 {
1369 wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;
1370
1371 int w = data.GetWidth(),
1372 h = data.GetHeight();
1373
1374 wxBitmap bmpMask(GetWidth(), GetHeight(), 32);
1375 wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h));
1376 wxAlphaPixelData::Iterator pMask(dataMask),
1377 p(data);
1378 for ( int y = 0; y < h; y++ )
1379 {
1380 wxAlphaPixelData::Iterator rowStartMask = pMask,
1381 rowStart = p;
1382
1383 for ( int x = 0; x < w; x++ )
1384 {
1385 const wxAlphaPixelData::Iterator::ChannelType
1386 alpha = p.Alpha();
1387
1388 pMask.Red() = alpha;
1389 pMask.Green() = alpha;
1390 pMask.Blue() = alpha;
1391
1392 ++p;
1393 ++pMask;
1394 }
1395
1396 p = rowStart;
1397 p.OffsetY(data, 1);
1398
1399 pMask = rowStartMask;
1400 pMask.OffsetY(dataMask, 1);
1401 }
1402
1403 SetMask(new wxMask(bmpMask));
1404 }
55e18dbe
VZ
1405}
1406
1407void wxBitmap::UseAlpha()
1408{
1e74d03b
VZ
1409 // remember that we are using alpha channel, we'll need to create a proper
1410 // mask in UngetRawData()
20b69855 1411 M_BITMAPDATA->UseAlpha( true );
55e18dbe 1412}