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