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