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