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