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