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