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