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