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