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