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