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