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