]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bitmap.cpp
update from Hans F. Nordhaug
[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
1028int wxBitmap::GetQuality() const
1029{
20b69855 1030 return 0;
5fde6fcc
GD
1031}
1032
1033wxMask *wxBitmap::GetMask() const
1034{
1035 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1036
1037 return M_BITMAPDATA->m_bitmapMask;
1038}
1039
20b69855
SC
1040bool wxBitmap::HasAlpha() const
1041{
1042 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1043
1044 return M_BITMAPDATA->HasAlpha() ;
1045}
1046
e9576ca5
SC
1047void wxBitmap::SetWidth(int w)
1048{
1049 if (!M_BITMAPDATA)
1050 m_refData = new wxBitmapRefData;
1051
20b69855 1052 M_BITMAPDATA->SetWidth(w);
e9576ca5
SC
1053}
1054
1055void wxBitmap::SetHeight(int h)
1056{
1057 if (!M_BITMAPDATA)
1058 m_refData = new wxBitmapRefData;
1059
20b69855 1060 M_BITMAPDATA->SetHeight(h);
e9576ca5
SC
1061}
1062
1063void wxBitmap::SetDepth(int d)
1064{
1065 if (!M_BITMAPDATA)
1066 m_refData = new wxBitmapRefData;
1067
20b69855 1068 M_BITMAPDATA->SetDepth(d);
e9576ca5
SC
1069}
1070
20b69855 1071void wxBitmap::SetQuality(int WXUNUSED(quality))
e9576ca5 1072{
e9576ca5
SC
1073}
1074
1075void wxBitmap::SetOk(bool isOk)
1076{
1077 if (!M_BITMAPDATA)
1078 m_refData = new wxBitmapRefData;
1079
20b69855 1080 M_BITMAPDATA->SetOk(isOk);
e9576ca5
SC
1081}
1082
a6de86fa 1083#if wxUSE_PALETTE
5fde6fcc
GD
1084wxPalette *wxBitmap::GetPalette() const
1085{
1086 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1087
1088 return &M_BITMAPDATA->m_bitmapPalette;
1089}
1090
e9576ca5
SC
1091void wxBitmap::SetPalette(const wxPalette& palette)
1092{
1093 if (!M_BITMAPDATA)
1094 m_refData = new wxBitmapRefData;
1095
1096 M_BITMAPDATA->m_bitmapPalette = palette ;
1097}
a6de86fa 1098#endif // wxUSE_PALETTE
e9576ca5
SC
1099
1100void wxBitmap::SetMask(wxMask *mask)
1101{
1102 if (!M_BITMAPDATA)
1103 m_refData = new wxBitmapRefData;
1104
a8562f55 1105 // Remove existing mask if there is one.
1e74d03b 1106 delete M_BITMAPDATA->m_bitmapMask;
a8562f55 1107
e9576ca5
SC
1108 M_BITMAPDATA->m_bitmapMask = mask ;
1109}
1110
20b69855 1111WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
5fde6fcc 1112{
20b69855 1113 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
5fde6fcc
GD
1114}
1115
20b69855
SC
1116// ----------------------------------------------------------------------------
1117// wxMask
1118// ----------------------------------------------------------------------------
e9576ca5
SC
1119
1120wxMask::wxMask()
1121{
20b69855 1122 Init() ;
e9576ca5
SC
1123}
1124
1125// Construct a mask from a bitmap and a colour indicating
1126// the transparent area
1127wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1128{
20b69855 1129 Init() ;
e9576ca5
SC
1130 Create(bitmap, colour);
1131}
1132
20b69855
SC
1133// Construct a mask from a mono bitmap (copies the bitmap).
1134wxMask::wxMask(const wxBitmap& bitmap)
e9576ca5 1135{
20b69855
SC
1136 Init() ;
1137 Create(bitmap);
e9576ca5
SC
1138}
1139
1140// Construct a mask from a mono bitmap (copies the bitmap).
20b69855 1141wxMask::wxMask(const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
e9576ca5 1142{
20b69855
SC
1143 Init() ;
1144 Create(data, width , height , bytesPerRow );
e9576ca5
SC
1145}
1146
1147wxMask::~wxMask()
1148{
4e9ed364
RR
1149 if ( m_maskBitmap )
1150 {
20b69855 1151 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
4e9ed364
RR
1152 m_maskBitmap = NULL ;
1153 }
e9576ca5
SC
1154}
1155
20b69855 1156void wxMask::Init()
e9576ca5 1157{
20b69855 1158 m_width = m_height = m_bytesPerRow = 0 ;
20b69855 1159 m_maskBitmap = NULL ;
20b69855 1160}
5fde6fcc 1161
20b69855
SC
1162void *wxMask::GetRawAccess() const
1163{
1164 return m_memBuf.GetData() ;
1165}
5fde6fcc 1166
20b69855
SC
1167void wxMask::RealizeNative()
1168{
20b69855
SC
1169 if ( m_maskBitmap )
1170 {
1171 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1172 m_maskBitmap = NULL ;
1173 }
1174 Rect rect = { 0 , 0 , m_height , m_width } ;
1175 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_maskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 ,
1176 (char*) m_memBuf.GetData() , m_bytesPerRow ) ) ;
20b69855 1177}
5fde6fcc 1178
20b69855
SC
1179// Create a mask from a mono bitmap (copies the bitmap).
1180bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1181{
1182 m_memBuf = data ;
1183 m_width = width ;
1184 m_height = height ;
1185 m_bytesPerRow = bytesPerRow ;
1186 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1187 RealizeNative() ;
1188 return true ;
e9576ca5
SC
1189}
1190
20b69855
SC
1191// Create a mask from a mono bitmap (copies the bitmap).
1192bool wxMask::Create(const wxBitmap& bitmap)
e9576ca5 1193{
20b69855
SC
1194 m_width = bitmap.GetWidth() ;
1195 m_height = bitmap.GetHeight() ;
1196 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1197 size_t size = m_bytesPerRow * m_height ;
1198 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1199 memset( destdatabase , 0 , size ) ;
1200 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1201 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1202 {
1203 unsigned char *destdata= destdatabase ;
1204 for( int x = 0 ; x < m_width ; ++x )
1205 {
1206 srcdata++ ;
1207 unsigned char r = *srcdata++ ;
1208 unsigned char g = *srcdata++ ;
1209 unsigned char b = *srcdata++ ;
1210 if ( ( r + g + b ) > 0x10 )
1211 *destdata++ = 0x00 ;
1212 else
1213 *destdata++ = 0xFF ;
1214 }
1215 }
1216 m_memBuf.UngetWriteBuf( size ) ;
1217 RealizeNative() ;
1218 return TRUE;
e9576ca5
SC
1219}
1220
1221// Create a mask from a bitmap and a colour indicating
1222// the transparent area
1223bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1224{
20b69855
SC
1225 m_width = bitmap.GetWidth() ;
1226 m_height = bitmap.GetHeight() ;
1227 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1228 size_t size = m_bytesPerRow * m_height ;
1229
1230 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1231 memset( destdatabase , 0 , size ) ;
1232 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1233 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
8208e181 1234 {
20b69855
SC
1235 unsigned char *destdata= destdatabase ;
1236 for( int x = 0 ; x < m_width ; ++x )
55e18dbe 1237 {
20b69855
SC
1238 srcdata++ ;
1239 unsigned char r = *srcdata++ ;
1240 unsigned char g = *srcdata++ ;
1241 unsigned char b = *srcdata++ ;
1242 if ( colour == wxColour( r , g , b) )
1243 *destdata++ = 0x00 ;
8208e181 1244 else
20b69855 1245 *destdata++ = 0xFF ;
8208e181
SC
1246 }
1247 }
20b69855
SC
1248 m_memBuf.UngetWriteBuf( size ) ;
1249 RealizeNative() ;
8208e181 1250 return TRUE;
e9576ca5
SC
1251}
1252
20b69855 1253WXHBITMAP wxMask::GetHBITMAP() const
5fde6fcc 1254{
20b69855 1255 return m_maskBitmap ;
5fde6fcc
GD
1256}
1257
20b69855
SC
1258// ----------------------------------------------------------------------------
1259// wxBitmapHandler
1260// ----------------------------------------------------------------------------
e9576ca5 1261
be52b341
GD
1262wxBitmapHandler::~wxBitmapHandler()
1263{
1264}
1265
e9576ca5
SC
1266bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1267{
1268 return FALSE;
1269}
1270
a8562f55 1271bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1272 int desiredWidth, int desiredHeight)
1273{
1274 return FALSE;
1275}
1276
a8562f55 1277bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5
SC
1278{
1279 return FALSE;
1280}
1281
20b69855
SC
1282// ----------------------------------------------------------------------------
1283// Standard Handlers
1284// ----------------------------------------------------------------------------
e9576ca5 1285
519cb848
SC
1286class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1287{
1288 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1289public:
1290 inline wxPICTResourceHandler()
1291 {
3bf2bdfb
GD
1292 SetName(wxT("Macintosh Pict resource"));
1293 SetExtension(wxEmptyString);
1294 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
519cb848
SC
1295 };
1296
1297 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1298 int desiredWidth, int desiredHeight);
1299};
1300IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1301
1302bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1303 int desiredWidth, int desiredHeight)
1304{
4e9ed364 1305 Str255 theName ;
427ff662 1306 wxMacStringToPascal( name , theName ) ;
55e18dbe 1307
4e9ed364
RR
1308 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1309 if ( thePict )
1310 {
20b69855
SC
1311 wxMetafile mf ;
1312 mf.SetHMETAFILE((WXHMETAFILE) thePict ) ;
1313 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1314 wxMemoryDC dc ;
1315 dc.SelectObject( *bitmap ) ;
1316 mf.Play( &dc ) ;
1317 dc.SelectObject( wxNullBitmap ) ;
4e9ed364
RR
1318 return TRUE ;
1319 }
1320 return FALSE ;
519cb848
SC
1321}
1322
e9576ca5
SC
1323void wxBitmap::InitStandardHandlers()
1324{
973b0afb
GD
1325 AddHandler(new wxPICTResourceHandler) ;
1326 AddHandler(new wxICONResourceHandler) ;
e9576ca5 1327}
55e18dbe
VZ
1328
1329// ----------------------------------------------------------------------------
1330// raw bitmap access support
1331// ----------------------------------------------------------------------------
1332
1333void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1334{
1335 if ( !Ok() )
1336 {
1337 // no bitmap, no data (raw or otherwise)
1338 return NULL;
1339 }
1340
20b69855
SC
1341 data.m_width = GetWidth() ;
1342 data.m_height = GetHeight() ;
1343 data.m_stride = GetWidth() * 4 ;
1344 return GetRawAccess() ;
55e18dbe
VZ
1345}
1346
1e74d03b 1347void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
55e18dbe
VZ
1348{
1349 if ( !Ok() )
1350 return;
1351
20b69855
SC
1352 // TODO : if we have some information about the API we should check
1353 // this code looks strange...
1354
1355 if ( M_BITMAPDATA->HasAlpha() )
1e74d03b
VZ
1356 {
1357 wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;
1358
1359 int w = data.GetWidth(),
1360 h = data.GetHeight();
1361
1362 wxBitmap bmpMask(GetWidth(), GetHeight(), 32);
1363 wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h));
1364 wxAlphaPixelData::Iterator pMask(dataMask),
1365 p(data);
1366 for ( int y = 0; y < h; y++ )
1367 {
1368 wxAlphaPixelData::Iterator rowStartMask = pMask,
1369 rowStart = p;
1370
1371 for ( int x = 0; x < w; x++ )
1372 {
1373 const wxAlphaPixelData::Iterator::ChannelType
1374 alpha = p.Alpha();
1375
1376 pMask.Red() = alpha;
1377 pMask.Green() = alpha;
1378 pMask.Blue() = alpha;
1379
1380 ++p;
1381 ++pMask;
1382 }
1383
1384 p = rowStart;
1385 p.OffsetY(data, 1);
1386
1387 pMask = rowStartMask;
1388 pMask.OffsetY(dataMask, 1);
1389 }
1390
1391 SetMask(new wxMask(bmpMask));
1392 }
55e18dbe
VZ
1393}
1394
1395void wxBitmap::UseAlpha()
1396{
1e74d03b
VZ
1397 // remember that we are using alpha channel, we'll need to create a proper
1398 // mask in UngetRawData()
20b69855 1399 M_BITMAPDATA->UseAlpha( true );
55e18dbe 1400}