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