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