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