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