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