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