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