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