]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/bitmap.cpp
no 10.4 support anymore
[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 == kCGImageAlphaNoneSkipLast || 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 KillPicture( m_pictHandle ) ;
751 m_pictHandle = NULL ;
752 }
753 #endif
754 #endif
755 if ( m_hBitmap )
756 {
757 CGContextRelease(m_hBitmap);
758 m_hBitmap = NULL ;
759 }
760
761 wxDELETE(m_bitmapMask);
762 }
763
764 wxBitmapRefData::~wxBitmapRefData()
765 {
766 Free() ;
767 }
768
769
770
771 // ----------------------------------------------------------------------------
772 // wxBitmap
773 // ----------------------------------------------------------------------------
774
775 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
776 {
777 bool created = false ;
778 int w = icon.GetWidth() ;
779 int h = icon.GetHeight() ;
780
781 Create( w , h ) ;
782 #ifdef __WXOSX_CARBON__
783 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
784 {
785 IconFamilyHandle iconFamily = NULL ;
786 Handle imagehandle = NewHandle( 0 ) ;
787 Handle maskhandle = NewHandle( 0 ) ;
788
789 OSType maskType = 0;
790 OSType dataType = 0;
791 IconSelectorValue selector = 0 ;
792
793 switch (w)
794 {
795 case 128:
796 dataType = kThumbnail32BitData ;
797 maskType = kThumbnail8BitMask ;
798 selector = kSelectorAllAvailableData ;
799 break;
800
801 case 48:
802 dataType = kHuge32BitData ;
803 maskType = kHuge8BitMask ;
804 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
805 break;
806
807 case 32:
808 dataType = kLarge32BitData ;
809 maskType = kLarge8BitMask ;
810 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
811 break;
812
813 case 16:
814 dataType = kSmall32BitData ;
815 maskType = kSmall8BitMask ;
816 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
817 break;
818
819 default:
820 break;
821 }
822
823 OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ;
824
825 err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ;
826 err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ;
827 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
828 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
829
830 if ( imagehandlesize != 0 && maskhandlesize != 0 )
831 {
832 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
833 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
834
835 UseAlpha() ;
836
837 unsigned char *source = (unsigned char *) *imagehandle ;
838 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
839 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
840
841 for ( int y = 0 ; y < h ; ++y )
842 {
843 for ( int x = 0 ; x < w ; ++x )
844 {
845 unsigned char a = *sourcemask++;
846 *destination++ = a;
847 source++ ;
848 #if wxOSX_USE_PREMULTIPLIED_ALPHA
849 *destination++ = ( (*source++) * a + 127 ) / 255;
850 *destination++ = ( (*source++) * a + 127 ) / 255;
851 *destination++ = ( (*source++) * a + 127 ) / 255;
852 #else
853 *destination++ = *source++ ;
854 *destination++ = *source++ ;
855 *destination++ = *source++ ;
856 #endif
857 }
858 }
859
860 EndRawAccess() ;
861 DisposeHandle( imagehandle ) ;
862 DisposeHandle( maskhandle ) ;
863 created = true ;
864 }
865
866 DisposeHandle( (Handle) iconFamily ) ;
867 }
868 #endif
869 if ( !created )
870 {
871 wxMemoryDC dc ;
872 dc.SelectObject( *this ) ;
873 dc.DrawIcon( icon , 0 , 0 ) ;
874 dc.SelectObject( wxNullBitmap ) ;
875 }
876
877 return true;
878 }
879
880 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
881 {
882 wxBitmapRefData* bitmapRefData;
883
884 m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
885
886 if (bitmapRefData->IsOk())
887 {
888 if ( no_bits == 1 )
889 {
890 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
891 if ( the_width % (sizeof(unsigned char) * 8) )
892 linesize += sizeof(unsigned char);
893
894 unsigned char* linestart = (unsigned char*) bits ;
895 unsigned char* destptr = (unsigned char*) BeginRawAccess() ;
896
897 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() )
898 {
899 unsigned char* destination = destptr;
900 int index, bit, mask;
901
902 for ( int x = 0 ; x < the_width ; ++x )
903 {
904 index = x / 8 ;
905 bit = x % 8 ;
906 mask = 1 << bit ;
907
908 if ( linestart[index] & mask )
909 {
910 *destination++ = 0xFF ;
911 *destination++ = 0 ;
912 *destination++ = 0 ;
913 *destination++ = 0 ;
914 }
915 else
916 {
917 *destination++ = 0xFF ;
918 *destination++ = 0xFF ;
919 *destination++ = 0xFF ;
920 *destination++ = 0xFF ;
921 }
922 }
923 }
924
925 EndRawAccess() ;
926 }
927 else
928 {
929 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
930 }
931 } /* bitmapRefData->IsOk() */
932 }
933
934 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
935 {
936 (void) Create(data, type, width, height, depth);
937 }
938
939 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
940 {
941 LoadFile(filename, type);
942 }
943
944 wxBitmap::wxBitmap(CGImageRef image)
945 {
946 (void) Create(image);
947 }
948
949 wxGDIRefData* wxBitmap::CreateGDIRefData() const
950 {
951 return new wxBitmapRefData;
952 }
953
954 wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
955 {
956 return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data));
957 }
958
959 void * wxBitmap::GetRawAccess() const
960 {
961 wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ;
962
963 return M_BITMAPDATA->GetRawAccess() ;
964 }
965
966 void * wxBitmap::BeginRawAccess()
967 {
968 wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ;
969
970 return M_BITMAPDATA->BeginRawAccess() ;
971 }
972
973 void wxBitmap::EndRawAccess()
974 {
975 wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
976
977 M_BITMAPDATA->EndRawAccess() ;
978 }
979
980 CGImageRef wxBitmap::CreateCGImage() const
981 {
982 wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ;
983
984 return M_BITMAPDATA->CreateCGImage() ;
985 }
986
987 #ifndef __WXOSX_IPHONE__
988 IconRef wxBitmap::GetIconRef() const
989 {
990 wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ;
991
992 return M_BITMAPDATA->GetIconRef() ;
993 }
994
995 IconRef wxBitmap::CreateIconRef() const
996 {
997 IconRef icon = GetIconRef();
998 verify_noerr( AcquireIconRef(icon) );
999 return icon;
1000 }
1001 #endif
1002
1003 #if wxOSX_USE_COCOA
1004
1005 WX_NSImage wxBitmap::GetNSImage() const
1006 {
1007 wxCFRef< CGImageRef > cgimage(CreateCGImage());
1008 return wxOSXGetNSImageFromCGImage( cgimage );
1009 }
1010
1011 #endif
1012
1013 #if wxOSX_USE_IPHONE
1014
1015 WX_UIImage wxBitmap::GetUIImage() const
1016 {
1017 wxCFRef< CGImageRef > cgimage(CreateCGImage());
1018 return wxOSXGetUIImageFromCGImage( cgimage );
1019 }
1020
1021 #endif
1022 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
1023 {
1024 wxCHECK_MSG( IsOk() &&
1025 (rect.x >= 0) && (rect.y >= 0) &&
1026 (rect.x+rect.width <= GetWidth()) &&
1027 (rect.y+rect.height <= GetHeight()),
1028 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1029
1030 wxBitmap ret( rect.width, rect.height, GetDepth() );
1031 wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") );
1032
1033 int destwidth = rect.width ;
1034 int destheight = rect.height ;
1035
1036 {
1037 unsigned char *sourcedata = (unsigned char*) GetRawAccess() ;
1038 unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ;
1039 wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ;
1040
1041 int sourcelinesize = GetBitmapData()->GetBytesPerRow() ;
1042 int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ;
1043 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
1044 unsigned char *dest = destdata ;
1045
1046 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
1047 {
1048 memcpy( dest , source , destlinesize ) ;
1049 }
1050 }
1051
1052 ret.EndRawAccess() ;
1053
1054 if ( M_BITMAPDATA->m_bitmapMask )
1055 {
1056 wxMemoryBuffer maskbuf ;
1057 int rowBytes = GetBestBytesPerRow( destwidth * kMaskBytesPerPixel );
1058 size_t maskbufsize = rowBytes * destheight ;
1059
1060 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
1061 int destlinesize = rowBytes ;
1062
1063 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
1064 unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
1065 wxASSERT( (source != NULL) && (destdata != NULL) ) ;
1066
1067 source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ;
1068 unsigned char *dest = destdata ;
1069
1070 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
1071 {
1072 memcpy( dest , source , destlinesize ) ;
1073 }
1074
1075 maskbuf.UngetWriteBuf( maskbufsize ) ;
1076 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
1077 }
1078 else if ( HasAlpha() )
1079 ret.UseAlpha() ;
1080
1081 return ret;
1082 }
1083
1084 bool wxBitmap::Create(int w, int h, int d)
1085 {
1086 UnRef();
1087
1088 if ( d < 0 )
1089 d = wxDisplayDepth() ;
1090
1091 m_refData = new wxBitmapRefData( w , h , d );
1092
1093 return M_BITMAPDATA->IsOk() ;
1094 }
1095
1096
1097 bool wxBitmap::Create(CGImageRef image)
1098 {
1099 UnRef();
1100
1101 m_refData = new wxBitmapRefData( image );
1102
1103 return M_BITMAPDATA->IsOk() ;
1104 }
1105
1106 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
1107 {
1108 UnRef();
1109
1110 wxBitmapHandler *handler = FindHandler(type);
1111
1112 if ( handler )
1113 {
1114 m_refData = new wxBitmapRefData;
1115
1116 return handler->LoadFile(this, filename, type, -1, -1);
1117 }
1118 else
1119 {
1120 #if wxUSE_IMAGE
1121 wxImage loadimage(filename, type);
1122 if (loadimage.IsOk())
1123 {
1124 *this = loadimage;
1125
1126 return true;
1127 }
1128 #endif
1129 }
1130
1131 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1132
1133 return false;
1134 }
1135
1136 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
1137 {
1138 UnRef();
1139
1140 m_refData = new wxBitmapRefData;
1141
1142 wxBitmapHandler *handler = FindHandler(type);
1143
1144 if ( handler == NULL )
1145 {
1146 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1147
1148 return false;
1149 }
1150
1151 return handler->Create(this, data, type, width, height, depth);
1152 }
1153
1154 #if wxUSE_IMAGE
1155
1156 wxBitmap::wxBitmap(const wxImage& image, int depth)
1157 {
1158 wxCHECK_RET( image.IsOk(), wxT("invalid image") );
1159
1160 // width and height of the device-dependent bitmap
1161 int width = image.GetWidth();
1162 int height = image.GetHeight();
1163
1164 wxBitmapRefData* bitmapRefData;
1165
1166 m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ;
1167
1168 if ( bitmapRefData->IsOk())
1169 {
1170 // Create picture
1171
1172 bool hasAlpha = false ;
1173
1174 if ( image.HasMask() )
1175 {
1176 // takes precedence, don't mix with alpha info
1177 }
1178 else
1179 {
1180 hasAlpha = image.HasAlpha() ;
1181 }
1182
1183 if ( hasAlpha )
1184 UseAlpha() ;
1185
1186 unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ;
1187 register unsigned char* data = image.GetData();
1188 if ( destinationstart != NULL && data != NULL )
1189 {
1190 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
1191 for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++)
1192 {
1193 unsigned char * destination = destinationstart;
1194 for (int x = 0; x < width; x++)
1195 {
1196 if ( hasAlpha )
1197 {
1198 const unsigned char a = *alpha++;
1199 *destination++ = a ;
1200
1201 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1202 *destination++ = ((*data++) * a + 127) / 255 ;
1203 *destination++ = ((*data++) * a + 127) / 255 ;
1204 *destination++ = ((*data++) * a + 127) / 255 ;
1205 #else
1206 *destination++ = *data++ ;
1207 *destination++ = *data++ ;
1208 *destination++ = *data++ ;
1209 #endif
1210 }
1211 else
1212 {
1213 *destination++ = 0xFF ;
1214 *destination++ = *data++ ;
1215 *destination++ = *data++ ;
1216 *destination++ = *data++ ;
1217 }
1218 }
1219 }
1220
1221 EndRawAccess() ;
1222 }
1223 if ( image.HasMask() )
1224 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
1225 } /* bitmapRefData->IsOk() */
1226 }
1227
1228 wxImage wxBitmap::ConvertToImage() const
1229 {
1230 wxImage image;
1231
1232 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
1233
1234 // create an wxImage object
1235 int width = GetWidth();
1236 int height = GetHeight();
1237 image.Create( width, height );
1238
1239 unsigned char *data = image.GetData();
1240 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1241
1242 unsigned char* sourcestart = (unsigned char*) GetRawAccess() ;
1243
1244 bool hasAlpha = false ;
1245 bool hasMask = false ;
1246 int maskBytesPerRow = 0 ;
1247 unsigned char *alpha = NULL ;
1248 unsigned char *mask = NULL ;
1249
1250 if ( HasAlpha() )
1251 hasAlpha = true ;
1252
1253 if ( GetMask() )
1254 {
1255 hasMask = true ;
1256 mask = (unsigned char*) GetMask()->GetRawAccess() ;
1257 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
1258 }
1259
1260 if ( hasAlpha )
1261 {
1262 image.SetAlpha() ;
1263 alpha = image.GetAlpha() ;
1264 }
1265
1266 int index = 0;
1267
1268 // The following masking algorithm is the same as well in msw/gtk:
1269 // the colour used as transparent one in wxImage and the one it is
1270 // replaced with when it actually occurs in the bitmap
1271 static const int MASK_RED = 1;
1272 static const int MASK_GREEN = 2;
1273 static const int MASK_BLUE = 3;
1274 static const int MASK_BLUE_REPLACEMENT = 2;
1275
1276 for (int yy = 0; yy < height; yy++ , sourcestart += M_BITMAPDATA->GetBytesPerRow() , mask += maskBytesPerRow )
1277 {
1278 unsigned char * maskp = mask ;
1279 unsigned char * source = sourcestart;
1280 unsigned char a, r, g, b;
1281 long color;
1282
1283 for (int xx = 0; xx < width; xx++)
1284 {
1285 color = *((long*) source) ;
1286 #ifdef WORDS_BIGENDIAN
1287 a = ((color&0xFF000000) >> 24) ;
1288 r = ((color&0x00FF0000) >> 16) ;
1289 g = ((color&0x0000FF00) >> 8) ;
1290 b = (color&0x000000FF);
1291 #else
1292 b = ((color&0xFF000000) >> 24) ;
1293 g = ((color&0x00FF0000) >> 16) ;
1294 r = ((color&0x0000FF00) >> 8) ;
1295 a = (color&0x000000FF);
1296 #endif
1297 if ( hasMask )
1298 {
1299 if ( *maskp++ == 0xFF )
1300 {
1301 r = MASK_RED ;
1302 g = MASK_GREEN ;
1303 b = MASK_BLUE ;
1304 }
1305 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1306 b = MASK_BLUE_REPLACEMENT ;
1307 }
1308 else if ( hasAlpha )
1309 {
1310 *alpha++ = a ;
1311 #if wxOSX_USE_PREMULTIPLIED_ALPHA
1312 // this must be non-premultiplied data
1313 if ( a != 0xFF && a!= 0 )
1314 {
1315 r = r * 255 / a;
1316 g = g * 255 / a;
1317 b = b * 255 / a;
1318 }
1319 #endif
1320 }
1321
1322 data[index ] = r ;
1323 data[index + 1] = g ;
1324 data[index + 2] = b ;
1325
1326 index += 3;
1327 source += 4 ;
1328 }
1329 }
1330
1331 if ( hasMask )
1332 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1333
1334 return image;
1335 }
1336
1337 #endif //wxUSE_IMAGE
1338
1339 bool wxBitmap::SaveFile( const wxString& filename,
1340 wxBitmapType type, const wxPalette *palette ) const
1341 {
1342 bool success = false;
1343 wxBitmapHandler *handler = FindHandler(type);
1344
1345 if ( handler )
1346 {
1347 success = handler->SaveFile(this, filename, type, palette);
1348 }
1349 else
1350 {
1351 #if wxUSE_IMAGE
1352 wxImage image = ConvertToImage();
1353 success = image.SaveFile(filename, type);
1354 #else
1355 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1356 #endif
1357 }
1358
1359 return success;
1360 }
1361
1362 int wxBitmap::GetHeight() const
1363 {
1364 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1365
1366 return M_BITMAPDATA->GetHeight();
1367 }
1368
1369 int wxBitmap::GetWidth() const
1370 {
1371 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1372
1373 return M_BITMAPDATA->GetWidth() ;
1374 }
1375
1376 int wxBitmap::GetDepth() const
1377 {
1378 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1379
1380 return M_BITMAPDATA->GetDepth();
1381 }
1382
1383 wxMask *wxBitmap::GetMask() const
1384 {
1385 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
1386
1387 return M_BITMAPDATA->m_bitmapMask;
1388 }
1389
1390 bool wxBitmap::HasAlpha() const
1391 {
1392 wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") );
1393
1394 return M_BITMAPDATA->HasAlpha() ;
1395 }
1396
1397 void wxBitmap::SetWidth(int w)
1398 {
1399 AllocExclusive();
1400 M_BITMAPDATA->SetWidth(w);
1401 }
1402
1403 void wxBitmap::SetHeight(int h)
1404 {
1405 AllocExclusive();
1406 M_BITMAPDATA->SetHeight(h);
1407 }
1408
1409 void wxBitmap::SetDepth(int d)
1410 {
1411 AllocExclusive();
1412 M_BITMAPDATA->SetDepth(d);
1413 }
1414
1415 void wxBitmap::SetOk(bool isOk)
1416 {
1417 AllocExclusive();
1418 M_BITMAPDATA->SetOk(isOk);
1419 }
1420
1421 #if wxUSE_PALETTE
1422 wxPalette *wxBitmap::GetPalette() const
1423 {
1424 wxCHECK_MSG( IsOk(), NULL, wxT("Invalid bitmap GetPalette()") );
1425
1426 return &M_BITMAPDATA->m_bitmapPalette;
1427 }
1428
1429 void wxBitmap::SetPalette(const wxPalette& palette)
1430 {
1431 AllocExclusive();
1432 M_BITMAPDATA->m_bitmapPalette = palette ;
1433 }
1434 #endif // wxUSE_PALETTE
1435
1436 void wxBitmap::SetMask(wxMask *mask)
1437 {
1438 AllocExclusive();
1439 // Remove existing mask if there is one.
1440 delete M_BITMAPDATA->m_bitmapMask;
1441
1442 M_BITMAPDATA->m_bitmapMask = mask ;
1443 }
1444
1445 WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1446 {
1447 wxUnusedVar(mask);
1448
1449 return WXHBITMAP(M_BITMAPDATA->GetBitmapContext());
1450 }
1451
1452 // ----------------------------------------------------------------------------
1453 // wxMask
1454 // ----------------------------------------------------------------------------
1455
1456 wxMask::wxMask()
1457 {
1458 Init() ;
1459 }
1460
1461 wxMask::wxMask(const wxMask &tocopy) : wxObject()
1462 {
1463 Init();
1464
1465 m_bytesPerRow = tocopy.m_bytesPerRow;
1466 m_width = tocopy.m_width;
1467 m_height = tocopy.m_height;
1468
1469 size_t size = m_bytesPerRow * m_height;
1470 unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size );
1471 unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData();
1472 memcpy( dest, source, size );
1473 m_memBuf.UngetWriteBuf( size ) ;
1474 RealizeNative() ;
1475 }
1476
1477 // Construct a mask from a bitmap and a colour indicating
1478 // the transparent area
1479 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
1480 {
1481 Init() ;
1482 Create( bitmap, colour );
1483 }
1484
1485 // Construct a mask from a mono bitmap (copies the bitmap).
1486 wxMask::wxMask( const wxBitmap& bitmap )
1487 {
1488 Init() ;
1489 Create( bitmap );
1490 }
1491
1492 // Construct a mask from a mono bitmap (copies the bitmap).
1493
1494 wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1495 {
1496 Init() ;
1497 Create( data, width , height , bytesPerRow );
1498 }
1499
1500 wxMask::~wxMask()
1501 {
1502 if ( m_maskBitmap )
1503 {
1504 CGContextRelease( (CGContextRef) m_maskBitmap );
1505 m_maskBitmap = NULL ;
1506 }
1507 }
1508
1509 void wxMask::Init()
1510 {
1511 m_width = m_height = m_bytesPerRow = 0 ;
1512 m_maskBitmap = NULL ;
1513 }
1514
1515 void *wxMask::GetRawAccess() const
1516 {
1517 return m_memBuf.GetData() ;
1518 }
1519
1520 // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1521 // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1522
1523 void wxMask::RealizeNative()
1524 {
1525 if ( m_maskBitmap )
1526 {
1527 CGContextRelease( (CGContextRef) m_maskBitmap );
1528 m_maskBitmap = NULL ;
1529 }
1530
1531 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
1532 // from MouseTracking sample :
1533 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1534 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1535
1536 m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace,
1537 kCGImageAlphaNone );
1538 CGColorSpaceRelease( colorspace );
1539 wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ;
1540 }
1541
1542 // Create a mask from a mono bitmap (copies the bitmap).
1543
1544 bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1545 {
1546 m_memBuf = data ;
1547 m_width = width ;
1548 m_height = height ;
1549 m_bytesPerRow = bytesPerRow ;
1550
1551 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1552
1553 RealizeNative() ;
1554
1555 return true ;
1556 }
1557
1558 // Create a mask from a mono bitmap (copies the bitmap).
1559 bool wxMask::Create(const wxBitmap& bitmap)
1560 {
1561 m_width = bitmap.GetWidth() ;
1562 m_height = bitmap.GetHeight() ;
1563 m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ;
1564
1565 size_t size = m_bytesPerRow * m_height ;
1566 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1567 wxASSERT( destdatabase != NULL ) ;
1568
1569 if ( destdatabase )
1570 {
1571 memset( destdatabase , 0 , size ) ;
1572 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1573
1574 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1575 {
1576 unsigned char *destdata = destdatabase ;
1577 unsigned char r, g, b;
1578
1579 for ( int x = 0 ; x < m_width ; ++x )
1580 {
1581 srcdata++ ;
1582 r = *srcdata++ ;
1583 g = *srcdata++ ;
1584 b = *srcdata++ ;
1585
1586 if ( ( r + g + b ) > 0x10 )
1587 *destdata++ = 0xFF ;
1588 else
1589 *destdata++ = 0x00 ;
1590 }
1591 }
1592 }
1593
1594 m_memBuf.UngetWriteBuf( size ) ;
1595 RealizeNative() ;
1596
1597 return true;
1598 }
1599
1600 // Create a mask from a bitmap and a colour indicating
1601 // the transparent area
1602 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1603 {
1604 m_width = bitmap.GetWidth() ;
1605 m_height = bitmap.GetHeight() ;
1606 m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ;
1607
1608 size_t size = m_bytesPerRow * m_height ;
1609 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1610 wxASSERT( destdatabase != NULL ) ;
1611
1612 memset( destdatabase , 0 , size ) ;
1613 unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ;
1614 size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow();
1615
1616 for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow)
1617 {
1618 unsigned char *srcdata = srcdatabase ;
1619 unsigned char *destdata = destdatabase ;
1620 unsigned char r, g, b;
1621
1622 for ( int x = 0 ; x < m_width ; ++x )
1623 {
1624 srcdata++ ;
1625 r = *srcdata++ ;
1626 g = *srcdata++ ;
1627 b = *srcdata++ ;
1628
1629 if ( colour == wxColour( r , g , b ) )
1630 *destdata++ = 0xFF ;
1631 else
1632 *destdata++ = 0x00 ;
1633 }
1634 }
1635
1636 m_memBuf.UngetWriteBuf( size ) ;
1637 RealizeNative() ;
1638
1639 return true;
1640 }
1641
1642 WXHBITMAP wxMask::GetHBITMAP() const
1643 {
1644 return m_maskBitmap ;
1645 }
1646
1647 // ----------------------------------------------------------------------------
1648 // Standard Handlers
1649 // ----------------------------------------------------------------------------
1650
1651 class WXDLLEXPORT wxBundleResourceHandler: public wxBitmapHandler
1652 {
1653 DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler)
1654
1655 public:
1656 inline wxBundleResourceHandler()
1657 {
1658 };
1659
1660 virtual bool LoadFile(wxBitmap *bitmap,
1661 const wxString& name,
1662 wxBitmapType type,
1663 int desiredWidth,
1664 int desiredHeight);
1665 };
1666
1667 IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler, wxBitmapHandler);
1668
1669 class WXDLLEXPORT wxPNGResourceHandler: public wxBundleResourceHandler
1670 {
1671 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler)
1672
1673 public:
1674 inline wxPNGResourceHandler()
1675 {
1676 SetName(wxT("PNG resource"));
1677 SetExtension("PNG");
1678 SetType(wxBITMAP_TYPE_PNG_RESOURCE);
1679 };
1680 };
1681
1682 IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler, wxBundleResourceHandler)
1683
1684 class WXDLLEXPORT wxJPEGResourceHandler: public wxBundleResourceHandler
1685 {
1686 DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler)
1687
1688 public:
1689 inline wxJPEGResourceHandler()
1690 {
1691 SetName(wxT("JPEG resource"));
1692 SetExtension("JPEG");
1693 SetType(wxBITMAP_TYPE_JPEG_RESOURCE);
1694 };
1695 };
1696
1697 IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler, wxBundleResourceHandler)
1698
1699 bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap,
1700 const wxString& name,
1701 wxBitmapType WXUNUSED(type),
1702 int WXUNUSED(desiredWidth),
1703 int WXUNUSED(desiredHeight))
1704 {
1705 wxString ext = GetExtension().Lower();
1706 wxCFStringRef resname(name);
1707 wxCFStringRef restype(ext);
1708
1709 wxCFRef<CFURLRef> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL));
1710
1711 if ( imageURL.get() != NULL )
1712 {
1713 // Create the data provider object
1714 wxCFRef<CGDataProviderRef> provider(CGDataProviderCreateWithURL (imageURL) );
1715 CGImageRef image = NULL;
1716
1717 if ( ext == "jpeg" )
1718 image = CGImageCreateWithJPEGDataProvider (provider, NULL, true,
1719 kCGRenderingIntentDefault);
1720 else if ( ext == "png" )
1721 image = CGImageCreateWithPNGDataProvider (provider, NULL, true,
1722 kCGRenderingIntentDefault);
1723 if ( image != NULL )
1724 {
1725 bitmap->Create(image);
1726 CGImageRelease(image);
1727 }
1728 }
1729
1730 return false ;
1731 }
1732
1733 void wxBitmap::InitStandardHandlers()
1734 {
1735 #if wxOSX_USE_COCOA_OR_CARBON
1736 AddHandler( new wxICONResourceHandler ) ;
1737 #endif
1738 AddHandler( new wxPNGResourceHandler );
1739 AddHandler( new wxJPEGResourceHandler );
1740 }
1741
1742 // ----------------------------------------------------------------------------
1743 // raw bitmap access support
1744 // ----------------------------------------------------------------------------
1745
1746 void *wxBitmap::GetRawData(wxPixelDataBase& data, int WXUNUSED(bpp))
1747 {
1748 if ( !IsOk() )
1749 // no bitmap, no data (raw or otherwise)
1750 return NULL;
1751
1752 data.m_width = GetWidth() ;
1753 data.m_height = GetHeight() ;
1754 data.m_stride = GetBitmapData()->GetBytesPerRow() ;
1755
1756 return BeginRawAccess() ;
1757 }
1758
1759 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase))
1760 {
1761 EndRawAccess() ;
1762 }
1763
1764 void wxBitmap::UseAlpha()
1765 {
1766 // remember that we are using alpha channel:
1767 // we'll need to create a proper mask in UngetRawData()
1768 M_BITMAPDATA->UseAlpha( true );
1769 }