]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bitmap.cpp
Renamed DrawCheckButton due to conflict with existing and public api of wxUniversal...
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/mac/carbon/bitmap.cpp
e9576ca5 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#include "wx/wxprec.h"
b698c8e9 13
e9576ca5 14#include "wx/bitmap.h"
e4db172a
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
18#endif
19
e9576ca5 20#include "wx/icon.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)
1cb97a54 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 37#include "wx/mac/uma.h"
05d8deda
RN
38#include "wx/dcmemory.h"
39
20b69855 40// Implementation Notes
902725ee 41// --------------------
20b69855 42//
902725ee
WS
43// we are always working with a 32 bit deep pixel buffer
44// under QuickDraw its alpha parts are going to be ignored in the GWorld,
20b69855
SC
45// therefore we have a separate GWorld there for blitting the mask in
46
47// under Quartz then content is transformed into a CGImageRef representing the same data
48// which can be transferred to the GPU by the OS for fast rendering
49
1cb97a54 50// we don't dare use premultiplied alpha yet
20b69855
SC
51#define wxMAC_USE_PREMULTIPLIED_ALPHA 0
52
d45318b8
RN
53#if wxUSE_BMPBUTTON
54
20b69855 55void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
519cb848 56{
20b69855
SC
57 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
58 if ( bitmap.Ok() )
59 {
45cf8535 60 wxBitmapRefData * bmap = bitmap.GetBitmapData() ;
20b69855
SC
61 if ( bmap == NULL )
62 return ;
902725ee 63
45cf8535 64 if ( ( bmap->HasNativeSize() && forceType == 0 ) || forceType == kControlContentIconRef )
b28aeea5 65 {
45cf8535 66 wxBitmap scaleBmp ;
45cf8535 67 wxBitmapRefData* bmp = bmap ;
902725ee 68
45cf8535
SC
69 if ( !bmap->HasNativeSize() )
70 {
71 // as PICT conversion will only result in a 16x16 icon, let's attempt
902725ee
WS
72 // a few scales for better results
73
45cf8535
SC
74 int w = bitmap.GetWidth() ;
75 int h = bitmap.GetHeight() ;
76 int sz = wxMax( w , h ) ;
1cb97a54 77 if ( sz == 24 || sz == 64 )
45cf8535
SC
78 {
79 scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ;
80 bmp = scaleBmp.GetBitmapData() ;
81 }
82 }
902725ee 83
b28aeea5 84 info->contentType = kControlContentIconRef ;
45cf8535
SC
85 info->u.iconRef = bmp->GetIconRef() ;
86 AcquireIconRef( info->u.iconRef ) ;
b28aeea5 87 }
1cb97a54 88#if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
45cf8535
SC
89 else if ( forceType == kControlContentCGImageRef )
90 {
91 info->contentType = kControlContentCGImageRef ;
92 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
93 }
94#endif
b28aeea5
SC
95 else
96 {
97 info->contentType = kControlContentPictHandle ;
98 info->u.picture = bmap->GetPictHandle() ;
99 }
20b69855 100 }
519cb848
SC
101}
102
20b69855 103void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
519cb848 104{
20b69855 105 if ( info->contentType == kControlContentIconRef )
4e9ed364 106 {
45cf8535 107 ReleaseIconRef( info->u.iconRef ) ;
b28aeea5 108 }
531436ec
SC
109 else if ( info->contentType == kControlNoContent )
110 {
111 // there's no bitmap at all, fall through silently
112 }
b28aeea5
SC
113 else if ( info->contentType == kControlContentPictHandle )
114 {
45cf8535 115 // owned by the bitmap, no release here
4e9ed364 116 }
1cb97a54 117#if defined( __WXMAC_OSX__ ) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
20b69855
SC
118 else if ( info->contentType == kControlContentCGImageRef )
119 {
120 CGImageRelease( info->u.imageRef ) ;
121 }
122#endif
123 else
4e9ed364 124 {
20b69855 125 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
4e9ed364 126 }
03e11df5 127}
519cb848 128
d45318b8
RN
129#endif //wxUSE_BMPBUTTON
130
9cc62fc8 131#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
20b69855
SC
132
133void wxBitmapRefData::Init()
519cb848 134{
20b69855
SC
135 m_width = 0 ;
136 m_height = 0 ;
137 m_depth = 0 ;
138 m_ok = false ;
139 m_bitmapMask = NULL ;
1cb97a54 140
abb4f9c9 141#ifdef __WXMAC_OSX__
20b69855 142 m_cgImageRef = NULL ;
71cc158e 143#endif
1cb97a54 144
b28aeea5
SC
145 m_iconRef = NULL ;
146 m_pictHandle = NULL ;
20b69855
SC
147 m_hBitmap = NULL ;
148 m_hMaskBitmap = NULL;
64f45623 149 m_maskBytesPerRow = 0 ;
71cc158e 150
20b69855
SC
151 m_rawAccessCount = 0 ;
152 m_hasAlpha = false;
519cb848
SC
153}
154
20b69855 155wxBitmapRefData::wxBitmapRefData()
5fde6fcc 156{
20b69855 157 Init() ;
72055702
SC
158}
159
902725ee 160wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
72055702 161{
20b69855
SC
162 Init() ;
163 Create( w , h , d ) ;
164}
55e18dbe 165
902725ee 166bool wxBitmapRefData::Create( int w , int h , int d )
20b69855
SC
167{
168 m_width = w ;
902725ee 169 m_height = h ;
20b69855 170 m_depth = d ;
72055702 171
20b69855
SC
172 m_bytesPerRow = w * 4 ;
173 size_t size = m_bytesPerRow * h ;
1cb97a54
DS
174 void* data = m_memBuf.GetWriteBuf( size ) ;
175 memset( data , 0 , size ) ;
176 m_memBuf.UngetWriteBuf( size ) ;
71cc158e 177
20b69855
SC
178 m_hBitmap = NULL ;
179 Rect rect = { 0 , 0 , m_height , m_width } ;
180 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
902725ee 181 (char*) data , m_bytesPerRow ) ) ;
20b69855 182 wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ;
1cb97a54 183
20b69855 184 m_ok = ( m_hBitmap != NULL ) ;
71cc158e 185
902725ee 186 return m_ok ;
20b69855 187}
72055702 188
20b69855
SC
189void wxBitmapRefData::UseAlpha( bool use )
190{
191 if ( m_hasAlpha == use )
192 return ;
902725ee 193
20b69855 194 m_hasAlpha = use ;
20b69855 195 if ( m_hasAlpha )
72055702 196 {
1cb97a54
DS
197 wxASSERT( m_hMaskBitmap == NULL ) ;
198
20b69855
SC
199 int width = GetWidth() ;
200 int height = GetHeight() ;
431c82e0 201 m_maskBytesPerRow = ( width * 4 + 3 ) & 0xFFFFFFC ;
20b69855
SC
202 size_t size = height * m_maskBytesPerRow ;
203 unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ;
1cb97a54
DS
204 wxASSERT( data != NULL ) ;
205
20b69855 206 memset( data , 0 , size ) ;
20b69855 207 Rect rect = { 0 , 0 , height , width } ;
431c82e0 208 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
902725ee 209 (char*) data , m_maskBytesPerRow ) ) ;
20b69855
SC
210 wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ;
211 m_maskMemBuf.UngetWriteBuf(size) ;
1cb97a54 212
71cc158e 213#if !wxMAC_USE_CORE_GRAPHICS
20b69855 214 UpdateAlphaMask() ;
71cc158e 215#endif
72055702
SC
216 }
217 else
218 {
20b69855
SC
219 DisposeGWorld( m_hMaskBitmap ) ;
220 m_hMaskBitmap = NULL ;
221 m_maskBytesPerRow = 0 ;
72055702 222 }
72055702
SC
223}
224
20b69855 225void *wxBitmapRefData::GetRawAccess() const
72055702 226{
20b69855
SC
227 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
228 return m_memBuf.GetData() ;
229}
72055702 230
902725ee 231void *wxBitmapRefData::BeginRawAccess()
20b69855
SC
232{
233 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
234 wxASSERT( m_rawAccessCount == 0 ) ;
902725ee 235 wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
b28aeea5 236 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
1cb97a54
DS
237
238 ++m_rawAccessCount ;
239
abb4f9c9 240#ifdef __WXMAC_OSX__
1cb97a54
DS
241 // we must destroy an existing cached image, as
242 // the bitmap data may change now
20b69855
SC
243 if ( m_cgImageRef )
244 {
245 CGImageRelease( m_cgImageRef ) ;
246 m_cgImageRef = NULL ;
247 }
248#endif
1cb97a54 249
20b69855
SC
250 return m_memBuf.GetData() ;
251}
55e18dbe 252
20b69855
SC
253void wxBitmapRefData::EndRawAccess()
254{
255 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
256 wxASSERT( m_rawAccessCount == 1 ) ;
1cb97a54 257
20b69855 258 --m_rawAccessCount ;
1cb97a54 259
20b69855
SC
260#if !wxMAC_USE_CORE_GRAPHICS
261 UpdateAlphaMask() ;
262#endif
263}
55e18dbe 264
b28aeea5
SC
265bool wxBitmapRefData::HasNativeSize()
266{
267 int w = GetWidth() ;
268 int h = GetHeight() ;
269 int sz = wxMax( w , h ) ;
902725ee 270
1cb97a54 271 return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 );
b28aeea5
SC
272}
273
274IconRef wxBitmapRefData::GetIconRef()
275{
276 if ( m_iconRef == NULL )
277 {
278 // Create Icon Family Handle
902725ee 279
b28aeea5 280 IconFamilyHandle iconFamily = NULL ;
902725ee 281
169d1d64 282#ifdef WORDS_BIGENDIAN
1cb97a54 283 iconFamily = (IconFamilyHandle) NewHandle( 8 ) ;
b28aeea5
SC
284 (**iconFamily).resourceType = kIconFamilyType ;
285 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
169d1d64
SC
286#else
287 // test this solution on big endian as well
1cb97a54 288 iconFamily = (IconFamilyHandle) NewHandle( 0 ) ;
169d1d64 289#endif
902725ee 290
b28aeea5
SC
291 int w = GetWidth() ;
292 int h = GetHeight() ;
293 int sz = wxMax( w , h ) ;
902725ee 294
b28aeea5
SC
295 OSType dataType = 0 ;
296 OSType maskType = 0 ;
297
1cb97a54 298 switch (sz)
b28aeea5 299 {
1cb97a54
DS
300 case 128:
301 dataType = kThumbnail32BitData ;
302 maskType = kThumbnail8BitMask ;
303 break;
304
305 case 48:
306 dataType = kHuge32BitData ;
307 maskType = kHuge8BitMask ;
308 break;
309
310 case 32:
311 dataType = kLarge32BitData ;
312 maskType = kLarge8BitMask ;
313 break;
314
315 case 16:
316 dataType = kSmall32BitData ;
317 maskType = kSmall8BitMask ;
318 break;
319
320 default:
321 break;
b28aeea5
SC
322 }
323
324 if ( dataType != 0 )
325 {
326 // setup the header properly
327
902725ee 328 Handle data = NULL ;
b28aeea5
SC
329 Handle maskdata = NULL ;
330 unsigned char * maskptr = NULL ;
331 unsigned char * ptr = NULL ;
1cb97a54 332 size_t datasize, masksize ;
b28aeea5 333
1cb97a54
DS
334 datasize = sz * sz * 4 ;
335 data = NewHandle( datasize ) ;
b28aeea5
SC
336 HLock( data ) ;
337 ptr = (unsigned char*) *data ;
1cb97a54 338 memset( ptr , 0, datasize ) ;
b28aeea5
SC
339
340 masksize = sz * sz ;
902725ee 341 maskdata = NewHandle( masksize ) ;
b28aeea5
SC
342 HLock( maskdata ) ;
343 maskptr = (unsigned char*) *maskdata ;
344 memset( maskptr , 0 , masksize ) ;
345
346 bool hasAlpha = HasAlpha() ;
347 wxMask *mask = m_bitmapMask ;
348 unsigned char * source = (unsigned char*) GetRawAccess() ;
349 unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
1cb97a54 350
b28aeea5
SC
351 for ( int y = 0 ; y < h ; ++y )
352 {
353 unsigned char * dest = ptr + y * sz * 4 ;
354 unsigned char * maskdest = maskptr + y * sz ;
1cb97a54
DS
355 unsigned char a, r, g, b;
356
b28aeea5
SC
357 for ( int x = 0 ; x < w ; ++x )
358 {
1cb97a54
DS
359 a = *source ++ ;
360 r = *source ++ ;
361 g = *source ++ ;
362 b = *source ++ ;
902725ee 363
b28aeea5
SC
364 *dest++ = 0 ;
365 *dest++ = r ;
366 *dest++ = g ;
367 *dest++ = b ;
902725ee 368
b28aeea5 369 if ( mask )
93a2b888
SC
370 {
371 *maskdest++ = 0xFF - *masksource++ ;
372 masksource++ ;
373 masksource++ ;
392e179f 374 masksource++ ;
93a2b888 375 }
b28aeea5
SC
376 else if ( hasAlpha )
377 *maskdest++ = a ;
378 else
379 *maskdest++ = 0xFF ;
380 }
381 }
902725ee 382
b28aeea5
SC
383 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
384 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
385
386 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
387 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
1cb97a54 388
b28aeea5
SC
389 HUnlock( data ) ;
390 HUnlock( maskdata ) ;
391 DisposeHandle( data ) ;
392 DisposeHandle( maskdata ) ;
393 }
394 else
395 {
b28aeea5
SC
396 PicHandle pic = GetPictHandle() ;
397 SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
398 }
902725ee 399
b28aeea5 400 // transform into IconRef
902725ee
WS
401
402 static int iconCounter = 2 ;
1cb97a54
DS
403
404 OSStatus err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ;
b28aeea5 405 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
1cb97a54 406
902725ee 407 // we have to retain a reference, as Unregister will decrement it
b28aeea5
SC
408 AcquireIconRef( m_iconRef ) ;
409 UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
410 DisposeHandle( (Handle) iconFamily ) ;
411 ++iconCounter ;
412 }
1cb97a54 413
b28aeea5
SC
414 return m_iconRef ;
415}
416
417PicHandle wxBitmapRefData::GetPictHandle()
418{
419 if ( m_pictHandle == NULL )
420 {
1cb97a54
DS
421 CGrafPtr origPort = NULL ;
422 GDHandle origDev = NULL ;
423 GWorldPtr wp = NULL ;
424 GWorldPtr mask = NULL ;
b28aeea5
SC
425 int height = GetHeight() ;
426 int width = GetWidth() ;
902725ee 427
b28aeea5 428 Rect rect = { 0 , 0 , height , width } ;
1cb97a54 429 RgnHandle clipRgn = NULL ;
b28aeea5
SC
430
431 GetGWorld( &origPort , &origDev ) ;
b28aeea5
SC
432 wp = GetHBITMAP( &mask ) ;
433
b28aeea5
SC
434 if ( mask )
435 {
436 GWorldPtr monoworld ;
437 clipRgn = NewRgn() ;
438 OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
439 verify_noerr(err) ;
440 LockPixels( GetGWorldPixMap( monoworld ) ) ;
441 LockPixels( GetGWorldPixMap( mask ) ) ;
442 SetGWorld( monoworld , NULL ) ;
1cb97a54
DS
443
444 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
445 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
b28aeea5
SC
446 RGBForeColor( &black ) ;
447 RGBBackColor( &white ) ;
1cb97a54 448
b28aeea5
SC
449 CopyBits(GetPortBitMapForCopyBits(mask),
450 GetPortBitMapForCopyBits(monoworld),
451 &rect,
452 &rect,
1cb97a54 453 srcCopy, NULL);
b28aeea5 454 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
1cb97a54 455
b28aeea5
SC
456 UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
457 UnlockPixels( GetGWorldPixMap( mask ) ) ;
458 DisposeGWorld( monoworld ) ;
459 }
460
461 SetGWorld( wp , NULL ) ;
462 Rect portRect ;
463 GetPortBounds( wp , &portRect ) ;
464 m_pictHandle = OpenPicture(&portRect);
902725ee 465
1cb97a54 466 if (m_pictHandle)
b28aeea5 467 {
1cb97a54
DS
468 RGBColor white = { 0xffff , 0xffff , 0xffff } ;
469 RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ;
470
b28aeea5
SC
471 RGBForeColor( &black ) ;
472 RGBBackColor( &white ) ;
473
474 if ( clipRgn )
475 SetClip( clipRgn ) ;
476
477 LockPixels( GetGWorldPixMap( wp ) ) ;
478 CopyBits(GetPortBitMapForCopyBits(wp),
479 GetPortBitMapForCopyBits(wp),
480 &portRect,
481 &portRect,
482 srcCopy,clipRgn);
483 UnlockPixels( GetGWorldPixMap( wp ) ) ;
484 ClosePicture();
485 }
1cb97a54 486
b28aeea5
SC
487 SetGWorld( origPort , origDev ) ;
488 if ( clipRgn )
489 DisposeRgn( clipRgn ) ;
490 }
1cb97a54 491
b28aeea5
SC
492 return m_pictHandle ;
493}
55e18dbe 494
30e77b5c 495#ifdef __WXMAC_OSX__
71cc158e 496void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
20b69855 497{
71cc158e 498 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
1cb97a54 499
71cc158e 500 wxASSERT( data == membuf->GetData() ) ;
1cb97a54 501
71cc158e 502 delete membuf ;
5fde6fcc
GD
503}
504
20b69855 505CGImageRef wxBitmapRefData::CGImageCreate() const
be295828 506{
20b69855
SC
507 wxASSERT( m_ok ) ;
508 wxASSERT( m_rawAccessCount >= 0 ) ;
509 CGImageRef image ;
510 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
be295828 511 {
20b69855
SC
512 size_t imageSize = m_width * m_height * 4 ;
513 void * dataBuffer = m_memBuf.GetData() ;
514 int w = m_width ;
515 int h = m_height ;
516 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
517 wxMemoryBuffer* membuf = NULL ;
902725ee 518
20b69855 519 if ( m_bitmapMask )
be295828 520 {
1cb97a54 521 alphaInfo = kCGImageAlphaFirst ;
20b69855
SC
522 membuf = new wxMemoryBuffer( imageSize ) ;
523 memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
524 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
525 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
526 unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
20b69855 527 for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
be295828 528 {
20b69855 529 unsigned char *sourcemask = sourcemaskstart ;
392e179f 530 for ( int x = 0 ; x < w ; ++x , sourcemask += 4 , destalpha += 4 )
be295828 531 {
93a2b888 532 *destalpha = 0xFF - *sourcemask ;
be295828
SC
533 }
534 }
535 }
1cb97a54 536 else
e40298d5 537 {
1cb97a54
DS
538 if ( m_hasAlpha )
539 {
20b69855 540#if wxMAC_USE_PREMULTIPLIED_ALPHA
1cb97a54 541 alphaInfo = kCGImageAlphaPremultipliedFirst ;
20b69855 542#else
1cb97a54 543 alphaInfo = kCGImageAlphaFirst ;
20b69855 544#endif
1cb97a54
DS
545 }
546
20b69855 547 membuf = new wxMemoryBuffer( m_memBuf ) ;
e40298d5 548 }
1cb97a54 549
20b69855 550 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
902725ee 551 CGDataProviderRef dataProvider =
1cb97a54
DS
552 CGDataProviderCreateWithData(
553 membuf , (const void *)membuf->GetData() , imageSize,
902725ee
WS
554 wxMacMemoryBufferReleaseProc );
555 image =
1cb97a54
DS
556 ::CGImageCreate(
557 w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
902725ee
WS
558 dataProvider, NULL , false , kCGRenderingIntentDefault );
559 CGDataProviderRelease( dataProvider);
20b69855
SC
560 }
561 else
562 {
563 image = m_cgImageRef ;
564 CGImageRetain( image ) ;
be295828 565 }
1cb97a54 566
20b69855
SC
567 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
568 {
569 // we keep it for later use
570 m_cgImageRef = image ;
571 CGImageRetain( image ) ;
902725ee 572 }
1cb97a54 573
20b69855 574 return image ;
be295828 575}
20b69855 576#endif
be295828 577
20b69855
SC
578GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
579{
580 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
581 if ( mask )
582 {
583 *mask = NULL ;
584 if ( m_bitmapMask )
1cb97a54 585 {
902725ee 586 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
1cb97a54 587 }
20b69855
SC
588 else if ( m_hasAlpha )
589 {
71cc158e 590#if !wxMAC_USE_CORE_GRAPHICS
20b69855
SC
591 if ( m_rawAccessCount > 0 )
592 UpdateAlphaMask() ;
71cc158e 593#else
1cb97a54
DS
594 // this structure is not kept in synch when using CG, so if something
595 // is really accessing the GrafPorts, we have to sync it
71cc158e
SC
596 UpdateAlphaMask() ;
597#endif
1cb97a54 598
20b69855
SC
599 *mask = m_hMaskBitmap ;
600 }
601 }
1cb97a54 602
20b69855 603 return m_hBitmap ;
e9576ca5
SC
604}
605
902725ee 606void wxBitmapRefData::UpdateAlphaMask() const
e9576ca5 607{
20b69855 608 if ( m_hasAlpha )
e40298d5 609 {
20b69855
SC
610 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
611 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
902725ee 612
20b69855
SC
613 int h = GetHeight() ;
614 int w = GetWidth() ;
902725ee 615
20b69855
SC
616 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
617 {
618 unsigned char* destalpha = destalphabase ;
1cb97a54
DS
619
620 for ( int x = 0 ; x < w ; ++x , sourcemask += 4 )
e40298d5 621 {
431c82e0
SC
622 // we must have 24 bit depth for non quartz smooth alpha
623 *destalpha++ = 255 ;
624 *destalpha++ = 255 - *sourcemask ;
625 *destalpha++ = 255 - *sourcemask ;
626 *destalpha++ = 255 - *sourcemask ;
e40298d5 627 }
20b69855
SC
628 }
629 }
630}
631
20b69855
SC
632void wxBitmapRefData::Free()
633{
634 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
635
abb4f9c9 636#ifdef __WXMAC_OSX__
20b69855
SC
637 if ( m_cgImageRef )
638 {
639 CGImageRelease( m_cgImageRef ) ;
640 m_cgImageRef = NULL ;
e40298d5 641 }
71cc158e 642#endif
1cb97a54 643
b28aeea5
SC
644 if ( m_iconRef )
645 {
646 ReleaseIconRef( m_iconRef ) ;
647 m_iconRef = NULL ;
648 }
1cb97a54 649
b28aeea5
SC
650 if ( m_pictHandle )
651 {
652 KillPicture( m_pictHandle ) ;
653 m_pictHandle = NULL ;
654 }
1cb97a54 655
20b69855
SC
656 if ( m_hBitmap )
657 {
658 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
659 m_hBitmap = NULL ;
660 }
1cb97a54 661
20b69855
SC
662 if ( m_hMaskBitmap )
663 {
664 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
665 m_hMaskBitmap = NULL ;
666 }
55e18dbe 667
20b69855 668 if (m_bitmapMask)
e40298d5 669 {
20b69855
SC
670 delete m_bitmapMask;
671 m_bitmapMask = NULL;
e40298d5 672 }
e9576ca5
SC
673}
674
85f296a3
SC
675wxBitmapRefData::~wxBitmapRefData()
676{
20b69855 677 Free() ;
85f296a3
SC
678}
679
90b959ae
SC
680bool wxBitmap::CopyFromIcon(const wxIcon& icon)
681{
1cb97a54 682 bool created = false ;
20b69855
SC
683 int w = icon.GetWidth() ;
684 int h = icon.GetHeight() ;
b28aeea5 685
20b69855
SC
686 Create( icon.GetWidth() , icon.GetHeight() ) ;
687
71cc158e 688 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
20b69855
SC
689 {
690 IconFamilyHandle iconFamily = NULL ;
1cb97a54
DS
691 Handle imagehandle = NewHandle( 0 ) ;
692 Handle maskhandle = NewHandle( 0 ) ;
902725ee 693
9cc62fc8
SC
694 OSType maskType = 0;
695 OSType dataType = 0;
902725ee 696 IconSelectorValue selector = 0 ;
1cb97a54
DS
697
698 switch (w)
71cc158e 699 {
1cb97a54
DS
700 case 128:
701 dataType = kThumbnail32BitData ;
702 maskType = kThumbnail8BitMask ;
703 selector = kSelectorAllAvailableData ;
704 break;
705
706 case 48:
707 dataType = kHuge32BitData ;
708 maskType = kHuge8BitMask ;
709 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
710 break;
711
712 case 32:
713 dataType = kLarge32BitData ;
714 maskType = kLarge8BitMask ;
715 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
716 break;
717
718 case 16:
719 dataType = kSmall32BitData ;
720 maskType = kSmall8BitMask ;
721 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
722 break;
723
724 default:
725 break;
71cc158e 726 }
71cc158e 727
1cb97a54 728 OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ;
71cc158e 729
1cb97a54
DS
730 err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ;
731 err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ;
b28aeea5
SC
732 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
733 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
902725ee 734
b28aeea5 735 if ( imagehandlesize != 0 && maskhandlesize != 0 )
20b69855 736 {
b28aeea5
SC
737 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
738 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
1cb97a54 739
b28aeea5 740 UseAlpha() ;
1cb97a54 741
b28aeea5
SC
742 unsigned char *source = (unsigned char *) *imagehandle ;
743 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
b28aeea5 744 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
1cb97a54 745
b28aeea5 746 for ( int y = 0 ; y < h ; ++y )
20b69855 747 {
b28aeea5
SC
748 for ( int x = 0 ; x < w ; ++x )
749 {
750 *destination++ = *sourcemask++ ;
751 source++ ;
752 *destination++ = *source++ ;
753 *destination++ = *source++ ;
754 *destination++ = *source++ ;
755 }
20b69855 756 }
1cb97a54 757
b28aeea5
SC
758 EndRawAccess() ;
759 DisposeHandle( imagehandle ) ;
760 DisposeHandle( maskhandle ) ;
95d8425f 761 created = true ;
20b69855 762 }
902725ee 763
1cb97a54 764 DisposeHandle( (Handle) iconFamily ) ;
20b69855 765 }
902725ee 766
b28aeea5 767 if ( !created )
902725ee 768 {
20b69855
SC
769 wxMemoryDC dc ;
770 dc.SelectObject( *this ) ;
771 dc.DrawIcon( icon , 0 , 0 ) ;
772 dc.SelectObject( wxNullBitmap ) ;
773 }
1cb97a54 774
125c389e 775 return true;
90b959ae
SC
776}
777
e9576ca5
SC
778wxBitmap::wxBitmap()
779{
e9576ca5
SC
780}
781
782wxBitmap::~wxBitmap()
783{
e9576ca5
SC
784}
785
786wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
787{
20b69855 788 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
e9576ca5 789
d2c6d549
GD
790 if ( no_bits == 1 )
791 {
973b0afb 792 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
1cb97a54 793 if ( the_width % (sizeof(unsigned char) * 8) )
973b0afb 794 linesize += sizeof(unsigned char);
1cb97a54 795
20b69855
SC
796 unsigned char* linestart = (unsigned char*) bits ;
797 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
1cb97a54 798
973b0afb
GD
799 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
800 {
1cb97a54
DS
801 int index, bit, mask;
802
973b0afb
GD
803 for ( int x = 0 ; x < the_width ; ++x )
804 {
1cb97a54
DS
805 index = x / 8 ;
806 bit = x % 8 ;
807 mask = 1 << bit ;
808
a395a624 809 if ( !(linestart[index] & mask ) )
973b0afb 810 {
20b69855
SC
811 *destination++ = 0xFF ;
812 *destination++ = 0 ;
813 *destination++ = 0 ;
814 *destination++ = 0 ;
973b0afb
GD
815 }
816 else
817 {
20b69855
SC
818 *destination++ = 0xFF ;
819 *destination++ = 0xFF ;
820 *destination++ = 0xFF ;
821 *destination++ = 0xFF ;
973b0afb
GD
822 }
823 }
824 }
1cb97a54 825
20b69855 826 EndRawAccess() ;
d2c6d549
GD
827 }
828 else
829 {
973b0afb 830 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
d2c6d549 831 }
e9576ca5
SC
832}
833
834wxBitmap::wxBitmap(int w, int h, int d)
835{
836 (void)Create(w, h, d);
e9576ca5
SC
837}
838
a8562f55 839wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
840{
841 (void) Create(data, type, width, height, depth);
e9576ca5
SC
842}
843
a8562f55 844wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
e9576ca5 845{
a8562f55 846 LoadFile(filename, type);
e9576ca5
SC
847}
848
20b69855
SC
849wxBitmap::wxBitmap(const char **bits)
850{
851 (void) CreateFromXpm(bits);
852}
853
854wxBitmap::wxBitmap(char **bits)
855{
856 (void) CreateFromXpm((const char **)bits);
857}
858
1cb97a54 859void * wxBitmap::GetRawAccess() const
20b69855
SC
860{
861 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
1cb97a54 862
20b69855
SC
863 return M_BITMAPDATA->GetRawAccess() ;
864}
865
1cb97a54 866void * wxBitmap::BeginRawAccess()
20b69855
SC
867{
868 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
1cb97a54 869
20b69855
SC
870 return M_BITMAPDATA->BeginRawAccess() ;
871}
872
873void wxBitmap::EndRawAccess()
874{
875 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
1cb97a54 876
20b69855
SC
877 M_BITMAPDATA->EndRawAccess() ;
878}
879
973b0afb 880bool wxBitmap::CreateFromXpm(const char **bits)
e9576ca5 881{
d45318b8 882#if wxUSE_IMAGE
637b7e4f 883 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
1cb97a54 884
973b0afb
GD
885 wxXPMDecoder decoder;
886 wxImage img = decoder.ReadData(bits);
637b7e4f 887 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
1cb97a54 888
55e18dbe 889 *this = wxBitmap(img);
1cb97a54 890
902725ee 891 return true;
d45318b8 892#else
1cb97a54 893
902725ee 894 return false;
d45318b8 895#endif
973b0afb
GD
896}
897
30e77b5c 898#ifdef __WXMAC_OSX__
20b69855 899WXCGIMAGEREF wxBitmap::CGImageCreate() const
03e11df5 900{
20b69855 901 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
1cb97a54 902
20b69855 903 return M_BITMAPDATA->CGImageCreate() ;
03e11df5 904}
20b69855 905#endif
03e11df5 906
5fde6fcc
GD
907wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
908{
20b69855 909 wxCHECK_MSG( Ok() &&
5fde6fcc
GD
910 (rect.x >= 0) && (rect.y >= 0) &&
911 (rect.x+rect.width <= GetWidth()) &&
912 (rect.y+rect.height <= GetHeight()),
913 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
914
20b69855
SC
915 wxBitmap ret( rect.width, rect.height, GetDepth() );
916 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
5fde6fcc 917
20b69855
SC
918 int sourcewidth = GetWidth() ;
919 int destwidth = rect.width ;
920 int destheight = rect.height ;
1cb97a54 921
20b69855 922 {
1cb97a54
DS
923 unsigned char *sourcedata = (unsigned char*) GetRawAccess() ;
924 unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ;
925 wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ;
926
20b69855
SC
927 int sourcelinesize = sourcewidth * 4 ;
928 int destlinesize = destwidth * 4 ;
929 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
930 unsigned char *dest = destdata ;
1cb97a54
DS
931
932 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
20b69855
SC
933 {
934 memcpy( dest , source , destlinesize ) ;
935 }
936 }
1cb97a54 937
20b69855 938 ret.EndRawAccess() ;
902725ee 939
20b69855
SC
940 if ( M_BITMAPDATA->m_bitmapMask )
941 {
942 wxMemoryBuffer maskbuf ;
392e179f 943 int rowBytes = ( destwidth * 4 + 3 ) & 0xFFFFFFC ;
20b69855 944 size_t maskbufsize = rowBytes * destheight ;
20b69855 945
7fe44dee 946 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
20b69855 947 int destlinesize = rowBytes ;
1cb97a54 948
20b69855 949 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
1cb97a54
DS
950 unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
951 wxASSERT( (source != NULL) && (destdata != NULL) ) ;
952
392e179f 953 source += rect.x * 4 + rect.y * sourcelinesize ;
20b69855
SC
954 unsigned char *dest = destdata ;
955
1cb97a54 956 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
20b69855 957 {
392e179f 958 memcpy( dest , source , destlinesize ) ;
20b69855 959 }
1cb97a54 960
20b69855
SC
961 maskbuf.UngetWriteBuf( maskbufsize ) ;
962 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
963 }
964 else if ( HasAlpha() )
965 ret.UseAlpha() ;
5fde6fcc 966
20b69855 967 return ret;
5fde6fcc
GD
968}
969
e9576ca5
SC
970bool wxBitmap::Create(int w, int h, int d)
971{
972 UnRef();
973
20b69855
SC
974 if ( d < 0 )
975 d = wxDisplayDepth() ;
2a391f87 976
20b69855 977 m_refData = new wxBitmapRefData( w , h , d );
55e18dbe 978
20b69855 979 return M_BITMAPDATA->Ok() ;
519cb848
SC
980}
981
a8562f55 982bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
e9576ca5
SC
983{
984 UnRef();
985
e9576ca5
SC
986 wxBitmapHandler *handler = FindHandler(type);
987
a8562f55
GD
988 if ( handler )
989 {
4e9ed364 990 m_refData = new wxBitmapRefData;
e9576ca5 991
a8562f55 992 return handler->LoadFile(this, filename, type, -1, -1);
e9576ca5 993 }
a8562f55
GD
994 else
995 {
d45318b8 996#if wxUSE_IMAGE
a8562f55 997 wxImage loadimage(filename, type);
1cb97a54
DS
998 if (loadimage.Ok())
999 {
a8562f55 1000 *this = loadimage;
1cb97a54 1001
a8562f55
GD
1002 return true;
1003 }
d45318b8 1004#endif
a8562f55 1005 }
1cb97a54 1006
427ff662 1007 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1cb97a54 1008
a8562f55 1009 return false;
e9576ca5
SC
1010}
1011
a8562f55 1012bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
1013{
1014 UnRef();
1015
1016 m_refData = new wxBitmapRefData;
1017
1018 wxBitmapHandler *handler = FindHandler(type);
1019
1cb97a54
DS
1020 if ( handler == NULL )
1021 {
427ff662 1022 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
e9576ca5 1023
902725ee 1024 return false;
e9576ca5
SC
1025 }
1026
1027 return handler->Create(this, data, type, width, height, depth);
1028}
1029
d45318b8
RN
1030#if wxUSE_IMAGE
1031
fec19ea9
VS
1032wxBitmap::wxBitmap(const wxImage& image, int depth)
1033{
637b7e4f 1034 wxCHECK_RET( image.Ok(), wxT("invalid image") );
fec19ea9 1035
fec19ea9
VS
1036 // width and height of the device-dependent bitmap
1037 int width = image.GetWidth();
1038 int height = image.GetHeight();
1039
d0ee33f5 1040 m_refData = new wxBitmapRefData( width , height , depth ) ;
55e18dbe 1041
20b69855 1042 // Create picture
fec19ea9 1043
20b69855 1044 bool hasAlpha = false ;
902725ee 1045
20b69855
SC
1046 if ( image.HasMask() )
1047 {
1048 // takes precedence, don't mix with alpha info
1049 }
1050 else
1051 {
1052 hasAlpha = image.HasAlpha() ;
1053 }
902725ee 1054
20b69855
SC
1055 if ( hasAlpha )
1056 UseAlpha() ;
902725ee 1057
20b69855 1058 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
fec19ea9 1059 register unsigned char* data = image.GetData();
20b69855 1060 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
1cb97a54 1061
fec19ea9
VS
1062 for (int y = 0; y < height; y++)
1063 {
1064 for (int x = 0; x < width; x++)
1065 {
20b69855
SC
1066 if ( hasAlpha )
1067 {
1068 const unsigned char a = *alpha++;
1069 *destination++ = a ;
1cb97a54 1070
20b69855 1071#if wxMAC_USE_PREMULTIPLIED_ALPHA
1cb97a54
DS
1072 *destination++ = ((*data++) * a + 127) / 255 ;
1073 *destination++ = ((*data++) * a + 127) / 255 ;
1074 *destination++ = ((*data++) * a + 127) / 255 ;
20b69855
SC
1075#else
1076 *destination++ = *data++ ;
1077 *destination++ = *data++ ;
1078 *destination++ = *data++ ;
1079#endif
1080 }
1081 else
1082 {
1083 *destination++ = 0xFF ;
1084 *destination++ = *data++ ;
1085 *destination++ = *data++ ;
1086 *destination++ = *data++ ;
1087 }
fec19ea9 1088 }
55e18dbe 1089 }
1cb97a54 1090
20b69855
SC
1091 EndRawAccess() ;
1092 if ( image.HasMask() )
20b69855 1093 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
fec19ea9
VS
1094}
1095
1096wxImage wxBitmap::ConvertToImage() const
1097{
1098 wxImage image;
55e18dbe 1099
fec19ea9
VS
1100 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1101
1102 // create an wxImage object
1103 int width = GetWidth();
1104 int height = GetHeight();
1105 image.Create( width, height );
1106
1107 unsigned char *data = image.GetData();
fec19ea9
VS
1108 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1109
20b69855
SC
1110 unsigned char* source = (unsigned char*) GetRawAccess() ;
1111
1112 bool hasAlpha = false ;
1113 bool hasMask = false ;
7e7f40ed 1114 int maskBytesPerRow = 0 ;
20b69855
SC
1115 unsigned char *alpha = NULL ;
1116 unsigned char *mask = NULL ;
1cb97a54 1117
20b69855 1118 if ( HasAlpha() )
20b69855 1119 hasAlpha = true ;
20b69855
SC
1120
1121 if ( GetMask() )
2b5f62a0 1122 {
20b69855
SC
1123 hasMask = true ;
1124 mask = (unsigned char*) GetMask()->GetRawAccess() ;
7e7f40ed 1125 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
e40298d5 1126 }
20b69855
SC
1127
1128 if ( hasAlpha )
e40298d5 1129 {
20b69855
SC
1130 image.SetAlpha() ;
1131 alpha = image.GetAlpha() ;
e40298d5 1132 }
1cb97a54 1133
20b69855 1134 int index = 0;
902725ee 1135
20b69855
SC
1136 // The following masking algorithm is the same as well in msw/gtk:
1137 // the colour used as transparent one in wxImage and the one it is
7fe44dee 1138 // replaced with when it actually occurs in the bitmap
20b69855
SC
1139 static const int MASK_RED = 1;
1140 static const int MASK_GREEN = 2;
1141 static const int MASK_BLUE = 3;
1142 static const int MASK_BLUE_REPLACEMENT = 2;
1143
7e7f40ed 1144 for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow )
fec19ea9 1145 {
7e7f40ed 1146 unsigned char * maskp = mask ;
1cb97a54
DS
1147 unsigned char a, r, g, b;
1148 long color;
1149
fec19ea9
VS
1150 for (int xx = 0; xx < width; xx++)
1151 {
1cb97a54 1152 color = *((long*) source) ;
f288c87b 1153#ifdef WORDS_BIGENDIAN
1cb97a54
DS
1154 a = ((color&0xFF000000) >> 24) ;
1155 r = ((color&0x00FF0000) >> 16) ;
1156 g = ((color&0x0000FF00) >> 8) ;
1157 b = (color&0x000000FF);
f288c87b
SC
1158#else
1159 b = ((color&0xFF000000) >> 24) ;
1160 g = ((color&0x00FF0000) >> 16) ;
1161 r = ((color&0x0000FF00) >> 8) ;
1162 a = (color&0x000000FF);
1163#endif
20b69855 1164 if ( hasMask )
55e18dbe 1165 {
93a2b888 1166 if ( *maskp++ == 0xFF )
e40298d5 1167 {
e59ea2c5
SC
1168 r = MASK_RED ;
1169 g = MASK_GREEN ;
1170 b = MASK_BLUE ;
e40298d5 1171 }
e59ea2c5
SC
1172 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1173 b = MASK_BLUE_REPLACEMENT ;
7fe44dee 1174
93a2b888
SC
1175 maskp++ ;
1176 maskp++ ;
392e179f 1177 maskp++ ;
fec19ea9 1178 }
20b69855
SC
1179 else if ( hasAlpha )
1180 *alpha++ = a ;
1181
1182 data[index ] = r ;
1183 data[index + 1] = g ;
1184 data[index + 2] = b ;
1cb97a54 1185
fec19ea9 1186 index += 3;
20b69855 1187 source += 4 ;
fec19ea9
VS
1188 }
1189 }
1cb97a54 1190
20b69855
SC
1191 if ( hasMask )
1192 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1cb97a54 1193
fec19ea9
VS
1194 return image;
1195}
1196
d45318b8 1197#endif //wxUSE_IMAGE
fec19ea9 1198
7fe44dee
DS
1199bool wxBitmap::SaveFile( const wxString& filename,
1200 wxBitmapType type, const wxPalette *palette ) const
e9576ca5 1201{
902725ee 1202 bool success = false;
e9576ca5
SC
1203 wxBitmapHandler *handler = FindHandler(type);
1204
a8562f55
GD
1205 if ( handler )
1206 {
902725ee 1207 success = handler->SaveFile(this, filename, type, palette);
a8562f55
GD
1208 }
1209 else
1210 {
d45318b8 1211#if wxUSE_IMAGE
a8562f55 1212 wxImage image = ConvertToImage();
902725ee
WS
1213 success = image.SaveFile(filename, type);
1214#else
1215 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
d45318b8 1216#endif
a8562f55 1217 }
55e18dbe 1218
902725ee 1219 return success;
e9576ca5
SC
1220}
1221
5fde6fcc
GD
1222bool wxBitmap::Ok() const
1223{
20b69855 1224 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
5fde6fcc
GD
1225}
1226
1227int wxBitmap::GetHeight() const
1228{
1229 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1230
20b69855 1231 return M_BITMAPDATA->GetHeight();
5fde6fcc
GD
1232}
1233
1234int wxBitmap::GetWidth() const
1235{
1236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1237
20b69855 1238 return M_BITMAPDATA->GetWidth() ;
5fde6fcc
GD
1239}
1240
1241int wxBitmap::GetDepth() const
1242{
1243 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1244
20b69855 1245 return M_BITMAPDATA->GetDepth();
5fde6fcc
GD
1246}
1247
179e085f 1248#if WXWIN_COMPATIBILITY_2_4
5fde6fcc
GD
1249int wxBitmap::GetQuality() const
1250{
20b69855 1251 return 0;
5fde6fcc
GD
1252}
1253
1cb97a54
DS
1254void wxBitmap::SetQuality(int WXUNUSED(quality))
1255{
1256}
179e085f
RN
1257#endif
1258
5fde6fcc
GD
1259wxMask *wxBitmap::GetMask() const
1260{
1261 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1262
1263 return M_BITMAPDATA->m_bitmapMask;
1264}
1265
20b69855
SC
1266bool wxBitmap::HasAlpha() const
1267{
1268 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1269
1270 return M_BITMAPDATA->HasAlpha() ;
1271}
1272
e9576ca5
SC
1273void wxBitmap::SetWidth(int w)
1274{
1275 if (!M_BITMAPDATA)
1276 m_refData = new wxBitmapRefData;
1277
20b69855 1278 M_BITMAPDATA->SetWidth(w);
e9576ca5
SC
1279}
1280
1281void wxBitmap::SetHeight(int h)
1282{
1283 if (!M_BITMAPDATA)
1284 m_refData = new wxBitmapRefData;
1285
20b69855 1286 M_BITMAPDATA->SetHeight(h);
e9576ca5
SC
1287}
1288
1289void wxBitmap::SetDepth(int d)
1290{
1291 if (!M_BITMAPDATA)
1292 m_refData = new wxBitmapRefData;
1293
20b69855 1294 M_BITMAPDATA->SetDepth(d);
e9576ca5
SC
1295}
1296
e9576ca5
SC
1297void wxBitmap::SetOk(bool isOk)
1298{
1299 if (!M_BITMAPDATA)
1300 m_refData = new wxBitmapRefData;
1301
20b69855 1302 M_BITMAPDATA->SetOk(isOk);
e9576ca5
SC
1303}
1304
a6de86fa 1305#if wxUSE_PALETTE
5fde6fcc
GD
1306wxPalette *wxBitmap::GetPalette() const
1307{
1308 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1309
1310 return &M_BITMAPDATA->m_bitmapPalette;
1311}
1312
e9576ca5
SC
1313void wxBitmap::SetPalette(const wxPalette& palette)
1314{
1315 if (!M_BITMAPDATA)
1316 m_refData = new wxBitmapRefData;
1317
1318 M_BITMAPDATA->m_bitmapPalette = palette ;
1319}
a6de86fa 1320#endif // wxUSE_PALETTE
e9576ca5
SC
1321
1322void wxBitmap::SetMask(wxMask *mask)
1323{
1324 if (!M_BITMAPDATA)
1325 m_refData = new wxBitmapRefData;
1326
a8562f55 1327 // Remove existing mask if there is one.
1e74d03b 1328 delete M_BITMAPDATA->m_bitmapMask;
a8562f55 1329
e9576ca5
SC
1330 M_BITMAPDATA->m_bitmapMask = mask ;
1331}
1332
20b69855 1333WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
5fde6fcc 1334{
20b69855 1335 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
5fde6fcc
GD
1336}
1337
20b69855
SC
1338// ----------------------------------------------------------------------------
1339// wxMask
1340// ----------------------------------------------------------------------------
e9576ca5
SC
1341
1342wxMask::wxMask()
1343{
20b69855 1344 Init() ;
e9576ca5
SC
1345}
1346
1347// Construct a mask from a bitmap and a colour indicating
1348// the transparent area
7fe44dee 1349wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
e9576ca5 1350{
20b69855 1351 Init() ;
7fe44dee 1352 Create( bitmap, colour );
e9576ca5
SC
1353}
1354
20b69855 1355// Construct a mask from a mono bitmap (copies the bitmap).
7fe44dee 1356wxMask::wxMask( const wxBitmap& bitmap )
e9576ca5 1357{
20b69855 1358 Init() ;
7fe44dee 1359 Create( bitmap );
e9576ca5
SC
1360}
1361
1362// Construct a mask from a mono bitmap (copies the bitmap).
392e179f 1363
1cb97a54 1364wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
e9576ca5 1365{
20b69855 1366 Init() ;
7fe44dee 1367 Create( data, width , height , bytesPerRow );
e9576ca5
SC
1368}
1369
1370wxMask::~wxMask()
1371{
4e9ed364
RR
1372 if ( m_maskBitmap )
1373 {
7fe44dee 1374 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
4e9ed364
RR
1375 m_maskBitmap = NULL ;
1376 }
e9576ca5
SC
1377}
1378
902725ee 1379void wxMask::Init()
e9576ca5 1380{
20b69855 1381 m_width = m_height = m_bytesPerRow = 0 ;
20b69855 1382 m_maskBitmap = NULL ;
20b69855 1383}
5fde6fcc 1384
20b69855
SC
1385void *wxMask::GetRawAccess() const
1386{
1387 return m_memBuf.GetData() ;
1388}
5fde6fcc 1389
7fe44dee
DS
1390// The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1391// bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
431c82e0 1392
902725ee 1393void wxMask::RealizeNative()
20b69855 1394{
20b69855
SC
1395 if ( m_maskBitmap )
1396 {
7fe44dee 1397 DisposeGWorld( (GWorldPtr)m_maskBitmap ) ;
20b69855
SC
1398 m_maskBitmap = NULL ;
1399 }
1cb97a54 1400
20b69855 1401 Rect rect = { 0 , 0 , m_height , m_width } ;
93a2b888 1402
7fe44dee 1403 OSStatus err = NewGWorldFromPtr(
392e179f 1404 (GWorldPtr*) &m_maskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
7fe44dee
DS
1405 (char*) m_memBuf.GetData() , m_bytesPerRow ) ;
1406 verify_noerr( err ) ;
20b69855 1407}
5fde6fcc 1408
20b69855 1409// Create a mask from a mono bitmap (copies the bitmap).
392e179f 1410
20b69855
SC
1411bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1412{
1413 m_memBuf = data ;
1414 m_width = width ;
1415 m_height = height ;
1416 m_bytesPerRow = bytesPerRow ;
1cb97a54 1417
20b69855 1418 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1cb97a54 1419
20b69855 1420 RealizeNative() ;
1cb97a54 1421
20b69855 1422 return true ;
e9576ca5
SC
1423}
1424
20b69855
SC
1425// Create a mask from a mono bitmap (copies the bitmap).
1426bool wxMask::Create(const wxBitmap& bitmap)
e9576ca5 1427{
20b69855
SC
1428 m_width = bitmap.GetWidth() ;
1429 m_height = bitmap.GetHeight() ;
392e179f 1430 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
1cb97a54 1431
20b69855
SC
1432 size_t size = m_bytesPerRow * m_height ;
1433 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1cb97a54
DS
1434 wxASSERT( destdatabase != NULL ) ;
1435
20b69855
SC
1436 memset( destdatabase , 0 , size ) ;
1437 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1cb97a54 1438
20b69855
SC
1439 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1440 {
1cb97a54
DS
1441 unsigned char *destdata = destdatabase ;
1442 unsigned char r, g, b;
1443
1444 for ( int x = 0 ; x < m_width ; ++x )
20b69855
SC
1445 {
1446 srcdata++ ;
1cb97a54
DS
1447 r = *srcdata++ ;
1448 g = *srcdata++ ;
1449 b = *srcdata++ ;
1450
20b69855 1451 if ( ( r + g + b ) > 0x10 )
93a2b888
SC
1452 {
1453 *destdata++ = 0xFF ;
1454 *destdata++ = 0xFF ;
20b69855 1455 *destdata++ = 0xFF ;
392e179f 1456 *destdata++ = 0xFF ;
93a2b888
SC
1457 }
1458 else
1459 {
1460 *destdata++ = 0x00 ;
1461 *destdata++ = 0x00 ;
1462 *destdata++ = 0x00 ;
392e179f 1463 *destdata++ = 0x00 ;
93a2b888 1464 }
20b69855
SC
1465 }
1466 }
1cb97a54 1467
20b69855
SC
1468 m_memBuf.UngetWriteBuf( size ) ;
1469 RealizeNative() ;
1cb97a54 1470
902725ee 1471 return true;
e9576ca5
SC
1472}
1473
1474// Create a mask from a bitmap and a colour indicating
1475// the transparent area
1476bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1477{
20b69855
SC
1478 m_width = bitmap.GetWidth() ;
1479 m_height = bitmap.GetHeight() ;
392e179f 1480 m_bytesPerRow = ( m_width * 4 + 3 ) & 0xFFFFFFC ;
20b69855 1481
1cb97a54 1482 size_t size = m_bytesPerRow * m_height ;
20b69855 1483 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1cb97a54
DS
1484 wxASSERT( destdatabase != NULL ) ;
1485
20b69855
SC
1486 memset( destdatabase , 0 , size ) ;
1487 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1cb97a54 1488
20b69855 1489 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
8208e181 1490 {
1cb97a54
DS
1491 unsigned char *destdata = destdatabase ;
1492 unsigned char r, g, b;
1493
1494 for ( int x = 0 ; x < m_width ; ++x )
55e18dbe 1495 {
20b69855 1496 srcdata++ ;
1cb97a54
DS
1497 r = *srcdata++ ;
1498 g = *srcdata++ ;
1499 b = *srcdata++ ;
1500
7fe44dee 1501 if ( colour == wxColour( r , g , b ) )
93a2b888 1502 {
20b69855 1503 *destdata++ = 0xFF ;
93a2b888
SC
1504 *destdata++ = 0xFF ;
1505 *destdata++ = 0xFF ;
392e179f 1506 *destdata++ = 0xFF ;
93a2b888
SC
1507 }
1508 else
1509 {
1510 *destdata++ = 0x00 ;
1511 *destdata++ = 0x00 ;
1512 *destdata++ = 0x00 ;
392e179f 1513 *destdata++ = 0x00 ;
93a2b888 1514 }
8208e181
SC
1515 }
1516 }
1cb97a54 1517
20b69855
SC
1518 m_memBuf.UngetWriteBuf( size ) ;
1519 RealizeNative() ;
1cb97a54 1520
902725ee 1521 return true;
e9576ca5
SC
1522}
1523
20b69855 1524WXHBITMAP wxMask::GetHBITMAP() const
5fde6fcc 1525{
20b69855 1526 return m_maskBitmap ;
5fde6fcc
GD
1527}
1528
20b69855
SC
1529// ----------------------------------------------------------------------------
1530// wxBitmapHandler
1531// ----------------------------------------------------------------------------
e9576ca5 1532
be52b341
GD
1533wxBitmapHandler::~wxBitmapHandler()
1534{
1535}
1536
e9576ca5
SC
1537bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1538{
902725ee 1539 return false;
e9576ca5
SC
1540}
1541
a8562f55 1542bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1543 int desiredWidth, int desiredHeight)
1544{
902725ee 1545 return false;
e9576ca5
SC
1546}
1547
a8562f55 1548bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5 1549{
902725ee 1550 return false;
e9576ca5
SC
1551}
1552
20b69855
SC
1553// ----------------------------------------------------------------------------
1554// Standard Handlers
1555// ----------------------------------------------------------------------------
e9576ca5 1556
519cb848
SC
1557class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1558{
1559 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1cb97a54 1560
519cb848
SC
1561public:
1562 inline wxPICTResourceHandler()
1563 {
3bf2bdfb
GD
1564 SetName(wxT("Macintosh Pict resource"));
1565 SetExtension(wxEmptyString);
1566 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
519cb848
SC
1567 };
1568
1569 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1570 int desiredWidth, int desiredHeight);
1571};
7fe44dee 1572
519cb848
SC
1573IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1574
179e085f 1575
1cb97a54 1576bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
519cb848
SC
1577 int desiredWidth, int desiredHeight)
1578{
179e085f 1579#if wxUSE_METAFILE
4e9ed364 1580 Str255 theName ;
427ff662 1581 wxMacStringToPascal( name , theName ) ;
55e18dbe 1582
4e9ed364
RR
1583 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1584 if ( thePict )
1585 {
20b69855 1586 wxMetafile mf ;
7fe44dee 1587
1cb97a54 1588 mf.SetHMETAFILE( (WXHMETAFILE) thePict ) ;
20b69855
SC
1589 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1590 wxMemoryDC dc ;
1591 dc.SelectObject( *bitmap ) ;
1592 mf.Play( &dc ) ;
1593 dc.SelectObject( wxNullBitmap ) ;
1cb97a54 1594
902725ee 1595 return true ;
4e9ed364 1596 }
7fe44dee 1597#endif
1cb97a54 1598
902725ee 1599 return false ;
519cb848
SC
1600}
1601
e9576ca5
SC
1602void wxBitmap::InitStandardHandlers()
1603{
1cb97a54
DS
1604 AddHandler( new wxPICTResourceHandler ) ;
1605 AddHandler( new wxICONResourceHandler ) ;
e9576ca5 1606}
55e18dbe
VZ
1607
1608// ----------------------------------------------------------------------------
1609// raw bitmap access support
1610// ----------------------------------------------------------------------------
1611
1612void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1613{
1614 if ( !Ok() )
55e18dbe
VZ
1615 // no bitmap, no data (raw or otherwise)
1616 return NULL;
55e18dbe 1617
20b69855
SC
1618 data.m_width = GetWidth() ;
1619 data.m_height = GetHeight() ;
1620 data.m_stride = GetWidth() * 4 ;
1cb97a54 1621
6945b587 1622 return BeginRawAccess() ;
55e18dbe
VZ
1623}
1624
1e74d03b 1625void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
55e18dbe 1626{
6945b587 1627 EndRawAccess() ;
55e18dbe
VZ
1628}
1629
1630void wxBitmap::UseAlpha()
1631{
1cb97a54
DS
1632 // remember that we are using alpha channel:
1633 // we'll need to create a proper mask in UngetRawData()
20b69855 1634 M_BITMAPDATA->UseAlpha( true );
55e18dbe 1635}