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