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