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