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