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